sqlmaps_executor.go 14 KB

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