goext/sq/scanner.go

214 lines
4.3 KiB
Go
Raw Normal View History

2022-12-11 03:12:02 +01:00
package sq
import (
2023-05-28 19:41:24 +02:00
"context"
2022-12-11 03:12:02 +01:00
"database/sql"
"errors"
2023-05-28 19:41:24 +02:00
"fmt"
2022-12-11 03:12:02 +01:00
"github.com/jmoiron/sqlx"
2023-05-28 19:41:24 +02:00
"reflect"
"strings"
2022-12-11 03:12:02 +01:00
)
2022-12-22 15:49:10 +01:00
type StructScanMode string
const (
SModeFast StructScanMode = "FAST"
SModeExtended StructScanMode = "EXTENDED"
)
type StructScanSafety string
const (
2023-05-28 19:41:24 +02:00
Safe StructScanSafety = "SAFE" // return error for missing fields
Unsafe StructScanSafety = "UNSAFE" // ignore missing fields
2022-12-22 15:49:10 +01:00
)
2023-05-28 19:41:24 +02:00
func InsertSingle[TData any](ctx context.Context, q Queryable, tableName string, v TData) (sql.Result, error) {
rval := reflect.ValueOf(v)
rtyp := rval.Type()
columns := make([]string, 0)
params := make([]string, 0)
pp := PP{}
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
}
paramkey := fmt.Sprintf("_%s", columnName)
columns = append(columns, "\""+columnName+"\"")
params = append(params, ":"+paramkey)
pp[paramkey] = rvfield.Interface()
}
sqlstr := fmt.Sprintf("INSERT"+" INTO \"%s\" (%s) VALUES (%s)", tableName, strings.Join(columns, ", "), strings.Join(params, ", "))
sqlr, err := q.Exec(ctx, sqlstr, pp)
if err != nil {
return nil, err
}
return sqlr, nil
}
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 {
return *new(TData), err
}
data, err := ScanSingle[TData](rows, mode, sec, true)
if err != nil {
return *new(TData), err
}
return data, nil
}
func QueryAll[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 {
return nil, err
}
data, err := ScanAll[TData](rows, mode, sec, true)
if err != nil {
return nil, err
}
return data, nil
}
2022-12-22 15:49:10 +01:00
func ScanSingle[TData any](rows *sqlx.Rows, mode StructScanMode, sec StructScanSafety, close bool) (TData, error) {
2022-12-11 03:12:02 +01:00
if rows.Next() {
2022-12-22 15:49:10 +01:00
var strscan *StructScanner
if sec == Safe {
strscan = NewStructScanner(rows, false)
var data TData
err := strscan.Start(&data)
if err != nil {
return *new(TData), err
}
} else if sec == Unsafe {
strscan = NewStructScanner(rows, true)
var data TData
err := strscan.Start(&data)
if err != nil {
return *new(TData), err
}
} else {
return *new(TData), errors.New("unknown value for <sec>")
}
2022-12-11 03:12:02 +01:00
var data TData
2022-12-22 15:49:10 +01:00
if mode == SModeFast {
err := strscan.StructScanBase(&data)
if err != nil {
return *new(TData), err
}
} else if mode == SModeExtended {
err := strscan.StructScanExt(&data)
if err != nil {
return *new(TData), err
}
} else {
return *new(TData), errors.New("unknown value for <mode>")
2022-12-11 03:12:02 +01:00
}
2022-12-22 15:49:10 +01:00
2022-12-11 03:12:02 +01:00
if rows.Next() {
2022-12-22 15:49:10 +01:00
if close {
_ = rows.Close()
}
return *new(TData), errors.New("sql returned more than one row")
2022-12-11 03:12:02 +01:00
}
2022-12-22 15:49:10 +01:00
2022-12-11 03:12:02 +01:00
if close {
2022-12-22 15:49:10 +01:00
err := rows.Close()
2022-12-11 03:12:02 +01:00
if err != nil {
return *new(TData), err
}
}
2022-12-22 15:49:10 +01:00
2022-12-22 15:55:32 +01:00
if err := rows.Err(); err != nil {
return *new(TData), err
}
2022-12-11 03:12:02 +01:00
return data, nil
2022-12-22 15:49:10 +01:00
2022-12-11 03:12:02 +01:00
} else {
if close {
_ = rows.Close()
}
return *new(TData), sql.ErrNoRows
}
}
2022-12-22 15:49:10 +01:00
func ScanAll[TData any](rows *sqlx.Rows, mode StructScanMode, sec StructScanSafety, close bool) ([]TData, error) {
var strscan *StructScanner
if sec == Safe {
strscan = NewStructScanner(rows, false)
2022-12-11 03:12:02 +01:00
var data TData
2022-12-22 15:49:10 +01:00
err := strscan.Start(&data)
2022-12-11 03:12:02 +01:00
if err != nil {
return nil, err
}
2022-12-22 15:49:10 +01:00
} else if sec == Unsafe {
strscan = NewStructScanner(rows, true)
var data TData
err := strscan.Start(&data)
if err != nil {
return nil, err
}
} else {
return nil, errors.New("unknown value for <sec>")
}
res := make([]TData, 0)
for rows.Next() {
if mode == SModeFast {
var data TData
err := strscan.StructScanBase(&data)
if err != nil {
return nil, err
}
res = append(res, data)
} else if mode == SModeExtended {
var data TData
err := strscan.StructScanExt(&data)
if err != nil {
return nil, err
}
res = append(res, data)
} else {
return nil, errors.New("unknown value for <mode>")
}
2022-12-11 03:12:02 +01:00
}
if close {
2022-12-22 15:49:10 +01:00
err := strscan.rows.Close()
2022-12-11 03:12:02 +01:00
if err != nil {
return nil, err
}
}
2022-12-22 15:55:32 +01:00
if err := rows.Err(); err != nil {
return nil, err
}
2022-12-11 03:12:02 +01:00
return res, nil
}