sqltemplates_executor.go 16 KB

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