diff --git a/AUTHORS b/AUTHORS index 7e4fac5a1..be53cbce3 100644 --- a/AUTHORS +++ b/AUTHORS @@ -54,6 +54,7 @@ Jeffrey Charles Jerome Meyer Jiajia Zhong Jian Zhen +Jille Timmermans Joshua Prunier Julien Lefevre Julien Schmidt diff --git a/interface.go b/interface.go new file mode 100644 index 000000000..86bccc7f6 --- /dev/null +++ b/interface.go @@ -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 +} diff --git a/interface_test.go b/interface_test.go new file mode 100644 index 000000000..e799ee63e --- /dev/null +++ b/interface_test.go @@ -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) +}