diff --git a/sdk/go/internal/db/driver.go b/sdk/go/internal/db/driver.go new file mode 100644 index 000000000..2b3ab7571 --- /dev/null +++ b/sdk/go/internal/db/driver.go @@ -0,0 +1,17 @@ +package db + +import ( + "database/sql/driver" +) + +// GlobalParameterConverter is a global valueConverter instance to convert parameters. +var GlobalParameterConverter = &valueConverter{} + +var _ driver.ValueConverter = (*valueConverter)(nil) + +// valueConverter is a no-op value converter. +type valueConverter struct{} + +func (c *valueConverter) ConvertValue(v any) (driver.Value, error) { + return driver.Value(v), nil +} diff --git a/sdk/go/mysql/internals.go b/sdk/go/mysql/internals.go index 7fd3767e8..9b0333107 100644 --- a/sdk/go/mysql/internals.go +++ b/sdk/go/mysql/internals.go @@ -216,6 +216,9 @@ func toOutboundMysqlParameterValue(x any) C.outbound_mysql_parameter_value_t { case int64: *(*C.int64_t)(unsafe.Pointer(&ret.val)) = int64(v) ret.tag = paramValueInt64 + case int: + *(*C.int64_t)(unsafe.Pointer(&ret.val)) = int64(v) + ret.tag = paramValueInt64 case uint8: *(*C.uint8_t)(unsafe.Pointer(&ret.val)) = uint8(v) ret.tag = paramValueUint8 diff --git a/sdk/go/mysql/mysql.go b/sdk/go/mysql/mysql.go index 0dbfed850..0fe87a448 100644 --- a/sdk/go/mysql/mysql.go +++ b/sdk/go/mysql/mysql.go @@ -7,6 +7,8 @@ import ( "errors" "io" "reflect" + + spindb "github.com/fermyon/spin/sdk/go/internal/db" ) // Open returns a new connection to the database. @@ -60,6 +62,7 @@ type stmt struct { } var _ driver.Stmt = (*stmt)(nil) +var _ driver.ColumnConverter = (*stmt)(nil) // Close closes the statement. func (s *stmt) Close() error { @@ -92,6 +95,11 @@ func (s *stmt) Exec(args []driver.Value) (driver.Result, error) { return &result{}, err } +// ColumnConverter returns GlobalParameterConverter to prevent using driver.DefaultParameterConverter. +func (s *stmt) ColumnConverter(_ int) driver.ValueConverter { + return spindb.GlobalParameterConverter +} + type result struct{} func (r result) LastInsertId() (int64, error) { diff --git a/sdk/go/pg/pg.go b/sdk/go/pg/pg.go index 8e79a8293..0c7fbf58d 100644 --- a/sdk/go/pg/pg.go +++ b/sdk/go/pg/pg.go @@ -7,10 +7,9 @@ import ( "errors" "io" "reflect" -) -// globalValueConv a valueConv instance -var globalValueConv = &valueConv{} + spindb "github.com/fermyon/spin/sdk/go/internal/db" +) // Open returns a new connection to the database. func Open(address string) *sql.DB { @@ -96,16 +95,9 @@ func (s *stmt) Exec(args []driver.Value) (driver.Result, error) { return &result{rowsAffected: int64(n)}, err } -// ColumnConverter return globalValueConv to don't use driver.DefaultParameterConverter +// ColumnConverter returns GlobalParameterConverter to prevent using driver.DefaultParameterConverter. func (s *stmt) ColumnConverter(_ int) driver.ValueConverter { - return globalValueConv -} - -// valueConv a convertor not convert value -type valueConv struct{} - -func (c *valueConv) ConvertValue(v any) (driver.Value, error) { - return driver.Value(v), nil + return spindb.GlobalParameterConverter } type result struct { diff --git a/sdk/go/sqlite/sqlite.go b/sdk/go/sqlite/sqlite.go index 6d0211f0d..6684d464c 100644 --- a/sdk/go/sqlite/sqlite.go +++ b/sdk/go/sqlite/sqlite.go @@ -6,6 +6,8 @@ import ( "database/sql/driver" "errors" "io" + + spindb "github.com/fermyon/spin/sdk/go/internal/db" ) // Open returns a new connection to the database. @@ -135,6 +137,7 @@ type stmt struct { } var _ driver.Stmt = (*stmt)(nil) +var _ driver.ColumnConverter = (*stmt)(nil) // Close closes the statement. func (s *stmt) Close() error { @@ -167,6 +170,11 @@ func (s *stmt) Exec(args []driver.Value) (driver.Result, error) { return &result{}, err } +// ColumnConverter returns GlobalParameterConverter to prevent using driver.DefaultParameterConverter. +func (s *stmt) ColumnConverter(_ int) driver.ValueConverter { + return spindb.GlobalParameterConverter +} + type result struct{} func (r result) LastInsertId() (int64, error) {