From 3c439ba4285e21ddb77aeefd399ef9fadb444512 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Fri, 9 Feb 2024 15:58:21 +0100 Subject: [PATCH] v0.0.386 InsertAndQuerySingle --- goextVersion.go | 4 ++-- sq/scanner.go | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/goextVersion.go b/goextVersion.go index d29167a..c5180b2 100644 --- a/goextVersion.go +++ b/goextVersion.go @@ -1,5 +1,5 @@ package goext -const GoextVersion = "0.0.385" +const GoextVersion = "0.0.386" -const GoextVersionTimestamp = "2024-02-09T15:40:09+0100" +const GoextVersionTimestamp = "2024-02-09T15:58:21+0100" diff --git a/sq/scanner.go b/sq/scanner.go index 267af53..97e61dd 100644 --- a/sq/scanner.go +++ b/sq/scanner.go @@ -40,6 +40,33 @@ func InsertSingle[TData any](ctx context.Context, q Queryable, tableName string, return sqlr, nil } +func InsertAndQuerySingle[TData any](ctx context.Context, q Queryable, tableName string, v TData, idColumn string, mode StructScanMode, sec StructScanSafety) (TData, error) { + + rval := reflect.ValueOf(v) + + idRVal := rval.FieldByName(idColumn) + if !idRVal.IsValid() || idRVal.IsZero() { + return *new(TData), fmt.Errorf("failed to find idColumn '%s' in %T", idColumn, v) + } + + idValue, err := convertValueToDB(q, idRVal.Interface()) + if err != nil { + return *new(TData), err + } + + _, err = InsertSingle[TData](ctx, q, tableName, v) + if err != nil { + return *new(TData), err + } + + pp := PP{} + + //goland:noinspection ALL + sqlstr := fmt.Sprintf("SELECT * FROM %s WHERE %s = :%s", tableName, idColumn, pp.Add(idValue)) + + return QuerySingle[TData](ctx, q, sqlstr, pp, mode, sec) +} + func InsertMultiple[TData any](ctx context.Context, q Queryable, tableName string, vArr []TData, maxBatch int) ([]sql.Result, error) { if len(vArr) == 0 {