make SetExpr() available for insert op
This commit is contained in:
parent
5b2b0091de
commit
e5a3b63b75
25
session.go
25
session.go
|
@ -2807,8 +2807,29 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
colPlaces := strings.Repeat("?, ", len(colNames))
|
// insert expr columns, override if exists
|
||||||
colPlaces = colPlaces[0 : len(colPlaces)-2]
|
exprColumns := session.Statement.getExpr()
|
||||||
|
exprColVals := make([]string, 0, len(exprColumns))
|
||||||
|
for _, v := range exprColumns {
|
||||||
|
// remove the expr columns
|
||||||
|
for i, colName := range colNames {
|
||||||
|
if colName == v.colName {
|
||||||
|
colNames = append(colNames[:i], colNames[i+1:]...)
|
||||||
|
args = append(args[:i], args[i+1:]...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// append expr column to the end
|
||||||
|
colNames = append(colNames, v.colName)
|
||||||
|
exprColVals = append(exprColVals, v.expr)
|
||||||
|
}
|
||||||
|
|
||||||
|
colPlaces := strings.Repeat("?, ", len(colNames)-len(exprColumns))
|
||||||
|
if len(exprColVals) > 0 {
|
||||||
|
colPlaces = colPlaces + strings.Join(exprColVals, ", ")
|
||||||
|
} else {
|
||||||
|
colPlaces = colPlaces[0 : len(colPlaces)-2]
|
||||||
|
}
|
||||||
|
|
||||||
sqlStr := fmt.Sprintf("INSERT INTO %v%v%v (%v%v%v) VALUES (%v)",
|
sqlStr := fmt.Sprintf("INSERT INTO %v%v%v (%v%v%v) VALUES (%v)",
|
||||||
session.Engine.QuoteStr(),
|
session.Engine.QuoteStr(),
|
||||||
|
|
Loading…
Reference in New Issue