session_plus.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  1. // Copyright 2015 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package xorm
  5. import (
  6. "database/sql"
  7. "encoding/json"
  8. "fmt"
  9. "os"
  10. "reflect"
  11. "regexp"
  12. "strings"
  13. "github.com/Chronokeeper/anyxml"
  14. "github.com/xormplus/core"
  15. "gopkg.in/flosch/pongo2.v3"
  16. )
  17. type ResultBean struct {
  18. Has bool
  19. Result interface{}
  20. Error error
  21. }
  22. func (resultBean *ResultBean) Json() (bool, string, error) {
  23. if resultBean.Error != nil {
  24. return resultBean.Has, "", resultBean.Error
  25. }
  26. if !resultBean.Has {
  27. return resultBean.Has, "", nil
  28. }
  29. result, err := JSONString(resultBean.Result, true)
  30. return resultBean.Has, result, err
  31. }
  32. func (resultBean *ResultBean) GetResult() (bool, interface{}, error) {
  33. return resultBean.Has, resultBean.Result, resultBean.Error
  34. }
  35. func (session *Session) GetFirst(bean interface{}) *ResultBean {
  36. has, err := session.Get(bean)
  37. r := &ResultBean{Has: has, Result: bean, Error: err}
  38. return r
  39. }
  40. func (resultBean *ResultBean) Xml() (bool, string, error) {
  41. if resultBean.Error != nil {
  42. return false, "", resultBean.Error
  43. }
  44. if !resultBean.Has {
  45. return resultBean.Has, "", nil
  46. }
  47. has, result, err := resultBean.Json()
  48. if err != nil {
  49. return false, "", err
  50. }
  51. if !has {
  52. return has, "", nil
  53. }
  54. var anydata = []byte(result)
  55. var i interface{}
  56. err = json.Unmarshal(anydata, &i)
  57. if err != nil {
  58. return false, "", err
  59. }
  60. resultByte, err := anyxml.Xml(i)
  61. if err != nil {
  62. return false, "", err
  63. }
  64. return resultBean.Has, string(resultByte), err
  65. }
  66. func (resultBean *ResultBean) XmlIndent(prefix string, indent string, recordTag string) (bool, string, error) {
  67. if resultBean.Error != nil {
  68. return false, "", resultBean.Error
  69. }
  70. if !resultBean.Has {
  71. return resultBean.Has, "", nil
  72. }
  73. has, result, err := resultBean.Json()
  74. if err != nil {
  75. return false, "", err
  76. }
  77. if !has {
  78. return has, "", nil
  79. }
  80. var anydata = []byte(result)
  81. var i interface{}
  82. err = json.Unmarshal(anydata, &i)
  83. if err != nil {
  84. return false, "", err
  85. }
  86. resultByte, err := anyxml.XmlIndent(i, prefix, indent, recordTag)
  87. if err != nil {
  88. return false, "", err
  89. }
  90. return resultBean.Has, string(resultByte), err
  91. }
  92. type ResultMap struct {
  93. Results []map[string]interface{}
  94. Error error
  95. }
  96. func (resultMap *ResultMap) List() ([]map[string]interface{}, error) {
  97. return resultMap.Results, resultMap.Error
  98. }
  99. func (resultMap *ResultMap) Count() (int, error) {
  100. if resultMap.Error != nil {
  101. return 0, resultMap.Error
  102. }
  103. if resultMap.Results == nil {
  104. return 0, nil
  105. }
  106. return len(resultMap.Results), nil
  107. }
  108. func (resultMap *ResultMap) ListPage(firstResult int, maxResults int) ([]map[string]interface{}, error) {
  109. if resultMap.Error != nil {
  110. return nil, resultMap.Error
  111. }
  112. if resultMap.Results == nil {
  113. return nil, nil
  114. }
  115. if firstResult > maxResults {
  116. return nil, ErrParamsFormat
  117. }
  118. if firstResult < 0 {
  119. return nil, ErrParamsFormat
  120. }
  121. if maxResults < 0 {
  122. return nil, ErrParamsFormat
  123. }
  124. if maxResults > len(resultMap.Results) {
  125. return nil, ErrParamsFormat
  126. }
  127. return resultMap.Results[(firstResult - 1):maxResults], resultMap.Error
  128. }
  129. func (resultMap *ResultMap) Json() (string, error) {
  130. if resultMap.Error != nil {
  131. return "", resultMap.Error
  132. }
  133. return JSONString(resultMap.Results, true)
  134. }
  135. func (resultMap *ResultMap) Xml() (string, error) {
  136. if resultMap.Error != nil {
  137. return "", resultMap.Error
  138. }
  139. results, err := anyxml.Xml(resultMap.Results)
  140. if err != nil {
  141. return "", err
  142. }
  143. return string(results), nil
  144. }
  145. func (resultMap *ResultMap) XmlIndent(prefix string, indent string, recordTag string) (string, error) {
  146. if resultMap.Error != nil {
  147. return "", resultMap.Error
  148. }
  149. results, err := anyxml.XmlIndent(resultMap.Results, prefix, indent, recordTag)
  150. if err != nil {
  151. return "", err
  152. }
  153. return string(results), nil
  154. }
  155. func (resultMap *ResultMap) SaveAsCSV(filename string, headers []string, perm os.FileMode) error {
  156. if resultMap.Error != nil {
  157. return resultMap.Error
  158. }
  159. dataset, err := NewDatasetWithData(headers, resultMap.Results, true)
  160. if err != nil {
  161. return err
  162. }
  163. csv, err := dataset.CSV()
  164. if err != nil {
  165. return err
  166. }
  167. return csv.WriteFile(filename, perm)
  168. }
  169. func (resultMap *ResultMap) SaveAsTSV(filename string, headers []string, perm os.FileMode) error {
  170. if resultMap.Error != nil {
  171. return resultMap.Error
  172. }
  173. dataset, err := NewDatasetWithData(headers, resultMap.Results, true)
  174. if err != nil {
  175. return err
  176. }
  177. tsv, err := dataset.TSV()
  178. if err != nil {
  179. return err
  180. }
  181. return tsv.WriteFile(filename, perm)
  182. }
  183. func (resultMap *ResultMap) SaveAsHTML(filename string, headers []string, perm os.FileMode) error {
  184. if resultMap.Error != nil {
  185. return resultMap.Error
  186. }
  187. dataset, err := NewDatasetWithData(headers, resultMap.Results, true)
  188. if err != nil {
  189. return err
  190. }
  191. html := dataset.HTML()
  192. return html.WriteFile(filename, perm)
  193. }
  194. func (resultMap *ResultMap) SaveAsXML(filename string, headers []string, perm os.FileMode) error {
  195. if resultMap.Error != nil {
  196. return resultMap.Error
  197. }
  198. dataset, err := NewDatasetWithData(headers, resultMap.Results, false)
  199. if err != nil {
  200. return err
  201. }
  202. xml, err := dataset.XML()
  203. if err != nil {
  204. return err
  205. }
  206. return xml.WriteFile(filename, perm)
  207. }
  208. func (resultMap *ResultMap) SaveAsXMLWithTagNamePrefixIndent(tagName string, prifix string, indent string, filename string, headers []string, perm os.FileMode) error {
  209. if resultMap.Error != nil {
  210. return resultMap.Error
  211. }
  212. dataset, err := NewDatasetWithData(headers, resultMap.Results, false)
  213. if err != nil {
  214. return err
  215. }
  216. xml, err := dataset.XMLWithTagNamePrefixIndent(tagName, prifix, indent)
  217. if err != nil {
  218. return err
  219. }
  220. return xml.WriteFile(filename, perm)
  221. }
  222. func (resultMap *ResultMap) SaveAsYAML(filename string, headers []string, perm os.FileMode) error {
  223. if resultMap.Error != nil {
  224. return resultMap.Error
  225. }
  226. dataset, err := NewDatasetWithData(headers, resultMap.Results, false)
  227. if err != nil {
  228. return err
  229. }
  230. yaml, err := dataset.YAML()
  231. if err != nil {
  232. return err
  233. }
  234. return yaml.WriteFile(filename, perm)
  235. }
  236. func (resultMap *ResultMap) SaveAsJSON(filename string, headers []string, perm os.FileMode) error {
  237. if resultMap.Error != nil {
  238. return resultMap.Error
  239. }
  240. dataset, err := NewDatasetWithData(headers, resultMap.Results, false)
  241. if err != nil {
  242. return err
  243. }
  244. json, err := dataset.JSON()
  245. if err != nil {
  246. return err
  247. }
  248. return json.WriteFile(filename, perm)
  249. }
  250. func (resultMap *ResultMap) SaveAsXLSX(filename string, headers []string, perm os.FileMode) error {
  251. if resultMap.Error != nil {
  252. return resultMap.Error
  253. }
  254. dataset, err := NewDatasetWithData(headers, resultMap.Results, true)
  255. if err != nil {
  256. return err
  257. }
  258. xlsx, err := dataset.XLSX()
  259. if err != nil {
  260. return err
  261. }
  262. return xlsx.WriteFile(filename, perm)
  263. }
  264. type ResultStructs struct {
  265. Result interface{}
  266. Error error
  267. }
  268. func (resultStructs *ResultStructs) Json() (string, error) {
  269. if resultStructs.Error != nil {
  270. return "", resultStructs.Error
  271. }
  272. return JSONString(resultStructs.Result, true)
  273. }
  274. func (resultStructs *ResultStructs) Xml() (string, error) {
  275. if resultStructs.Error != nil {
  276. return "", resultStructs.Error
  277. }
  278. result, err := resultStructs.Json()
  279. if err != nil {
  280. return "", err
  281. }
  282. var anydata = []byte(result)
  283. var i interface{}
  284. err = json.Unmarshal(anydata, &i)
  285. if err != nil {
  286. return "", err
  287. }
  288. resultByte, err := anyxml.Xml(i)
  289. if err != nil {
  290. return "", err
  291. }
  292. return string(resultByte), nil
  293. }
  294. func (resultStructs *ResultStructs) XmlIndent(prefix string, indent string, recordTag string) (string, error) {
  295. if resultStructs.Error != nil {
  296. return "", resultStructs.Error
  297. }
  298. result, err := resultStructs.Json()
  299. if err != nil {
  300. return "", err
  301. }
  302. var anydata = []byte(result)
  303. var i interface{}
  304. err = json.Unmarshal(anydata, &i)
  305. if err != nil {
  306. return "", err
  307. }
  308. resultByte, err := anyxml.XmlIndent(i, prefix, indent, recordTag)
  309. if err != nil {
  310. return "", err
  311. }
  312. return string(resultByte), nil
  313. }
  314. func (session *Session) SqlMapClient(sqlTagName string, args ...interface{}) *Session {
  315. return session.Sql(session.Engine.sqlMap.Sql[sqlTagName], args...)
  316. }
  317. func (session *Session) SqlTemplateClient(sqlTagName string, args ...interface{}) *Session {
  318. session.IsSqlFunc = true
  319. if session.Engine.sqlTemplate.Template[sqlTagName] == nil {
  320. if len(args) == 0 {
  321. return session.Sql("")
  322. } else {
  323. map1 := args[0].(*map[string]interface{})
  324. return session.Sql("", map1)
  325. }
  326. }
  327. if len(args) == 0 {
  328. parmap := &pongo2.Context{"1": 1}
  329. sql, err := session.Engine.sqlTemplate.Template[sqlTagName].Execute(*parmap)
  330. if err != nil {
  331. session.Engine.logger.Error(err)
  332. }
  333. return session.Sql(sql)
  334. } else {
  335. map1 := args[0].(*map[string]interface{})
  336. sql, err := session.Engine.sqlTemplate.Template[sqlTagName].Execute(*map1)
  337. if err != nil {
  338. session.Engine.logger.Error(err)
  339. }
  340. return session.Sql(sql, map1)
  341. }
  342. }
  343. func (session *Session) Search(rowsSlicePtr interface{}, condiBean ...interface{}) *ResultStructs {
  344. err := session.Find(rowsSlicePtr, condiBean...)
  345. r := &ResultStructs{Result: rowsSlicePtr, Error: err}
  346. return r
  347. }
  348. func (session *Session) genSelectSql(dialect core.Dialect, rownumber string) string {
  349. var sql = session.Statement.RawSQL
  350. var orderBys = session.Statement.OrderStr
  351. if dialect.DBType() != core.MSSQL && dialect.DBType() != core.ORACLE {
  352. if session.Statement.Start > 0 {
  353. sql = fmt.Sprintf("%v LIMIT %v OFFSET %v", sql, session.Statement.LimitN, session.Statement.Start)
  354. } else if session.Statement.LimitN > 0 {
  355. sql = fmt.Sprintf("%v LIMIT %v", sql, session.Statement.LimitN)
  356. }
  357. } else if dialect.DBType() == core.ORACLE {
  358. if session.Statement.Start != 0 || session.Statement.LimitN != 0 {
  359. sql = fmt.Sprintf("SELECT aat.* FROM (SELECT at.*,ROWNUM %v FROM (%v) at WHERE ROWNUM <= %d) aat WHERE %v > %d",
  360. rownumber, sql, session.Statement.Start+session.Statement.LimitN, rownumber, session.Statement.Start)
  361. }
  362. } else {
  363. keepSelect := false
  364. var fullQuery string
  365. if session.Statement.Start > 0 {
  366. fullQuery = fmt.Sprintf("SELECT sq.* FROM (SELECT ROW_NUMBER() OVER (ORDER BY %v) AS %v,", orderBys, rownumber)
  367. } else if session.Statement.LimitN > 0 {
  368. fullQuery = fmt.Sprintf("SELECT TOP %d", session.Statement.LimitN)
  369. } else {
  370. keepSelect = true
  371. }
  372. if !keepSelect {
  373. expr := `^\s*SELECT\s*`
  374. reg, err := regexp.Compile(expr)
  375. if err != nil {
  376. fmt.Println(err)
  377. }
  378. sql = strings.ToUpper(sql)
  379. if reg.MatchString(sql) {
  380. str := reg.FindAllString(sql, -1)
  381. fullQuery = fmt.Sprintf("%v %v", fullQuery, sql[len(str[0]):])
  382. }
  383. }
  384. if session.Statement.Start > 0 {
  385. // T-SQL offset starts with 1, not like MySQL with 0;
  386. if session.Statement.LimitN > 0 {
  387. fullQuery = fmt.Sprintf("%v) AS sq WHERE %v BETWEEN %d AND %d", fullQuery, rownumber, session.Statement.Start+1, session.Statement.Start+session.Statement.LimitN)
  388. } else {
  389. fullQuery = fmt.Sprintf("%v) AS sq WHERE %v >= %d", fullQuery, rownumber, session.Statement.Start+1)
  390. }
  391. } else {
  392. fullQuery = fmt.Sprintf("%v ORDER BY %v", fullQuery, orderBys)
  393. }
  394. if keepSelect {
  395. if len(orderBys) > 0 {
  396. sql = fmt.Sprintf("%v ORDER BY %v", sql, orderBys)
  397. }
  398. } else {
  399. sql = fullQuery
  400. }
  401. }
  402. return sql
  403. }
  404. // Exec a raw sql and return records as ResultMap
  405. func (session *Session) Query() *ResultMap {
  406. defer session.resetStatement()
  407. if session.IsAutoClose {
  408. defer session.Close()
  409. }
  410. var dialect = session.Statement.Engine.Dialect()
  411. rownumber := "xorm" + NewShortUUID().String()
  412. sql := session.genSelectSql(dialect, rownumber)
  413. params := session.Statement.RawParams
  414. i := len(params)
  415. var result []map[string]interface{}
  416. var err error
  417. if i == 1 {
  418. vv := reflect.ValueOf(params[0])
  419. if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Map {
  420. result, err = session.queryAll(sql, params...)
  421. } else {
  422. result, err = session.queryAllByMap(sql, params[0])
  423. }
  424. } else {
  425. result, err = session.queryAll(sql, params...)
  426. }
  427. if dialect.DBType() == core.MSSQL {
  428. if session.Statement.Start > 0 {
  429. for i, _ := range result {
  430. delete(result[i], rownumber)
  431. }
  432. }
  433. } else if dialect.DBType() == core.ORACLE {
  434. if session.Statement.Start != 0 || session.Statement.LimitN != 0 {
  435. for i, _ := range result {
  436. delete(result[i], rownumber)
  437. }
  438. }
  439. }
  440. r := &ResultMap{Results: result, Error: err}
  441. return r
  442. }
  443. // Exec a raw sql and return records as ResultMap
  444. func (session *Session) QueryWithDateFormat(dateFormat string) *ResultMap {
  445. defer session.resetStatement()
  446. if session.IsAutoClose {
  447. defer session.Close()
  448. }
  449. var dialect = session.Statement.Engine.Dialect()
  450. rownumber := "xorm" + NewShortUUID().String()
  451. sql := session.genSelectSql(dialect, rownumber)
  452. params := session.Statement.RawParams
  453. i := len(params)
  454. var result []map[string]interface{}
  455. var err error
  456. if i == 1 {
  457. vv := reflect.ValueOf(params[0])
  458. if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Map {
  459. result, err = session.queryAllWithDateFormat(dateFormat, sql, params...)
  460. } else {
  461. result, err = session.queryAllByMapWithDateFormat(dateFormat, sql, params[0])
  462. }
  463. } else {
  464. result, err = session.queryAllWithDateFormat(dateFormat, sql, params...)
  465. }
  466. if dialect.DBType() == core.MSSQL {
  467. if session.Statement.Start > 0 {
  468. for i, _ := range result {
  469. delete(result[i], rownumber)
  470. }
  471. }
  472. } else if dialect.DBType() == core.ORACLE {
  473. if session.Statement.Start != 0 || session.Statement.LimitN != 0 {
  474. for i, _ := range result {
  475. delete(result[i], rownumber)
  476. }
  477. }
  478. }
  479. r := &ResultMap{Results: result, Error: err}
  480. return r
  481. }
  482. // Execute raw sql
  483. func (session *Session) Execute() (sql.Result, error) {
  484. defer session.resetStatement()
  485. if session.IsAutoClose {
  486. defer session.Close()
  487. }
  488. sqlStr := session.Statement.RawSQL
  489. params := session.Statement.RawParams
  490. i := len(params)
  491. if i == 1 {
  492. vv := reflect.ValueOf(params[0])
  493. if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Map {
  494. return session.exec(sqlStr, params...)
  495. } else {
  496. sqlStr1, args, _ := core.MapToSlice(sqlStr, params[0])
  497. return session.exec(sqlStr1, args...)
  498. }
  499. } else {
  500. return session.exec(sqlStr, params...)
  501. }
  502. }
  503. // =============================
  504. // for Object
  505. // =============================
  506. func (session *Session) queryAll(sqlStr string, paramStr ...interface{}) (resultsSlice []map[string]interface{}, err error) {
  507. session.queryPreprocess(&sqlStr, paramStr...)
  508. if session.IsAutoCommit {
  509. return query3(session.DB(), sqlStr, paramStr...)
  510. }
  511. return txQuery3(session.Tx, sqlStr, paramStr...)
  512. }
  513. func (session *Session) queryAllByMap(sqlStr string, paramMap interface{}) (resultsSlice []map[string]interface{}, err error) {
  514. sqlStr1, param, _ := core.MapToSlice(sqlStr, paramMap)
  515. session.queryPreprocess(&sqlStr1, param...)
  516. if session.IsAutoCommit {
  517. return query3(session.DB(), sqlStr1, param...)
  518. }
  519. return txQuery3(session.Tx, sqlStr1, param...)
  520. }
  521. func (session *Session) queryAllByMapWithDateFormat(dateFormat string, sqlStr string, paramMap interface{}) (resultsSlice []map[string]interface{}, err error) {
  522. sqlStr1, param, _ := core.MapToSlice(sqlStr, paramMap)
  523. session.queryPreprocess(&sqlStr1, param...)
  524. if session.IsAutoCommit {
  525. return query3WithDateFormat(session.DB(), dateFormat, sqlStr1, param...)
  526. }
  527. return txQuery3WithDateFormat(session.Tx, dateFormat, sqlStr1, param...)
  528. }
  529. func (session *Session) queryAllWithDateFormat(dateFormat string, sqlStr string, paramStr ...interface{}) (resultsSlice []map[string]interface{}, err error) {
  530. session.queryPreprocess(&sqlStr, paramStr...)
  531. if session.IsAutoCommit {
  532. return query3WithDateFormat(session.DB(), dateFormat, sqlStr, paramStr...)
  533. }
  534. return txQuery3WithDateFormat(session.Tx, dateFormat, sqlStr, paramStr...)
  535. }
  536. func (session *Session) queryAllToJsonString(sql string, paramStr ...interface{}) (string, error) {
  537. results, err := session.queryAll(sql, paramStr...)
  538. if err != nil {
  539. return "", err
  540. }
  541. return JSONString(results, true)
  542. }
  543. func (session *Session) queryAllToXmlString(sql string, paramStr ...interface{}) (string, error) {
  544. resultMap, err := session.queryAll(sql, paramStr...)
  545. if err != nil {
  546. return "", err
  547. }
  548. results, err := anyxml.Xml(resultMap)
  549. if err != nil {
  550. return "", err
  551. }
  552. return string(results), nil
  553. }
  554. func (session *Session) queryAllToXmlIndentString(sql string, prefix string, indent string, paramStr ...interface{}) (string, error) {
  555. resultSlice, err := session.queryAll(sql, paramStr...)
  556. if err != nil {
  557. return "", err
  558. }
  559. results, err := anyxml.XmlIndent(resultSlice, prefix, indent, "result")
  560. if err != nil {
  561. return "", err
  562. }
  563. return string(results), nil
  564. }
  565. func (session *Session) queryAllToXmlStringWithDateFormat(dateFormat string, sql string, paramStr ...interface{}) (string, error) {
  566. resultSlice, err := session.queryAll(sql, paramStr...)
  567. if err != nil {
  568. return "", err
  569. }
  570. results, err := anyxml.XmlWithDateFormat(dateFormat, resultSlice)
  571. if err != nil {
  572. return "", err
  573. }
  574. return string(results), nil
  575. }
  576. func (session *Session) queryAllToXmlIndentStringWithDateFormat(dateFormat string, sql string, prefix string, indent string, paramStr ...interface{}) (string, error) {
  577. resultSlice, err := session.queryAll(sql, paramStr...)
  578. if err != nil {
  579. return "", err
  580. }
  581. results, err := anyxml.XmlIndentWithDateFormat(dateFormat, resultSlice, prefix, indent, "results")
  582. if err != nil {
  583. return "", err
  584. }
  585. return string(results), nil
  586. }
  587. func (session *Session) queryAllByMapToJsonString(sql string, paramMap interface{}) (string, error) {
  588. results, err := session.queryAllByMap(sql, paramMap)
  589. if err != nil {
  590. return "", err
  591. }
  592. return JSONString(results, true)
  593. }
  594. func (session *Session) queryAllByMapToJsonStringWithDateFormat(dateFormat string, sql string, paramMap interface{}) (string, error) {
  595. results, err := session.queryAllByMapWithDateFormat(dateFormat, sql, paramMap)
  596. if err != nil {
  597. return "", err
  598. }
  599. return JSONString(results, true)
  600. }
  601. func (session *Session) queryAllToJsonStringWithDateFormat(dateFormat string, sql string, paramStr ...interface{}) (string, error) {
  602. results, err := session.queryAllWithDateFormat(dateFormat, sql, paramStr...)
  603. if err != nil {
  604. return "", err
  605. }
  606. return JSONString(results, true)
  607. }
  608. func (session *Session) queryPreprocessByMap(sqlStr *string, paramMap interface{}) {
  609. re := regexp.MustCompile(`[?](\w+)`)
  610. query := *sqlStr
  611. names := make(map[string]int)
  612. var i int
  613. query = re.ReplaceAllStringFunc(query, func(src string) string {
  614. names[src[1:]] = i
  615. i += 1
  616. return "?"
  617. })
  618. for _, filter := range session.Engine.dialect.Filters() {
  619. query = filter.Do(query, session.Engine.dialect, session.Statement.RefTable)
  620. }
  621. *sqlStr = query
  622. session.Engine.logSQL(*sqlStr, paramMap)
  623. }
  624. func (session *Session) Sqls(sqls interface{}, parmas ...interface{}) *SqlsExecutor {
  625. sqlsExecutor := new(SqlsExecutor)
  626. switch sqls.(type) {
  627. case string:
  628. sqlsExecutor.sqls = sqls.(string)
  629. case []string:
  630. sqlsExecutor.sqls = sqls.([]string)
  631. case map[string]string:
  632. sqlsExecutor.sqls = sqls.(map[string]string)
  633. default:
  634. sqlsExecutor.sqls = nil
  635. sqlsExecutor.err = ErrParamsType
  636. }
  637. if len(parmas) == 0 {
  638. sqlsExecutor.parmas = nil
  639. }
  640. if len(parmas) > 1 {
  641. sqlsExecutor.parmas = nil
  642. sqlsExecutor.err = ErrParamsType
  643. }
  644. if len(parmas) == 1 {
  645. switch parmas[0].(type) {
  646. case map[string]interface{}:
  647. sqlsExecutor.parmas = parmas[0].(map[string]interface{})
  648. case []map[string]interface{}:
  649. sqlsExecutor.parmas = parmas[0].([]map[string]interface{})
  650. case map[string]map[string]interface{}:
  651. sqlsExecutor.parmas = parmas[0].(map[string]map[string]interface{})
  652. default:
  653. sqlsExecutor.parmas = nil
  654. sqlsExecutor.err = ErrParamsType
  655. }
  656. }
  657. sqlsExecutor.session = session
  658. return sqlsExecutor
  659. }
  660. func (session *Session) SqlMapsClient(sqlkeys interface{}, parmas ...interface{}) *SqlMapsExecutor {
  661. sqlMapsExecutor := new(SqlMapsExecutor)
  662. switch sqlkeys.(type) {
  663. case string:
  664. sqlMapsExecutor.sqlkeys = sqlkeys.(string)
  665. case []string:
  666. sqlMapsExecutor.sqlkeys = sqlkeys.([]string)
  667. case map[string]string:
  668. sqlMapsExecutor.sqlkeys = sqlkeys.(map[string]string)
  669. default:
  670. sqlMapsExecutor.sqlkeys = nil
  671. sqlMapsExecutor.err = ErrParamsType
  672. }
  673. if len(parmas) == 0 {
  674. sqlMapsExecutor.parmas = nil
  675. }
  676. if len(parmas) > 1 {
  677. sqlMapsExecutor.parmas = nil
  678. sqlMapsExecutor.err = ErrParamsType
  679. }
  680. if len(parmas) == 1 {
  681. switch parmas[0].(type) {
  682. case map[string]interface{}:
  683. sqlMapsExecutor.parmas = parmas[0].(map[string]interface{})
  684. case []map[string]interface{}:
  685. sqlMapsExecutor.parmas = parmas[0].([]map[string]interface{})
  686. case map[string]map[string]interface{}:
  687. sqlMapsExecutor.parmas = parmas[0].(map[string]map[string]interface{})
  688. default:
  689. sqlMapsExecutor.parmas = nil
  690. sqlMapsExecutor.err = ErrParamsType
  691. }
  692. }
  693. sqlMapsExecutor.session = session
  694. return sqlMapsExecutor
  695. }
  696. func (session *Session) SqlTemplatesClient(sqlkeys interface{}, parmas ...interface{}) *SqlTemplatesExecutor {
  697. sqlTemplatesExecutor := new(SqlTemplatesExecutor)
  698. switch sqlkeys.(type) {
  699. case string:
  700. sqlTemplatesExecutor.sqlkeys = sqlkeys.(string)
  701. case []string:
  702. sqlTemplatesExecutor.sqlkeys = sqlkeys.([]string)
  703. case map[string]string:
  704. sqlTemplatesExecutor.sqlkeys = sqlkeys.(map[string]string)
  705. default:
  706. sqlTemplatesExecutor.sqlkeys = nil
  707. sqlTemplatesExecutor.err = ErrParamsType
  708. }
  709. if len(parmas) == 0 {
  710. sqlTemplatesExecutor.parmas = nil
  711. }
  712. if len(parmas) > 1 {
  713. sqlTemplatesExecutor.parmas = nil
  714. sqlTemplatesExecutor.err = ErrParamsType
  715. }
  716. if len(parmas) == 1 {
  717. switch parmas[0].(type) {
  718. case map[string]interface{}:
  719. sqlTemplatesExecutor.parmas = parmas[0].(map[string]interface{})
  720. case []map[string]interface{}:
  721. sqlTemplatesExecutor.parmas = parmas[0].([]map[string]interface{})
  722. case map[string]map[string]interface{}:
  723. sqlTemplatesExecutor.parmas = parmas[0].(map[string]map[string]interface{})
  724. default:
  725. sqlTemplatesExecutor.parmas = nil
  726. sqlTemplatesExecutor.err = ErrParamsType
  727. }
  728. }
  729. sqlTemplatesExecutor.session = session
  730. return sqlTemplatesExecutor
  731. }