-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
service: use reflect to generate insert sql
- Loading branch information
Showing
8 changed files
with
246 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
package service | ||
|
||
import ( | ||
"reflect" | ||
"strings" | ||
|
||
"github.com/fatih/camelcase" | ||
gopluralize "github.com/gertd/go-pluralize" | ||
) | ||
|
||
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() | ||
} | ||
|
||
if rt.Kind() != reflect.Struct { | ||
return nil | ||
} | ||
|
||
var dbFields []string | ||
for i := 0; i < rt.NumField(); i++ { | ||
fieldType := rt.Field(i) | ||
if tag, ok := fieldType.Tag.Lookup("db"); ok { | ||
dbFields = append(dbFields, ":"+tag) | ||
} | ||
} | ||
|
||
return dbFields | ||
} | ||
|
||
func fieldsNamesOf(record interface{}) []string { | ||
rt := reflect.TypeOf(record) | ||
if rt.Kind() == reflect.Ptr { | ||
rt = rt.Elem() | ||
} | ||
|
||
if rt.Kind() != reflect.Struct { | ||
return nil | ||
} | ||
|
||
var dbFields []string | ||
for i := 0; i < rt.NumField(); i++ { | ||
fieldType := rt.Field(i) | ||
if tag, ok := fieldType.Tag.Lookup("db"); ok { | ||
dbFields = append(dbFields, tag) | ||
} | ||
} | ||
|
||
return dbFields | ||
} | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package service | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/c9s/bbgo/pkg/types" | ||
) | ||
|
||
func Test_tableNameOf(t *testing.T) { | ||
type args struct { | ||
record interface{} | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want string | ||
}{ | ||
{ | ||
name: "MarginInterest", | ||
args: args{record: &types.MarginInterest{}}, | ||
want: "margin_interests", | ||
}, | ||
{ | ||
name: "MarginLoan", | ||
args: args{record: &types.MarginLoan{}}, | ||
want: "margin_loans", | ||
}, | ||
{ | ||
name: "MarginRepay", | ||
args: args{record: &types.MarginRepay{}}, | ||
want: "margin_repays", | ||
}, | ||
{ | ||
name: "MarginLiquidation", | ||
args: args{record: &types.MarginLiquidation{}}, | ||
want: "margin_liquidations", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := tableNameOf(tt.args.record); got != tt.want { | ||
t.Errorf("tableNameOf() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func Test_fieldsNamesOf(t *testing.T) { | ||
type args struct { | ||
record interface{} | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want []string | ||
}{ | ||
{ | ||
name: "MarginInterest", | ||
args: args{record: &types.MarginInterest{}}, | ||
want: []string{"asset", "principle", "interest", "interest_rate", "isolated_symbol", "time"}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := fieldsNamesOf(tt.args.record); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("fieldsNamesOf() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters