Well Format the changed file

This commit is contained in:
fwhez 2018-11-12 10:36:51 +08:00
parent d9161ec948
commit 432181e1f0
3 changed files with 20 additions and 21 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
documents documents
_book _book
.idea/workspace.xml

17
main.go
View File

@ -105,20 +105,19 @@ func (s *DB) New() *DB {
// DataSource returns its opened dataSource: // DataSource returns its opened dataSource:
// gorm.Open("postgres", "host=localhost user=root dbname=test sslmode=disable password=123") // gorm.Open("postgres", "host=localhost user=root dbname=test sslmode=disable password=123")
// Then dataSource = "host=localhost user=root dbname=test sslmode=disable password=123" // Then dataSource = "host=localhost user=root dbname=test sslmode=disable password=123"
func (s *DB) DataSource() string{ func (s *DB) DataSource() string {
return s.dataSource return s.dataSource
} }
// CopyDB returns its origin db engine whose driver is github.com/lib/pq // CopyDB returns its origin db engine whose driver is github.com/lib/pq
func (s *DB) CopyDB() (*sql.DB,error){ func (s *DB) CopyDB() (*sql.DB, error) {
if s.copyDB!=nil { if s.copyDB != nil {
return s.copyDB,nil return s.copyDB, nil
}else{ } else {
return sql.Open(s.Dialect().GetName(), s.DataSource()) return sql.Open(s.Dialect().GetName(), s.DataSource())
} }
} }
type closer interface { type closer interface {
Close() error Close() error
} }
@ -501,13 +500,13 @@ func (s *DB) Exec(sql string, values ...interface{}) *DB {
// db.CopyIn(false, "user", args, "name","age") // db.CopyIn(false, "user", args, "name","age")
// This stands for 'insert into user(name,age) values('tom', 9),('sara',10),('jim',19)' // This stands for 'insert into user(name,age) values('tom', 9),('sara',10),('jim',19)'
// or 'COPY 'user' ('name','age') FROM STDIN`' // or 'COPY 'user' ('name','age') FROM STDIN`'
func (s *DB) CopyIn(closeAfterUsed bool,table string, args [][]interface{}, columns ...string) error { func (s *DB) CopyIn(closeAfterUsed bool, table string, args [][]interface{}, columns ...string) error {
if s.Dialect().GetName() != "postgres" { if s.Dialect().GetName() != "postgres" {
return errors.New("CopyIn only supports postgres") return errors.New("CopyIn only supports postgres")
} }
pdb, err := s.CopyDB() pdb, err := s.CopyDB()
if closeAfterUsed{ if closeAfterUsed {
defer pdb.Close() defer pdb.Close()
} }
@ -523,7 +522,7 @@ func (s *DB) CopyIn(closeAfterUsed bool,table string, args [][]interface{}, colu
if err != nil { if err != nil {
return err return err
} }
for _,v:= range args { for _, v := range args {
_, err = stmt.Exec(v...) _, err = stmt.Exec(v...)
if err != nil { if err != nil {
txn.Rollback() txn.Rollback()

View File

@ -1080,22 +1080,21 @@ func TestDB_CopyIn(t *testing.T) {
if e != nil { if e != nil {
t.Fatal(e.Error()) t.Fatal(e.Error())
} }
type Example struct{ type Example struct {
Name string Name string
Age int Age int
} }
var examples = make([]Example,0) var examples = make([]Example, 0)
e=DB.Raw("select * from example").Find(&examples).Error e = DB.Raw("select * from example").Find(&examples).Error
if e!=nil { if e != nil {
t.Fatal(e.Error()) t.Fatal(e.Error())
} }
if len(examples) ==0 { if len(examples) == 0 {
t.Fatal("examples length wants more than 3, but got 0") t.Fatal("examples length wants more than 3, but got 0")
} }
fmt.Println(examples) fmt.Println(examples)
} }
func BenchmarkGorm(b *testing.B) { func BenchmarkGorm(b *testing.B) {
b.N = 2000 b.N = 2000
for x := 0; x < b.N; x++ { for x := 0; x < b.N; x++ {