You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

quote.go 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // Copyright 2020 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package schemas
  5. import (
  6. "strings"
  7. )
  8. // Quoter represents a quoter to the SQL table name and column name
  9. type Quoter struct {
  10. Prefix byte
  11. Suffix byte
  12. IsReserved func(string) bool
  13. }
  14. var (
  15. // AlwaysFalseReverse always think it's not a reverse word
  16. AlwaysNoReserve = func(string) bool { return false }
  17. // AlwaysReverse always reverse the word
  18. AlwaysReserve = func(string) bool { return true }
  19. // CommanQuoteMark represnets the common quote mark
  20. CommanQuoteMark byte = '`'
  21. // CommonQuoter represetns a common quoter
  22. CommonQuoter = Quoter{CommanQuoteMark, CommanQuoteMark, AlwaysReserve}
  23. )
  24. func (q Quoter) IsEmpty() bool {
  25. return q.Prefix == 0 && q.Suffix == 0
  26. }
  27. func (q Quoter) Quote(s string) string {
  28. var buf strings.Builder
  29. q.QuoteTo(&buf, s)
  30. return buf.String()
  31. }
  32. // Trim removes quotes from s
  33. func (q Quoter) Trim(s string) string {
  34. if len(s) < 2 {
  35. return s
  36. }
  37. var buf strings.Builder
  38. for i := 0; i < len(s); i++ {
  39. switch {
  40. case i == 0 && s[i] == q.Prefix:
  41. case i == len(s)-1 && s[i] == q.Suffix:
  42. case s[i] == q.Suffix && s[i+1] == '.':
  43. case s[i] == q.Prefix && s[i-1] == '.':
  44. default:
  45. buf.WriteByte(s[i])
  46. }
  47. }
  48. return buf.String()
  49. }
  50. func (q Quoter) Join(a []string, sep string) string {
  51. var b strings.Builder
  52. q.JoinWrite(&b, a, sep)
  53. return b.String()
  54. }
  55. func (q Quoter) JoinWrite(b *strings.Builder, a []string, sep string) error {
  56. if len(a) == 0 {
  57. return nil
  58. }
  59. n := len(sep) * (len(a) - 1)
  60. for i := 0; i < len(a); i++ {
  61. n += len(a[i])
  62. }
  63. b.Grow(n)
  64. for i, s := range a {
  65. if i > 0 {
  66. if _, err := b.WriteString(sep); err != nil {
  67. return err
  68. }
  69. }
  70. q.QuoteTo(b, strings.TrimSpace(s))
  71. }
  72. return nil
  73. }
  74. func findWord(v string, start int) int {
  75. for j := start; j < len(v); j++ {
  76. switch v[j] {
  77. case '.', ' ':
  78. return j
  79. }
  80. }
  81. return len(v)
  82. }
  83. func findStart(value string, start int) int {
  84. if value[start] == '.' {
  85. return start + 1
  86. }
  87. if value[start] != ' ' {
  88. return start
  89. }
  90. var k = -1
  91. for j := start; j < len(value); j++ {
  92. if value[j] != ' ' {
  93. k = j
  94. break
  95. }
  96. }
  97. if k == -1 {
  98. return len(value)
  99. }
  100. if (value[k] == 'A' || value[k] == 'a') && (value[k+1] == 'S' || value[k+1] == 's') {
  101. k = k + 2
  102. }
  103. for j := k; j < len(value); j++ {
  104. if value[j] != ' ' {
  105. return j
  106. }
  107. }
  108. return len(value)
  109. }
  110. func (q Quoter) quoteWordTo(buf *strings.Builder, word string) error {
  111. var realWord = word
  112. if (word[0] == CommanQuoteMark && word[len(word)-1] == CommanQuoteMark) ||
  113. (word[0] == q.Prefix && word[len(word)-1] == q.Suffix) {
  114. realWord = word[1 : len(word)-1]
  115. }
  116. if q.IsEmpty() {
  117. _, err := buf.WriteString(realWord)
  118. return err
  119. }
  120. isReserved := q.IsReserved(realWord)
  121. if isReserved && realWord != "*" {
  122. if err := buf.WriteByte(q.Prefix); err != nil {
  123. return err
  124. }
  125. }
  126. if _, err := buf.WriteString(realWord); err != nil {
  127. return err
  128. }
  129. if isReserved && realWord != "*" {
  130. return buf.WriteByte(q.Suffix)
  131. }
  132. return nil
  133. }
  134. // QuoteTo quotes the table or column names. i.e. if the quotes are [ and ]
  135. // name -> [name]
  136. // `name` -> [name]
  137. // [name] -> [name]
  138. // schema.name -> [schema].[name]
  139. // `schema`.`name` -> [schema].[name]
  140. // `schema`.name -> [schema].[name]
  141. // schema.`name` -> [schema].[name]
  142. // [schema].name -> [schema].[name]
  143. // schema.[name] -> [schema].[name]
  144. // name AS a -> [name] AS a
  145. // schema.name AS a -> [schema].[name] AS a
  146. func (q Quoter) QuoteTo(buf *strings.Builder, value string) error {
  147. var i int
  148. for i < len(value) {
  149. start := findStart(value, i)
  150. if start > i {
  151. if _, err := buf.WriteString(value[i:start]); err != nil {
  152. return err
  153. }
  154. }
  155. if start == len(value) {
  156. return nil
  157. }
  158. var nextEnd = findWord(value, start)
  159. if err := q.quoteWordTo(buf, value[start:nextEnd]); err != nil {
  160. return err
  161. }
  162. i = nextEnd
  163. }
  164. return nil
  165. }
  166. // Strings quotes a slice of string
  167. func (q Quoter) Strings(s []string) []string {
  168. var res = make([]string, 0, len(s))
  169. for _, a := range s {
  170. res = append(res, q.Quote(a))
  171. }
  172. return res
  173. }
  174. // Replace replaces common quote(`) as the quotes on the sql
  175. func (q Quoter) Replace(sql string) string {
  176. if q.IsEmpty() {
  177. return sql
  178. }
  179. var buf strings.Builder
  180. buf.Grow(len(sql))
  181. var beginSingleQuote bool
  182. for i := 0; i < len(sql); i++ {
  183. if !beginSingleQuote && sql[i] == CommanQuoteMark {
  184. var j = i + 1
  185. for ; j < len(sql); j++ {
  186. if sql[j] == CommanQuoteMark {
  187. break
  188. }
  189. }
  190. word := sql[i+1 : j]
  191. isReserved := q.IsReserved(word)
  192. if isReserved {
  193. buf.WriteByte(q.Prefix)
  194. }
  195. buf.WriteString(word)
  196. if isReserved {
  197. buf.WriteByte(q.Suffix)
  198. }
  199. i = j
  200. } else {
  201. if sql[i] == '\'' {
  202. beginSingleQuote = !beginSingleQuote
  203. }
  204. buf.WriteByte(sql[i])
  205. }
  206. }
  207. return buf.String()
  208. }