v0.0.385 UpdateAndQuerySingle
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m29s
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m29s
This commit is contained in:
parent
1869ff3d75
commit
ad24f6db44
@ -1,5 +1,5 @@
|
|||||||
package goext
|
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"
|
||||||
|
@ -4,9 +4,11 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
"gogs.mikescher.com/BlackForestBytes/goext/exerr"
|
"gogs.mikescher.com/BlackForestBytes/goext/exerr"
|
||||||
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||||
|
"reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
type StructScanMode string
|
type StructScanMode string
|
||||||
@ -89,6 +91,33 @@ func UpdateSingle[TData any](ctx context.Context, q Queryable, tableName string,
|
|||||||
return sqlr, nil
|
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) {
|
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)
|
rows, err := q.Query(ctx, sql, pp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user