|
|
@ -42,21 +42,6 @@ type (
|
|
|
|
// SqlOption defines the method to customize a sql connection.
|
|
|
|
// SqlOption defines the method to customize a sql connection.
|
|
|
|
SqlOption func(*commonSqlConn)
|
|
|
|
SqlOption func(*commonSqlConn)
|
|
|
|
|
|
|
|
|
|
|
|
// StmtSession interface represents a session that can be used to execute statements.
|
|
|
|
|
|
|
|
StmtSession interface {
|
|
|
|
|
|
|
|
Close() error
|
|
|
|
|
|
|
|
Exec(args ...any) (sql.Result, error)
|
|
|
|
|
|
|
|
ExecCtx(ctx context.Context, args ...any) (sql.Result, error)
|
|
|
|
|
|
|
|
QueryRow(v any, args ...any) error
|
|
|
|
|
|
|
|
QueryRowCtx(ctx context.Context, v any, args ...any) error
|
|
|
|
|
|
|
|
QueryRowPartial(v any, args ...any) error
|
|
|
|
|
|
|
|
QueryRowPartialCtx(ctx context.Context, v any, args ...any) error
|
|
|
|
|
|
|
|
QueryRows(v any, args ...any) error
|
|
|
|
|
|
|
|
QueryRowsCtx(ctx context.Context, v any, args ...any) error
|
|
|
|
|
|
|
|
QueryRowsPartial(v any, args ...any) error
|
|
|
|
|
|
|
|
QueryRowsPartialCtx(ctx context.Context, v any, args ...any) error
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// thread-safe
|
|
|
|
// thread-safe
|
|
|
|
// 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
|
|
|
@ -65,7 +50,7 @@ type (
|
|
|
|
onError func(context.Context, error)
|
|
|
|
onError func(context.Context, error)
|
|
|
|
beginTx beginnable
|
|
|
|
beginTx beginnable
|
|
|
|
brk breaker.Breaker
|
|
|
|
brk breaker.Breaker
|
|
|
|
accept func(error) bool
|
|
|
|
accept breaker.Acceptable
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
connProvider func() (*sql.DB, error)
|
|
|
|
connProvider func() (*sql.DB, error)
|
|
|
@ -76,18 +61,6 @@ type (
|
|
|
|
Query(query string, args ...any) (*sql.Rows, error)
|
|
|
|
Query(query string, args ...any) (*sql.Rows, error)
|
|
|
|
QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
|
|
|
|
QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
statement struct {
|
|
|
|
|
|
|
|
query string
|
|
|
|
|
|
|
|
stmt *sql.Stmt
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
stmtConn interface {
|
|
|
|
|
|
|
|
Exec(args ...any) (sql.Result, error)
|
|
|
|
|
|
|
|
ExecContext(ctx context.Context, args ...any) (sql.Result, error)
|
|
|
|
|
|
|
|
Query(args ...any) (*sql.Rows, error)
|
|
|
|
|
|
|
|
QueryContext(ctx context.Context, args ...any) (*sql.Rows, error)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// NewSqlConn returns a SqlConn with given driver name and datasource.
|
|
|
|
// NewSqlConn returns a SqlConn with given driver name and datasource.
|
|
|
@ -191,6 +164,8 @@ func (db *commonSqlConn) PrepareCtx(ctx context.Context, query string) (stmt Stm
|
|
|
|
stmt = statement{
|
|
|
|
stmt = statement{
|
|
|
|
query: query,
|
|
|
|
query: query,
|
|
|
|
stmt: st,
|
|
|
|
stmt: st,
|
|
|
|
|
|
|
|
brk: db.brk,
|
|
|
|
|
|
|
|
accept: db.acceptable,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}, db.acceptable)
|
|
|
|
}, db.acceptable)
|
|
|
@ -311,7 +286,7 @@ func (db *commonSqlConn) acceptable(err error) bool {
|
|
|
|
|
|
|
|
|
|
|
|
func (db *commonSqlConn) queryRows(ctx context.Context, scanner func(*sql.Rows) error,
|
|
|
|
func (db *commonSqlConn) queryRows(ctx context.Context, scanner func(*sql.Rows) error,
|
|
|
|
q string, args ...any) (err error) {
|
|
|
|
q string, args ...any) (err error) {
|
|
|
|
var qerr error
|
|
|
|
var scanFailed bool
|
|
|
|
err = db.brk.DoWithAcceptable(func() error {
|
|
|
|
err = db.brk.DoWithAcceptable(func() error {
|
|
|
|
conn, err := db.connProv()
|
|
|
|
conn, err := db.connProv()
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
@ -320,11 +295,14 @@ func (db *commonSqlConn) queryRows(ctx context.Context, scanner func(*sql.Rows)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return query(ctx, conn, func(rows *sql.Rows) error {
|
|
|
|
return query(ctx, conn, func(rows *sql.Rows) error {
|
|
|
|
qerr = scanner(rows)
|
|
|
|
e := scanner(rows)
|
|
|
|
return qerr
|
|
|
|
if e != nil {
|
|
|
|
|
|
|
|
scanFailed = true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return e
|
|
|
|
}, q, args...)
|
|
|
|
}, q, args...)
|
|
|
|
}, func(err error) bool {
|
|
|
|
}, func(err error) bool {
|
|
|
|
return errors.Is(err, qerr) || db.acceptable(err)
|
|
|
|
return scanFailed || db.acceptable(err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
if errors.Is(err, breaker.ErrServiceUnavailable) {
|
|
|
|
if errors.Is(err, breaker.ErrServiceUnavailable) {
|
|
|
|
metricReqErr.Inc("queryRows", "breaker")
|
|
|
|
metricReqErr.Inc("queryRows", "breaker")
|
|
|
@ -333,83 +311,6 @@ func (db *commonSqlConn) queryRows(ctx context.Context, scanner func(*sql.Rows)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s statement) Close() error {
|
|
|
|
|
|
|
|
return s.stmt.Close()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (s statement) Exec(args ...any) (sql.Result, error) {
|
|
|
|
|
|
|
|
return s.ExecCtx(context.Background(), args...)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (s statement) ExecCtx(ctx context.Context, args ...any) (result sql.Result, err error) {
|
|
|
|
|
|
|
|
ctx, span := startSpan(ctx, "Exec")
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
|
|
|
|
endSpan(span, err)
|
|
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return execStmt(ctx, s.stmt, s.query, args...)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (s statement) QueryRow(v any, args ...any) error {
|
|
|
|
|
|
|
|
return s.QueryRowCtx(context.Background(), v, args...)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (s statement) QueryRowCtx(ctx context.Context, v any, args ...any) (err error) {
|
|
|
|
|
|
|
|
ctx, span := startSpan(ctx, "QueryRow")
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
|
|
|
|
endSpan(span, err)
|
|
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return queryStmt(ctx, s.stmt, func(rows *sql.Rows) error {
|
|
|
|
|
|
|
|
return unmarshalRow(v, rows, true)
|
|
|
|
|
|
|
|
}, s.query, args...)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (s statement) QueryRowPartial(v any, args ...any) error {
|
|
|
|
|
|
|
|
return s.QueryRowPartialCtx(context.Background(), v, args...)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (s statement) QueryRowPartialCtx(ctx context.Context, v any, args ...any) (err error) {
|
|
|
|
|
|
|
|
ctx, span := startSpan(ctx, "QueryRowPartial")
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
|
|
|
|
endSpan(span, err)
|
|
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return queryStmt(ctx, s.stmt, func(rows *sql.Rows) error {
|
|
|
|
|
|
|
|
return unmarshalRow(v, rows, false)
|
|
|
|
|
|
|
|
}, s.query, args...)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (s statement) QueryRows(v any, args ...any) error {
|
|
|
|
|
|
|
|
return s.QueryRowsCtx(context.Background(), v, args...)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (s statement) QueryRowsCtx(ctx context.Context, v any, args ...any) (err error) {
|
|
|
|
|
|
|
|
ctx, span := startSpan(ctx, "QueryRows")
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
|
|
|
|
endSpan(span, err)
|
|
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return queryStmt(ctx, s.stmt, func(rows *sql.Rows) error {
|
|
|
|
|
|
|
|
return unmarshalRows(v, rows, true)
|
|
|
|
|
|
|
|
}, s.query, args...)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (s statement) QueryRowsPartial(v any, args ...any) error {
|
|
|
|
|
|
|
|
return s.QueryRowsPartialCtx(context.Background(), v, args...)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (s statement) QueryRowsPartialCtx(ctx context.Context, v any, args ...any) (err error) {
|
|
|
|
|
|
|
|
ctx, span := startSpan(ctx, "QueryRowsPartial")
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
|
|
|
|
endSpan(span, err)
|
|
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return queryStmt(ctx, s.stmt, func(rows *sql.Rows) error {
|
|
|
|
|
|
|
|
return unmarshalRows(v, rows, false)
|
|
|
|
|
|
|
|
}, s.query, args...)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// WithAcceptable returns a SqlOption that setting the acceptable function.
|
|
|
|
// WithAcceptable returns a SqlOption that setting the acceptable function.
|
|
|
|
// acceptable is the func to check if the error can be accepted.
|
|
|
|
// acceptable is the func to check if the error can be accepted.
|
|
|
|
func WithAcceptable(acceptable func(err error) bool) SqlOption {
|
|
|
|
func WithAcceptable(acceptable func(err error) bool) SqlOption {
|
|
|
|