-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathorm.go
647 lines (576 loc) · 18.7 KB
/
orm.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
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
package orm
import (
"database/sql"
"fmt"
"reflect"
"time"
// Drivers
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
)
var globalConnections = map[string]*connection{}
// Schematic prints all information ORM inferred from your entities in startup, remember to pass
// your entities in Entities when you call SetupConnections if you want their data inferred
// otherwise Schematic does not print correct data since GoLobby ORM also
// incrementally cache your entities metadata and schema.
func Schematic() {
for conn, connObj := range globalConnections {
fmt.Printf("----------------%s---------------\n", conn)
connObj.Schematic()
fmt.Println("-----------------------------------")
}
}
type ConnectionConfig struct {
// Name of your database connection, it's up to you to name them anything
// just remember that having a connection name is mandatory if
// you have multiple connections
Name string
// If you already have an active database connection configured pass it in this value and
// do not pass Driver and DSN fields.
DB *sql.DB
// Which dialect of sql to generate queries for, you don't need it most of the times when you are using
// traditional databases such as mysql, sqlite3, postgres.
Dialect *Dialect
// List of entities that you want to use for this connection, remember that you can ignore this field
// and GoLobby ORM will build our metadata cache incrementally but you will lose schematic
// information that we can provide you and also potentialy validations that we
// can do with the database
Entities []Entity
// Database validations, check if all tables exists and also table schemas contains all necessary columns.
// Check if all infered tables exist in your database
DatabaseValidations bool
}
// SetupConnections declares a new connections for ORM.
func SetupConnections(configs ...ConnectionConfig) error {
for _, c := range configs {
if err := setupConnection(c); err != nil {
return err
}
}
for _, conn := range globalConnections {
if !conn.DatabaseValidations {
continue
}
tables, err := getListOfTables(conn.Dialect.QueryListTables)(conn.DB)
if err != nil {
return err
}
for _, table := range tables {
if conn.DatabaseValidations {
spec, err := getTableSchema(conn.Dialect.QueryTableSchema)(conn.DB, table)
if err != nil {
return err
}
conn.DBSchema[table] = spec
} else {
conn.DBSchema[table] = nil
}
}
// check tables existence
if conn.DatabaseValidations {
err := conn.validateAllTablesArePresent()
if err != nil {
return err
}
}
if conn.DatabaseValidations {
err = conn.validateTablesSchemas()
if err != nil {
return err
}
}
}
return nil
}
func setupConnection(config ConnectionConfig) error {
schemas := map[string]*schema{}
if config.Name == "" {
config.Name = "default"
}
for _, entity := range config.Entities {
s := schemaOfHeavyReflectionStuff(entity)
var configurator EntityConfigurator
entity.ConfigureEntity(&configurator)
schemas[configurator.table] = s
}
s := &connection{
Name: config.Name,
DB: config.DB,
Dialect: config.Dialect,
Schemas: schemas,
DBSchema: make(map[string][]columnSpec),
DatabaseValidations: config.DatabaseValidations,
}
globalConnections[fmt.Sprintf("%s", config.Name)] = s
return nil
}
// Entity defines the interface that each of your structs that
// you want to use as database entities should have,
// it's a simple one and its ConfigureEntity.
type Entity interface {
// ConfigureEntity should be defined for all of your database entities
// and it can define Table, DB and also relations of your Entity.
ConfigureEntity(e *EntityConfigurator)
}
// InsertAll given entities into database based on their ConfigureEntity
// we can find table and also DB name.
func InsertAll(objs ...Entity) error {
if len(objs) == 0 {
return nil
}
s := getSchemaFor(objs[0])
cols := s.Columns(false)
var values [][]interface{}
for _, obj := range objs {
createdAtF := s.createdAt()
if createdAtF != nil {
genericSet(obj, createdAtF.Name, sql.NullTime{Time: time.Now(), Valid: true})
}
updatedAtF := s.updatedAt()
if updatedAtF != nil {
genericSet(obj, updatedAtF.Name, sql.NullTime{Time: time.Now(), Valid: true})
}
values = append(values, genericValuesOf(obj, false))
}
is := insertStmt{
PlaceHolderGenerator: s.getDialect().PlaceHolderGenerator,
Table: s.getTable(),
Columns: cols,
Values: values,
}
q, args := is.ToSql()
_, err := s.getConnection().exec(q, args...)
if err != nil {
return err
}
return nil
}
// Insert given entity into database based on their ConfigureEntity
// we can find table and also DB name.
func Insert(o Entity) error {
s := getSchemaFor(o)
cols := s.Columns(false)
var values [][]interface{}
createdAtF := s.createdAt()
if createdAtF != nil {
genericSet(o, createdAtF.Name, sql.NullTime{Time: time.Now(), Valid: true})
}
updatedAtF := s.updatedAt()
if updatedAtF != nil {
genericSet(o, updatedAtF.Name, sql.NullTime{Time: time.Now(), Valid: true})
}
values = append(values, genericValuesOf(o, false))
is := insertStmt{
PlaceHolderGenerator: s.getDialect().PlaceHolderGenerator,
Table: s.getTable(),
Columns: cols,
Values: values,
}
if s.getDialect().DriverName == "postgres" {
is.Returning = s.pkName()
}
q, args := is.ToSql()
res, err := s.getConnection().exec(q, args...)
if err != nil {
return err
}
id, err := res.LastInsertId()
if err != nil {
return err
}
if s.pkName() != "" {
// intermediate tables usually have no single pk column.
s.setPK(o, id)
}
return nil
}
func isZero(val interface{}) bool {
switch val.(type) {
case int64:
return val.(int64) == 0
case int:
return val.(int) == 0
case string:
return val.(string) == ""
default:
return reflect.ValueOf(val).Elem().IsZero()
}
}
// Save saves given entity, if primary key is set
// we will make an update query and if
// primary key is zero value we will
// insert it.
func Save(obj Entity) error {
if isZero(getSchemaFor(obj).getPK(obj)) {
return Insert(obj)
} else {
return Update(obj)
}
}
// Find finds the Entity you want based on generic type and primary key you passed.
func Find[T Entity](id interface{}) (T, error) {
var q string
out := new(T)
md := getSchemaFor(*out)
q, args, err := NewQueryBuilder[T](md).
SetDialect(md.getDialect()).
Table(md.Table).
Select(md.Columns(true)...).
Where(md.pkName(), id).
ToSql()
if err != nil {
return *out, err
}
err = bind[T](out, q, args)
if err != nil {
return *out, err
}
return *out, nil
}
func toKeyValues(obj Entity, withPK bool) []any {
var tuples []any
vs := genericValuesOf(obj, withPK)
cols := getSchemaFor(obj).Columns(withPK)
for i, col := range cols {
tuples = append(tuples, col, vs[i])
}
return tuples
}
// Update given Entity in database.
func Update(obj Entity) error {
s := getSchemaFor(obj)
q, args, err := NewQueryBuilder[Entity](s).
SetDialect(s.getDialect()).
Set(toKeyValues(obj, false)...).
Where(s.pkName(), genericGetPKValue(obj)).Table(s.Table).ToSql()
if err != nil {
return err
}
_, err = s.getConnection().exec(q, args...)
return err
}
// Delete given Entity from database
func Delete(obj Entity) error {
s := getSchemaFor(obj)
genericSet(obj, "deleted_at", sql.NullTime{Time: time.Now(), Valid: true})
query, args, err := NewQueryBuilder[Entity](s).SetDialect(s.getDialect()).Table(s.Table).Where(s.pkName(), genericGetPKValue(obj)).SetDelete().ToSql()
if err != nil {
return err
}
_, err = s.getConnection().exec(query, args...)
return err
}
func bind[T Entity](output interface{}, q string, args []interface{}) error {
outputMD := getSchemaFor(*new(T))
rows, err := outputMD.getConnection().query(q, args...)
if err != nil {
return err
}
return newBinder(outputMD).bind(rows, output)
}
// HasManyConfig contains all information we need for querying HasMany relationships.
// We can infer both fields if you have them in standard way but you
// can specify them if you want custom ones.
type HasManyConfig struct {
// PropertyTable is table of the property of HasMany relationship,
// consider `Comment` in Post and Comment relationship,
// each Post HasMany Comment, so PropertyTable is
// `comments`.
PropertyTable string
// PropertyForeignKey is the foreign key field name in the property table,
// for example in Post HasMany Comment, if comment has `post_id` field,
// it's the PropertyForeignKey field.
PropertyForeignKey string
}
// HasMany configures a QueryBuilder for a HasMany relationship
// this relationship will be defined for owner argument
// that has many of PROPERTY generic type for example
// HasMany[Comment](&Post{})
// is for Post HasMany Comment relationship.
func HasMany[PROPERTY Entity](owner Entity) *QueryBuilder[PROPERTY] {
outSchema := getSchemaFor(*new(PROPERTY))
q := NewQueryBuilder[PROPERTY](outSchema)
// getting config from our cache
c, ok := getSchemaFor(owner).relations[outSchema.Table].(HasManyConfig)
if !ok {
q.err = fmt.Errorf("wrong config passed for HasMany")
}
s := getSchemaFor(owner)
return q.
SetDialect(s.getDialect()).
Table(c.PropertyTable).
Select(outSchema.Columns(true)...).
Where(c.PropertyForeignKey, genericGetPKValue(owner))
}
// HasOneConfig contains all information we need for a HasOne relationship,
// it's similar to HasManyConfig.
type HasOneConfig struct {
// PropertyTable is table of the property of HasOne relationship,
// consider `HeaderPicture` in Post and HeaderPicture relationship,
// each Post HasOne HeaderPicture, so PropertyTable is
// `header_pictures`.
PropertyTable string
// PropertyForeignKey is the foreign key field name in the property table,
// forexample in Post HasOne HeaderPicture, if header_picture has `post_id` field,
// it's the PropertyForeignKey field.
PropertyForeignKey string
}
// HasOne configures a QueryBuilder for a HasOne relationship
// this relationship will be defined for owner argument
// that has one of PROPERTY generic type for example
// HasOne[HeaderPicture](&Post{})
// is for Post HasOne HeaderPicture relationship.
func HasOne[PROPERTY Entity](owner Entity) *QueryBuilder[PROPERTY] {
property := getSchemaFor(*new(PROPERTY))
q := NewQueryBuilder[PROPERTY](property)
c, ok := getSchemaFor(owner).relations[property.Table].(HasOneConfig)
if !ok {
q.err = fmt.Errorf("wrong config passed for HasOne")
}
// settings default config Values
return q.
SetDialect(property.getDialect()).
Table(c.PropertyTable).
Select(property.Columns(true)...).
Where(c.PropertyForeignKey, genericGetPKValue(owner))
}
// BelongsToConfig contains all information we need for a BelongsTo relationship
// BelongsTo is a relationship between a Comment and it's Post,
// A Comment BelongsTo Post.
type BelongsToConfig struct {
// OwnerTable is the table that contains owner of a BelongsTo
// relationship.
OwnerTable string
// LocalForeignKey is name of the field that links property
// to its owner in BelongsTo relation. for example when
// a Comment BelongsTo Post, LocalForeignKey is
// post_id of Comment.
LocalForeignKey string
// ForeignColumnName is name of the field that LocalForeignKey
// field value will point to it, for example when
// a Comment BelongsTo Post, ForeignColumnName is
// id of Post.
ForeignColumnName string
}
// BelongsTo configures a QueryBuilder for a BelongsTo relationship between
// OWNER type parameter and property argument, so
// property BelongsTo OWNER.
func BelongsTo[OWNER Entity](property Entity) *QueryBuilder[OWNER] {
owner := getSchemaFor(*new(OWNER))
q := NewQueryBuilder[OWNER](owner)
c, ok := getSchemaFor(property).relations[owner.Table].(BelongsToConfig)
if !ok {
q.err = fmt.Errorf("wrong config passed for BelongsTo")
}
ownerIDidx := 0
for idx, field := range owner.fields {
if field.Name == c.LocalForeignKey {
ownerIDidx = idx
}
}
ownerID := genericValuesOf(property, true)[ownerIDidx]
return q.
SetDialect(owner.getDialect()).
Table(c.OwnerTable).Select(owner.Columns(true)...).
Where(c.ForeignColumnName, ownerID)
}
// BelongsToManyConfig contains information that we
// need for creating many to many queries.
type BelongsToManyConfig struct {
// IntermediateTable is the name of the middle table
// in a BelongsToMany (Many to Many) relationship.
// for example when we have Post BelongsToMany
// Category, this table will be post_categories
// table, remember that this field cannot be
// inferred.
IntermediateTable string
// IntermediatePropertyID is the name of the field name
// of property foreign key in intermediate table,
// for example when we have Post BelongsToMany
// Category, in post_categories table, it would
// be post_id.
IntermediatePropertyID string
// IntermediateOwnerID is the name of the field name
// of property foreign key in intermediate table,
// for example when we have Post BelongsToMany
// Category, in post_categories table, it would
// be category_id.
IntermediateOwnerID string
// Table name of the owner in BelongsToMany relation,
// for example in Post BelongsToMany Category
// Owner table is name of Category table
// for example `categories`.
OwnerTable string
// OwnerLookupColumn is name of the field in the owner
// table that is used in query, for example in Post BelongsToMany Category
// Owner lookup field would be Category primary key which is id.
OwnerLookupColumn string
}
// BelongsToMany configures a QueryBuilder for a BelongsToMany relationship
func BelongsToMany[OWNER Entity](property Entity) *QueryBuilder[OWNER] {
out := *new(OWNER)
outSchema := getSchemaFor(out)
q := NewQueryBuilder[OWNER](outSchema)
c, ok := getSchemaFor(property).relations[outSchema.Table].(BelongsToManyConfig)
if !ok {
q.err = fmt.Errorf("wrong config passed for HasMany")
}
return q.
Select(outSchema.Columns(true)...).
Table(outSchema.Table).
WhereIn(c.OwnerLookupColumn, Raw(fmt.Sprintf(`SELECT %s FROM %s WHERE %s = ?`,
c.IntermediatePropertyID,
c.IntermediateTable, c.IntermediateOwnerID), genericGetPKValue(property)))
}
// Add adds `items` to `to` using relations defined between items and to in ConfigureEntity method of `to`.
func Add(to Entity, items ...Entity) error {
if len(items) == 0 {
return nil
}
rels := getSchemaFor(to).relations
tname := getSchemaFor(items[0]).Table
c, ok := rels[tname]
if !ok {
return fmt.Errorf("no config found for given to and item...")
}
switch c.(type) {
case HasManyConfig:
return addProperty(to, items...)
case HasOneConfig:
return addProperty(to, items[0])
case BelongsToManyConfig:
return addM2M(to, items...)
default:
return fmt.Errorf("cannot add for relation: %T", rels[getSchemaFor(items[0]).Table])
}
}
func addM2M(to Entity, items ...Entity) error {
//TODO: Optimize this
rels := getSchemaFor(to).relations
tname := getSchemaFor(items[0]).Table
c := rels[tname].(BelongsToManyConfig)
var values [][]interface{}
ownerPk := genericGetPKValue(to)
for _, item := range items {
pk := genericGetPKValue(item)
if isZero(pk) {
err := Insert(item)
if err != nil {
return err
}
pk = genericGetPKValue(item)
}
values = append(values, []interface{}{ownerPk, pk})
}
i := insertStmt{
PlaceHolderGenerator: getSchemaFor(to).getDialect().PlaceHolderGenerator,
Table: c.IntermediateTable,
Columns: []string{c.IntermediateOwnerID, c.IntermediatePropertyID},
Values: values,
}
q, args := i.ToSql()
_, err := getConnectionFor(items[0]).DB.Exec(q, args...)
if err != nil {
return err
}
return err
}
// addHasMany(Post, comments)
func addProperty(to Entity, items ...Entity) error {
var lastTable string
for _, obj := range items {
s := getSchemaFor(obj)
if lastTable == "" {
lastTable = s.Table
} else {
if lastTable != s.Table {
return fmt.Errorf("cannot batch insert for two different tables: %s and %s", s.Table, lastTable)
}
}
}
i := insertStmt{
PlaceHolderGenerator: getSchemaFor(to).getDialect().PlaceHolderGenerator,
Table: getSchemaFor(items[0]).getTable(),
}
ownerPKIdx := -1
ownerPKName := getSchemaFor(items[0]).relations[getSchemaFor(to).Table].(BelongsToConfig).LocalForeignKey
for idx, col := range getSchemaFor(items[0]).Columns(false) {
if col == ownerPKName {
ownerPKIdx = idx
}
}
ownerPK := genericGetPKValue(to)
if ownerPKIdx != -1 {
cols := getSchemaFor(items[0]).Columns(false)
i.Columns = append(i.Columns, cols...)
// Owner PK is present in the items struct
for _, item := range items {
vals := genericValuesOf(item, false)
if cols[ownerPKIdx] != getSchemaFor(items[0]).relations[getSchemaFor(to).Table].(BelongsToConfig).LocalForeignKey {
return fmt.Errorf("owner pk idx is not correct")
}
vals[ownerPKIdx] = ownerPK
i.Values = append(i.Values, vals)
}
} else {
ownerPKIdx = 0
cols := getSchemaFor(items[0]).Columns(false)
cols = append(cols[:ownerPKIdx+1], cols[ownerPKIdx:]...)
cols[ownerPKIdx] = getSchemaFor(items[0]).relations[getSchemaFor(to).Table].(BelongsToConfig).LocalForeignKey
i.Columns = append(i.Columns, cols...)
for _, item := range items {
vals := genericValuesOf(item, false)
if cols[ownerPKIdx] != getSchemaFor(items[0]).relations[getSchemaFor(to).Table].(BelongsToConfig).LocalForeignKey {
return fmt.Errorf("owner pk idx is not correct")
}
vals = append(vals[:ownerPKIdx+1], vals[ownerPKIdx:]...)
vals[ownerPKIdx] = ownerPK
i.Values = append(i.Values, vals)
}
}
q, args := i.ToSql()
_, err := getConnectionFor(items[0]).DB.Exec(q, args...)
if err != nil {
return err
}
return err
}
// Query creates a new QueryBuilder for given type parameter, sets dialect and table as well.
func Query[E Entity]() *QueryBuilder[E] {
s := getSchemaFor(*new(E))
q := NewQueryBuilder[E](s)
q.SetDialect(s.getDialect()).Table(s.Table)
return q
}
// ExecRaw executes given query string and arguments on given type parameter database connection.
func ExecRaw[E Entity](q string, args ...interface{}) (int64, int64, error) {
e := new(E)
res, err := getSchemaFor(*e).getSQLDB().Exec(q, args...)
if err != nil {
return 0, 0, err
}
id, err := res.LastInsertId()
if err != nil {
return 0, 0, err
}
affected, err := res.RowsAffected()
if err != nil {
return 0, 0, err
}
return id, affected, nil
}
// QueryRaw queries given query string and arguments on given type parameter database connection.
func QueryRaw[OUTPUT Entity](q string, args ...interface{}) ([]OUTPUT, error) {
o := new(OUTPUT)
rows, err := getSchemaFor(*o).getSQLDB().Query(q, args...)
if err != nil {
return nil, err
}
var output []OUTPUT
err = newBinder(getSchemaFor(*o)).bind(rows, &output)
if err != nil {
return nil, err
}
return output, nil
}