Browse Source

增加批量添加

master
zhenghaorong 2 years ago
parent
commit
dcf3856862
2 changed files with 81 additions and 24 deletions
  1. +41
    -12
      chain.go
  2. +40
    -12
      transaction_chain.go

+ 41
- 12
chain.go View File

@ -20,7 +20,8 @@ type Query struct {
title string title string
where []string where []string
where_or []string where_or []string
join [][]string //[["tablea as a","a.id=b.id","left"]]
join [][]string //[["tablea as a","a.id=b.id","left"]]
save_data []map[string]interface{} //[["title":"a","num":1,],["title":"a","num":1,]]
data []string data []string
value []interface{} value []interface{}
orderby string orderby string
@ -111,6 +112,14 @@ func (this *Query) WhereOr(where string) *Query {
this.where_or = append(this.where_or, where) this.where_or = append(this.where_or, where)
return this return this
} }
func (this *Query) saveData(value map[string]interface{}) *Query {
this.save_data = append(this.save_data, value)
return this
}
func (this *Query) saveDatas(value []map[string]interface{}) *Query {
this.save_data = append(this.save_data, value...)
return this
}
func (this *Query) Value(value interface{}) *Query { func (this *Query) Value(value interface{}) *Query {
this.value = append(this.value, value) this.value = append(this.value, value)
return this return this
@ -328,22 +337,42 @@ func (this *Query) CreateAllStmt() error {
dbName := getTableName(this.dbname, this.table) dbName := getTableName(this.dbname, this.table)
var sql string var sql string
var dataSql = []string{}
var valSql = []string{}
if len(this.save_data) > 0 {
this.data = []string{}
this.value = []interface{}{}
for i, datum := range this.save_data {
for s, v := range datum {
if i == 0 {
this.data = append(this.data, s)
dataSql = append(dataSql, "?")
}
this.value = append(this.value, v)
}
valSql = append(valSql, "("+strings.Join(dataSql, " , ")+")")
}
} else {
for i := 0; i < len(this.data); i++ {
dataSql = append(dataSql, "?")
}
valSql = append(valSql, "("+strings.Join(dataSql, " , ")+")")
}
sql = helper.StringJoin("insert into ", dbName, " (", strings.Join(this.data, " , ")+")")
if len(this.data) == 0 {
return errors.New("参数错误,没有数据表")
}
if len(this.value) == 0 { if len(this.value) == 0 {
return errors.New("参数错误,条件值错误") return errors.New("参数错误,条件值错误")
} }
var valueSql = []string{}
var dataSql = []string{}
for i := 0; i < len(this.data); i++ {
dataSql = append(valueSql, "?")
}
dataSqlText := "(" + strings.Join(dataSql, " , ") + ")"
for i := 0; i < len(this.value); i++ {
valueSql = append(valueSql, dataSqlText)
setText := " values "
if len(valSql) > 1 {
setText = " value "
} }
sql = sql + " " + strings.Join(valueSql, " , ")
sql = helper.StringJoin("insert into ", dbName, " (", strings.Join(this.data, " , "), ")", setText, strings.Join(valSql, ","))
if this.debug { if this.debug {
log.Println("insert sql:", sql, this.value) log.Println("insert sql:", sql, this.value)
} }


+ 40
- 12
transaction_chain.go View File

@ -23,6 +23,7 @@ type TxQuery struct {
join [][]string //[["tablea as a","a.id=b.id","left"]] join [][]string //[["tablea as a","a.id=b.id","left"]]
data []string data []string
value []interface{} value []interface{}
save_data []map[string]interface{}
orderby string orderby string
groupby string groupby string
page int page int
@ -117,6 +118,14 @@ func (this *TxQuery) Value(value interface{}) *TxQuery {
this.value = append(this.value, value) this.value = append(this.value, value)
return this return this
} }
func (this *TxQuery) saveData(value map[string]interface{}) *TxQuery {
this.save_data = append(this.save_data, value)
return this
}
func (this *TxQuery) saveDatas(value []map[string]interface{}) *TxQuery {
this.save_data = append(this.save_data, value...)
return this
}
func (this *TxQuery) Values(values []interface{}) *TxQuery { func (this *TxQuery) Values(values []interface{}) *TxQuery {
this.value = append(this.value, values...) this.value = append(this.value, values...)
return this return this
@ -363,24 +372,43 @@ func (this *TxQuery) CreateAllStmt() error {
dbName := getTableName(this.dbname, this.table) dbName := getTableName(this.dbname, this.table)
var sql string var sql string
var dataSql = []string{}
var valSql = []string{}
if len(this.save_data) > 0 {
this.data = []string{}
this.value = []interface{}{}
for i, datum := range this.save_data {
for s, v := range datum {
if i == 0 {
this.data = append(this.data, s)
dataSql = append(dataSql, "?")
}
this.value = append(this.value, v)
}
valSql = append(valSql, "("+strings.Join(dataSql, " , ")+")")
}
} else {
for i := 0; i < len(this.data); i++ {
dataSql = append(dataSql, "?")
}
valSql = append(valSql, "("+strings.Join(dataSql, " , ")+")")
}
sql = helper.StringJoin("insert into ", dbName, " (", strings.Join(this.data, " , ")+")")
if len(this.data) == 0 {
return errors.New("参数错误,没有数据表")
}
if len(this.value) == 0 { if len(this.value) == 0 {
return errors.New("参数错误,条件值错误") return errors.New("参数错误,条件值错误")
} }
var valueSql = []string{}
var dataSql = []string{}
for i := 0; i < len(this.data); i++ {
dataSql = append(valueSql, "?")
setText := " values "
if len(valSql) > 1 {
setText = " value "
} }
dataSqlText := "(" + strings.Join(dataSql, " , ") + ")"
for i := 0; i < len(this.value); i++ {
valueSql = append(valueSql, dataSqlText)
}
sql = sql + " " + strings.Join(valueSql, " , ")
if this.debug {
log.Println("insert sql:", sql, this.value)
sql = helper.StringJoin("insert into ", dbName, " (", strings.Join(this.data, " , "), ")", setText, strings.Join(valSql, ","))
if len(this.value) == 0 {
return errors.New("参数错误,条件值错误")
} }
if this.debug { if this.debug {
log.Println("insert sql:", sql, this.value) log.Println("insert sql:", sql, this.value)
} }


Loading…
Cancel
Save