v0.0.358
Some checks failed
Build Docker and Deploy / Run goext test-suite (push) Failing after 1m22s
Some checks failed
Build Docker and Deploy / Run goext test-suite (push) Failing after 1m22s
This commit is contained in:
71
sq/builder.go
Normal file
71
sq/builder.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package sq
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/exerr"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func BuildUpdateStatement(q Queryable, tableName string, obj any, idColumn string) (string, PP, error) {
|
||||
rval := reflect.ValueOf(obj)
|
||||
rtyp := rval.Type()
|
||||
|
||||
params := PP{}
|
||||
|
||||
setClauses := make([]string, 0)
|
||||
|
||||
matchClause := ""
|
||||
|
||||
for i := 0; i < rtyp.NumField(); i++ {
|
||||
|
||||
rsfield := rtyp.Field(i)
|
||||
rvfield := rval.Field(i)
|
||||
|
||||
if !rsfield.IsExported() {
|
||||
continue
|
||||
}
|
||||
|
||||
columnName := rsfield.Tag.Get("db")
|
||||
if columnName == "" || columnName == "-" {
|
||||
continue
|
||||
}
|
||||
|
||||
if idColumn == columnName {
|
||||
idValue, err := convertValueToDB(q, rvfield.Interface())
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
|
||||
matchClause = fmt.Sprintf("(%s = :%s)", columnName, params.Add(idValue))
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
if rsfield.Type.Kind() == reflect.Ptr && rvfield.IsNil() {
|
||||
|
||||
setClauses = append(setClauses, fmt.Sprintf("%s = NULL", columnName))
|
||||
|
||||
} else {
|
||||
|
||||
val, err := convertValueToDB(q, rvfield.Interface())
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
|
||||
setClauses = append(setClauses, fmt.Sprintf("(%s = :%s)", columnName, params.Add(val)))
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if len(setClauses) == 0 {
|
||||
return "", nil, exerr.New(exerr.TypeSQLBuild, "no updates clauses found in object").Build()
|
||||
}
|
||||
|
||||
if matchClause == "" {
|
||||
return "", nil, exerr.New(exerr.TypeSQLBuild, "id column not found in object").Build()
|
||||
}
|
||||
|
||||
//goland:noinspection SqlNoDataSourceInspection
|
||||
return fmt.Sprintf("UPDATE %s SET %s WHERE %s", tableName, strings.Join(setClauses, ", "), matchClause), params, nil
|
||||
}
|
85
sq/builder_test.go
Normal file
85
sq/builder_test.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package sq
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/glebarez/go-sqlite"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/tst"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type dummyQueryable struct {
|
||||
}
|
||||
|
||||
func (d dummyQueryable) Exec(ctx context.Context, sql string, prep PP) (sql.Result, error) {
|
||||
return nil, errors.New("err")
|
||||
}
|
||||
|
||||
func (d dummyQueryable) Query(ctx context.Context, sql string, prep PP) (*sqlx.Rows, error) {
|
||||
return nil, errors.New("err")
|
||||
}
|
||||
|
||||
func (d dummyQueryable) ListConverter() []DBTypeConverter {
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestCreateUpdateStatement(t *testing.T) {
|
||||
|
||||
type request struct {
|
||||
ID string `json:"id" db:"id"`
|
||||
Timestamp int `json:"timestamp" db:"timestamp"`
|
||||
StrVal string `json:"strVal" db:"str_val"`
|
||||
FloatVal float64 `json:"floatVal" db:"float_val"`
|
||||
Dummy bool `json:"dummyBool" db:"dummy_bool"`
|
||||
JsonVal JsonObj `json:"jsonVal" db:"json_val"`
|
||||
}
|
||||
|
||||
sqlite.RegisterAsSQLITE3()
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
dbdir := t.TempDir()
|
||||
dbfile1 := filepath.Join(dbdir, langext.MustHexUUID()+".sqlite3")
|
||||
|
||||
url := fmt.Sprintf("file:%s?_pragma=journal_mode(%s)&_pragma=timeout(%d)&_pragma=foreign_keys(%s)&_pragma=busy_timeout(%d)", dbfile1, "DELETE", 1000, "true", 1000)
|
||||
|
||||
xdb := tst.Must(sqlx.Open("sqlite", url))(t)
|
||||
|
||||
db := NewDB(xdb)
|
||||
db.RegisterDefaultConverter()
|
||||
|
||||
_, err := db.Exec(ctx, "CREATE TABLE `requests` ( id TEXT NOT NULL, timestamp INTEGER NOT NULL, PRIMARY KEY (id) ) STRICT", PP{})
|
||||
tst.AssertNoErr(t, err)
|
||||
|
||||
sqlStr, pp, err := BuildUpdateStatement(db, "requests", request{
|
||||
ID: "9927",
|
||||
Timestamp: 12321,
|
||||
StrVal: "hello world",
|
||||
Dummy: true,
|
||||
FloatVal: 3.14159,
|
||||
JsonVal: JsonObj{
|
||||
"firs": 1,
|
||||
"second": true,
|
||||
},
|
||||
}, "id")
|
||||
|
||||
tst.AssertNoErr(t, err)
|
||||
|
||||
fmt.Printf("\n\n")
|
||||
|
||||
fmt.Printf("######## PP:\n")
|
||||
for k, v := range pp {
|
||||
fmt.Printf("['%s'] => %+v\n", k, v)
|
||||
}
|
||||
fmt.Printf("\n\n")
|
||||
|
||||
fmt.Printf("######## SQL:\n%+v\n\n", sqlStr)
|
||||
|
||||
fmt.Printf("\n\n")
|
||||
|
||||
}
|
12
sq/params.go
12
sq/params.go
@@ -1,5 +1,7 @@
|
||||
package sq
|
||||
|
||||
import "gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||
|
||||
type PP map[string]any
|
||||
|
||||
func Join(pps ...PP) PP {
|
||||
@@ -11,3 +13,13 @@ func Join(pps ...PP) PP {
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func (pp *PP) Add(v any) string {
|
||||
id := PPID()
|
||||
(*pp)[id] = v
|
||||
return id
|
||||
}
|
||||
|
||||
func PPID() string {
|
||||
return "p_" + langext.RandBase62(8)
|
||||
}
|
||||
|
@@ -3,8 +3,8 @@ package sq
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/glebarez/go-sqlite"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/mattn/go-sqlite3"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/rfctime"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/tst"
|
||||
@@ -20,18 +20,18 @@ func TestTypeConverter1(t *testing.T) {
|
||||
Timestamp time.Time `db:"timestamp"`
|
||||
}
|
||||
|
||||
sqlite.RegisterAsSQLITE3()
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
dbdir := t.TempDir()
|
||||
dbfile1 := filepath.Join(dbdir, langext.MustHexUUID()+".sqlite3")
|
||||
|
||||
sqlite3.Version() // ensure loaded
|
||||
|
||||
tst.AssertNoErr(t, os.MkdirAll(dbdir, os.ModePerm))
|
||||
|
||||
url := fmt.Sprintf("file:%s?_journal=%s&_timeout=%d&_fk=%s&_busy_timeout=%d", dbfile1, "DELETE", 1000, "true", 1000)
|
||||
url := fmt.Sprintf("file:%s?_pragma=journal_mode(%s)&_pragma=timeout(%d)&_pragma=foreign_keys(%s)&_pragma=busy_timeout(%d)", dbfile1, "DELETE", 1000, "true", 1000)
|
||||
|
||||
xdb := tst.Must(sqlx.Open("sqlite3", url))(t)
|
||||
xdb := tst.Must(sqlx.Open("sqlite", url))(t)
|
||||
|
||||
db := NewDB(xdb)
|
||||
db.RegisterDefaultConverter()
|
||||
@@ -47,23 +47,26 @@ func TestTypeConverter1(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestTypeConverter2(t *testing.T) {
|
||||
|
||||
sqlite.RegisterAsSQLITE3()
|
||||
|
||||
type RequestData struct {
|
||||
ID string `db:"id"`
|
||||
Timestamp rfctime.UnixMilliTime `db:"timestamp"`
|
||||
}
|
||||
|
||||
sqlite.RegisterAsSQLITE3()
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
dbdir := t.TempDir()
|
||||
dbfile1 := filepath.Join(dbdir, langext.MustHexUUID()+".sqlite3")
|
||||
|
||||
sqlite3.Version() // ensure loaded
|
||||
|
||||
tst.AssertNoErr(t, os.MkdirAll(dbdir, os.ModePerm))
|
||||
|
||||
url := fmt.Sprintf("file:%s?_journal=%s&_timeout=%d&_fk=%s&_busy_timeout=%d", dbfile1, "DELETE", 1000, "true", 1000)
|
||||
url := fmt.Sprintf("file:%s?_pragma=journal_mode(%s)&_pragma=timeout(%d)&_pragma=foreign_keys(%s)&_pragma=busy_timeout(%d)", dbfile1, "DELETE", 1000, "true", 1000)
|
||||
|
||||
xdb := tst.Must(sqlx.Open("sqlite3", url))(t)
|
||||
xdb := tst.Must(sqlx.Open("sqlite", url))(t)
|
||||
|
||||
db := NewDB(xdb)
|
||||
db.RegisterDefaultConverter()
|
||||
|
Reference in New Issue
Block a user