engine_sqlmap.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package xorm
  2. import (
  3. "os"
  4. "path/filepath"
  5. "strings"
  6. )
  7. func (engine *Engine) SetSqlMapCipher(cipher Cipher) {
  8. engine.SqlMap.Cipher = cipher
  9. }
  10. func (engine *Engine) ClearSqlMapCipher() {
  11. engine.SqlMap.Cipher = nil
  12. }
  13. func (engine *Engine) RegisterSqlMap(sqlm SqlM, Cipher ...Cipher) error {
  14. switch sqlm.(type) {
  15. case *XmlSqlMap:
  16. if len(engine.SqlMap.Extension) == 0 {
  17. engine.SqlMap.Extension = map[string]string{"xml": ".xml", "json": ".json", "xsql": ".sql"}
  18. }
  19. engine.SqlMap.Extension["xml"] = sqlm.Extension()
  20. case *JsonSqlMap:
  21. if len(engine.SqlMap.Extension) == 0 {
  22. engine.SqlMap.Extension = map[string]string{"xml": ".xml", "json": ".json", "xsql": ".sql"}
  23. }
  24. engine.SqlMap.Extension["json"] = sqlm.Extension()
  25. case *XSqlMap:
  26. if len(engine.SqlMap.Extension) == 0 {
  27. engine.SqlMap.Extension = map[string]string{"xml": ".xml", "json": ".json", "xsql": ".sql"}
  28. }
  29. engine.SqlMap.Extension["xsql"] = sqlm.Extension()
  30. default:
  31. return ErrParamsType
  32. }
  33. if len(Cipher) > 0 {
  34. engine.SqlMap.Cipher = Cipher[0]
  35. }
  36. engine.SqlMap.SqlMapRootDir = sqlm.RootDir()
  37. err := filepath.Walk(engine.SqlMap.SqlMapRootDir, engine.SqlMap.walkFunc)
  38. if err != nil {
  39. return err
  40. }
  41. return nil
  42. }
  43. func (engine *Engine) LoadSqlMap(filepath string) error {
  44. if len(engine.SqlMap.Extension) == 0 {
  45. engine.SqlMap.Extension = map[string]string{"xml": ".xml", "json": ".json", "xsql": ".sql"}
  46. } else {
  47. if engine.SqlMap.Extension["xml"] == "" || len(engine.SqlMap.Extension["xml"]) == 0 {
  48. engine.SqlMap.Extension["xml"] = ".xml"
  49. }
  50. if engine.SqlMap.Extension["json"] == "" || len(engine.SqlMap.Extension["json"]) == 0 {
  51. engine.SqlMap.Extension["json"] = ".json"
  52. }
  53. if engine.SqlMap.Extension["xsql"] == "" || len(engine.SqlMap.Extension["xsql"]) == 0 {
  54. engine.SqlMap.Extension["xsql"] = ".sql"
  55. }
  56. }
  57. if strings.HasSuffix(filepath, engine.SqlMap.Extension["xml"]) || strings.HasSuffix(filepath, engine.SqlMap.Extension["json"]) || strings.HasSuffix(filepath, engine.SqlMap.Extension["xsql"]) {
  58. err := engine.loadSqlMap(filepath)
  59. if err != nil {
  60. return err
  61. }
  62. }
  63. return nil
  64. }
  65. func (engine *Engine) BatchLoadSqlMap(filepathSlice []string) error {
  66. if len(engine.SqlMap.Extension) == 0 {
  67. engine.SqlMap.Extension = map[string]string{"xml": ".xml", "json": ".json", "xsql": ".sql"}
  68. } else {
  69. if engine.SqlMap.Extension["xml"] == "" || len(engine.SqlMap.Extension["xml"]) == 0 {
  70. engine.SqlMap.Extension["xml"] = ".xml"
  71. }
  72. if engine.SqlMap.Extension["json"] == "" || len(engine.SqlMap.Extension["json"]) == 0 {
  73. engine.SqlMap.Extension["json"] = ".json"
  74. }
  75. if engine.SqlMap.Extension["xsql"] == "" || len(engine.SqlMap.Extension["xsql"]) == 0 {
  76. engine.SqlMap.Extension["xsql"] = ".sql"
  77. }
  78. }
  79. for _, filepath := range filepathSlice {
  80. if strings.HasSuffix(filepath, engine.SqlMap.Extension["xml"]) || strings.HasSuffix(filepath, engine.SqlMap.Extension["json"]) || strings.HasSuffix(filepath, engine.SqlMap.Extension["xsql"]) {
  81. err := engine.loadSqlMap(filepath)
  82. if err != nil {
  83. return err
  84. }
  85. }
  86. }
  87. return nil
  88. }
  89. func (engine *Engine) ReloadSqlMap(filepath string) error {
  90. if len(engine.SqlMap.Extension) == 0 {
  91. engine.SqlMap.Extension = map[string]string{"xml": ".xml", "json": ".json", "xsql": ".sql"}
  92. } else {
  93. if engine.SqlMap.Extension["xml"] == "" || len(engine.SqlMap.Extension["xml"]) == 0 {
  94. engine.SqlMap.Extension["xml"] = ".xml"
  95. }
  96. if engine.SqlMap.Extension["json"] == "" || len(engine.SqlMap.Extension["json"]) == 0 {
  97. engine.SqlMap.Extension["json"] = ".json"
  98. }
  99. if engine.SqlMap.Extension["xsql"] == "" || len(engine.SqlMap.Extension["xsql"]) == 0 {
  100. engine.SqlMap.Extension["xsql"] = ".sql"
  101. }
  102. }
  103. if strings.HasSuffix(filepath, engine.SqlMap.Extension["xml"]) || strings.HasSuffix(filepath, engine.SqlMap.Extension["json"]) || strings.HasSuffix(filepath, engine.SqlMap.Extension["xsql"]) {
  104. err := engine.reloadSqlMap(filepath)
  105. if err != nil {
  106. return err
  107. }
  108. }
  109. return nil
  110. }
  111. func (engine *Engine) BatchReloadSqlMap(filepathSlice []string) error {
  112. if len(engine.SqlMap.Extension) == 0 {
  113. engine.SqlMap.Extension = map[string]string{"xml": ".xml", "json": ".json", "xsql": ".sql"}
  114. } else {
  115. if engine.SqlMap.Extension["xml"] == "" || len(engine.SqlMap.Extension["xml"]) == 0 {
  116. engine.SqlMap.Extension["xml"] = ".xml"
  117. }
  118. if engine.SqlMap.Extension["json"] == "" || len(engine.SqlMap.Extension["json"]) == 0 {
  119. engine.SqlMap.Extension["json"] = ".json"
  120. }
  121. if engine.SqlMap.Extension["xsql"] == "" || len(engine.SqlMap.Extension["xsql"]) == 0 {
  122. engine.SqlMap.Extension["xsql"] = ".sql"
  123. }
  124. }
  125. for _, filepath := range filepathSlice {
  126. if strings.HasSuffix(filepath, engine.SqlMap.Extension["xml"]) || strings.HasSuffix(filepath, engine.SqlMap.Extension["json"]) || strings.HasSuffix(filepath, engine.SqlMap.Extension["xsql"]) {
  127. err := engine.loadSqlMap(filepath)
  128. if err != nil {
  129. return err
  130. }
  131. }
  132. }
  133. return nil
  134. }
  135. func (engine *Engine) loadSqlMap(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 = engine.SqlMap.paresSql(filepath)
  144. if err != nil {
  145. return err
  146. }
  147. return nil
  148. }
  149. func (engine *Engine) reloadSqlMap(filepath string) error {
  150. info, err := os.Lstat(filepath)
  151. if err != nil {
  152. return err
  153. }
  154. if info.IsDir() {
  155. return nil
  156. }
  157. err = engine.SqlMap.paresSql(filepath)
  158. if err != nil {
  159. return err
  160. }
  161. return nil
  162. }
  163. func (engine *Engine) AddSql(key string, sql string) {
  164. engine.SqlMap.addSql(key, sql)
  165. }
  166. func (engine *Engine) UpdateSql(key string, sql string) {
  167. engine.SqlMap.updateSql(key, sql)
  168. }
  169. func (engine *Engine) RemoveSql(key string) {
  170. engine.SqlMap.removeSql(key)
  171. }
  172. func (engine *Engine) BatchAddSql(sqlStrMap map[string]string) {
  173. engine.SqlMap.batchAddSql(sqlStrMap)
  174. }
  175. func (engine *Engine) BatchUpdateSql(sqlStrMap map[string]string) {
  176. engine.SqlMap.batchUpdateSql(sqlStrMap)
  177. }
  178. func (engine *Engine) BatchRemoveSql(key []string) {
  179. engine.SqlMap.batchRemoveSql(key)
  180. }
  181. func (engine *Engine) GetSql(key string) string {
  182. return engine.SqlMap.getSql(key)
  183. }
  184. func (engine *Engine) GetSqlMap(keys ...interface{}) map[string]string {
  185. return engine.SqlMap.getSqlMap(keys...)
  186. }