From 6a197add5714a4d3d7a7943ef41fd4dcb76f56a0 Mon Sep 17 00:00:00 2001 From: zhenghaorong Date: Fri, 2 Sep 2022 11:27:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=89=B9=E9=87=8F=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- chain.go | 55 ++++++++++++++++++++++++++++++++++++++++++ transaction_chain.go | 57 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+) diff --git a/chain.go b/chain.go index 6c1bf7a..7be6b15 100644 --- a/chain.go +++ b/chain.go @@ -318,6 +318,51 @@ func (this *Query) UpdateStmt() error { 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 func (this *Query) CreateStmt() error { @@ -512,3 +557,13 @@ func (this *Query) Create() (int64, error) { 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) +} diff --git a/transaction_chain.go b/transaction_chain.go index 5266d2f..8db21c9 100644 --- a/transaction_chain.go +++ b/transaction_chain.go @@ -353,6 +353,49 @@ func (this *TxQuery) CreateStmt() error { 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 func (this *TxQuery) DeleteStmt() error { @@ -477,6 +520,20 @@ func (this *TxQuery) Create() (int64, error) { 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) +} + /** * 提交 */