Batch insert with sqlite
This commit is contained in:
parent
adb0e6293e
commit
2e2fd54278
27
dialects/sqlite/main.go
Normal file
27
dialects/sqlite/main.go
Normal file
@ -0,0 +1,27 @@
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
// import sqlite3 driver
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
// Config database config
|
||||
type Config gorm.Config
|
||||
|
||||
// Open initialize GORM db connection
|
||||
func Open(dsn string, config Config) (*gorm.DB, error) {
|
||||
dialect, err := New(dsn)
|
||||
config.Dialect = dialect
|
||||
gormConfig := gorm.Config(config)
|
||||
|
||||
return &gorm.DB{Config: &gormConfig}, err
|
||||
}
|
||||
|
||||
// New initialize sqlite dialect
|
||||
func New(dsn string) (*Dialect, error) {
|
||||
dbSQL, err := sql.Open("sqlite3", dsn)
|
||||
return &Dialect{DB: dbSQL}, err
|
||||
}
|
30
dialects/sqlite/main_test.go
Normal file
30
dialects/sqlite/main_test.go
Normal file
@ -0,0 +1,30 @@
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
)
|
||||
|
||||
var DB *gorm.DB
|
||||
|
||||
func init() {
|
||||
var err error
|
||||
DB, err = Open(filepath.Join(os.TempDir(), "gorm.db"), Config{})
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("No error should happen when connecting to test database, but got err=%+v", err))
|
||||
}
|
||||
}
|
||||
|
||||
func TestInsert(t *testing.T) {
|
||||
type User struct {
|
||||
gorm.Model
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
|
||||
DB.Create([]*User{{Name: "name1", Age: 10}, {Name: "name2", Age: 20}})
|
||||
}
|
@ -2,17 +2,16 @@ package sqlite
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/jinzhu/gorm/model"
|
||||
|
||||
// import sqlite3 driver
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
// Dialect Sqlite3 Dialect for GORM
|
||||
type Dialect struct {
|
||||
DB *sql.DB
|
||||
}
|
||||
|
||||
// Quote quote for value
|
||||
@ -29,13 +28,12 @@ func (dialect *Dialect) Insert(tx *gorm.DB) (err error) {
|
||||
)
|
||||
|
||||
s := bytes.NewBufferString("INSERT INTO ")
|
||||
tableName := <-tableNameChan
|
||||
s.WriteString(tableName)
|
||||
s.WriteString(dialect.Quote(<-tableNameChan))
|
||||
|
||||
if assignments := <-assignmentsChan; len(assignments) > 0 {
|
||||
columns := []string{}
|
||||
|
||||
// Write columns (table.column1, table.column2, table.column3)
|
||||
// Write columns (column1, column2, column3)
|
||||
s.WriteString(" (")
|
||||
|
||||
// Write values (v1, v2, v3), (v2-1, v2-2, v2-3)
|
||||
@ -43,9 +41,9 @@ func (dialect *Dialect) Insert(tx *gorm.DB) (err error) {
|
||||
|
||||
for idx, fields := range assignments {
|
||||
if idx != 0 {
|
||||
s.WriteString(", ")
|
||||
valueBuffer.WriteString(",")
|
||||
}
|
||||
valueBuffer = bytes.NewBufferString(" (")
|
||||
valueBuffer.WriteString(" (")
|
||||
|
||||
for j, field := range fields {
|
||||
if idx == 0 {
|
||||
@ -53,8 +51,6 @@ func (dialect *Dialect) Insert(tx *gorm.DB) (err error) {
|
||||
if j != 0 {
|
||||
s.WriteString(", ")
|
||||
}
|
||||
s.WriteString(dialect.Quote(tableName))
|
||||
s.WriteString(".")
|
||||
s.WriteString(dialect.Quote(field.Field.DBName))
|
||||
}
|
||||
|
||||
@ -64,12 +60,16 @@ func (dialect *Dialect) Insert(tx *gorm.DB) (err error) {
|
||||
valueBuffer.WriteString("?")
|
||||
|
||||
if field.IsBlank {
|
||||
args = append(args, field.Field.DefaultValue)
|
||||
if field.Field.HasDefaultValue {
|
||||
args = append(args, field.Field.DefaultValue)
|
||||
} else {
|
||||
args = append(args, nil)
|
||||
}
|
||||
} else {
|
||||
args = append(args, field.Value.Interface())
|
||||
}
|
||||
}
|
||||
valueBuffer = bytes.NewBufferString(") ")
|
||||
valueBuffer.WriteString(")")
|
||||
}
|
||||
s.WriteString(") ")
|
||||
|
||||
@ -78,6 +78,13 @@ func (dialect *Dialect) Insert(tx *gorm.DB) (err error) {
|
||||
s.WriteString(" DEFAULT VALUES")
|
||||
}
|
||||
|
||||
fmt.Println(s.String())
|
||||
fmt.Printf("%#v \n", args)
|
||||
if result, err := dialect.DB.Exec(s.String(), args...); err == nil {
|
||||
tx.RowsAffected, _ = result.RowsAffected()
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -89,7 +89,7 @@ func GetAssignments(tx *gorm.DB) chan [][]*Field {
|
||||
case reflect.Slice:
|
||||
fields := [][]*Field{}
|
||||
for i := 0; i < results.Len(); i++ {
|
||||
fields = append(fields, structToField(results.Index(i), s, assignableChecker))
|
||||
fields = append(fields, structToField(indirect(results.Index(i)), s, assignableChecker))
|
||||
}
|
||||
fieldChan <- fields
|
||||
case reflect.Struct:
|
||||
|
@ -22,23 +22,25 @@ func GetTable(tx *gorm.DB) chan string {
|
||||
tableName = name
|
||||
} else {
|
||||
for _, v := range []interface{}{tx.Statement.Table, tx.Statement.Dest} {
|
||||
if t, ok := v.(tabler); ok {
|
||||
tableName = t.TableName()
|
||||
} else if t, ok := v.(dbTabler); ok {
|
||||
tableName = t.TableName(tx)
|
||||
} else if s := schema.Parse(tx.Statement.Table); s != nil {
|
||||
if s.TableName != "" {
|
||||
tableName = s.TableName
|
||||
} else {
|
||||
tableName = schema.ToDBName(s.ModelType.Name())
|
||||
if !tx.Config.SingularTable {
|
||||
tableName = inflection.Plural(tableName)
|
||||
if v != nil {
|
||||
if t, ok := v.(tabler); ok {
|
||||
tableName = t.TableName()
|
||||
} else if t, ok := v.(dbTabler); ok {
|
||||
tableName = t.TableName(tx)
|
||||
} else if s := schema.Parse(v); s != nil {
|
||||
if s.TableName != "" {
|
||||
tableName = s.TableName
|
||||
} else {
|
||||
tableName = schema.ToDBName(s.ModelType.Name())
|
||||
if !tx.Config.SingularTable {
|
||||
tableName = inflection.Plural(tableName)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if tableName != "" {
|
||||
break
|
||||
if tableName != "" {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user