-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
schema.go
308 lines (279 loc) · 6.7 KB
/
schema.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
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
package sqlmapper
// DatabaseType represents the supported database types
type DatabaseType string
// Database represents the common interface for all database implementations
type Database interface {
Parse(content string) (*Schema, error)
Generate(schema *Schema) (string, error)
}
const (
MySQL DatabaseType = "mysql"
PostgreSQL DatabaseType = "postgresql"
SQLServer DatabaseType = "sqlserver"
Oracle DatabaseType = "oracle"
SQLite DatabaseType = "sqlite"
)
// Schema represents a database schema
type Schema struct {
Name string
Tables []Table
Procedures []Procedure
Functions []Function
Triggers []Trigger
Views []View
Sequences []Sequence
Extensions []Extension
Permissions []Permission
UserDefinedTypes []UserDefinedType
Partitions map[string][]Partition // table_name -> partitions
DatabaseLinks []DatabaseLink
Tablespaces []Tablespace
Roles []Role
Users []User
Clusters []Cluster
MaterializedLogs []MaterializedViewLog
Types []Type
}
// Table represents a database table
type Table struct {
Name string
Schema string
Columns []Column
Indexes []Index
Constraints []Constraint
Data []Row
TableSpace string
Storage *StorageClause
Temporary bool
Comment string
Options string // Storage engine options (e.g., ENGINE=InnoDB, CHARSET=utf8mb4)
}
// Column represents a table column
type Column struct {
Name string
DataType string
Length int
Scale int
Precision int
IsNullable bool `default:"true"`
DefaultValue string
AutoIncrement bool
IsPrimaryKey bool
IsUnique bool
Comment string
Order int
CheckExpression string
}
// Index represents a table index
type Index struct {
Name string
Columns []string
IsUnique bool
IsBitmap bool // Oracle için bitmap indeks desteği
IsClustered bool // SQL Server için clustered indeks desteği
Type string // BTREE, HASH etc.
Condition string // WHERE clause
TableSpace string
Storage *StorageClause
Compression bool
}
// Constraint represents a table constraint
type Constraint struct {
Name string
Type string // PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK
Columns []string
RefTable string
RefColumns []string
UpdateRule string
DeleteRule string
CheckExpression string
Deferrable bool
Initially string // IMMEDIATE, DEFERRED
}
// Row represents table data
type Row struct {
Values map[string]interface{}
}
// Procedure represents a stored procedure
type Procedure struct {
Name string
Schema string
Parameters []Parameter
Body string
Language string
Security string // DEFINER, INVOKER
SQLSecurity string
Deterministic bool
Comment string
}
// Function represents a database function
type Function struct {
Name string
Schema string
Parameters []Parameter
Returns string
Body string
Language string
IsProc bool
}
// Parameter represents a procedure or function parameter
type Parameter struct {
Name string
DataType string
Direction string // IN, OUT, INOUT
Default string
}
// Trigger represents a database trigger
type Trigger struct {
Name string
Schema string
Table string
Timing string
Event string
Body string
Condition string
ForEachRow bool
}
// View represents a database view
type View struct {
Name string
Schema string
Definition string
IsMaterialized bool
}
// Sequence represents a database sequence
type Sequence struct {
Name string
Schema string
IncrementBy int
MinValue int
MaxValue int
StartValue int
Cache int
Cycle bool
}
// Extension represents a database extension
type Extension struct {
Name string
Version string
Schema string
}
// Permission represents a database permission
type Permission struct {
Type string // GRANT, REVOKE
Privileges []string
Object string
Grantee string
WithGrant bool
}
// UserDefinedType represents custom data types
type UserDefinedType struct {
Name string
Schema string
BaseType string
Properties map[string]interface{}
}
// Partition represents table partition information
type Partition struct {
Name string
Type string // RANGE, LIST, HASH
SubPartitions []SubPartition
Expression string
Values []string
TableSpace string
Storage *StorageClause
}
// SubPartition represents table sub-partition information
type SubPartition struct {
Name string
Type string
Expression string
Values []string
TableSpace string
Storage *StorageClause
}
// MaterializedViewLog represents materialized view log information
type MaterializedViewLog struct {
Name string
Schema string
TableName string
Columns []string
RowID bool
PrimaryKey bool
SequenceNumber bool
CommitSCN bool
Storage *StorageClause
}
// DatabaseLink represents database link information
type DatabaseLink struct {
Name string
Owner string
ConnectInfo string
Public bool
}
// Tablespace represents tablespace information
type Tablespace struct {
Name string
Type string // PERMANENT, TEMPORARY
Status string
Autoextend bool
MaxSize int64
InitialSize int64
DataFile string
BlockSize int
Logging bool
}
// Role represents database role information
type Role struct {
Name string
Password string
Permissions []Permission
Members []string
System bool
}
// User represents database user information
type User struct {
Name string
Password string
DefaultRole string
Roles []string
Permissions []Permission
Profile string
Status string
TableSpace string
TempSpace string
}
// Cluster represents Oracle cluster information
type Cluster struct {
Name string
Schema string
TableSpace string
Key []string
Tables []string
Size int
HashKeys int
Storage *StorageClause
}
// StorageClause represents storage properties
type StorageClause struct {
Initial int64
Next int64
MinExtents int
MaxExtents int
Pctincrease int
Buffer int
TableSpace string
Logging bool
}
// Parser represents an interface for database dump operations
type Parser interface {
Parse(content string) (*Schema, error)
Generate(schema *Schema) (string, error)
}
// Type represents a database type
type Type struct {
Name string
Schema string
Kind string // ENUM, COMPOSITE, DOMAIN, etc.
Definition string
}