more refactor
This commit is contained in:
parent
13cff1f270
commit
fd1f887a98
|
@ -11,64 +11,46 @@ import (
|
||||||
"xorm.io/builder"
|
"xorm.io/builder"
|
||||||
"xorm.io/xorm/dialects"
|
"xorm.io/xorm/dialects"
|
||||||
"xorm.io/xorm/internal/utils"
|
"xorm.io/xorm/internal/utils"
|
||||||
"xorm.io/xorm/schemas"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Join The joinOP should be one of INNER, LEFT OUTER, CROSS etc - this will be prepended to JOIN
|
// Join The joinOP should be one of INNER, LEFT OUTER, CROSS etc - this will be prepended to JOIN
|
||||||
func (statement *Statement) Join(joinOP string, tablename interface{}, condition interface{}, args ...interface{}) *Statement {
|
func (statement *Statement) Join(joinOP string, joinTable interface{}, condition interface{}, args ...interface{}) *Statement {
|
||||||
var buf strings.Builder
|
statement.joins = append(statement.joins, join{
|
||||||
if len(statement.JoinStr) > 0 {
|
op: joinOP,
|
||||||
fmt.Fprintf(&buf, "%v %v JOIN ", statement.JoinStr, joinOP)
|
table: joinTable,
|
||||||
} else {
|
condition: condition,
|
||||||
fmt.Fprintf(&buf, "%v JOIN ", joinOP)
|
args: args,
|
||||||
}
|
})
|
||||||
|
|
||||||
condStr := ""
|
|
||||||
condArgs := []interface{}{}
|
|
||||||
switch condTp := condition.(type) {
|
|
||||||
case string:
|
|
||||||
condStr = condTp
|
|
||||||
case builder.Cond:
|
|
||||||
var err error
|
|
||||||
condStr, condArgs, err = builder.ToSQL(condTp)
|
|
||||||
if err != nil {
|
|
||||||
statement.LastError = err
|
|
||||||
return statement
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
statement.LastError = fmt.Errorf("unsupported join condition type: %v", condTp)
|
|
||||||
return statement
|
return statement
|
||||||
}
|
}
|
||||||
|
|
||||||
switch tp := tablename.(type) {
|
func (statement *Statement) writeJoins(w builder.Writer) error {
|
||||||
|
for _, join := range statement.joins {
|
||||||
|
if err := statement.writeJoin(w, join); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (statement *Statement) writeJoin(buf builder.Writer, join join) error {
|
||||||
|
// write join operator
|
||||||
|
if _, err := fmt.Fprintf(buf, " %v JOIN ", join.op); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// write table or sub query
|
||||||
|
switch tp := join.table.(type) {
|
||||||
case builder.Builder:
|
case builder.Builder:
|
||||||
subSQL, subQueryArgs, err := tp.ToSQL()
|
if err := tp.WriteTo(buf); err != nil {
|
||||||
if err != nil {
|
return err
|
||||||
statement.LastError = err
|
|
||||||
return statement
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fields := strings.Split(tp.TableName(), ".")
|
|
||||||
aliasName := statement.dialect.Quoter().Trim(fields[len(fields)-1])
|
|
||||||
aliasName = schemas.CommonQuoter.Trim(aliasName)
|
|
||||||
|
|
||||||
fmt.Fprintf(&buf, "(%s) %s ON %v", statement.ReplaceQuote(subSQL), statement.quote(aliasName), statement.ReplaceQuote(condStr))
|
|
||||||
statement.joinArgs = append(append(statement.joinArgs, subQueryArgs...), condArgs...)
|
|
||||||
case *builder.Builder:
|
case *builder.Builder:
|
||||||
subSQL, subQueryArgs, err := tp.ToSQL()
|
if err := tp.WriteTo(buf); err != nil {
|
||||||
if err != nil {
|
return err
|
||||||
statement.LastError = err
|
|
||||||
return statement
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fields := strings.Split(tp.TableName(), ".")
|
|
||||||
aliasName := statement.dialect.Quoter().Trim(fields[len(fields)-1])
|
|
||||||
aliasName = schemas.CommonQuoter.Trim(aliasName)
|
|
||||||
|
|
||||||
fmt.Fprintf(&buf, "(%s) %s ON %v", statement.ReplaceQuote(subSQL), statement.quote(aliasName), statement.ReplaceQuote(condStr))
|
|
||||||
statement.joinArgs = append(append(statement.joinArgs, subQueryArgs...), condArgs...)
|
|
||||||
default:
|
default:
|
||||||
tbName := dialects.FullTableName(statement.dialect, statement.tagParser.GetTableMapper(), tablename, true)
|
tbName := dialects.FullTableName(statement.dialect, statement.tagParser.GetTableMapper(), join.table, true)
|
||||||
if !utils.IsSubQuery(tbName) {
|
if !utils.IsSubQuery(tbName) {
|
||||||
var buf strings.Builder
|
var buf strings.Builder
|
||||||
_ = statement.dialect.Quoter().QuoteTo(&buf, tbName)
|
_ = statement.dialect.Quoter().QuoteTo(&buf, tbName)
|
||||||
|
@ -76,21 +58,37 @@ func (statement *Statement) Join(joinOP string, tablename interface{}, condition
|
||||||
} else {
|
} else {
|
||||||
tbName = statement.ReplaceQuote(tbName)
|
tbName = statement.ReplaceQuote(tbName)
|
||||||
}
|
}
|
||||||
fmt.Fprintf(&buf, "%s ON %v", tbName, statement.ReplaceQuote(condStr))
|
if _, err := fmt.Fprint(buf, tbName); err != nil {
|
||||||
statement.joinArgs = append(statement.joinArgs, condArgs...)
|
|
||||||
}
|
|
||||||
|
|
||||||
statement.JoinStr = buf.String()
|
|
||||||
statement.joinArgs = append(statement.joinArgs, args...)
|
|
||||||
return statement
|
|
||||||
}
|
|
||||||
|
|
||||||
func (statement *Statement) writeJoin(w builder.Writer) error {
|
|
||||||
if statement.JoinStr != "" {
|
|
||||||
if _, err := fmt.Fprint(w, " ", statement.JoinStr); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
w.Append(statement.joinArgs...)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// write alias FIXME
|
||||||
|
/*fields := strings.Split(tp.TableName(), ".")
|
||||||
|
aliasName := statement.dialect.Quoter().Trim(fields[len(fields)-1])
|
||||||
|
aliasName = schemas.CommonQuoter.Trim(aliasName)
|
||||||
|
if _, err := fmt.Fprint(buf, " ", statement.quote(aliasName)); err != nil {
|
||||||
|
return err
|
||||||
|
}*/
|
||||||
|
|
||||||
|
// write condition
|
||||||
|
if _, err := fmt.Fprint(buf, " ON "); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
switch condTp := join.condition.(type) {
|
||||||
|
case string:
|
||||||
|
if _, err := fmt.Fprint(buf, condTp); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
buf.Append(join.args...)
|
||||||
|
case builder.Cond:
|
||||||
|
if err := condTp.WriteTo(buf); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("unsupported join condition type: %v", condTp)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,7 +34,7 @@ func (statement *Statement) GenQuerySQL(sqlOrArgs ...interface{}) (string, []int
|
||||||
if len(statement.SelectStr) > 0 {
|
if len(statement.SelectStr) > 0 {
|
||||||
columnStr = statement.SelectStr
|
columnStr = statement.SelectStr
|
||||||
} else {
|
} else {
|
||||||
if statement.JoinStr == "" {
|
if len(statement.joins) == 0 {
|
||||||
if columnStr == "" {
|
if columnStr == "" {
|
||||||
if statement.GroupByStr != "" {
|
if statement.GroupByStr != "" {
|
||||||
columnStr = statement.quoteColumnStr(statement.GroupByStr)
|
columnStr = statement.quoteColumnStr(statement.GroupByStr)
|
||||||
|
@ -109,7 +109,7 @@ func (statement *Statement) GenGetSQL(bean interface{}) (string, []interface{},
|
||||||
columnStr = statement.SelectStr
|
columnStr = statement.SelectStr
|
||||||
} else {
|
} else {
|
||||||
// TODO: always generate column names, not use * even if join
|
// TODO: always generate column names, not use * even if join
|
||||||
if len(statement.JoinStr) == 0 {
|
if len(statement.joins) == 0 {
|
||||||
if len(columnStr) == 0 {
|
if len(columnStr) == 0 {
|
||||||
if len(statement.GroupByStr) > 0 {
|
if len(statement.GroupByStr) > 0 {
|
||||||
columnStr = statement.quoteColumnStr(statement.GroupByStr)
|
columnStr = statement.quoteColumnStr(statement.GroupByStr)
|
||||||
|
@ -199,7 +199,7 @@ func (statement *Statement) writeFrom(w builder.Writer) error {
|
||||||
if err := statement.writeAlias(w); err != nil {
|
if err := statement.writeAlias(w); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return statement.writeJoin(w)
|
return statement.writeJoins(w)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (statement *Statement) writeLimitOffset(w builder.Writer) error {
|
func (statement *Statement) writeLimitOffset(w builder.Writer) error {
|
||||||
|
@ -296,7 +296,7 @@ func (statement *Statement) writeMssqlPaginationCond(w *builder.BytesWriter) err
|
||||||
} else {
|
} else {
|
||||||
column = statement.RefTable.PKColumns()[0].Name
|
column = statement.RefTable.PKColumns()[0].Name
|
||||||
}
|
}
|
||||||
if statement.needTableName() {
|
if statement.NeedTableName() {
|
||||||
if len(statement.TableAlias) > 0 {
|
if len(statement.TableAlias) > 0 {
|
||||||
column = fmt.Sprintf("%s.%s", statement.TableAlias, column)
|
column = fmt.Sprintf("%s.%s", statement.TableAlias, column)
|
||||||
} else {
|
} else {
|
||||||
|
@ -442,7 +442,7 @@ func (statement *Statement) GenExistSQL(bean ...interface{}) (string, []interfac
|
||||||
if _, err := fmt.Fprintf(buf, "SELECT TOP 1 * FROM %s", tableName); err != nil {
|
if _, err := fmt.Fprintf(buf, "SELECT TOP 1 * FROM %s", tableName); err != nil {
|
||||||
return "", nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
if err := statement.writeJoin(buf); err != nil {
|
if err := statement.writeJoins(buf); err != nil {
|
||||||
return "", nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
if statement.Conds().IsValid() {
|
if statement.Conds().IsValid() {
|
||||||
|
@ -457,7 +457,7 @@ func (statement *Statement) GenExistSQL(bean ...interface{}) (string, []interfac
|
||||||
if _, err := fmt.Fprintf(buf, "SELECT * FROM %s", tableName); err != nil {
|
if _, err := fmt.Fprintf(buf, "SELECT * FROM %s", tableName); err != nil {
|
||||||
return "", nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
if err := statement.writeJoin(buf); err != nil {
|
if err := statement.writeJoins(buf); err != nil {
|
||||||
return "", nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
if _, err := fmt.Fprintf(buf, " WHERE "); err != nil {
|
if _, err := fmt.Fprintf(buf, " WHERE "); err != nil {
|
||||||
|
@ -478,7 +478,7 @@ func (statement *Statement) GenExistSQL(bean ...interface{}) (string, []interfac
|
||||||
if _, err := fmt.Fprintf(buf, "SELECT 1 FROM %s", tableName); err != nil {
|
if _, err := fmt.Fprintf(buf, "SELECT 1 FROM %s", tableName); err != nil {
|
||||||
return "", nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
if err := statement.writeJoin(buf); err != nil {
|
if err := statement.writeJoins(buf); err != nil {
|
||||||
return "", nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
if statement.Conds().IsValid() {
|
if statement.Conds().IsValid() {
|
||||||
|
@ -503,7 +503,7 @@ func (statement *Statement) genSelect() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
columnStr := statement.ColumnStr()
|
columnStr := statement.ColumnStr()
|
||||||
if statement.JoinStr == "" {
|
if len(statement.joins) == 0 {
|
||||||
if columnStr == "" {
|
if columnStr == "" {
|
||||||
if statement.GroupByStr != "" {
|
if statement.GroupByStr != "" {
|
||||||
columnStr = statement.quoteColumnStr(statement.GroupByStr)
|
columnStr = statement.quoteColumnStr(statement.GroupByStr)
|
||||||
|
|
|
@ -102,7 +102,7 @@ func (statement *Statement) genColumnStr() string {
|
||||||
buf.WriteString(", ")
|
buf.WriteString(", ")
|
||||||
}
|
}
|
||||||
|
|
||||||
if statement.JoinStr != "" {
|
if len(statement.joins) > 0 {
|
||||||
if statement.TableAlias != "" {
|
if statement.TableAlias != "" {
|
||||||
buf.WriteString(statement.TableAlias)
|
buf.WriteString(statement.TableAlias)
|
||||||
} else {
|
} else {
|
||||||
|
@ -119,7 +119,7 @@ func (statement *Statement) genColumnStr() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (statement *Statement) colName(col *schemas.Column, tableName string) string {
|
func (statement *Statement) colName(col *schemas.Column, tableName string) string {
|
||||||
if statement.needTableName() {
|
if statement.NeedTableName() {
|
||||||
nm := tableName
|
nm := tableName
|
||||||
if len(statement.TableAlias) > 0 {
|
if len(statement.TableAlias) > 0 {
|
||||||
nm = statement.TableAlias
|
nm = statement.TableAlias
|
||||||
|
|
|
@ -34,6 +34,13 @@ var (
|
||||||
ErrTableNotFound = errors.New("Table not found")
|
ErrTableNotFound = errors.New("Table not found")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type join struct {
|
||||||
|
op string
|
||||||
|
table interface{}
|
||||||
|
condition interface{}
|
||||||
|
args []interface{}
|
||||||
|
}
|
||||||
|
|
||||||
// Statement save all the sql info for executing SQL
|
// Statement save all the sql info for executing SQL
|
||||||
type Statement struct {
|
type Statement struct {
|
||||||
RefTable *schemas.Table
|
RefTable *schemas.Table
|
||||||
|
@ -45,8 +52,7 @@ type Statement struct {
|
||||||
idParam schemas.PK
|
idParam schemas.PK
|
||||||
orderStr string
|
orderStr string
|
||||||
orderArgs []interface{}
|
orderArgs []interface{}
|
||||||
JoinStr string
|
joins []join
|
||||||
joinArgs []interface{}
|
|
||||||
GroupByStr string
|
GroupByStr string
|
||||||
HavingStr string
|
HavingStr string
|
||||||
SelectStr string
|
SelectStr string
|
||||||
|
@ -123,8 +129,7 @@ func (statement *Statement) Reset() {
|
||||||
statement.LimitN = nil
|
statement.LimitN = nil
|
||||||
statement.ResetOrderBy()
|
statement.ResetOrderBy()
|
||||||
statement.UseCascade = true
|
statement.UseCascade = true
|
||||||
statement.JoinStr = ""
|
statement.joins = nil
|
||||||
statement.joinArgs = make([]interface{}, 0)
|
|
||||||
statement.GroupByStr = ""
|
statement.GroupByStr = ""
|
||||||
statement.HavingStr = ""
|
statement.HavingStr = ""
|
||||||
statement.ColumnMap = columnMap{}
|
statement.ColumnMap = columnMap{}
|
||||||
|
@ -205,8 +210,9 @@ func (statement *Statement) SetRefBean(bean interface{}) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (statement *Statement) needTableName() bool {
|
// NeedTableName returns true if need table name before column names
|
||||||
return len(statement.JoinStr) > 0
|
func (statement *Statement) NeedTableName() bool {
|
||||||
|
return len(statement.joins) > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// Incr Generate "Update ... Set column = column + arg" statement
|
// Incr Generate "Update ... Set column = column + arg" statement
|
||||||
|
@ -605,8 +611,7 @@ func (statement *Statement) BuildConds(table *schemas.Table, bean interface{}, i
|
||||||
// MergeConds merge conditions from bean and id
|
// MergeConds merge conditions from bean and id
|
||||||
func (statement *Statement) MergeConds(bean interface{}) error {
|
func (statement *Statement) MergeConds(bean interface{}) error {
|
||||||
if !statement.NoAutoCondition && statement.RefTable != nil {
|
if !statement.NoAutoCondition && statement.RefTable != nil {
|
||||||
addedTableName := (len(statement.JoinStr) > 0)
|
autoCond, err := statement.BuildConds(statement.RefTable, bean, true, true, false, true, statement.NeedTableName())
|
||||||
autoCond, err := statement.BuildConds(statement.RefTable, bean, true, true, false, true, addedTableName)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -673,7 +678,7 @@ func (statement *Statement) joinColumns(cols []*schemas.Column, includeTableName
|
||||||
// CondDeleted returns the conditions whether a record is soft deleted.
|
// CondDeleted returns the conditions whether a record is soft deleted.
|
||||||
func (statement *Statement) CondDeleted(col *schemas.Column) builder.Cond {
|
func (statement *Statement) CondDeleted(col *schemas.Column) builder.Cond {
|
||||||
colName := statement.quote(col.Name)
|
colName := statement.quote(col.Name)
|
||||||
if statement.JoinStr != "" {
|
if len(statement.joins) > 0 {
|
||||||
var prefix string
|
var prefix string
|
||||||
if statement.TableAlias != "" {
|
if statement.TableAlias != "" {
|
||||||
prefix = statement.TableAlias
|
prefix = statement.TableAlias
|
||||||
|
|
11
rows.go
11
rows.go
|
@ -46,12 +46,11 @@ func newRows(session *Session, bean interface{}) (*Rows, error) {
|
||||||
|
|
||||||
if rows.session.statement.RawSQL == "" {
|
if rows.session.statement.RawSQL == "" {
|
||||||
var autoCond builder.Cond
|
var autoCond builder.Cond
|
||||||
var addedTableName = (len(session.statement.JoinStr) > 0)
|
table := rows.session.statement.RefTable
|
||||||
var table = rows.session.statement.RefTable
|
|
||||||
|
|
||||||
if !session.statement.NoAutoCondition {
|
if !session.statement.NoAutoCondition {
|
||||||
var err error
|
var err error
|
||||||
autoCond, err = session.statement.BuildConds(table, bean, true, true, false, true, addedTableName)
|
autoCond, err = session.statement.BuildConds(table, bean, true, true, false, true, session.statement.NeedTableName())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -103,12 +102,12 @@ func (rows *Rows) Scan(beans ...interface{}) error {
|
||||||
return rows.Err()
|
return rows.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
var bean = beans[0]
|
bean := beans[0]
|
||||||
var tp = reflect.TypeOf(bean)
|
tp := reflect.TypeOf(bean)
|
||||||
if tp.Kind() == reflect.Ptr {
|
if tp.Kind() == reflect.Ptr {
|
||||||
tp = tp.Elem()
|
tp = tp.Elem()
|
||||||
}
|
}
|
||||||
var beanKind = tp.Kind()
|
beanKind := tp.Kind()
|
||||||
|
|
||||||
if len(beans) == 1 {
|
if len(beans) == 1 {
|
||||||
if reflect.Indirect(reflect.ValueOf(bean)).Type() != rows.beanType {
|
if reflect.Indirect(reflect.ValueOf(bean)).Type() != rows.beanType {
|
||||||
|
|
|
@ -354,7 +354,7 @@ func (session *Session) DB() *core.DB {
|
||||||
|
|
||||||
func (session *Session) canCache() bool {
|
func (session *Session) canCache() bool {
|
||||||
if session.statement.RefTable == nil ||
|
if session.statement.RefTable == nil ||
|
||||||
session.statement.JoinStr != "" ||
|
session.statement.NeedTableName() ||
|
||||||
session.statement.RawSQL != "" ||
|
session.statement.RawSQL != "" ||
|
||||||
!session.statement.UseCache ||
|
!session.statement.UseCache ||
|
||||||
session.statement.IsForUpdate ||
|
session.statement.IsForUpdate ||
|
||||||
|
|
|
@ -114,7 +114,6 @@ func (session *Session) find(rowsSlicePtr interface{}, condiBean ...interface{})
|
||||||
|
|
||||||
var (
|
var (
|
||||||
table = session.statement.RefTable
|
table = session.statement.RefTable
|
||||||
addedTableName = (len(session.statement.JoinStr) > 0)
|
|
||||||
autoCond builder.Cond
|
autoCond builder.Cond
|
||||||
)
|
)
|
||||||
if tp == tpStruct {
|
if tp == tpStruct {
|
||||||
|
@ -123,7 +122,7 @@ func (session *Session) find(rowsSlicePtr interface{}, condiBean ...interface{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
autoCond, err = session.statement.BuildConds(condTable, condiBean[0], true, true, false, true, addedTableName)
|
autoCond, err = session.statement.BuildConds(condTable, condiBean[0], true, true, false, true, session.statement.NeedTableName())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -156,14 +156,14 @@ func (session *Session) insertMultipleStruct(rowsSlicePtr interface{}) (int64, e
|
||||||
}
|
}
|
||||||
args = append(args, val)
|
args = append(args, val)
|
||||||
|
|
||||||
var colName = col.Name
|
colName := col.Name
|
||||||
session.afterClosures = append(session.afterClosures, func(bean interface{}) {
|
session.afterClosures = append(session.afterClosures, func(bean interface{}) {
|
||||||
col := table.GetColumn(colName)
|
col := table.GetColumn(colName)
|
||||||
setColumnTime(bean, col, t)
|
setColumnTime(bean, col, t)
|
||||||
})
|
})
|
||||||
} else if col.IsVersion && session.statement.CheckVersion {
|
} else if col.IsVersion && session.statement.CheckVersion {
|
||||||
args = append(args, 1)
|
args = append(args, 1)
|
||||||
var colName = col.Name
|
colName := col.Name
|
||||||
session.afterClosures = append(session.afterClosures, func(bean interface{}) {
|
session.afterClosures = append(session.afterClosures, func(bean interface{}) {
|
||||||
col := table.GetColumn(colName)
|
col := table.GetColumn(colName)
|
||||||
setColumnInt(bean, col, 1)
|
setColumnInt(bean, col, 1)
|
||||||
|
@ -276,7 +276,7 @@ func (session *Session) insertStruct(bean interface{}) (int64, error) {
|
||||||
processor.BeforeInsert()
|
processor.BeforeInsert()
|
||||||
}
|
}
|
||||||
|
|
||||||
var tableName = session.statement.TableName()
|
tableName := session.statement.TableName()
|
||||||
table := session.statement.RefTable
|
table := session.statement.RefTable
|
||||||
|
|
||||||
colNames, args, err := session.genInsertColumns(bean)
|
colNames, args, err := session.genInsertColumns(bean)
|
||||||
|
@ -517,7 +517,7 @@ func (session *Session) genInsertColumns(bean interface{}) ([]string, []interfac
|
||||||
}
|
}
|
||||||
args = append(args, val)
|
args = append(args, val)
|
||||||
|
|
||||||
var colName = col.Name
|
colName := col.Name
|
||||||
session.afterClosures = append(session.afterClosures, func(bean interface{}) {
|
session.afterClosures = append(session.afterClosures, func(bean interface{}) {
|
||||||
col := table.GetColumn(colName)
|
col := table.GetColumn(colName)
|
||||||
setColumnTime(bean, col, t)
|
setColumnTime(bean, col, t)
|
||||||
|
@ -547,7 +547,7 @@ func (session *Session) insertMapInterface(m map[string]interface{}) (int64, err
|
||||||
return 0, ErrTableNotFound
|
return 0, ErrTableNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
var columns = make([]string, 0, len(m))
|
columns := make([]string, 0, len(m))
|
||||||
exprs := session.statement.ExprColumns
|
exprs := session.statement.ExprColumns
|
||||||
for k := range m {
|
for k := range m {
|
||||||
if !exprs.IsColExist(k) {
|
if !exprs.IsColExist(k) {
|
||||||
|
@ -556,7 +556,7 @@ func (session *Session) insertMapInterface(m map[string]interface{}) (int64, err
|
||||||
}
|
}
|
||||||
sort.Strings(columns)
|
sort.Strings(columns)
|
||||||
|
|
||||||
var args = make([]interface{}, 0, len(m))
|
args := make([]interface{}, 0, len(m))
|
||||||
for _, colName := range columns {
|
for _, colName := range columns {
|
||||||
args = append(args, m[colName])
|
args = append(args, m[colName])
|
||||||
}
|
}
|
||||||
|
@ -574,7 +574,7 @@ func (session *Session) insertMultipleMapInterface(maps []map[string]interface{}
|
||||||
return 0, ErrTableNotFound
|
return 0, ErrTableNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
var columns = make([]string, 0, len(maps[0]))
|
columns := make([]string, 0, len(maps[0]))
|
||||||
exprs := session.statement.ExprColumns
|
exprs := session.statement.ExprColumns
|
||||||
for k := range maps[0] {
|
for k := range maps[0] {
|
||||||
if !exprs.IsColExist(k) {
|
if !exprs.IsColExist(k) {
|
||||||
|
@ -583,9 +583,9 @@ func (session *Session) insertMultipleMapInterface(maps []map[string]interface{}
|
||||||
}
|
}
|
||||||
sort.Strings(columns)
|
sort.Strings(columns)
|
||||||
|
|
||||||
var argss = make([][]interface{}, 0, len(maps))
|
argss := make([][]interface{}, 0, len(maps))
|
||||||
for _, m := range maps {
|
for _, m := range maps {
|
||||||
var args = make([]interface{}, 0, len(m))
|
args := make([]interface{}, 0, len(m))
|
||||||
for _, colName := range columns {
|
for _, colName := range columns {
|
||||||
args = append(args, m[colName])
|
args = append(args, m[colName])
|
||||||
}
|
}
|
||||||
|
@ -605,7 +605,7 @@ func (session *Session) insertMapString(m map[string]string) (int64, error) {
|
||||||
return 0, ErrTableNotFound
|
return 0, ErrTableNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
var columns = make([]string, 0, len(m))
|
columns := make([]string, 0, len(m))
|
||||||
exprs := session.statement.ExprColumns
|
exprs := session.statement.ExprColumns
|
||||||
for k := range m {
|
for k := range m {
|
||||||
if !exprs.IsColExist(k) {
|
if !exprs.IsColExist(k) {
|
||||||
|
@ -615,7 +615,7 @@ func (session *Session) insertMapString(m map[string]string) (int64, error) {
|
||||||
|
|
||||||
sort.Strings(columns)
|
sort.Strings(columns)
|
||||||
|
|
||||||
var args = make([]interface{}, 0, len(m))
|
args := make([]interface{}, 0, len(m))
|
||||||
for _, colName := range columns {
|
for _, colName := range columns {
|
||||||
args = append(args, m[colName])
|
args = append(args, m[colName])
|
||||||
}
|
}
|
||||||
|
@ -633,7 +633,7 @@ func (session *Session) insertMultipleMapString(maps []map[string]string) (int64
|
||||||
return 0, ErrTableNotFound
|
return 0, ErrTableNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
var columns = make([]string, 0, len(maps[0]))
|
columns := make([]string, 0, len(maps[0]))
|
||||||
exprs := session.statement.ExprColumns
|
exprs := session.statement.ExprColumns
|
||||||
for k := range maps[0] {
|
for k := range maps[0] {
|
||||||
if !exprs.IsColExist(k) {
|
if !exprs.IsColExist(k) {
|
||||||
|
@ -642,9 +642,9 @@ func (session *Session) insertMultipleMapString(maps []map[string]string) (int64
|
||||||
}
|
}
|
||||||
sort.Strings(columns)
|
sort.Strings(columns)
|
||||||
|
|
||||||
var argss = make([][]interface{}, 0, len(maps))
|
argss := make([][]interface{}, 0, len(maps))
|
||||||
for _, m := range maps {
|
for _, m := range maps {
|
||||||
var args = make([]interface{}, 0, len(m))
|
args := make([]interface{}, 0, len(m))
|
||||||
for _, colName := range columns {
|
for _, colName := range columns {
|
||||||
args = append(args, m[colName])
|
args = append(args, m[colName])
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue