Skip to content

Commit e65dc09

Browse files
committed
Support select all
1 parent bfb7324 commit e65dc09

File tree

3 files changed

+74
-16
lines changed

3 files changed

+74
-16
lines changed

loader.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,18 @@ func GetTxId(ctx context.Context) *string {
3232
}
3333
return nil
3434
}
35-
func InitFields(modelType reflect.Type, db *sql.DB) (map[string]int, func(i int) string, string, error) {
35+
func InitFields(modelType reflect.Type, db *sql.DB) (map[string]int, string, func(i int) string, string, error) {
3636
fieldsIndex, err := GetColumnIndexes(modelType)
3737
if err != nil {
38-
return nil, nil, "", err
38+
return nil, "", nil, "", err
3939
}
40+
fields := BuildFields(modelType)
4041
if db == nil {
41-
return fieldsIndex, nil, "", nil
42+
return fieldsIndex, fields, nil, "", nil
4243
}
4344
driver := GetDriver(db)
4445
buildParam := GetBuild(db)
45-
return fieldsIndex, buildParam, driver, nil
46+
return fieldsIndex, fields, buildParam, driver, nil
4647
}
4748
type Loader struct {
4849
Database *sql.DB
@@ -54,6 +55,7 @@ type Loader struct {
5455
mapJsonColumnKeys map[string]string
5556
fieldsIndex map[string]int
5657
table string
58+
Fields string
5759
IsRollback bool
5860
toArray func(interface{}) interface {
5961
driver.Valuer
@@ -124,15 +126,16 @@ func NewSqlLoader(db *sql.DB, tableName string, modelType reflect.Type, mp func(
124126
if er0 != nil {
125127
return nil, er0
126128
}
127-
return &Loader{Database: db, IsRollback: true, BuildParam: buildParam, Map: mp, modelType: modelType, modelsType: modelsType, keys: idNames, mapJsonColumnKeys: mapJsonColumnKeys, fieldsIndex: fieldsIndex, table: tableName, toArray: toArray}, nil
129+
fields := BuildFields(modelType)
130+
return &Loader{Database: db, IsRollback: true, BuildParam: buildParam, Map: mp, modelType: modelType, modelsType: modelsType, keys: idNames, mapJsonColumnKeys: mapJsonColumnKeys, fieldsIndex: fieldsIndex, table: tableName, Fields: fields, toArray: toArray}, nil
128131
}
129132

130133
func (s *Loader) Keys() []string {
131134
return s.keys
132135
}
133136

134137
func (s *Loader) All(ctx context.Context) (interface{}, error) {
135-
query := BuildSelectAllQuery(s.table)
138+
query := fmt.Sprintf("select %s from %s", s.Fields, s.table)
136139
result := reflect.New(s.modelsType).Interface()
137140
var err error
138141
tx := GetTx(ctx)
@@ -157,7 +160,8 @@ func (s *Loader) All(ctx context.Context) (interface{}, error) {
157160
}
158161

159162
func (s *Loader) Load(ctx context.Context, id interface{}) (interface{}, error) {
160-
queryFindById, values := BuildFindByIdWithDB(s.Database, s.table, id, s.mapJsonColumnKeys, s.keys, s.BuildParam)
163+
query := fmt.Sprintf("select %s from %s", s.Fields, s.table)
164+
queryFindById, values := BuildFindByIdWithDB(s.Database, query, id, s.mapJsonColumnKeys, s.keys, s.BuildParam)
161165
tx := GetTx(ctx)
162166
var r interface{}
163167
var er1 error
@@ -230,7 +234,8 @@ func (s *Loader) LoadAndDecode(ctx context.Context, id interface{}, result inter
230234
}
231235
func (s *Loader) Get(ctx context.Context, id interface{}, result interface{}) (bool, error) {
232236
var values []interface{}
233-
sql, values := BuildFindByIdWithDB(s.Database, s.table, id, s.mapJsonColumnKeys, s.keys, s.BuildParam)
237+
query := fmt.Sprintf("select %s from %s", s.Fields, s.table)
238+
sql, values := BuildFindByIdWithDB(s.Database, query, id, s.mapJsonColumnKeys, s.keys, s.BuildParam)
234239
var rowData interface{}
235240
var er1 error
236241
tx := GetTx(ctx)
@@ -339,17 +344,17 @@ func BuildSelectAllQuery(table string) string {
339344
return fmt.Sprintf("select * from %v", table)
340345
}
341346

342-
func BuildFindByIdWithDB(db *sql.DB, table string, id interface{}, mapJsonColumnKeys map[string]string, keys []string, options ...func(i int) string) (string, []interface{}) {
347+
func BuildFindByIdWithDB(db *sql.DB, query string, id interface{}, mapJsonColumnKeys map[string]string, keys []string, options ...func(i int) string) (string, []interface{}) {
343348
var buildParam func(i int) string
344349
if len(options) > 0 && options[0] != nil {
345350
buildParam = options[0]
346351
} else {
347352
buildParam = GetBuild(db)
348353
}
349-
return BuildFindById(table, buildParam, id, mapJsonColumnKeys, keys)
354+
return BuildFindById(query, buildParam, id, mapJsonColumnKeys, keys)
350355
}
351356

352-
func BuildFindById(table string, buildParam func(i int) string, id interface{}, mapJsonColumnKeys map[string]string, keys []string) (string, []interface{}) {
357+
func BuildFindById(selectAll string, buildParam func(i int) string, id interface{}, mapJsonColumnKeys map[string]string, keys []string) (string, []interface{}) {
353358
var where = ""
354359
var values []interface{}
355360
if len(keys) == 1 {
@@ -370,7 +375,7 @@ func BuildFindById(table string, buildParam func(i int) string, id interface{},
370375
where = "where " + strings.Join(conditions, " and ")
371376
}
372377
}
373-
return fmt.Sprintf("select * from %v %v", table, where), values
378+
return fmt.Sprintf("%s %s", selectAll, where), values
374379
}
375380

376381
func IsNil(i interface{}) bool {

util.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,58 @@ type Schema struct {
4141
Fields map[string]*FieldDB
4242
}
4343

44+
func BuildFieldsBySchema(schema *Schema) string {
45+
columns := make([]string, 0)
46+
for _, s := range schema.SColumns {
47+
columns = append(columns, s)
48+
}
49+
return strings.Join(columns, ",")
50+
}
51+
func BuildQueryBySchema(table string, schema *Schema) string {
52+
columns := make([]string, 0)
53+
for _, s := range schema.SColumns {
54+
columns = append(columns, s)
55+
}
56+
return "select " + strings.Join(columns, ",") + " from " + table + " "
57+
}
58+
func BuildFields(modelType reflect.Type) string {
59+
columns := GetFields(modelType)
60+
return strings.Join(columns, ",")
61+
}
62+
func GetFields(modelType reflect.Type) []string {
63+
m := modelType
64+
if m.Kind() == reflect.Ptr {
65+
m = m.Elem()
66+
}
67+
numField := m.NumField()
68+
columns := make([]string, 0)
69+
for idx := 0; idx < numField; idx++ {
70+
field := m.Field(idx)
71+
tag, _ := field.Tag.Lookup("gorm")
72+
if !strings.Contains(tag, IgnoreReadWrite) {
73+
if has := strings.Contains(tag, "column"); has {
74+
json := field.Name
75+
col := json
76+
str1 := strings.Split(tag, ";")
77+
num := len(str1)
78+
for i := 0; i < num; i++ {
79+
str2 := strings.Split(str1[i], ":")
80+
for j := 0; j < len(str2); j++ {
81+
if str2[j] == "column" {
82+
col = str2[j+1]
83+
columns = append(columns, col)
84+
}
85+
}
86+
}
87+
}
88+
}
89+
}
90+
return columns
91+
}
92+
func BuildQuery(table string, modelType reflect.Type) string {
93+
columns := GetFields(modelType)
94+
return "select " + strings.Join(columns, ",") + " from " + table + " "
95+
}
4496
func CreateSchema(modelType reflect.Type) *Schema {
4597
m := modelType
4698
if m.Kind() == reflect.Ptr {

writer.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,21 @@ func End(tx *sql.Tx, res int64, err error, options...bool) (int64, error) {
4545
er := Commit(tx, err, options...)
4646
return res, er
4747
}
48-
func Init(modelType reflect.Type, db *sql.DB) (map[string]int, *Schema, map[string]string, []string, []string, func(i int) string, string, error) {
48+
func Init(modelType reflect.Type, db *sql.DB) (map[string]int, *Schema, map[string]string, []string, []string, string, func(i int) string, string, error) {
4949
fieldsIndex, err := GetColumnIndexes(modelType)
5050
if err != nil {
51-
return nil, nil, nil, nil, nil, nil, "", err
51+
return nil, nil, nil, nil, nil, "", nil, "", err
5252
}
5353
schema := CreateSchema(modelType)
54+
fields := BuildFieldsBySchema(schema)
5455
jsonColumnMap := MakeJsonColumnMap(modelType)
5556
keys, arr := FindPrimaryKeys(modelType)
5657
if db == nil {
57-
return fieldsIndex, schema, jsonColumnMap, keys, arr, nil, "", nil
58+
return fieldsIndex, schema, jsonColumnMap, keys, arr, fields, nil, "", nil
5859
}
5960
driver := GetDriver(db)
6061
buildParam := GetBuild(db)
61-
return fieldsIndex, schema, jsonColumnMap, keys, arr, buildParam, driver, nil
62+
return fieldsIndex, schema, jsonColumnMap, keys, arr, fields, buildParam, driver, nil
6263
}
6364
type Writer struct {
6465
*Loader

0 commit comments

Comments
 (0)