Skip to content

Commit

Permalink
add global value convertor for mysql and sqlite go sdk (#1955)
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick <[email protected]>
  • Loading branch information
Patrick0308 authored Oct 28, 2023
1 parent 09c7e5c commit 8d9a771
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 12 deletions.
17 changes: 17 additions & 0 deletions sdk/go/internal/db/driver.go
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 3 additions & 0 deletions sdk/go/mysql/internals.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions sdk/go/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
16 changes: 4 additions & 12 deletions sdk/go/pg/pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions sdk/go/sqlite/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 8d9a771

Please sign in to comment.