Skip to content

Commit

Permalink
patched batch insert
Browse files Browse the repository at this point in the history
  • Loading branch information
adranwit committed Apr 17, 2019
1 parent e7ac89a commit 4f6c472
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## April 15 2019 0.7.1
* Patched batch insert

## April 15 2019 0.7.0
* Added dynamic sql driver
* Added request limiter
Expand Down
6 changes: 5 additions & 1 deletion manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ func (m *AbstractManager) PersistData(connection Connection, data interface{}, t
var processed = 0
dialect := GetDatastoreDialect(m.config.DriverName)
canUseBatch := dialect != nil && dialect.CanPersistBatch()
Logf("[%v]: canUseBatch: %v\n", m.config.DriverName, canUseBatch)
var batchControl = &batchControl{
values: []interface{}{},
dataIndexes: []int{},
Expand Down Expand Up @@ -407,15 +408,18 @@ func (m *AbstractManager) PersistData(connection Connection, data interface{}, t
collection[index] = structPointerValue.Elem().Interface()
}
}

var batchSize = m.config.GetInt(BatchSizeKey, defaultBatchSize)
Logf("batch size: %v\n", batchSize)

persist := func(index int, item interface{}) error {
parametrizedSQL := sqlProvider(item)
if len(parametrizedSQL.Values) == 1 && parametrizedSQL.Type == SQLTypeUpdate {
//nothing to udpate, one parameter is ID=? without values to update
return nil
}

if parametrizedSQL.Type == SQLTypeInsert && canUseBatch && batchControl.firstSeq > 0 {
if parametrizedSQL.Type == SQLTypeInsert && canUseBatch {
if len(batchControl.dataIndexes) > batchSize {
if _, err := batchControl.Flush(connection, updateId); err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions sql_dialect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,8 @@ func TestSqlDialect(t *testing.T) {

assert.Nil(t, dialect.DropDatastore(manager, "bar"))

{
mySQLDialect := dsc.GetDatastoreDialect("mysql")
assert.True(t, mySQLDialect.CanPersistBatch())
}
}

0 comments on commit 4f6c472

Please sign in to comment.