250cbf59 by slene

fix values name

1 parent 5ccdaeb0
Showing 1 changed file with 25 additions and 12 deletions
...@@ -231,7 +231,7 @@ func (t *dbTables) getJoinSql() (join string) { ...@@ -231,7 +231,7 @@ func (t *dbTables) getJoinSql() (join string) {
231 return 231 return
232 } 232 }
233 233
234 func (d *dbTables) parseExprs(mi *modelInfo, exprs []string) (index, column string, info *fieldInfo, success bool) { 234 func (d *dbTables) parseExprs(mi *modelInfo, exprs []string) (index, column, name string, info *fieldInfo, success bool) {
235 var ( 235 var (
236 ffi *fieldInfo 236 ffi *fieldInfo
237 jtl *dbTable 237 jtl *dbTable
...@@ -290,6 +290,11 @@ func (d *dbTables) parseExprs(mi *modelInfo, exprs []string) (index, column stri ...@@ -290,6 +290,11 @@ func (d *dbTables) parseExprs(mi *modelInfo, exprs []string) (index, column stri
290 } 290 }
291 column = fi.column 291 column = fi.column
292 info = fi 292 info = fi
293 if jtl != nil {
294 name = jtl.name + ExprSep + fi.name
295 } else {
296 name = fi.name
297 }
293 298
294 switch fi.fieldType { 299 switch fi.fieldType {
295 case RelManyToMany, RelReverseMany: 300 case RelManyToMany, RelReverseMany:
...@@ -304,6 +309,7 @@ func (d *dbTables) parseExprs(mi *modelInfo, exprs []string) (index, column stri ...@@ -304,6 +309,7 @@ func (d *dbTables) parseExprs(mi *modelInfo, exprs []string) (index, column stri
304 if exist == false { 309 if exist == false {
305 index = "" 310 index = ""
306 column = "" 311 column = ""
312 name = ""
307 success = false 313 success = false
308 return 314 return
309 } 315 }
...@@ -349,7 +355,7 @@ func (d *dbTables) getCondSql(cond *Condition, sub bool) (where string, params [ ...@@ -349,7 +355,7 @@ func (d *dbTables) getCondSql(cond *Condition, sub bool) (where string, params [
349 exprs = exprs[:num] 355 exprs = exprs[:num]
350 } 356 }
351 357
352 index, column, _, suc := d.parseExprs(mi, exprs) 358 index, column, _, _, suc := d.parseExprs(mi, exprs)
353 if suc == false { 359 if suc == false {
354 panic(fmt.Errorf("unknown field/column name `%s`", strings.Join(p.exprs, ExprSep))) 360 panic(fmt.Errorf("unknown field/column name `%s`", strings.Join(p.exprs, ExprSep)))
355 } 361 }
...@@ -387,7 +393,7 @@ func (d *dbTables) getOrderSql(orders []string) (orderSql string) { ...@@ -387,7 +393,7 @@ func (d *dbTables) getOrderSql(orders []string) (orderSql string) {
387 } 393 }
388 exprs := strings.Split(order, ExprSep) 394 exprs := strings.Split(order, ExprSep)
389 395
390 index, column, _, suc := d.parseExprs(d.mi, exprs) 396 index, column, _, _, suc := d.parseExprs(d.mi, exprs)
391 if suc == false { 397 if suc == false {
392 panic(fmt.Errorf("unknown field/column name `%s`", strings.Join(exprs, ExprSep))) 398 panic(fmt.Errorf("unknown field/column name `%s`", strings.Join(exprs, ExprSep)))
393 } 399 }
...@@ -1249,18 +1255,18 @@ func (d *dbBase) ReadValues(q dbQuerier, qs *querySet, mi *modelInfo, cond *Cond ...@@ -1249,18 +1255,18 @@ func (d *dbBase) ReadValues(q dbQuerier, qs *querySet, mi *modelInfo, cond *Cond
1249 cols = make([]string, 0, len(exprs)) 1255 cols = make([]string, 0, len(exprs))
1250 infos = make([]*fieldInfo, 0, len(exprs)) 1256 infos = make([]*fieldInfo, 0, len(exprs))
1251 for _, ex := range exprs { 1257 for _, ex := range exprs {
1252 index, col, fi, suc := tables.parseExprs(mi, strings.Split(ex, ExprSep)) 1258 index, col, name, fi, suc := tables.parseExprs(mi, strings.Split(ex, ExprSep))
1253 if suc == false { 1259 if suc == false {
1254 panic(fmt.Errorf("unknown field/column name `%s`", ex)) 1260 panic(fmt.Errorf("unknown field/column name `%s`", ex))
1255 } 1261 }
1256 cols = append(cols, fmt.Sprintf("%s.`%s`", index, col)) 1262 cols = append(cols, fmt.Sprintf("%s.`%s` `%s`", index, col, name))
1257 infos = append(infos, fi) 1263 infos = append(infos, fi)
1258 } 1264 }
1259 } else { 1265 } else {
1260 cols = make([]string, 0, len(mi.fields.dbcols)) 1266 cols = make([]string, 0, len(mi.fields.dbcols))
1261 infos = make([]*fieldInfo, 0, len(exprs)) 1267 infos = make([]*fieldInfo, 0, len(exprs))
1262 for _, fi := range mi.fields.fieldsDB { 1268 for _, fi := range mi.fields.fieldsDB {
1263 cols = append(cols, fmt.Sprintf("T0.`%s`", fi.column)) 1269 cols = append(cols, fmt.Sprintf("T0.`%s` `%s`", fi.column, fi.name))
1264 infos = append(infos, fi) 1270 infos = append(infos, fi)
1265 } 1271 }
1266 } 1272 }
...@@ -1287,8 +1293,19 @@ func (d *dbBase) ReadValues(q dbQuerier, qs *querySet, mi *modelInfo, cond *Cond ...@@ -1287,8 +1293,19 @@ func (d *dbBase) ReadValues(q dbQuerier, qs *querySet, mi *modelInfo, cond *Cond
1287 refs[i] = &ref 1293 refs[i] = &ref
1288 } 1294 }
1289 1295
1290 var cnt int64 1296 var (
1297 cnt int64
1298 columns []string
1299 )
1291 for rs.Next() { 1300 for rs.Next() {
1301 if cnt == 0 {
1302 if cols, err := rs.Columns(); err != nil {
1303 return 0, err
1304 } else {
1305 columns = cols
1306 }
1307 }
1308
1292 if err := rs.Scan(refs...); err != nil { 1309 if err := rs.Scan(refs...); err != nil {
1293 return 0, err 1310 return 0, err
1294 } 1311 }
...@@ -1306,11 +1323,7 @@ func (d *dbBase) ReadValues(q dbQuerier, qs *querySet, mi *modelInfo, cond *Cond ...@@ -1306,11 +1323,7 @@ func (d *dbBase) ReadValues(q dbQuerier, qs *querySet, mi *modelInfo, cond *Cond
1306 panic(fmt.Sprintf("db value convert failed `%v` %s", val, err.Error())) 1323 panic(fmt.Sprintf("db value convert failed `%v` %s", val, err.Error()))
1307 } 1324 }
1308 1325
1309 if hasExprs { 1326 params[columns[i]] = value
1310 params[exprs[i]] = value
1311 } else {
1312 params[mi.fields.dbcols[i]] = value
1313 }
1314 } 1327 }
1315 maps = append(maps, params) 1328 maps = append(maps, params)
1316 case 2: 1329 case 2:
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!