chore: change interface{} to any (#2818)

* chore: change interface{} to any

* chore: update goctl version to 1.5.0

* chore: update goctl deps
This commit is contained in:
Kevin Wan
2023-01-24 16:32:02 +08:00
committed by GitHub
parent 7e0ac77139
commit ae87114282
221 changed files with 1910 additions and 2207 deletions

View File

@@ -64,7 +64,7 @@ func (bi *BulkInserter) Flush() {
}
// Insert inserts given args.
func (bi *BulkInserter) Insert(args ...interface{}) error {
func (bi *BulkInserter) Insert(args ...any) error {
value, err := format(bi.stmt.valueFormat, args...)
if err != nil {
return err
@@ -110,12 +110,12 @@ type dbInserter struct {
resultHandler ResultHandler
}
func (in *dbInserter) AddTask(task interface{}) bool {
func (in *dbInserter) AddTask(task any) bool {
in.values = append(in.values, task.(string))
return len(in.values) >= maxBulkRows
}
func (in *dbInserter) Execute(bulk interface{}) {
func (in *dbInserter) Execute(bulk any) {
values := bulk.([]string)
if len(values) == 0 {
return
@@ -135,7 +135,7 @@ func (in *dbInserter) Execute(bulk interface{}) {
}
}
func (in *dbInserter) RemoveAll() interface{} {
func (in *dbInserter) RemoveAll() any {
values := in.values
in.values = nil
return values

View File

@@ -14,11 +14,11 @@ import (
type mockedConn struct {
query string
args []interface{}
args []any
execErr error
}
func (c *mockedConn) ExecCtx(_ context.Context, query string, args ...interface{}) (sql.Result, error) {
func (c *mockedConn) ExecCtx(_ context.Context, query string, args ...any) (sql.Result, error) {
c.query = query
c.args = args
return nil, c.execErr
@@ -28,19 +28,19 @@ func (c *mockedConn) PrepareCtx(ctx context.Context, query string) (StmtSession,
panic("implement me")
}
func (c *mockedConn) QueryRowCtx(ctx context.Context, v interface{}, query string, args ...interface{}) error {
func (c *mockedConn) QueryRowCtx(ctx context.Context, v any, query string, args ...any) error {
panic("implement me")
}
func (c *mockedConn) QueryRowPartialCtx(ctx context.Context, v interface{}, query string, args ...interface{}) error {
func (c *mockedConn) QueryRowPartialCtx(ctx context.Context, v any, query string, args ...any) error {
panic("implement me")
}
func (c *mockedConn) QueryRowsCtx(ctx context.Context, v interface{}, query string, args ...interface{}) error {
func (c *mockedConn) QueryRowsCtx(ctx context.Context, v any, query string, args ...any) error {
panic("implement me")
}
func (c *mockedConn) QueryRowsPartialCtx(ctx context.Context, v interface{}, query string, args ...interface{}) error {
func (c *mockedConn) QueryRowsPartialCtx(ctx context.Context, v any, query string, args ...any) error {
panic("implement me")
}
@@ -48,7 +48,7 @@ func (c *mockedConn) TransactCtx(ctx context.Context, fn func(context.Context, S
panic("should not called")
}
func (c *mockedConn) Exec(query string, args ...interface{}) (sql.Result, error) {
func (c *mockedConn) Exec(query string, args ...any) (sql.Result, error) {
return c.ExecCtx(context.Background(), query, args...)
}
@@ -56,19 +56,19 @@ func (c *mockedConn) Prepare(query string) (StmtSession, error) {
panic("should not called")
}
func (c *mockedConn) QueryRow(v interface{}, query string, args ...interface{}) error {
func (c *mockedConn) QueryRow(v any, query string, args ...any) error {
panic("should not called")
}
func (c *mockedConn) QueryRowPartial(v interface{}, query string, args ...interface{}) error {
func (c *mockedConn) QueryRowPartial(v any, query string, args ...any) error {
panic("should not called")
}
func (c *mockedConn) QueryRows(v interface{}, query string, args ...interface{}) error {
func (c *mockedConn) QueryRows(v any, query string, args ...any) error {
panic("should not called")
}
func (c *mockedConn) QueryRowsPartial(v interface{}, query string, args ...interface{}) error {
func (c *mockedConn) QueryRowsPartial(v any, query string, args ...any) error {
panic("should not called")
}

View File

@@ -25,13 +25,13 @@ type rowsScanner interface {
Columns() ([]string, error)
Err() error
Next() bool
Scan(v ...interface{}) error
Scan(v ...any) error
}
func getTaggedFieldValueMap(v reflect.Value) (map[string]interface{}, error) {
func getTaggedFieldValueMap(v reflect.Value) (map[string]any, error) {
rt := mapping.Deref(v.Type())
size := rt.NumField()
result := make(map[string]interface{}, size)
result := make(map[string]any, size)
for i := 0; i < size; i++ {
key := parseTagName(rt.Field(i))
@@ -61,7 +61,7 @@ func getTaggedFieldValueMap(v reflect.Value) (map[string]interface{}, error) {
return result, nil
}
func mapStructFieldsIntoSlice(v reflect.Value, columns []string, strict bool) ([]interface{}, error) {
func mapStructFieldsIntoSlice(v reflect.Value, columns []string, strict bool) ([]any, error) {
fields := unwrapFields(v)
if strict && len(columns) < len(fields) {
return nil, ErrNotMatchDestination
@@ -72,7 +72,7 @@ func mapStructFieldsIntoSlice(v reflect.Value, columns []string, strict bool) ([
return nil, err
}
values := make([]interface{}, len(columns))
values := make([]any, len(columns))
if len(taggedMap) == 0 {
for i := 0; i < len(values); i++ {
valueField := fields[i]
@@ -98,7 +98,7 @@ func mapStructFieldsIntoSlice(v reflect.Value, columns []string, strict bool) ([
if tagged, ok := taggedMap[column]; ok {
values[i] = tagged
} else {
var anonymous interface{}
var anonymous any
values[i] = &anonymous
}
}
@@ -117,7 +117,7 @@ func parseTagName(field reflect.StructField) string {
return options[0]
}
func unmarshalRow(v interface{}, scanner rowsScanner, strict bool) error {
func unmarshalRow(v any, scanner rowsScanner, strict bool) error {
if !scanner.Next() {
if err := scanner.Err(); err != nil {
return err
@@ -160,7 +160,7 @@ func unmarshalRow(v interface{}, scanner rowsScanner, strict bool) error {
}
}
func unmarshalRows(v interface{}, scanner rowsScanner, strict bool) error {
func unmarshalRows(v any, scanner rowsScanner, strict bool) error {
rv := reflect.ValueOf(v)
if err := mapping.ValidatePtr(&rv); err != nil {
return err
@@ -180,7 +180,7 @@ func unmarshalRows(v interface{}, scanner rowsScanner, strict bool) error {
rve.Set(reflect.Append(rve, reflect.Indirect(item)))
}
}
fillFn := func(value interface{}) error {
fillFn := func(value any) error {
if rve.CanSet() {
if err := scanner.Scan(value); err != nil {
return err

View File

@@ -1080,6 +1080,6 @@ func (m *mockedScanner) Next() bool {
return false
}
func (m *mockedScanner) Scan(v ...interface{}) error {
func (m *mockedScanner) Scan(v ...any) error {
return m.scanErr
}

View File

@@ -17,18 +17,18 @@ var ErrNotFound = sql.ErrNoRows
type (
// Session stands for raw connections or transaction sessions
Session interface {
Exec(query string, args ...interface{}) (sql.Result, error)
ExecCtx(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
Exec(query string, args ...any) (sql.Result, error)
ExecCtx(ctx context.Context, query string, args ...any) (sql.Result, error)
Prepare(query string) (StmtSession, error)
PrepareCtx(ctx context.Context, query string) (StmtSession, error)
QueryRow(v interface{}, query string, args ...interface{}) error
QueryRowCtx(ctx context.Context, v interface{}, query string, args ...interface{}) error
QueryRowPartial(v interface{}, query string, args ...interface{}) error
QueryRowPartialCtx(ctx context.Context, v interface{}, query string, args ...interface{}) error
QueryRows(v interface{}, query string, args ...interface{}) error
QueryRowsCtx(ctx context.Context, v interface{}, query string, args ...interface{}) error
QueryRowsPartial(v interface{}, query string, args ...interface{}) error
QueryRowsPartialCtx(ctx context.Context, v interface{}, query string, args ...interface{}) error
QueryRow(v any, query string, args ...any) error
QueryRowCtx(ctx context.Context, v any, query string, args ...any) error
QueryRowPartial(v any, query string, args ...any) error
QueryRowPartialCtx(ctx context.Context, v any, query string, args ...any) error
QueryRows(v any, query string, args ...any) error
QueryRowsCtx(ctx context.Context, v any, query string, args ...any) error
QueryRowsPartial(v any, query string, args ...any) error
QueryRowsPartialCtx(ctx context.Context, v any, query string, args ...any) error
}
// SqlConn only stands for raw connections, so Transact method can be called.
@@ -47,16 +47,16 @@ type (
// StmtSession interface represents a session that can be used to execute statements.
StmtSession interface {
Close() error
Exec(args ...interface{}) (sql.Result, error)
ExecCtx(ctx context.Context, args ...interface{}) (sql.Result, error)
QueryRow(v interface{}, args ...interface{}) error
QueryRowCtx(ctx context.Context, v interface{}, args ...interface{}) error
QueryRowPartial(v interface{}, args ...interface{}) error
QueryRowPartialCtx(ctx context.Context, v interface{}, args ...interface{}) error
QueryRows(v interface{}, args ...interface{}) error
QueryRowsCtx(ctx context.Context, v interface{}, args ...interface{}) error
QueryRowsPartial(v interface{}, args ...interface{}) error
QueryRowsPartialCtx(ctx context.Context, v interface{}, args ...interface{}) 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
@@ -73,10 +73,10 @@ type (
connProvider func() (*sql.DB, error)
sessionConn interface {
Exec(query string, args ...interface{}) (sql.Result, error)
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
Query(query string, args ...interface{}) (*sql.Rows, error)
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
Exec(query string, args ...any) (sql.Result, error)
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
Query(query string, args ...any) (*sql.Rows, error)
QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
}
statement struct {
@@ -85,10 +85,10 @@ type (
}
stmtConn interface {
Exec(args ...interface{}) (sql.Result, error)
ExecContext(ctx context.Context, args ...interface{}) (sql.Result, error)
Query(args ...interface{}) (*sql.Rows, error)
QueryContext(ctx context.Context, args ...interface{}) (*sql.Rows, error)
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)
}
)
@@ -131,11 +131,11 @@ func NewSqlConnFromDB(db *sql.DB, opts ...SqlOption) SqlConn {
return conn
}
func (db *commonSqlConn) Exec(q string, args ...interface{}) (result sql.Result, err error) {
func (db *commonSqlConn) Exec(q string, args ...any) (result sql.Result, err error) {
return db.ExecCtx(context.Background(), q, args...)
}
func (db *commonSqlConn) ExecCtx(ctx context.Context, q string, args ...interface{}) (
func (db *commonSqlConn) ExecCtx(ctx context.Context, q string, args ...any) (
result sql.Result, err error) {
ctx, span := startSpan(ctx, "Exec")
defer func() {
@@ -196,12 +196,12 @@ func (db *commonSqlConn) PrepareCtx(ctx context.Context, query string) (stmt Stm
return
}
func (db *commonSqlConn) QueryRow(v interface{}, q string, args ...interface{}) error {
func (db *commonSqlConn) QueryRow(v any, q string, args ...any) error {
return db.QueryRowCtx(context.Background(), v, q, args...)
}
func (db *commonSqlConn) QueryRowCtx(ctx context.Context, v interface{}, q string,
args ...interface{}) (err error) {
func (db *commonSqlConn) QueryRowCtx(ctx context.Context, v any, q string,
args ...any) (err error) {
ctx, span := startSpan(ctx, "QueryRow")
defer func() {
endSpan(span, err)
@@ -212,12 +212,12 @@ func (db *commonSqlConn) QueryRowCtx(ctx context.Context, v interface{}, q strin
}, q, args...)
}
func (db *commonSqlConn) QueryRowPartial(v interface{}, q string, args ...interface{}) error {
func (db *commonSqlConn) QueryRowPartial(v any, q string, args ...any) error {
return db.QueryRowPartialCtx(context.Background(), v, q, args...)
}
func (db *commonSqlConn) QueryRowPartialCtx(ctx context.Context, v interface{},
q string, args ...interface{}) (err error) {
func (db *commonSqlConn) QueryRowPartialCtx(ctx context.Context, v any,
q string, args ...any) (err error) {
ctx, span := startSpan(ctx, "QueryRowPartial")
defer func() {
endSpan(span, err)
@@ -228,12 +228,12 @@ func (db *commonSqlConn) QueryRowPartialCtx(ctx context.Context, v interface{},
}, q, args...)
}
func (db *commonSqlConn) QueryRows(v interface{}, q string, args ...interface{}) error {
func (db *commonSqlConn) QueryRows(v any, q string, args ...any) error {
return db.QueryRowsCtx(context.Background(), v, q, args...)
}
func (db *commonSqlConn) QueryRowsCtx(ctx context.Context, v interface{}, q string,
args ...interface{}) (err error) {
func (db *commonSqlConn) QueryRowsCtx(ctx context.Context, v any, q string,
args ...any) (err error) {
ctx, span := startSpan(ctx, "QueryRows")
defer func() {
endSpan(span, err)
@@ -244,12 +244,12 @@ func (db *commonSqlConn) QueryRowsCtx(ctx context.Context, v interface{}, q stri
}, q, args...)
}
func (db *commonSqlConn) QueryRowsPartial(v interface{}, q string, args ...interface{}) error {
func (db *commonSqlConn) QueryRowsPartial(v any, q string, args ...any) error {
return db.QueryRowsPartialCtx(context.Background(), v, q, args...)
}
func (db *commonSqlConn) QueryRowsPartialCtx(ctx context.Context, v interface{},
q string, args ...interface{}) (err error) {
func (db *commonSqlConn) QueryRowsPartialCtx(ctx context.Context, v any,
q string, args ...any) (err error) {
ctx, span := startSpan(ctx, "QueryRowsPartial")
defer func() {
endSpan(span, err)
@@ -296,7 +296,7 @@ func (db *commonSqlConn) acceptable(err error) bool {
}
func (db *commonSqlConn) queryRows(ctx context.Context, scanner func(*sql.Rows) error,
q string, args ...interface{}) (err error) {
q string, args ...any) (err error) {
var qerr error
err = db.brk.DoWithAcceptable(func() error {
conn, err := db.connProv()
@@ -323,11 +323,11 @@ func (s statement) Close() error {
return s.stmt.Close()
}
func (s statement) Exec(args ...interface{}) (sql.Result, error) {
func (s statement) Exec(args ...any) (sql.Result, error) {
return s.ExecCtx(context.Background(), args...)
}
func (s statement) ExecCtx(ctx context.Context, args ...interface{}) (result sql.Result, err error) {
func (s statement) ExecCtx(ctx context.Context, args ...any) (result sql.Result, err error) {
ctx, span := startSpan(ctx, "Exec")
defer func() {
endSpan(span, err)
@@ -336,11 +336,11 @@ func (s statement) ExecCtx(ctx context.Context, args ...interface{}) (result sql
return execStmt(ctx, s.stmt, s.query, args...)
}
func (s statement) QueryRow(v interface{}, args ...interface{}) error {
func (s statement) QueryRow(v any, args ...any) error {
return s.QueryRowCtx(context.Background(), v, args...)
}
func (s statement) QueryRowCtx(ctx context.Context, v interface{}, args ...interface{}) (err error) {
func (s statement) QueryRowCtx(ctx context.Context, v any, args ...any) (err error) {
ctx, span := startSpan(ctx, "QueryRow")
defer func() {
endSpan(span, err)
@@ -351,11 +351,11 @@ func (s statement) QueryRowCtx(ctx context.Context, v interface{}, args ...inter
}, s.query, args...)
}
func (s statement) QueryRowPartial(v interface{}, args ...interface{}) error {
func (s statement) QueryRowPartial(v any, args ...any) error {
return s.QueryRowPartialCtx(context.Background(), v, args...)
}
func (s statement) QueryRowPartialCtx(ctx context.Context, v interface{}, args ...interface{}) (err error) {
func (s statement) QueryRowPartialCtx(ctx context.Context, v any, args ...any) (err error) {
ctx, span := startSpan(ctx, "QueryRowPartial")
defer func() {
endSpan(span, err)
@@ -366,11 +366,11 @@ func (s statement) QueryRowPartialCtx(ctx context.Context, v interface{}, args .
}, s.query, args...)
}
func (s statement) QueryRows(v interface{}, args ...interface{}) error {
func (s statement) QueryRows(v any, args ...any) error {
return s.QueryRowsCtx(context.Background(), v, args...)
}
func (s statement) QueryRowsCtx(ctx context.Context, v interface{}, args ...interface{}) (err error) {
func (s statement) QueryRowsCtx(ctx context.Context, v any, args ...any) (err error) {
ctx, span := startSpan(ctx, "QueryRows")
defer func() {
endSpan(span, err)
@@ -381,11 +381,11 @@ func (s statement) QueryRowsCtx(ctx context.Context, v interface{}, args ...inte
}, s.query, args...)
}
func (s statement) QueryRowsPartial(v interface{}, args ...interface{}) error {
func (s statement) QueryRowsPartial(v any, args ...any) error {
return s.QueryRowsPartialCtx(context.Background(), v, args...)
}
func (s statement) QueryRowsPartialCtx(ctx context.Context, v interface{}, args ...interface{}) (err error) {
func (s statement) QueryRowsPartialCtx(ctx context.Context, v any, args ...any) (err error) {
ctx, span := startSpan(ctx, "QueryRowsPartial")
defer func() {
endSpan(span, err)

View File

@@ -34,7 +34,7 @@ func SetSlowThreshold(threshold time.Duration) {
slowThreshold.Set(threshold)
}
func exec(ctx context.Context, conn sessionConn, q string, args ...interface{}) (sql.Result, error) {
func exec(ctx context.Context, conn sessionConn, q string, args ...any) (sql.Result, error) {
guard := newGuard("exec")
if err := guard.start(q, args...); err != nil {
return nil, err
@@ -46,7 +46,7 @@ func exec(ctx context.Context, conn sessionConn, q string, args ...interface{})
return result, err
}
func execStmt(ctx context.Context, conn stmtConn, q string, args ...interface{}) (sql.Result, error) {
func execStmt(ctx context.Context, conn stmtConn, q string, args ...any) (sql.Result, error) {
guard := newGuard("execStmt")
if err := guard.start(q, args...); err != nil {
return nil, err
@@ -59,7 +59,7 @@ func execStmt(ctx context.Context, conn stmtConn, q string, args ...interface{})
}
func query(ctx context.Context, conn sessionConn, scanner func(*sql.Rows) error,
q string, args ...interface{}) error {
q string, args ...any) error {
guard := newGuard("query")
if err := guard.start(q, args...); err != nil {
return err
@@ -76,7 +76,7 @@ func query(ctx context.Context, conn sessionConn, scanner func(*sql.Rows) error,
}
func queryStmt(ctx context.Context, conn stmtConn, scanner func(*sql.Rows) error,
q string, args ...interface{}) error {
q string, args ...any) error {
guard := newGuard("queryStmt")
if err := guard.start(q, args...); err != nil {
return err
@@ -94,7 +94,7 @@ func queryStmt(ctx context.Context, conn stmtConn, scanner func(*sql.Rows) error
type (
sqlGuard interface {
start(q string, args ...interface{}) error
start(q string, args ...any) error
finish(ctx context.Context, err error)
}
@@ -117,7 +117,7 @@ func newGuard(command string) sqlGuard {
return nilGuard{}
}
func (n nilGuard) start(_ string, _ ...interface{}) error {
func (n nilGuard) start(_ string, _ ...any) error {
return nil
}
@@ -139,7 +139,7 @@ func (e *realSqlGuard) finish(ctx context.Context, err error) {
metricReqDur.Observe(int64(duration/time.Millisecond), e.command)
}
func (e *realSqlGuard) start(q string, args ...interface{}) error {
func (e *realSqlGuard) start(q string, args ...any) error {
stmt, err := format(q, args...)
if err != nil {
return err

View File

@@ -16,7 +16,7 @@ func TestStmt_exec(t *testing.T) {
tests := []struct {
name string
query string
args []interface{}
args []any
delay bool
hasError bool
err error
@@ -26,28 +26,28 @@ func TestStmt_exec(t *testing.T) {
{
name: "normal",
query: "select user from users where id=?",
args: []interface{}{1},
args: []any{1},
lastInsertId: 1,
rowsAffected: 2,
},
{
name: "exec error",
query: "select user from users where id=?",
args: []interface{}{1},
args: []any{1},
hasError: true,
err: errors.New("exec"),
},
{
name: "exec more args error",
query: "select user from users where id=? and name=?",
args: []interface{}{1},
args: []any{1},
hasError: true,
err: errors.New("exec"),
},
{
name: "slowcall",
query: "select user from users where id=?",
args: []interface{}{1},
args: []any{1},
delay: true,
lastInsertId: 1,
rowsAffected: 2,
@@ -56,8 +56,8 @@ func TestStmt_exec(t *testing.T) {
for _, test := range tests {
test := test
fns := []func(args ...interface{}) (sql.Result, error){
func(args ...interface{}) (sql.Result, error) {
fns := []func(args ...any) (sql.Result, error){
func(args ...any) (sql.Result, error) {
return exec(context.Background(), &mockedSessionConn{
lastInsertId: test.lastInsertId,
rowsAffected: test.rowsAffected,
@@ -65,7 +65,7 @@ func TestStmt_exec(t *testing.T) {
delay: test.delay,
}, test.query, args...)
},
func(args ...interface{}) (sql.Result, error) {
func(args ...any) (sql.Result, error) {
return execStmt(context.Background(), &mockedStmtConn{
lastInsertId: test.lastInsertId,
rowsAffected: test.rowsAffected,
@@ -102,7 +102,7 @@ func TestStmt_query(t *testing.T) {
tests := []struct {
name string
query string
args []interface{}
args []any
delay bool
hasError bool
err error
@@ -110,34 +110,34 @@ func TestStmt_query(t *testing.T) {
{
name: "normal",
query: "select user from users where id=?",
args: []interface{}{1},
args: []any{1},
},
{
name: "query error",
query: "select user from users where id=?",
args: []interface{}{1},
args: []any{1},
hasError: true,
err: errors.New("exec"),
},
{
name: "query more args error",
query: "select user from users where id=? and name=?",
args: []interface{}{1},
args: []any{1},
hasError: true,
err: errors.New("exec"),
},
{
name: "slowcall",
query: "select user from users where id=?",
args: []interface{}{1},
args: []any{1},
delay: true,
},
}
for _, test := range tests {
test := test
fns := []func(args ...interface{}) error{
func(args ...interface{}) error {
fns := []func(args ...any) error{
func(args ...any) error {
return query(context.Background(), &mockedSessionConn{
err: test.err,
delay: test.delay,
@@ -145,7 +145,7 @@ func TestStmt_query(t *testing.T) {
return nil
}, test.query, args...)
},
func(args ...interface{}) error {
func(args ...any) error {
return queryStmt(context.Background(), &mockedStmtConn{
err: test.err,
delay: test.delay,
@@ -226,11 +226,11 @@ type mockedSessionConn struct {
delay bool
}
func (m *mockedSessionConn) Exec(query string, args ...interface{}) (sql.Result, error) {
func (m *mockedSessionConn) Exec(query string, args ...any) (sql.Result, error) {
return m.ExecContext(context.Background(), query, args...)
}
func (m *mockedSessionConn) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
func (m *mockedSessionConn) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) {
if m.delay {
time.Sleep(defaultSlowThreshold + time.Millisecond)
}
@@ -240,11 +240,11 @@ func (m *mockedSessionConn) ExecContext(ctx context.Context, query string, args
}, m.err
}
func (m *mockedSessionConn) Query(query string, args ...interface{}) (*sql.Rows, error) {
func (m *mockedSessionConn) Query(query string, args ...any) (*sql.Rows, error) {
return m.QueryContext(context.Background(), query, args...)
}
func (m *mockedSessionConn) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) {
func (m *mockedSessionConn) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error) {
if m.delay {
time.Sleep(defaultSlowThreshold + time.Millisecond)
}
@@ -263,11 +263,11 @@ type mockedStmtConn struct {
delay bool
}
func (m *mockedStmtConn) Exec(args ...interface{}) (sql.Result, error) {
func (m *mockedStmtConn) Exec(args ...any) (sql.Result, error) {
return m.ExecContext(context.Background(), args...)
}
func (m *mockedStmtConn) ExecContext(_ context.Context, _ ...interface{}) (sql.Result, error) {
func (m *mockedStmtConn) ExecContext(_ context.Context, _ ...any) (sql.Result, error) {
if m.delay {
time.Sleep(defaultSlowThreshold + time.Millisecond)
}
@@ -277,11 +277,11 @@ func (m *mockedStmtConn) ExecContext(_ context.Context, _ ...interface{}) (sql.R
}, m.err
}
func (m *mockedStmtConn) Query(args ...interface{}) (*sql.Rows, error) {
func (m *mockedStmtConn) Query(args ...any) (*sql.Rows, error) {
return m.QueryContext(context.Background(), args...)
}
func (m *mockedStmtConn) QueryContext(_ context.Context, _ ...interface{}) (*sql.Rows, error) {
func (m *mockedStmtConn) QueryContext(_ context.Context, _ ...any) (*sql.Rows, error) {
if m.delay {
time.Sleep(defaultSlowThreshold + time.Millisecond)
}

View File

@@ -26,11 +26,11 @@ func NewSessionFromTx(tx *sql.Tx) Session {
return txSession{Tx: tx}
}
func (t txSession) Exec(q string, args ...interface{}) (sql.Result, error) {
func (t txSession) Exec(q string, args ...any) (sql.Result, error) {
return t.ExecCtx(context.Background(), q, args...)
}
func (t txSession) ExecCtx(ctx context.Context, q string, args ...interface{}) (result sql.Result, err error) {
func (t txSession) ExecCtx(ctx context.Context, q string, args ...any) (result sql.Result, err error) {
ctx, span := startSpan(ctx, "Exec")
defer func() {
endSpan(span, err)
@@ -62,11 +62,11 @@ func (t txSession) PrepareCtx(ctx context.Context, q string) (stmtSession StmtSe
}, nil
}
func (t txSession) QueryRow(v interface{}, q string, args ...interface{}) error {
func (t txSession) QueryRow(v any, q string, args ...any) error {
return t.QueryRowCtx(context.Background(), v, q, args...)
}
func (t txSession) QueryRowCtx(ctx context.Context, v interface{}, q string, args ...interface{}) (err error) {
func (t txSession) QueryRowCtx(ctx context.Context, v any, q string, args ...any) (err error) {
ctx, span := startSpan(ctx, "QueryRow")
defer func() {
endSpan(span, err)
@@ -77,12 +77,12 @@ func (t txSession) QueryRowCtx(ctx context.Context, v interface{}, q string, arg
}, q, args...)
}
func (t txSession) QueryRowPartial(v interface{}, q string, args ...interface{}) error {
func (t txSession) QueryRowPartial(v any, q string, args ...any) error {
return t.QueryRowPartialCtx(context.Background(), v, q, args...)
}
func (t txSession) QueryRowPartialCtx(ctx context.Context, v interface{}, q string,
args ...interface{}) (err error) {
func (t txSession) QueryRowPartialCtx(ctx context.Context, v any, q string,
args ...any) (err error) {
ctx, span := startSpan(ctx, "QueryRowPartial")
defer func() {
endSpan(span, err)
@@ -93,11 +93,11 @@ func (t txSession) QueryRowPartialCtx(ctx context.Context, v interface{}, q stri
}, q, args...)
}
func (t txSession) QueryRows(v interface{}, q string, args ...interface{}) error {
func (t txSession) QueryRows(v any, q string, args ...any) error {
return t.QueryRowsCtx(context.Background(), v, q, args...)
}
func (t txSession) QueryRowsCtx(ctx context.Context, v interface{}, q string, args ...interface{}) (err error) {
func (t txSession) QueryRowsCtx(ctx context.Context, v any, q string, args ...any) (err error) {
ctx, span := startSpan(ctx, "QueryRows")
defer func() {
endSpan(span, err)
@@ -108,12 +108,12 @@ func (t txSession) QueryRowsCtx(ctx context.Context, v interface{}, q string, ar
}, q, args...)
}
func (t txSession) QueryRowsPartial(v interface{}, q string, args ...interface{}) error {
func (t txSession) QueryRowsPartial(v any, q string, args ...any) error {
return t.QueryRowsPartialCtx(context.Background(), v, q, args...)
}
func (t txSession) QueryRowsPartialCtx(ctx context.Context, v interface{}, q string,
args ...interface{}) (err error) {
func (t txSession) QueryRowsPartialCtx(ctx context.Context, v any, q string,
args ...any) (err error) {
ctx, span := startSpan(ctx, "QueryRowsPartial")
defer func() {
endSpan(span, err)

View File

@@ -23,11 +23,11 @@ func (mt *mockTx) Commit() error {
return nil
}
func (mt *mockTx) Exec(q string, args ...interface{}) (sql.Result, error) {
func (mt *mockTx) Exec(q string, args ...any) (sql.Result, error) {
return nil, nil
}
func (mt *mockTx) ExecCtx(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
func (mt *mockTx) ExecCtx(ctx context.Context, query string, args ...any) (sql.Result, error) {
return nil, nil
}
@@ -39,35 +39,35 @@ func (mt *mockTx) PrepareCtx(ctx context.Context, query string) (StmtSession, er
return nil, nil
}
func (mt *mockTx) QueryRow(v interface{}, q string, args ...interface{}) error {
func (mt *mockTx) QueryRow(v any, q string, args ...any) error {
return nil
}
func (mt *mockTx) QueryRowCtx(ctx context.Context, v interface{}, query string, args ...interface{}) error {
func (mt *mockTx) QueryRowCtx(ctx context.Context, v any, query string, args ...any) error {
return nil
}
func (mt *mockTx) QueryRowPartial(v interface{}, q string, args ...interface{}) error {
func (mt *mockTx) QueryRowPartial(v any, q string, args ...any) error {
return nil
}
func (mt *mockTx) QueryRowPartialCtx(ctx context.Context, v interface{}, query string, args ...interface{}) error {
func (mt *mockTx) QueryRowPartialCtx(ctx context.Context, v any, query string, args ...any) error {
return nil
}
func (mt *mockTx) QueryRows(v interface{}, q string, args ...interface{}) error {
func (mt *mockTx) QueryRows(v any, q string, args ...any) error {
return nil
}
func (mt *mockTx) QueryRowsCtx(ctx context.Context, v interface{}, query string, args ...interface{}) error {
func (mt *mockTx) QueryRowsCtx(ctx context.Context, v any, query string, args ...any) error {
return nil
}
func (mt *mockTx) QueryRowsPartial(v interface{}, q string, args ...interface{}) error {
func (mt *mockTx) QueryRowsPartial(v any, q string, args ...any) error {
return nil
}
func (mt *mockTx) QueryRowsPartialCtx(ctx context.Context, v interface{}, query string, args ...interface{}) error {
func (mt *mockTx) QueryRowsPartialCtx(ctx context.Context, v any, query string, args ...any) error {
return nil
}

View File

@@ -51,7 +51,7 @@ func escape(input string) string {
return b.String()
}
func format(query string, args ...interface{}) (string, error) {
func format(query string, args ...any) (string, error) {
numArgs := len(args)
if numArgs == 0 {
return query, nil
@@ -141,7 +141,7 @@ func logSqlError(ctx context.Context, stmt string, err error) {
}
}
func writeValue(buf *strings.Builder, arg interface{}) {
func writeValue(buf *strings.Builder, arg any) {
switch v := arg.(type) {
case bool:
if v {

View File

@@ -34,92 +34,92 @@ func TestFormat(t *testing.T) {
tests := []struct {
name string
query string
args []interface{}
args []any
expect string
hasErr bool
}{
{
name: "mysql normal",
query: "select name, age from users where bool=? and phone=?",
args: []interface{}{true, "133"},
args: []any{true, "133"},
expect: "select name, age from users where bool=1 and phone='133'",
},
{
name: "mysql normal",
query: "select name, age from users where bool=? and phone=?",
args: []interface{}{false, "133"},
args: []any{false, "133"},
expect: "select name, age from users where bool=0 and phone='133'",
},
{
name: "pg normal",
query: "select name, age from users where bool=$1 and phone=$2",
args: []interface{}{true, "133"},
args: []any{true, "133"},
expect: "select name, age from users where bool=1 and phone='133'",
},
{
name: "pg normal reverse",
query: "select name, age from users where bool=$2 and phone=$1",
args: []interface{}{"133", false},
args: []any{"133", false},
expect: "select name, age from users where bool=0 and phone='133'",
},
{
name: "pg error not number",
query: "select name, age from users where bool=$a and phone=$1",
args: []interface{}{"133", false},
args: []any{"133", false},
hasErr: true,
},
{
name: "pg error more args",
query: "select name, age from users where bool=$2 and phone=$1 and nickname=$3",
args: []interface{}{"133", false},
args: []any{"133", false},
hasErr: true,
},
{
name: "oracle normal",
query: "select name, age from users where bool=:1 and phone=:2",
args: []interface{}{true, "133"},
args: []any{true, "133"},
expect: "select name, age from users where bool=1 and phone='133'",
},
{
name: "oracle normal reverse",
query: "select name, age from users where bool=:2 and phone=:1",
args: []interface{}{"133", false},
args: []any{"133", false},
expect: "select name, age from users where bool=0 and phone='133'",
},
{
name: "oracle error not number",
query: "select name, age from users where bool=:a and phone=:1",
args: []interface{}{"133", false},
args: []any{"133", false},
hasErr: true,
},
{
name: "oracle error more args",
query: "select name, age from users where bool=:2 and phone=:1 and nickname=:3",
args: []interface{}{"133", false},
args: []any{"133", false},
hasErr: true,
},
{
name: "select with date",
query: "select * from user where date='2006-01-02 15:04:05' and name=:1",
args: []interface{}{"foo"},
args: []any{"foo"},
expect: "select * from user where date='2006-01-02 15:04:05' and name='foo'",
},
{
name: "select with date and escape",
query: `select * from user where date=' 2006-01-02 15:04:05 \'' and name=:1`,
args: []interface{}{"foo"},
args: []any{"foo"},
expect: `select * from user where date=' 2006-01-02 15:04:05 \'' and name='foo'`,
},
{
name: "select with date and bad arg",
query: `select * from user where date='2006-01-02 15:04:05 \'' and name=:a`,
args: []interface{}{"foo"},
args: []any{"foo"},
hasErr: true,
},
{
name: "select with date and escape error",
query: `select * from user where date='2006-01-02 15:04:05 \`,
args: []interface{}{"foo"},
args: []any{"foo"},
hasErr: true,
},
}