From d99adb203b04fd998f72b5d7d9c2173c582cee56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Sun, 14 Jan 2024 00:07:01 +0100 Subject: [PATCH] v0.0.373 BuildInsertStatement --- exerr/data.go | 5 +++-- goextVersion.go | 4 ++-- sq/builder.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ sq/paginate.go | 4 ++-- 4 files changed, 56 insertions(+), 6 deletions(-) diff --git a/exerr/data.go b/exerr/data.go index 33e6649..3feab6e 100644 --- a/exerr/data.go +++ b/exerr/data.go @@ -48,8 +48,9 @@ var ( TypeMongoReflection = NewType("MONGO_REFLECTION", langext.Ptr(500)) TypeMongoInvalidOpt = NewType("MONGO_INVALIDOPT", langext.Ptr(500)) - TypeSQLQuery = NewType("SQL_QUERY", langext.Ptr(500)) - TypeSQLBuild = NewType("SQL_BUILD", langext.Ptr(500)) + 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) diff --git a/goextVersion.go b/goextVersion.go index 231f662..35eb8ee 100644 --- a/goextVersion.go +++ b/goextVersion.go @@ -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" diff --git a/sq/builder.go b/sq/builder.go index 3b1db75..c510225 100644 --- a/sq/builder.go +++ b/sq/builder.go @@ -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 +} diff --git a/sq/paginate.go b/sq/paginate.go index 9f25f2b..8bdfe99 100644 --- a/sq/paginate.go +++ b/sq/paginate.go @@ -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