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
_book
.idea/workspace.xml

17
main.go
View File

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

View File

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