From dcf3856862e1841ef1a6cb135eb8a936e6ba0f98 Mon Sep 17 00:00:00 2001 From: zhenghaorong Date: Fri, 2 Sep 2022 14:20:41 +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 | 53 ++++++++++++++++++++++++++++++++++---------- transaction_chain.go | 52 +++++++++++++++++++++++++++++++++---------- 2 files changed, 81 insertions(+), 24 deletions(-) diff --git a/chain.go b/chain.go index 7be6b15..7d56a1d 100644 --- a/chain.go +++ b/chain.go @@ -20,7 +20,8 @@ type Query struct { title string where []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 value []interface{} orderby string @@ -111,6 +112,14 @@ func (this *Query) WhereOr(where string) *Query { this.where_or = append(this.where_or, where) 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 { this.value = append(this.value, value) return this @@ -328,22 +337,42 @@ func (this *Query) CreateAllStmt() error { dbName := getTableName(this.dbname, this.table) 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 { 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 { log.Println("insert sql:", sql, this.value) } diff --git a/transaction_chain.go b/transaction_chain.go index 8db21c9..3192430 100644 --- a/transaction_chain.go +++ b/transaction_chain.go @@ -23,6 +23,7 @@ type TxQuery struct { join [][]string //[["tablea as a","a.id=b.id","left"]] data []string value []interface{} + save_data []map[string]interface{} orderby string groupby string page int @@ -117,6 +118,14 @@ func (this *TxQuery) Value(value interface{}) *TxQuery { this.value = append(this.value, value) 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 { this.value = append(this.value, values...) return this @@ -363,24 +372,43 @@ func (this *TxQuery) CreateAllStmt() error { dbName := getTableName(this.dbname, this.table) 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 { 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 { log.Println("insert sql:", sql, this.value) }