sqltemplate_pongo2.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. package xorm
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "strings"
  6. "gopkg.in/flosch/pongo2.v3"
  7. )
  8. type Pongo2Template struct {
  9. SqlTemplateRootDir string
  10. Template map[string]*pongo2.Template
  11. extension string
  12. Capacity uint
  13. Cipher Cipher
  14. Type int
  15. Funcs map[string]FuncMap
  16. }
  17. func (sqlTemplate *Pongo2Template) RootDir() string {
  18. return sqlTemplate.SqlTemplateRootDir
  19. }
  20. func (sqlTemplate *Pongo2Template) SetFuncs(key string, funcMap FuncMap) {
  21. sqlTemplate.Funcs[key] = funcMap
  22. }
  23. func (sqlTemplate *Pongo2Template) Extension() string {
  24. return sqlTemplate.extension
  25. }
  26. func (sqlTemplate *Pongo2Template) SetSqlTemplateCipher(cipher Cipher) {
  27. sqlTemplate.Cipher = cipher
  28. }
  29. func (sqlTemplate *Pongo2Template) WalkFunc(path string, info os.FileInfo, err error) error {
  30. if err != nil {
  31. return err
  32. }
  33. if info.IsDir() {
  34. return nil
  35. }
  36. if strings.HasSuffix(path, sqlTemplate.extension) {
  37. err = sqlTemplate.paresSqlTemplate(info.Name(), path)
  38. if err != nil {
  39. return err
  40. }
  41. }
  42. return nil
  43. }
  44. func (sqlTemplate *Pongo2Template) paresSqlTemplate(filename string, filepath string) error {
  45. var sqlt *pongo2.Template
  46. var err error
  47. var content []byte
  48. if sqlTemplate.Cipher == nil {
  49. sqlt, err = pongo2.FromFile(filepath)
  50. if err != nil {
  51. return err
  52. }
  53. } else {
  54. content, err = sqlTemplate.ReadTemplate(filepath)
  55. if err != nil {
  56. return err
  57. }
  58. sqlt, err = pongo2.FromString(string(content))
  59. if err != nil {
  60. return err
  61. }
  62. }
  63. sqlTemplate.checkNilAndInit()
  64. sqlTemplate.Template[filename] = sqlt
  65. return nil
  66. }
  67. func (sqlTemplate *Pongo2Template) ReadTemplate(filepath string) ([]byte, error) {
  68. content, err := ioutil.ReadFile(filepath)
  69. if err != nil {
  70. return nil, err
  71. }
  72. content, err = sqlTemplate.Cipher.Decrypt(content)
  73. if err != nil {
  74. return nil, err
  75. }
  76. return content, nil
  77. }
  78. //-------------------------------------------------------------------------------------------------------------
  79. func (sqlTemplate *Pongo2Template) LoadSqlTemplate(filepath string) error {
  80. if strings.HasSuffix(filepath, sqlTemplate.extension) {
  81. err := sqlTemplate.loadSqlTemplate(filepath)
  82. if err != nil {
  83. return err
  84. }
  85. }
  86. return nil
  87. }
  88. func (sqlTemplate *Pongo2Template) BatchLoadSqlTemplate(filepathSlice []string) error {
  89. for _, filepath := range filepathSlice {
  90. if strings.HasSuffix(filepath, sqlTemplate.extension) {
  91. err := sqlTemplate.loadSqlTemplate(filepath)
  92. if err != nil {
  93. return err
  94. }
  95. }
  96. }
  97. return nil
  98. }
  99. func (sqlTemplate *Pongo2Template) ReloadSqlTemplate(filepath string) error {
  100. if strings.HasSuffix(filepath, sqlTemplate.extension) {
  101. err := sqlTemplate.reloadSqlTemplate(filepath)
  102. if err != nil {
  103. return err
  104. }
  105. }
  106. return nil
  107. }
  108. func (sqlTemplate *Pongo2Template) BatchReloadSqlTemplate(filepathSlice []string) error {
  109. for _, filepath := range filepathSlice {
  110. if strings.HasSuffix(filepath, sqlTemplate.extension) {
  111. err := sqlTemplate.loadSqlTemplate(filepath)
  112. if err != nil {
  113. return err
  114. }
  115. }
  116. }
  117. return nil
  118. }
  119. func (sqlTemplate *Pongo2Template) loadSqlTemplate(filepath string) error {
  120. info, err := os.Lstat(filepath)
  121. if err != nil {
  122. return err
  123. }
  124. if info.IsDir() {
  125. return nil
  126. }
  127. err = sqlTemplate.paresSqlTemplate(info.Name(), filepath)
  128. if err != nil {
  129. return err
  130. }
  131. return nil
  132. }
  133. func (sqlTemplate *Pongo2Template) reloadSqlTemplate(filepath string) error {
  134. info, err := os.Lstat(filepath)
  135. if err != nil {
  136. return err
  137. }
  138. if info.IsDir() {
  139. return nil
  140. }
  141. err = sqlTemplate.paresSqlTemplate(info.Name(), filepath)
  142. if err != nil {
  143. return err
  144. }
  145. return nil
  146. }
  147. func (sqlTemplate *Pongo2Template) checkNilAndInit() {
  148. if sqlTemplate.Template == nil {
  149. sqlTemplate.Template = make(map[string]*pongo2.Template, 100)
  150. }
  151. }
  152. func (sqlTemplate *Pongo2Template) AddSqlTemplate(key string, sqlTemplateStr string) error {
  153. template, err := pongo2.FromString(sqlTemplateStr)
  154. if err != nil {
  155. return err
  156. }
  157. sqlTemplate.checkNilAndInit()
  158. sqlTemplate.Template[key] = template
  159. return nil
  160. }
  161. func (sqlTemplate *Pongo2Template) UpdateSqlTemplate(key string, sqlTemplateStr string) error {
  162. template, err := pongo2.FromString(sqlTemplateStr)
  163. if err != nil {
  164. return err
  165. }
  166. sqlTemplate.checkNilAndInit()
  167. sqlTemplate.Template[key] = template
  168. return nil
  169. }
  170. func (sqlTemplate *Pongo2Template) RemoveSqlTemplate(key string) {
  171. sqlTemplate.checkNilAndInit()
  172. delete(sqlTemplate.Template, key)
  173. }
  174. func (sqlTemplate *Pongo2Template) BatchAddSqlTemplate(key string, sqlTemplateStrMap map[string]string) error {
  175. sqlTemplate.checkNilAndInit()
  176. for k, v := range sqlTemplateStrMap {
  177. template, err := pongo2.FromString(v)
  178. if err != nil {
  179. return err
  180. }
  181. sqlTemplate.Template[k] = template
  182. }
  183. return nil
  184. }
  185. func (sqlTemplate *Pongo2Template) BatchUpdateSqlTemplate(key string, sqlTemplateStrMap map[string]string) error {
  186. sqlTemplate.checkNilAndInit()
  187. for k, v := range sqlTemplateStrMap {
  188. template, err := pongo2.FromString(v)
  189. if err != nil {
  190. return err
  191. }
  192. sqlTemplate.Template[k] = template
  193. }
  194. return nil
  195. }
  196. func (sqlTemplate *Pongo2Template) BatchRemoveSqlTemplate(key []string) {
  197. sqlTemplate.checkNilAndInit()
  198. for _, v := range key {
  199. delete(sqlTemplate.Template, v)
  200. }
  201. }
  202. func (sqlTemplate *Pongo2Template) GetSqlTemplate(key string) *pongo2.Template {
  203. return sqlTemplate.Template[key]
  204. }
  205. func (sqlTemplate *Pongo2Template) GetSqlTemplates(keys ...interface{}) map[string]*pongo2.Template {
  206. var resultSqlTemplates map[string]*pongo2.Template
  207. i := len(keys)
  208. if i == 0 {
  209. return sqlTemplate.Template
  210. }
  211. if i == 1 {
  212. switch keys[0].(type) {
  213. case string:
  214. resultSqlTemplates = make(map[string]*pongo2.Template, 1)
  215. case []string:
  216. ks := keys[0].([]string)
  217. n := len(ks)
  218. resultSqlTemplates = make(map[string]*pongo2.Template, n)
  219. }
  220. } else {
  221. resultSqlTemplates = make(map[string]*pongo2.Template, i)
  222. }
  223. for k, _ := range keys {
  224. switch keys[k].(type) {
  225. case string:
  226. key := keys[k].(string)
  227. resultSqlTemplates[key] = sqlTemplate.Template[key]
  228. case []string:
  229. ks := keys[k].([]string)
  230. for _, v := range ks {
  231. resultSqlTemplates[v] = sqlTemplate.Template[v]
  232. }
  233. }
  234. }
  235. return resultSqlTemplates
  236. }
  237. func (sqlTemplate *Pongo2Template) Execute(key string, args ...interface{}) (string, error) {
  238. if sqlTemplate.Template[key] == nil {
  239. return "", nil
  240. }
  241. if len(args) == 0 {
  242. parmap := &pongo2.Context{"1": 1}
  243. sql, err := sqlTemplate.Template[key].Execute(*parmap)
  244. return sql, err
  245. } else {
  246. map1 := args[0].(*map[string]interface{})
  247. sql, err := sqlTemplate.Template[key].Execute(*map1)
  248. return sql, err
  249. }
  250. }