http urls monitor.

template.go 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // Copyright (c) 2012-present The upper.io/db authors. All rights reserved.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining
  4. // a copy of this software and associated documentation files (the
  5. // "Software"), to deal in the Software without restriction, including
  6. // without limitation the rights to use, copy, modify, merge, publish,
  7. // distribute, sublicense, and/or sell copies of the Software, and to
  8. // permit persons to whom the Software is furnished to do so, subject to
  9. // the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be
  12. // included in all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  18. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. package mysql
  22. import (
  23. "upper.io/db.v3/internal/cache"
  24. "upper.io/db.v3/internal/sqladapter/exql"
  25. )
  26. const (
  27. adapterColumnSeparator = `.`
  28. adapterIdentifierSeparator = `, `
  29. adapterIdentifierQuote = "`{{.Value}}`"
  30. adapterValueSeparator = `, `
  31. adapterValueQuote = `'{{.}}'`
  32. adapterAndKeyword = `AND`
  33. adapterOrKeyword = `OR`
  34. adapterNotKeyword = `NOT`
  35. adapterDescKeyword = `DESC`
  36. adapterAscKeyword = `ASC`
  37. adapterDefaultOperator = `=`
  38. adapterAssignmentOperator = `=`
  39. adapterClauseGroup = `({{.}})`
  40. adapterClauseOperator = ` {{.}} `
  41. adapterColumnValue = `{{.Column}} {{.Operator}} {{.Value}}`
  42. adapterTableAliasLayout = `{{.Name}}{{if .Alias}} AS {{.Alias}}{{end}}`
  43. adapterColumnAliasLayout = `{{.Name}}{{if .Alias}} AS {{.Alias}}{{end}}`
  44. adapterSortByColumnLayout = `{{.Column}} {{.Order}}`
  45. adapterOrderByLayout = `
  46. {{if .SortColumns}}
  47. ORDER BY {{.SortColumns}}
  48. {{end}}
  49. `
  50. adapterWhereLayout = `
  51. {{if .Conds}}
  52. WHERE {{.Conds}}
  53. {{end}}
  54. `
  55. adapterUsingLayout = `
  56. {{if .Columns}}
  57. USING ({{.Columns}})
  58. {{end}}
  59. `
  60. adapterJoinLayout = `
  61. {{if .Table}}
  62. {{ if .On }}
  63. {{.Type}} JOIN {{.Table}}
  64. {{.On}}
  65. {{ else if .Using }}
  66. {{.Type}} JOIN {{.Table}}
  67. {{.Using}}
  68. {{ else if .Type | eq "CROSS" }}
  69. {{.Type}} JOIN {{.Table}}
  70. {{else}}
  71. NATURAL {{.Type}} JOIN {{.Table}}
  72. {{end}}
  73. {{end}}
  74. `
  75. adapterOnLayout = `
  76. {{if .Conds}}
  77. ON {{.Conds}}
  78. {{end}}
  79. `
  80. adapterSelectLayout = `
  81. SELECT
  82. {{if .Distinct}}
  83. DISTINCT
  84. {{end}}
  85. {{if .Columns}}
  86. {{.Columns}}
  87. {{else}}
  88. *
  89. {{end}}
  90. {{if .Table}}
  91. FROM {{.Table}}
  92. {{end}}
  93. {{.Joins}}
  94. {{.Where}}
  95. {{.GroupBy}}
  96. {{.OrderBy}}
  97. {{if .Limit}}
  98. LIMIT {{.Limit}}
  99. {{end}}
  100. ` +
  101. // The argument for LIMIT when only OFFSET is specified is a pretty odd magic
  102. // number; this comes directly from MySQL's manual, see:
  103. // https://dev.mysql.com/doc/refman/5.7/en/select.html
  104. //
  105. // "To retrieve all rows from a certain offset up to the end of the result
  106. // set, you can use some large number for the second parameter. This
  107. // statement retrieves all rows from the 96th row to the last:
  108. // SELECT * FROM tbl LIMIT 95,18446744073709551615; "
  109. //
  110. // ¯\_(ツ)_/¯
  111. `
  112. {{if .Offset}}
  113. {{if not .Limit}}
  114. LIMIT 18446744073709551615
  115. {{end}}
  116. OFFSET {{.Offset}}
  117. {{end}}
  118. `
  119. adapterDeleteLayout = `
  120. DELETE
  121. FROM {{.Table}}
  122. {{.Where}}
  123. `
  124. adapterUpdateLayout = `
  125. UPDATE
  126. {{.Table}}
  127. SET {{.ColumnValues}}
  128. {{ .Where }}
  129. `
  130. adapterSelectCountLayout = `
  131. SELECT
  132. COUNT(1) AS _t
  133. FROM {{.Table}}
  134. {{.Where}}
  135. `
  136. adapterInsertLayout = `
  137. INSERT INTO {{.Table}}
  138. {{if .Columns }}({{.Columns}}){{end}}
  139. VALUES
  140. {{if .Values}}
  141. {{.Values}}
  142. {{else}}
  143. ()
  144. {{end}}
  145. {{if .Returning}}
  146. RETURNING {{.Returning}}
  147. {{end}}
  148. `
  149. adapterTruncateLayout = `
  150. TRUNCATE TABLE {{.Table}}
  151. `
  152. adapterDropDatabaseLayout = `
  153. DROP DATABASE {{.Database}}
  154. `
  155. adapterDropTableLayout = `
  156. DROP TABLE {{.Table}}
  157. `
  158. adapterGroupByLayout = `
  159. {{if .GroupColumns}}
  160. GROUP BY {{.GroupColumns}}
  161. {{end}}
  162. `
  163. )
  164. var template = &exql.Template{
  165. ColumnSeparator: adapterColumnSeparator,
  166. IdentifierSeparator: adapterIdentifierSeparator,
  167. IdentifierQuote: adapterIdentifierQuote,
  168. ValueSeparator: adapterValueSeparator,
  169. ValueQuote: adapterValueQuote,
  170. AndKeyword: adapterAndKeyword,
  171. OrKeyword: adapterOrKeyword,
  172. DescKeyword: adapterDescKeyword,
  173. AscKeyword: adapterAscKeyword,
  174. AssignmentOperator: adapterAssignmentOperator,
  175. ClauseGroup: adapterClauseGroup,
  176. ClauseOperator: adapterClauseOperator,
  177. ColumnValue: adapterColumnValue,
  178. TableAliasLayout: adapterTableAliasLayout,
  179. ColumnAliasLayout: adapterColumnAliasLayout,
  180. SortByColumnLayout: adapterSortByColumnLayout,
  181. WhereLayout: adapterWhereLayout,
  182. JoinLayout: adapterJoinLayout,
  183. OnLayout: adapterOnLayout,
  184. UsingLayout: adapterUsingLayout,
  185. OrderByLayout: adapterOrderByLayout,
  186. InsertLayout: adapterInsertLayout,
  187. SelectLayout: adapterSelectLayout,
  188. UpdateLayout: adapterUpdateLayout,
  189. DeleteLayout: adapterDeleteLayout,
  190. TruncateLayout: adapterTruncateLayout,
  191. DropDatabaseLayout: adapterDropDatabaseLayout,
  192. DropTableLayout: adapterDropTableLayout,
  193. CountLayout: adapterSelectCountLayout,
  194. GroupByLayout: adapterGroupByLayout,
  195. Cache: cache.NewCache(),
  196. }