-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose a MySQLConn interface for use with database/sql.Conn.Raw() #1454
base: master
Are you sure you want to change the base?
Conversation
@Jille You don't need to go through access to private data to access the var now time.Time
// Force a parametrized query so the driver gets a typed value, not a string processed because of parseTime option
_ = conn.QueryRowContext(context.TODO(), "SELECT NOW() FROM dual WHERE 1=?", int64(1)).Scan(&now)
fmt.Println(now.Location()) However it is misleading if the DSN has not been set correctly to ensure that the server side |
Thanks for the quick reply @dolmen I'm writing a library that can encode values to TSV format for LOAD DATA LOCAL INFILE. For that I want to have the same semantics as regular query encoding to avoid surprising my users with strange differences between encoders. https://github.com/go-sql-driver/mysql/blob/master/packets.go#L1184 uses cfg.Loc, so I want to encode using cfg.Loc too. I want to be exactly the same, even if someone does strange things with their I don't understand what the mistake is. In my example I want the time_zone used to encode values, and I'm getting that, right? |
Based on the design of @methane We can later add a LoadLocalInfile() method instead of the Register...() functions. for issue go-sql-driver#1416
What is the status of this? |
func (mc *mysqlConn) isMySQLConn() { | ||
} | ||
|
||
func (mc *mysqlConn) Location() *time.Location { | ||
return mc.cfg.Loc | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move these two methods to connection.go
"time" | ||
) | ||
|
||
var _ MySQLConn = &mysqlConn{} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move this to connection.go too.
interface.go
Outdated
var _ MySQLConn = &mysqlConn{} | ||
|
||
func ExampleMySQLConn() { | ||
db, _ := sql.Open("mysql", "root:pw@unix(/tmp/mysql.sock)/myDatabase?parseTime=true&loc=Europe%2FAmsterdam") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
time_zone=%27Europe%2FAmsterdam%27
must also be set. If not, the behaviour depends on the server setting.
interface.go
Outdated
location = mc.Location() | ||
return nil | ||
}) | ||
fmt.Println(location) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also check this:
var now time.Time
// Force a parametrized query so the driver gets a typed value, not a string processed because of parseTime option
_ = conn.QueryRowContext(context.TODO(), "SELECT NOW() FROM dual WHERE 1=?", int64(1)).Scan(&now)
fmt.Println(now.Location())
And compare location
to now.Location()
.
interface.go
Outdated
|
||
func ExampleMySQLConn() { | ||
db, _ := sql.Open("mysql", "root:pw@unix(/tmp/mysql.sock)/myDatabase?parseTime=true&loc=Europe%2FAmsterdam") | ||
conn, _ := db.Conn(context.Background()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add this code below:
var tz string
_ = conn.QueryRowContext(context.TODO(), "SELECT @@time_zone").Scan(&tz)
fmt.Println("time_zone:", tz)
Based on the design of @methane
We can later add a LoadLocalInfile() method instead of the Register...() functions.
for issue #1416
Description
Please explain the changes you made here.
Checklist