|
|
|
@ -42,21 +42,6 @@ type (
|
|
|
|
|
// SqlOption defines the method to customize a sql connection.
|
|
|
|
|
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
|
|
|
|
|
// Because CORBA doesn't support PREPARE, so we need to combine the
|
|
|
|
|
// query arguments into one string and do underlying query without arguments
|
|
|
|
@ -65,7 +50,7 @@ type (
|
|
|
|
|
onError func(context.Context, error)
|
|
|
|
|
beginTx beginnable
|
|
|
|
|
brk breaker.Breaker
|
|
|
|
|
accept func(error) bool
|
|
|
|
|
accept breaker.Acceptable
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
connProvider func() (*sql.DB, error)
|
|
|
|
@ -76,18 +61,6 @@ type (
|
|
|
|
|
Query(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.
|
|
|
|
@ -189,8 +162,10 @@ func (db *commonSqlConn) PrepareCtx(ctx context.Context, query string) (stmt Stm
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stmt = statement{
|
|
|
|
|
query: query,
|
|
|
|
|
stmt: st,
|
|
|
|
|
query: query,
|
|
|
|
|
stmt: st,
|
|
|
|
|
brk: db.brk,
|
|
|
|
|
accept: db.acceptable,
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}, 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,
|
|
|
|
|
q string, args ...any) (err error) {
|
|
|
|
|
var qerr error
|
|
|
|
|
var scanFailed bool
|
|
|
|
|
err = db.brk.DoWithAcceptable(func() error {
|
|
|
|
|
conn, err := db.connProv()
|
|
|
|
|
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 {
|
|
|
|
|
qerr = scanner(rows)
|
|
|
|
|
return qerr
|
|
|
|
|
e := scanner(rows)
|
|
|
|
|
if e != nil {
|
|
|
|
|
scanFailed = true
|
|
|
|
|
}
|
|
|
|
|
return e
|
|
|
|
|
}, q, args...)
|
|
|
|
|
}, func(err error) bool {
|
|
|
|
|
return errors.Is(err, qerr) || db.acceptable(err)
|
|
|
|
|
return scanFailed || db.acceptable(err)
|
|
|
|
|
})
|
|
|
|
|
if errors.Is(err, breaker.ErrServiceUnavailable) {
|
|
|
|
|
metricReqErr.Inc("queryRows", "breaker")
|
|
|
|
@ -333,83 +311,6 @@ func (db *commonSqlConn) queryRows(ctx context.Context, scanner func(*sql.Rows)
|
|
|
|
|
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.
|
|
|
|
|
// acceptable is the func to check if the error can be accepted.
|
|
|
|
|
func WithAcceptable(acceptable func(err error) bool) SqlOption {
|
|
|
|
|