Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Jille
Copy link

@Jille Jille commented Jun 26, 2023

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

Coverage Status

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

@dolmen
Copy link
Contributor

dolmen commented Jun 26, 2023

@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
Copy link
Author

Jille commented Jun 26, 2023

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 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?

interface.go Outdated Show resolved Hide resolved
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
Copy link
Author

Jille commented Aug 15, 2023

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.

@methane methane 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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants