fork github.com/doug-martin
This commit is contained in:
@@ -0,0 +1,253 @@
|
||||
package exec
|
||||
|
||||
import (
|
||||
"context"
|
||||
gsql "database/sql"
|
||||
"reflect"
|
||||
|
||||
"git.fsdpf.net/go/db/v2/internal/errors"
|
||||
"git.fsdpf.net/go/db/v2/internal/util"
|
||||
)
|
||||
|
||||
type (
|
||||
QueryExecutor struct {
|
||||
de DbExecutor
|
||||
err error
|
||||
query string
|
||||
args []interface{}
|
||||
}
|
||||
)
|
||||
|
||||
var (
|
||||
errUnsupportedScanStructType = errors.New("type must be a pointer to a struct when scanning into a struct")
|
||||
errUnsupportedScanStructsType = errors.New("type must be a pointer to a slice when scanning into structs")
|
||||
errUnsupportedScanValsType = errors.New("type must be a pointer to a slice when scanning into vals")
|
||||
errScanValPointer = errors.New("type must be a pointer when scanning into val")
|
||||
errScanValNonSlice = errors.New("type cannot be a pointer to a slice when scanning into val")
|
||||
)
|
||||
|
||||
func newQueryExecutor(de DbExecutor, err error, query string, args ...interface{}) QueryExecutor {
|
||||
return QueryExecutor{de: de, err: err, query: query, args: args}
|
||||
}
|
||||
|
||||
func (q QueryExecutor) ToSQL() (sql string, args []interface{}, err error) {
|
||||
return q.query, q.args, q.err
|
||||
}
|
||||
|
||||
func (q QueryExecutor) Exec() (gsql.Result, error) {
|
||||
return q.ExecContext(context.Background())
|
||||
}
|
||||
|
||||
func (q QueryExecutor) ExecContext(ctx context.Context) (gsql.Result, error) {
|
||||
if q.err != nil {
|
||||
return nil, q.err
|
||||
}
|
||||
return q.de.ExecContext(ctx, q.query, q.args...)
|
||||
}
|
||||
|
||||
func (q QueryExecutor) Query() (*gsql.Rows, error) {
|
||||
return q.QueryContext(context.Background())
|
||||
}
|
||||
|
||||
func (q QueryExecutor) QueryContext(ctx context.Context) (*gsql.Rows, error) {
|
||||
if q.err != nil {
|
||||
return nil, q.err
|
||||
}
|
||||
return q.de.QueryContext(ctx, q.query, q.args...)
|
||||
}
|
||||
|
||||
// This will execute the SQL and append results to the slice
|
||||
//
|
||||
// var myStructs []MyStruct
|
||||
// if err := db.From("test").ScanStructs(&myStructs); err != nil{
|
||||
// panic(err.Error()
|
||||
// }
|
||||
// //use your structs
|
||||
//
|
||||
// i: A pointer to a slice of structs.
|
||||
func (q QueryExecutor) ScanStructs(i interface{}) error {
|
||||
return q.ScanStructsContext(context.Background(), i)
|
||||
}
|
||||
|
||||
// This will execute the SQL and append results to the slice
|
||||
//
|
||||
// var myStructs []MyStruct
|
||||
// if err := db.From("test").ScanStructsContext(ctx, &myStructs); err != nil{
|
||||
// panic(err.Error()
|
||||
// }
|
||||
// //use your structs
|
||||
//
|
||||
// i: A pointer to a slice of structs.
|
||||
func (q QueryExecutor) ScanStructsContext(ctx context.Context, i interface{}) error {
|
||||
scanner, err := q.ScannerContext(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = scanner.Close() }()
|
||||
return scanner.ScanStructs(i)
|
||||
}
|
||||
|
||||
// This will execute the SQL and fill out the struct with the fields returned.
|
||||
// This method returns a boolean value that is false if no record was found
|
||||
//
|
||||
// var myStruct MyStruct
|
||||
// found, err := db.From("test").Limit(1).ScanStruct(&myStruct)
|
||||
// if err != nil{
|
||||
// panic(err.Error()
|
||||
// }
|
||||
// if !found{
|
||||
// fmt.Println("NOT FOUND")
|
||||
// }
|
||||
//
|
||||
// i: A pointer to a struct
|
||||
func (q QueryExecutor) ScanStruct(i interface{}) (bool, error) {
|
||||
return q.ScanStructContext(context.Background(), i)
|
||||
}
|
||||
|
||||
// This will execute the SQL and fill out the struct with the fields returned.
|
||||
// This method returns a boolean value that is false if no record was found
|
||||
//
|
||||
// var myStruct MyStruct
|
||||
// found, err := db.From("test").Limit(1).ScanStructContext(ctx, &myStruct)
|
||||
// if err != nil{
|
||||
// panic(err.Error()
|
||||
// }
|
||||
// if !found{
|
||||
// fmt.Println("NOT FOUND")
|
||||
// }
|
||||
//
|
||||
// i: A pointer to a struct
|
||||
func (q QueryExecutor) ScanStructContext(ctx context.Context, i interface{}) (bool, error) {
|
||||
val := reflect.ValueOf(i)
|
||||
if !util.IsPointer(val.Kind()) {
|
||||
return false, errUnsupportedScanStructType
|
||||
}
|
||||
val = reflect.Indirect(val)
|
||||
if !util.IsStruct(val.Kind()) {
|
||||
return false, errUnsupportedScanStructType
|
||||
}
|
||||
|
||||
scanner, err := q.ScannerContext(ctx)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
defer func() { _ = scanner.Close() }()
|
||||
|
||||
if scanner.Next() {
|
||||
err = scanner.ScanStruct(i)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return true, scanner.Err()
|
||||
}
|
||||
|
||||
return false, scanner.Err()
|
||||
}
|
||||
|
||||
// This will execute the SQL and append results to the slice.
|
||||
//
|
||||
// var ids []uint32
|
||||
// if err := db.From("test").Select("id").ScanVals(&ids); err != nil{
|
||||
// panic(err.Error()
|
||||
// }
|
||||
//
|
||||
// i: Takes a pointer to a slice of primitive values.
|
||||
func (q QueryExecutor) ScanVals(i interface{}) error {
|
||||
return q.ScanValsContext(context.Background(), i)
|
||||
}
|
||||
|
||||
// This will execute the SQL and append results to the slice.
|
||||
//
|
||||
// var ids []uint32
|
||||
// if err := db.From("test").Select("id").ScanValsContext(ctx, &ids); err != nil{
|
||||
// panic(err.Error()
|
||||
// }
|
||||
//
|
||||
// i: Takes a pointer to a slice of primitive values.
|
||||
func (q QueryExecutor) ScanValsContext(ctx context.Context, i interface{}) error {
|
||||
scanner, err := q.ScannerContext(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = scanner.Close() }()
|
||||
return scanner.ScanVals(i)
|
||||
}
|
||||
|
||||
// This will execute the SQL and set the value of the primitive. This method will return false if no record is found.
|
||||
//
|
||||
// var id uint32
|
||||
// found, err := db.From("test").Select("id").Limit(1).ScanVal(&id)
|
||||
// if err != nil{
|
||||
// panic(err.Error()
|
||||
// }
|
||||
// if !found{
|
||||
// fmt.Println("NOT FOUND")
|
||||
// }
|
||||
//
|
||||
// i: Takes a pointer to a primitive value.
|
||||
func (q QueryExecutor) ScanVal(i interface{}) (bool, error) {
|
||||
return q.ScanValContext(context.Background(), i)
|
||||
}
|
||||
|
||||
// This will execute the SQL and set the value of the primitive. This method will return false if no record is found.
|
||||
//
|
||||
// var id uint32
|
||||
// found, err := db.From("test").Select("id").Limit(1).ScanValContext(ctx, &id)
|
||||
// if err != nil{
|
||||
// panic(err.Error()
|
||||
// }
|
||||
// if !found{
|
||||
// fmt.Println("NOT FOUND")
|
||||
// }
|
||||
//
|
||||
// i: Takes a pointer to a primitive value.
|
||||
func (q QueryExecutor) ScanValContext(ctx context.Context, i interface{}) (bool, error) {
|
||||
val := reflect.ValueOf(i)
|
||||
if !util.IsPointer(val.Kind()) {
|
||||
return false, errScanValPointer
|
||||
}
|
||||
val = reflect.Indirect(val)
|
||||
if util.IsSlice(val.Kind()) {
|
||||
switch i.(type) {
|
||||
case *gsql.RawBytes: // do nothing
|
||||
case *[]byte: // do nothing
|
||||
case gsql.Scanner: // do nothing
|
||||
default:
|
||||
return false, errScanValNonSlice
|
||||
}
|
||||
}
|
||||
|
||||
scanner, err := q.ScannerContext(ctx)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
defer func() { _ = scanner.Close() }()
|
||||
|
||||
if scanner.Next() {
|
||||
err = scanner.ScanVal(i)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return true, scanner.Err()
|
||||
}
|
||||
|
||||
return false, scanner.Err()
|
||||
}
|
||||
|
||||
// Scanner will return a Scanner that can be used for manually scanning rows.
|
||||
func (q QueryExecutor) Scanner() (Scanner, error) {
|
||||
return q.ScannerContext(context.Background())
|
||||
}
|
||||
|
||||
// ScannerContext will return a Scanner that can be used for manually scanning rows.
|
||||
func (q QueryExecutor) ScannerContext(ctx context.Context) (Scanner, error) {
|
||||
rows, err := q.QueryContext(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return NewScanner(rows), nil
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,36 @@
|
||||
package exec
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
|
||||
"git.fsdpf.net/go/db/v2/internal/sb"
|
||||
)
|
||||
|
||||
type (
|
||||
//nolint:stylecheck // keep name for backwards compatibility
|
||||
DbExecutor interface {
|
||||
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
|
||||
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
|
||||
}
|
||||
QueryFactory interface {
|
||||
FromSQL(sql string, args ...interface{}) QueryExecutor
|
||||
FromSQLBuilder(b sb.SQLBuilder) QueryExecutor
|
||||
}
|
||||
querySupport struct {
|
||||
de DbExecutor
|
||||
}
|
||||
)
|
||||
|
||||
func NewQueryFactory(de DbExecutor) QueryFactory {
|
||||
return &querySupport{de}
|
||||
}
|
||||
|
||||
func (qs *querySupport) FromSQL(query string, args ...interface{}) QueryExecutor {
|
||||
return newQueryExecutor(qs.de, nil, query, args...)
|
||||
}
|
||||
|
||||
func (qs *querySupport) FromSQLBuilder(b sb.SQLBuilder) QueryExecutor {
|
||||
query, args, err := b.ToSQL()
|
||||
return newQueryExecutor(qs.de, err, query, args...)
|
||||
}
|
||||
+168
@@ -0,0 +1,168 @@
|
||||
package exec
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"reflect"
|
||||
|
||||
"git.fsdpf.net/go/db/v2/exp"
|
||||
"git.fsdpf.net/go/db/v2/internal/errors"
|
||||
"git.fsdpf.net/go/db/v2/internal/util"
|
||||
)
|
||||
|
||||
type (
|
||||
// Scanner knows how to scan sql.Rows into structs.
|
||||
Scanner interface {
|
||||
Next() bool
|
||||
ScanStruct(i interface{}) error
|
||||
ScanStructs(i interface{}) error
|
||||
ScanVal(i interface{}) error
|
||||
ScanVals(i interface{}) error
|
||||
Close() error
|
||||
Err() error
|
||||
}
|
||||
|
||||
scanner struct {
|
||||
rows *sql.Rows
|
||||
columnMap util.ColumnMap
|
||||
columns []string
|
||||
}
|
||||
)
|
||||
|
||||
func unableToFindFieldError(col string) error {
|
||||
return errors.New(`unable to find corresponding field to column "%s" returned by query`, col)
|
||||
}
|
||||
|
||||
// NewScanner returns a scanner that can be used for scanning rows into structs.
|
||||
func NewScanner(rows *sql.Rows) Scanner {
|
||||
return &scanner{rows: rows}
|
||||
}
|
||||
|
||||
// Next prepares the next row for Scanning. See sql.Rows#Next for more
|
||||
// information.
|
||||
func (s *scanner) Next() bool {
|
||||
return s.rows.Next()
|
||||
}
|
||||
|
||||
// Err returns the error, if any that was encountered during iteration. See
|
||||
// sql.Rows#Err for more information.
|
||||
func (s *scanner) Err() error {
|
||||
return s.rows.Err()
|
||||
}
|
||||
|
||||
// ScanStruct will scan the current row into i.
|
||||
func (s *scanner) ScanStruct(i interface{}) error {
|
||||
// Setup columnMap and columns, but only once.
|
||||
if s.columnMap == nil || s.columns == nil {
|
||||
cm, err := util.GetColumnMap(i)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cols, err := s.rows.Columns()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s.columnMap = cm
|
||||
s.columns = cols
|
||||
}
|
||||
|
||||
scans := make([]interface{}, 0, len(s.columns))
|
||||
for _, col := range s.columns {
|
||||
data, ok := s.columnMap[col]
|
||||
switch {
|
||||
case !ok:
|
||||
return unableToFindFieldError(col)
|
||||
default:
|
||||
scans = append(scans, reflect.New(data.GoType).Interface())
|
||||
}
|
||||
}
|
||||
|
||||
if err := s.rows.Scan(scans...); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
record := exp.Record{}
|
||||
for index, col := range s.columns {
|
||||
record[col] = scans[index]
|
||||
}
|
||||
|
||||
util.AssignStructVals(i, record, s.columnMap)
|
||||
|
||||
return s.Err()
|
||||
}
|
||||
|
||||
// ScanStructs scans results in slice of structs
|
||||
func (s *scanner) ScanStructs(i interface{}) error {
|
||||
val, err := checkScanStructsTarget(i)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return s.scanIntoSlice(val, func(i interface{}) error {
|
||||
return s.ScanStruct(i)
|
||||
})
|
||||
}
|
||||
|
||||
// ScanVal will scan the current row and column into i.
|
||||
func (s *scanner) ScanVal(i interface{}) error {
|
||||
if err := s.rows.Scan(i); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return s.Err()
|
||||
}
|
||||
|
||||
// ScanStructs scans results in slice of values
|
||||
func (s *scanner) ScanVals(i interface{}) error {
|
||||
val, err := checkScanValsTarget(i)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return s.scanIntoSlice(val, func(i interface{}) error {
|
||||
return s.ScanVal(i)
|
||||
})
|
||||
}
|
||||
|
||||
// Close closes the Rows, preventing further enumeration. See sql.Rows#Close
|
||||
// for more info.
|
||||
func (s *scanner) Close() error {
|
||||
return s.rows.Close()
|
||||
}
|
||||
|
||||
func (s *scanner) scanIntoSlice(val reflect.Value, it func(i interface{}) error) error {
|
||||
elemType := util.GetSliceElementType(val)
|
||||
|
||||
for s.Next() {
|
||||
row := reflect.New(elemType)
|
||||
if rowErr := it(row.Interface()); rowErr != nil {
|
||||
return rowErr
|
||||
}
|
||||
util.AppendSliceElement(val, row)
|
||||
}
|
||||
|
||||
return s.Err()
|
||||
}
|
||||
|
||||
func checkScanStructsTarget(i interface{}) (reflect.Value, error) {
|
||||
val := reflect.ValueOf(i)
|
||||
if !util.IsPointer(val.Kind()) {
|
||||
return val, errUnsupportedScanStructsType
|
||||
}
|
||||
val = reflect.Indirect(val)
|
||||
if !util.IsSlice(val.Kind()) {
|
||||
return val, errUnsupportedScanStructsType
|
||||
}
|
||||
return val, nil
|
||||
}
|
||||
|
||||
func checkScanValsTarget(i interface{}) (reflect.Value, error) {
|
||||
val := reflect.ValueOf(i)
|
||||
if !util.IsPointer(val.Kind()) {
|
||||
return val, errUnsupportedScanValsType
|
||||
}
|
||||
val = reflect.Indirect(val)
|
||||
if !util.IsSlice(val.Kind()) {
|
||||
return val, errUnsupportedScanValsType
|
||||
}
|
||||
return val, nil
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package exec
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"github.com/stretchr/testify/suite"
|
||||
)
|
||||
|
||||
type scannerSuite struct {
|
||||
suite.Suite
|
||||
}
|
||||
|
||||
func TestScanner(t *testing.T) {
|
||||
suite.Run(t, &scannerSuite{})
|
||||
}
|
||||
|
||||
func (s *scannerSuite) TestScanStructs() {
|
||||
type StructWithTags struct {
|
||||
Address string `db:"address"`
|
||||
Name string `db:"name"`
|
||||
}
|
||||
db, mock, err := sqlmock.New()
|
||||
s.Require().NoError(err)
|
||||
|
||||
mock.ExpectQuery(`SELECT \* FROM "items"`).
|
||||
WithArgs().
|
||||
WillReturnRows(sqlmock.NewRows([]string{"address", "name"}).
|
||||
AddRow(testAddr1, testName1).
|
||||
AddRow(testAddr2, testName2),
|
||||
)
|
||||
rows, err := db.Query(`SELECT * FROM "items"`)
|
||||
s.Require().NoError(err)
|
||||
|
||||
sc := NewScanner(rows)
|
||||
|
||||
result := make([]StructWithTags, 0)
|
||||
err = sc.ScanStructs(result)
|
||||
s.Require().EqualError(err, errUnsupportedScanStructsType.Error())
|
||||
|
||||
err = sc.ScanStructs(&result)
|
||||
s.Require().NoError(err)
|
||||
s.Require().ElementsMatch(
|
||||
[]StructWithTags{{Address: testAddr1, Name: testName1}, {Address: testAddr2, Name: testName2}},
|
||||
result,
|
||||
)
|
||||
}
|
||||
|
||||
func (s *scannerSuite) TestScanVals() {
|
||||
db, mock, err := sqlmock.New()
|
||||
s.Require().NoError(err)
|
||||
|
||||
mock.ExpectQuery(`SELECT "id" FROM "items"`).
|
||||
WithArgs().
|
||||
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1).AddRow(2))
|
||||
|
||||
rows, err := db.Query(`SELECT "id" FROM "items"`)
|
||||
s.Require().NoError(err)
|
||||
|
||||
sc := NewScanner(rows)
|
||||
|
||||
result := make([]int, 0)
|
||||
err = sc.ScanVals(result)
|
||||
s.Require().EqualError(err, errUnsupportedScanValsType.Error())
|
||||
|
||||
err = sc.ScanVals(&result)
|
||||
s.Require().NoError(err)
|
||||
s.Require().ElementsMatch([]int{1, 2}, result)
|
||||
}
|
||||
Reference in New Issue
Block a user