v0.0.385 UpdateAndQuerySingle
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m29s

This commit is contained in:
Mike Schwörer 2024-02-09 15:40:09 +01:00
parent 1869ff3d75
commit ad24f6db44
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
2 changed files with 31 additions and 2 deletions

View File

@ -1,5 +1,5 @@
package goext
const GoextVersion = "0.0.384"
const GoextVersion = "0.0.385"
const GoextVersionTimestamp = "2024-02-09T15:20:46+0100"
const GoextVersionTimestamp = "2024-02-09T15:40:09+0100"

View File

@ -4,9 +4,11 @@ import (
"context"
"database/sql"
"errors"
"fmt"
"github.com/jmoiron/sqlx"
"gogs.mikescher.com/BlackForestBytes/goext/exerr"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"reflect"
)
type StructScanMode string
@ -89,6 +91,33 @@ func UpdateSingle[TData any](ctx context.Context, q Queryable, tableName string,
return sqlr, nil
}
func UpdateAndQuerySingle[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 = UpdateSingle[TData](ctx, q, tableName, v, idColumn)
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 QuerySingle[TData any](ctx context.Context, q Queryable, sql string, pp PP, mode StructScanMode, sec StructScanSafety) (TData, error) {
rows, err := q.Query(ctx, sql, pp)
if err != nil {