sqltemplate_jet.go 6.8 KB

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