Conversation

Jille

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

  • Code compiles correctly
  • Created tests which fail without the change (if possible)
  • All tests passing
  • Extended the README / documentation, if necessary
  • Added myself / the copyright holder to the AUTHORS file

@coveralls

Coverage Status

coverage: 81.727% (-0.3%) from 82.06% when pulling 28853e6 on Jille:conninterface into 564dee9 on go-sql-driver:master.

@dolmen

@Jille You don't need to go through access to private data to access the loc parameter. You can get it this way:

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 time_zone setting is set accordingly. In fact you made that mistake in your example (you rely on the server-defined value which is dangerous for a generic tool).

@Jille

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://.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 time_zone setting.

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
@Jille

What is the status of this?

Comment on lines +16 to +21
func (mc *mysqlConn) isMySQLConn() {
}

func (mc *mysqlConn) Location() *time.Location {
return mc.cfg.Loc
}
Copy link
Member

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{}
Copy link
Member

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.

@methanemethane added this to the v1.9.0 milestone Mar 6, 2024
interface.go Outdated
var _ MySQLConn = &mysqlConn{}

func ExampleMySQLConn() {
db, _ := sql.Open("mysql", "root:pw@unix(/tmp/mysql.sock)/myDatabase?parseTime=true&loc=Europe%2FAmsterdam")
Copy link
Contributor

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)
Copy link
Contributor

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())
Copy link
Contributor

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)

@methanemethane modified the milestones: v1.9.0, v1.10.0 Feb 20, 2025
Sign up for free to join this conversation on . Already have an account? Sign in to comment
None yet
None yet

Successfully merging this pull request may close these issues.