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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Comment on lines +16 to +21
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

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{}
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.


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)
}