@@ -32,17 +32,18 @@ func GetTxId(ctx context.Context) *string {
32
32
}
33
33
return nil
34
34
}
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 ) {
36
36
fieldsIndex , err := GetColumnIndexes (modelType )
37
37
if err != nil {
38
- return nil , nil , "" , err
38
+ return nil , "" , nil , "" , err
39
39
}
40
+ fields := BuildFields (modelType )
40
41
if db == nil {
41
- return fieldsIndex , nil , "" , nil
42
+ return fieldsIndex , fields , nil , "" , nil
42
43
}
43
44
driver := GetDriver (db )
44
45
buildParam := GetBuild (db )
45
- return fieldsIndex , buildParam , driver , nil
46
+ return fieldsIndex , fields , buildParam , driver , nil
46
47
}
47
48
type Loader struct {
48
49
Database * sql.DB
@@ -54,6 +55,7 @@ type Loader struct {
54
55
mapJsonColumnKeys map [string ]string
55
56
fieldsIndex map [string ]int
56
57
table string
58
+ Fields string
57
59
IsRollback bool
58
60
toArray func (interface {}) interface {
59
61
driver.Valuer
@@ -124,15 +126,16 @@ func NewSqlLoader(db *sql.DB, tableName string, modelType reflect.Type, mp func(
124
126
if er0 != nil {
125
127
return nil , er0
126
128
}
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
128
131
}
129
132
130
133
func (s * Loader ) Keys () []string {
131
134
return s .keys
132
135
}
133
136
134
137
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 )
136
139
result := reflect .New (s .modelsType ).Interface ()
137
140
var err error
138
141
tx := GetTx (ctx )
@@ -157,7 +160,8 @@ func (s *Loader) All(ctx context.Context) (interface{}, error) {
157
160
}
158
161
159
162
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 )
161
165
tx := GetTx (ctx )
162
166
var r interface {}
163
167
var er1 error
@@ -230,7 +234,8 @@ func (s *Loader) LoadAndDecode(ctx context.Context, id interface{}, result inter
230
234
}
231
235
func (s * Loader ) Get (ctx context.Context , id interface {}, result interface {}) (bool , error ) {
232
236
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 )
234
239
var rowData interface {}
235
240
var er1 error
236
241
tx := GetTx (ctx )
@@ -339,17 +344,17 @@ func BuildSelectAllQuery(table string) string {
339
344
return fmt .Sprintf ("select * from %v" , table )
340
345
}
341
346
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 {}) {
343
348
var buildParam func (i int ) string
344
349
if len (options ) > 0 && options [0 ] != nil {
345
350
buildParam = options [0 ]
346
351
} else {
347
352
buildParam = GetBuild (db )
348
353
}
349
- return BuildFindById (table , buildParam , id , mapJsonColumnKeys , keys )
354
+ return BuildFindById (query , buildParam , id , mapJsonColumnKeys , keys )
350
355
}
351
356
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 {}) {
353
358
var where = ""
354
359
var values []interface {}
355
360
if len (keys ) == 1 {
@@ -370,7 +375,7 @@ func BuildFindById(table string, buildParam func(i int) string, id interface{},
370
375
where = "where " + strings .Join (conditions , " and " )
371
376
}
372
377
}
373
- return fmt .Sprintf ("select * from %v %v " , table , where ), values
378
+ return fmt .Sprintf ("%s %s " , selectAll , where ), values
374
379
}
375
380
376
381
func IsNil (i interface {}) bool {
0 commit comments