Skip to content

Commit

Permalink
Expose a MySQLConn interface for use with database/sql.Conn.Raw()
Browse files Browse the repository at this point in the history
Based on the design of @methane

We can later add a LoadLocalInfile() method instead of the Register...()
functions.

for issue #1416
  • Loading branch information
Jille committed Jun 26, 2023
1 parent 564dee9 commit 91a33fb
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Jeffrey Charles <jeffreycharles at gmail.com>
Jerome Meyer <jxmeyer at gmail.com>
Jiajia Zhong <zhong2plus at gmail.com>
Jian Zhen <zhenjl at gmail.com>
Jille Timmermans <jille at quis.cx>
Joshua Prunier <joshua.prunier at gmail.com>
Julien Lefevre <julien.lefevr at gmail.com>
Julien Schmidt <go-sql-driver at julienschmidt.com>
Expand Down
21 changes: 21 additions & 0 deletions interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package mysql

import (
"time"
)

// MySQLConn exposes the usable methods on driverConn given to database/sql.Conn.Raw.
type MySQLConn interface {
// Prevent other modules from implementing this interface so we can keep adding methods.
isMySQLConn()

// Location gets the Config.Loc of this connection. (This may differ from `time_zone` connection variable.)
Location() *time.Location
}

func (mc *mysqlConn) isMySQLConn() {
}

func (mc *mysqlConn) Location() *time.Location {
return mc.cfg.Loc
}
22 changes: 22 additions & 0 deletions interface_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package mysql

import (
"context"
"database/sql"
"fmt"
"time"
)

var _ MySQLConn = &mysqlConn{}

func ExampleMySQLConn() {
db, _ := sql.Open("mysql", "root:pw@unix(/tmp/mysql.sock)/myDatabase?parseTime=true&loc=Europe%2FAmsterdam")
conn, _ := db.Conn(context.Background())
var location *time.Location
conn.Raw(func(dc any) error {
mc := dc.(MySQLConn)
location = mc.Location()
return nil
})
fmt.Println(location)
}

0 comments on commit 91a33fb

Please sign in to comment.