forked from ngorm/ngorm
/
ngorm.go
1101 lines (1013 loc) · 29.7 KB
/
ngorm.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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//Package ngorm is a Go Object relation mapper that focus on performance,
//maintainability, modularity, battle testing, extensibility , safety and
//developer friendliness.
//
//
// Installation
//
// You can install with go get
// go get -u github.com/akamajoris/ngorm
//
// Supported databases
//
//At the moment the following databases are supported
// - ql
// - postgresql
//
//
package ngorm
import (
"bytes"
"context"
"database/sql"
"errors"
"fmt"
"reflect"
"strings"
"time"
"github.com/akamajoris/ngorm/builder"
"github.com/akamajoris/ngorm/dialects"
"github.com/akamajoris/ngorm/engine"
"github.com/akamajoris/ngorm/errmsg"
"github.com/akamajoris/ngorm/hooks"
"github.com/akamajoris/ngorm/model"
"github.com/akamajoris/ngorm/regexes"
"github.com/akamajoris/ngorm/scope"
"github.com/akamajoris/ngorm/search"
"github.com/akamajoris/ngorm/util"
)
//Opener is an interface that is used to open up connection to SQL databases.
type Opener interface {
Open(dialect string, args ...interface{}) (model.SQLCommon, dialects.Dialect, error)
}
// DB provide an API for interacting with SQL databases using Go data structures.
type DB struct {
db *model.SQLCommonWrapper
dialect dialects.Dialect
connStr string
ctx context.Context
cancel func()
singularTable bool
structMap *model.SafeStructsMap
e *engine.Engine
err error
now func() time.Time
}
func (db *DB) clone() *DB {
return &DB{
db: db.db,
dialect: db.dialect,
ctx: db.ctx,
cancel: db.cancel,
singularTable: db.singularTable,
structMap: db.structMap,
now: time.Now,
e: db.NewEngine(),
}
}
//Open opens a database connection and returns *DB instance., dialect is the
//name of the driver that you want to use. The underlying connections are
//handled by database/sql package. Arguments that are accepted by database/sql
//Open function are valid here.
//
// Not all databases are supported. There is still an ongoing efforts to add
// more databases but for now the following are the databases supported by this
// library,
//
// * ql https://github.com/cznic/ql
//
// The drivers for the libraries must be imported inside your application in the
// same package as you invoke this function.
//
// Example
//
// import _ "github.com/cznic/ql/driver" // imports ql driver
func Open(dialect string, args ...interface{}) (*DB, error) {
return OpenWithOpener(dialects.Opener(), dialect, args...)
}
// OpenWithOpener uses the opener to initialize the dialects and establish
// database connection. In fact Open does not do anything by itself, it just
// calls this function with the default Opener.
//
// Please see Open function for details. The only difference is here you need to
// pass an Opener. See the Opener interface for details about what the Opener is
// and what it is used for.
func OpenWithOpener(opener Opener, dialect string, args ...interface{}) (*DB, error) {
db, dia, err := opener.Open(dialect, args...)
if err != nil {
return nil, err
}
dia.SetDB(db)
ctx, cancel := context.WithCancel(context.Background())
return &DB{
db: &model.SQLCommonWrapper{SQLCommon: db},
dialect: dia,
structMap: model.NewStructsMap(),
ctx: ctx,
cancel: cancel,
}, nil
}
// NewEngine returns an initialized engine ready to kick some ass.
func (db *DB) NewEngine() *engine.Engine {
e := engine.Get()
e.StructMap = db.structMap
e.SingularTable = db.singularTable
e.Ctx = db.ctx
e.Dialect = db.dialect
e.SQLDB = db.db
e.Now = db.now
return e
}
//CreateTable creates new database tables that maps to the models.
func (db *DB) CreateTable(models ...interface{}) (sql.Result, error) {
query, err := db.CreateTableSQL(models...)
if err != nil {
return nil, err
}
if isQL(db) {
return db.ExecTx(query.Q, query.Args...)
}
return db.SQLCommon().Exec(query.Q, query.Args...)
}
// Verbose prints what is executed on stdout.
//
//DOn't set this to true when in production. It is dog slow, and a security
//risk. Use this only in development
func (db *DB) Verbose(b bool) {
db.db.Verbose(b)
}
//ExecTx wraps the query execution in a Transaction. This ensure all operations
//are Rolled back in case the execution fails.
func (db *DB) ExecTx(query string, args ...interface{}) (sql.Result, error) {
tx, err := db.db.Begin()
if err != nil {
return nil, err
}
r, err := tx.Exec(query, args...)
if err != nil {
_ = tx.Rollback()
return nil, err
}
err = tx.Commit()
if err != nil {
return nil, err
}
return r, nil
}
//CreateTableSQL return the sql query for creating tables for all the given
//models. The queries are wrapped in a TRANSACTION block.
func (db *DB) CreateTableSQL(models ...interface{}) (*model.Expr, error) {
var scopeVars map[string]interface{}
if db.e != nil {
scopeVars = db.e.Scope.GetAll()
}
var buf bytes.Buffer
if isQL(db) {
_, _ = buf.WriteString("BEGIN TRANSACTION; \n")
}
for _, m := range models {
e := db.NewEngine()
defer engine.Put(e)
for k, v := range scopeVars {
e.Scope.Set(k, v)
}
// Firste we generate the SQL
err := scope.CreateTable(e, m)
if err != nil {
return nil, err
}
_, _ = buf.WriteString("\t" + e.Scope.SQL + ";\n")
if e.Scope.MultiExpr {
for _, expr := range e.Scope.Exprs {
_, _ = buf.WriteString("\t" + expr.Q + ";\n")
}
}
}
if isQL(db) {
_, _ = buf.WriteString("COMMIT;")
}
return &model.Expr{Q: buf.String()}, nil
}
func isQL(db *DB) bool {
return dialects.IsQL(db.Dialect())
}
//DropTableSQL generates sql query for DROP TABLE. The generated query is
//wrapped under TRANSACTION block.
func (db *DB) DropTableSQL(models ...interface{}) (*model.Expr, error) {
var buf bytes.Buffer
if isQL(db) {
_, _ = buf.WriteString("BEGIN TRANSACTION; \n")
}
for _, m := range models {
e := db.NewEngine()
defer engine.Put(e)
if n, ok := m.(string); ok {
e.Search.TableName = n
}
// Firste we generate the SQL
err := scope.DropTable(e, m)
if err != nil {
return nil, err
}
_, _ = buf.WriteString("\t" + e.Scope.SQL + ";\n")
}
if isQL(db) {
_, _ = buf.WriteString("COMMIT;")
}
return &model.Expr{Q: buf.String()}, nil
}
//DropTable drops tables that are mapped to models. You can also pass the name
//of the table as astring and it will be handled.
func (db *DB) DropTable(models ...interface{}) (sql.Result, error) {
query, err := db.DropTableSQL(models...)
if err != nil {
return nil, err
}
if isQL(db) {
return db.ExecTx(query.Q, query.Args...)
}
return db.SQLCommon().Exec(query.Q, query.Args...)
}
//Automigrate creates tables that map to models if the tables don't exist yet in
//the database. This also takes care of situation where the models's fields have
//been updated(changed)
func (db *DB) Automigrate(models ...interface{}) (sql.Result, error) {
query, err := db.AutomigrateSQL(models...)
if err != nil {
return nil, err
}
if isQL(db) {
return db.ExecTx(query.Q, query.Args...)
}
return db.SQLCommon().Exec(query.Q, query.Args...)
}
//AutomigrateSQL generates sql query for running migrations on models.
func (db *DB) AutomigrateSQL(models ...interface{}) (*model.Expr, error) {
// var buf bytes.Buffer
buf := util.B.Get()
defer func() {
util.B.Put(buf)
}()
if isQL(db) {
buf.WriteString("BEGIN TRANSACTION;\n")
}
keys := make(map[string]bool)
for _, m := range models {
e := db.NewEngine()
defer engine.Put(e)
// Firste we generate the SQL
err := scope.Automigrate(e, m)
if err != nil {
return nil, err
}
if e.Scope.SQL != "" {
i := strings.Index(e.Scope.SQL, "(")
k := e.Scope.SQL[:i]
if _, ok := keys[k]; !ok {
buf.WriteString("\t" + e.Scope.SQL + ";\n")
keys[k] = true
}
}
if e.Scope.MultiExpr {
for _, expr := range e.Scope.Exprs {
k := expr.Q
i := strings.Index(expr.Q, "(")
if i > 0 {
k = expr.Q[:i]
}
if _, ok := keys[k]; !ok {
buf.WriteString("\t" + expr.Q + ";\n")
keys[k] = true
}
}
}
}
if isQL(db) {
buf.WriteString("COMMIT;")
}
return &model.Expr{Q: buf.String()}, nil
}
//Close closes the database connection and sends Done signal across all
//goroutines that subscribed to this instance context.
func (db *DB) Close() error {
db.cancel()
return db.db.Close()
}
//Create creates a new record.
//
// You can hijack the execution of the generated SQL by overriding
// model.HookCreateExec hook.
func (db *DB) Create(value interface{}) error {
e := db.NewEngine()
defer engine.Put(e)
e.Scope.ContextValue(value)
return hooks.Create(e)
}
//CreateSQL generates SQl query for creating a new record/records for value.
// The end query is wrapped under for ql dialectTRANSACTION block.
func (db *DB) CreateSQL(value interface{}) (*model.Expr, error) {
var e *engine.Engine
if db.e != nil {
e = db.e
} else {
e = db.NewEngine()
}
defer engine.Put(e)
e.Scope.ContextValue(value)
err := hooks.CreateSQL(e)
if err != nil {
return nil, err
}
return &model.Expr{Q: e.Scope.SQL, Args: e.Scope.SQLVars}, nil
}
//Dialect return the dialect that is used by DB
func (db *DB) Dialect() dialects.Dialect {
return db.dialect
}
//SQLCommon return SQLCommon used by the DB
func (db *DB) SQLCommon() model.SQLCommon {
return db.db
}
//SaveSQL generates SQL query for saving/updating database record for value.
func (db *DB) SaveSQL(value interface{}) (*model.Expr, error) {
e := db.NewEngine()
defer engine.Put(e)
e.Scope.ContextValue(value)
err := hooks.UpdateSQL(e)
if err != nil {
return nil, err
}
return &model.Expr{Q: e.Scope.SQL, Args: e.Scope.SQLVars}, nil
}
// Save update value in database, if the value doesn't have primary key, will insert it
func (db *DB) Save(value interface{}) error {
e := db.NewEngine()
defer engine.Put(e)
e.Scope.ContextValue(value)
field, _ := scope.PrimaryField(e, value)
if field == nil || field.IsBlank {
return db.Create(value)
}
return hooks.Update(e)
}
//Model sets value as the database model. This model will be used for future
//calls on the returned DB e.g
//
// db.Model(&user).Update("name","hero")
//
// You don't have to call db.Begin().Model() since this calls Begin automatically for you.
// It is safe for chaining.
func (db *DB) Model(value interface{}) *DB {
c := db.clone()
c.e.Scope.ContextValue(value)
return c
}
//Update runs UPDATE queries.
func (db *DB) Update(attrs ...interface{}) error {
return db.Updates(util.ToSearchableMap(attrs), true)
}
//Updates runs UPDATE query
func (db *DB) Updates(values interface{}, ignoreProtectedAttrs ...bool) error {
if db.e == nil || db.e.Scope.Value == nil {
return errmsg.ErrMissingModel
}
defer db.recycle()
var ignore bool
if len(ignoreProtectedAttrs) > 0 {
ignore = ignoreProtectedAttrs[0]
}
db.e.Scope.Set(model.IgnoreProtectedAttrs, ignore)
db.e.Scope.Set(model.UpdateInterface, values)
return hooks.Update(db.e)
}
//UpdateSQL generates SQL that will be executed when you use db.Update
func (db *DB) UpdateSQL(attrs ...interface{}) (*model.Expr, error) {
return db.UpdatesSQL(util.ToSearchableMap(attrs), true)
}
//UpdatesSQL generates sql that will be used when you run db.UpdatesSQL
func (db *DB) UpdatesSQL(values interface{}, ignoreProtectedAttrs ...bool) (*model.Expr, error) {
if db.e == nil || db.e.Scope.Value == nil {
return nil, errmsg.ErrMissingModel
}
defer db.recycle()
var ignore bool
if len(ignoreProtectedAttrs) > 0 {
ignore = ignoreProtectedAttrs[0]
}
db.e.Scope.Set(model.IgnoreProtectedAttrs, ignore)
db.e.Scope.Set(model.UpdateInterface, values)
err := hooks.UpdateSQL(db.e)
if err != nil {
return nil, err
}
return &model.Expr{Q: db.e.Scope.SQL, Args: db.e.Scope.SQLVars}, nil
}
//Set sets scope key to value.
func (db *DB) Set(key string, value interface{}) *DB {
if db.e == nil {
db.e = db.NewEngine()
}
db.e.Scope.Set(key, value)
return db
}
//SingularTable enables or disables singular tables name. By default this is
//disabled, meaning table names are in plural.
//
// Model | Plural table name
// ----------------------------
// Session | sessions
// User | users
//
// Model | Singular table name
// ----------------------------
// Session | session
// User | user
func (db *DB) SingularTable(enable bool) {
db.singularTable = enable
if db.e != nil {
db.e.SingularTable = enable
}
}
//HasTable returns true if there is a table for the given value, the value can
//either be a string representing a table name or a ngorm model.
func (db *DB) HasTable(value interface{}) bool {
var name string
if n, ok := value.(string); ok {
name = n
} else {
e := db.NewEngine()
name = scope.TableName(e, value)
engine.Put(e)
}
return db.Dialect().HasTable(name)
}
//First fetches the first record and order by primary key.
func (db *DB) First(out interface{}, where ...interface{}) error {
db.Set(model.OrderByPK, "ASC")
defer db.recycle()
search.Inline(db.e, where...)
search.Limit(db.e, 1)
db.e.Scope.ContextValue(out)
return hooks.Query(db.e)
}
//FirstSQL returns SQL query for retrieving the first record ordering by primary
//key.
func (db *DB) FirstSQL(out interface{}, where ...interface{}) (*model.Expr, error) {
db.Set(model.OrderByPK, "ASC")
search.Inline(db.e, where...)
search.Limit(db.e, 1)
db.e.Scope.ContextValue(out)
err := hooks.QuerySQL(db.e)
if err != nil {
return nil, err
}
return &model.Expr{Q: db.e.Scope.SQL, Args: db.e.Scope.SQLVars}, nil
}
//Last finds the last record and order by primary key.
func (db *DB) Last(out interface{}, where ...interface{}) error {
db.Set(model.OrderByPK, "DESC")
search.Inline(db.e, where...)
search.Limit(db.e, 1)
db.e.Scope.ContextValue(out)
return hooks.Query(db.e)
}
//LastSQL returns SQL query for retrieving the last record ordering by primary
//key.
func (db *DB) LastSQL(out interface{}, where ...interface{}) (*model.Expr, error) {
db.Set(model.OrderByPK, "DESC")
search.Inline(db.e, where...)
search.Limit(db.e, 1)
db.e.Scope.ContextValue(out)
err := hooks.QuerySQL(db.e)
if err != nil {
return nil, err
}
return &model.Expr{Q: db.e.Scope.SQL, Args: db.e.Scope.SQLVars}, nil
}
// Limit specify the number of records to be retrieved
func (db *DB) Limit(limit interface{}) *DB {
if db.e == nil {
db.e = db.NewEngine()
}
search.Limit(db.e, limit)
return db
}
// FindSQL generates SQL query for finding records that match given conditions
func (db *DB) FindSQL(out interface{}, where ...interface{}) (*model.Expr, error) {
if db.e == nil {
db.e = db.NewEngine()
}
defer db.recycle()
search.Inline(db.e, where...)
db.e.Scope.ContextValue(out)
err := hooks.QuerySQL(db.e)
if err != nil {
return nil, err
}
return &model.Expr{Q: db.e.Scope.SQL, Args: db.e.Scope.SQLVars}, nil
}
// Find find records that match given conditions
func (db *DB) Find(out interface{}, where ...interface{}) error {
if db.e == nil {
db.e = db.NewEngine()
}
defer db.recycle()
search.Inline(db.e, where...)
db.e.Scope.ContextValue(out)
return hooks.Query(db.e)
}
// Attrs initialize struct with argument if record not found
func (db *DB) Attrs(attrs ...interface{}) *DB {
if db.e == nil {
db.e = db.NewEngine()
}
search.Attr(db.e, attrs...)
return db
}
// Assign assign result with argument regardless it is found or not
func (db *DB) Assign(attrs ...interface{}) *DB {
if db.e == nil {
db.e = db.NewEngine()
}
search.Assign(db.e, attrs...)
return db
}
// Group specify the group method on the find
func (db *DB) Group(query string) *DB {
if db.e == nil {
db.e = db.NewEngine()
}
_ = search.Group(db.e, query)
return db
}
// Having specify HAVING conditions for GROUP BY
func (db *DB) Having(query string, values ...interface{}) *DB {
if db.e == nil {
db.e = db.NewEngine()
}
search.Having(db.e, query, values...)
return db
}
// Joins specify Joins conditions
func (db *DB) Joins(query string, args ...interface{}) *DB {
if db.e == nil {
db.e = db.NewEngine()
}
search.Join(db.e, query, args...)
return db
}
// Offset specify the number of records to skip before starting to return the records
func (db *DB) Offset(offset interface{}) *DB {
if db.e == nil {
db.e = db.NewEngine()
}
search.Offset(db.e, offset)
return db
}
// Order specify order when retrieve records from database, set reorder to
// `true` to overwrite defined conditions
// db.Order("name DESC")
// db.Order("name DESC", true) // reorder
func (db *DB) Order(value interface{}, reorder ...bool) *DB {
if db.e == nil {
db.e = db.NewEngine()
}
search.Order(db.e, value, reorder...)
return db
}
// Select specify fields that you want to retrieve from database when querying,
// by default, will select all fields; When creating/updating, specify fields
// that you want to save to database
func (db *DB) Select(query interface{}, args ...interface{}) *DB {
if db.e == nil {
db.e = db.NewEngine()
}
search.Select(db.e, query, args...)
return db
}
// Omit specify fields that you want to ignore when saving to database for
// creating, updating
func (db *DB) Omit(columns ...string) *DB {
if db.e == nil {
db.e = db.NewEngine()
}
search.Omit(db.e, columns...)
return db
}
// Not filter records that don't match current conditions, similar to `Where`
func (db *DB) Not(query interface{}, args ...interface{}) *DB {
if db.e == nil {
db.e = db.NewEngine()
}
search.Not(db.e, query, args...)
return db
}
// Or filter records that match before conditions or this one, similar to `Where`
func (db *DB) Or(query interface{}, args ...interface{}) *DB {
if db.e == nil {
db.e = db.NewEngine()
}
search.Or(db.e, query, args...)
return db
}
// Where return a new relation, filter records with given conditions, accepts
//`map`, `struct` or `string` as conditions
func (db *DB) Where(query interface{}, args ...interface{}) *DB {
if db.e == nil {
db.e = db.NewEngine()
}
search.Where(db.e, query, args...)
return db
}
// FirstOrInit find first matched record or initialize a new one with given
//conditions (only works with struct, map conditions)
func (db *DB) FirstOrInit(out interface{}, where ...interface{}) error {
if db.e == nil {
db.e = db.NewEngine()
}
defer db.recycle()
db.e.Scope.ContextValue(out)
err := db.Begin().First(out, where...)
if err != nil {
if err != errmsg.ErrRecordNotFound {
return err
}
search.Inline(db.e, where...)
scope.Initialize(db.e)
return nil
}
_, _ = scope.UpdatedAttrsWithValues(db.e, db.e.Search.AssignAttrs)
return nil
}
// Begin gives back a fresh copy of DB ready for chaining methods that operates
// on the same model..
func (db *DB) Begin() *DB {
return db.clone()
}
func (db *DB) recycle() {
engine.Put(db.e)
db.e = nil
}
// Table specify the table you would like to run db operations
func (db *DB) Table(name string) *DB {
ndb := db.Begin()
search.Table(ndb.e, name)
return ndb
}
// Pluck used to query single column from a model as a map
// var ages []int64
// db.Find(&users).Pluck("age", &ages)
func (db *DB) Pluck(column string, value interface{}) error {
dest := reflect.ValueOf(value)
if dest.Kind() == reflect.Ptr {
dest = dest.Elem()
}
defer db.recycle()
search.Select(db.e, column)
if dest.Kind() != reflect.Slice {
return fmt.Errorf("results should be a slice, not %s", dest.Kind())
}
err := builder.PrepareQuery(db.e, db.e.Scope.Value)
if err != nil {
return err
}
rows, err := db.SQLCommon().Query(db.e.Scope.SQL, db.e.Scope.SQLVars...)
if err != nil {
return err
}
defer func() { _ = rows.Close() }()
for rows.Next() {
elem := reflect.New(dest.Type().Elem()).Interface()
err := rows.Scan(elem)
if err != nil {
return err
}
dest.Set(reflect.Append(dest, reflect.ValueOf(elem).Elem()))
}
return nil
}
// Count get how many records for a model
func (db *DB) Count(value interface{}) error {
query, ok := db.e.Search.Selects["query"]
if !ok || regexes.CountingQuery.MatchString(fmt.Sprint(query)) {
search.Select(db.e, "count(*)")
}
defer db.recycle()
db.e.Search.IgnoreOrderQuery = true
err := builder.PrepareQuery(db.e, db.e.Scope.Value)
if err != nil {
return err
}
return db.SQLCommon().QueryRow(db.e.Scope.SQL, db.e.Scope.SQLVars...).Scan(value)
}
// AddIndexSQL generates SQL to add index for columns with given name
func (db *DB) AddIndexSQL(indexName string, columns ...string) (*model.Expr, error) {
if db.e == nil || db.e.Scope.Value == nil {
return nil, errmsg.ErrMissingModel
}
err := builder.AddIndex(db.e, false, indexName, columns...)
if err != nil {
return nil, err
}
return &model.Expr{Q: db.e.Scope.SQL, Args: db.e.Scope.SQLVars}, nil
}
// AddIndex add index for columns with given name
func (db *DB) AddIndex(indexName string, columns ...string) (sql.Result, error) {
sql, err := db.AddIndexSQL(indexName, columns...)
if err != nil {
return nil, err
}
if isQL(db) {
return db.ExecTx(util.WrapTX(sql.Q), sql.Args...)
}
return db.SQLCommon().Exec(sql.Q, sql.Args...)
}
// DropTableIfExists drop table if it is exist
func (db *DB) DropTableIfExists(values ...interface{}) error {
for _, value := range values {
if db.HasTable(value) {
_, err := db.Begin().DropTable(value)
if err != nil {
return err
}
}
}
return nil
}
// Delete delete value match given conditions, if the value has primary key,
//then will including the primary key as condition
func (db *DB) Delete(value interface{}, where ...interface{}) error {
e := db.NewEngine()
defer engine.Put(e)
e.Scope.ContextValue(value)
search.Inline(e, where...)
return hooks.Delete(e)
}
// DeleteSQL generates SQL to delete value match given conditions, if the value has primary key,
//then will including the primary key as condition
func (db *DB) DeleteSQL(value interface{}, where ...interface{}) (*model.Expr, error) {
e := db.NewEngine()
defer engine.Put(e)
e.Scope.ContextValue(value)
search.Inline(e, where...)
err := hooks.DeleteSQL(e)
if err != nil {
return nil, err
}
return &model.Expr{Q: e.Scope.SQL, Args: e.Scope.SQLVars}, nil
}
// UpdateColumn update attributes without callbacks
func (db *DB) UpdateColumn(attrs ...interface{}) error {
return db.UpdateColumns(util.ToSearchableMap(attrs...))
}
// UpdateColumns update attributes without
func (db *DB) UpdateColumns(values interface{}) error {
if db.e == nil || db.e.Scope.Value == nil {
return errmsg.ErrMissingModel
}
defer db.recycle()
db.e.Scope.Set(model.UpdateColumn, true)
db.e.Scope.Set(model.SaveAssociations, false)
db.e.Scope.Set(model.UpdateInterface, values)
return hooks.Update(db.e)
}
// AddUniqueIndex add unique index for columns with given name
func (db *DB) AddUniqueIndex(indexName string, columns ...string) (sql.Result, error) {
if db.e == nil || db.e.Scope.Value == nil {
return nil, errmsg.ErrMissingModel
}
err := builder.AddIndex(db.e, true, indexName, columns...)
if err != nil {
return nil, err
}
if isQL(db) {
return db.ExecTx(util.WrapTX(db.e.Scope.SQL), db.e.Scope.SQLVars...)
}
return db.SQLCommon().Exec(db.e.Scope.SQL, db.e.Scope.SQLVars...)
}
// RemoveIndex remove index with name
func (db *DB) RemoveIndex(indexName string) error {
if db.e == nil || db.e.Scope.Value == nil {
return errmsg.ErrMissingModel
}
defer db.recycle()
return db.Dialect().RemoveIndex(
scope.TableName(db.e, db.e.Scope.Value), indexName)
}
// DropColumn drop a column
func (db *DB) DropColumn(column string) (sql.Result, error) {
if db.e == nil || db.e.Scope.Value == nil {
return nil, errmsg.ErrMissingModel
}
defer db.recycle()
db.e.Scope.SQL = fmt.Sprintf("ALTER TABLE %v DROP COLUMN %v",
scope.QuotedTableName(db.e, db.e.Scope.Value), scope.Quote(db.e, column))
if isQL(db) {
return db.ExecTx(
util.WrapTX(db.e.Scope.SQL), db.e.Scope.SQLVars...,
)
}
return db.SQLCommon().Exec(db.e.Scope.SQL, db.e.Scope.SQLVars...)
}
// ModifyColumn modify column to type
func (db *DB) ModifyColumn(column string, typ string) (sql.Result, error) {
if db.e == nil || db.e.Scope.Value == nil {
return nil, errmsg.ErrMissingModel
}
defer db.recycle()
if isQL(db) {
return nil, errors.New("ngorm: ql does to support MODIFY column")
}
db.e.Scope.SQL = fmt.Sprintf("ALTER TABLE %v MODIFY %v %v",
scope.QuotedTableName(db.e, db.e.Scope.Value), scope.Quote(db.e, column), typ)
return db.ExecTx(
util.WrapTX(db.e.Scope.SQL), db.e.Scope.SQLVars...,
)
}
// Ping checks if you can connect to the database
func (db *DB) Ping() error {
if dr, ok := db.db.SQLCommon.(*sql.DB); ok {
return dr.Ping()
}
return errors.New("ngorm: ping not supported")
}
// Preload preload associations with given conditions
// db.Preload("Orders", "state NOT IN (?)", "cancelled").Find(&users)
func (db *DB) Preload(column string, conditions ...interface{}) *DB {
if db.e == nil {
db.e = db.NewEngine()
}
search.Preload(db.e, column, conditions...)
return db
}
// FirstOrCreate find first matched record or create a new one with given
//conditions (only works with struct, map conditions)
func (db *DB) FirstOrCreate(out interface{}, where ...interface{}) error {
if db.e == nil {
db.e = db.NewEngine()
}
defer db.recycle()
db.e.Scope.ContextValue(out)
err := db.Begin().First(out, where...)
if err != nil {
if err != errmsg.ErrRecordNotFound {
return err
}
// re use the existing engine
db.e.Scope.SQLVars = nil
db.e.Scope.SQL = ""
search.Inline(db.e, where...)
scope.Initialize(db.e)
return db.Create(out)
}
if len(db.e.Search.AssignAttrs) > 0 {
return db.Update(db.e.Search.AssignAttrs...)
}
return nil
}
// AddForeignKey adds foreign key to an existing table.
func (db *DB) AddForeignKey(field string, dest string, onDelete string, onUpdate string) error {
sql, err := db.AddForeignKeySQL(field, dest, onDelete, onUpdate)
if err != nil {
return err
}
_, err = db.SQLCommon().Exec(sql)
if err != nil {
return fmt.Errorf("%v \n %s", err, sql)
}
return nil
}
// AddForeignKeySQL generates sql to adds foreign key to an existing table.
func (db *DB) AddForeignKeySQL(field string, dest string, onDelete string, onUpdate string) (string, error) {
if db.e == nil || db.e.Scope.Value == nil {
return "", errmsg.ErrMissingModel
}
defer db.recycle()
if isQL(db) {
return "", errors.New("ql does not support foreign key")
}
name := scope.TableName(db.e, db.e.Scope.Value)
keyName := db.Dialect().BuildForeignKeyName(
name, field, dest)
if db.Dialect().HasForeignKey(name, keyName) {
return "", errors.New("key already exists")
}
var query = `ALTER TABLE %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s ON DELETE %s ON UPDATE %s;`
sql := fmt.Sprintf(query,
scope.QuotedTableName(db.e, db.e.Scope.Value),
scope.Quote(db.e, keyName),
scope.Quote(db.e, field), dest, onDelete, onUpdate)
return sql, nil
}
// Association returns association object
func (db *DB) Association(column string) (*Association, error) {
if db.e == nil || db.e.Scope.Value == nil {
return nil, errmsg.ErrMissingModel
}
p, err := scope.PrimaryField(db.e, db.e.Scope.Value)
if err != nil {
return nil, err
}
if p.IsBlank {
return nil, errors.New("primary field can not be blank")
}
field, err := scope.FieldByName(db.e, db.e.Scope.Value, column)
if err != nil {
return nil, err
}
ndb := db.Begin()
ndb.e.Scope.ContextValue(db.e.Scope.Value)
ndb.e.Scope.Set(model.AssociationSource, db.e.Scope.Value)
if field.Relationship == nil || len(field.Relationship.ForeignFieldNames) == 0 {