-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathbackends.go
175 lines (156 loc) · 6.45 KB
/
backends.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sqlchemy
import (
"reflect"
)
type DBBackendName string
const (
// MySQL is the backend name for MySQL/MariaDB
MySQLBackend = DBBackendName("MySQL")
// Clickhouse is the backend name of Clickhouse
ClickhouseBackend = DBBackendName("Clickhouse")
// SQLite is the backend name of Sqlite3
SQLiteBackend = DBBackendName("SQLite")
// PostgreSQLBackend = DBBackendName("PostgreSQL")
// DAMENG is the backend name of DAMENG
DamengBackend = DBBackendName("DAMENG")
)
// IBackend is the interface for all kinds of sql backends, e.g. MySQL, ClickHouse, Sqlite, PostgreSQL, etc.
type IBackend interface {
// Name returns the name of the driver
Name() DBBackendName
// GetTableSQL returns the SQL for query tablenames
GetTableSQL() string
// GetCreateSQL returns the SQL for create a table
GetCreateSQLs(ts ITableSpec) []string
// IsSupportIndexAndContraints returns whether the backend supports index and contraints such as foreigh keys
// MySQL: true
// Sqlite: true
// Clickhouse: false
IsSupportIndexAndContraints() bool
// FetchTableColumnSpecs parse the table definition in database to extract columns' specification of a table
FetchTableColumnSpecs(ts ITableSpec) ([]IColumnSpec, error)
// FetchIndexesAndConstraints parse the table defintion in database to extract index and constraints information of a table
FetchIndexesAndConstraints(ts ITableSpec) ([]STableIndex, []STableConstraint, error)
// GetColumnSpecByFieldType parse the field of model struct to extract column specifiction of a field
GetColumnSpecByFieldType(table *STableSpec, fieldType reflect.Type, fieldname string, tagmap map[string]string, isPointer bool) IColumnSpec
// CurrentUTCTimeStampString returns the string represents current UTC time
CurrentUTCTimeStampString() string
// CurrentTimeStampString returns the string represents current local time
CurrentTimeStampString() string
//
CaseInsensitiveLikeString() string
//
RegexpWhereClause(cond *SRegexpConition) string
//
UnionAllString() string
//
UnionDistinctString() string
// support mixed insert vars
SupportMixedInsertVariables() bool
// Drop table
DropTableSQL(table string) string
// Capability
// CanUpdate returns wether the backend supports update
CanUpdate() bool
// CanInsert returns wether the backend supports Insert
CanInsert() bool
// CanInsertOrUpdate returns weather the backend supports InsertOrUpdate
CanInsertOrUpdate() bool
// InsertSQLTemplate returns the template of insert SQL
InsertSQLTemplate() string
// UpdateSQLTemplate returns the template of update SQL
UpdateSQLTemplate() string
// InsertOrUpdateSQLTemplate returns the template of insert or update SQL
InsertOrUpdateSQLTemplate() string
// prepare insert or update sql
// t: ITableSpec
// names: insert target column names
// insertFields: insert target column values format
// primaryKeys: on conditions primary keys
// updates: update set values
// values: insert values
// updateupdateValues: update values
PrepareInsertOrUpdateSQL(ts ITableSpec, insertColNames []string, insertFields []string, onPrimaryCols []string, updateSetCols []string, insertValues []interface{}, updateValues []interface{}) (string, []interface{})
// CanSupportRowAffected returns wether the backend support RowAffected method after update
// MySQL: true
// Sqlite: false
// Clickhouse: false
CanSupportRowAffected() bool
// CommitTableChangeSQL outputs the SQLs to alter a table
CommitTableChangeSQL(ts ITableSpec, changes STableChanges) []string
QuoteChar() string
///////////////////////////////////////////////////////////////////////
////////////////// FUNCTIONS //////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
// cast field to specified type
CAST(field IQueryField, typeStr string, fieldname string) IQueryField
// cast field to string
CASTString(field IQueryField, fieldname string) IQueryField
// cast field to integer
CASTInt(field IQueryField, fieldname string) IQueryField
// cast field to float
CASTFloat(field IQueryField, fieldname string) IQueryField
// TIMESTAMPADD
TIMESTAMPADD(name string, field IQueryField, offsetSeconds int) IQueryField
// DATE_FORMAT
DATE_FORMAT(name string, field IQueryField, format string) IQueryField
// INET_ATON
INET_ATON(field IQueryField) IQueryField
// AND_Val
AND_Val(name string, field IQueryField, v interface{}) IQueryField
// OR_Val
OR_Val(name string, field IQueryField, v interface{}) IQueryField
// SUBSTR
SUBSTR(name string, field IQueryField, pos, length int) IQueryField
// CONCAT
CONCAT(name string, fields ...IQueryField) IQueryField
// REPLACE
REPLACE(name string, field IQueryField, old string, new string) IQueryField
// GROUP_CONCAT2
GROUP_CONCAT2(name string, sep string, field IQueryField) IQueryField
// DISTINCT
DISTINCT(name string, field IQueryField) IQueryField
// COUNT
COUNT(name string, field ...IQueryField) IQueryField
// MAX
MAX(name string, field IQueryField) IQueryField
// MIN
MIN(name string, field IQueryField) IQueryField
// SUM
SUM(name string, field IQueryField) IQueryField
// AVG
AVG(name string, field IQueryField) IQueryField
// LENGTH
LENGTH(name string, field IQueryField) IQueryField
// LOWER
LOWER(name string, field IQueryField) IQueryField
// UPPER
UPPER(name string, field IQueryField) IQueryField
// DATEDIFF
DATEDIFF(unit string, field1, field2 IQueryField) IQueryField
/////////////////////////////////////////////////////////////////////////////
///////////////////// Filters ///////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
Equals(f IQueryField, v interface{}) ICondition
}
var _driver_tbl = make(map[DBBackendName]IBackend)
// RegisterBackend registers a backend
func RegisterBackend(drv IBackend) {
_driver_tbl[drv.Name()] = drv
}
func getBackend(name DBBackendName) IBackend {
return _driver_tbl[name]
}