engineplus.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package xorm
  2. import (
  3. "encoding/json"
  4. )
  5. func (engine *Engine) SetSqlMapRootDir(sqlMapRootDir string) *Engine {
  6. engine.SqlMap.SqlMapRootDir = sqlMapRootDir
  7. return engine
  8. }
  9. func (engine *Engine) SqlMapClient(sqlTagName string, args ...interface{}) *Session {
  10. session := engine.NewSession()
  11. session.isAutoClose = true
  12. session.isSqlFunc = true
  13. return session.Sql(engine.SqlMap.Sql[sqlTagName], args...)
  14. }
  15. func (engine *Engine) SqlTemplateClient(sqlTagName string, args ...interface{}) *Session {
  16. session := engine.NewSession()
  17. session.isAutoClose = true
  18. session.isSqlFunc = true
  19. return session.SqlTemplateClient(sqlTagName, args...)
  20. }
  21. func (engine *Engine) Search(beans interface{}, condiBeans ...interface{}) *ResultStructs {
  22. session := engine.NewSession()
  23. defer session.Close()
  24. return session.Search(beans, condiBeans...)
  25. }
  26. // Get retrieve one record from table, bean's non-empty fields
  27. // are conditions
  28. func (engine *Engine) GetFirst(bean interface{}) *ResultBean {
  29. session := engine.NewSession()
  30. defer session.Close()
  31. return session.GetFirst(bean)
  32. }
  33. func JSONString(v interface{}, IndentJSON bool) (string, error) {
  34. var result []byte
  35. var err error
  36. if IndentJSON {
  37. result, err = json.MarshalIndent(v, "", " ")
  38. } else {
  39. result, err = json.Marshal(v)
  40. }
  41. if err != nil {
  42. return "", err
  43. }
  44. if string(result) == "null" {
  45. return "", nil
  46. }
  47. return string(result), nil
  48. }
  49. func (engine *Engine) Sqls(sqls interface{}, parmas ...interface{}) *SqlsExecutor {
  50. session := engine.NewSession()
  51. session.isAutoClose = false
  52. session.isSqlFunc = true
  53. return session.Sqls(sqls, parmas...)
  54. }
  55. func (engine *Engine) SqlMapsClient(sqlkeys interface{}, parmas ...interface{}) *SqlMapsExecutor {
  56. session := engine.NewSession()
  57. session.isAutoClose = false
  58. session.isSqlFunc = true
  59. return session.SqlMapsClient(sqlkeys, parmas...)
  60. }
  61. func (engine *Engine) SqlTemplatesClient(sqlkeys interface{}, parmas ...interface{}) *SqlTemplatesExecutor {
  62. session := engine.NewSession()
  63. session.isAutoClose = false
  64. session.isSqlFunc = true
  65. return session.SqlTemplatesClient(sqlkeys, parmas...)
  66. }