Skip to content

Commit ac426b2

Browse files
committed
Separate writer, batch, action log to new packages
1 parent 37af8d8 commit ac426b2

13 files changed

+136
-110
lines changed

action_log.go renamed to action/action_log.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
package sql
1+
package action
22

33
import (
44
"context"
55
"database/sql"
66
"fmt"
77
"strings"
88
"time"
9+
10+
q "github.com/core-go/sql"
911
)
1012

1113
type ActionLogConf struct {
1214
Log bool `yaml:"log" mapstructure:"log" json:"log,omitempty" gorm:"column:log" bson:"log,omitempty" dynamodbav:"log,omitempty" firestore:"log,omitempty"`
13-
DB Config `yaml:"db" mapstructure:"db" json:"db,omitempty" gorm:"column:db" bson:"db,omitempty" dynamodbav:"db,omitempty" firestore:"db,omitempty"`
15+
DB q.Config `yaml:"db" mapstructure:"db" json:"db,omitempty" gorm:"column:db" bson:"db,omitempty" dynamodbav:"db,omitempty" firestore:"db,omitempty"`
1416
Schema ActionLogSchema `yaml:"schema" mapstructure:"schema" json:"schema,omitempty" gorm:"column:schema" bson:"schema,omitempty" dynamodbav:"schema,omitempty" firestore:"schema,omitempty"`
1517
Config ActionLogConfig `yaml:"config" mapstructure:"config" json:"config,omitempty" gorm:"column:config" bson:"config,omitempty" dynamodbav:"config,omitempty" firestore:"config,omitempty"`
1618
}
@@ -58,7 +60,7 @@ func NewActionLogWriter(database *sql.DB, tableName string, config ActionLogConf
5860
s.Timestamp = strings.ToLower(s.Timestamp)
5961
s.Status = strings.ToLower(s.Status)
6062
s.Desc = strings.ToLower(s.Desc)
61-
driver := GetDriver(database)
63+
driver := q.GetDriver(database)
6264
if len(s.Id) == 0 {
6365
s.Id = "id"
6466
}
@@ -84,7 +86,7 @@ func NewActionLogWriter(database *sql.DB, tableName string, config ActionLogConf
8486
if len(options) > 0 && options[0] != nil {
8587
buildParam = options[0]
8688
} else {
87-
buildParam = GetBuild(database)
89+
buildParam = q.GetBuild(database)
8890
}
8991
writer := ActionLogWriter{Database: database, Table: tableName, Config: config, Schema: s, Generate: generate, BuildParam: buildParam, Driver: driver}
9092
return &writer
@@ -153,12 +155,12 @@ func GetString(ctx context.Context, key string) string {
153155
return ""
154156
}
155157
func BuildInsertSQL(db *sql.DB, tableName string, model map[string]interface{}, options ...func(i int) string) (string, []interface{}) {
156-
driver := GetDriver(db)
158+
driver := q.GetDriver(db)
157159
var buildParam func(i int) string
158160
if len(options) > 0 && options[0] != nil {
159161
buildParam = options[0]
160162
} else {
161-
buildParam = GetBuild(db)
163+
buildParam = q.GetBuild(db)
162164
}
163165
var cols []string
164166
var values []interface{}
@@ -179,7 +181,7 @@ func BuildInsertSQL(db *sql.DB, tableName string, model map[string]interface{},
179181
}
180182

181183
func QuoteString(name string, driver string) string {
182-
if driver == DriverPostgres {
184+
if driver == q.DriverPostgres {
183185
name = "`" + strings.Replace(name, "`", "``", -1) + "`"
184186
}
185187
return name

batch_inserter.go renamed to batch/batch_inserter.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
package sql
1+
package batch
22

33
import (
44
"context"
55
"database/sql"
66
"database/sql/driver"
77
"reflect"
8+
9+
q "github.com/core-go/sql"
810
)
911

1012
type BatchInserter struct {
@@ -14,7 +16,7 @@ type BatchInserter struct {
1416
Map func(ctx context.Context, model interface{}) (interface{}, error)
1517
BoolSupport bool
1618
VersionIndex int
17-
Schema *Schema
19+
Schema *q.Schema
1820
ToArray func(interface{}) interface {
1921
driver.Valuer
2022
sql.Scanner
@@ -45,11 +47,11 @@ func NewSqlBatchInserter(db *sql.DB, tableName string, modelType reflect.Type, m
4547
if len(options) > 0 && options[0] != nil {
4648
buildParam = options[0]
4749
} else {
48-
buildParam = GetBuild(db)
50+
buildParam = q.GetBuild(db)
4951
}
50-
driver := GetDriver(db)
51-
boolSupport := driver == DriverPostgres
52-
schema := CreateSchema(modelType)
52+
driver := q.GetDriver(db)
53+
boolSupport := driver == q.DriverPostgres
54+
schema := q.CreateSchema(modelType)
5355
return &BatchInserter{db: db, tableName: tableName, BuildParam: buildParam, BoolSupport: boolSupport, Schema: schema, Map: mp, ToArray: toArray}
5456
}
5557

@@ -59,26 +61,26 @@ func (w *BatchInserter) Write(ctx context.Context, models interface{}) ([]int, [
5961
var models2 interface{}
6062
var er0 error
6163
if w.Map != nil {
62-
models2, er0 = MapModels(ctx, models, w.Map)
64+
models2, er0 = q.MapModels(ctx, models, w.Map)
6365
if er0 != nil {
6466
s0 := reflect.ValueOf(models2)
65-
_, er0b := InterfaceSlice(models2)
66-
failIndices = ToArrayIndex(s0, failIndices)
67+
_, er0b := q.InterfaceSlice(models2)
68+
failIndices = q.ToArrayIndex(s0, failIndices)
6769
return successIndices, failIndices, er0b
6870
}
6971
} else {
7072
models2 = models
7173
}
7274
s := reflect.ValueOf(models2)
73-
_, er2 := InsertBatchWithSchema(ctx, w.db, w.tableName, models2, w.ToArray, w.BuildParam, w.Schema)
75+
_, er2 := q.InsertBatchWithSchema(ctx, w.db, w.tableName, models2, w.ToArray, w.BuildParam, w.Schema)
7476

7577
if er2 == nil {
7678
// Return full success
77-
successIndices = ToArrayIndex(s, successIndices)
79+
successIndices = q.ToArrayIndex(s, successIndices)
7880
return successIndices, failIndices, er2
7981
} else {
8082
// Return full fail
81-
failIndices = ToArrayIndex(s, failIndices)
83+
failIndices = q.ToArrayIndex(s, failIndices)
8284
}
8385
return successIndices, failIndices, er2
8486
}

batch_patcher.go renamed to batch/batch_patcher.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
package sql
1+
package batch
22

33
import (
44
"context"
55
"database/sql"
66
"reflect"
7+
8+
q "github.com/core-go/sql"
79
)
810

911
type BatchPatcher struct {
@@ -23,21 +25,21 @@ func NewBatchPatcherWithIds(db *sql.DB, tableName string, modelType reflect.Type
2325
modelsTypes := reflect.Zero(reflect.SliceOf(modelType)).Type()
2426
idJsonName := make([]string, 0)
2527
if fieldName == nil || len(fieldName) == 0 {
26-
fieldName, idJsonName = FindPrimaryKeys(modelType)
28+
fieldName, idJsonName = q.FindPrimaryKeys(modelType)
2729
}
2830
var buildParam func(i int) string
2931
if len(options) > 0 && options[0] != nil {
3032
buildParam = options[0]
3133
} else {
32-
buildParam = GetBuild(db)
34+
buildParam = q.GetBuild(db)
3335
}
3436
return &BatchPatcher{db: db, tableName: tableName, idNames: fieldName, idJsonName: idJsonName, modelsType: modelType, modelsTypes: modelsTypes, buildParam: buildParam}
3537
}
3638

3739
func (w *BatchPatcher) Write(ctx context.Context, models []map[string]interface{}) ([]int, []int, error) {
3840
successIndices := make([]int, 0)
3941
failIndices := make([]int, 0)
40-
_, err := PatchInTransaction(ctx, w.db, w.tableName, models, w.idNames, w.idJsonName, w.buildParam)
42+
_, err := q.PatchInTransaction(ctx, w.db, w.tableName, models, w.idNames, w.idJsonName, w.buildParam)
4143

4244
if err == nil {
4345
// Return full success

batch_updater.go renamed to batch/batch_updater.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
package sql
1+
package batch
22

33
import (
44
"context"
55
"database/sql"
66
"database/sql/driver"
77
"reflect"
8+
9+
q "github.com/core-go/sql"
810
)
911

1012
type BatchUpdater struct {
@@ -14,7 +16,7 @@ type BatchUpdater struct {
1416
Map func(ctx context.Context, model interface{}) (interface{}, error)
1517
BoolSupport bool
1618
VersionIndex int
17-
Schema *Schema
19+
Schema *q.Schema
1820
ToArray func(interface{}) interface {
1921
driver.Valuer
2022
sql.Scanner
@@ -55,11 +57,11 @@ func NewSqlBatchUpdater(db *sql.DB, tableName string, modelType reflect.Type, ve
5557
if len(options) > 0 && options[0] != nil {
5658
buildParam = options[0]
5759
} else {
58-
buildParam = GetBuild(db)
60+
buildParam = q.GetBuild(db)
5961
}
60-
driver := GetDriver(db)
61-
boolSupport := driver == DriverPostgres
62-
schema := CreateSchema(modelType)
62+
driver := q.GetDriver(db)
63+
boolSupport := driver == q.DriverPostgres
64+
schema := q.CreateSchema(modelType)
6365
return &BatchUpdater{db: db, tableName: tableName, Schema: schema, BoolSupport: boolSupport, VersionIndex: versionIndex, Map: mp, BuildParam: buildParam, ToArray: toArray}
6466
}
6567
func (w *BatchUpdater) Write(ctx context.Context, models interface{}) ([]int, []int, error) {
@@ -68,25 +70,25 @@ func (w *BatchUpdater) Write(ctx context.Context, models interface{}) ([]int, []
6870
var models2 interface{}
6971
var er0 error
7072
if w.Map != nil {
71-
models2, er0 = MapModels(ctx, models, w.Map)
73+
models2, er0 = q.MapModels(ctx, models, w.Map)
7274
if er0 != nil {
7375
s0 := reflect.ValueOf(models2)
74-
_, er0b := InterfaceSlice(models2)
75-
failIndices = ToArrayIndex(s0, failIndices)
76+
_, er0b := q.InterfaceSlice(models2)
77+
failIndices = q.ToArrayIndex(s0, failIndices)
7678
return successIndices, failIndices, er0b
7779
}
7880
} else {
7981
models2 = models
8082
}
81-
_, err := UpdateBatchWithVersion(ctx, w.db, w.tableName, models2, w.VersionIndex, w.ToArray, w.BuildParam, w.BoolSupport, w.Schema)
83+
_, err := q.UpdateBatchWithVersion(ctx, w.db, w.tableName, models2, w.VersionIndex, w.ToArray, w.BuildParam, w.BoolSupport, w.Schema)
8284
s := reflect.ValueOf(models)
8385
if err == nil {
8486
// Return full success
85-
successIndices = ToArrayIndex(s, successIndices)
87+
successIndices = q.ToArrayIndex(s, successIndices)
8688
return successIndices, failIndices, err
8789
} else {
8890
// Return full fail
89-
failIndices = ToArrayIndex(s, failIndices)
91+
failIndices = q.ToArrayIndex(s, failIndices)
9092
}
9193
return successIndices, failIndices, err
9294
}

batch_writer.go renamed to batch/batch_writer.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
package sql
1+
package batch
22

33
import (
44
"context"
55
"database/sql"
66
"database/sql/driver"
77
"reflect"
8+
9+
q "github.com/core-go/sql"
810
)
911

1012
type BatchWriter struct {
1113
db *sql.DB
1214
tableName string
1315
Map func(ctx context.Context, model interface{}) (interface{}, error)
14-
Schema *Schema
16+
Schema *q.Schema
1517
ToArray func(interface{}) interface {
1618
driver.Valuer
1719
sql.Scanner
@@ -32,7 +34,7 @@ func NewBatchWriterWithArray(db *sql.DB, tableName string, modelType reflect.Typ
3234
if len(options) > 0 && options[0] != nil {
3335
mp = options[0]
3436
}
35-
schema := CreateSchema(modelType)
37+
schema := q.CreateSchema(modelType)
3638
return &BatchWriter{db: db, tableName: tableName, Schema: schema, Map: mp, ToArray: toArray}
3739
}
3840

@@ -42,26 +44,26 @@ func (w *BatchWriter) Write(ctx context.Context, models interface{}) ([]int, []i
4244
var m interface{}
4345
var er0 error
4446
if w.Map != nil {
45-
m, er0 = MapModels(ctx, models, w.Map)
47+
m, er0 = q.MapModels(ctx, models, w.Map)
4648
if er0 != nil {
4749
s0 := reflect.ValueOf(m)
48-
_, er0b := InterfaceSlice(m)
49-
failIndices = ToArrayIndex(s0, failIndices)
50+
_, er0b := q.InterfaceSlice(m)
51+
failIndices = q.ToArrayIndex(s0, failIndices)
5052
return successIndices, failIndices, er0b
5153
}
5254
} else {
5355
m = models
5456
}
5557
s := reflect.ValueOf(m)
56-
_, er2 := SaveBatchWithArray(ctx, w.db, w.tableName, m, w.ToArray, w.Schema)
58+
_, er2 := q.SaveBatchWithArray(ctx, w.db, w.tableName, m, w.ToArray, w.Schema)
5759

5860
if er2 == nil {
5961
// Return full success
60-
successIndices = ToArrayIndex(s, successIndices)
62+
successIndices = q.ToArrayIndex(s, successIndices)
6163
return successIndices, failIndices, er2
6264
} else {
6365
// Return full fail
64-
failIndices = ToArrayIndex(s, failIndices)
66+
failIndices = q.ToArrayIndex(s, failIndices)
6567
}
6668
return successIndices, failIndices, er2
6769
}

size_batch_inserter.go renamed to batch/size_batch_inserter.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
package sql
1+
package batch
22

33
import (
44
"context"
55
"database/sql"
66
"reflect"
7+
8+
q "github.com/core-go/sql"
79
)
810

911
type SizeBatchInserter struct {
@@ -24,7 +26,7 @@ func NewSizeSqlBatchInserter(db *sql.DB, tableName string, mp func(context.Conte
2426
if len(options) > 0 && options[0] != nil {
2527
buildParam = options[0]
2628
} else {
27-
buildParam = GetBuild(db)
29+
buildParam = q.GetBuild(db)
2830
}
2931
return &SizeBatchInserter{db: db, tableName: tableName, BuildParam: buildParam, Map: mp}
3032
}
@@ -35,24 +37,24 @@ func (w *SizeBatchInserter) Write(ctx context.Context, models interface{}) ([]in
3537
var models2 interface{}
3638
var er0 error
3739
if w.Map != nil {
38-
models2, er0 = MapModels(ctx, models, w.Map)
40+
models2, er0 = q.MapModels(ctx, models, w.Map)
3941
if er0 != nil {
4042
s0 := reflect.ValueOf(models2)
41-
_, er0b := InterfaceSlice(models2)
43+
_, er0b := q.InterfaceSlice(models2)
4244
failIndices = ToArrayIndex(s0, failIndices)
4345
return successIndices, failIndices, er0b
4446
}
4547
} else {
4648
models2 = models
4749
}
4850
s := reflect.ValueOf(models2)
49-
_models, er1 := InterfaceSlice(models2)
51+
_models, er1 := q.InterfaceSlice(models2)
5052
if er1 != nil {
5153
// Return full fail
5254
failIndices = ToArrayIndex(s, failIndices)
5355
return successIndices, failIndices, er1
5456
}
55-
_, er2 := InsertManyWithSize(ctx, w.db, w.tableName, _models, 0, w.BuildParam)
57+
_, er2 := q.InsertManyWithSize(ctx, w.db, w.tableName, _models, 0, w.BuildParam)
5658

5759
if er2 == nil {
5860
// Return full success

0 commit comments

Comments
 (0)