Switch pgx as default driver

This commit is contained in:
Jinzhu 2020-07-03 08:53:38 +08:00
parent 3c03b6e527
commit 2d945a9641
3 changed files with 13 additions and 4 deletions

View File

@ -60,6 +60,7 @@ func (db *PreparedStmtDB) ExecContext(ctx context.Context, query string, args ..
return stmt.ExecContext(ctx, args...) return stmt.ExecContext(ctx, args...)
} else { } else {
db.mux.Lock() db.mux.Lock()
stmt.Close()
delete(db.Stmts, query) delete(db.Stmts, query)
db.mux.Unlock() db.mux.Unlock()
} }
@ -72,6 +73,7 @@ func (db *PreparedStmtDB) QueryContext(ctx context.Context, query string, args .
return stmt.QueryContext(ctx, args...) return stmt.QueryContext(ctx, args...)
} else { } else {
db.mux.Lock() db.mux.Lock()
stmt.Close()
delete(db.Stmts, query) delete(db.Stmts, query)
db.mux.Unlock() db.mux.Unlock()
} }
@ -84,6 +86,7 @@ func (db *PreparedStmtDB) QueryRowContext(ctx context.Context, query string, arg
return stmt.QueryRowContext(ctx, args...) return stmt.QueryRowContext(ctx, args...)
} else { } else {
db.mux.Lock() db.mux.Lock()
stmt.Close()
delete(db.Stmts, query) delete(db.Stmts, query)
db.mux.Unlock() db.mux.Unlock()
} }
@ -101,6 +104,7 @@ func (tx *PreparedStmtTX) ExecContext(ctx context.Context, query string, args ..
return tx.Tx.Stmt(stmt).ExecContext(ctx, args...) return tx.Tx.Stmt(stmt).ExecContext(ctx, args...)
} else { } else {
tx.PreparedStmtDB.mux.Lock() tx.PreparedStmtDB.mux.Lock()
stmt.Close()
delete(tx.PreparedStmtDB.Stmts, query) delete(tx.PreparedStmtDB.Stmts, query)
tx.PreparedStmtDB.mux.Unlock() tx.PreparedStmtDB.mux.Unlock()
} }
@ -113,6 +117,7 @@ func (tx *PreparedStmtTX) QueryContext(ctx context.Context, query string, args .
return tx.Tx.Stmt(stmt).QueryContext(ctx, args...) return tx.Tx.Stmt(stmt).QueryContext(ctx, args...)
} else { } else {
tx.PreparedStmtDB.mux.Lock() tx.PreparedStmtDB.mux.Lock()
stmt.Close()
delete(tx.PreparedStmtDB.Stmts, query) delete(tx.PreparedStmtDB.Stmts, query)
tx.PreparedStmtDB.mux.Unlock() tx.PreparedStmtDB.mux.Unlock()
} }
@ -125,6 +130,7 @@ func (tx *PreparedStmtTX) QueryRowContext(ctx context.Context, query string, arg
return tx.Tx.Stmt(stmt).QueryRowContext(ctx, args...) return tx.Tx.Stmt(stmt).QueryRowContext(ctx, args...)
} else { } else {
tx.PreparedStmtDB.mux.Lock() tx.PreparedStmtDB.mux.Lock()
stmt.Close()
delete(tx.PreparedStmtDB.Stmts, query) delete(tx.PreparedStmtDB.Stmts, query)
tx.PreparedStmtDB.mux.Unlock() tx.PreparedStmtDB.mux.Unlock()
} }

View File

@ -6,11 +6,11 @@ require (
github.com/google/uuid v1.1.1 github.com/google/uuid v1.1.1
github.com/jinzhu/now v1.1.1 github.com/jinzhu/now v1.1.1
github.com/lib/pq v1.6.0 github.com/lib/pq v1.6.0
gorm.io/driver/mysql v0.2.7 gorm.io/driver/mysql v0.2.8
gorm.io/driver/postgres v0.2.4 gorm.io/driver/postgres v0.2.5
gorm.io/driver/sqlite v1.0.8 gorm.io/driver/sqlite v1.0.8
gorm.io/driver/sqlserver v0.2.4 gorm.io/driver/sqlserver v0.2.4
gorm.io/gorm v0.2.9 gorm.io/gorm v0.2.19
) )
replace gorm.io/gorm => ../ replace gorm.io/gorm => ../

View File

@ -54,7 +54,10 @@ func OpenTestConnection() (db *gorm.DB, err error) {
if dbDSN == "" { if dbDSN == "" {
dbDSN = "user=gorm password=gorm DB.name=gorm port=9920 sslmode=disable TimeZone=Asia/Shanghai" dbDSN = "user=gorm password=gorm DB.name=gorm port=9920 sslmode=disable TimeZone=Asia/Shanghai"
} }
db, err = gorm.Open(postgres.Open(dbDSN), &gorm.Config{}) db, err = gorm.Open(postgres.New(postgres.Config{
DSN: dbDSN,
PreferSimpleProtocol: true,
}), &gorm.Config{})
case "sqlserver": case "sqlserver":
// CREATE LOGIN gorm WITH PASSWORD = 'LoremIpsum86'; // CREATE LOGIN gorm WITH PASSWORD = 'LoremIpsum86';
// CREATE DATABASE gorm; // CREATE DATABASE gorm;