-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDbaEngine.go
More file actions
320 lines (222 loc) · 9.32 KB
/
DbaEngine.go
File metadata and controls
320 lines (222 loc) · 9.32 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package UniEngine
import "fmt"
import "strings"
import "reflect"
/*
func (self TDATA) GetSqlInsert(UniEngineEx UniEngine.TUniEngine, aTableName string) string {
}
func (self TDATA) GetSqlInsertL(UniEngineEx UniEngine.TUniEngine, aTableName string, aDataSize int64) string {
}
func (self TDATA) GetSqlUpdate(UniEngineEx UniEngine.TUniEngine, aTableName string) string {
}
func (self TDATA) GetSqlDelete(UniEngineEx UniEngine.TUniEngine, aTableName string) string {
}
func (self TDATA) SetSqlValues(UniEngineEx UniEngine.TUniEngine, e UniEngine.TQueryType, i *[]interface{}) {
}
func (self TDATA) SetSqlResult(UniEngineEx UniEngine.TUniEngine, result interface{}, column []string, fields []interface{}) {
}
*/
const CONST_PRIVIDER_NAME_TO_POSTGR = "PostgreSQL"
const CONST_PRIVIDER_NAME_TO_SQLSRV = "SQL Server"
const CONST_PRIVIDER_NAME_TO_ORACLE = "Oracle"
const CONST_PRIVIDER_NAME_TO_ACCESS = "Access"
const CONST_PRIVIDER_NAME_TO_SQLITE = ""
const CONST_PRIVIDER_NAME_TO_MYSQLN = "MySQL"
const CONST_PRIVIDER_NAME_TO_KINGES = ""
type TQueryType int
const (
EtSelect TQueryType = 1 + iota
EtInsert
EtUpdate
EtDelele
)
type TConstType int
const (
CtPK TConstType = 1 + iota
CtFK
CtUK
CtDF
)
type TDriveType int
const (
DtPOSTGR TDriveType = 1 + iota
DtSQLSRV
DtORACLE
DtACCESS
DtSQLITE
DtMYSQLN
DtKINGES //#人大金仓
)
var THasSetSqlResult = reflect.TypeOf(new(HasSetSqlResult)).Elem()
var THasGetMapUnique = reflect.TypeOf(new(HasGetMapUnique)).Elem()
type GetMapUnique func(u interface{}) string
type HasGetMapUnique interface {
GetMapUnique() string
}
type HasStartSelect interface {
StartSelect(TUniEngine) error
}
type HasEndedSelect interface {
EndedSelect(TUniEngine) error
}
type HasStartUpdate interface {
StartUpdate(TUniEngine) error
}
type HasEndedUpdate interface {
EndedUpdate(TUniEngine) error
}
type HasStartDelete interface {
StartDelete(TUniEngine) error
}
type HasEndedDelete interface {
EndedDelete(TUniEngine) error
}
type HasStartInsert interface {
StartInsert(TUniEngine) error
}
type HasEndedInsert interface {
EndedInsert(TUniEngine) error
}
// #单笔更新SQL
type HasGetSqlUpdate interface {
GetSqlUpdate(TUniEngine, string) string
}
// #单笔插入SQL
type HasGetSqlInsert interface {
GetSqlInsert(TUniEngine, string) string
}
// #批量插入SQL
type HasGetSqlInsertL interface {
GetSqlInsertL(TUniEngine, string, int64) string
}
// #单笔删除SQL
type HasGetSqlDelete interface {
GetSqlDelete(TUniEngine, string) string
}
// #单笔取值
type HasSetSqlValues interface {
SetSqlValues(TUniEngine, TQueryType, *[]interface{})
}
// #批量取值L
type HasSetSqlValuesL interface {
SetSqlValuesL(TUniEngine, TQueryType, reflect.Value, *[]interface{})
}
// #读取数据
type HasSetSqlResult interface {
SetSqlResult(TUniEngine, interface{}, []string, []interface{})
}
// #for tuniengine get exist table
type HasGetSqlExistTable interface {
GetSqlExistTable(TUniEngine, string) string
}
// #for tuniengine get exist table
type HasGetSqlExistViews interface {
GetSqlExistViews(TUniEngine, string) string
}
type TExistTable4POSTGR struct{}
func (self TExistTable4POSTGR) GetSqlExistTable(UniEngineEx TUniEngine, TableName string) string {
result := "select count(relname) as value from pg_class where relname = '%s'"
return fmt.Sprintf(result, strings.ToLower(TableName))
}
func (self TExistTable4POSTGR) GetSqlExistViews(UniEngineEx TUniEngine, TableName string) string {
result := "select count(relname) as value from pg_class where relname = '%s'"
return fmt.Sprintf(result, strings.ToLower(TableName))
}
type TExistTable4SQLSRV struct{}
func (self TExistTable4SQLSRV) GetSqlExistTable(UniEngineEx TUniEngine, TableName string) string {
result := "select count(*) from sysobjects where 1=1 and name = '%s'"
return fmt.Sprintf(result, strings.ToLower(TableName))
}
func (self TExistTable4SQLSRV) GetSqlExistViews(UniEngineEx TUniEngine, TableName string) string {
result := "select count(*) from sysobjects where 1=1 and name = '%s'"
return fmt.Sprintf(result, strings.ToLower(TableName))
}
type TExistTable4ORACLE struct{}
func (self TExistTable4ORACLE) GetSqlExistTable(UniEngineEx TUniEngine, TableName string) string {
result := "select count(*) from all_tables where table_name = upper('%s')"
return fmt.Sprintf(result, TableName)
}
func (self TExistTable4ORACLE) GetSqlExistViews(UniEngineEx TUniEngine, TableName string) string {
result := "select count(*) from user_views where view_name = upper('%s')"
return fmt.Sprintf(result, TableName)
}
type TExistTable4MYSQLN struct{}
func (self TExistTable4MYSQLN) GetSqlExistTable(UniEngineEx TUniEngine, TableName string, DataBase string) string {
result := "select count(*) from information_schema.tables t where table_name = '%s' and table_schema = '%s'"
return fmt.Sprintf(result, TableName, DataBase)
}
func (self TExistTable4MYSQLN) GetSqlExistViews(UniEngineEx TUniEngine, TableName string, DataBase string) string {
result := "select count(*) from information_schema.tables t where table_name = '%s' and table_schema = '%s'"
return fmt.Sprintf(result, TableName, DataBase)
}
// #for tuniengine get exist field
type HasGetSqlExistField interface {
GetSqlExistField(TUniEngine, string, string) string
}
type TExistField4POSTGR struct{}
func (self TExistField4POSTGR) GetSqlExistField(UniEngineEx TUniEngine, TableName string, FieldName string) string {
result := "select count(a.attname) as value from pg_attribute a" +
" left join pg_class b on a.attrelid=b.oid where b.relname='%s' and a.attname='%s' and attnum>0"
return fmt.Sprintf(result, strings.ToLower(TableName), strings.ToLower(FieldName))
}
type TExistField4SQLSRV struct{}
func (self TExistField4SQLSRV) GetSqlExistField(UniEngineEx TUniEngine, TableName string, FieldName string) string {
result := "select count(*) as value from syscolumns where 1=1 and id=object_id('%s') and name = '%s'"
return fmt.Sprintf(result, strings.ToLower(TableName), strings.ToLower(FieldName))
}
type TExistField4ORACLE struct{}
func (self TExistField4ORACLE) GetSqlExistField(UniEngineEx TUniEngine, TableName string, FieldName string) string {
result := "select count(*) as value from user_tab_columns where table_name=upper('%s') and column_name=upper('%s')"
return fmt.Sprintf(result, strings.ToUpper(TableName), strings.ToUpper(FieldName))
}
type TExistField4MYSQLN struct{}
func (self TExistField4MYSQLN) GetSqlExistField(UniEngineEx TUniEngine, TableName string, FieldName string, DataBase string) string {
result := "select count(*) as value from information_schema.columns where and table_schema = %s and table_name = %s and column_name =%s"
return fmt.Sprintf(result, DataBase, TableName, FieldName)
}
// #for tuniengine get exist const
type HasGetSqlExistConst interface {
GetSqlExistConst() string
}
// #for tuniengine get primary keys
type HasGetSqlAutoKeys interface {
GetSqlAutoKeys(TUniEngine, string) string
}
type TAutoKeys4POSTGR struct{}
func (self TAutoKeys4POSTGR) GetSqlAutoKeys(UniEngineEx TUniEngine, TableName string) string {
result := "select attname as field_name from pg_attribute" +
" left join pg_class on pg_attribute.attrelid = pg_class.oid" +
" where pg_class.relname = '%s' and attstattarget=-1 and attnum>0" +
" and exists (select * from pg_constraint where pg_constraint.conrelid = pg_class.oid and pg_constraint.contype='p' and attnum=any(conkey))"
return fmt.Sprintf(result, strings.ToLower(TableName))
}
type TAutoKeys4SQLSRV struct{}
func (self TAutoKeys4SQLSRV) GetSqlAutoKeys(UniEngineEx TUniEngine, TableName string) string {
//@result := "select a.name as field_name from syscolumns a inner join sysindexkeys b on a.id=b.id and a.colid =b.colid where a.id = object_id('%s')"
result := "select syscolumns.name as field_name" +
" from syscolumns,sysobjects,sysindexes,sysindexkeys" +
" where syscolumns.id = object_id('%s')" +
" and sysobjects.xtype = 'pk'" +
" and sysobjects.parent_obj = syscolumns.id" +
" and sysindexes.id = syscolumns.id" +
" and sysobjects.name = sysindexes.name" +
" and sysindexkeys.id = syscolumns.id" +
" and sysindexkeys.indid = sysindexes.indid" +
" and syscolumns.colid = sysindexkeys.colid;"
return fmt.Sprintf(result, TableName)
}
type TAutoKeys4ORACLE struct{}
func (self TAutoKeys4ORACLE) GetSqlAutoKeys(UniEngineEx TUniEngine, TableName string) string {
result := "select cu.column_name as field_name from user_cons_columns cu, user_constraints au where cu.constraint_name = au.constraint_name and au.constraint_type = upper('p') and au.table_name =upper('%s')"
return fmt.Sprintf(result, TableName)
}
type TAutoKeys4MYSQLN struct {
DataBase string
}
func (self TAutoKeys4MYSQLN) GetSqlAutoKeys(UniEngineEx TUniEngine, TableName string) string {
if self.DataBase == "" {
panic("UniEngine: you shoule be specify attribute [database] when using mysql.")
}
result := "select column_name as field_name from information_schema.columns where 1=1 and table_schema = '%s' and table_name = '%s' and column_key = 'PRI'"
return fmt.Sprintf(result, self.DataBase, TableName)
}