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.

parser.go 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package mssql
  2. import (
  3. "bytes"
  4. "io"
  5. "strconv"
  6. )
  7. type parser struct {
  8. r *bytes.Reader
  9. w bytes.Buffer
  10. paramCount int
  11. paramMax int
  12. }
  13. func (p *parser) next() (rune, bool) {
  14. ch, _, err := p.r.ReadRune()
  15. if err != nil {
  16. if err != io.EOF {
  17. panic(err)
  18. }
  19. return 0, false
  20. }
  21. return ch, true
  22. }
  23. func (p *parser) unread() {
  24. err := p.r.UnreadRune()
  25. if err != nil {
  26. panic(err)
  27. }
  28. }
  29. func (p *parser) write(ch rune) {
  30. p.w.WriteRune(ch)
  31. }
  32. type stateFunc func(*parser) stateFunc
  33. func parseParams(query string) (string, int) {
  34. p := &parser{
  35. r: bytes.NewReader([]byte(query)),
  36. }
  37. state := parseNormal
  38. for state != nil {
  39. state = state(p)
  40. }
  41. return p.w.String(), p.paramMax
  42. }
  43. func parseNormal(p *parser) stateFunc {
  44. for {
  45. ch, ok := p.next()
  46. if !ok {
  47. return nil
  48. }
  49. if ch == '?' {
  50. return parseParameter
  51. } else if ch == '$' || ch == ':' {
  52. ch2, ok := p.next()
  53. if !ok {
  54. p.write(ch)
  55. return nil
  56. }
  57. p.unread()
  58. if ch2 >= '0' && ch2 <= '9' {
  59. return parseParameter
  60. }
  61. }
  62. p.write(ch)
  63. switch ch {
  64. case '\'':
  65. return parseQuote
  66. case '"':
  67. return parseDoubleQuote
  68. case '[':
  69. return parseBracket
  70. case '-':
  71. return parseLineComment
  72. case '/':
  73. return parseComment
  74. }
  75. }
  76. }
  77. func parseParameter(p *parser) stateFunc {
  78. var paramN int
  79. var ok bool
  80. for {
  81. var ch rune
  82. ch, ok = p.next()
  83. if ok && ch >= '0' && ch <= '9' {
  84. paramN = paramN*10 + int(ch-'0')
  85. } else {
  86. break
  87. }
  88. }
  89. if ok {
  90. p.unread()
  91. }
  92. if paramN == 0 {
  93. p.paramCount++
  94. paramN = p.paramCount
  95. }
  96. if paramN > p.paramMax {
  97. p.paramMax = paramN
  98. }
  99. p.w.WriteString("@p")
  100. p.w.WriteString(strconv.Itoa(paramN))
  101. if !ok {
  102. return nil
  103. }
  104. return parseNormal
  105. }
  106. func parseQuote(p *parser) stateFunc {
  107. for {
  108. ch, ok := p.next()
  109. if !ok {
  110. return nil
  111. }
  112. p.write(ch)
  113. if ch == '\'' {
  114. return parseNormal
  115. }
  116. }
  117. }
  118. func parseDoubleQuote(p *parser) stateFunc {
  119. for {
  120. ch, ok := p.next()
  121. if !ok {
  122. return nil
  123. }
  124. p.write(ch)
  125. if ch == '"' {
  126. return parseNormal
  127. }
  128. }
  129. }
  130. func parseBracket(p *parser) stateFunc {
  131. for {
  132. ch, ok := p.next()
  133. if !ok {
  134. return nil
  135. }
  136. p.write(ch)
  137. if ch == ']' {
  138. ch, ok = p.next()
  139. if !ok {
  140. return nil
  141. }
  142. if ch != ']' {
  143. p.unread()
  144. return parseNormal
  145. }
  146. p.write(ch)
  147. }
  148. }
  149. }
  150. func parseLineComment(p *parser) stateFunc {
  151. ch, ok := p.next()
  152. if !ok {
  153. return nil
  154. }
  155. if ch != '-' {
  156. p.unread()
  157. return parseNormal
  158. }
  159. p.write(ch)
  160. for {
  161. ch, ok = p.next()
  162. if !ok {
  163. return nil
  164. }
  165. p.write(ch)
  166. if ch == '\n' {
  167. return parseNormal
  168. }
  169. }
  170. }
  171. func parseComment(p *parser) stateFunc {
  172. var nested int
  173. ch, ok := p.next()
  174. if !ok {
  175. return nil
  176. }
  177. if ch != '*' {
  178. p.unread()
  179. return parseNormal
  180. }
  181. p.write(ch)
  182. for {
  183. ch, ok = p.next()
  184. if !ok {
  185. return nil
  186. }
  187. p.write(ch)
  188. for ch == '*' {
  189. ch, ok = p.next()
  190. if !ok {
  191. return nil
  192. }
  193. p.write(ch)
  194. if ch == '/' {
  195. if nested == 0 {
  196. return parseNormal
  197. } else {
  198. nested--
  199. }
  200. }
  201. }
  202. for ch == '/' {
  203. ch, ok = p.next()
  204. if !ok {
  205. return nil
  206. }
  207. p.write(ch)
  208. if ch == '*' {
  209. nested++
  210. }
  211. }
  212. }
  213. }