sqlmap.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package xorm
  2. import (
  3. "bufio"
  4. "bytes"
  5. "encoding/json"
  6. "encoding/xml"
  7. "io/ioutil"
  8. "os"
  9. "strings"
  10. )
  11. type SqlMap struct {
  12. SqlMapRootDir string
  13. Sql map[string]string
  14. Extension map[string]string
  15. Capacity uint
  16. Cipher Cipher
  17. }
  18. func (sqlMap *SqlMap) checkNilAndInit() {
  19. if sqlMap.Sql == nil {
  20. if sqlMap.Capacity == 0 {
  21. sqlMap.Sql = make(map[string]string, 100)
  22. } else {
  23. sqlMap.Sql = make(map[string]string, sqlMap.Capacity)
  24. }
  25. }
  26. }
  27. type SqlM interface {
  28. RootDir() string
  29. Extension() string
  30. }
  31. func (sqlMap *SqlMap) walkFunc(path string, info os.FileInfo, err error) error {
  32. if err != nil {
  33. return err
  34. }
  35. if info.IsDir() {
  36. return nil
  37. }
  38. if strings.HasSuffix(path, sqlMap.Extension["xml"]) || strings.HasSuffix(path, sqlMap.Extension["json"]) || strings.HasSuffix(path, sqlMap.Extension["xsql"]) {
  39. err = sqlMap.paresSql(path)
  40. if err != nil {
  41. return err
  42. }
  43. }
  44. return nil
  45. }
  46. func (sqlMap *SqlMap) paresSql(filepath string) error {
  47. content, err := ioutil.ReadFile(filepath)
  48. if err != nil {
  49. return err
  50. }
  51. enc := sqlMap.Cipher
  52. if enc != nil {
  53. content, err = enc.Decrypt(content)
  54. if err != nil {
  55. return err
  56. }
  57. }
  58. sqlMap.checkNilAndInit()
  59. if strings.HasSuffix(filepath, sqlMap.Extension["xml"]) {
  60. var result XmlSql
  61. err = xml.Unmarshal(content, &result)
  62. if err != nil {
  63. return err
  64. }
  65. for _, sql := range result.Sql {
  66. sqlMap.Sql[sql.Id] = sql.Value
  67. }
  68. return nil
  69. }
  70. if strings.HasSuffix(filepath, sqlMap.Extension["json"]) {
  71. var result map[string]string
  72. err = json.Unmarshal(content, &result)
  73. if err != nil {
  74. return err
  75. }
  76. for k := range result {
  77. sqlMap.Sql[k] = result[k]
  78. }
  79. return nil
  80. }
  81. if strings.HasSuffix(filepath, sqlMap.Extension["xsql"]) {
  82. scanner := &Scanner{}
  83. result := scanner.Run(bufio.NewScanner(bytes.NewReader(content)))
  84. for k := range result {
  85. sqlMap.Sql[k] = result[k]
  86. }
  87. return nil
  88. }
  89. return nil
  90. }
  91. func (sqlMap *SqlMap) addSql(key string, sql string) {
  92. sqlMap.checkNilAndInit()
  93. sqlMap.Sql[key] = sql
  94. }
  95. func (sqlMap *SqlMap) updateSql(key string, sql string) {
  96. sqlMap.checkNilAndInit()
  97. sqlMap.Sql[key] = sql
  98. }
  99. func (sqlMap *SqlMap) removeSql(key string) {
  100. sqlMap.checkNilAndInit()
  101. delete(sqlMap.Sql, key)
  102. }
  103. func (sqlMap *SqlMap) batchAddSql(sqlStrMap map[string]string) {
  104. sqlMap.checkNilAndInit()
  105. for k, v := range sqlStrMap {
  106. sqlMap.Sql[k] = v
  107. }
  108. }
  109. func (sqlMap *SqlMap) batchUpdateSql(sqlStrMap map[string]string) {
  110. sqlMap.checkNilAndInit()
  111. for k, v := range sqlStrMap {
  112. sqlMap.Sql[k] = v
  113. }
  114. }
  115. func (sqlMap *SqlMap) batchRemoveSql(key []string) {
  116. sqlMap.checkNilAndInit()
  117. for _, v := range key {
  118. delete(sqlMap.Sql, v)
  119. }
  120. }
  121. func (sqlMap *SqlMap) getSql(key string) string {
  122. return sqlMap.Sql[key]
  123. }
  124. func (sqlMap *SqlMap) getSqlMap(keys ...interface{}) map[string]string {
  125. var resultSqlMap map[string]string
  126. i := len(keys)
  127. if i == 0 {
  128. return sqlMap.Sql
  129. }
  130. if i == 1 {
  131. switch keys[0].(type) {
  132. case string:
  133. resultSqlMap = make(map[string]string, 1)
  134. case []string:
  135. ks := keys[0].([]string)
  136. n := len(ks)
  137. resultSqlMap = make(map[string]string, n)
  138. }
  139. } else {
  140. resultSqlMap = make(map[string]string, i)
  141. }
  142. for k, _ := range keys {
  143. switch keys[k].(type) {
  144. case string:
  145. key := keys[k].(string)
  146. resultSqlMap[key] = sqlMap.Sql[key]
  147. case []string:
  148. ks := keys[k].([]string)
  149. for _, v := range ks {
  150. resultSqlMap[v] = sqlMap.Sql[v]
  151. }
  152. }
  153. }
  154. return resultSqlMap
  155. }