session_plus.go 22 KB

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