v0.0.373 BuildInsertStatement
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m24s
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m24s
This commit is contained in:
parent
f1f91f4cfa
commit
d99adb203b
@ -50,6 +50,7 @@ var (
|
||||
|
||||
TypeSQLQuery = NewType("SQL_QUERY", langext.Ptr(500))
|
||||
TypeSQLBuild = NewType("SQL_BUILD", langext.Ptr(500))
|
||||
TypeSQLDecode = NewType("SQL_DECODE", langext.Ptr(500))
|
||||
|
||||
TypeWrap = NewType("Wrap", nil)
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
package goext
|
||||
|
||||
const GoextVersion = "0.0.372"
|
||||
const GoextVersion = "0.0.373"
|
||||
|
||||
const GoextVersionTimestamp = "2024-01-13T21:36:47+0100"
|
||||
const GoextVersionTimestamp = "2024-01-14T00:07:01+0100"
|
||||
|
@ -69,3 +69,52 @@ func BuildUpdateStatement(q Queryable, tableName string, obj any, idColumn strin
|
||||
//goland:noinspection SqlNoDataSourceInspection
|
||||
return fmt.Sprintf("UPDATE %s SET %s WHERE %s", tableName, strings.Join(setClauses, ", "), matchClause), params, nil
|
||||
}
|
||||
|
||||
func BuildInsertStatement(q Queryable, tableName string, obj any) (string, PP, error) {
|
||||
rval := reflect.ValueOf(obj)
|
||||
rtyp := rval.Type()
|
||||
|
||||
params := PP{}
|
||||
|
||||
fields := make([]string, 0)
|
||||
values := make([]string, 0)
|
||||
|
||||
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 rsfield.Type.Kind() == reflect.Ptr && rvfield.IsNil() {
|
||||
|
||||
fields = append(fields, columnName)
|
||||
values = append(fields, "NULL")
|
||||
|
||||
} else {
|
||||
|
||||
val, err := convertValueToDB(q, rvfield.Interface())
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
|
||||
fields = append(fields, columnName)
|
||||
values = append(fields, ":"+params.Add(val))
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if len(fields) == 0 {
|
||||
return "", nil, exerr.New(exerr.TypeSQLBuild, "no fields found in object").Build()
|
||||
}
|
||||
|
||||
//goland:noinspection SqlNoDataSourceInspection
|
||||
return fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s)", tableName, strings.Join(fields, ", "), values), params, nil
|
||||
}
|
||||
|
@ -75,7 +75,7 @@ func Paginate[TData any](ctx context.Context, q Queryable, table string, filter
|
||||
}
|
||||
|
||||
if !countRows.Next() {
|
||||
return nil, pag.Pagination{}, exerr.New(exerr.TypeSQLQuery, "SQL COUNT(*) query returned no rows").Str("table", table).Any("filter", filter).Build() //TODO TypeSQLDecode
|
||||
return nil, pag.Pagination{}, exerr.New(exerr.TypeSQLDecode, "SQL COUNT(*) query returned no rows").Str("table", table).Any("filter", filter).Build()
|
||||
}
|
||||
|
||||
var countRes int
|
||||
@ -113,7 +113,7 @@ func Count(ctx context.Context, q Queryable, table string, filter PaginateFilter
|
||||
}
|
||||
|
||||
if !countRows.Next() {
|
||||
return 0, exerr.New(exerr.TypeSQLQuery, "SQL COUNT(*) query returned no rows").Str("table", table).Any("filter", filter).Build() //TODO TypeSQLDecode
|
||||
return 0, exerr.New(exerr.TypeSQLDecode, "SQL COUNT(*) query returned no rows").Str("table", table).Any("filter", filter).Build()
|
||||
}
|
||||
|
||||
var countRes int
|
||||
|
Loading…
Reference in New Issue
Block a user