Well Format the changed file
This commit is contained in:
parent
d9161ec948
commit
432181e1f0
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1,3 @@
|
||||
documents
|
||||
_book
|
||||
.idea/workspace.xml
|
||||
|
27
main.go
27
main.go
@ -19,7 +19,7 @@ type DB struct {
|
||||
|
||||
// for copyIn
|
||||
dataSource string
|
||||
copyDB *sql.DB
|
||||
copyDB *sql.DB
|
||||
|
||||
// single db
|
||||
db SQLCommon
|
||||
@ -75,10 +75,10 @@ func Open(dialect string, args ...interface{}) (db *DB, err error) {
|
||||
}
|
||||
|
||||
db = &DB{
|
||||
db: dbSQL,
|
||||
logger: defaultLogger,
|
||||
callbacks: DefaultCallback,
|
||||
dialect: newDialect(dialect, dbSQL),
|
||||
db: dbSQL,
|
||||
logger: defaultLogger,
|
||||
callbacks: DefaultCallback,
|
||||
dialect: newDialect(dialect, dbSQL),
|
||||
dataSource: source,
|
||||
}
|
||||
db.parent = db
|
||||
@ -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()
|
||||
|
13
main_test.go
13
main_test.go
@ -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
|
||||
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++ {
|
||||
|
Loading…
x
Reference in New Issue
Block a user