backends.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. "reflect"
  17. )
  18. type DBBackendName string
  19. const (
  20. // MySQL is the backend name for MySQL/MariaDB
  21. MySQLBackend = DBBackendName("MySQL")
  22. // Clickhouse is the backend name of Clickhouse
  23. ClickhouseBackend = DBBackendName("Clickhouse")
  24. // SQLite is the backend name of Sqlite3
  25. SQLiteBackend = DBBackendName("SQLite")
  26. // PostgreSQLBackend = DBBackendName("PostgreSQL")
  27. // DAMENG is the backend name of DAMENG
  28. DamengBackend = DBBackendName("DAMENG")
  29. )
  30. // IBackend is the interface for all kinds of sql backends, e.g. MySQL, ClickHouse, Sqlite, PostgreSQL, etc.
  31. type IBackend interface {
  32. // Name returns the name of the driver
  33. Name() DBBackendName
  34. // GetTableSQL returns the SQL for query tablenames
  35. GetTableSQL() string
  36. // GetCreateSQL returns the SQL for create a table
  37. GetCreateSQLs(ts ITableSpec) []string
  38. // IsSupportIndexAndContraints returns whether the backend supports index and contraints such as foreigh keys
  39. // MySQL: true
  40. // Sqlite: true
  41. // Clickhouse: false
  42. IsSupportIndexAndContraints() bool
  43. // FetchTableColumnSpecs parse the table definition in database to extract columns' specification of a table
  44. FetchTableColumnSpecs(ts ITableSpec) ([]IColumnSpec, error)
  45. // FetchIndexesAndConstraints parse the table defintion in database to extract index and constraints information of a table
  46. FetchIndexesAndConstraints(ts ITableSpec) ([]STableIndex, []STableConstraint, error)
  47. // GetColumnSpecByFieldType parse the field of model struct to extract column specifiction of a field
  48. GetColumnSpecByFieldType(table *STableSpec, fieldType reflect.Type, fieldname string, tagmap map[string]string, isPointer bool) IColumnSpec
  49. // CurrentUTCTimeStampString returns the string represents current UTC time
  50. CurrentUTCTimeStampString() string
  51. // CurrentTimeStampString returns the string represents current local time
  52. CurrentTimeStampString() string
  53. //
  54. CaseInsensitiveLikeString() string
  55. //
  56. RegexpWhereClause(cond *SRegexpConition) string
  57. //
  58. UnionAllString() string
  59. //
  60. UnionDistinctString() string
  61. // support mixed insert vars
  62. SupportMixedInsertVariables() bool
  63. // Drop table
  64. DropTableSQL(table string) string
  65. // Capability
  66. // CanUpdate returns wether the backend supports update
  67. CanUpdate() bool
  68. // CanInsert returns wether the backend supports Insert
  69. CanInsert() bool
  70. // CanInsertOrUpdate returns weather the backend supports InsertOrUpdate
  71. CanInsertOrUpdate() bool
  72. // InsertSQLTemplate returns the template of insert SQL
  73. InsertSQLTemplate() string
  74. // UpdateSQLTemplate returns the template of update SQL
  75. UpdateSQLTemplate() string
  76. // InsertOrUpdateSQLTemplate returns the template of insert or update SQL
  77. InsertOrUpdateSQLTemplate() string
  78. // prepare insert or update sql
  79. // t: ITableSpec
  80. // names: insert target column names
  81. // insertFields: insert target column values format
  82. // primaryKeys: on conditions primary keys
  83. // updates: update set values
  84. // values: insert values
  85. // updateupdateValues: update values
  86. PrepareInsertOrUpdateSQL(ts ITableSpec, insertColNames []string, insertFields []string, onPrimaryCols []string, updateSetCols []string, insertValues []interface{}, updateValues []interface{}) (string, []interface{})
  87. // CanSupportRowAffected returns wether the backend support RowAffected method after update
  88. // MySQL: true
  89. // Sqlite: false
  90. // Clickhouse: false
  91. CanSupportRowAffected() bool
  92. // CommitTableChangeSQL outputs the SQLs to alter a table
  93. CommitTableChangeSQL(ts ITableSpec, changes STableChanges) []string
  94. QuoteChar() string
  95. ///////////////////////////////////////////////////////////////////////
  96. ////////////////// FUNCTIONS //////////////////////////////////////////
  97. ///////////////////////////////////////////////////////////////////////
  98. // cast field to specified type
  99. CAST(field IQueryField, typeStr string, fieldname string) IQueryField
  100. // cast field to string
  101. CASTString(field IQueryField, fieldname string) IQueryField
  102. // cast field to integer
  103. CASTInt(field IQueryField, fieldname string) IQueryField
  104. // cast field to float
  105. CASTFloat(field IQueryField, fieldname string) IQueryField
  106. // TIMESTAMPADD
  107. TIMESTAMPADD(name string, field IQueryField, offsetSeconds int) IQueryField
  108. // DATE_FORMAT
  109. DATE_FORMAT(name string, field IQueryField, format string) IQueryField
  110. // INET_ATON
  111. INET_ATON(field IQueryField) IQueryField
  112. // INET6_ATON
  113. INET6_ATON(field IQueryField) IQueryField
  114. // AND_Val
  115. AND_Val(name string, field IQueryField, v interface{}) IQueryField
  116. // OR_Val
  117. OR_Val(name string, field IQueryField, v interface{}) IQueryField
  118. // SUBSTR
  119. SUBSTR(name string, field IQueryField, pos, length int) IQueryField
  120. // CONCAT
  121. CONCAT(name string, fields ...IQueryField) IQueryField
  122. // REPLACE
  123. REPLACE(name string, field IQueryField, old string, new string) IQueryField
  124. // GROUP_CONCAT2
  125. GROUP_CONCAT2(name string, sep string, field IQueryField) IQueryField
  126. // DISTINCT
  127. DISTINCT(name string, field IQueryField) IQueryField
  128. // COUNT
  129. COUNT(name string, field ...IQueryField) IQueryField
  130. // MAX
  131. MAX(name string, field IQueryField) IQueryField
  132. // MIN
  133. MIN(name string, field IQueryField) IQueryField
  134. // SUM
  135. SUM(name string, field IQueryField) IQueryField
  136. // AVG
  137. AVG(name string, field IQueryField) IQueryField
  138. // LENGTH
  139. LENGTH(name string, field IQueryField) IQueryField
  140. // LOWER
  141. LOWER(name string, field IQueryField) IQueryField
  142. // UPPER
  143. UPPER(name string, field IQueryField) IQueryField
  144. // DATEDIFF
  145. DATEDIFF(unit string, field1, field2 IQueryField) IQueryField
  146. /////////////////////////////////////////////////////////////////////////////
  147. ///////////////////// Filters ///////////////////////////////////////////////
  148. /////////////////////////////////////////////////////////////////////////////
  149. Equals(f IQueryField, v interface{}) ICondition
  150. }
  151. var _driver_tbl = make(map[DBBackendName]IBackend)
  152. // RegisterBackend registers a backend
  153. func RegisterBackend(drv IBackend) {
  154. _driver_tbl[drv.Name()] = drv
  155. }
  156. func getBackend(name DBBackendName) IBackend {
  157. return _driver_tbl[name]
  158. }