From c3b43ecb4bab83c01bb40bfca5793cc9978359f1 Mon Sep 17 00:00:00 2001 From: xormplus Date: Sat, 3 Feb 2018 13:09:55 +0800 Subject: [PATCH] =?UTF-8?q?fix=20bugs=20on=20Sqls=EF=BC=8CSqlMapsClient?= =?UTF-8?q?=EF=BC=8CSqlTemplatesClient?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- engineplus.go | 6 +++--- sql_executor.go | 31 +++++++++++++++++++++++-------- sqlmaps_executor.go | 38 ++++++++++++++++++++++++++++---------- sqltemplates_executor.go | 29 +++++++++++++++++++++-------- 4 files changed, 75 insertions(+), 29 deletions(-) diff --git a/engineplus.go b/engineplus.go index 418661b..63f9f2f 100644 --- a/engineplus.go +++ b/engineplus.go @@ -58,21 +58,21 @@ func JSONString(v interface{}, IndentJSON bool) (string, error) { func (engine *Engine) Sqls(sqls interface{}, parmas ...interface{}) *SqlsExecutor { session := engine.NewSession() - session.isAutoClose = true + session.isAutoClose = false session.isSqlFunc = true return session.Sqls(sqls, parmas...) } func (engine *Engine) SqlMapsClient(sqlkeys interface{}, parmas ...interface{}) *SqlMapsExecutor { session := engine.NewSession() - session.isAutoClose = true + session.isAutoClose = false session.isSqlFunc = true return session.SqlMapsClient(sqlkeys, parmas...) } func (engine *Engine) SqlTemplatesClient(sqlkeys interface{}, parmas ...interface{}) *SqlTemplatesExecutor { session := engine.NewSession() - session.isAutoClose = true + session.isAutoClose = false session.isSqlFunc = true return session.SqlTemplatesClient(sqlkeys, parmas...) } diff --git a/sql_executor.go b/sql_executor.go index 09a27fc..e0fb009 100644 --- a/sql_executor.go +++ b/sql_executor.go @@ -15,9 +15,7 @@ type SqlsExecutor struct { func (sqlsExecutor *SqlsExecutor) Execute() ([][]map[string]interface{}, map[string][]map[string]interface{}, error) { defer sqlsExecutor.session.resetStatement() - if sqlsExecutor.session.isAutoClose { - defer sqlsExecutor.session.Close() - } + defer sqlsExecutor.session.Close() if sqlsExecutor.err != nil { return nil, nil, sqlsExecutor.err @@ -96,7 +94,7 @@ func (sqlsExecutor *SqlsExecutor) Execute() ([][]map[string]interface{}, map[str return nil, nil, ErrParamsType } } - + sqlsExecutor.session.isSqlFunc = true resultSlice := make([][]map[string]interface{}, 1) if sqlModel == 1 { @@ -112,6 +110,12 @@ func (sqlsExecutor *SqlsExecutor) Execute() ([][]map[string]interface{}, map[str resultSlice[0] = make([]map[string]interface{}, len(model_1_results.Results)) resultSlice[0] = model_1_results.Results + if sqlsExecutor.session.isSqlFunc == true { + err1 := sqlsExecutor.session.Commit() + if err1 != nil { + return nil, nil, err1 + } + } return resultSlice, nil, nil } else if sqlModel == 2 { if err != nil { @@ -137,6 +141,12 @@ func (sqlsExecutor *SqlsExecutor) Execute() ([][]map[string]interface{}, map[str } resultMap[0]["RowsAffected"] = RowsAffected resultSlice[0] = resultMap + if sqlsExecutor.session.isSqlFunc == true { + err1 := sqlsExecutor.session.Commit() + if err1 != nil { + return nil, nil, err1 + } + } return resultSlice, nil, nil } else { resultSlice[0] = nil @@ -150,6 +160,7 @@ func (sqlsExecutor *SqlsExecutor) Execute() ([][]map[string]interface{}, map[str if sqlsExecutor.parmas == nil { for i, _ := range sqlsSlice { + sqlsExecutor.session.isSqlFunc = true sqlStr := strings.TrimSpace(sqlsSlice[i]) sqlCmd := strings.ToLower(strings.Split(sqlStr, " ")[0]) switch sqlCmd { @@ -162,7 +173,7 @@ func (sqlsExecutor *SqlsExecutor) Execute() ([][]map[string]interface{}, map[str default: sqlModel = 3 } - + sqlsExecutor.session.isSqlFunc = true if sqlModel == 1 { if model_1_results.Error != nil { if sqlsExecutor.session.isSqlFunc == true { @@ -230,6 +241,7 @@ func (sqlsExecutor *SqlsExecutor) Execute() ([][]map[string]interface{}, map[str } for i, _ := range sqlsSlice { + sqlsExecutor.session.isSqlFunc = true sqlStr := strings.TrimSpace(sqlsSlice[i]) sqlCmd := strings.ToLower(strings.Split(sqlStr, " ")[0]) if parmaSlice[i] == nil { @@ -258,7 +270,7 @@ func (sqlsExecutor *SqlsExecutor) Execute() ([][]map[string]interface{}, map[str } sqlsExecutor.session.engine.RemoveSql(key) } - + sqlsExecutor.session.isSqlFunc = true if sqlModel == 1 { if model_1_results.Error != nil { if sqlsExecutor.session.isSqlFunc == true { @@ -328,6 +340,7 @@ func (sqlsExecutor *SqlsExecutor) Execute() ([][]map[string]interface{}, map[str if sqlsExecutor.parmas == nil { for k, _ := range sqlsMap { + sqlsExecutor.session.isSqlFunc = true sqlStr := strings.TrimSpace(sqlsMap[k]) sqlCmd := strings.ToLower(strings.Split(sqlStr, " ")[0]) @@ -343,7 +356,7 @@ func (sqlsExecutor *SqlsExecutor) Execute() ([][]map[string]interface{}, map[str default: sqlModel = 3 } - + sqlsExecutor.session.isSqlFunc = true if sqlModel == 1 { if model_1_results.Error != nil { if sqlsExecutor.session.isSqlFunc == true { @@ -395,6 +408,7 @@ func (sqlsExecutor *SqlsExecutor) Execute() ([][]map[string]interface{}, map[str } } + } else { switch sqlsExecutor.parmas.(type) { case map[string]map[string]interface{}: @@ -411,6 +425,7 @@ func (sqlsExecutor *SqlsExecutor) Execute() ([][]map[string]interface{}, map[str } for k, _ := range sqlsMap { + sqlsExecutor.session.isSqlFunc = true sqlStr := strings.TrimSpace(sqlsMap[k]) sqlCmd := strings.ToLower(strings.Split(sqlStr, " ")[0]) if parmasMap[k] == nil { @@ -444,7 +459,7 @@ func (sqlsExecutor *SqlsExecutor) Execute() ([][]map[string]interface{}, map[str } sqlsExecutor.session.engine.RemoveSql(key) } - + sqlsExecutor.session.isSqlFunc = true if sqlModel == 1 { if model_1_results.Error != nil { if sqlsExecutor.session.isSqlFunc == true { diff --git a/sqlmaps_executor.go b/sqlmaps_executor.go index 2f813f9..ffe6a91 100644 --- a/sqlmaps_executor.go +++ b/sqlmaps_executor.go @@ -14,9 +14,7 @@ type SqlMapsExecutor struct { func (sqlMapsExecutor *SqlMapsExecutor) Execute() ([][]map[string]interface{}, map[string][]map[string]interface{}, error) { defer sqlMapsExecutor.session.resetStatement() - if sqlMapsExecutor.session.isAutoClose { - defer sqlMapsExecutor.session.Close() - } + defer sqlMapsExecutor.session.Close() if sqlMapsExecutor.err != nil { return nil, nil, sqlMapsExecutor.err @@ -93,7 +91,7 @@ func (sqlMapsExecutor *SqlMapsExecutor) Execute() ([][]map[string]interface{}, m return nil, nil, ErrParamsType } } - + sqlMapsExecutor.session.isSqlFunc = true resultSlice := make([][]map[string]interface{}, 1) if sqlModel == 1 { @@ -108,6 +106,14 @@ func (sqlMapsExecutor *SqlMapsExecutor) Execute() ([][]map[string]interface{}, m } resultSlice[0] = model_1_results.Results + + if sqlMapsExecutor.session.isSqlFunc == true { + err1 := sqlMapsExecutor.session.Commit() + if err1 != nil { + return nil, nil, err1 + } + } + return resultSlice, nil, nil } else if sqlModel == 2 { if err != nil { @@ -133,10 +139,20 @@ func (sqlMapsExecutor *SqlMapsExecutor) Execute() ([][]map[string]interface{}, m } resultMap[0]["RowsAffected"] = RowsAffected resultSlice[0] = resultMap + + if sqlMapsExecutor.session.isSqlFunc == true { + err1 := sqlMapsExecutor.session.Commit() + if err1 != nil { + return nil, nil, err1 + } + } + return resultSlice, nil, nil } else { resultSlice[0] = nil } + + case []string: sqlkeysSlice := sqlMapsExecutor.sqlkeys.([]string) n := len(sqlkeysSlice) @@ -145,6 +161,7 @@ func (sqlMapsExecutor *SqlMapsExecutor) Execute() ([][]map[string]interface{}, m if sqlMapsExecutor.parmas == nil { for i, _ := range sqlkeysSlice { + sqlMapsExecutor.session.isSqlFunc = true sqlStr := sqlMapsExecutor.session.engine.GetSql(sqlkeysSlice[i]) sqlStr = strings.TrimSpace(sqlStr) sqlCmd := strings.ToLower(strings.Split(sqlStr, " ")[0]) @@ -159,7 +176,7 @@ func (sqlMapsExecutor *SqlMapsExecutor) Execute() ([][]map[string]interface{}, m default: sqlModel = 3 } - + sqlMapsExecutor.session.isSqlFunc = true if sqlModel == 1 { if model_1_results.Error != nil { if sqlMapsExecutor.session.isSqlFunc == true { @@ -210,7 +227,6 @@ func (sqlMapsExecutor *SqlMapsExecutor) Execute() ([][]map[string]interface{}, m } } - } else { switch sqlMapsExecutor.parmas.(type) { case []map[string]interface{}: @@ -227,6 +243,7 @@ func (sqlMapsExecutor *SqlMapsExecutor) Execute() ([][]map[string]interface{}, m } for i, _ := range sqlkeysSlice { + sqlMapsExecutor.session.isSqlFunc = true sqlStr := sqlMapsExecutor.session.engine.GetSql(sqlkeysSlice[i]) sqlStr = strings.TrimSpace(sqlStr) sqlCmd := strings.ToLower(strings.Split(sqlStr, " ")[0]) @@ -254,7 +271,7 @@ func (sqlMapsExecutor *SqlMapsExecutor) Execute() ([][]map[string]interface{}, m sqlModel = 3 } } - + sqlMapsExecutor.session.isSqlFunc = true if sqlModel == 1 { if model_1_results.Error != nil { if sqlMapsExecutor.session.isSqlFunc == true { @@ -304,7 +321,6 @@ func (sqlMapsExecutor *SqlMapsExecutor) Execute() ([][]map[string]interface{}, m resultSlice[i] = nil } } - } if sqlMapsExecutor.session.isSqlFunc == true { @@ -324,6 +340,7 @@ func (sqlMapsExecutor *SqlMapsExecutor) Execute() ([][]map[string]interface{}, m if sqlMapsExecutor.parmas == nil { for k, _ := range sqlkeysMap { + sqlMapsExecutor.session.isSqlFunc = true sqlStr := sqlMapsExecutor.session.engine.GetSql(sqlkeysMap[k]) sqlStr = strings.TrimSpace(sqlStr) sqlCmd := strings.ToLower(strings.Split(sqlStr, " ")[0]) @@ -338,7 +355,7 @@ func (sqlMapsExecutor *SqlMapsExecutor) Execute() ([][]map[string]interface{}, m default: sqlModel = 3 } - + sqlMapsExecutor.session.isSqlFunc = true if sqlModel == 1 { if model_1_results.Error != nil { if sqlMapsExecutor.session.isSqlFunc == true { @@ -406,6 +423,7 @@ func (sqlMapsExecutor *SqlMapsExecutor) Execute() ([][]map[string]interface{}, m } for k, _ := range sqlkeysMap { + sqlMapsExecutor.session.isSqlFunc = true sqlStr := sqlMapsExecutor.session.engine.GetSql(sqlkeysMap[k]) sqlStr = strings.TrimSpace(sqlStr) sqlCmd := strings.ToLower(strings.Split(sqlStr, " ")[0]) @@ -433,7 +451,7 @@ func (sqlMapsExecutor *SqlMapsExecutor) Execute() ([][]map[string]interface{}, m sqlModel = 3 } } - + sqlMapsExecutor.session.isSqlFunc = true if sqlModel == 1 { if model_1_results.Error != nil { if sqlMapsExecutor.session.isSqlFunc == true { diff --git a/sqltemplates_executor.go b/sqltemplates_executor.go index 68dac65..b2c97ec 100644 --- a/sqltemplates_executor.go +++ b/sqltemplates_executor.go @@ -14,9 +14,7 @@ type SqlTemplatesExecutor struct { func (sqlTemplatesExecutor *SqlTemplatesExecutor) Execute() ([][]map[string]interface{}, map[string][]map[string]interface{}, error) { defer sqlTemplatesExecutor.session.resetStatement() - if sqlTemplatesExecutor.session.isAutoClose { - defer sqlTemplatesExecutor.session.Close() - } + defer sqlTemplatesExecutor.session.Close() if sqlTemplatesExecutor.err != nil { return nil, nil, sqlTemplatesExecutor.err @@ -131,6 +129,12 @@ func (sqlTemplatesExecutor *SqlTemplatesExecutor) Execute() ([][]map[string]inte } resultSlice[0] = model_1_results.Results + if sqlTemplatesExecutor.session.isSqlFunc == true { + err1 := sqlTemplatesExecutor.session.Commit() + if err1 != nil { + return nil, nil, err1 + } + } return resultSlice, nil, nil } else if sqlModel == 2 { if err != nil { @@ -150,6 +154,12 @@ func (sqlTemplatesExecutor *SqlTemplatesExecutor) Execute() ([][]map[string]inte } resultMap[0]["RowsAffected"] = RowsAffected resultSlice[0] = resultMap + if sqlTemplatesExecutor.session.isSqlFunc == true { + err1 := sqlTemplatesExecutor.session.Commit() + if err1 != nil { + return nil, nil, err1 + } + } return resultSlice, nil, nil } else { resultSlice[0] = nil @@ -163,6 +173,7 @@ func (sqlTemplatesExecutor *SqlTemplatesExecutor) Execute() ([][]map[string]inte if sqlTemplatesExecutor.parmas == nil { for i, _ := range sqlkeysSlice { + sqlTemplatesExecutor.session.isSqlFunc = true sqlStr, err := sqlTemplatesExecutor.session.engine.SqlTemplate.Execute(sqlkeysSlice[i]) if err != nil { if sqlTemplatesExecutor.session.isSqlFunc == true { @@ -186,7 +197,7 @@ func (sqlTemplatesExecutor *SqlTemplatesExecutor) Execute() ([][]map[string]inte default: sqlModel = 3 } - + sqlTemplatesExecutor.session.isSqlFunc = true if sqlModel == 1 { if model_1_results.Error != nil { if sqlTemplatesExecutor.session.isSqlFunc == true { @@ -255,6 +266,7 @@ func (sqlTemplatesExecutor *SqlTemplatesExecutor) Execute() ([][]map[string]inte } for i, _ := range sqlkeysSlice { + sqlTemplatesExecutor.session.isSqlFunc = true sqlStr, err := sqlTemplatesExecutor.session.engine.SqlTemplate.Execute(sqlkeysSlice[i], parmaSlice[i]) if err != nil { if sqlTemplatesExecutor.session.isSqlFunc == true { @@ -291,7 +303,7 @@ func (sqlTemplatesExecutor *SqlTemplatesExecutor) Execute() ([][]map[string]inte sqlModel = 3 } } - + sqlTemplatesExecutor.session.isSqlFunc = true if sqlModel == 1 { if model_1_results.Error != nil { if sqlTemplatesExecutor.session.isSqlFunc == true { @@ -362,6 +374,7 @@ func (sqlTemplatesExecutor *SqlTemplatesExecutor) Execute() ([][]map[string]inte if sqlTemplatesExecutor.parmas == nil { for k, _ := range sqlkeysMap { + sqlTemplatesExecutor.session.isSqlFunc = true sqlStr, err := sqlTemplatesExecutor.session.engine.SqlTemplate.Execute(sqlkeysMap[k]) if err != nil { if sqlTemplatesExecutor.session.isSqlFunc == true { @@ -385,7 +398,7 @@ func (sqlTemplatesExecutor *SqlTemplatesExecutor) Execute() ([][]map[string]inte default: sqlModel = 3 } - + sqlTemplatesExecutor.session.isSqlFunc = true if sqlModel == 1 { if model_1_results.Error != nil { if sqlTemplatesExecutor.session.isSqlFunc == true { @@ -454,6 +467,7 @@ func (sqlTemplatesExecutor *SqlTemplatesExecutor) Execute() ([][]map[string]inte } for k, _ := range sqlkeysMap { + sqlTemplatesExecutor.session.isSqlFunc = true sqlStr, err := sqlTemplatesExecutor.session.engine.SqlTemplate.Execute(sqlkeysMap[k], parmasMap[k]) if err != nil { if sqlTemplatesExecutor.session.isSqlFunc == true { @@ -490,7 +504,7 @@ func (sqlTemplatesExecutor *SqlTemplatesExecutor) Execute() ([][]map[string]inte sqlModel = 3 } } - + sqlTemplatesExecutor.session.isSqlFunc = true if sqlModel == 1 { if model_1_results.Error != nil { if sqlTemplatesExecutor.session.isSqlFunc == true { @@ -540,7 +554,6 @@ func (sqlTemplatesExecutor *SqlTemplatesExecutor) Execute() ([][]map[string]inte resultsMap[k] = nil } } - } if sqlTemplatesExecutor.session.isSqlFunc == true {