forked from cloudwan/gohan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql.go
842 lines (754 loc) · 23.1 KB
/
sql.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
// Copyright (C) 2015 NTT Innovation Institute, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sql
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
"github.com/cloudwan/gohan/db/pagination"
"github.com/cloudwan/gohan/db/transaction"
"github.com/cloudwan/gohan/util"
"database/sql"
"github.com/cloudwan/gohan/schema"
"github.com/jmoiron/sqlx"
sq "github.com/lann/squirrel"
// DB import
_ "github.com/go-sql-driver/mysql"
_ "github.com/mattn/go-sqlite3"
_ "github.com/nati/go-fakedb"
)
const retryDB = 50
const retryDBWait = 10
const (
configVersionColumnName = "config_version"
stateVersionColumnName = "state_version"
stateErrorColumnName = "state_error"
stateColumnName = "state"
stateMonitoringColumnName = "state_monitoring"
)
//DB is sql implementation of DB
type DB struct {
sqlType, connectionString string
handlers map[string]propertyHandler
DB *sqlx.DB
}
//Transaction is sql implementation of Transaction
type Transaction struct {
transaction *sqlx.Tx
db *DB
closed bool
}
//NewDB constructor
func NewDB() *DB {
handlers := make(map[string]propertyHandler)
//TODO(nati) dynamic configuration
handlers["string"] = &stringHandler{}
handlers["number"] = &numberHandler{}
handlers["integer"] = &numberHandler{}
handlers["object"] = &jsonHandler{}
handlers["array"] = &jsonHandler{}
handlers["boolean"] = &boolHandler{}
return &DB{handlers: handlers}
}
//propertyHandler for each propertys
type propertyHandler interface {
encode(*schema.Property, interface{}) (interface{}, error)
decode(*schema.Property, interface{}) (interface{}, error)
dataType(*schema.Property) string
}
type defaultHandler struct {
}
func (handler *defaultHandler) encode(property *schema.Property, data interface{}) (interface{}, error) {
return data, nil
}
func (handler *defaultHandler) decode(property *schema.Property, data interface{}) (interface{}, error) {
return data, nil
}
func (handler *defaultHandler) dataType(property *schema.Property) (res string) {
// TODO(marcin) extend types for schema. Here is pretty ugly guessing
if property.ID == "id" || property.Relation != "" || property.Unique {
res = "varchar(255)"
} else {
res = "text"
}
return
}
type stringHandler struct {
defaultHandler
}
func (handler *stringHandler) encode(property *schema.Property, data interface{}) (interface{}, error) {
return data, nil
}
func (handler *stringHandler) decode(property *schema.Property, data interface{}) (interface{}, error) {
if bytes, ok := data.([]byte); ok {
return string(bytes), nil
}
return data, nil
}
type boolHandler struct{}
func (handler *boolHandler) encode(property *schema.Property, data interface{}) (interface{}, error) {
return data, nil
}
func (handler *boolHandler) decode(property *schema.Property, data interface{}) (res interface{}, err error) {
// different SQL drivers encode result with different type
// so we need to do manual checks
if data == nil {
return nil, nil
}
switch t := data.(type) {
default:
err = fmt.Errorf("unknown type %T", t)
return
case []uint8: // mysql
res, err = strconv.ParseUint(string(data.([]uint8)), 10, 64)
res = (res.(uint64) != 0)
case int64: //apparently also mysql
res = (data.(int64) != 0)
case bool: // sqlite3
res = data
}
return
}
func (handler *boolHandler) dataType(property *schema.Property) string {
return "boolean"
}
type numberHandler struct{}
func (handler *numberHandler) encode(property *schema.Property, data interface{}) (interface{}, error) {
return data, nil
}
func (handler *numberHandler) decode(property *schema.Property, data interface{}) (res interface{}, err error) {
// different SQL drivers encode result with different type
// so we need to do manual checks
if data == nil {
return nil, nil
}
switch t := data.(type) {
default:
return data, nil
case []uint8: // mysql
uintValue, _ := strconv.ParseUint(string(t), 10, 64)
res = int(uintValue)
case int64: // sqlite3
res = int(t)
}
return
}
func (handler *numberHandler) dataType(property *schema.Property) string {
return "numeric"
}
type jsonHandler struct {
}
func (handler *jsonHandler) encode(property *schema.Property, data interface{}) (interface{}, error) {
bytes, err := json.Marshal(data)
//TODO(nati) should handle encoding err
if err != nil {
return nil, err
}
return string(bytes), nil
}
func (handler *jsonHandler) decode(property *schema.Property, data interface{}) (interface{}, error) {
if bytes, ok := data.([]byte); ok {
var ret interface{}
err := json.Unmarshal(bytes, &ret)
return ret, err
}
return data, nil
}
func (handler *jsonHandler) dataType(property *schema.Property) string {
return "text"
}
func quote(str string) string {
return fmt.Sprintf("`%s`", str)
}
//Connect connec to the db
func (db *DB) Connect(sqlType, conn string, maxOpenConn int) (err error) {
db.sqlType = sqlType
db.connectionString = conn
rawDB, err := sql.Open(db.sqlType, db.connectionString)
if err != nil {
return err
}
rawDB.SetMaxOpenConns(maxOpenConn)
rawDB.SetMaxIdleConns(maxOpenConn)
db.DB = sqlx.NewDb(rawDB, db.sqlType)
if db.sqlType == "sqlite3" {
db.DB.Exec("PRAGMA foreign_keys = ON;")
}
for i := 0; i < retryDB; i++ {
err = db.DB.Ping()
if err == nil {
return nil
}
time.Sleep(retryDBWait * time.Second)
log.Info("Retrying db connection... (%s)", err)
}
return fmt.Errorf("Failed to connect db")
}
func (db *DB) Close() {
db.DB.Close()
}
//Begin starts new transaction
func (db *DB) Begin() (transaction.Transaction, error) {
transaction, err := db.DB.Beginx()
if err != nil {
return nil, err
}
if db.sqlType == "sqlite3" {
transaction.Exec("PRAGMA foreign_keys = ON;")
}
return &Transaction{
db: db,
transaction: transaction,
closed: false,
}, nil
}
func (db *DB) genTableCols(s *schema.Schema, cascade bool, exclude []string) ([]string, []string, []string) {
var cols []string
var relations []string
var indices []string
schemaManager := schema.GetManager()
for _, property := range s.Properties {
if util.ContainsString(exclude, property.ID) {
continue
}
handler := db.handlers[property.Type]
sqlDataType := property.SQLType
sqlDataProperties := ""
if db.sqlType == "sqlite3" {
sqlDataType = strings.Replace(sqlDataType, "auto_increment", "autoincrement", 1)
}
if sqlDataType == "" {
sqlDataType = handler.dataType(&property)
if property.ID == "id" {
sqlDataProperties = " primary key"
} else {
if property.Nullable {
sqlDataProperties = " null"
} else {
sqlDataProperties = " not null"
}
if property.Unique {
sqlDataProperties = " unique"
}
}
}
sql := "`" + property.ID + "` " + sqlDataType + sqlDataProperties
cols = append(cols, sql)
if property.Relation != "" {
foreignSchema, _ := schemaManager.Schema(property.Relation)
if foreignSchema != nil {
cascadeString := ""
if cascade ||
property.OnDeleteCascade ||
(property.Relation == s.Parent && s.OnParentDeleteCascade) {
cascadeString = "on delete cascade"
}
relationColumn := "id"
if property.RelationColumn != "" {
relationColumn = property.RelationColumn
}
relations = append(relations, fmt.Sprintf("foreign key(`%s`) REFERENCES `%s`(%s) %s",
property.ID, foreignSchema.GetDbTableName(), relationColumn, cascadeString))
}
}
if property.Indexed {
prefix := ""
if sqlDataType == "text" {
prefix = "(255)"
}
indices = append(indices, fmt.Sprintf("CREATE INDEX %s_%s_idx ON `%s`(`%s`%s);", s.Plural, property.ID,
s.Plural, property.ID, prefix))
}
}
return cols, relations, indices
}
//AlterTableDef generates alter table sql
func (db *DB) AlterTableDef(s *schema.Schema, cascade bool) (string, []string, error) {
var existing []string
rows, err := db.DB.Query(fmt.Sprintf("select * from `%s` limit 1;", s.GetDbTableName()))
if err == nil {
defer rows.Close()
existing, err = rows.Columns()
}
if err != nil {
return "", nil, err
}
cols, relations, indices := db.genTableCols(s, cascade, existing)
cols = append(cols, relations...)
if len(cols) == 0 {
return "", nil, nil
}
alterTable := fmt.Sprintf("alter table`%s` add (%s);\n", s.GetDbTableName(), strings.Join(cols, ","))
log.Debug("Altering table: " + alterTable)
log.Debug("Altering indices: " + strings.Join(indices, ""))
return alterTable, indices, nil
}
//GenTableDef generates create table sql
func (db *DB) GenTableDef(s *schema.Schema, cascade bool) (string, []string) {
cols, relations, indices := db.genTableCols(s, cascade, nil)
if s.StateVersioning() {
cols = append(cols, quote(configVersionColumnName)+"int not null default 1")
cols = append(cols, quote(stateVersionColumnName)+"int not null default 0")
cols = append(cols, quote(stateErrorColumnName)+"text not null default ''")
cols = append(cols, quote(stateColumnName)+"text not null default ''")
cols = append(cols, quote(stateMonitoringColumnName)+"text not null default ''")
}
cols = append(cols, relations...)
tableSQL := fmt.Sprintf("create table `%s` (%s);\n", s.GetDbTableName(), strings.Join(cols, ","))
log.Debug("Creating table: " + tableSQL)
log.Debug("Creating indices: " + strings.Join(indices, ""))
return tableSQL, indices
}
//RegisterTable creates table in the db
func (db *DB) RegisterTable(s *schema.Schema, cascade bool) error {
if s.IsAbstract() {
return nil
}
tableDef, indices, err := db.AlterTableDef(s, cascade)
if err != nil {
tableDef, indices = db.GenTableDef(s, cascade)
}
if tableDef == "" {
return nil
}
_, err = db.DB.Exec(tableDef)
if err != nil && indices != nil {
for _, indexSql := range indices {
_, err = db.DB.Exec(indexSql)
if err != nil {
return err
}
}
}
return err
}
//DropTable drop table definition
func (db *DB) DropTable(s *schema.Schema) error {
if s.IsAbstract() {
return nil
}
sql := fmt.Sprintf("drop table if exists %s\n", quote(s.GetDbTableName()))
_, err := db.DB.Exec(sql)
return err
}
func escapeID(ID string) string {
return strings.Replace(ID, "-", "_escape_", -1)
}
func logQuery(sql string, args ...interface{}) {
sqlFormat := strings.Replace(sql, "?", "%s", -1)
query := fmt.Sprintf(sqlFormat, args...)
log.Debug("Executing SQL query '%s'", query)
}
// Exec executes sql in transaction
func (tx *Transaction) Exec(sql string, args ...interface{}) error {
logQuery(sql, args...)
_, err := tx.transaction.Exec(sql, args...)
return err
}
//Create create resource in the db
func (tx *Transaction) Create(resource *schema.Resource) error {
var cols []string
var values []interface{}
db := tx.db
s := resource.Schema()
data := resource.Data()
q := sq.Insert(quote(s.GetDbTableName()))
for _, attr := range s.Properties {
//TODO(nati) support optional value
if _, ok := data[attr.ID]; ok {
handler := db.handler(&attr)
cols = append(cols, quote(attr.ID))
encoded, err := handler.encode(&attr, data[attr.ID])
if err != nil {
return fmt.Errorf("SQL Create encoding error: %s", err)
}
values = append(values, encoded)
}
}
q = q.Columns(cols...).Values(values...)
sql, args, err := q.ToSql()
if err != nil {
return err
}
return tx.Exec(sql, args...)
}
func (tx *Transaction) updateQuery(resource *schema.Resource) (sq.UpdateBuilder, error) {
s := resource.Schema()
db := tx.db
data := resource.Data()
q := sq.Update(quote(s.GetDbTableName()))
for _, attr := range s.Properties {
//TODO(nati) support optional value
if _, ok := data[attr.ID]; ok {
handler := db.handler(&attr)
encoded, err := handler.encode(&attr, data[attr.ID])
if err != nil {
return q, fmt.Errorf("SQL Update encoding error: %s", err)
}
q = q.Set(quote(attr.ID), encoded)
}
}
if s.Parent != "" {
q = q.Set(s.ParentSchemaPropertyID(), resource.ParentID())
}
return q, nil
}
//Update update resource in the db
func (tx *Transaction) Update(resource *schema.Resource) error {
q, err := tx.updateQuery(resource)
if err != nil {
return err
}
sql, args, err := q.ToSql()
if err != nil {
return err
}
if resource.Schema().StateVersioning() {
sql += ", `" + configVersionColumnName + "` = `" + configVersionColumnName + "` + 1"
}
sql += " WHERE id = ?"
args = append(args, resource.ID())
return tx.Exec(sql, args...)
}
//StateUpdate update resource state
func (tx *Transaction) StateUpdate(resource *schema.Resource, state *transaction.ResourceState) error {
q, err := tx.updateQuery(resource)
if err != nil {
return err
}
if resource.Schema().StateVersioning() && state != nil {
q = q.Set(quote(stateVersionColumnName), state.StateVersion)
q = q.Set(quote(stateErrorColumnName), state.Error)
q = q.Set(quote(stateColumnName), state.State)
q = q.Set(quote(stateMonitoringColumnName), state.Monitoring)
}
q = q.Where(sq.Eq{"id": resource.ID()})
sql, args, err := q.ToSql()
if err != nil {
return err
}
return tx.Exec(sql, args...)
}
//Delete delete resource from db
func (tx *Transaction) Delete(s *schema.Schema, resourceID interface{}) error {
sql, args, err := sq.Delete(quote(s.GetDbTableName())).Where(sq.Eq{"id": resourceID}).ToSql()
if err != nil {
return err
}
return tx.Exec(sql, args...)
}
func (db *DB) handler(property *schema.Property) propertyHandler {
handler, ok := db.handlers[property.Type]
if ok {
return handler
}
return &defaultHandler{}
}
func makeColumnID(tableName string, property schema.Property) string {
return fmt.Sprintf("%s__%s", tableName, property.ID)
}
func makeColumn(tableName string, property schema.Property) string {
return fmt.Sprintf("%s.%s", tableName, quote(property.ID))
}
func makeAliasTableName(tableName string, property schema.Property) string {
return fmt.Sprintf("%s__%s", tableName, property.RelationProperty)
}
// MakeColumns generates an array that has Gohan style colmun names
func MakeColumns(s *schema.Schema, tableName string, join bool) []string {
var cols []string
manager := schema.GetManager()
for _, property := range s.Properties {
cols = append(cols, makeColumn(tableName, property)+" as "+quote(makeColumnID(tableName, property)))
if property.RelationProperty != "" && join {
relatedSchema, _ := manager.Schema(property.Relation)
aliasTableName := makeAliasTableName(tableName, property)
cols = append(cols, MakeColumns(relatedSchema, aliasTableName, true)...)
}
}
return cols
}
func makeStateColumns(s *schema.Schema) (cols []string) {
dbTableName := s.GetDbTableName()
cols = append(cols, dbTableName+"."+configVersionColumnName+" as "+quote(configVersionColumnName))
cols = append(cols, dbTableName+"."+stateVersionColumnName+" as "+quote(stateVersionColumnName))
cols = append(cols, dbTableName+"."+stateErrorColumnName+" as "+quote(stateErrorColumnName))
cols = append(cols, dbTableName+"."+stateColumnName+" as "+quote(stateColumnName))
cols = append(cols, dbTableName+"."+stateMonitoringColumnName+" as "+quote(stateMonitoringColumnName))
return cols
}
func makeJoin(s *schema.Schema, tableName string, q sq.SelectBuilder) sq.SelectBuilder {
manager := schema.GetManager()
for _, property := range s.Properties {
if property.RelationProperty == "" {
continue
}
relatedSchema, _ := manager.Schema(property.Relation)
aliasTableName := makeAliasTableName(tableName, property)
q = q.LeftJoin(
fmt.Sprintf("%s as %s on %s.%s = %s.id", quote(relatedSchema.GetDbTableName()), quote(aliasTableName),
quote(tableName), quote(property.ID), quote(aliasTableName)))
q = makeJoin(relatedSchema, aliasTableName, q)
}
return q
}
func (tx *Transaction) decode(s *schema.Schema, tableName string, data map[string]interface{}, resource map[string]interface{}) {
manager := schema.GetManager()
db := tx.db
for _, property := range s.Properties {
handler := db.handler(&property)
value := data[makeColumnID(tableName, property)]
if value != nil || property.Nullable {
decoded, err := handler.decode(&property, value)
if err != nil {
log.Error(fmt.Sprintf("SQL List decoding error: %s", err))
}
resource[property.ID] = decoded
}
if property.RelationProperty != "" {
relatedSchema, _ := manager.Schema(property.Relation)
resourceData := map[string]interface{}{}
aliasTableName := makeAliasTableName(tableName, property)
tx.decode(relatedSchema, aliasTableName, data, resourceData)
resource[property.RelationProperty] = resourceData
}
}
}
func decodeState(data map[string]interface{}, state *transaction.ResourceState) error {
var ok bool
state.ConfigVersion, ok = data[configVersionColumnName].(int64)
if !ok {
return fmt.Errorf("Wrong state column %s returned from query", configVersionColumnName)
}
state.StateVersion, ok = data[stateVersionColumnName].(int64)
if !ok {
return fmt.Errorf("Wrong state column %s returned from query", stateVersionColumnName)
}
stateError, ok := data[stateErrorColumnName].([]byte)
if !ok {
return fmt.Errorf("Wrong state column %s returned from query", stateErrorColumnName)
}
state.Error = string(stateError)
stateState, ok := data[stateColumnName].([]byte)
if !ok {
return fmt.Errorf("Wrong state column %s returned from query", stateColumnName)
}
state.State = string(stateState)
stateMonitoring, ok := data[stateMonitoringColumnName].([]byte)
if !ok {
return fmt.Errorf("Wrong state column %s returned from query", stateMonitoringColumnName)
}
state.Monitoring = string(stateMonitoring)
return nil
}
//List resources in the db
func (tx *Transaction) List(s *schema.Schema, filter transaction.Filter, pg *pagination.Paginator) (list []*schema.Resource, total uint64, err error) {
cols := MakeColumns(s, s.GetDbTableName(), true)
q := sq.Select(cols...).From(quote(s.GetDbTableName()))
q, err = addFilterToQuery(s, q, filter, true)
if err != nil {
return nil, 0, err
}
if pg != nil {
property, err := s.GetPropertyByID(pg.Key)
if err == nil {
q = q.OrderBy(makeColumn(s.GetDbTableName(), *property) + " " + pg.Order)
if pg.Limit > 0 {
q = q.Limit(pg.Limit)
}
if pg.Offset > 0 {
q = q.Offset(pg.Offset)
}
}
}
q = makeJoin(s, s.GetDbTableName(), q)
sql, args, err := q.ToSql()
if err != nil {
return
}
logQuery(sql, args...)
rows, err := tx.transaction.Queryx(sql, args...)
if err != nil {
return
}
defer rows.Close()
list, err = tx.decodeRows(s, rows, list)
if err != nil {
return nil, 0, err
}
total, err = tx.count(s, filter)
return
}
// Query with raw sql string
func (tx *Transaction) Query(s *schema.Schema, query string, arguments []interface{}) (list []*schema.Resource, err error) {
logQuery(query, arguments...)
rows, err := tx.transaction.Queryx(query, arguments...)
if err != nil {
return nil, fmt.Errorf("Failed to run query: %s", query)
}
defer rows.Close()
list, err = tx.decodeRows(s, rows, list)
if err != nil {
return nil, err
}
return
}
func (tx *Transaction) decodeRows(s *schema.Schema, rows *sqlx.Rows, list []*schema.Resource) ([]*schema.Resource, error) {
for rows.Next() {
resourceData := map[string]interface{}{}
data := map[string]interface{}{}
rows.MapScan(data)
var resource *schema.Resource
tx.decode(s, s.GetDbTableName(), data, resourceData)
resource, err := schema.NewResource(s, resourceData)
if err != nil {
return nil, fmt.Errorf("Failed to decode rows")
}
list = append(list, resource)
}
return list, nil
}
//count count all matching resources in the db
func (tx *Transaction) count(s *schema.Schema, filter transaction.Filter) (res uint64, err error) {
q := sq.Select("Count(id) as count").From(quote(s.GetDbTableName()))
//Filter get already tested
q, _ = addFilterToQuery(s, q, filter, false)
sql, args, err := q.ToSql()
if err != nil {
return
}
result := map[string]interface{}{}
err = tx.transaction.QueryRowx(sql, args...).MapScan(result)
if err != nil {
return
}
count, _ := result["count"]
decoder := &numberHandler{}
decoded, decodeErr := decoder.decode(nil, count)
if decodeErr != nil {
err = fmt.Errorf("SQL List decoding error: %s", decodeErr)
return
}
res = uint64(decoded.(int))
return
}
//Fetch resources by ID in the db
func (tx *Transaction) Fetch(s *schema.Schema, filter transaction.Filter) (*schema.Resource, error) {
list, _, err := tx.List(s, filter, nil)
if len(list) < 1 {
return nil, fmt.Errorf("Failed to fetch %s", filter)
}
return list[0], err
}
//StateFetch fetches the state of the specified resource
func (tx *Transaction) StateFetch(s *schema.Schema, filter transaction.Filter) (state transaction.ResourceState, err error) {
if !s.StateVersioning() {
err = fmt.Errorf("Schema %s does not support state versioning.", s.ID)
return
}
cols := makeStateColumns(s)
q := sq.Select(cols...).From(quote(s.GetDbTableName()))
q, _ = addFilterToQuery(s, q, filter, true)
sql, args, err := q.ToSql()
if err != nil {
return
}
logQuery(sql, args...)
rows, err := tx.transaction.Queryx(sql, args...)
if err != nil {
return
}
defer rows.Close()
if !rows.Next() {
err = fmt.Errorf("No resource found")
return
}
data := map[string]interface{}{}
rows.MapScan(data)
err = decodeState(data, &state)
return
}
//RawTransaction returns raw transaction
func (tx *Transaction) RawTransaction() *sqlx.Tx {
return tx.transaction
}
//SetIsolationLevel specify transaction isolation level
func (tx *Transaction) SetIsolationLevel(level transaction.Type) error {
if tx.db.sqlType == "mysql" {
err := tx.Exec(fmt.Sprintf("set session transaction isolation level %s", level))
return err
}
return nil
}
//Commit commits transaction
func (tx *Transaction) Commit() error {
err := tx.transaction.Commit()
if err != nil {
return err
}
tx.closed = true
return nil
}
//Close closes connection
func (tx *Transaction) Close() error {
//Rollback if it isn't committed yet
var err error
if !tx.closed {
err = tx.transaction.Rollback()
if err != nil {
return err
}
tx.closed = true
}
return nil
}
//Closed returns whether the transaction is closed
func (tx *Transaction) Closed() bool {
return tx.closed
}
func addFilterToQuery(s *schema.Schema, q sq.SelectBuilder, filter map[string]interface{}, join bool) (sq.SelectBuilder, error) {
if filter == nil {
return q, nil
}
for key, value := range filter {
property, err := s.GetPropertyByID(key)
if err != nil {
return q, err
}
var column string
if join {
column = makeColumn(s.GetDbTableName(), *property)
} else {
column = quote(key)
}
queryValues, ok := value.([]string)
if ok && property.Type == "boolean" {
v := make([]bool, len(queryValues))
for i, j := range queryValues {
v[i], _ = strconv.ParseBool(j)
}
q = q.Where(sq.Eq{column: v})
} else {
q = q.Where(sq.Eq{column: value})
}
}
return q, nil
}
//SetMaxOpenConns limit maximum connections
func (db *DB) SetMaxOpenConns(maxIdleConns int) {
// db.DB.SetMaxOpenConns(maxIdleConns)
// db.DB.SetMaxIdleConns(maxIdleConns)
}