sqltemplate_default.go 7.1 KB

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