Skip to content

Commit 93e157a

Browse files
committed
Expose a MySQLConn interface for use with database/sql.Conn.Raw()
Based on the design of @methane We can later add a LoadLocalInfile() method instead of the Register...() functions. for issue go-sql-driver#1416
1 parent 564dee9 commit 93e157a

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

interface.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package mysql
2+
3+
import (
4+
"context"
5+
"database/sql"
6+
"fmt"
7+
"time"
8+
)
9+
10+
// MySQLConn exposes the usable methods on driverConn given to database/sql.Conn.Raw.
11+
type MySQLConn interface {
12+
// Prevent other modules from implementing this interface so we can keep adding methods.
13+
isMySQLConn()
14+
15+
// Location gets the Config.Loc of this connection. (This may differ from `time_zone` connection variable.)
16+
Location() *time.Location
17+
}
18+
19+
func (mc *mysqlConn) isMySQLConn() {
20+
}
21+
22+
func (mc *mysqlConn) Location() *time.Location {
23+
return mc.cfg.Loc
24+
}
25+
26+
var _ MySQLConn = &mysqlConn{}
27+
28+
func ExampleMySQLConn() {
29+
db, _ := sql.Open("mysql", "root:pw@unix(/tmp/mysql.sock)/myDatabase?parseTime=true&loc=Europe%2FAmsterdam")
30+
conn, _ := db.Conn(context.Background())
31+
var location *time.Location
32+
conn.Raw(func(dc any) error {
33+
mc := dc.(MySQLConn)
34+
location = mc.Location()
35+
return nil
36+
})
37+
fmt.Println(location)
38+
}

0 commit comments

Comments
 (0)