|
|
@ -6,6 +6,9 @@ import (
|
|
|
|
"github.com/tal-tech/go-zero/core/breaker"
|
|
|
|
"github.com/tal-tech/go-zero/core/breaker"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// datasource placeholder for logging error.
|
|
|
|
|
|
|
|
const rawDB = "sql.DB"
|
|
|
|
|
|
|
|
|
|
|
|
// ErrNotFound is an alias of sql.ErrNoRows
|
|
|
|
// ErrNotFound is an alias of sql.ErrNoRows
|
|
|
|
var ErrNotFound = sql.ErrNoRows
|
|
|
|
var ErrNotFound = sql.ErrNoRows
|
|
|
|
|
|
|
|
|
|
|
@ -23,6 +26,7 @@ type (
|
|
|
|
// SqlConn only stands for raw connections, so Transact method can be called.
|
|
|
|
// SqlConn only stands for raw connections, so Transact method can be called.
|
|
|
|
SqlConn interface {
|
|
|
|
SqlConn interface {
|
|
|
|
Session
|
|
|
|
Session
|
|
|
|
|
|
|
|
RawDB() (*sql.DB, error)
|
|
|
|
Transact(func(session Session) error) error
|
|
|
|
Transact(func(session Session) error) error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -43,13 +47,15 @@ type (
|
|
|
|
// Because CORBA doesn't support PREPARE, so we need to combine the
|
|
|
|
// Because CORBA doesn't support PREPARE, so we need to combine the
|
|
|
|
// query arguments into one string and do underlying query without arguments
|
|
|
|
// query arguments into one string and do underlying query without arguments
|
|
|
|
commonSqlConn struct {
|
|
|
|
commonSqlConn struct {
|
|
|
|
driverName string
|
|
|
|
|
|
|
|
datasource string
|
|
|
|
datasource string
|
|
|
|
|
|
|
|
connProv connProvider
|
|
|
|
beginTx beginnable
|
|
|
|
beginTx beginnable
|
|
|
|
brk breaker.Breaker
|
|
|
|
brk breaker.Breaker
|
|
|
|
accept func(error) bool
|
|
|
|
accept func(error) bool
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
connProvider func() (*sql.DB, error)
|
|
|
|
|
|
|
|
|
|
|
|
sessionConn interface {
|
|
|
|
sessionConn interface {
|
|
|
|
Exec(query string, args ...interface{}) (sql.Result, error)
|
|
|
|
Exec(query string, args ...interface{}) (sql.Result, error)
|
|
|
|
Query(query string, args ...interface{}) (*sql.Rows, error)
|
|
|
|
Query(query string, args ...interface{}) (*sql.Rows, error)
|
|
|
@ -69,10 +75,30 @@ type (
|
|
|
|
// NewSqlConn returns a SqlConn with given driver name and datasource.
|
|
|
|
// NewSqlConn returns a SqlConn with given driver name and datasource.
|
|
|
|
func NewSqlConn(driverName, datasource string, opts ...SqlOption) SqlConn {
|
|
|
|
func NewSqlConn(driverName, datasource string, opts ...SqlOption) SqlConn {
|
|
|
|
conn := &commonSqlConn{
|
|
|
|
conn := &commonSqlConn{
|
|
|
|
driverName: driverName,
|
|
|
|
|
|
|
|
datasource: datasource,
|
|
|
|
datasource: datasource,
|
|
|
|
beginTx: begin,
|
|
|
|
connProv: func() (*sql.DB, error) {
|
|
|
|
brk: breaker.NewBreaker(),
|
|
|
|
return getSqlConn(driverName, datasource)
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
beginTx: begin,
|
|
|
|
|
|
|
|
brk: breaker.NewBreaker(),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
|
|
|
|
opt(conn)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return conn
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// NewSqlConnFromDB returns a SqlConn with the given sql.DB.
|
|
|
|
|
|
|
|
// Use it with caution, it's provided for other ORM to interact with.
|
|
|
|
|
|
|
|
func NewSqlConnFromDB(db *sql.DB, opts ...SqlOption) SqlConn {
|
|
|
|
|
|
|
|
conn := &commonSqlConn{
|
|
|
|
|
|
|
|
datasource: rawDB,
|
|
|
|
|
|
|
|
connProv: func() (*sql.DB, error) {
|
|
|
|
|
|
|
|
return db, nil
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
beginTx: begin,
|
|
|
|
|
|
|
|
brk: breaker.NewBreaker(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(conn)
|
|
|
|
opt(conn)
|
|
|
@ -84,7 +110,7 @@ func NewSqlConn(driverName, datasource string, opts ...SqlOption) SqlConn {
|
|
|
|
func (db *commonSqlConn) Exec(q string, args ...interface{}) (result sql.Result, err error) {
|
|
|
|
func (db *commonSqlConn) Exec(q string, args ...interface{}) (result sql.Result, err error) {
|
|
|
|
err = db.brk.DoWithAcceptable(func() error {
|
|
|
|
err = db.brk.DoWithAcceptable(func() error {
|
|
|
|
var conn *sql.DB
|
|
|
|
var conn *sql.DB
|
|
|
|
conn, err = getSqlConn(db.driverName, db.datasource)
|
|
|
|
conn, err = db.connProv()
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
logInstanceError(db.datasource, err)
|
|
|
|
logInstanceError(db.datasource, err)
|
|
|
|
return err
|
|
|
|
return err
|
|
|
@ -100,7 +126,7 @@ func (db *commonSqlConn) Exec(q string, args ...interface{}) (result sql.Result,
|
|
|
|
func (db *commonSqlConn) Prepare(query string) (stmt StmtSession, err error) {
|
|
|
|
func (db *commonSqlConn) Prepare(query string) (stmt StmtSession, err error) {
|
|
|
|
err = db.brk.DoWithAcceptable(func() error {
|
|
|
|
err = db.brk.DoWithAcceptable(func() error {
|
|
|
|
var conn *sql.DB
|
|
|
|
var conn *sql.DB
|
|
|
|
conn, err = getSqlConn(db.driverName, db.datasource)
|
|
|
|
conn, err = db.connProv()
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
logInstanceError(db.datasource, err)
|
|
|
|
logInstanceError(db.datasource, err)
|
|
|
|
return err
|
|
|
|
return err
|
|
|
@ -145,6 +171,10 @@ func (db *commonSqlConn) QueryRowsPartial(v interface{}, q string, args ...inter
|
|
|
|
}, q, args...)
|
|
|
|
}, q, args...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (db *commonSqlConn) RawDB() (*sql.DB, error) {
|
|
|
|
|
|
|
|
return db.connProv()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (db *commonSqlConn) Transact(fn func(Session) error) error {
|
|
|
|
func (db *commonSqlConn) Transact(fn func(Session) error) error {
|
|
|
|
return db.brk.DoWithAcceptable(func() error {
|
|
|
|
return db.brk.DoWithAcceptable(func() error {
|
|
|
|
return transact(db, db.beginTx, fn)
|
|
|
|
return transact(db, db.beginTx, fn)
|
|
|
@ -163,7 +193,7 @@ func (db *commonSqlConn) acceptable(err error) bool {
|
|
|
|
func (db *commonSqlConn) queryRows(scanner func(*sql.Rows) error, q string, args ...interface{}) error {
|
|
|
|
func (db *commonSqlConn) queryRows(scanner func(*sql.Rows) error, q string, args ...interface{}) error {
|
|
|
|
var qerr error
|
|
|
|
var qerr error
|
|
|
|
return db.brk.DoWithAcceptable(func() error {
|
|
|
|
return db.brk.DoWithAcceptable(func() error {
|
|
|
|
conn, err := getSqlConn(db.driverName, db.datasource)
|
|
|
|
conn, err := db.connProv()
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
logInstanceError(db.datasource, err)
|
|
|
|
logInstanceError(db.datasource, err)
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|