Skip to content

Commit

Permalink
fix bugs on Sqls,SqlMapsClient,SqlTemplatesClient
Browse files Browse the repository at this point in the history
  • Loading branch information
xormplus committed Feb 3, 2018
1 parent fb84300 commit c3b43ec
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 29 deletions.
6 changes: 3 additions & 3 deletions engineplus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
}
31 changes: 23 additions & 8 deletions sql_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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])

Expand All @@ -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 {
Expand Down Expand Up @@ -395,6 +408,7 @@ func (sqlsExecutor *SqlsExecutor) Execute() ([][]map[string]interface{}, map[str
}

}

} else {
switch sqlsExecutor.parmas.(type) {
case map[string]map[string]interface{}:
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
38 changes: 28 additions & 10 deletions sqlmaps_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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])
Expand All @@ -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 {
Expand Down Expand Up @@ -210,7 +227,6 @@ func (sqlMapsExecutor *SqlMapsExecutor) Execute() ([][]map[string]interface{}, m
}

}

} else {
switch sqlMapsExecutor.parmas.(type) {
case []map[string]interface{}:
Expand All @@ -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])
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -304,7 +321,6 @@ func (sqlMapsExecutor *SqlMapsExecutor) Execute() ([][]map[string]interface{}, m
resultSlice[i] = nil
}
}

}

if sqlMapsExecutor.session.isSqlFunc == true {
Expand All @@ -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])
Expand All @@ -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 {
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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 {
Expand Down
29 changes: 21 additions & 8 deletions sqltemplates_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -540,7 +554,6 @@ func (sqlTemplatesExecutor *SqlTemplatesExecutor) Execute() ([][]map[string]inte
resultsMap[k] = nil
}
}

}

if sqlTemplatesExecutor.session.isSqlFunc == true {
Expand Down

0 comments on commit c3b43ec

Please sign in to comment.