sql_executor.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. package xorm
  2. import (
  3. "database/sql"
  4. "strings"
  5. "time"
  6. )
  7. type SqlsExecutor struct {
  8. session *Session
  9. sqls interface{}
  10. parmas interface{}
  11. err error
  12. }
  13. func (sqlsExecutor *SqlsExecutor) Execute() ([][]map[string]interface{}, map[string][]map[string]interface{}, error) {
  14. defer sqlsExecutor.session.resetStatement()
  15. defer sqlsExecutor.session.Close()
  16. if sqlsExecutor.err != nil {
  17. return nil, nil, sqlsExecutor.err
  18. }
  19. var model_1_results *ResultMap
  20. var model_2_results sql.Result
  21. var err error
  22. sqlModel := 1
  23. if sqlsExecutor.session.isSqlFunc == true {
  24. err := sqlsExecutor.session.Begin()
  25. if err != nil {
  26. return nil, nil, err
  27. }
  28. }
  29. switch sqlsExecutor.sqls.(type) {
  30. case string:
  31. sqlStr := strings.TrimSpace(sqlsExecutor.sqls.(string))
  32. sqlCmd := strings.ToLower(strings.Split(sqlStr, " ")[0])
  33. if sqlsExecutor.parmas == nil {
  34. switch sqlCmd {
  35. case "select":
  36. model_1_results = sqlsExecutor.session.Sql(sqlStr).Query()
  37. sqlModel = 1
  38. case "insert", "delete", "update", "create", "drop":
  39. model_2_results, err = sqlsExecutor.session.Sql(sqlStr).Execute()
  40. sqlModel = 2
  41. default:
  42. sqlModel = 3
  43. }
  44. } else {
  45. switch sqlsExecutor.parmas.(type) {
  46. case []map[string]interface{}:
  47. parmaMap, _ := sqlsExecutor.parmas.([]map[string]interface{})
  48. key := NewV4().String() + time.Now().String()
  49. sqlsExecutor.session.engine.AddSql(key, sqlStr)
  50. switch sqlCmd {
  51. case "select":
  52. model_1_results = sqlsExecutor.session.SqlMapClient(key, &parmaMap[0]).Query()
  53. sqlModel = 1
  54. case "insert", "delete", "update", "create", "drop":
  55. model_2_results, err = sqlsExecutor.session.SqlMapClient(key, &parmaMap[0]).Execute()
  56. sqlModel = 2
  57. default:
  58. sqlModel = 3
  59. }
  60. sqlsExecutor.session.engine.RemoveSql(key)
  61. case map[string]interface{}:
  62. parmaMap, _ := sqlsExecutor.parmas.(map[string]interface{})
  63. key := NewV4().String() + time.Now().String()
  64. sqlsExecutor.session.engine.AddSql(key, sqlStr)
  65. switch sqlCmd {
  66. case "select":
  67. model_1_results = sqlsExecutor.session.SqlMapClient(key, &parmaMap).Query()
  68. sqlModel = 1
  69. case "insert", "delete", "update", "create", "drop":
  70. model_2_results, err = sqlsExecutor.session.SqlMapClient(key, &parmaMap).Execute()
  71. sqlModel = 2
  72. default:
  73. sqlModel = 3
  74. }
  75. sqlsExecutor.session.engine.RemoveSql(key)
  76. default:
  77. if sqlsExecutor.session.isSqlFunc == true {
  78. err1 := sqlsExecutor.session.Rollback()
  79. if err1 != nil {
  80. return nil, nil, err1
  81. }
  82. }
  83. return nil, nil, ErrParamsType
  84. }
  85. }
  86. sqlsExecutor.session.isSqlFunc = true
  87. resultSlice := make([][]map[string]interface{}, 1)
  88. if sqlModel == 1 {
  89. if model_1_results.Error != nil {
  90. if sqlsExecutor.session.isSqlFunc == true {
  91. err1 := sqlsExecutor.session.Rollback()
  92. if err1 != nil {
  93. return nil, nil, err1
  94. }
  95. }
  96. return nil, nil, model_1_results.Error
  97. }
  98. resultSlice[0] = make([]map[string]interface{}, len(model_1_results.Result))
  99. resultSlice[0] = model_1_results.Result
  100. if sqlsExecutor.session.isSqlFunc == true {
  101. err1 := sqlsExecutor.session.Commit()
  102. if err1 != nil {
  103. return nil, nil, err1
  104. }
  105. }
  106. return resultSlice, nil, nil
  107. } else if sqlModel == 2 {
  108. if err != nil {
  109. if sqlsExecutor.session.isSqlFunc == true {
  110. err1 := sqlsExecutor.session.Rollback()
  111. if err1 != nil {
  112. return nil, nil, err1
  113. }
  114. }
  115. return nil, nil, err
  116. }
  117. resultMap := make([]map[string]interface{}, 1)
  118. resultMap[0] = make(map[string]interface{})
  119. //todo all database support LastInsertId
  120. LastInsertId, _ := model_2_results.LastInsertId()
  121. resultMap[0]["LastInsertId"] = LastInsertId
  122. RowsAffected, err := model_2_results.RowsAffected()
  123. if err != nil {
  124. return nil, nil, err
  125. }
  126. resultMap[0]["RowsAffected"] = RowsAffected
  127. resultSlice[0] = resultMap
  128. if sqlsExecutor.session.isSqlFunc == true {
  129. err1 := sqlsExecutor.session.Commit()
  130. if err1 != nil {
  131. return nil, nil, err1
  132. }
  133. }
  134. return resultSlice, nil, nil
  135. } else {
  136. resultSlice[0] = nil
  137. }
  138. case []string:
  139. sqlsSlice := sqlsExecutor.sqls.([]string)
  140. n := len(sqlsSlice)
  141. resultSlice := make([][]map[string]interface{}, n)
  142. parmaSlice := make([]map[string]interface{}, n)
  143. if sqlsExecutor.parmas == nil {
  144. for i, _ := range sqlsSlice {
  145. sqlsExecutor.session.isSqlFunc = true
  146. sqlStr := strings.TrimSpace(sqlsSlice[i])
  147. sqlCmd := strings.ToLower(strings.Split(sqlStr, " ")[0])
  148. switch sqlCmd {
  149. case "select":
  150. model_1_results = sqlsExecutor.session.Sql(sqlStr).Query()
  151. sqlModel = 1
  152. case "insert", "delete", "update", "create", "drop":
  153. model_2_results, err = sqlsExecutor.session.Sql(sqlStr).Execute()
  154. sqlModel = 2
  155. default:
  156. sqlModel = 3
  157. }
  158. sqlsExecutor.session.isSqlFunc = true
  159. if sqlModel == 1 {
  160. if model_1_results.Error != nil {
  161. if sqlsExecutor.session.isSqlFunc == true {
  162. err1 := sqlsExecutor.session.Rollback()
  163. if err1 != nil {
  164. return nil, nil, err1
  165. }
  166. }
  167. return nil, nil, model_1_results.Error
  168. }
  169. resultSlice[i] = model_1_results.Result
  170. } else if sqlModel == 2 {
  171. if err != nil {
  172. if sqlsExecutor.session.isSqlFunc == true {
  173. err1 := sqlsExecutor.session.Rollback()
  174. if err1 != nil {
  175. return nil, nil, err1
  176. }
  177. }
  178. return nil, nil, err
  179. }
  180. resultMap := make([]map[string]interface{}, 1)
  181. resultMap[0] = make(map[string]interface{})
  182. //todo all database support LastInsertId
  183. LastInsertId, _ := model_2_results.LastInsertId()
  184. resultMap[0]["LastInsertId"] = LastInsertId
  185. RowsAffected, err := model_2_results.RowsAffected()
  186. if err != nil {
  187. if sqlsExecutor.session.isSqlFunc == true {
  188. err1 := sqlsExecutor.session.Rollback()
  189. if err1 != nil {
  190. return nil, nil, err1
  191. }
  192. }
  193. return nil, nil, err
  194. }
  195. resultMap[0]["RowsAffected"] = RowsAffected
  196. resultSlice[i] = make([]map[string]interface{}, 1)
  197. resultSlice[i] = resultMap
  198. } else {
  199. resultSlice[i] = nil
  200. }
  201. }
  202. } else {
  203. switch sqlsExecutor.parmas.(type) {
  204. case []map[string]interface{}:
  205. parmaSlice = sqlsExecutor.parmas.([]map[string]interface{})
  206. default:
  207. if sqlsExecutor.session.isSqlFunc == true {
  208. err1 := sqlsExecutor.session.Rollback()
  209. if err1 != nil {
  210. return nil, nil, err1
  211. }
  212. }
  213. return nil, nil, ErrParamsType
  214. }
  215. for i, _ := range sqlsSlice {
  216. sqlsExecutor.session.isSqlFunc = true
  217. sqlStr := strings.TrimSpace(sqlsSlice[i])
  218. sqlCmd := strings.ToLower(strings.Split(sqlStr, " ")[0])
  219. if parmaSlice[i] == nil {
  220. switch sqlCmd {
  221. case "select":
  222. model_1_results = sqlsExecutor.session.Sql(sqlStr).Query()
  223. sqlModel = 1
  224. case "insert", "delete", "update", "create", "drop":
  225. model_2_results, err = sqlsExecutor.session.Sql(sqlStr).Execute()
  226. sqlModel = 2
  227. default:
  228. sqlModel = 3
  229. }
  230. } else {
  231. key := NewV4().String() + time.Now().String()
  232. sqlsExecutor.session.engine.AddSql(key, sqlStr)
  233. switch sqlCmd {
  234. case "select":
  235. model_1_results = sqlsExecutor.session.SqlMapClient(key, &parmaSlice[i]).Query()
  236. sqlModel = 1
  237. case "insert", "delete", "update", "create", "drop":
  238. model_2_results, err = sqlsExecutor.session.SqlMapClient(key, &parmaSlice[i]).Execute()
  239. sqlModel = 2
  240. default:
  241. sqlModel = 3
  242. }
  243. sqlsExecutor.session.engine.RemoveSql(key)
  244. }
  245. sqlsExecutor.session.isSqlFunc = true
  246. if sqlModel == 1 {
  247. if model_1_results.Error != nil {
  248. if sqlsExecutor.session.isSqlFunc == true {
  249. err1 := sqlsExecutor.session.Rollback()
  250. if err1 != nil {
  251. return nil, nil, err1
  252. }
  253. }
  254. return nil, nil, model_1_results.Error
  255. }
  256. resultSlice[i] = model_1_results.Result
  257. } else if sqlModel == 2 {
  258. if err != nil {
  259. if sqlsExecutor.session.isSqlFunc == true {
  260. err1 := sqlsExecutor.session.Rollback()
  261. if err1 != nil {
  262. return nil, nil, err1
  263. }
  264. }
  265. return nil, nil, err
  266. }
  267. resultMap := make([]map[string]interface{}, 1)
  268. resultMap[0] = make(map[string]interface{})
  269. //todo all database support LastInsertId
  270. LastInsertId, _ := model_2_results.LastInsertId()
  271. resultMap[0]["LastInsertId"] = LastInsertId
  272. RowsAffected, err := model_2_results.RowsAffected()
  273. if err != nil {
  274. if sqlsExecutor.session.isSqlFunc == true {
  275. err1 := sqlsExecutor.session.Rollback()
  276. if err1 != nil {
  277. return nil, nil, err1
  278. }
  279. }
  280. return nil, nil, err
  281. }
  282. resultMap[0]["RowsAffected"] = RowsAffected
  283. resultSlice[i] = make([]map[string]interface{}, 1)
  284. resultSlice[i] = resultMap
  285. } else {
  286. resultSlice[i] = nil
  287. }
  288. }
  289. }
  290. if sqlsExecutor.session.isSqlFunc == true {
  291. err1 := sqlsExecutor.session.Commit()
  292. if err1 != nil {
  293. return nil, nil, err1
  294. }
  295. }
  296. return resultSlice, nil, nil
  297. case map[string]string:
  298. sqlsMap := sqlsExecutor.sqls.(map[string]string)
  299. n := len(sqlsMap)
  300. resultsMap := make(map[string][]map[string]interface{}, n)
  301. parmasMap := make(map[string]map[string]interface{}, n)
  302. if sqlsExecutor.parmas == nil {
  303. for k, _ := range sqlsMap {
  304. sqlsExecutor.session.isSqlFunc = true
  305. sqlStr := strings.TrimSpace(sqlsMap[k])
  306. sqlCmd := strings.ToLower(strings.Split(sqlStr, " ")[0])
  307. switch sqlCmd {
  308. case "select":
  309. sqlModel = 1
  310. model_1_results = sqlsExecutor.session.Sql(sqlStr).Query()
  311. case "insert", "delete", "update", "create", "drop":
  312. sqlModel = 2
  313. model_2_results, err = sqlsExecutor.session.Sql(sqlStr).Execute()
  314. default:
  315. sqlModel = 3
  316. }
  317. sqlsExecutor.session.isSqlFunc = true
  318. if sqlModel == 1 {
  319. if model_1_results.Error != nil {
  320. if sqlsExecutor.session.isSqlFunc == true {
  321. err1 := sqlsExecutor.session.Rollback()
  322. if err1 != nil {
  323. return nil, nil, err1
  324. }
  325. }
  326. return nil, nil, model_1_results.Error
  327. }
  328. resultsMap[k] = model_1_results.Result
  329. } else if sqlModel == 2 {
  330. if err != nil {
  331. if sqlsExecutor.session.isSqlFunc == true {
  332. err1 := sqlsExecutor.session.Rollback()
  333. if err1 != nil {
  334. return nil, nil, err1
  335. }
  336. }
  337. return nil, nil, err
  338. }
  339. resultMap := make([]map[string]interface{}, 1)
  340. resultMap[0] = make(map[string]interface{})
  341. //todo all database support LastInsertId
  342. LastInsertId, _ := model_2_results.LastInsertId()
  343. resultMap[0]["LastInsertId"] = LastInsertId
  344. RowsAffected, err := model_2_results.RowsAffected()
  345. if err != nil {
  346. if sqlsExecutor.session.isSqlFunc == true {
  347. err1 := sqlsExecutor.session.Rollback()
  348. if err1 != nil {
  349. return nil, nil, err1
  350. }
  351. }
  352. return nil, nil, err
  353. }
  354. resultMap[0]["RowsAffected"] = RowsAffected
  355. resultsMap[k] = make([]map[string]interface{}, 1)
  356. resultsMap[k] = resultMap
  357. } else {
  358. resultsMap[k] = nil
  359. }
  360. }
  361. } else {
  362. switch sqlsExecutor.parmas.(type) {
  363. case map[string]map[string]interface{}:
  364. parmasMap = sqlsExecutor.parmas.(map[string]map[string]interface{})
  365. default:
  366. if sqlsExecutor.session.isSqlFunc == true {
  367. err1 := sqlsExecutor.session.Rollback()
  368. if err1 != nil {
  369. return nil, nil, err1
  370. }
  371. }
  372. return nil, nil, ErrParamsType
  373. }
  374. for k, _ := range sqlsMap {
  375. sqlsExecutor.session.isSqlFunc = true
  376. sqlStr := strings.TrimSpace(sqlsMap[k])
  377. sqlCmd := strings.ToLower(strings.Split(sqlStr, " ")[0])
  378. if parmasMap[k] == nil {
  379. switch sqlCmd {
  380. case "select":
  381. sqlModel = 1
  382. model_1_results = sqlsExecutor.session.Sql(sqlStr).Query()
  383. case "insert", "delete", "update", "create", "drop":
  384. sqlModel = 2
  385. model_2_results, err = sqlsExecutor.session.Sql(sqlStr).Execute()
  386. default:
  387. sqlModel = 3
  388. }
  389. } else {
  390. key := NewV4().String() + time.Now().String()
  391. sqlsExecutor.session.engine.AddSql(key, sqlStr)
  392. parmaMap := parmasMap[k]
  393. switch sqlCmd {
  394. case "select":
  395. sqlModel = 1
  396. model_1_results = sqlsExecutor.session.SqlMapClient(key, &parmaMap).Query()
  397. case "insert", "delete", "update", "create", "drop":
  398. sqlModel = 2
  399. model_2_results, err = sqlsExecutor.session.SqlMapClient(key, &parmaMap).Execute()
  400. default:
  401. sqlModel = 3
  402. }
  403. sqlsExecutor.session.engine.RemoveSql(key)
  404. }
  405. sqlsExecutor.session.isSqlFunc = true
  406. if sqlModel == 1 {
  407. if model_1_results.Error != nil {
  408. if sqlsExecutor.session.isSqlFunc == true {
  409. err1 := sqlsExecutor.session.Rollback()
  410. if err1 != nil {
  411. return nil, nil, err1
  412. }
  413. }
  414. return nil, nil, model_1_results.Error
  415. }
  416. resultsMap[k] = model_1_results.Result
  417. } else if sqlModel == 2 {
  418. if err != nil {
  419. if sqlsExecutor.session.isSqlFunc == true {
  420. err1 := sqlsExecutor.session.Rollback()
  421. if err1 != nil {
  422. return nil, nil, err1
  423. }
  424. }
  425. return nil, nil, err
  426. }
  427. resultMap := make([]map[string]interface{}, 1)
  428. resultMap[0] = make(map[string]interface{})
  429. //todo all database support LastInsertId
  430. LastInsertId, _ := model_2_results.LastInsertId()
  431. resultMap[0]["LastInsertId"] = LastInsertId
  432. RowsAffected, err := model_2_results.RowsAffected()
  433. if err != nil {
  434. if sqlsExecutor.session.isSqlFunc == true {
  435. err1 := sqlsExecutor.session.Rollback()
  436. if err1 != nil {
  437. return nil, nil, err1
  438. }
  439. }
  440. return nil, nil, err
  441. }
  442. resultMap[0]["RowsAffected"] = RowsAffected
  443. resultsMap[k] = make([]map[string]interface{}, 1)
  444. resultsMap[k] = resultMap
  445. } else {
  446. resultsMap[k] = nil
  447. }
  448. }
  449. }
  450. if sqlsExecutor.session.isSqlFunc == true {
  451. err1 := sqlsExecutor.session.Commit()
  452. if err1 != nil {
  453. return nil, nil, err1
  454. }
  455. }
  456. return nil, resultsMap, nil
  457. }
  458. return nil, nil, nil
  459. }