-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples.go
More file actions
532 lines (471 loc) · 11 KB
/
examples.go
File metadata and controls
532 lines (471 loc) · 11 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
package main
import (
"errors"
"fmt"
"github.com/sharovik/orm/clients"
"github.com/sharovik/orm/dto"
"github.com/sharovik/orm/query"
)
type AnotherModel struct {
dto.BaseModel
}
type TestTableModel struct {
dto.BaseModel
}
var (
err error
client clients.BaseClientInterface
model *TestTableModel
another *AnotherModel
)
func main() {
//We create database configuration for MySQL database
configuration := clients.DatabaseConfig{
Host: "localhost",
Username: "root",
Password: "secret",
Database: "test",
Type: clients.DatabaseTypeMySQL,
}
client, err = clients.InitClient(configuration)
if err != nil {
fmt.Println(fmt.Sprintf("Failed to connect to the database. Reason: %s", err))
return
}
if err = createTables(); err != nil {
panic(err)
}
fmt.Println("Tables created")
if err = triggerSelectQueries(); err != nil {
panic(err)
}
fmt.Println("Select queries executed")
if err = triggerInsert(); err != nil {
panic(err)
}
fmt.Println("Insert queries executed")
if err = triggerUpdate(); err != nil {
panic(err)
}
fmt.Println("Update queries executed")
if err = triggerTransactions(); err != nil {
panic(err)
}
fmt.Println("Transaction queries executed")
if err = triggerAlter(); err != nil {
panic(err)
}
fmt.Println("Alter queries executed")
if err = triggerDelete(); err != nil {
panic(err)
}
fmt.Println("Delete queries executed")
if err = triggerTableDrop(); err != nil {
panic(err)
}
fmt.Println("Drop queries executed")
if err = triggerTableRenaming(); err != nil {
panic(err)
}
fmt.Println("Tables renamed and dropped queries executed")
fmt.Println("All good")
}
func createTables() error {
another = new(AnotherModel)
another.SetTableName("another")
another.SetPrimaryKey(dto.ModelField{
Name: "id",
Type: "integer",
Value: nil,
Default: nil,
Length: 0,
IsNullable: false,
IsUnsigned: true,
AutoIncrement: true,
})
//Let's create a table for that model
q := new(clients.Query).Create(another).IfNotExists()
_, err := client.Execute(q)
if err != nil {
return err
}
model = new(TestTableModel)
model.SetTableName("test_table_name")
model.SetPrimaryKey(dto.ModelField{
Name: "id",
Type: "integer",
Value: nil,
Default: nil,
Length: 0,
IsNullable: false,
IsUnsigned: true,
AutoIncrement: true,
})
model.AddModelField(dto.ModelField{
Name: "another_id",
Type: dto.IntegerColumnType,
Value: nil,
Default: nil,
Length: 0,
IsNullable: true,
IsPrimaryKey: false,
IsUnsigned: true,
AutoIncrement: false,
})
model.AddModelField(dto.ModelField{
Name: "test_field",
Type: dto.VarcharColumnType,
Value: "something",
Default: "",
Length: 255,
IsNullable: true,
IsPrimaryKey: false,
AutoIncrement: false,
})
model.AddModelField(dto.ModelField{
Name: "test_field2",
Type: dto.VarcharColumnType,
Value: "something",
Default: "",
Length: 255,
IsNullable: true,
IsPrimaryKey: false,
AutoIncrement: false,
})
//Let's create a table for that model
q = new(clients.Query).Create(model).
AddForeignKey(dto.ForeignKey{
Name: "another_key",
Target: query.Reference{
Table: "another",
Key: "id",
},
With: query.Reference{
Table: model.TableName,
Key: "another_id",
},
OnDelete: dto.CascadeAction,
OnUpdate: dto.NoActionAction,
}).AddIndex(dto.Index{
Name: "some_test_non_unique_index",
Target: model.TableName,
Key: "test_field2",
Unique: false,
}).AddIndex(dto.Index{
Name: "some_test_unique_index",
Target: model.TableName,
Key: "test_field",
Unique: true,
}).IfNotExists()
_, err = client.Execute(q)
return err
}
func triggerTableRenaming() error {
//We rename the another table
q := new(clients.Query).Rename(another.GetTableName(), "new_another_table")
_, err := client.Execute(q)
if err != nil {
fmt.Println(err)
return err
}
q = new(clients.Query).Select([]interface{}{}).From(&dto.BaseModel{
TableName: "new_another_table",
})
_, err = client.Execute(q)
if err != nil {
fmt.Println(err)
return err
}
//We drop the table
another.SetTableName("new_another_table")
q = new(clients.Query).Drop(another)
_, err = client.Execute(q)
if err != nil {
fmt.Println(err)
return err
}
return nil
}
func triggerTableDrop() error {
//We drop the table
q := new(clients.Query).Drop(model)
_, err := client.Execute(q)
if err != nil {
fmt.Println(err)
return err
}
return nil
}
func triggerDelete() error {
//We delete item from the table
q := new(clients.Query).Delete().
From(model).
Where(query.Where{
First: "id",
Operator: "=",
Second: "1",
})
_, err := client.Execute(q)
if err != nil {
fmt.Println(err)
return err
}
return nil
}
func triggerAlter() error {
//We alter table
q := new(clients.Query).Alter(model).
AddColumn(dto.ModelField{
Name: "new_column",
Type: dto.VarcharColumnType,
Value: nil,
Default: "",
Length: 244,
IsNullable: true,
IsPrimaryKey: false,
IsUnsigned: false,
AutoIncrement: false,
}).DropColumn(dto.ModelField{
Name: "test_field",
}).DropForeignKey(dto.ForeignKey{
Name: "another_key",
}).DropIndex(dto.Index{
Name: "some_test_unique_index",
})
_, err := client.Execute(q)
if err != nil {
fmt.Println(err)
return err
}
//We add new indexes
q = new(clients.Query).Alter(model).
AddIndex(dto.Index{
Name: "my_brand_new_index",
Target: model.GetTableName(),
Key: "new_column",
Unique: false,
})
_, err = client.Execute(q)
if err != nil {
fmt.Println(err)
return err
}
return nil
}
func triggerSelectQueries() error {
//We select specific columns from the table
var columns = []interface{}{"id", "another_id"}
q := new(clients.Query).Select(columns).From(model)
_, err := client.Execute(q)
if err != nil {
fmt.Println(err)
return err
}
//We select specific columns from the table
q = new(clients.Query).Select(columns).From(model.TableName)
_, err = client.Execute(q)
if err != nil {
fmt.Println(err)
return err
}
//We do select with join to other table
q = new(clients.Query).
Select([]interface{}{model.GetTableName() + ".id", "another_id"}).
From(model).
Join(query.Join{
Target: query.Reference{Table: "another", Key: "id"},
With: query.Reference{Table: model.TableName, Key: "another_id"},
Condition: "=",
Type: query.LeftJoinType,
}).
Where(query.Where{
First: "another.id",
Operator: "is",
Second: "NULL",
})
_, err = client.Execute(q)
if err != nil {
fmt.Println(err)
return err
}
return nil
}
func triggerInsert() error {
//We insert new item into our table
q := new(clients.Query).Insert(model)
_, err := client.Execute(q)
if err != nil {
fmt.Println(err)
return err
}
model.AddModelField(dto.ModelField{
Name: "test_field",
Type: dto.VarcharColumnType,
Value: "something2",
Default: "",
Length: 255,
IsNullable: true,
IsPrimaryKey: false,
AutoIncrement: false,
})
//We insert new item into our table
q = new(clients.Query).Insert(model)
_, err = client.Execute(q)
if err != nil {
fmt.Println(err)
return err
}
//We select all model columns from our table
q = new(clients.Query).Select(model.GetColumns()).From(model)
_, err = client.Execute(q)
if err != nil {
fmt.Println(err)
return err
}
//We do select from the table where id = 1 OR id = 2
q = new(clients.Query).Select(model.GetColumns()).
From(model).
Where(query.Where{
First: "id",
Operator: "=",
Second: "1",
}).
Where(query.Where{
First: "id",
Operator: "=",
Second: "2",
Type: query.WhereOrType, //For OR condition, you can use the Type attribute of Where object
})
_, err = client.Execute(q)
if err != nil {
fmt.Println(err)
return err
}
//We do select with the more complex WHERE clause
//The output will be:
//SELECT id, id, id, another_id, test_field, test_field2 FROM test_table_name WHERE (id = 1 OR id = 2) OR id = 3
q = new(clients.Query).Select(model.GetColumns()).
From(model).
Where(query.Where{
First: query.Where{
First: "id",
Operator: "=",
Second: "1",
},
Operator: "",
Second: query.Where{
First: "id",
Operator: "=",
Second: "2",
Type: query.WhereOrType, //For OR condition, you can use the Type attribute of Where object
},
}).
Where(query.Where{
First: "id",
Operator: "=",
Second: "3",
Type: query.WhereOrType, //For OR condition, you can use the Type attribute of Where object
})
_, err = client.Execute(q)
if err != nil {
fmt.Println(err)
return err
}
return nil
}
func triggerUpdate() error {
//We update the table
model.AddModelField(dto.ModelField{
Name: "test_field2",
Value: "test test test",
})
q := new(clients.Query).Update(model).Where(query.Where{
First: model.GetPrimaryKey().Name,
Operator: "=",
Second: query.Bind{
Field: model.GetPrimaryKey().Name,
Value: model.GetPrimaryKey().Value,
},
})
_, err := client.Execute(q)
if err != nil {
fmt.Println(err)
return err
}
return nil
}
func triggerTransactions() error {
q := new(clients.Query).BeginTransaction()
_, err := client.Execute(q)
if err != nil {
return err
}
//We update the table
model.AddModelField(dto.ModelField{
Name: "test_field2",
Value: "another test",
})
q = new(clients.Query).Update(model).Where(query.Where{
First: model.GetPrimaryKey().Name,
Operator: "=",
Second: query.Bind{
Field: model.GetPrimaryKey().Name,
Value: model.GetPrimaryKey().Value,
},
})
_, err = client.Execute(q)
if err != nil {
return err
}
q = new(clients.Query).CommitTransaction()
_, err = client.Execute(q)
if err != nil {
return err
}
//Now we need to trigger rollback scenario
q = new(clients.Query).BeginTransaction()
_, err = client.Execute(q)
if err != nil {
return err
}
//We update the table
model.AddModelField(dto.ModelField{
Name: "test_field2",
Value: "__SHOULD_NOT_BE_UPDATED__",
})
q = new(clients.Query).Update(model).Where(query.Where{
First: model.GetPrimaryKey().Name,
Operator: "=",
Second: query.Bind{
Field: model.GetPrimaryKey().Name,
Value: model.GetPrimaryKey().Value,
},
})
_, err = client.Execute(q)
if err != nil {
return err
}
//Now we need to trigger rollback scenario
q = new(clients.Query).RollbackTransaction()
_, err = client.Execute(q)
if err != nil {
return err
}
//Now we need to trigger rollback scenario
q = new(clients.Query).Select(model.GetColumns()).From(model).Where(query.Where{
First: "test_field2",
Operator: "=",
Second: query.Bind{
Field: "test_field2",
Value: "__SHOULD_NOT_BE_UPDATED__",
},
})
res, err := client.Execute(q)
if err != nil {
return err
}
if len(res.Items()) > 0 {
return errors.New("there should be no items with that value")
}
return nil
}