2019-03-05 18:55:47 +01:00
|
|
|
package execution
|
|
|
|
|
|
|
|
|
|
import (
|
2019-06-20 12:22:19 +02:00
|
|
|
"context"
|
2019-03-05 18:55:47 +01:00
|
|
|
"database/sql"
|
2019-03-16 14:02:45 +01:00
|
|
|
"database/sql/driver"
|
2019-03-05 18:55:47 +01:00
|
|
|
"errors"
|
2019-03-14 09:18:23 +01:00
|
|
|
"fmt"
|
2019-06-23 18:55:57 +02:00
|
|
|
"github.com/go-jet/jet/execution/internal"
|
2019-03-05 18:55:47 +01:00
|
|
|
"github.com/serenize/snaker"
|
|
|
|
|
"reflect"
|
2019-03-15 21:55:43 +01:00
|
|
|
"strconv"
|
2019-03-14 09:18:23 +01:00
|
|
|
"strings"
|
2019-03-09 14:20:44 +01:00
|
|
|
"time"
|
2019-03-05 18:55:47 +01:00
|
|
|
)
|
|
|
|
|
|
2019-06-23 18:55:57 +02:00
|
|
|
func Query(db DB, context context.Context, query string, args []interface{}, destinationPtr interface{}) error {
|
2019-05-20 17:37:55 +02:00
|
|
|
|
|
|
|
|
if destinationPtr == nil {
|
|
|
|
|
return errors.New("Destination is nil. ")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
destinationPtrType := reflect.TypeOf(destinationPtr)
|
|
|
|
|
if destinationPtrType.Kind() != reflect.Ptr {
|
|
|
|
|
return errors.New("Destination has to be a pointer to slice or pointer to struct. ")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if destinationPtrType.Elem().Kind() == reflect.Slice {
|
2019-06-20 12:22:19 +02:00
|
|
|
return queryToSlice(db, context, query, args, destinationPtr)
|
2019-05-20 17:37:55 +02:00
|
|
|
} else if destinationPtrType.Elem().Kind() == reflect.Struct {
|
|
|
|
|
tempSlicePtrValue := reflect.New(reflect.SliceOf(destinationPtrType))
|
|
|
|
|
tempSliceValue := tempSlicePtrValue.Elem()
|
|
|
|
|
|
2019-06-20 12:22:19 +02:00
|
|
|
err := queryToSlice(db, context, query, args, tempSlicePtrValue.Interface())
|
2019-05-20 17:37:55 +02:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Println("TEMP SLICE SIZE: ", tempSliceValue.Len())
|
|
|
|
|
|
|
|
|
|
if tempSliceValue.Len() == 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
structValue := reflect.ValueOf(destinationPtr).Elem()
|
|
|
|
|
firstTempStruct := tempSliceValue.Index(0).Elem()
|
|
|
|
|
|
|
|
|
|
if structValue.Type().AssignableTo(firstTempStruct.Type()) {
|
|
|
|
|
structValue.Set(tempSliceValue.Index(0).Elem())
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
} else {
|
|
|
|
|
return errors.New("Unsupported destination type. ")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-23 18:55:57 +02:00
|
|
|
func queryToSlice(db DB, ctx context.Context, query string, args []interface{}, slicePtr interface{}) error {
|
2019-03-05 18:55:47 +01:00
|
|
|
if db == nil {
|
|
|
|
|
return errors.New("db is nil")
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-20 17:37:55 +02:00
|
|
|
if slicePtr == nil {
|
|
|
|
|
return errors.New("Destination is nil. ")
|
2019-03-05 18:55:47 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-20 17:37:55 +02:00
|
|
|
destinationType := reflect.TypeOf(slicePtr)
|
|
|
|
|
if destinationType.Kind() != reflect.Ptr && destinationType.Elem().Kind() != reflect.Slice {
|
|
|
|
|
return errors.New("Destination has to be a pointer to slice. ")
|
2019-03-05 18:55:47 +01:00
|
|
|
}
|
|
|
|
|
|
2019-06-20 12:22:19 +02:00
|
|
|
if ctx == nil {
|
|
|
|
|
ctx = context.Background()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rows, err := db.QueryContext(ctx, query, args...)
|
2019-03-05 18:55:47 +01:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2019-03-09 14:20:44 +01:00
|
|
|
defer rows.Close()
|
2019-03-05 18:55:47 +01:00
|
|
|
|
2019-05-20 17:37:55 +02:00
|
|
|
scanContext, err := newScanContext(rows)
|
2019-03-14 09:18:23 +01:00
|
|
|
|
2019-05-20 17:37:55 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
2019-03-14 09:18:23 +01:00
|
|
|
}
|
2019-03-05 18:55:47 +01:00
|
|
|
|
2019-05-20 17:37:55 +02:00
|
|
|
if len(scanContext.row) == 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
groupTime := time.Duration(0)
|
2019-03-30 09:59:24 +01:00
|
|
|
|
2019-06-12 16:21:50 +02:00
|
|
|
slicePtrValue := reflect.ValueOf(slicePtr)
|
|
|
|
|
|
2019-03-05 18:55:47 +01:00
|
|
|
for rows.Next() {
|
2019-04-04 13:07:21 +02:00
|
|
|
err := rows.Scan(scanContext.row...)
|
2019-03-05 18:55:47 +01:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-15 21:55:43 +01:00
|
|
|
scanContext.rowNum++
|
|
|
|
|
|
2019-05-20 17:37:55 +02:00
|
|
|
begin := time.Now()
|
2019-03-05 18:55:47 +01:00
|
|
|
|
2019-06-12 16:21:50 +02:00
|
|
|
_, err = mapRowToSlice(scanContext, "", slicePtrValue, nil)
|
2019-05-20 17:37:55 +02:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
2019-03-05 18:55:47 +01:00
|
|
|
}
|
2019-05-20 17:37:55 +02:00
|
|
|
|
|
|
|
|
groupTime += time.Now().Sub(begin)
|
2019-03-05 18:55:47 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-20 17:37:55 +02:00
|
|
|
fmt.Println(groupTime.String())
|
|
|
|
|
|
2019-06-21 16:16:57 +02:00
|
|
|
err = rows.Close()
|
2019-03-05 18:55:47 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-21 16:16:57 +02:00
|
|
|
err = rows.Err()
|
|
|
|
|
|
2019-05-05 12:37:23 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-29 14:39:48 +02:00
|
|
|
fmt.Println(strconv.Itoa(scanContext.rowNum) + " ROW(S) PROCESSED")
|
2019-03-16 14:02:45 +01:00
|
|
|
|
2019-03-05 18:55:47 +01:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-20 17:37:55 +02:00
|
|
|
func mapRowToSlice(scanContext *scanContext, groupKey string, slicePtrValue reflect.Value, structField *reflect.StructField) (updated bool, err error) {
|
2019-04-04 13:07:21 +02:00
|
|
|
|
2019-05-20 17:37:55 +02:00
|
|
|
sliceElemType := getSliceElemType(slicePtrValue)
|
2019-03-14 09:18:23 +01:00
|
|
|
|
2019-05-20 17:37:55 +02:00
|
|
|
if isGoBaseType(sliceElemType) {
|
|
|
|
|
index := 0
|
|
|
|
|
if structField != nil {
|
2019-06-12 12:47:30 +02:00
|
|
|
tableName, columnName := getRefTableNameFrom(structField)
|
2019-06-17 12:05:52 +02:00
|
|
|
index = scanContext.columnIndex(tableName, columnName)
|
2019-03-14 09:18:23 +01:00
|
|
|
|
2019-05-20 17:37:55 +02:00
|
|
|
if index < 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-21 17:34:43 +02:00
|
|
|
rowElemPtr := scanContext.rowElemValuePtr(index)
|
2019-03-14 09:18:23 +01:00
|
|
|
|
2019-05-20 17:37:55 +02:00
|
|
|
if !rowElemPtr.IsNil() {
|
2019-05-21 13:11:45 +02:00
|
|
|
updated = true
|
2019-05-22 11:28:32 +02:00
|
|
|
err = appendElemToSlice(slicePtrValue, rowElemPtr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2019-03-14 09:18:23 +01:00
|
|
|
}
|
2019-05-20 17:37:55 +02:00
|
|
|
|
|
|
|
|
return
|
2019-03-14 09:18:23 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-20 17:37:55 +02:00
|
|
|
if sliceElemType.Kind() != reflect.Struct {
|
|
|
|
|
return false, errors.New("Unsupported dest type: " + structField.Name + " " + structField.Type.String())
|
|
|
|
|
}
|
2019-03-14 09:18:23 +01:00
|
|
|
|
2019-06-20 11:19:23 +02:00
|
|
|
structGroupKey := scanContext.getGroupKey(sliceElemType, structField)
|
2019-03-30 09:59:24 +01:00
|
|
|
|
2019-05-20 17:37:55 +02:00
|
|
|
groupKey = groupKey + ":" + structGroupKey
|
2019-03-30 09:59:24 +01:00
|
|
|
|
2019-06-20 11:19:23 +02:00
|
|
|
index, ok := scanContext.uniqueDestObjectsMap[groupKey]
|
2019-03-30 09:59:24 +01:00
|
|
|
|
2019-05-20 17:37:55 +02:00
|
|
|
if ok {
|
|
|
|
|
structPtrValue := getSliceElemPtrAt(slicePtrValue, index)
|
2019-03-30 09:59:24 +01:00
|
|
|
|
2019-06-20 11:19:23 +02:00
|
|
|
return mapRowToStruct(scanContext, groupKey, structPtrValue, structField, true)
|
2019-05-20 17:37:55 +02:00
|
|
|
} else {
|
|
|
|
|
destinationStructPtr := newElemPtrValueForSlice(slicePtrValue)
|
2019-03-30 09:59:24 +01:00
|
|
|
|
2019-05-20 17:37:55 +02:00
|
|
|
updated, err = mapRowToStruct(scanContext, groupKey, destinationStructPtr, structField)
|
2019-03-30 09:59:24 +01:00
|
|
|
|
2019-05-20 17:37:55 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if updated {
|
2019-06-20 11:19:23 +02:00
|
|
|
scanContext.uniqueDestObjectsMap[groupKey] = slicePtrValue.Elem().Len()
|
2019-05-22 11:28:32 +02:00
|
|
|
err = appendElemToSlice(slicePtrValue, destinationStructPtr)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2019-05-20 17:37:55 +02:00
|
|
|
}
|
2019-03-30 09:59:24 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-20 17:37:55 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getSliceElemType(slicePtrValue reflect.Value) reflect.Type {
|
|
|
|
|
sliceTypePtr := slicePtrValue.Type()
|
2019-03-14 09:18:23 +01:00
|
|
|
|
|
|
|
|
elemType := sliceTypePtr.Elem().Elem()
|
|
|
|
|
|
|
|
|
|
if elemType.Kind() == reflect.Ptr {
|
|
|
|
|
return elemType.Elem()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return elemType
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-20 17:37:55 +02:00
|
|
|
func getSliceElemPtrAt(slicePtrValue reflect.Value, index int) reflect.Value {
|
|
|
|
|
sliceValue := slicePtrValue.Elem()
|
|
|
|
|
elem := sliceValue.Index(index)
|
2019-03-30 09:59:24 +01:00
|
|
|
|
2019-05-20 17:37:55 +02:00
|
|
|
if elem.Kind() == reflect.Ptr {
|
|
|
|
|
return elem
|
2019-03-14 09:18:23 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-20 17:37:55 +02:00
|
|
|
return elem.Addr()
|
2019-03-30 09:59:24 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-22 11:28:32 +02:00
|
|
|
func appendElemToSlice(slicePtrValue reflect.Value, objPtrValue reflect.Value) error {
|
2019-05-20 17:37:55 +02:00
|
|
|
if slicePtrValue.IsNil() {
|
|
|
|
|
panic("Slice is nil")
|
2019-03-15 21:55:43 +01:00
|
|
|
}
|
2019-05-20 17:37:55 +02:00
|
|
|
sliceValue := slicePtrValue.Elem()
|
|
|
|
|
sliceElemType := sliceValue.Type().Elem()
|
2019-03-15 21:55:43 +01:00
|
|
|
|
2019-05-20 17:37:55 +02:00
|
|
|
newElemValue := objPtrValue
|
2019-03-14 09:18:23 +01:00
|
|
|
|
2019-05-20 17:37:55 +02:00
|
|
|
if sliceElemType.Kind() != reflect.Ptr {
|
|
|
|
|
newElemValue = objPtrValue.Elem()
|
2019-03-14 09:18:23 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-22 11:28:32 +02:00
|
|
|
if !newElemValue.Type().AssignableTo(sliceElemType) {
|
|
|
|
|
return fmt.Errorf("Scan: can't append %s to %s slice ", newElemValue.Type().String(), sliceValue.Type().String())
|
2019-03-14 09:18:23 +01:00
|
|
|
}
|
2019-05-22 11:28:32 +02:00
|
|
|
|
|
|
|
|
sliceValue.Set(reflect.Append(sliceValue, newElemValue))
|
|
|
|
|
|
|
|
|
|
return nil
|
2019-03-05 18:55:47 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-20 17:37:55 +02:00
|
|
|
func newElemPtrValueForSlice(slicePtrValue reflect.Value) reflect.Value {
|
|
|
|
|
destinationSliceType := slicePtrValue.Type().Elem()
|
2019-06-12 12:47:30 +02:00
|
|
|
elemType := indirectType(destinationSliceType.Elem())
|
2019-03-14 09:18:23 +01:00
|
|
|
|
2019-05-20 17:37:55 +02:00
|
|
|
return reflect.New(elemType)
|
2019-03-14 09:18:23 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-20 17:37:55 +02:00
|
|
|
func mapRowToDestinationPtr(scanContext *scanContext, groupKey string, destPtrValue reflect.Value, structField *reflect.StructField) (updated bool, err error) {
|
2019-03-14 09:18:23 +01:00
|
|
|
|
2019-05-20 17:37:55 +02:00
|
|
|
if destPtrValue.Kind() != reflect.Ptr {
|
|
|
|
|
return false, errors.New("Internal error. ")
|
|
|
|
|
}
|
2019-03-14 09:18:23 +01:00
|
|
|
|
2019-05-20 17:37:55 +02:00
|
|
|
destValueKind := destPtrValue.Elem().Kind()
|
2019-03-14 09:18:23 +01:00
|
|
|
|
2019-05-20 17:37:55 +02:00
|
|
|
if destValueKind == reflect.Struct {
|
|
|
|
|
return mapRowToStruct(scanContext, groupKey, destPtrValue, structField)
|
|
|
|
|
} else if destValueKind == reflect.Slice {
|
|
|
|
|
return mapRowToSlice(scanContext, groupKey, destPtrValue, structField)
|
|
|
|
|
} else {
|
|
|
|
|
return false, errors.New("Unsupported dest type: " + structField.Name + " " + structField.Type.String())
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-03-05 18:55:47 +01:00
|
|
|
|
2019-05-20 17:37:55 +02:00
|
|
|
func mapRowToDestinationValue(scanContext *scanContext, groupKey string, dest reflect.Value, structField *reflect.StructField) (updated bool, err error) {
|
2019-03-14 09:18:23 +01:00
|
|
|
|
2019-05-20 17:37:55 +02:00
|
|
|
var destPtrValue reflect.Value
|
2019-03-14 09:18:23 +01:00
|
|
|
|
2019-05-20 17:37:55 +02:00
|
|
|
if dest.Kind() != reflect.Ptr {
|
|
|
|
|
destPtrValue = dest.Addr()
|
|
|
|
|
} else if dest.Kind() == reflect.Ptr {
|
|
|
|
|
if dest.IsNil() {
|
|
|
|
|
destPtrValue = reflect.New(dest.Type().Elem())
|
2019-03-14 09:18:23 +01:00
|
|
|
} else {
|
2019-05-20 17:37:55 +02:00
|
|
|
destPtrValue = dest
|
2019-03-14 09:18:23 +01:00
|
|
|
}
|
|
|
|
|
} else {
|
2019-05-20 17:37:55 +02:00
|
|
|
return false, errors.New("Internal error. ")
|
2019-03-14 09:18:23 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-20 17:37:55 +02:00
|
|
|
updated, err = mapRowToDestinationPtr(scanContext, groupKey, destPtrValue, structField)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if dest.Kind() == reflect.Ptr && dest.IsNil() && updated {
|
|
|
|
|
dest.Set(destPtrValue)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return
|
2019-03-05 18:55:47 +01:00
|
|
|
}
|
|
|
|
|
|
2019-06-20 11:19:23 +02:00
|
|
|
func mapRowToStruct(scanContext *scanContext, groupKey string, structPtrValue reflect.Value, structField *reflect.StructField, onlySlices ...bool) (updated bool, err error) {
|
2019-05-20 17:37:55 +02:00
|
|
|
structType := structPtrValue.Type().Elem()
|
|
|
|
|
structValue := structPtrValue.Elem()
|
2019-03-30 09:59:24 +01:00
|
|
|
|
2019-06-12 12:47:30 +02:00
|
|
|
tableName, _ := getRefTableNameFrom(structField)
|
2019-03-30 09:59:24 +01:00
|
|
|
|
|
|
|
|
if tableName == "" {
|
2019-06-17 12:05:52 +02:00
|
|
|
tableName = structType.Name()
|
2019-03-30 09:59:24 +01:00
|
|
|
}
|
|
|
|
|
|
2019-03-05 18:55:47 +01:00
|
|
|
for i := 0; i < structType.NumField(); i++ {
|
2019-03-30 09:59:24 +01:00
|
|
|
field := structType.Field(i)
|
2019-04-03 14:18:58 +02:00
|
|
|
|
2019-03-05 18:55:47 +01:00
|
|
|
fieldValue := structValue.Field(i)
|
2019-06-20 11:19:23 +02:00
|
|
|
columnName := field.Name
|
2019-03-09 14:20:44 +01:00
|
|
|
|
2019-04-04 13:07:21 +02:00
|
|
|
if scannerValue, ok := implementsScanner(fieldValue); ok {
|
2019-06-20 11:19:23 +02:00
|
|
|
if len(onlySlices) > 0 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cellValue := scanContext.getCellValue(tableName, columnName)
|
2019-04-03 19:21:46 +02:00
|
|
|
|
|
|
|
|
if cellValue == nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-20 11:19:23 +02:00
|
|
|
initializeValueIfNilPtr(fieldValue)
|
2019-04-03 19:21:46 +02:00
|
|
|
|
2019-04-04 13:07:21 +02:00
|
|
|
scanner := scannerValue.Interface().(sql.Scanner)
|
2019-04-03 19:21:46 +02:00
|
|
|
|
2019-05-20 17:37:55 +02:00
|
|
|
err = scanner.Scan(cellValue)
|
2019-04-03 19:21:46 +02:00
|
|
|
|
|
|
|
|
if err != nil {
|
2019-05-22 11:28:32 +02:00
|
|
|
err = fmt.Errorf("%s, at struct field: %s %s of type %s. ", err.Error(), field.Name, field.Type.String(), structType.String())
|
2019-05-20 17:37:55 +02:00
|
|
|
return
|
2019-04-03 19:21:46 +02:00
|
|
|
}
|
2019-05-20 17:37:55 +02:00
|
|
|
updated = true
|
2019-05-21 17:34:43 +02:00
|
|
|
} else if isGoBaseType(field.Type) {
|
2019-06-20 11:19:23 +02:00
|
|
|
if len(onlySlices) > 0 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cellValue := scanContext.getCellValue(tableName, columnName)
|
2019-05-21 17:34:43 +02:00
|
|
|
|
|
|
|
|
if cellValue != nil {
|
|
|
|
|
updated = true
|
2019-06-20 11:19:23 +02:00
|
|
|
initializeValueIfNilPtr(fieldValue)
|
2019-05-22 11:28:32 +02:00
|
|
|
err = setReflectValue(reflect.ValueOf(cellValue), fieldValue)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
err = fmt.Errorf("Scan: %s, at struct field: %s %s of type %s. ", err.Error(), field.Name, field.Type.String(), structType.String())
|
|
|
|
|
return
|
|
|
|
|
}
|
2019-05-21 17:34:43 +02:00
|
|
|
}
|
|
|
|
|
} else {
|
2019-05-20 17:37:55 +02:00
|
|
|
var changed bool
|
|
|
|
|
changed, err = mapRowToDestinationValue(scanContext, groupKey, fieldValue, &field)
|
2019-03-14 09:18:23 +01:00
|
|
|
|
|
|
|
|
if err != nil {
|
2019-05-20 17:37:55 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if changed {
|
|
|
|
|
updated = true
|
2019-03-09 14:20:44 +01:00
|
|
|
}
|
|
|
|
|
}
|
2019-03-05 18:55:47 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-20 17:37:55 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-20 11:19:23 +02:00
|
|
|
func getRefTableNameFrom(structField *reflect.StructField) (table, column string) {
|
|
|
|
|
if structField == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sqlTag := structField.Tag.Get("sql")
|
|
|
|
|
|
|
|
|
|
if sqlTag != "" {
|
|
|
|
|
tagInfo := tagInfo(sqlTag)
|
|
|
|
|
|
|
|
|
|
return tagInfo.table, tagInfo.column
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !structField.Anonymous {
|
|
|
|
|
return structField.Name, ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fieldType := indirectType(structField.Type)
|
|
|
|
|
|
|
|
|
|
if fieldType.Kind() == reflect.Slice {
|
|
|
|
|
fieldType = fieldType.Elem()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fieldType.Name(), ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func initializeValueIfNilPtr(value reflect.Value) {
|
2019-05-20 17:37:55 +02:00
|
|
|
if !value.IsValid() || !value.CanSet() {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-12 16:21:50 +02:00
|
|
|
if value.Kind() == reflect.Ptr && value.IsNil() {
|
2019-05-20 17:37:55 +02:00
|
|
|
value.Set(reflect.New(value.Type().Elem()))
|
|
|
|
|
}
|
2019-03-05 18:55:47 +01:00
|
|
|
}
|
|
|
|
|
|
2019-04-04 13:07:21 +02:00
|
|
|
func implementsScanner(value reflect.Value) (reflect.Value, bool) {
|
|
|
|
|
if _, ok := value.Interface().(sql.Scanner); ok {
|
|
|
|
|
return value, true
|
|
|
|
|
} else if value.CanAddr() {
|
|
|
|
|
if _, ok := value.Addr().Interface().(sql.Scanner); ok {
|
|
|
|
|
return value.Addr(), true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return value, false
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-20 11:19:23 +02:00
|
|
|
func valueToString(value reflect.Value) string {
|
2019-03-16 14:02:45 +01:00
|
|
|
|
2019-06-20 11:19:23 +02:00
|
|
|
if !value.IsValid() {
|
|
|
|
|
return "nil"
|
|
|
|
|
}
|
2019-03-16 14:02:45 +01:00
|
|
|
|
2019-03-14 09:18:23 +01:00
|
|
|
var valueInterface interface{}
|
|
|
|
|
if value.Kind() == reflect.Ptr {
|
2019-06-20 11:19:23 +02:00
|
|
|
if value.IsNil() {
|
|
|
|
|
return "nil"
|
|
|
|
|
} else {
|
|
|
|
|
valueInterface = value.Elem().Interface()
|
|
|
|
|
}
|
2019-03-14 09:18:23 +01:00
|
|
|
} else {
|
|
|
|
|
valueInterface = value.Interface()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if t, ok := valueInterface.(time.Time); ok {
|
|
|
|
|
return t.String()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fmt.Sprintf("%#v", valueInterface)
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-20 17:37:55 +02:00
|
|
|
func isGoBaseType(objType reflect.Type) bool {
|
2019-03-09 14:20:44 +01:00
|
|
|
typeStr := objType.String()
|
|
|
|
|
|
|
|
|
|
switch typeStr {
|
2019-05-27 13:11:15 +02:00
|
|
|
case "string", "int", "int16", "int32", "int64", "float32", "float64", "time.Time", "bool", "[]byte", "[]uint8",
|
|
|
|
|
"*string", "*int", "*int16", "*int32", "*int64", "*float32", "*float64", "*time.Time", "*bool", "*[]byte", "*[]uint8":
|
2019-03-09 14:20:44 +01:00
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-21 17:34:43 +02:00
|
|
|
func setReflectValue(source, destination reflect.Value) error {
|
|
|
|
|
var sourceElem reflect.Value
|
2019-03-05 18:55:47 +01:00
|
|
|
if destination.Kind() == reflect.Ptr {
|
|
|
|
|
if source.Kind() == reflect.Ptr {
|
2019-05-21 17:34:43 +02:00
|
|
|
sourceElem = source
|
2019-03-05 18:55:47 +01:00
|
|
|
} else {
|
2019-05-21 17:34:43 +02:00
|
|
|
if source.CanAddr() {
|
|
|
|
|
sourceElem = source.Addr()
|
|
|
|
|
} else {
|
2019-05-27 13:11:15 +02:00
|
|
|
sourceCopy := reflect.New(source.Type())
|
|
|
|
|
sourceCopy.Elem().Set(source)
|
2019-05-21 17:34:43 +02:00
|
|
|
|
2019-05-27 13:11:15 +02:00
|
|
|
sourceElem = sourceCopy
|
2019-05-21 17:34:43 +02:00
|
|
|
}
|
2019-03-05 18:55:47 +01:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if source.Kind() == reflect.Ptr {
|
2019-05-21 17:34:43 +02:00
|
|
|
sourceElem = source.Elem()
|
2019-03-05 18:55:47 +01:00
|
|
|
} else {
|
2019-05-21 17:34:43 +02:00
|
|
|
sourceElem = source
|
2019-03-05 18:55:47 +01:00
|
|
|
}
|
|
|
|
|
}
|
2019-05-21 17:34:43 +02:00
|
|
|
|
|
|
|
|
if !sourceElem.Type().AssignableTo(destination.Type()) {
|
2019-05-22 11:28:32 +02:00
|
|
|
return errors.New("can't set " + sourceElem.Type().String() + " to " + destination.Type().String())
|
2019-05-21 17:34:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
destination.Set(sourceElem)
|
|
|
|
|
|
|
|
|
|
return nil
|
2019-03-05 18:55:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func createScanValue(columnTypes []*sql.ColumnType) []interface{} {
|
|
|
|
|
values := make([]interface{}, len(columnTypes))
|
|
|
|
|
|
|
|
|
|
for i, sqlColumnType := range columnTypes {
|
2019-03-16 14:02:45 +01:00
|
|
|
columnType := newScanType(sqlColumnType)
|
2019-03-05 18:55:47 +01:00
|
|
|
|
|
|
|
|
columnValue := reflect.New(columnType)
|
|
|
|
|
|
|
|
|
|
values[i] = columnValue.Interface()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return values
|
|
|
|
|
}
|
2019-03-09 14:20:44 +01:00
|
|
|
|
2019-06-23 18:55:57 +02:00
|
|
|
var nullFloatType = reflect.TypeOf(internal.NullFloat32{})
|
2019-03-30 09:59:24 +01:00
|
|
|
var nullFloat64Type = reflect.TypeOf(sql.NullFloat64{})
|
2019-06-23 18:55:57 +02:00
|
|
|
var nullInt16Type = reflect.TypeOf(internal.NullInt16{})
|
|
|
|
|
var nullInt32Type = reflect.TypeOf(internal.NullInt32{})
|
2019-03-16 14:02:45 +01:00
|
|
|
var nullInt64Type = reflect.TypeOf(sql.NullInt64{})
|
|
|
|
|
var nullStringType = reflect.TypeOf(sql.NullString{})
|
|
|
|
|
var nullBoolType = reflect.TypeOf(sql.NullBool{})
|
2019-06-23 18:55:57 +02:00
|
|
|
var nullTimeType = reflect.TypeOf(internal.NullTime{})
|
|
|
|
|
var nullByteArrayType = reflect.TypeOf(internal.NullByteArray{})
|
2019-03-09 14:20:44 +01:00
|
|
|
|
2019-03-16 14:02:45 +01:00
|
|
|
func newScanType(columnType *sql.ColumnType) reflect.Type {
|
2019-03-09 14:20:44 +01:00
|
|
|
switch columnType.DatabaseTypeName() {
|
2019-03-16 14:02:45 +01:00
|
|
|
case "INT2":
|
|
|
|
|
return nullInt16Type
|
|
|
|
|
case "INT4":
|
|
|
|
|
return nullInt32Type
|
|
|
|
|
case "INT8":
|
|
|
|
|
return nullInt64Type
|
2019-05-27 13:11:15 +02:00
|
|
|
case "VARCHAR", "TEXT", "", "_TEXT", "TSVECTOR", "BPCHAR", "UUID", "JSON", "JSONB", "INTERVAL", "POINT", "BIT", "VARBIT", "XML":
|
2019-03-16 14:02:45 +01:00
|
|
|
return nullStringType
|
2019-03-09 14:20:44 +01:00
|
|
|
case "FLOAT4":
|
2019-03-16 14:02:45 +01:00
|
|
|
return nullFloatType
|
2019-05-31 12:59:57 +02:00
|
|
|
case "FLOAT8", "NUMERIC", "DECIMAL":
|
2019-03-30 09:59:24 +01:00
|
|
|
return nullFloat64Type
|
2019-03-16 14:02:45 +01:00
|
|
|
case "BOOL":
|
|
|
|
|
return nullBoolType
|
2019-05-27 13:11:15 +02:00
|
|
|
case "BYTEA":
|
|
|
|
|
return nullByteArrayType
|
|
|
|
|
case "DATE", "TIMESTAMP", "TIMESTAMPTZ", "TIME", "TIMETZ":
|
2019-03-16 14:02:45 +01:00
|
|
|
return nullTimeType
|
2019-03-09 14:20:44 +01:00
|
|
|
default:
|
2019-05-27 13:11:15 +02:00
|
|
|
fmt.Println("Unknown column database type " + columnType.DatabaseTypeName() + " using string as default.")
|
|
|
|
|
return nullStringType
|
2019-03-09 14:20:44 +01:00
|
|
|
}
|
|
|
|
|
}
|
2019-05-20 17:37:55 +02:00
|
|
|
|
|
|
|
|
type scanContext struct {
|
2019-06-20 11:19:23 +02:00
|
|
|
rowNum int
|
|
|
|
|
|
|
|
|
|
row []interface{}
|
|
|
|
|
uniqueDestObjectsMap map[string]int
|
2019-05-20 17:37:55 +02:00
|
|
|
|
2019-06-17 12:05:52 +02:00
|
|
|
columnNameIndexMap map[string]int
|
2019-06-20 11:19:23 +02:00
|
|
|
groupKeyInfoCache map[string]groupKeyInfo
|
2019-05-20 17:37:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newScanContext(rows *sql.Rows) (*scanContext, error) {
|
|
|
|
|
columnNames, err := rows.Columns()
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
columnTypes, err := rows.ColumnTypes()
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-17 12:05:52 +02:00
|
|
|
columnNameIndexMap := map[string]int{}
|
|
|
|
|
|
|
|
|
|
for i, columnName := range columnNames {
|
|
|
|
|
columnNameIndexMap[strings.ToLower(columnName)] = i
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-20 17:37:55 +02:00
|
|
|
return &scanContext{
|
2019-06-20 11:19:23 +02:00
|
|
|
row: createScanValue(columnTypes),
|
|
|
|
|
uniqueDestObjectsMap: make(map[string]int),
|
|
|
|
|
|
|
|
|
|
groupKeyInfoCache: make(map[string]groupKeyInfo),
|
|
|
|
|
|
2019-06-17 12:05:52 +02:00
|
|
|
columnNameIndexMap: columnNameIndexMap,
|
2019-05-20 17:37:55 +02:00
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-20 11:19:23 +02:00
|
|
|
func (s *scanContext) getGroupKey(structType reflect.Type, structField *reflect.StructField) string {
|
|
|
|
|
|
|
|
|
|
mapKey := structType.Name()
|
|
|
|
|
|
|
|
|
|
if structField != nil {
|
|
|
|
|
mapKey += structField.Type.String()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if groupKeyInfo, ok := s.groupKeyInfoCache[mapKey]; ok {
|
|
|
|
|
return s.constructGroupKey(groupKeyInfo)
|
|
|
|
|
} else {
|
|
|
|
|
groupKeyInfo := s.getGroupKeyInfo(structType, structField)
|
|
|
|
|
|
|
|
|
|
s.groupKeyInfoCache[mapKey] = groupKeyInfo
|
|
|
|
|
|
|
|
|
|
return s.constructGroupKey(groupKeyInfo)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *scanContext) constructGroupKey(groupKeyInfo groupKeyInfo) string {
|
|
|
|
|
if len(groupKeyInfo.indexes) == 0 && len(groupKeyInfo.subTypes) == 0 {
|
|
|
|
|
return "|ROW: " + strconv.Itoa(s.rowNum) + "|"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
groupKeys := []string{}
|
|
|
|
|
|
|
|
|
|
for _, index := range groupKeyInfo.indexes {
|
|
|
|
|
cellValue := s.rowElem(index)
|
|
|
|
|
subKey := valueToString(reflect.ValueOf(cellValue))
|
|
|
|
|
|
|
|
|
|
groupKeys = append(groupKeys, subKey)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
subTypesGroupKeys := []string{}
|
|
|
|
|
for _, subType := range groupKeyInfo.subTypes {
|
|
|
|
|
subTypesGroupKeys = append(subTypesGroupKeys, s.constructGroupKey(subType))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "{" + groupKeyInfo.typeName + "(" + strings.Join(groupKeys, ",") + strings.Join(subTypesGroupKeys, ",") + ")}"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *scanContext) getGroupKeyInfo(structType reflect.Type, structField *reflect.StructField) groupKeyInfo {
|
|
|
|
|
tableName, _ := getRefTableNameFrom(structField)
|
|
|
|
|
|
|
|
|
|
if tableName == "" {
|
|
|
|
|
tableName = structType.Name()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret := groupKeyInfo{typeName: structType.Name()}
|
|
|
|
|
|
|
|
|
|
for i := 0; i < structType.NumField(); i++ {
|
|
|
|
|
field := structType.Field(i)
|
|
|
|
|
|
|
|
|
|
if !isGoBaseType(field.Type) {
|
|
|
|
|
var structType reflect.Type
|
|
|
|
|
if field.Type.Kind() == reflect.Struct {
|
|
|
|
|
structType = field.Type
|
|
|
|
|
} else if field.Type.Kind() == reflect.Ptr && field.Type.Elem().Kind() == reflect.Struct {
|
|
|
|
|
structType = field.Type.Elem()
|
|
|
|
|
} else {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
subType := s.getGroupKeyInfo(structType, &field)
|
|
|
|
|
|
|
|
|
|
if len(subType.indexes) != 0 || len(subType.subTypes) != 0 {
|
|
|
|
|
ret.subTypes = append(ret.subTypes, subType)
|
|
|
|
|
}
|
|
|
|
|
} else if tagInfo(field.Tag.Get("sql")).isPrimaryKey {
|
|
|
|
|
index := s.columnIndex(tableName, field.Name)
|
|
|
|
|
|
|
|
|
|
if index < 0 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret.indexes = append(ret.indexes, index)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type groupKeyInfo struct {
|
|
|
|
|
typeName string
|
|
|
|
|
indexes []int
|
|
|
|
|
subTypes []groupKeyInfo
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *scanContext) columnIndex(tableName, columnName string) int {
|
|
|
|
|
if tableName == "" {
|
|
|
|
|
name := strings.ToLower(columnName)
|
2019-06-17 12:05:52 +02:00
|
|
|
if i, ok := s.columnNameIndexMap[name]; ok {
|
|
|
|
|
return i
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-20 11:19:23 +02:00
|
|
|
name = strings.ToLower(snaker.CamelToSnake(columnName))
|
2019-06-17 12:05:52 +02:00
|
|
|
if i, ok := s.columnNameIndexMap[name]; ok {
|
|
|
|
|
return i
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2019-06-20 11:19:23 +02:00
|
|
|
name := strings.ToLower(tableName + "." + columnName)
|
2019-06-17 12:05:52 +02:00
|
|
|
if i, ok := s.columnNameIndexMap[name]; ok {
|
|
|
|
|
return i
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-20 11:19:23 +02:00
|
|
|
name = strings.ToLower(snaker.CamelToSnake(tableName) + "." + snaker.CamelToSnake(columnName))
|
2019-06-17 12:05:52 +02:00
|
|
|
if i, ok := s.columnNameIndexMap[name]; ok {
|
|
|
|
|
return i
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-20 11:19:23 +02:00
|
|
|
name = strings.ToLower(tableName + "." + snaker.CamelToSnake(columnName))
|
2019-06-17 12:05:52 +02:00
|
|
|
if i, ok := s.columnNameIndexMap[name]; ok {
|
|
|
|
|
return i
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-20 11:19:23 +02:00
|
|
|
name = strings.ToLower(snaker.CamelToSnake(tableName) + "." + columnName)
|
2019-06-17 12:05:52 +02:00
|
|
|
if i, ok := s.columnNameIndexMap[name]; ok {
|
|
|
|
|
return i
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *scanContext) getCellValue(tableName, fieldName string) interface{} {
|
|
|
|
|
index := s.columnIndex(tableName, fieldName)
|
|
|
|
|
|
|
|
|
|
if index < 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return s.rowElem(index)
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-20 17:37:55 +02:00
|
|
|
func (s *scanContext) rowElem(index int) interface{} {
|
|
|
|
|
|
|
|
|
|
valuer, ok := s.row[index].(driver.Valuer)
|
|
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
|
panic("Scan value doesn't implement driver.Valuer")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
value, err := valuer.Value()
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return value
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-21 17:34:43 +02:00
|
|
|
func (s *scanContext) rowElemValuePtr(index int) reflect.Value {
|
2019-05-20 17:37:55 +02:00
|
|
|
rowElem := s.rowElem(index)
|
|
|
|
|
rowElemValue := reflect.ValueOf(rowElem)
|
|
|
|
|
|
|
|
|
|
if rowElemValue.Kind() == reflect.Ptr {
|
|
|
|
|
return rowElemValue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if rowElemValue.CanAddr() {
|
|
|
|
|
return rowElemValue.Addr()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newElem := reflect.New(rowElemValue.Type())
|
|
|
|
|
newElem.Elem().Set(rowElemValue)
|
|
|
|
|
return newElem
|
|
|
|
|
}
|
2019-06-12 12:47:30 +02:00
|
|
|
|
|
|
|
|
type sqlTagInfo struct {
|
|
|
|
|
isPrimaryKey bool
|
|
|
|
|
table string
|
|
|
|
|
column string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func tagInfo(tag string) sqlTagInfo {
|
|
|
|
|
sqlTags := strings.Split(tag, ",")
|
|
|
|
|
tagMap := map[string]string{}
|
|
|
|
|
|
|
|
|
|
for _, tag := range sqlTags {
|
|
|
|
|
tagParts := strings.Split(tag, ":")
|
|
|
|
|
|
|
|
|
|
tagKey := tagParts[0]
|
|
|
|
|
tagValue := ""
|
|
|
|
|
if len(tagParts) > 1 {
|
|
|
|
|
tagValue = tagParts[1]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tagMap[tagKey] = tagValue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, isPrimaryKey := tagMap["primary_key"]
|
|
|
|
|
|
|
|
|
|
return sqlTagInfo{
|
|
|
|
|
isPrimaryKey: isPrimaryKey,
|
|
|
|
|
table: tagMap["table"],
|
|
|
|
|
column: tagMap["column"],
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func indirectType(reflectType reflect.Type) reflect.Type {
|
|
|
|
|
if reflectType.Kind() != reflect.Ptr {
|
|
|
|
|
return reflectType
|
|
|
|
|
}
|
|
|
|
|
return reflectType.Elem()
|
|
|
|
|
}
|