/
hub.go
824 lines (709 loc) · 20.4 KB
/
hub.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
package datahub
import (
"errors"
"fmt"
"sync"
"time"
"git.kanosolution.net/kano/dbflex"
"git.kanosolution.net/kano/dbflex/orm"
"github.com/sebarcode/codekit"
"github.com/sebarcode/logger"
)
// Hub main datahub object. This object need to be initiated to work with datahub
type Hub struct {
connFn func() (dbflex.IConnection, error)
usePool bool
pool *dbflex.DbPooling
poolSize int
poolItems []*dbflex.PoolItem
mtx *sync.Mutex
_log *logger.LogEngine
txconn dbflex.IConnection
}
func GeneralDbConnBuilder(txt string) func() (dbflex.IConnection, error) {
return GeneralDbConnBuilderWithTx(txt, false)
}
func GeneralDbConnBuilderWithTx(txt string, useTx bool) func() (dbflex.IConnection, error) {
return func() (dbflex.IConnection, error) {
conn, e := dbflex.NewConnectionFromURI(txt, nil)
if e != nil {
return nil, e
}
if !useTx {
conn.DisableTx(!useTx)
}
if e = conn.Connect(); e != nil {
return nil, fmt.Errorf("fail to connect to datahub. %s", e.Error())
}
return conn, nil
}
}
type HubOptions struct {
AutoClose time.Duration
AutoRelease time.Duration
Timeout time.Duration
UsePool bool
PoolSize int
}
// NewHub function to create new hub
func NewHub(fn func() (dbflex.IConnection, error), usePool bool, poolsize int) *Hub {
h := new(Hub)
h.connFn = fn
h.usePool = usePool
h.poolSize = poolsize
if h.usePool {
h.pool = dbflex.NewDbPooling(h.poolSize, h.connFn).SetLog(h.Log())
h.pool.Timeout = 7 * time.Second
h.pool.AutoClose = 5 * time.Second
//h.pool.AutoRelease = 3 * time.Second
}
return h
}
// NewHub function to create new hub
func NewHubWithOpts(fn func() (dbflex.IConnection, error), opts *HubOptions) *Hub {
if opts == nil {
opts = new(HubOptions)
}
h := new(Hub)
h.connFn = fn
h.SetOptions(opts)
return h
}
// Set Hub Opts
func (h *Hub) SetOptions(opts *HubOptions) *Hub {
h.usePool = opts.UsePool
h.poolSize = opts.PoolSize
if h.usePool {
h.pool = dbflex.NewDbPooling(h.poolSize, h.connFn).SetLog(h.Log())
h.pool.Timeout = opts.Timeout
h.pool.AutoClose = opts.AutoClose
h.pool.AutoRelease = opts.AutoRelease
}
return h
}
// Log get logger object
func (h *Hub) Log() *logger.LogEngine {
if h._log == nil {
h._log = logger.NewLogEngine(true, false, "", "", "")
}
return h._log
}
// SetLog set logger
func (h *Hub) SetLog(l *logger.LogEngine) *Hub {
h._log = l
if h.pool != nil {
h.pool.SetLog(l)
}
return h
}
// GetConnection to generate connection. It will return index, connection and error. Index and connection need
// to be retain for purpose of closing the connection. It is advised to use other DB operation related of Hub rather
// than build manual connection
func (h *Hub) GetConnection() (string, dbflex.IConnection, error) {
return h.getConn()
}
// CloseConnection to close connection
func (h *Hub) CloseConnection(idx string, conn dbflex.IConnection) {
h.closeConn(idx, conn)
}
// GetClassicConnection get connection without using pool. CleanUp operation need to be done manually
func (h *Hub) GetClassicConnection() (dbflex.IConnection, error) {
return h.connFn()
}
func (h *Hub) getConnFromPool() (string, dbflex.IConnection, error) {
if h.txconn != nil {
return "", h.txconn, nil
}
if h.poolSize == 0 {
h.poolSize = 100
}
if h.mtx == nil {
h.mtx = new(sync.Mutex)
}
if h.pool == nil {
h.pool = dbflex.NewDbPooling(h.poolSize, h.connFn).SetLog(h.Log())
h.pool.Timeout = 90 * time.Second
h.pool.AutoClose = 5 * time.Second
//h.pool.AutoRelease = 3 * time.Second
}
it, err := h.pool.Get()
if err != nil {
return "", nil, fmt.Errorf("unable get connection from pool. %s", err.Error())
}
conn := it.Connection()
idx := ""
h.mtx.Lock()
defer h.mtx.Unlock()
h.poolItems = append(h.poolItems, it)
idx = it.ID
return idx, conn, nil
}
// SetAutoCloseDuration set duration for a connection inside Hub Pool to be closed if it is not being used
func (h *Hub) SetAutoCloseDuration(d time.Duration) *Hub {
if h.usePool {
if h.pool == nil {
h.pool = dbflex.NewDbPooling(h.poolSize, h.connFn)
}
h.pool.AutoClose = d
}
return h
}
// SetAutoReleaseDuration set duration for a connection in pool to be released for a process
func (h *Hub) SetAutoReleaseDuration(d time.Duration) *Hub {
if h.usePool {
if h.pool == nil {
h.pool = dbflex.NewDbPooling(h.poolSize, h.connFn)
}
h.pool.Timeout = d + time.Duration(5*time.Second)
h.pool.AutoRelease = d
}
return h
}
func (h *Hub) closeConn(idx string, conn dbflex.IConnection) {
if h.txconn != nil {
return
}
if !h.usePool {
conn.Close()
}
if h.mtx == nil {
h.mtx = new(sync.Mutex)
}
h.mtx.Lock()
defer h.mtx.Unlock()
for _, it := range h.poolItems {
if it.ID == idx {
it.Release()
break
}
}
}
func (h *Hub) getConn() (string, dbflex.IConnection, error) {
if h.txconn != nil {
return "", h.txconn, nil
}
if h.connFn == nil {
return "", nil, fmt.Errorf("connection fn is not yet defined")
}
if h.usePool {
return h.getConnFromPool()
}
conn, err := h.connFn()
if err != nil {
return "", nil, fmt.Errorf("unable to open connection. %s", err.Error())
}
return "", conn, nil
}
// UsePool is a hub using pool
func (h *Hub) UsePool() bool {
return h.usePool
}
// PoolSize returns size of the pool
func (h *Hub) PoolSize() int {
return h.poolSize
}
// DeleteQuery delete object in database based on specific model and filter, will be DEPRECATED use DeleteByFilter instead
func (h *Hub) DeleteQuery(model orm.DataModel, where *dbflex.Filter) error {
return h.DeleteByFilter(model, where)
}
// DeleteByFilter delete object in database based on specific model and filter
func (h *Hub) DeleteByFilter(model orm.DataModel, where *dbflex.Filter) error {
idx, conn, err := h.getConn()
if err != nil {
return fmt.Errorf("connection error. %s", err.Error())
}
defer h.closeConn(idx, conn)
cmd := dbflex.From(model.TableName()).Delete()
if where != nil {
cmd.Where(where)
}
_, err = conn.Execute(cmd, nil)
return err
}
// Save will save data into database, if fields is activated it will save the mentioned fields only,
// fields will only work with update hence it will assume record already exist
func (h *Hub) Save(data orm.DataModel, fields ...string) error {
data.SetThis(data)
idx, conn, err := h.getConn()
if err != nil {
return fmt.Errorf("connection error. %s", err.Error())
}
defer h.closeConn(idx, conn)
if len(fields) == 0 {
if err = orm.Save(conn, data); err != nil {
return err
}
} else {
w := data.GetFilterID(conn)
if err := orm.DataModel(data).PreSave(conn); err != nil {
return err
}
cmd := dbflex.From(data.TableName()).Where(w).Update(fields...)
if _, err := conn.Execute(cmd, codekit.M{}.Set("data", data)); err != nil {
return err
}
orm.DataModel(data).PostSave(conn)
}
return nil
}
// Insert will create data into database
func (h *Hub) Insert(data orm.DataModel) error {
data.SetThis(data)
idx, conn, err := h.getConn()
if err != nil {
return fmt.Errorf("connection error. %s", err.Error())
}
defer h.closeConn(idx, conn)
if err = orm.Insert(conn, data); err != nil {
return err
}
return nil
}
// UpdateField update relevant fields in data based on specific filter
func (h *Hub) UpdateField(data orm.DataModel, where *dbflex.Filter, fields ...string) error {
data.SetThis(data)
idx, conn, err := h.getConn()
if err != nil {
return fmt.Errorf("connection error. %s", err.Error())
}
defer h.closeConn(idx, conn)
updatedFields := fields
if err := orm.DataModel(data).PreSave(conn); err != nil {
return err
}
cmd := dbflex.From(data.TableName()).Update(updatedFields...).Where(where)
conn.Execute(cmd, codekit.M{}.Set("data", data))
orm.DataModel(data).PostSave(conn)
return nil
}
// Update will update single data in database based on specific model
func (h *Hub) Update(data orm.DataModel, fields ...string) error {
data.SetThis(data)
idx, conn, err := h.getConn()
if err != nil {
return fmt.Errorf("connection error. %s", err.Error())
}
defer h.closeConn(idx, conn)
if len(fields) == 0 {
if err = orm.Update(conn, data); err != nil {
return err
}
return nil
}
var f *dbflex.Filter
idFields, idValues := data.GetID(conn)
if len(idFields) == 0 {
f = dbflex.Eq(idFields[0], idValues[0])
} else {
fs := make([]*dbflex.Filter, len(idFields))
for idx, name := range idFields {
fs[idx] = dbflex.Eq(name, idValues[idx])
}
f = dbflex.And(fs...)
}
return h.UpdateField(data, f, fields...)
}
// Delete delete the active record on database
func (h *Hub) Delete(data orm.DataModel) error {
data.SetThis(data)
idx, conn, err := h.getConn()
if err != nil {
return fmt.Errorf("connection error. %s", err.Error())
}
defer h.closeConn(idx, conn)
if err = orm.Delete(conn, data); err != nil {
return err
}
return nil
}
// GetByID returns single data based on its ID. Data need to be comply with orm.DataModel
func (h *Hub) GetByID(data orm.DataModel, ids ...interface{}) error {
data.SetThis(data)
data.SetID(ids...)
return h.Get(data)
}
// GetByAttr returns single data based on value on single field. Data need to be comply with orm.DataModel
func (h *Hub) GetByAttr(data orm.DataModel, attr string, value interface{}) error {
w := dbflex.Eq(attr, value)
qp := dbflex.NewQueryParam().SetWhere(w).SetTake(1)
return h.GetByParm(data, qp)
}
/*
GetByFilter returns single data based on filter enteered. Data need to be comply with orm.DataModel.
Because no sort is defined, it will only 1st row by any given sort
If sort is needed pls use by ByParm
*/
func (h *Hub) GetByFilter(data orm.DataModel, filter *dbflex.Filter) error {
qp := dbflex.NewQueryParam().SetWhere(filter).SetTake(1)
return h.GetByParm(data, qp)
}
// GetByParm return single data based on filter
func (h *Hub) GetByParm(data orm.DataModel, parm *dbflex.QueryParam) error {
data.SetThis(data)
if parm == nil {
parm = dbflex.NewQueryParam()
}
idx, conn, err := h.getConn()
if err != nil {
return fmt.Errorf("connection error. %s", err.Error())
}
defer h.closeConn(idx, conn)
cmd := dbflex.From(data.TableName())
if len(parm.Select) == 0 {
cmd.Select()
} else {
cmd.Select(parm.Select...)
}
if where := parm.Where; where != nil {
cmd.Where(where)
}
if sort := parm.Sort; len(sort) > 0 {
cmd.OrderBy(sort...)
}
if skip := parm.Skip; skip > 0 {
cmd.Skip(skip)
}
if take := parm.Take; take > 0 {
cmd.Take(take)
}
cursor := conn.Cursor(cmd, nil)
if err := cursor.Error(); err != nil {
return err
}
if err = cursor.Fetch(data).Close(); err != nil {
return err
}
return nil
}
// Get return single data based on model. It will find record based on releant ID field
func (h *Hub) Get(data orm.DataModel) error {
data.SetThis(data)
idx, conn, err := h.getConn()
if err != nil {
return fmt.Errorf("connection error. %s", err.Error())
}
defer h.closeConn(idx, conn)
if err = orm.Get(conn, data); err != nil {
return err
}
return nil
}
// Gets return all data based on model and filter
func (h *Hub) Gets(data orm.DataModel, parm *dbflex.QueryParam, dest interface{}) error {
if parm == nil {
parm = dbflex.NewQueryParam()
}
idx, conn, err := h.getConn()
if err != nil {
return fmt.Errorf("connection error. %s", err.Error())
}
defer h.closeConn(idx, conn)
if err = orm.Gets(conn, data, dest, parm); err != nil {
return err
}
return nil
}
// GetsByFilter like gets but require only filter
func (h *Hub) GetsByFilter(data orm.DataModel, filter *dbflex.Filter, dest interface{}) error {
qp := dbflex.NewQueryParam()
qp.SetWhere(filter)
return h.Gets(data, qp, dest)
}
// Count returns number of data based on model and filter
func (h *Hub) Count(data orm.DataModel, qp *dbflex.QueryParam) (int, error) {
return h.CountAny(data.TableName(), qp)
}
// CountByFilter returns number of data based on model and filter
func (h *Hub) CountByFilter(data orm.DataModel, f *dbflex.Filter) (int, error) {
return h.CountAny(data.TableName(), dbflex.NewQueryParam().SetWhere(f))
}
// CountByAnyFilter returns number of data based on table name and filter
func (h *Hub) CountAnyByFilter(name string, f *dbflex.Filter) (int, error) {
return h.CountAny(name, dbflex.NewQueryParam().SetWhere(f))
}
// CountAny returns number of data based on tablename and filter
func (h *Hub) CountAny(name string, qp *dbflex.QueryParam) (int, error) {
if qp == nil {
qp = dbflex.NewQueryParam()
}
idx, conn, err := h.getConn()
if err != nil {
return 0, fmt.Errorf("connection error. %s", err.Error())
}
defer h.closeConn(idx, conn)
var cmd dbflex.ICommand
if qp == nil || qp.Where == nil {
cmd = dbflex.From(name).Select()
} else {
cmd = dbflex.From(name).Where(qp.Where).Select()
}
cur := conn.Cursor(cmd, nil)
if err = cur.Error(); err != nil {
return 0, fmt.Errorf("cursor error. %s", err.Error())
}
defer cur.Close()
return cur.Count(), nil
}
/*
GetAnyByFilter returns single data based on filter enteered. Data need to be comply with orm.DataModel.
Because no sort is defined, it will only return 1st row by any given resultset
If sort is needed pls use by ByParm
*/
func (h *Hub) GetAnyByFilter(tableName string, filter *dbflex.Filter, dest interface{}) error {
qp := dbflex.NewQueryParam().SetWhere(filter).SetTake(1)
return h.GetAnyByParm(tableName, qp, dest)
}
// GetAnyByParm return single data based on filter and table name
func (h *Hub) GetAnyByParm(tableName string, parm *dbflex.QueryParam, dest interface{}) error {
if parm == nil {
parm = dbflex.NewQueryParam()
}
idx, conn, err := h.getConn()
if err != nil {
return fmt.Errorf("connection error. %s", err.Error())
}
defer h.closeConn(idx, conn)
cmd := dbflex.From(tableName)
if len(parm.Select) == 0 {
cmd.Select()
} else {
cmd.Select(parm.Select...)
}
if where := parm.Where; where != nil {
cmd.Where(where)
}
if sort := parm.Sort; len(sort) > 0 {
cmd.OrderBy(sort...)
}
if skip := parm.Skip; skip > 0 {
cmd.Skip(skip)
}
if take := parm.Take; take > 0 {
cmd.Take(take)
}
cursor := conn.Cursor(cmd, nil)
if err := cursor.Error(); err != nil {
return err
}
if err = cursor.Fetch(dest).Close(); err != nil {
return err
}
return nil
}
// Execute will execute command. Normally used with no-datamodel object
func (h *Hub) Execute(cmd dbflex.ICommand, object interface{}) (interface{}, error) {
idx, conn, err := h.getConn()
if err != nil {
return nil, fmt.Errorf("connection error. %s", err.Error())
}
defer h.closeConn(idx, conn)
parm := codekit.M{}
return conn.Execute(cmd, parm.Set("data", object))
}
// Populate will return all data based on command. Normally used with no-datamodel object
func (h *Hub) Populate(cmd dbflex.ICommand, result interface{}) (int, error) {
idx, conn, err := h.getConn()
if err != nil {
return 0, fmt.Errorf("connection error. %s", err.Error())
}
defer h.closeConn(idx, conn)
c := conn.Cursor(cmd, nil)
if err = c.Error(); err != nil {
return 0, fmt.Errorf("unable to prepare cursor. %s", err.Error())
}
defer c.Close()
if err = c.Fetchs(result, 0).Error(); err != nil {
return 0, fmt.Errorf("unable to fetch data. %s", err.Error())
}
return c.Count(), nil
}
// PopulateByParm returns all data based on table name and QueryParm. Normally used with no-datamodel object
func (h *Hub) PopulateByParm(tableName string, parm *dbflex.QueryParam, dest interface{}) error {
idx, conn, err := h.getConn()
if err != nil {
return fmt.Errorf("connection error. %s", err.Error())
}
defer h.closeConn(idx, conn)
qry := dbflex.From(tableName)
if w := parm.Select; w != nil {
qry.Select(w...)
} else {
qry.Select()
}
if w := parm.Where; w != nil {
qry.Where(w)
}
if o := parm.Sort; len(o) > 0 {
qry.OrderBy(o...)
}
if o := parm.Skip; o > 0 {
qry.Skip(o)
}
if o := parm.Take; o > 0 {
qry.Take(o)
}
if o := parm.GroupBy; len(o) > 0 {
qry.GroupBy(o...)
}
if o := parm.Aggregates; len(o) > 0 {
qry.Aggr(o...)
}
cur := conn.Cursor(qry, nil)
if err = cur.Error(); err != nil {
return fmt.Errorf("error when running cursor for PopulateByParm. %s", err.Error())
}
err = cur.Fetchs(dest, 0).Close()
return err
}
/*
PopulateByFilter returns all data based on filter and n count.
No sort is given, hence it will take n data for resp filter
If n-count is 0, it will load all
*/
func (h *Hub) PopulateByFilter(tableName string, filter *dbflex.Filter, n int, dest interface{}) error {
qp := dbflex.NewQueryParam().SetWhere(filter)
if n > 0 {
qp = qp.SetTake(n)
}
return h.PopulateByParm(tableName, qp, dest)
}
// PopulateSQL returns data based on SQL Query
func (h *Hub) PopulateSQL(sql string, dest interface{}) error {
idx, conn, err := h.getConn()
if err != nil {
return fmt.Errorf("connection error. %s", err.Error())
}
defer h.closeConn(idx, conn)
qry := dbflex.SQL(sql)
cur := conn.Cursor(qry, nil)
if err = cur.Error(); err != nil {
return fmt.Errorf("error when running cursor for PopulateSQL. %s", err.Error())
}
err = cur.Fetchs(dest, 0).Close()
return err
}
func (h *Hub) Close() {
if h.usePool {
h.pool.Close()
}
}
// InsertAny insert any object into database table
func (h *Hub) InsertAny(name string, object interface{}) error {
idx, conn, err := h.getConn()
if err != nil {
return fmt.Errorf("connection error. %s", err.Error())
}
defer h.closeConn(idx, conn)
cmd := dbflex.From(name).Insert()
if _, err = conn.Execute(cmd, codekit.M{}.Set("data", object)); err != nil {
return fmt.Errorf("unable to save. %s", err.Error())
}
return nil
}
// SaveAny save any object into database table. Normally used with no-datamodel object
func (h *Hub) SaveAny(name string, filter *dbflex.Filter, object interface{}) error {
idx, conn, err := h.getConn()
if err != nil {
return fmt.Errorf("connection error. %s", err.Error())
}
defer h.closeConn(idx, conn)
if filter == nil {
return errors.New("SaveAny should have valid filter")
}
cmd := dbflex.From(name).Where(filter).Save()
if _, err = conn.Execute(cmd, codekit.M{}.Set("data", object)); err != nil {
return fmt.Errorf("unable to save. %s", err.Error())
}
return nil
}
// UpdateAny update specific fields on database table. Normally used with no-datamodel object
func (h *Hub) UpdateAny(name string, filter *dbflex.Filter, object interface{}, fields ...string) error {
idx, conn, err := h.getConn()
if err != nil {
return fmt.Errorf("connection error. %s", err.Error())
}
defer h.closeConn(idx, conn)
if filter == nil {
return errors.New("UpdateAny should have filter")
}
cmd := dbflex.From(name).Where(filter).Update(fields...)
if _, err = conn.Execute(cmd, codekit.M{}.Set("data", object)); err != nil {
return fmt.Errorf("unable to save. %s", err.Error())
}
return nil
}
// DeleteAny delete record on database table. Normally used with no-datamodel object
func (h *Hub) DeleteAny(name string, filter *dbflex.Filter) error {
idx, conn, err := h.getConn()
if err != nil {
return fmt.Errorf("connection error. %s", err.Error())
}
defer h.closeConn(idx, conn)
cmd := dbflex.From(name)
if filter != nil {
cmd.Where(filter)
}
cmd.Delete()
if _, err = conn.Execute(cmd, nil); err != nil {
return fmt.Errorf("unable to save. %s", err.Error())
}
return nil
}
// EnsureTable will ensure existense of table according to given object
func (h *Hub) EnsureTable(name string, keys []string, object interface{}) error {
idx, conn, e := h.GetConnection()
if e != nil {
return e
}
defer h.CloseConnection(idx, conn)
return conn.EnsureTable(name, keys, object)
}
// EnsureIndex ensure index existence
func (h *Hub) EnsureIndex(tableName, indexName string, unique bool, fields ...string) error {
idx, conn, e := h.GetConnection()
if e != nil {
return e
}
defer h.CloseConnection(idx, conn)
return conn.EnsureIndex(tableName, indexName, unique, fields...)
}
// EnsureDb
func (h *Hub) EnsureDb(obj orm.DataModel) error {
idx, conn, e := h.GetConnection()
if e != nil {
return e
}
defer h.CloseConnection(idx, conn)
return orm.EnsureDb(conn, obj)
}
// Validate validate if a connection can be established
func (h *Hub) Validate() error {
idx, conn, e := h.GetConnection()
if e != nil {
return e
}
defer h.CloseConnection(idx, conn)
return nil
}
func (h *Hub) GetByQuery(obj orm.DataModel, queryName string, param codekit.M) error {
if param == nil {
param = codekit.M{}
}
idx, conn, e := h.GetConnection()
if e != nil {
return e
}
defer h.CloseConnection(idx, conn)
return orm.GetQuery(conn, obj, queryName, param)
}
func (h *Hub) GetsByQuery(obj orm.DataModel, queryName string, param codekit.M, target interface{}) error {
if param == nil {
param = codekit.M{}
}
idx, conn, e := h.GetConnection()
if e != nil {
return e
}
defer h.CloseConnection(idx, conn)
return orm.GetsQuery(conn, obj, queryName, param, target)
}