Browse Source

增加批量添加

master
zhenghaorong 2 years ago
parent
commit
6a197add57
2 changed files with 112 additions and 0 deletions
  1. +55
    -0
      chain.go
  2. +57
    -0
      transaction_chain.go

+ 55
- 0
chain.go View File

@ -318,6 +318,51 @@ func (this *Query) UpdateStmt() error {
return nil return nil
} }
// 拼批量插入sql
func (this *Query) CreateAllStmt() error {
if this.dbname == "" && this.table == "" {
return errors.New("参数错误,没有数据表")
}
dbName := getTableName(this.dbname, this.table)
var sql string
sql = helper.StringJoin("insert into ", dbName, " (", strings.Join(this.data, " , ")+")")
if len(this.value) == 0 {
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)
}
sql = sql + " " + strings.Join(valueSql, " , ")
if this.debug {
log.Println("insert sql:", sql, this.value)
}
if this.conn == nil {
this.conn = DB
}
stmt, err = this.conn.Prepare(sql)
if err != nil {
return err
}
this.stmt = stmt
return nil
}
// 拼插入sql // 拼插入sql
func (this *Query) CreateStmt() error { func (this *Query) CreateStmt() error {
@ -512,3 +557,13 @@ func (this *Query) Create() (int64, error) {
return StmtForInsertExec(this.stmt, this.value) return StmtForInsertExec(this.stmt, this.value)
} }
func (this *Query) CreateAll() (int64, error) {
err := this.CreateAllStmt()
if err != nil {
return 0, err
}
return StmtForInsertExec(this.stmt, this.value)
}

+ 57
- 0
transaction_chain.go View File

@ -353,6 +353,49 @@ func (this *TxQuery) CreateStmt() error {
return nil return nil
} }
// 拼批量插入sql
func (this *TxQuery) CreateAllStmt() error {
if this.dbname == "" && this.table == "" {
return errors.New("参数错误,没有数据表")
}
dbName := getTableName(this.dbname, this.table)
var sql string
sql = helper.StringJoin("insert into ", dbName, " (", strings.Join(this.data, " , ")+")")
if len(this.value) == 0 {
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)
}
sql = sql + " " + strings.Join(valueSql, " , ")
if this.debug {
log.Println("insert sql:", sql, this.value)
}
if this.debug {
log.Println("insert sql:", sql, this.value)
}
stmt, err = this.tx.Prepare(sql)
if err != nil {
return err
}
this.stmt = stmt
return nil
}
// 拼删除sql // 拼删除sql
func (this *TxQuery) DeleteStmt() error { func (this *TxQuery) DeleteStmt() error {
@ -477,6 +520,20 @@ func (this *TxQuery) Create() (int64, error) {
return StmtForInsertExec(this.stmt, this.value) return StmtForInsertExec(this.stmt, this.value)
} }
/**
* 执行批量写入
* return is_insert error
*/
func (this *TxQuery) CreateAll() (int64, error) {
err := this.CreateAllStmt()
if err != nil {
return 0, err
}
return StmtForInsertExec(this.stmt, this.value)
}
/** /**
* 提交 * 提交
*/ */


Loading…
Cancel
Save