bbgo_origin/pkg/service/reflect.go

255 lines
5.3 KiB
Go
Raw Permalink Normal View History

package service
import (
2022-06-01 04:02:15 +00:00
"context"
"reflect"
"strings"
2022-06-01 04:02:15 +00:00
"github.com/Masterminds/squirrel"
"github.com/fatih/camelcase"
gopluralize "github.com/gertd/go-pluralize"
2022-06-01 04:02:15 +00:00
"github.com/jmoiron/sqlx"
2022-06-01 10:29:54 +00:00
"github.com/sirupsen/logrus"
)
var pluralize = gopluralize.NewClient()
func tableNameOf(record interface{}) string {
rt := reflect.TypeOf(record)
if rt.Kind() == reflect.Ptr {
rt = rt.Elem()
}
typeName := rt.Name()
tableName := strings.Join(camelcase.Split(typeName), "_")
tableName = strings.ToLower(tableName)
return pluralize.Plural(tableName)
}
func placeholdersOf(record interface{}) []string {
rt := reflect.TypeOf(record)
if rt.Kind() == reflect.Ptr {
rt = rt.Elem()
}
2024-06-19 08:07:39 +00:00
vt := reflect.ValueOf(record)
if vt.Kind() == reflect.Ptr {
vt = vt.Elem()
}
if rt.Kind() != reflect.Struct {
return nil
}
var dbFields []string
for i := 0; i < rt.NumField(); i++ {
fieldType := rt.Field(i)
fieldValue := vt.Field(i)
if fieldType.Type.Kind() == reflect.Ptr && fieldValue.IsNil() {
continue
}
if tag, ok := fieldType.Tag.Lookup("db"); ok {
2023-07-28 06:41:36 +00:00
if tag == "gid" || tag == "-" || tag == "" {
2022-06-08 04:03:58 +00:00
continue
}
dbFields = append(dbFields, ":"+tag)
}
}
return dbFields
}
func fieldsNamesOf(record interface{}) []string {
rt := reflect.TypeOf(record)
if rt.Kind() == reflect.Ptr {
rt = rt.Elem()
}
2024-06-19 08:07:39 +00:00
vt := reflect.ValueOf(record)
if vt.Kind() == reflect.Ptr {
vt = vt.Elem()
}
if rt.Kind() != reflect.Struct {
return nil
}
var dbFields []string
for i := 0; i < rt.NumField(); i++ {
fieldType := rt.Field(i)
fieldValue := vt.Field(i)
// skip value=nil field
if fieldType.Type.Kind() == reflect.Ptr && fieldValue.IsNil() {
continue
}
if tag, ok := fieldType.Tag.Lookup("db"); ok {
2023-07-28 06:41:36 +00:00
if tag == "gid" || tag == "-" || tag == "" {
2022-06-08 04:03:58 +00:00
continue
}
dbFields = append(dbFields, tag)
}
}
return dbFields
}
2022-06-08 04:03:58 +00:00
func ParseStructTag(s string) (string, map[string]string) {
opts := make(map[string]string)
ss := strings.Split(s, ",")
if len(ss) > 1 {
for _, opt := range ss[1:] {
aa := strings.SplitN(opt, "=", 2)
if len(aa) == 2 {
opts[aa[0]] = aa[1]
} else {
opts[aa[0]] = ""
}
}
}
return ss[0], opts
}
type ReflectCache struct {
tableNames map[string]string
fields map[string][]string
placeholders map[string][]string
insertSqls map[string]string
}
func NewReflectCache() *ReflectCache {
return &ReflectCache{
tableNames: make(map[string]string),
fields: make(map[string][]string),
placeholders: make(map[string][]string),
insertSqls: make(map[string]string),
}
}
func (c *ReflectCache) InsertSqlOf(t interface{}) string {
rt := reflect.TypeOf(t)
if rt.Kind() == reflect.Ptr {
rt = rt.Elem()
}
typeName := rt.Name()
sql, ok := c.insertSqls[typeName]
if ok {
return sql
}
tableName := dbCache.TableNameOf(t)
fields := dbCache.FieldsOf(t)
placeholders := dbCache.PlaceholderOf(t)
fieldClause := strings.Join(fields, ", ")
placeholderClause := strings.Join(placeholders, ", ")
sql = `INSERT INTO ` + tableName + ` (` + fieldClause + `) VALUES (` + placeholderClause + `)`
c.insertSqls[typeName] = sql
return sql
}
func (c *ReflectCache) TableNameOf(t interface{}) string {
rt := reflect.TypeOf(t)
if rt.Kind() == reflect.Ptr {
rt = rt.Elem()
}
typeName := rt.Name()
tableName, ok := c.tableNames[typeName]
if ok {
return tableName
}
tableName = tableNameOf(t)
c.tableNames[typeName] = tableName
return tableName
}
func (c *ReflectCache) PlaceholderOf(t interface{}) []string {
rt := reflect.TypeOf(t)
if rt.Kind() == reflect.Ptr {
rt = rt.Elem()
}
typeName := rt.Name()
placeholders, ok := c.placeholders[typeName]
if ok {
return placeholders
}
placeholders = placeholdersOf(t)
c.placeholders[typeName] = placeholders
return placeholders
}
func (c *ReflectCache) FieldsOf(t interface{}) []string {
rt := reflect.TypeOf(t)
if rt.Kind() == reflect.Ptr {
rt = rt.Elem()
}
typeName := rt.Name()
fields, ok := c.fields[typeName]
if ok {
return fields
}
fields = fieldsNamesOf(t)
c.fields[typeName] = fields
return fields
}
2022-06-01 04:02:15 +00:00
// scanRowsOfType use the given type to scan rows
// this is usually slower than the native one since it uses reflect.
func scanRowsOfType(rows *sqlx.Rows, tpe interface{}) (interface{}, error) {
refType := reflect.TypeOf(tpe)
if refType.Kind() == reflect.Ptr {
refType = refType.Elem()
}
2022-06-01 07:53:08 +00:00
sliceRef := reflect.MakeSlice(reflect.SliceOf(refType), 0, 100)
// sliceRef := reflect.New(reflect.SliceOf(refType))
2022-06-01 04:02:15 +00:00
for rows.Next() {
var recordRef = reflect.New(refType)
var record = recordRef.Interface()
2022-06-01 07:53:08 +00:00
if err := rows.StructScan(record); err != nil {
2022-06-01 04:02:15 +00:00
return sliceRef.Interface(), err
}
2022-06-01 07:53:08 +00:00
sliceRef = reflect.Append(sliceRef, recordRef.Elem())
2022-06-01 04:02:15 +00:00
}
return sliceRef.Interface(), rows.Err()
}
func insertType(db *sqlx.DB, record interface{}) error {
sql := dbCache.InsertSqlOf(record)
_, err := db.NamedExec(sql, record)
return err
}
func selectAndScanType(ctx context.Context, db *sqlx.DB, sel squirrel.SelectBuilder, tpe interface{}) (interface{}, error) {
sql, args, err := sel.ToSql()
if err != nil {
return nil, err
}
2022-06-01 10:29:54 +00:00
logrus.Debugf("selectAndScanType: %T <- %s", tpe, sql)
logrus.Debugf("queryArgs: %v", args)
2022-06-01 04:02:15 +00:00
rows, err := db.QueryxContext(ctx, sql, args...)
if err != nil {
return nil, err
}
defer rows.Close()
return scanRowsOfType(rows, tpe)
}