backends_base.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package sqlchemy
  15. import (
  16. "fmt"
  17. "reflect"
  18. "strings"
  19. )
  20. var defaultBackend IBackend = (*SBaseBackend)(nil)
  21. type SBaseBackend struct{}
  22. func (bb *SBaseBackend) Name() DBBackendName {
  23. return "default"
  24. }
  25. func (bb *SBaseBackend) GetCreateSQLs(ts ITableSpec) []string {
  26. return []string{}
  27. }
  28. func (bb *SBaseBackend) IsSupportIndexAndContraints() bool {
  29. return false
  30. }
  31. func (bb *SBaseBackend) GetTableSQL() string {
  32. return "SHOW TABLES"
  33. }
  34. func (bb *SBaseBackend) FetchTableColumnSpecs(ts ITableSpec) ([]IColumnSpec, error) {
  35. return nil, nil
  36. }
  37. func (bb *SBaseBackend) GetColumnSpecByFieldType(table *STableSpec, fieldType reflect.Type, fieldname string, tagmap map[string]string, isPointer bool) IColumnSpec {
  38. return nil
  39. }
  40. func (bb *SBaseBackend) CurrentUTCTimeStampString() string {
  41. return "NOW('UTC')"
  42. }
  43. func (bb *SBaseBackend) CurrentTimeStampString() string {
  44. return "NOW()"
  45. }
  46. func (bb *SBaseBackend) CaseInsensitiveLikeString() string {
  47. return "LIKE"
  48. }
  49. func (bb *SBaseBackend) RegexpWhereClause(cond *SRegexpConition) string {
  50. return tupleConditionWhereClause(&cond.STupleCondition, SQL_OP_REGEXP)
  51. }
  52. func (bb *SBaseBackend) UnionAllString() string {
  53. return "UNION ALL"
  54. }
  55. func (bb *SBaseBackend) UnionDistinctString() string {
  56. return "UNION"
  57. }
  58. func (bb *SBaseBackend) DropTableSQL(table string) string {
  59. return fmt.Sprintf("DROP TABLE `%s`", table)
  60. }
  61. func (bb *SBaseBackend) SupportMixedInsertVariables() bool {
  62. return true
  63. }
  64. func (bb *SBaseBackend) CanUpdate() bool {
  65. return false
  66. }
  67. func (bb *SBaseBackend) CanInsert() bool {
  68. return false
  69. }
  70. func (bb *SBaseBackend) CanInsertOrUpdate() bool {
  71. return false
  72. }
  73. func (bb *SBaseBackend) CommitTableChangeSQL(ts ITableSpec, changes STableChanges) []string {
  74. return nil
  75. }
  76. func (bb *SBaseBackend) FetchIndexesAndConstraints(ts ITableSpec) ([]STableIndex, []STableConstraint, error) {
  77. return nil, nil, nil
  78. }
  79. func (bb *SBaseBackend) DropIndexSQLTemplate() string {
  80. return "DROP INDEX `{{ .Index }}` ON `{{ .Table }}`"
  81. }
  82. func (bb *SBaseBackend) CanSupportRowAffected() bool {
  83. return true
  84. }
  85. func (bb *SBaseBackend) InsertSQLTemplate() string {
  86. return "INSERT INTO `{{ .Table }}` ({{ .Columns }}) VALUES ({{ .Values }})"
  87. }
  88. func (bb *SBaseBackend) UpdateSQLTemplate() string {
  89. return "UPDATE `{{ .Table }}` SET {{ .Columns }} WHERE {{ .Conditions }}"
  90. }
  91. func (bb *SBaseBackend) InsertOrUpdateSQLTemplate() string {
  92. return ""
  93. }
  94. func (bb *SBaseBackend) CAST(field IQueryField, typeStr string, fieldname string) IQueryField {
  95. return NewFunctionField(fieldname, false, `CAST(%s AS `+typeStr+`)`, field)
  96. }
  97. // cast field to string
  98. func (bb *SBaseBackend) CASTString(field IQueryField, fieldname string) IQueryField {
  99. return NewFunctionField(fieldname, false, `CAST(%s AS CHAR)`, field)
  100. }
  101. // cast field to integer
  102. func (bb *SBaseBackend) CASTInt(field IQueryField, fieldname string) IQueryField {
  103. return NewFunctionField(fieldname, false, `CAST(%s AS SIGNED)`, field)
  104. }
  105. // cast field to float
  106. func (bb *SBaseBackend) CASTFloat(field IQueryField, fieldname string) IQueryField {
  107. return NewFunctionField(fieldname, false, `CAST(%s AS REAL)`, field)
  108. }
  109. // TimestampAdd represents a SQL function TimestampAdd
  110. func (bb *SBaseBackend) TIMESTAMPADD(name string, field IQueryField, offsetSeconds int) IQueryField {
  111. return NewFunctionField(name, false, `TIMESTAMPADD(SECOND, `+fmt.Sprintf("%d", offsetSeconds)+`, %s)`, field)
  112. }
  113. // DATE_FORMAT represents a SQL function DATE_FORMAT
  114. func (bb *SBaseBackend) DATE_FORMAT(name string, field IQueryField, format string) IQueryField {
  115. return NewFunctionField(name, false, `DATE_FORMAT(%s, "`+strings.ReplaceAll(format, "%", "%%")+`")`, field)
  116. }
  117. // INET_ATON represents a SQL function INET_ATON
  118. func (bb *SBaseBackend) INET_ATON(field IQueryField) IQueryField {
  119. return NewFunctionField("", false, `INET_ATON(%s)`, field)
  120. }
  121. // INET6_ATON represents a SQL function INET6_ATON
  122. func (bb *SBaseBackend) INET6_ATON(field IQueryField) IQueryField {
  123. return NewFunctionField("", false, `INET6_ATON(%s)`, field)
  124. }
  125. // SubStr represents a SQL function SUBSTR
  126. func (bb *SBaseBackend) SUBSTR(name string, field IQueryField, pos, length int) IQueryField {
  127. var rightStr string
  128. if length <= 0 {
  129. rightStr = fmt.Sprintf("%d)", pos)
  130. } else {
  131. rightStr = fmt.Sprintf("%d, %d)", pos, length)
  132. }
  133. return NewFunctionField(name, false, `SUBSTR(%s, `+rightStr, field)
  134. }
  135. // OR_Val represents a SQL function that does binary | operation on a field
  136. func (bb *SBaseBackend) OR_Val(name string, field IQueryField, v interface{}) IQueryField {
  137. rightStr := fmt.Sprintf("|%v", v)
  138. return NewFunctionField(name, false, "%s"+rightStr, field)
  139. }
  140. // AND_Val represents a SQL function that does binary & operation on a field
  141. func (bb *SBaseBackend) AND_Val(name string, field IQueryField, v interface{}) IQueryField {
  142. rightStr := fmt.Sprintf("&%v", v)
  143. return NewFunctionField(name, false, "%s"+rightStr, field)
  144. }
  145. // CONCAT represents a SQL function CONCAT
  146. func (bb *SBaseBackend) CONCAT(name string, fields ...IQueryField) IQueryField {
  147. params := []string{}
  148. for i := 0; i < len(fields); i++ {
  149. params = append(params, "%s")
  150. }
  151. return NewFunctionField(name, false, `CONCAT(`+strings.Join(params, ",")+`)`, fields...)
  152. }
  153. // REPLACE represents a SQL function REPLACE
  154. func (bb *SBaseBackend) REPLACE(name string, field IQueryField, old string, new string) IQueryField {
  155. return NewFunctionField(name, false, fmt.Sprintf(`REPLACE(%s, "%s", "%s")`, "%s", old, new), field)
  156. }
  157. // DISTINCT represents the SQL function DISTINCT
  158. func (bb *SBaseBackend) DISTINCT(name string, field IQueryField) IQueryField {
  159. return NewFunctionField(name, false, "DISTINCT(%s)", field)
  160. }
  161. // COUNT represents the SQL function COUNT
  162. func (bb *SBaseBackend) COUNT(name string, field ...IQueryField) IQueryField {
  163. var expr string
  164. if len(field) == 0 {
  165. expr = "COUNT(*)"
  166. } else {
  167. expr = "COUNT(%s)"
  168. }
  169. return NewFunctionField(name, true, expr, field...)
  170. }
  171. // MAX represents the SQL function MAX
  172. func (bb *SBaseBackend) MAX(name string, field IQueryField) IQueryField {
  173. return NewFunctionField(name, true, "MAX(%s)", field)
  174. }
  175. // MIN represents the SQL function MIN
  176. func (bb *SBaseBackend) MIN(name string, field IQueryField) IQueryField {
  177. return NewFunctionField(name, true, "MIN(%s)", field)
  178. }
  179. // SUM represents the SQL function SUM
  180. func (bb *SBaseBackend) SUM(name string, field IQueryField) IQueryField {
  181. return NewFunctionField(name, true, "SUM(%s)", field)
  182. }
  183. // AVG represents the SQL function SUM
  184. func (bb *SBaseBackend) AVG(name string, field IQueryField) IQueryField {
  185. return NewFunctionField(name, true, "AVG(%s)", field)
  186. }
  187. // LENGTH represents SQL function LENGTH
  188. func (bb *SBaseBackend) LENGTH(name string, field IQueryField) IQueryField {
  189. return NewFunctionField(name, false, "LENGTH(%s)", field)
  190. }
  191. func (bb *SBaseBackend) GROUP_CONCAT2(name string, sep string, field IQueryField) IQueryField {
  192. return NewFunctionField(name, true, fmt.Sprintf("GROUP_CONCAT(%%s SEPARATOR '%s')", sep), field)
  193. }
  194. // LOWER represents SQL function of LOWER
  195. func (bb *SBaseBackend) LOWER(name string, field IQueryField) IQueryField {
  196. return NewFunctionField(name, false, "LOWER(%s)", field)
  197. }
  198. // UPPER represents SQL function of UPPER
  199. func (bb *SBaseBackend) UPPER(name string, field IQueryField) IQueryField {
  200. return NewFunctionField(name, false, "UPPER(%s)", field)
  201. }
  202. // DATEDIFF represents SQL function of DATEDIFF
  203. func (bb *SBaseBackend) DATEDIFF(unit string, field1, field2 IQueryField) IQueryField {
  204. return NewFunctionField("", false, fmt.Sprintf("DATEDIFF('%s',%s,%s)", unit, "%s", "%s"), field1, field2)
  205. }
  206. func (bb *SBaseBackend) QuoteChar() string {
  207. return "`"
  208. }
  209. func (bb *SBaseBackend) PrepareInsertOrUpdateSQL(ts ITableSpec, insertColNames []string, insertFields []string, onPrimaryCols []string, updateSetCols []string, insertValues []interface{}, updateValues []interface{}) (string, []interface{}) {
  210. return "", nil
  211. }
  212. func (bb *SBaseBackend) Equals(f IQueryField, v interface{}) ICondition {
  213. c := SEqualsCondition{NewTupleCondition(f, v)}
  214. return &c
  215. }