Expose a MySQLConn interface for use with database/sql.Conn.Raw()#1454
Expose a MySQLConn interface for use with database/sql.Conn.Raw()#1454Jille wants to merge 1 commit intogo-sql-driver:masterfrom
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.
move these two methods to connection.go
| "time" | ||
| ) | ||
|
|
||
| var _ MySQLConn = &mysqlConn{} |
There was a problem hiding this comment.
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.
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.
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.
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