session_plus.go 21 KB

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