diff --git a/go.mod b/go.mod index 4fbf124e02..e02349b9d1 100644 --- a/go.mod +++ b/go.mod @@ -19,7 +19,7 @@ require ( github.com/eclipse/paho.mqtt.golang v1.5.1 github.com/fatih/color v1.15.0 github.com/foxcpp/go-mockdns v0.0.0-20201212160233-ede2f9158d15 - github.com/go-sql-driver/mysql v1.5.0 + github.com/go-sql-driver/mysql v1.9.3 github.com/gofrs/uuid v3.3.0+incompatible github.com/golang/geo v0.0.0-20190916061304-5b978397cfec github.com/google/flatbuffers v25.9.23+incompatible @@ -62,6 +62,7 @@ require ( cloud.google.com/go/iam v1.2.2 // indirect cloud.google.com/go/longrunning v0.6.2 // indirect cloud.google.com/go/monitoring v1.21.2 // indirect + filippo.io/edwards25519 v1.1.0 // indirect github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect github.com/99designs/keyring v1.2.2 // indirect github.com/Azure/azure-sdk-for-go/sdk/internal v1.11.2 // indirect diff --git a/go.sum b/go.sum index 8ee1035e30..fb18e91ba9 100644 --- a/go.sum +++ b/go.sum @@ -40,6 +40,8 @@ cloud.google.com/go/storage v1.5.0/go.mod h1:tpKbwo567HUNpVclU5sGELwQWBDZ8gh0Zeo cloud.google.com/go/storage v1.43.0 h1:CcxnSohZwizt4LCzQHWvBf1/kvtHUn7gk9QERXPyXFs= cloud.google.com/go/storage v1.43.0/go.mod h1:ajvxEa7WmZS1PxvKRq4bq0tFT3vMd502JwstCcYv0Q0= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= +filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= +filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 h1:/vQbFIOMbk2FiG/kXiLl8BRyzTWDw7gX/Hz7Dd5eDMs= github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4/go.mod h1:hN7oaIRCjzsZ2dE+yG5k+rsdt3qcwykqK6HVGcKwsw4= github.com/99designs/keyring v1.2.2 h1:pZd3neh/EmUzWONb35LxQfvuY7kiSXAq3HQd97+XBn0= @@ -197,8 +199,8 @@ github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg= github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk= -github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs= -github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= +github.com/go-sql-driver/mysql v1.9.3 h1:U/N249h2WzJ3Ukj8SowVFjdtZKfu9vlLZxjPXV1aweo= +github.com/go-sql-driver/mysql v1.9.3/go.mod h1:qn46aNg1333BRMNU69Lq93t8du/dwxI64Gl8i5p1WMU= github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4= github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 h1:ZpnhV/YsD2/4cESfV5+Hoeu/iUR3ruzNvZ+yQfO03a0= diff --git a/stdlib/sql/from_private_test.go b/stdlib/sql/from_private_test.go index a2cef22a91..f7c76c6ffb 100644 --- a/stdlib/sql/from_private_test.go +++ b/stdlib/sql/from_private_test.go @@ -249,6 +249,30 @@ func TestMssqlOpenFunctionDialer(t *testing.T) { } } +func TestMysqlOpenFunctionDialer(t *testing.T) { + expectErr := errors.New("test dial error") + deps := mockDeps{ + Deps: flux.NewDefaultDependencies(), + dialer: &mockDialer{err: expectErr}, + } + + openFn := mysqlOpenFunction("username:password@tcp(localhost:3306)/dbname") + db, err := openFn(deps) + if err != nil { + t.Fatalf("unexpected error from open function: %v", err) + } + defer db.Close() + + // Ping triggers a real connection attempt, which will use our mock dialer. + err = db.Ping() + if err == nil { + t.Fatal("expected error from Ping, got nil") + } + if !errors.Is(err, expectErr) { + t.Fatalf("expected error %q, got: %v", expectErr, err) + } +} + func TestFromSqliteUrlValidation(t *testing.T) { testCases := executetest.SourceUrlValidationTestCases{ { diff --git a/stdlib/sql/mysql.go b/stdlib/sql/mysql.go index bf1b1b5c61..636874c769 100644 --- a/stdlib/sql/mysql.go +++ b/stdlib/sql/mysql.go @@ -7,6 +7,7 @@ import ( "strings" "time" + "github.com/go-sql-driver/mysql" "github.com/influxdata/flux" "github.com/influxdata/flux/codes" "github.com/influxdata/flux/execute" @@ -197,3 +198,22 @@ func MysqlColumnTranslateFunc() translationFunc { func mysqlQuoteIdent(name string) string { return fmt.Sprintf("`%s`", strings.ReplaceAll(name, "`", "``")) } + +func mysqlOpenFunction(dataSourceName string) openFunc { + return func(deps flux.Dependencies) (*sql.DB, error) { + cfg, err := mysql.ParseDSN(dataSourceName) + if err != nil { + return nil, err + } + dialer, err := deps.Dialer() + if err != nil { + return nil, err + } + cfg.DialFunc = dialer.DialContext + connector, err := mysql.NewConnector(cfg) + if err != nil { + return nil, err + } + return sql.OpenDB(connector), nil + } +} diff --git a/stdlib/sql/open.go b/stdlib/sql/open.go index e227e44b52..a29ea63ad2 100644 --- a/stdlib/sql/open.go +++ b/stdlib/sql/open.go @@ -40,6 +40,8 @@ func getOpenFunc(driverName, dataSourceName string) openFunc { switch driverName { case "mssql", "sqlserver": return mssqlOpenFunction(dataSourceName) + case "mysql": + return mysqlOpenFunction(dataSourceName) case "postgres": return postgresOpenFunction(dataSourceName) default: