mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
refract package gdb; add selected & affected count to logging
This commit is contained in:
@ -77,21 +77,21 @@ type DB interface {
|
||||
// Query APIs.
|
||||
// ===========================================================================
|
||||
|
||||
Query(sql string, args ...interface{}) (*sql.Rows, error) // See Core.Query.
|
||||
Exec(sql string, args ...interface{}) (sql.Result, error) // See Core.Exec.
|
||||
Prepare(sql string, execOnMaster ...bool) (*Stmt, error) // See Core.Prepare.
|
||||
Query(ctx context.Context, sql string, args ...interface{}) (Result, error) // See Core.Query.
|
||||
Exec(ctx context.Context, sql string, args ...interface{}) (sql.Result, error) // See Core.Exec.
|
||||
Prepare(ctx context.Context, sql string, execOnMaster ...bool) (*Stmt, error) // See Core.Prepare.
|
||||
|
||||
// ===========================================================================
|
||||
// Common APIs for CURD.
|
||||
// ===========================================================================
|
||||
|
||||
Insert(table string, data interface{}, batch ...int) (sql.Result, error) // See Core.Insert.
|
||||
InsertIgnore(table string, data interface{}, batch ...int) (sql.Result, error) // See Core.InsertIgnore.
|
||||
InsertAndGetId(table string, data interface{}, batch ...int) (int64, error) // See Core.InsertAndGetId.
|
||||
Replace(table string, data interface{}, batch ...int) (sql.Result, error) // See Core.Replace.
|
||||
Save(table string, data interface{}, batch ...int) (sql.Result, error) // See Core.Save.
|
||||
Update(table string, data interface{}, condition interface{}, args ...interface{}) (sql.Result, error) // See Core.Update.
|
||||
Delete(table string, condition interface{}, args ...interface{}) (sql.Result, error) // See Core.Delete.
|
||||
Insert(ctx context.Context, table string, data interface{}, batch ...int) (sql.Result, error) // See Core.Insert.
|
||||
InsertIgnore(ctx context.Context, table string, data interface{}, batch ...int) (sql.Result, error) // See Core.InsertIgnore.
|
||||
InsertAndGetId(ctx context.Context, table string, data interface{}, batch ...int) (int64, error) // See Core.InsertAndGetId.
|
||||
Replace(ctx context.Context, table string, data interface{}, batch ...int) (sql.Result, error) // See Core.Replace.
|
||||
Save(ctx context.Context, table string, data interface{}, batch ...int) (sql.Result, error) // See Core.Save.
|
||||
Update(ctx context.Context, table string, data interface{}, condition interface{}, args ...interface{}) (sql.Result, error) // See Core.Update.
|
||||
Delete(ctx context.Context, table string, condition interface{}, args ...interface{}) (sql.Result, error) // See Core.Delete.
|
||||
|
||||
// ===========================================================================
|
||||
// Internal APIs for CURD, which can be overwritten by custom CURD implements.
|
||||
@ -101,7 +101,7 @@ type DB interface {
|
||||
DoInsert(ctx context.Context, link Link, table string, data List, option DoInsertOption) (result sql.Result, err error) // See Core.DoInsert.
|
||||
DoUpdate(ctx context.Context, link Link, table string, data interface{}, condition string, args ...interface{}) (result sql.Result, err error) // See Core.DoUpdate.
|
||||
DoDelete(ctx context.Context, link Link, table string, condition string, args ...interface{}) (result sql.Result, err error) // See Core.DoDelete.
|
||||
DoQuery(ctx context.Context, link Link, sql string, args ...interface{}) (rows *sql.Rows, err error) // See Core.DoQuery.
|
||||
DoQuery(ctx context.Context, link Link, sql string, args ...interface{}) (result Result, err error) // See Core.DoQuery.
|
||||
DoExec(ctx context.Context, link Link, sql string, args ...interface{}) (result sql.Result, err error) // See Core.DoExec.
|
||||
DoCommit(ctx context.Context, link Link, sql string, args []interface{}) (newSql string, newArgs []interface{}, err error) // See Core.DoCommit.
|
||||
DoPrepare(ctx context.Context, link Link, sql string) (*Stmt, error) // See Core.DoPrepare.
|
||||
@ -110,14 +110,14 @@ type DB interface {
|
||||
// Query APIs for convenience purpose.
|
||||
// ===========================================================================
|
||||
|
||||
GetAll(sql string, args ...interface{}) (Result, error) // See Core.GetAll.
|
||||
GetOne(sql string, args ...interface{}) (Record, error) // See Core.GetOne.
|
||||
GetValue(sql string, args ...interface{}) (Value, error) // See Core.GetValue.
|
||||
GetArray(sql string, args ...interface{}) ([]Value, error) // See Core.GetArray.
|
||||
GetCount(sql string, args ...interface{}) (int, error) // See Core.GetCount.
|
||||
GetScan(objPointer interface{}, sql string, args ...interface{}) error // See Core.GetScan.
|
||||
Union(unions ...*Model) *Model // See Core.Union.
|
||||
UnionAll(unions ...*Model) *Model // See Core.UnionAll.
|
||||
GetAll(ctx context.Context, sql string, args ...interface{}) (Result, error) // See Core.GetAll.
|
||||
GetOne(ctx context.Context, sql string, args ...interface{}) (Record, error) // See Core.GetOne.
|
||||
GetValue(ctx context.Context, sql string, args ...interface{}) (Value, error) // See Core.GetValue.
|
||||
GetArray(ctx context.Context, sql string, args ...interface{}) ([]Value, error) // See Core.GetArray.
|
||||
GetCount(ctx context.Context, sql string, args ...interface{}) (int, error) // See Core.GetCount.
|
||||
GetScan(ctx context.Context, objPointer interface{}, sql string, args ...interface{}) error // See Core.GetScan.
|
||||
Union(unions ...*Model) *Model // See Core.Union.
|
||||
UnionAll(unions ...*Model) *Model // See Core.UnionAll.
|
||||
|
||||
// ===========================================================================
|
||||
// Master/Slave specification support.
|
||||
@ -137,7 +137,7 @@ type DB interface {
|
||||
// Transaction.
|
||||
// ===========================================================================
|
||||
|
||||
Begin() (*TX, error) // See Core.Begin.
|
||||
Begin(ctx context.Context) (*TX, error) // See Core.Begin.
|
||||
Transaction(ctx context.Context, f func(ctx context.Context, tx *TX) error) error // See Core.Transaction.
|
||||
|
||||
// ===========================================================================
|
||||
@ -219,6 +219,7 @@ type Sql struct {
|
||||
End int64 // End execution timestamp in milliseconds.
|
||||
Group string // Group is the group name of the configuration that the sql is executed from.
|
||||
IsTransaction bool // IsTransaction marks whether this sql is executed in transaction.
|
||||
RowsAffected int64 // RowsAffected marks retrieved or affected number with current sql statement.
|
||||
}
|
||||
|
||||
// DoInsertOption is the input struct for function DoInsert.
|
||||
@ -274,8 +275,12 @@ const (
|
||||
ctxTimeoutTypeQuery
|
||||
ctxTimeoutTypePrepare
|
||||
commandEnvKeyForDryRun = "gf.gdb.dryrun"
|
||||
ctxStrictKeyName = "gf.gdb.CtxStrictEnabled"
|
||||
ctxStrictErrorStr = "context is required for database operation, did you missing call function Ctx"
|
||||
sqlTypeBegin = `DB.Begin`
|
||||
sqlTypeTXCommit = `TX.Commit`
|
||||
sqlTypeTXRollback = `TX.Rollback`
|
||||
sqlTypeQueryContext = `DB.QueryContext`
|
||||
sqlTypeExecContext = `DB.ExecContext`
|
||||
sqlTypePrepareContext = `DB.PrepareContext`
|
||||
)
|
||||
|
||||
var (
|
||||
|
||||
@ -38,7 +38,6 @@ func (c *Core) Ctx(ctx context.Context) DB {
|
||||
if ctx == nil {
|
||||
return c.db
|
||||
}
|
||||
ctx = context.WithValue(ctx, ctxStrictKeyName, 1)
|
||||
// It makes a shallow copy of current db and changes its context for next chaining operation.
|
||||
var (
|
||||
err error
|
||||
@ -139,23 +138,18 @@ func (c *Core) Slave(schema ...string) (*sql.DB, error) {
|
||||
}
|
||||
|
||||
// GetAll queries and returns data records from database.
|
||||
func (c *Core) GetAll(sql string, args ...interface{}) (Result, error) {
|
||||
return c.db.DoGetAll(c.GetCtx(), nil, sql, args...)
|
||||
func (c *Core) GetAll(ctx context.Context, sql string, args ...interface{}) (Result, error) {
|
||||
return c.db.DoGetAll(ctx, nil, sql, args...)
|
||||
}
|
||||
|
||||
// DoGetAll queries and returns data records from database.
|
||||
func (c *Core) DoGetAll(ctx context.Context, link Link, sql string, args ...interface{}) (result Result, err error) {
|
||||
rows, err := c.db.DoQuery(ctx, link, sql, args...)
|
||||
if err != nil || rows == nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
return c.convertRowsToResult(rows)
|
||||
return c.db.DoQuery(ctx, link, sql, args...)
|
||||
}
|
||||
|
||||
// GetOne queries and returns one record from database.
|
||||
func (c *Core) GetOne(sql string, args ...interface{}) (Record, error) {
|
||||
list, err := c.db.GetAll(sql, args...)
|
||||
func (c *Core) GetOne(ctx context.Context, sql string, args ...interface{}) (Record, error) {
|
||||
list, err := c.db.GetAll(ctx, sql, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -167,8 +161,8 @@ func (c *Core) GetOne(sql string, args ...interface{}) (Record, error) {
|
||||
|
||||
// GetArray queries and returns data values as slice from database.
|
||||
// Note that if there are multiple columns in the result, it returns just one column values randomly.
|
||||
func (c *Core) GetArray(sql string, args ...interface{}) ([]Value, error) {
|
||||
all, err := c.db.DoGetAll(c.GetCtx(), nil, sql, args...)
|
||||
func (c *Core) GetArray(ctx context.Context, sql string, args ...interface{}) ([]Value, error) {
|
||||
all, err := c.db.DoGetAll(ctx, nil, sql, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -177,8 +171,8 @@ func (c *Core) GetArray(sql string, args ...interface{}) ([]Value, error) {
|
||||
|
||||
// GetStruct queries one record from database and converts it to given struct.
|
||||
// The parameter `pointer` should be a pointer to struct.
|
||||
func (c *Core) GetStruct(pointer interface{}, sql string, args ...interface{}) error {
|
||||
one, err := c.db.GetOne(sql, args...)
|
||||
func (c *Core) GetStruct(ctx context.Context, pointer interface{}, sql string, args ...interface{}) error {
|
||||
one, err := c.db.GetOne(ctx, sql, args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -187,8 +181,8 @@ func (c *Core) GetStruct(pointer interface{}, sql string, args ...interface{}) e
|
||||
|
||||
// GetStructs queries records from database and converts them to given struct.
|
||||
// The parameter `pointer` should be type of struct slice: []struct/[]*struct.
|
||||
func (c *Core) GetStructs(pointer interface{}, sql string, args ...interface{}) error {
|
||||
all, err := c.db.GetAll(sql, args...)
|
||||
func (c *Core) GetStructs(ctx context.Context, pointer interface{}, sql string, args ...interface{}) error {
|
||||
all, err := c.db.GetAll(ctx, sql, args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -201,7 +195,7 @@ func (c *Core) GetStructs(pointer interface{}, sql string, args ...interface{})
|
||||
// If parameter `pointer` is type of struct pointer, it calls GetStruct internally for
|
||||
// the conversion. If parameter `pointer` is type of slice, it calls GetStructs internally
|
||||
// for conversion.
|
||||
func (c *Core) GetScan(pointer interface{}, sql string, args ...interface{}) error {
|
||||
func (c *Core) GetScan(ctx context.Context, pointer interface{}, sql string, args ...interface{}) error {
|
||||
t := reflect.TypeOf(pointer)
|
||||
k := t.Kind()
|
||||
if k != reflect.Ptr {
|
||||
@ -210,10 +204,10 @@ func (c *Core) GetScan(pointer interface{}, sql string, args ...interface{}) err
|
||||
k = t.Elem().Kind()
|
||||
switch k {
|
||||
case reflect.Array, reflect.Slice:
|
||||
return c.db.GetCore().GetStructs(pointer, sql, args...)
|
||||
return c.db.GetCore().GetStructs(ctx, pointer, sql, args...)
|
||||
|
||||
case reflect.Struct:
|
||||
return c.db.GetCore().GetStruct(pointer, sql, args...)
|
||||
return c.db.GetCore().GetStruct(ctx, pointer, sql, args...)
|
||||
}
|
||||
return gerror.NewCodef(gcode.CodeInvalidParameter, "element type should be type of struct/slice, unsupported: %v", k)
|
||||
}
|
||||
@ -221,8 +215,8 @@ func (c *Core) GetScan(pointer interface{}, sql string, args ...interface{}) err
|
||||
// GetValue queries and returns the field value from database.
|
||||
// The sql should query only one field from database, or else it returns only one
|
||||
// field of the result.
|
||||
func (c *Core) GetValue(sql string, args ...interface{}) (Value, error) {
|
||||
one, err := c.db.GetOne(sql, args...)
|
||||
func (c *Core) GetValue(ctx context.Context, sql string, args ...interface{}) (Value, error) {
|
||||
one, err := c.db.GetOne(ctx, sql, args...)
|
||||
if err != nil {
|
||||
return gvar.New(nil), err
|
||||
}
|
||||
@ -233,13 +227,13 @@ func (c *Core) GetValue(sql string, args ...interface{}) (Value, error) {
|
||||
}
|
||||
|
||||
// GetCount queries and returns the count from database.
|
||||
func (c *Core) GetCount(sql string, args ...interface{}) (int, error) {
|
||||
func (c *Core) GetCount(ctx context.Context, sql string, args ...interface{}) (int, error) {
|
||||
// If the query fields do not contains function "COUNT",
|
||||
// it replaces the sql string and adds the "COUNT" function to the fields.
|
||||
if !gregex.IsMatchString(`(?i)SELECT\s+COUNT\(.+\)\s+FROM`, sql) {
|
||||
sql, _ = gregex.ReplaceString(`(?i)(SELECT)\s+(.+)\s+(FROM)`, `$1 COUNT($2) $3`, sql)
|
||||
}
|
||||
value, err := c.db.GetValue(sql, args...)
|
||||
value, err := c.db.GetValue(ctx, sql, args...)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
@ -306,11 +300,11 @@ func (c *Core) PingSlave() error {
|
||||
// Data(g.Slice{g.Map{"uid": 10000, "name":"john"}, g.Map{"uid": 20000, "name":"smith"})
|
||||
//
|
||||
// The parameter `batch` specifies the batch operation count when given data is slice.
|
||||
func (c *Core) Insert(table string, data interface{}, batch ...int) (sql.Result, error) {
|
||||
func (c *Core) Insert(ctx context.Context, table string, data interface{}, batch ...int) (sql.Result, error) {
|
||||
if len(batch) > 0 {
|
||||
return c.Model(table).Data(data).Batch(batch[0]).Insert()
|
||||
return c.Model(table).Ctx(ctx).Data(data).Batch(batch[0]).Insert()
|
||||
}
|
||||
return c.Model(table).Data(data).Insert()
|
||||
return c.Model(table).Ctx(ctx).Data(data).Insert()
|
||||
}
|
||||
|
||||
// InsertIgnore does "INSERT IGNORE INTO ..." statement for the table.
|
||||
@ -322,19 +316,19 @@ func (c *Core) Insert(table string, data interface{}, batch ...int) (sql.Result,
|
||||
// Data(g.Slice{g.Map{"uid": 10000, "name":"john"}, g.Map{"uid": 20000, "name":"smith"})
|
||||
//
|
||||
// The parameter `batch` specifies the batch operation count when given data is slice.
|
||||
func (c *Core) InsertIgnore(table string, data interface{}, batch ...int) (sql.Result, error) {
|
||||
func (c *Core) InsertIgnore(ctx context.Context, table string, data interface{}, batch ...int) (sql.Result, error) {
|
||||
if len(batch) > 0 {
|
||||
return c.Model(table).Data(data).Batch(batch[0]).InsertIgnore()
|
||||
return c.Model(table).Ctx(ctx).Data(data).Batch(batch[0]).InsertIgnore()
|
||||
}
|
||||
return c.Model(table).Data(data).InsertIgnore()
|
||||
return c.Model(table).Ctx(ctx).Data(data).InsertIgnore()
|
||||
}
|
||||
|
||||
// InsertAndGetId performs action Insert and returns the last insert id that automatically generated.
|
||||
func (c *Core) InsertAndGetId(table string, data interface{}, batch ...int) (int64, error) {
|
||||
func (c *Core) InsertAndGetId(ctx context.Context, table string, data interface{}, batch ...int) (int64, error) {
|
||||
if len(batch) > 0 {
|
||||
return c.Model(table).Data(data).Batch(batch[0]).InsertAndGetId()
|
||||
return c.Model(table).Ctx(ctx).Data(data).Batch(batch[0]).InsertAndGetId()
|
||||
}
|
||||
return c.Model(table).Data(data).InsertAndGetId()
|
||||
return c.Model(table).Ctx(ctx).Data(data).InsertAndGetId()
|
||||
}
|
||||
|
||||
// Replace does "REPLACE INTO ..." statement for the table.
|
||||
@ -349,11 +343,11 @@ func (c *Core) InsertAndGetId(table string, data interface{}, batch ...int) (int
|
||||
// The parameter `data` can be type of map/gmap/struct/*struct/[]map/[]struct, etc.
|
||||
// If given data is type of slice, it then does batch replacing, and the optional parameter
|
||||
// `batch` specifies the batch operation count.
|
||||
func (c *Core) Replace(table string, data interface{}, batch ...int) (sql.Result, error) {
|
||||
func (c *Core) Replace(ctx context.Context, table string, data interface{}, batch ...int) (sql.Result, error) {
|
||||
if len(batch) > 0 {
|
||||
return c.Model(table).Data(data).Batch(batch[0]).Replace()
|
||||
return c.Model(table).Ctx(ctx).Data(data).Batch(batch[0]).Replace()
|
||||
}
|
||||
return c.Model(table).Data(data).Replace()
|
||||
return c.Model(table).Ctx(ctx).Data(data).Replace()
|
||||
}
|
||||
|
||||
// Save does "INSERT INTO ... ON DUPLICATE KEY UPDATE..." statement for the table.
|
||||
@ -367,11 +361,11 @@ func (c *Core) Replace(table string, data interface{}, batch ...int) (sql.Result
|
||||
//
|
||||
// If given data is type of slice, it then does batch saving, and the optional parameter
|
||||
// `batch` specifies the batch operation count.
|
||||
func (c *Core) Save(table string, data interface{}, batch ...int) (sql.Result, error) {
|
||||
func (c *Core) Save(ctx context.Context, table string, data interface{}, batch ...int) (sql.Result, error) {
|
||||
if len(batch) > 0 {
|
||||
return c.Model(table).Data(data).Batch(batch[0]).Save()
|
||||
return c.Model(table).Ctx(ctx).Data(data).Batch(batch[0]).Save()
|
||||
}
|
||||
return c.Model(table).Data(data).Save()
|
||||
return c.Model(table).Ctx(ctx).Data(data).Save()
|
||||
}
|
||||
|
||||
// DoInsert inserts or updates data forF given table.
|
||||
@ -507,8 +501,8 @@ func (c *Core) formatOnDuplicate(columns []string, option DoInsertOption) string
|
||||
// "status IN (?)", g.Slice{1,2,3}
|
||||
// "age IN(?,?)", 18, 50
|
||||
// User{ Id : 1, UserName : "john"}
|
||||
func (c *Core) Update(table string, data interface{}, condition interface{}, args ...interface{}) (sql.Result, error) {
|
||||
return c.Model(table).Data(data).Where(condition, args...).Update()
|
||||
func (c *Core) Update(ctx context.Context, table string, data interface{}, condition interface{}, args ...interface{}) (sql.Result, error) {
|
||||
return c.Model(table).Ctx(ctx).Data(data).Where(condition, args...).Update()
|
||||
}
|
||||
|
||||
// DoUpdate does "UPDATE ... " statement for the table.
|
||||
@ -598,8 +592,8 @@ func (c *Core) DoUpdate(ctx context.Context, link Link, table string, data inter
|
||||
// "status IN (?)", g.Slice{1,2,3}
|
||||
// "age IN(?,?)", 18, 50
|
||||
// User{ Id : 1, UserName : "john"}
|
||||
func (c *Core) Delete(table string, condition interface{}, args ...interface{}) (result sql.Result, err error) {
|
||||
return c.Model(table).Where(condition, args...).Delete()
|
||||
func (c *Core) Delete(ctx context.Context, table string, condition interface{}, args ...interface{}) (result sql.Result, err error) {
|
||||
return c.Model(table).Ctx(ctx).Where(condition, args...).Delete()
|
||||
}
|
||||
|
||||
// DoDelete does "DELETE FROM ... " statement for the table.
|
||||
@ -614,50 +608,6 @@ func (c *Core) DoDelete(ctx context.Context, link Link, table string, condition
|
||||
return c.db.DoExec(ctx, link, fmt.Sprintf("DELETE FROM %s%s", table, condition), args...)
|
||||
}
|
||||
|
||||
// convertRowsToResult converts underlying data record type sql.Rows to Result type.
|
||||
func (c *Core) convertRowsToResult(rows *sql.Rows) (Result, error) {
|
||||
if !rows.Next() {
|
||||
return nil, nil
|
||||
}
|
||||
// Column names and types.
|
||||
columns, err := rows.ColumnTypes()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
columnTypes := make([]string, len(columns))
|
||||
columnNames := make([]string, len(columns))
|
||||
for k, v := range columns {
|
||||
columnTypes[k] = v.DatabaseTypeName()
|
||||
columnNames[k] = v.Name()
|
||||
}
|
||||
var (
|
||||
values = make([]interface{}, len(columnNames))
|
||||
result = make(Result, 0)
|
||||
scanArgs = make([]interface{}, len(values))
|
||||
)
|
||||
for i := range values {
|
||||
scanArgs[i] = &values[i]
|
||||
}
|
||||
for {
|
||||
if err := rows.Scan(scanArgs...); err != nil {
|
||||
return result, err
|
||||
}
|
||||
record := Record{}
|
||||
for i, value := range values {
|
||||
if value == nil {
|
||||
record[columnNames[i]] = gvar.New(nil)
|
||||
} else {
|
||||
record[columnNames[i]] = gvar.New(c.convertFieldValueToLocalValue(value, columnTypes[i]))
|
||||
}
|
||||
}
|
||||
result = append(result, record)
|
||||
if !rows.Next() {
|
||||
break
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
// It just returns the pointer address.
|
||||
//
|
||||
@ -670,13 +620,25 @@ func (c *Core) MarshalJSON() ([]byte, error) {
|
||||
// writeSqlToLogger outputs the Sql object to logger.
|
||||
// It is enabled only if configuration "debug" is true.
|
||||
func (c *Core) writeSqlToLogger(ctx context.Context, sql *Sql) {
|
||||
var transactionIdStr string
|
||||
var (
|
||||
sqlTypeKey string
|
||||
transactionIdStr string
|
||||
)
|
||||
switch sql.Type {
|
||||
case sqlTypeQueryContext:
|
||||
sqlTypeKey = `selected`
|
||||
default:
|
||||
sqlTypeKey = `affected`
|
||||
}
|
||||
if sql.IsTransaction {
|
||||
if v := ctx.Value(transactionIdForLoggerCtx); v != nil {
|
||||
transactionIdStr = fmt.Sprintf(`[%d] `, v.(uint64))
|
||||
transactionIdStr = fmt.Sprintf(`[txid:%d] `, v.(uint64))
|
||||
}
|
||||
}
|
||||
s := fmt.Sprintf("[%3d ms] [%s] %s%s", sql.End-sql.Start, sql.Group, transactionIdStr, sql.Format)
|
||||
s := fmt.Sprintf(
|
||||
"[%3d ms] [%s] [%s:%d] %s%s",
|
||||
sql.End-sql.Start, sql.Group, sqlTypeKey, sql.RowsAffected, transactionIdStr, sql.Format,
|
||||
)
|
||||
if sql.Error != nil {
|
||||
s += "\nError: " + sql.Error.Error()
|
||||
c.logger.Error(ctx, s)
|
||||
|
||||
@ -48,7 +48,6 @@ type ConfigNode struct {
|
||||
UpdatedAt string `json:"updatedAt"` // (Optional) The filed name of table for automatic-filled updated datetime.
|
||||
DeletedAt string `json:"deletedAt"` // (Optional) The filed name of table for automatic-filled updated datetime.
|
||||
TimeMaintainDisabled bool `json:"timeMaintainDisabled"` // (Optional) Disable the automatic time maintaining feature.
|
||||
CtxStrict bool `json:"ctxStrict"` // (Optional) Strictly require context input for all database operations.
|
||||
}
|
||||
|
||||
const (
|
||||
|
||||
@ -109,7 +109,7 @@ func (c *Core) convertFieldValueToLocalValue(fieldValue interface{}, fieldType s
|
||||
return t.String()
|
||||
|
||||
default:
|
||||
// Auto detect field type, using key match.
|
||||
// Auto-detect field type, using key match.
|
||||
switch {
|
||||
case strings.Contains(t, "text") || strings.Contains(t, "char") || strings.Contains(t, "character"):
|
||||
return gconv.String(fieldValue)
|
||||
@ -179,17 +179,3 @@ func (c *Core) mappingAndFilterData(schema, table string, data map[string]interf
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
//// filterFields removes all key-value pairs which are not the field of given table.
|
||||
//func (c *Core) filterFields(schema, table string, data map[string]interface{}) map[string]interface{} {
|
||||
// // It must use data copy here to avoid its changing the origin data map.
|
||||
// newDataMap := make(map[string]interface{}, len(data))
|
||||
// if fields, err := c.db.TableFields(table, schema); err == nil {
|
||||
// for k, v := range data {
|
||||
// if _, ok := fields[k]; ok {
|
||||
// newDataMap[k] = v
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return newDataMap
|
||||
//}
|
||||
|
||||
@ -46,8 +46,8 @@ var (
|
||||
// You should call Commit or Rollback functions of the transaction object
|
||||
// if you no longer use the transaction. Commit or Rollback functions will also
|
||||
// close the transaction automatically.
|
||||
func (c *Core) Begin() (tx *TX, err error) {
|
||||
return c.doBeginCtx(c.GetCtx())
|
||||
func (c *Core) Begin(ctx context.Context) (tx *TX, err error) {
|
||||
return c.doBeginCtx(ctx)
|
||||
}
|
||||
|
||||
func (c *Core) doBeginCtx(ctx context.Context) (*TX, error) {
|
||||
@ -62,7 +62,7 @@ func (c *Core) doBeginCtx(ctx context.Context) (*TX, error) {
|
||||
mTime2 = gtime.TimestampMilli()
|
||||
sqlObj = &Sql{
|
||||
Sql: sqlStr,
|
||||
Type: "DB.Begin",
|
||||
Type: sqlTypeBegin,
|
||||
Args: nil,
|
||||
Format: sqlStr,
|
||||
Error: err,
|
||||
@ -209,7 +209,7 @@ func (tx *TX) Commit() error {
|
||||
mTime2 = gtime.TimestampMilli()
|
||||
sqlObj = &Sql{
|
||||
Sql: sqlStr,
|
||||
Type: "TX.Commit",
|
||||
Type: sqlTypeTXCommit,
|
||||
Args: nil,
|
||||
Format: sqlStr,
|
||||
Error: err,
|
||||
@ -243,7 +243,7 @@ func (tx *TX) Rollback() error {
|
||||
mTime2 = gtime.TimestampMilli()
|
||||
sqlObj = &Sql{
|
||||
Sql: sqlStr,
|
||||
Type: "TX.Rollback",
|
||||
Type: sqlTypeTXRollback,
|
||||
Args: nil,
|
||||
Format: sqlStr,
|
||||
Error: err,
|
||||
@ -336,7 +336,7 @@ func (tx *TX) Transaction(ctx context.Context, f func(ctx context.Context, tx *T
|
||||
|
||||
// Query does query operation on transaction.
|
||||
// See Core.Query.
|
||||
func (tx *TX) Query(sql string, args ...interface{}) (rows *sql.Rows, err error) {
|
||||
func (tx *TX) Query(sql string, args ...interface{}) (result Result, err error) {
|
||||
return tx.db.DoQuery(tx.ctx, &txLink{tx.tx}, sql, args...)
|
||||
}
|
||||
|
||||
@ -357,12 +357,7 @@ func (tx *TX) Prepare(sql string) (*Stmt, error) {
|
||||
|
||||
// GetAll queries and returns data records from database.
|
||||
func (tx *TX) GetAll(sql string, args ...interface{}) (Result, error) {
|
||||
rows, err := tx.Query(sql, args...)
|
||||
if err != nil || rows == nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
return tx.db.GetCore().convertRowsToResult(rows)
|
||||
return tx.Query(sql, args...)
|
||||
}
|
||||
|
||||
// GetOne queries and returns one record from database.
|
||||
|
||||
@ -10,21 +10,21 @@ package gdb
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"github.com/gogf/gf/errors/gcode"
|
||||
"github.com/gogf/gf/errors/gerror"
|
||||
"github.com/gogf/gf/container/gvar"
|
||||
"github.com/gogf/gf/internal/intlog"
|
||||
|
||||
"github.com/gogf/gf/os/gtime"
|
||||
)
|
||||
|
||||
// Query commits one query SQL to underlying driver and returns the execution result.
|
||||
// It is most commonly used for data querying.
|
||||
func (c *Core) Query(sql string, args ...interface{}) (rows *sql.Rows, err error) {
|
||||
return c.db.DoQuery(c.GetCtx(), nil, sql, args...)
|
||||
func (c *Core) Query(ctx context.Context, sql string, args ...interface{}) (result Result, err error) {
|
||||
return c.db.DoQuery(ctx, nil, sql, args...)
|
||||
}
|
||||
|
||||
// DoQuery commits the sql string and its arguments to underlying driver
|
||||
// through given link object and returns the execution result.
|
||||
func (c *Core) DoQuery(ctx context.Context, link Link, sql string, args ...interface{}) (rows *sql.Rows, err error) {
|
||||
func (c *Core) DoQuery(ctx context.Context, link Link, sql string, args ...interface{}) (result Result, err error) {
|
||||
// Transaction checks.
|
||||
if link == nil {
|
||||
if tx := TXFromCtx(ctx, c.db.GetGroup()); tx != nil {
|
||||
@ -51,12 +51,16 @@ func (c *Core) DoQuery(ctx context.Context, link Link, sql string, args ...inter
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
mTime1 := gtime.TimestampMilli()
|
||||
rows, err = link.QueryContext(ctx, sql, args...)
|
||||
rows, err := link.QueryContext(ctx, sql, args...)
|
||||
mTime2 := gtime.TimestampMilli()
|
||||
if err == nil {
|
||||
result, err = c.convertRowsToResult(ctx, rows)
|
||||
}
|
||||
sqlObj := &Sql{
|
||||
Sql: sql,
|
||||
Type: "DB.QueryContext",
|
||||
Type: sqlTypeQueryContext,
|
||||
Args: args,
|
||||
Format: FormatSqlWithArgs(sql, args),
|
||||
Error: err,
|
||||
@ -64,6 +68,7 @@ func (c *Core) DoQuery(ctx context.Context, link Link, sql string, args ...inter
|
||||
End: mTime2,
|
||||
Group: c.db.GetGroup(),
|
||||
IsTransaction: link.IsTransaction(),
|
||||
RowsAffected: int64(result.Len()),
|
||||
}
|
||||
// Tracing and logging.
|
||||
c.addSqlToTracing(ctx, sqlObj)
|
||||
@ -71,7 +76,7 @@ func (c *Core) DoQuery(ctx context.Context, link Link, sql string, args ...inter
|
||||
c.writeSqlToLogger(ctx, sqlObj)
|
||||
}
|
||||
if err == nil {
|
||||
return rows, nil
|
||||
return result, nil
|
||||
} else {
|
||||
err = formatError(err, sql, args...)
|
||||
}
|
||||
@ -80,8 +85,8 @@ func (c *Core) DoQuery(ctx context.Context, link Link, sql string, args ...inter
|
||||
|
||||
// Exec commits one query SQL to underlying driver and returns the execution result.
|
||||
// It is most commonly used for data inserting and updating.
|
||||
func (c *Core) Exec(sql string, args ...interface{}) (result sql.Result, err error) {
|
||||
return c.db.DoExec(c.GetCtx(), nil, sql, args...)
|
||||
func (c *Core) Exec(ctx context.Context, sql string, args ...interface{}) (result sql.Result, err error) {
|
||||
return c.db.DoExec(ctx, nil, sql, args...)
|
||||
}
|
||||
|
||||
// DoExec commits the sql string and its arguments to underlying driver
|
||||
@ -115,6 +120,7 @@ func (c *Core) DoExec(ctx context.Context, link Link, sql string, args ...interf
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
mTime1 := gtime.TimestampMilli()
|
||||
if !c.db.GetDryRun() {
|
||||
result, err = link.ExecContext(ctx, sql, args...)
|
||||
@ -122,9 +128,15 @@ func (c *Core) DoExec(ctx context.Context, link Link, sql string, args ...interf
|
||||
result = new(SqlResult)
|
||||
}
|
||||
mTime2 := gtime.TimestampMilli()
|
||||
var (
|
||||
rowsAffected int64
|
||||
)
|
||||
if err == nil {
|
||||
rowsAffected, err = result.RowsAffected()
|
||||
}
|
||||
sqlObj := &Sql{
|
||||
Sql: sql,
|
||||
Type: "DB.ExecContext",
|
||||
Type: sqlTypeExecContext,
|
||||
Args: args,
|
||||
Format: FormatSqlWithArgs(sql, args),
|
||||
Error: err,
|
||||
@ -132,6 +144,7 @@ func (c *Core) DoExec(ctx context.Context, link Link, sql string, args ...interf
|
||||
End: mTime2,
|
||||
Group: c.db.GetGroup(),
|
||||
IsTransaction: link.IsTransaction(),
|
||||
RowsAffected: rowsAffected,
|
||||
}
|
||||
// Tracing and logging.
|
||||
c.addSqlToTracing(ctx, sqlObj)
|
||||
@ -145,11 +158,6 @@ func (c *Core) DoExec(ctx context.Context, link Link, sql string, args ...interf
|
||||
// The parameter `link` specifies the current database connection operation object. You can modify the sql
|
||||
// string `sql` and its arguments `args` as you wish before they're committed to driver.
|
||||
func (c *Core) DoCommit(ctx context.Context, link Link, sql string, args []interface{}) (newSql string, newArgs []interface{}, err error) {
|
||||
if c.db.GetConfig().CtxStrict {
|
||||
if v := ctx.Value(ctxStrictKeyName); v == nil {
|
||||
return sql, args, gerror.NewCode(gcode.CodeMissingParameter, ctxStrictErrorStr)
|
||||
}
|
||||
}
|
||||
return sql, args, nil
|
||||
}
|
||||
|
||||
@ -161,7 +169,7 @@ func (c *Core) DoCommit(ctx context.Context, link Link, sql string, args []inter
|
||||
//
|
||||
// The parameter `execOnMaster` specifies whether executing the sql on master node,
|
||||
// or else it executes the sql on slave node if master-slave configured.
|
||||
func (c *Core) Prepare(sql string, execOnMaster ...bool) (*Stmt, error) {
|
||||
func (c *Core) Prepare(ctx context.Context, sql string, execOnMaster ...bool) (*Stmt, error) {
|
||||
var (
|
||||
err error
|
||||
link Link
|
||||
@ -175,7 +183,7 @@ func (c *Core) Prepare(sql string, execOnMaster ...bool) (*Stmt, error) {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return c.db.DoPrepare(c.GetCtx(), link, sql)
|
||||
return c.db.DoPrepare(ctx, link, sql)
|
||||
}
|
||||
|
||||
// DoPrepare calls prepare function on given link object and returns the statement object.
|
||||
@ -204,19 +212,13 @@ func (c *Core) DoPrepare(ctx context.Context, link Link, sql string) (*Stmt, err
|
||||
ctx, _ = context.WithTimeout(ctx, c.GetConfig().PrepareTimeout)
|
||||
}
|
||||
|
||||
if c.db.GetConfig().CtxStrict {
|
||||
if v := ctx.Value(ctxStrictKeyName); v == nil {
|
||||
return nil, gerror.NewCode(gcode.CodeMissingParameter, ctxStrictErrorStr)
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
mTime1 = gtime.TimestampMilli()
|
||||
stmt, err = link.PrepareContext(ctx, sql)
|
||||
mTime2 = gtime.TimestampMilli()
|
||||
sqlObj = &Sql{
|
||||
Sql: sql,
|
||||
Type: "DB.PrepareContext",
|
||||
Type: sqlTypePrepareContext,
|
||||
Args: nil,
|
||||
Format: FormatSqlWithArgs(sql, nil),
|
||||
Error: err,
|
||||
@ -238,3 +240,58 @@ func (c *Core) DoPrepare(ctx context.Context, link Link, sql string) (*Stmt, err
|
||||
sql: sql,
|
||||
}, err
|
||||
}
|
||||
|
||||
// convertRowsToResult converts underlying data record type sql.Rows to Result type.
|
||||
func (c *Core) convertRowsToResult(ctx context.Context, rows *sql.Rows) (Result, error) {
|
||||
if rows == nil {
|
||||
return nil, nil
|
||||
}
|
||||
defer func() {
|
||||
if err := rows.Close(); err != nil {
|
||||
intlog.Error(ctx, err)
|
||||
}
|
||||
}()
|
||||
if !rows.Next() {
|
||||
return nil, nil
|
||||
}
|
||||
// Column names and types.
|
||||
columns, err := rows.ColumnTypes()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var (
|
||||
columnTypes = make([]string, len(columns))
|
||||
columnNames = make([]string, len(columns))
|
||||
)
|
||||
for k, v := range columns {
|
||||
columnTypes[k] = v.DatabaseTypeName()
|
||||
columnNames[k] = v.Name()
|
||||
}
|
||||
var (
|
||||
values = make([]interface{}, len(columnNames))
|
||||
result = make(Result, 0)
|
||||
scanArgs = make([]interface{}, len(values))
|
||||
)
|
||||
for i := range values {
|
||||
scanArgs[i] = &values[i]
|
||||
}
|
||||
for {
|
||||
if err = rows.Scan(scanArgs...); err != nil {
|
||||
return result, err
|
||||
}
|
||||
record := Record{}
|
||||
for i, value := range values {
|
||||
if value == nil {
|
||||
record[columnNames[i]] = gvar.New(nil)
|
||||
} else {
|
||||
record[columnNames[i]] = gvar.New(c.convertFieldValueToLocalValue(value, columnTypes[i]))
|
||||
}
|
||||
}
|
||||
result = append(result, record)
|
||||
if !rows.Next() {
|
||||
break
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@ -433,67 +433,6 @@ func (m *Model) Sum(column string) (float64, error) {
|
||||
return value.Float64(), err
|
||||
}
|
||||
|
||||
// FindOne retrieves and returns a single Record by Model.WherePri and Model.One.
|
||||
// Also see Model.WherePri and Model.One.
|
||||
func (m *Model) FindOne(where ...interface{}) (Record, error) {
|
||||
if len(where) > 0 {
|
||||
return m.WherePri(where[0], where[1:]...).One()
|
||||
}
|
||||
return m.One()
|
||||
}
|
||||
|
||||
// FindAll retrieves and returns Result by by Model.WherePri and Model.All.
|
||||
// Also see Model.WherePri and Model.All.
|
||||
func (m *Model) FindAll(where ...interface{}) (Result, error) {
|
||||
if len(where) > 0 {
|
||||
return m.WherePri(where[0], where[1:]...).All()
|
||||
}
|
||||
return m.All()
|
||||
}
|
||||
|
||||
// FindValue retrieves and returns single field value by Model.WherePri and Model.Value.
|
||||
// Also see Model.WherePri and Model.Value.
|
||||
func (m *Model) FindValue(fieldsAndWhere ...interface{}) (Value, error) {
|
||||
if len(fieldsAndWhere) >= 2 {
|
||||
return m.WherePri(fieldsAndWhere[1], fieldsAndWhere[2:]...).Fields(gconv.String(fieldsAndWhere[0])).Value()
|
||||
}
|
||||
if len(fieldsAndWhere) == 1 {
|
||||
return m.Fields(gconv.String(fieldsAndWhere[0])).Value()
|
||||
}
|
||||
return m.Value()
|
||||
}
|
||||
|
||||
// FindArray queries and returns data values as slice from database.
|
||||
// Note that if there are multiple columns in the result, it returns just one column values randomly.
|
||||
// Also see Model.WherePri and Model.Value.
|
||||
func (m *Model) FindArray(fieldsAndWhere ...interface{}) ([]Value, error) {
|
||||
if len(fieldsAndWhere) >= 2 {
|
||||
return m.WherePri(fieldsAndWhere[1], fieldsAndWhere[2:]...).Fields(gconv.String(fieldsAndWhere[0])).Array()
|
||||
}
|
||||
if len(fieldsAndWhere) == 1 {
|
||||
return m.Fields(gconv.String(fieldsAndWhere[0])).Array()
|
||||
}
|
||||
return m.Array()
|
||||
}
|
||||
|
||||
// FindCount retrieves and returns the record number by Model.WherePri and Model.Count.
|
||||
// Also see Model.WherePri and Model.Count.
|
||||
func (m *Model) FindCount(where ...interface{}) (int, error) {
|
||||
if len(where) > 0 {
|
||||
return m.WherePri(where[0], where[1:]...).Count()
|
||||
}
|
||||
return m.Count()
|
||||
}
|
||||
|
||||
// FindScan retrieves and returns the record/records by Model.WherePri and Model.Scan.
|
||||
// Also see Model.WherePri and Model.Scan.
|
||||
func (m *Model) FindScan(pointer interface{}, where ...interface{}) error {
|
||||
if len(where) > 0 {
|
||||
return m.WherePri(where[0], where[1:]...).Scan(pointer)
|
||||
}
|
||||
return m.Scan(pointer)
|
||||
}
|
||||
|
||||
// Union does "(SELECT xxx FROM xxx) UNION (SELECT xxx FROM xxx) ..." statement for the model.
|
||||
func (m *Model) Union(unions ...*Model) *Model {
|
||||
return m.db.Union(unions...)
|
||||
|
||||
@ -68,7 +68,7 @@ func Test_Custom_Driver(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
t.Assert(latestSqlString.Val(), "")
|
||||
sqlString := "select 10000"
|
||||
value, err := g.DB("driver-test").GetValue(sqlString)
|
||||
value, err := g.DB("driver-test").GetValue(ctx, sqlString)
|
||||
t.AssertNil(err)
|
||||
t.Assert(value, 10000)
|
||||
t.Assert(latestSqlString.Val(), sqlString)
|
||||
|
||||
@ -30,11 +30,11 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
db gdb.DB
|
||||
dbPrefix gdb.DB
|
||||
dbCtxStrict gdb.DB
|
||||
dbInvalid gdb.DB
|
||||
configNode gdb.ConfigNode
|
||||
db gdb.DB
|
||||
dbPrefix gdb.DB
|
||||
dbInvalid gdb.DB
|
||||
configNode gdb.ConfigNode
|
||||
ctx = context.TODO()
|
||||
)
|
||||
|
||||
func init() {
|
||||
@ -60,15 +60,11 @@ func init() {
|
||||
nodePrefix := configNode
|
||||
nodePrefix.Prefix = TableNamePrefix1
|
||||
|
||||
nodeCtxStrict := configNode
|
||||
nodeCtxStrict.CtxStrict = true
|
||||
|
||||
nodeInvalid := configNode
|
||||
nodeInvalid.Port = "3307"
|
||||
|
||||
gdb.AddConfigNode("test", configNode)
|
||||
gdb.AddConfigNode("prefix", nodePrefix)
|
||||
gdb.AddConfigNode("ctxstrict", nodeCtxStrict)
|
||||
gdb.AddConfigNode("nodeinvalid", nodeInvalid)
|
||||
gdb.AddConfigNode(gdb.DefaultGroupName, configNode)
|
||||
|
||||
@ -79,10 +75,10 @@ func init() {
|
||||
db = r
|
||||
}
|
||||
schemaTemplate := "CREATE DATABASE IF NOT EXISTS `%s` CHARACTER SET UTF8"
|
||||
if _, err := db.Exec(fmt.Sprintf(schemaTemplate, TestSchema1)); err != nil {
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(schemaTemplate, TestSchema1)); err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
if _, err := db.Exec(fmt.Sprintf(schemaTemplate, TestSchema2)); err != nil {
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(schemaTemplate, TestSchema2)); err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
db.SetSchema(TestSchema1)
|
||||
@ -93,28 +89,14 @@ func init() {
|
||||
} else {
|
||||
dbPrefix = r
|
||||
}
|
||||
if _, err := dbPrefix.Exec(fmt.Sprintf(schemaTemplate, TestSchema1)); err != nil {
|
||||
if _, err := dbPrefix.Exec(ctx, fmt.Sprintf(schemaTemplate, TestSchema1)); err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
if _, err := dbPrefix.Exec(fmt.Sprintf(schemaTemplate, TestSchema2)); err != nil {
|
||||
if _, err := dbPrefix.Exec(ctx, fmt.Sprintf(schemaTemplate, TestSchema2)); err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
dbPrefix.SetSchema(TestSchema1)
|
||||
|
||||
// CtxStrict db.
|
||||
if r, err := gdb.New("ctxstrict"); err != nil {
|
||||
gtest.Error(err)
|
||||
} else {
|
||||
dbCtxStrict = r
|
||||
}
|
||||
if _, err := dbCtxStrict.Ctx(context.TODO()).Exec(fmt.Sprintf(schemaTemplate, TestSchema1)); err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
if _, err := dbCtxStrict.Ctx(context.TODO()).Exec(fmt.Sprintf(schemaTemplate, TestSchema2)); err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
dbCtxStrict.SetSchema(TestSchema1)
|
||||
|
||||
// Invalid db.
|
||||
if r, err := gdb.New("nodeinvalid"); err != nil {
|
||||
gtest.Error(err)
|
||||
@ -146,7 +128,7 @@ func createTableWithDb(db gdb.DB, table ...string) (name string) {
|
||||
|
||||
switch configNode.Type {
|
||||
case "sqlite":
|
||||
if _, err := db.Ctx(context.TODO()).Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE %s (
|
||||
id bigint unsigned NOT NULL AUTO_INCREMENT,
|
||||
passport varchar(45),
|
||||
@ -159,7 +141,7 @@ func createTableWithDb(db gdb.DB, table ...string) (name string) {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
case "pgsql":
|
||||
if _, err := db.Ctx(context.TODO()).Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE %s (
|
||||
id bigint NOT NULL,
|
||||
passport varchar(45),
|
||||
@ -172,7 +154,7 @@ func createTableWithDb(db gdb.DB, table ...string) (name string) {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
case "mssql":
|
||||
if _, err := db.Ctx(context.TODO()).Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='%s' and xtype='U')
|
||||
CREATE TABLE %s (
|
||||
ID numeric(10,0) NOT NULL,
|
||||
@ -186,7 +168,7 @@ func createTableWithDb(db gdb.DB, table ...string) (name string) {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
case "oracle":
|
||||
if _, err := db.Ctx(context.TODO()).Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE %s (
|
||||
ID NUMBER(10) NOT NULL,
|
||||
PASSPORT VARCHAR(45) NOT NULL,
|
||||
@ -199,7 +181,7 @@ func createTableWithDb(db gdb.DB, table ...string) (name string) {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
case "mysql":
|
||||
if _, err := db.Ctx(context.TODO()).Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE %s (
|
||||
id int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
passport varchar(45) NULL,
|
||||
@ -230,7 +212,7 @@ func createInitTableWithDb(db gdb.DB, table ...string) (name string) {
|
||||
})
|
||||
}
|
||||
|
||||
result, err := db.Ctx(context.TODO()).Insert(name, array.Slice())
|
||||
result, err := db.Insert(ctx, name, array.Slice())
|
||||
gtest.AssertNil(err)
|
||||
|
||||
n, e := result.RowsAffected()
|
||||
@ -240,7 +222,7 @@ func createInitTableWithDb(db gdb.DB, table ...string) (name string) {
|
||||
}
|
||||
|
||||
func dropTableWithDb(db gdb.DB, table string) {
|
||||
if _, err := db.Ctx(context.TODO()).Exec(fmt.Sprintf("DROP TABLE IF EXISTS `%s`", table)); err != nil {
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf("DROP TABLE IF EXISTS `%s`", table)); err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -25,7 +25,7 @@ func Test_Table_Relation_One(t *testing.T) {
|
||||
tableUserDetail = "user_detail_" + gtime.TimestampMicroStr()
|
||||
tableUserScores = "user_scores_" + gtime.TimestampMicroStr()
|
||||
)
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE %s (
|
||||
uid int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
name varchar(45) NOT NULL,
|
||||
@ -36,7 +36,7 @@ CREATE TABLE %s (
|
||||
}
|
||||
defer dropTable(tableUser)
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE %s (
|
||||
uid int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
address varchar(45) NOT NULL,
|
||||
@ -47,7 +47,7 @@ CREATE TABLE %s (
|
||||
}
|
||||
defer dropTable(tableUserDetail)
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE %s (
|
||||
id int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
uid int(10) unsigned NOT NULL,
|
||||
@ -170,7 +170,7 @@ func Test_Table_Relation_Many(t *testing.T) {
|
||||
tableUserDetail = "user_detail_" + gtime.TimestampMicroStr()
|
||||
tableUserScores = "user_scores_" + gtime.TimestampMicroStr()
|
||||
)
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE %s (
|
||||
uid int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
name varchar(45) NOT NULL,
|
||||
@ -181,7 +181,7 @@ CREATE TABLE %s (
|
||||
}
|
||||
defer dropTable(tableUser)
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE %s (
|
||||
uid int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
address varchar(45) NOT NULL,
|
||||
@ -192,7 +192,7 @@ CREATE TABLE %s (
|
||||
}
|
||||
defer dropTable(tableUserDetail)
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE %s (
|
||||
id int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
uid int(10) unsigned NOT NULL,
|
||||
@ -228,20 +228,20 @@ CREATE TABLE %s (
|
||||
var err error
|
||||
for i := 1; i <= 5; i++ {
|
||||
// User.
|
||||
_, err = db.Insert(tableUser, g.Map{
|
||||
_, err = db.Insert(ctx, tableUser, g.Map{
|
||||
"uid": i,
|
||||
"name": fmt.Sprintf(`name_%d`, i),
|
||||
})
|
||||
t.AssertNil(err)
|
||||
// Detail.
|
||||
_, err = db.Insert(tableUserDetail, g.Map{
|
||||
_, err = db.Insert(ctx, tableUserDetail, g.Map{
|
||||
"uid": i,
|
||||
"address": fmt.Sprintf(`address_%d`, i),
|
||||
})
|
||||
t.AssertNil(err)
|
||||
// Scores.
|
||||
for j := 1; j <= 5; j++ {
|
||||
_, err = db.Insert(tableUserScores, g.Map{
|
||||
_, err = db.Insert(ctx, tableUserScores, g.Map{
|
||||
"uid": i,
|
||||
"score": j,
|
||||
})
|
||||
@ -484,7 +484,7 @@ func Test_Table_Relation_Many_RelationKeyCaseInsensitive(t *testing.T) {
|
||||
tableUserDetail = "user_detail_" + gtime.TimestampMicroStr()
|
||||
tableUserScores = "user_scores_" + gtime.TimestampMicroStr()
|
||||
)
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE %s (
|
||||
uid int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
name varchar(45) NOT NULL,
|
||||
@ -495,7 +495,7 @@ CREATE TABLE %s (
|
||||
}
|
||||
defer dropTable(tableUser)
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE %s (
|
||||
uid int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
address varchar(45) NOT NULL,
|
||||
@ -506,7 +506,7 @@ CREATE TABLE %s (
|
||||
}
|
||||
defer dropTable(tableUserDetail)
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE %s (
|
||||
id int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
uid int(10) unsigned NOT NULL,
|
||||
@ -542,20 +542,20 @@ CREATE TABLE %s (
|
||||
var err error
|
||||
for i := 1; i <= 5; i++ {
|
||||
// User.
|
||||
_, err = db.Insert(tableUser, g.Map{
|
||||
_, err = db.Insert(ctx, tableUser, g.Map{
|
||||
"uid": i,
|
||||
"name": fmt.Sprintf(`name_%d`, i),
|
||||
})
|
||||
t.AssertNil(err)
|
||||
// Detail.
|
||||
_, err = db.Insert(tableUserDetail, g.Map{
|
||||
_, err = db.Insert(ctx, tableUserDetail, g.Map{
|
||||
"uid": i,
|
||||
"address": fmt.Sprintf(`address_%d`, i),
|
||||
})
|
||||
t.AssertNil(err)
|
||||
// Scores.
|
||||
for j := 1; j <= 5; j++ {
|
||||
_, err = db.Insert(tableUserScores, g.Map{
|
||||
_, err = db.Insert(ctx, tableUserScores, g.Map{
|
||||
"uid": i,
|
||||
"score": j,
|
||||
})
|
||||
@ -798,7 +798,7 @@ func Test_Table_Relation_Many_TheSameRelationNames(t *testing.T) {
|
||||
tableUserDetail = "user_detail_" + gtime.TimestampMicroStr()
|
||||
tableUserScores = "user_scores_" + gtime.TimestampMicroStr()
|
||||
)
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE %s (
|
||||
uid int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
name varchar(45) NOT NULL,
|
||||
@ -809,7 +809,7 @@ CREATE TABLE %s (
|
||||
}
|
||||
defer dropTable(tableUser)
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE %s (
|
||||
uid int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
address varchar(45) NOT NULL,
|
||||
@ -820,7 +820,7 @@ CREATE TABLE %s (
|
||||
}
|
||||
defer dropTable(tableUserDetail)
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE %s (
|
||||
id int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
uid int(10) unsigned NOT NULL,
|
||||
@ -856,20 +856,20 @@ CREATE TABLE %s (
|
||||
var err error
|
||||
for i := 1; i <= 5; i++ {
|
||||
// User.
|
||||
_, err = db.Insert(tableUser, g.Map{
|
||||
_, err = db.Insert(ctx, tableUser, g.Map{
|
||||
"uid": i,
|
||||
"name": fmt.Sprintf(`name_%d`, i),
|
||||
})
|
||||
t.AssertNil(err)
|
||||
// Detail.
|
||||
_, err = db.Insert(tableUserDetail, g.Map{
|
||||
_, err = db.Insert(ctx, tableUserDetail, g.Map{
|
||||
"uid": i,
|
||||
"address": fmt.Sprintf(`address_%d`, i),
|
||||
})
|
||||
t.AssertNil(err)
|
||||
// Scores.
|
||||
for j := 1; j <= 5; j++ {
|
||||
_, err = db.Insert(tableUserScores, g.Map{
|
||||
_, err = db.Insert(ctx, tableUserScores, g.Map{
|
||||
"uid": i,
|
||||
"score": j,
|
||||
})
|
||||
@ -1093,7 +1093,7 @@ func Test_Table_Relation_EmptyData(t *testing.T) {
|
||||
tableUserDetail = "user_detail_" + gtime.TimestampMicroStr()
|
||||
tableUserScores = "user_scores_" + gtime.TimestampMicroStr()
|
||||
)
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE %s (
|
||||
uid int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
name varchar(45) NOT NULL,
|
||||
@ -1104,7 +1104,7 @@ CREATE TABLE %s (
|
||||
}
|
||||
defer dropTable(tableUser)
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE %s (
|
||||
uid int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
address varchar(45) NOT NULL,
|
||||
@ -1115,7 +1115,7 @@ CREATE TABLE %s (
|
||||
}
|
||||
defer dropTable(tableUserDetail)
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE %s (
|
||||
id int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
uid int(10) unsigned NOT NULL,
|
||||
@ -1304,7 +1304,7 @@ func Test_Table_Relation_NoneEqualDataSize(t *testing.T) {
|
||||
tableUserDetail = "user_detail_" + gtime.TimestampMicroStr()
|
||||
tableUserScores = "user_scores_" + gtime.TimestampMicroStr()
|
||||
)
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE %s (
|
||||
uid int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
name varchar(45) NOT NULL,
|
||||
@ -1315,7 +1315,7 @@ CREATE TABLE %s (
|
||||
}
|
||||
defer dropTable(tableUser)
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE %s (
|
||||
uid int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
address varchar(45) NOT NULL,
|
||||
@ -1326,7 +1326,7 @@ CREATE TABLE %s (
|
||||
}
|
||||
defer dropTable(tableUserDetail)
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE %s (
|
||||
id int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
uid int(10) unsigned NOT NULL,
|
||||
@ -1362,20 +1362,20 @@ CREATE TABLE %s (
|
||||
var err error
|
||||
for i := 1; i <= 5; i++ {
|
||||
// User.
|
||||
_, err = db.Insert(tableUser, g.Map{
|
||||
_, err = db.Insert(ctx, tableUser, g.Map{
|
||||
"uid": i,
|
||||
"name": fmt.Sprintf(`name_%d`, i),
|
||||
})
|
||||
t.AssertNil(err)
|
||||
// Detail.
|
||||
//_, err = db.Insert(tableUserDetail, g.Map{
|
||||
//_, err = db.Insert(ctx, tableUserDetail, g.Map{
|
||||
// "uid": i,
|
||||
// "address": fmt.Sprintf(`address_%d`, i),
|
||||
//})
|
||||
//t.AssertNil(err)
|
||||
// Scores.
|
||||
//for j := 1; j <= 5; j++ {
|
||||
// _, err = db.Insert(tableUserScores, g.Map{
|
||||
// _, err = db.Insert(ctx, tableUserScores, g.Map{
|
||||
// "uid": i,
|
||||
// "score": j,
|
||||
// })
|
||||
@ -1559,7 +1559,7 @@ func Test_Table_Relation_EmbeddedStruct(t *testing.T) {
|
||||
tableUserDetail = "user_detail_" + gtime.TimestampMicroStr()
|
||||
tableUserScores = "user_scores_" + gtime.TimestampMicroStr()
|
||||
)
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE %s (
|
||||
uid int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
name varchar(45) NOT NULL,
|
||||
@ -1570,7 +1570,7 @@ CREATE TABLE %s (
|
||||
}
|
||||
defer dropTable(tableUser)
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE %s (
|
||||
uid int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
address varchar(45) NOT NULL,
|
||||
@ -1581,7 +1581,7 @@ CREATE TABLE %s (
|
||||
}
|
||||
defer dropTable(tableUserDetail)
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE %s (
|
||||
id int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
uid int(10) unsigned NOT NULL,
|
||||
@ -1615,20 +1615,20 @@ CREATE TABLE %s (
|
||||
var err error
|
||||
for i := 1; i <= 5; i++ {
|
||||
// User.
|
||||
_, err = db.Insert(tableUser, g.Map{
|
||||
_, err = db.Insert(ctx, tableUser, g.Map{
|
||||
"uid": i,
|
||||
"name": fmt.Sprintf(`name_%d`, i),
|
||||
})
|
||||
t.AssertNil(err)
|
||||
// Detail.
|
||||
_, err = db.Insert(tableUserDetail, g.Map{
|
||||
_, err = db.Insert(ctx, tableUserDetail, g.Map{
|
||||
"uid": i,
|
||||
"address": fmt.Sprintf(`address_%d`, i),
|
||||
})
|
||||
t.AssertNil(err)
|
||||
// Scores.
|
||||
for j := 1; j <= 5; j++ {
|
||||
_, err = db.Insert(tableUserScores, g.Map{
|
||||
_, err = db.Insert(ctx, tableUserScores, g.Map{
|
||||
"uid": i,
|
||||
"score": j,
|
||||
})
|
||||
|
||||
@ -91,7 +91,7 @@ func Test_Table_Relation_With_Scan(t *testing.T) {
|
||||
tableUserDetail = "user_detail"
|
||||
tableUserScores = "user_score"
|
||||
)
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
id int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
name varchar(45) NOT NULL,
|
||||
@ -102,7 +102,7 @@ PRIMARY KEY (id)
|
||||
}
|
||||
defer dropTable(tableUser)
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
uid int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
address varchar(45) NOT NULL,
|
||||
@ -113,7 +113,7 @@ PRIMARY KEY (uid)
|
||||
}
|
||||
defer dropTable(tableUserDetail)
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
id int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
uid int(10) unsigned NOT NULL,
|
||||
@ -289,7 +289,7 @@ func Test_Table_Relation_With(t *testing.T) {
|
||||
tableUserDetail = "user_detail"
|
||||
tableUserScores = "user_scores"
|
||||
)
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
id int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
name varchar(45) NOT NULL,
|
||||
@ -300,7 +300,7 @@ PRIMARY KEY (id)
|
||||
}
|
||||
defer dropTable(tableUser)
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
uid int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
address varchar(45) NOT NULL,
|
||||
@ -311,7 +311,7 @@ PRIMARY KEY (uid)
|
||||
}
|
||||
defer dropTable(tableUserDetail)
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
id int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
uid int(10) unsigned NOT NULL,
|
||||
@ -348,20 +348,20 @@ PRIMARY KEY (id)
|
||||
var err error
|
||||
for i := 1; i <= 5; i++ {
|
||||
// User.
|
||||
_, err = db.Insert(tableUser, g.Map{
|
||||
_, err = db.Insert(ctx, tableUser, g.Map{
|
||||
"id": i,
|
||||
"name": fmt.Sprintf(`name_%d`, i),
|
||||
})
|
||||
gtest.Assert(err, nil)
|
||||
// Detail.
|
||||
_, err = db.Insert(tableUserDetail, g.Map{
|
||||
_, err = db.Insert(ctx, tableUserDetail, g.Map{
|
||||
"uid": i,
|
||||
"address": fmt.Sprintf(`address_%d`, i),
|
||||
})
|
||||
gtest.Assert(err, nil)
|
||||
// Scores.
|
||||
for j := 1; j <= 5; j++ {
|
||||
_, err = db.Insert(tableUserScores, g.Map{
|
||||
_, err = db.Insert(ctx, tableUserScores, g.Map{
|
||||
"uid": i,
|
||||
"score": j,
|
||||
})
|
||||
@ -489,7 +489,7 @@ func Test_Table_Relation_WithAll(t *testing.T) {
|
||||
tableUserDetail = "user_detail"
|
||||
tableUserScores = "user_scores"
|
||||
)
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
id int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
name varchar(45) NOT NULL,
|
||||
@ -500,7 +500,7 @@ PRIMARY KEY (id)
|
||||
}
|
||||
defer dropTable(tableUser)
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
uid int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
address varchar(45) NOT NULL,
|
||||
@ -511,7 +511,7 @@ PRIMARY KEY (uid)
|
||||
}
|
||||
defer dropTable(tableUserDetail)
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
id int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
uid int(10) unsigned NOT NULL,
|
||||
@ -548,20 +548,20 @@ PRIMARY KEY (id)
|
||||
var err error
|
||||
for i := 1; i <= 5; i++ {
|
||||
// User.
|
||||
_, err = db.Insert(tableUser, g.Map{
|
||||
_, err = db.Insert(ctx, tableUser, g.Map{
|
||||
"id": i,
|
||||
"name": fmt.Sprintf(`name_%d`, i),
|
||||
})
|
||||
gtest.Assert(err, nil)
|
||||
// Detail.
|
||||
_, err = db.Insert(tableUserDetail, g.Map{
|
||||
_, err = db.Insert(ctx, tableUserDetail, g.Map{
|
||||
"uid": i,
|
||||
"address": fmt.Sprintf(`address_%d`, i),
|
||||
})
|
||||
gtest.Assert(err, nil)
|
||||
// Scores.
|
||||
for j := 1; j <= 5; j++ {
|
||||
_, err = db.Insert(tableUserScores, g.Map{
|
||||
_, err = db.Insert(ctx, tableUserScores, g.Map{
|
||||
"uid": i,
|
||||
"score": j,
|
||||
})
|
||||
@ -604,7 +604,7 @@ func Test_Table_Relation_WithAll_List(t *testing.T) {
|
||||
tableUserDetail = "user_detail"
|
||||
tableUserScores = "user_scores"
|
||||
)
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
id int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
name varchar(45) NOT NULL,
|
||||
@ -615,7 +615,7 @@ PRIMARY KEY (id)
|
||||
}
|
||||
defer dropTable(tableUser)
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
uid int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
address varchar(45) NOT NULL,
|
||||
@ -626,7 +626,7 @@ PRIMARY KEY (uid)
|
||||
}
|
||||
defer dropTable(tableUserDetail)
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
id int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
uid int(10) unsigned NOT NULL,
|
||||
@ -663,20 +663,20 @@ PRIMARY KEY (id)
|
||||
var err error
|
||||
for i := 1; i <= 5; i++ {
|
||||
// User.
|
||||
_, err = db.Insert(tableUser, g.Map{
|
||||
_, err = db.Insert(ctx, tableUser, g.Map{
|
||||
"id": i,
|
||||
"name": fmt.Sprintf(`name_%d`, i),
|
||||
})
|
||||
gtest.Assert(err, nil)
|
||||
// Detail.
|
||||
_, err = db.Insert(tableUserDetail, g.Map{
|
||||
_, err = db.Insert(ctx, tableUserDetail, g.Map{
|
||||
"uid": i,
|
||||
"address": fmt.Sprintf(`address_%d`, i),
|
||||
})
|
||||
gtest.Assert(err, nil)
|
||||
// Scores.
|
||||
for j := 1; j <= 5; j++ {
|
||||
_, err = db.Insert(tableUserScores, g.Map{
|
||||
_, err = db.Insert(ctx, tableUserScores, g.Map{
|
||||
"uid": i,
|
||||
"score": j,
|
||||
})
|
||||
@ -745,7 +745,7 @@ func Test_Table_Relation_WithAllCondition_List(t *testing.T) {
|
||||
tableUserDetail = "user_detail"
|
||||
tableUserScores = "user_scores"
|
||||
)
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
id int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
name varchar(45) NOT NULL,
|
||||
@ -756,7 +756,7 @@ PRIMARY KEY (id)
|
||||
}
|
||||
defer dropTable(tableUser)
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
uid int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
address varchar(45) NOT NULL,
|
||||
@ -767,7 +767,7 @@ PRIMARY KEY (uid)
|
||||
}
|
||||
defer dropTable(tableUserDetail)
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
id int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
uid int(10) unsigned NOT NULL,
|
||||
@ -804,20 +804,20 @@ PRIMARY KEY (id)
|
||||
var err error
|
||||
for i := 1; i <= 5; i++ {
|
||||
// User.
|
||||
_, err = db.Insert(tableUser, g.Map{
|
||||
_, err = db.Insert(ctx, tableUser, g.Map{
|
||||
"id": i,
|
||||
"name": fmt.Sprintf(`name_%d`, i),
|
||||
})
|
||||
gtest.Assert(err, nil)
|
||||
// Detail.
|
||||
_, err = db.Insert(tableUserDetail, g.Map{
|
||||
_, err = db.Insert(ctx, tableUserDetail, g.Map{
|
||||
"uid": i,
|
||||
"address": fmt.Sprintf(`address_%d`, i),
|
||||
})
|
||||
gtest.Assert(err, nil)
|
||||
// Scores.
|
||||
for j := 1; j <= 5; j++ {
|
||||
_, err = db.Insert(tableUserScores, g.Map{
|
||||
_, err = db.Insert(ctx, tableUserScores, g.Map{
|
||||
"uid": i,
|
||||
"score": j,
|
||||
})
|
||||
@ -881,7 +881,7 @@ func Test_Table_Relation_WithAll_Embedded_With_SelfMaintained_Attributes(t *test
|
||||
tableUserDetail = "user_detail"
|
||||
tableUserScores = "user_scores"
|
||||
)
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
id int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
name varchar(45) NOT NULL,
|
||||
@ -892,7 +892,7 @@ PRIMARY KEY (id)
|
||||
}
|
||||
defer dropTable(tableUser)
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
uid int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
address varchar(45) NOT NULL,
|
||||
@ -903,7 +903,7 @@ PRIMARY KEY (uid)
|
||||
}
|
||||
defer dropTable(tableUserDetail)
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
id int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
uid int(10) unsigned NOT NULL,
|
||||
@ -940,20 +940,20 @@ PRIMARY KEY (id)
|
||||
var err error
|
||||
for i := 1; i <= 5; i++ {
|
||||
// User.
|
||||
_, err = db.Insert(tableUser, g.Map{
|
||||
_, err = db.Insert(ctx, tableUser, g.Map{
|
||||
"id": i,
|
||||
"name": fmt.Sprintf(`name_%d`, i),
|
||||
})
|
||||
gtest.Assert(err, nil)
|
||||
// Detail.
|
||||
_, err = db.Insert(tableUserDetail, g.Map{
|
||||
_, err = db.Insert(ctx, tableUserDetail, g.Map{
|
||||
"uid": i,
|
||||
"address": fmt.Sprintf(`address_%d`, i),
|
||||
})
|
||||
gtest.Assert(err, nil)
|
||||
// Scores.
|
||||
for j := 1; j <= 5; j++ {
|
||||
_, err = db.Insert(tableUserScores, g.Map{
|
||||
_, err = db.Insert(ctx, tableUserScores, g.Map{
|
||||
"uid": i,
|
||||
"score": j,
|
||||
})
|
||||
@ -996,7 +996,7 @@ func Test_Table_Relation_WithAll_Embedded_Without_SelfMaintained_Attributes(t *t
|
||||
tableUserDetail = "user_detail"
|
||||
tableUserScores = "user_scores"
|
||||
)
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
id int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
name varchar(45) NOT NULL,
|
||||
@ -1007,7 +1007,7 @@ PRIMARY KEY (id)
|
||||
}
|
||||
defer dropTable(tableUser)
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
uid int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
address varchar(45) NOT NULL,
|
||||
@ -1018,7 +1018,7 @@ PRIMARY KEY (uid)
|
||||
}
|
||||
defer dropTable(tableUserDetail)
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
id int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
uid int(10) unsigned NOT NULL,
|
||||
@ -1060,20 +1060,20 @@ PRIMARY KEY (id)
|
||||
var err error
|
||||
for i := 1; i <= 5; i++ {
|
||||
// User.
|
||||
_, err = db.Insert(tableUser, g.Map{
|
||||
_, err = db.Insert(ctx, tableUser, g.Map{
|
||||
"id": i,
|
||||
"name": fmt.Sprintf(`name_%d`, i),
|
||||
})
|
||||
gtest.Assert(err, nil)
|
||||
// Detail.
|
||||
_, err = db.Insert(tableUserDetail, g.Map{
|
||||
_, err = db.Insert(ctx, tableUserDetail, g.Map{
|
||||
"uid": i,
|
||||
"address": fmt.Sprintf(`address_%d`, i),
|
||||
})
|
||||
gtest.Assert(err, nil)
|
||||
// Scores.
|
||||
for j := 1; j <= 5; j++ {
|
||||
_, err = db.Insert(tableUserScores, g.Map{
|
||||
_, err = db.Insert(ctx, tableUserScores, g.Map{
|
||||
"uid": i,
|
||||
"score": j,
|
||||
})
|
||||
@ -1119,7 +1119,7 @@ func Test_Table_Relation_WithAll_Embedded_WithoutMeta(t *testing.T) {
|
||||
tableUserDetail = "user_detail"
|
||||
tableUserScores = "user_scores"
|
||||
)
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
id int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
name varchar(45) NOT NULL,
|
||||
@ -1130,7 +1130,7 @@ PRIMARY KEY (id)
|
||||
}
|
||||
defer dropTable(tableUser)
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
uid int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
address varchar(45) NOT NULL,
|
||||
@ -1141,7 +1141,7 @@ PRIMARY KEY (uid)
|
||||
}
|
||||
defer dropTable(tableUserDetail)
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
id int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
uid int(10) unsigned NOT NULL,
|
||||
@ -1179,20 +1179,20 @@ PRIMARY KEY (id)
|
||||
var err error
|
||||
for i := 1; i <= 5; i++ {
|
||||
// User.
|
||||
_, err = db.Insert(tableUser, g.Map{
|
||||
_, err = db.Insert(ctx, tableUser, g.Map{
|
||||
"id": i,
|
||||
"name": fmt.Sprintf(`name_%d`, i),
|
||||
})
|
||||
gtest.Assert(err, nil)
|
||||
// Detail.
|
||||
_, err = db.Insert(tableUserDetail, g.Map{
|
||||
_, err = db.Insert(ctx, tableUserDetail, g.Map{
|
||||
"uid": i,
|
||||
"address": fmt.Sprintf(`address_%d`, i),
|
||||
})
|
||||
gtest.Assert(err, nil)
|
||||
// Scores.
|
||||
for j := 1; j <= 5; j++ {
|
||||
_, err = db.Insert(tableUserScores, g.Map{
|
||||
_, err = db.Insert(ctx, tableUserScores, g.Map{
|
||||
"uid": i,
|
||||
"score": j,
|
||||
})
|
||||
@ -1235,7 +1235,7 @@ func Test_Table_Relation_WithAll_AttributeStructAlsoHasWithTag(t *testing.T) {
|
||||
tableUserDetail = "user_detail"
|
||||
tableUserScores = "user_scores"
|
||||
)
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
id int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
name varchar(45) NOT NULL,
|
||||
@ -1246,7 +1246,7 @@ PRIMARY KEY (id)
|
||||
}
|
||||
defer dropTable(tableUser)
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
uid int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
address varchar(45) NOT NULL,
|
||||
@ -1257,7 +1257,7 @@ PRIMARY KEY (uid)
|
||||
}
|
||||
defer dropTable(tableUserDetail)
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
id int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
uid int(10) unsigned NOT NULL,
|
||||
@ -1294,20 +1294,20 @@ PRIMARY KEY (id)
|
||||
var err error
|
||||
for i := 1; i <= 5; i++ {
|
||||
// User.
|
||||
_, err = db.Insert(tableUser, g.Map{
|
||||
_, err = db.Insert(ctx, tableUser, g.Map{
|
||||
"id": i,
|
||||
"name": fmt.Sprintf(`name_%d`, i),
|
||||
})
|
||||
gtest.Assert(err, nil)
|
||||
// Detail.
|
||||
_, err = db.Insert(tableUserDetail, g.Map{
|
||||
_, err = db.Insert(ctx, tableUserDetail, g.Map{
|
||||
"uid": i,
|
||||
"address": fmt.Sprintf(`address_%d`, i),
|
||||
})
|
||||
gtest.Assert(err, nil)
|
||||
// Scores.
|
||||
for j := 1; j <= 5; j++ {
|
||||
_, err = db.Insert(tableUserScores, g.Map{
|
||||
_, err = db.Insert(ctx, tableUserScores, g.Map{
|
||||
"uid": i,
|
||||
"score": j,
|
||||
})
|
||||
@ -1351,7 +1351,7 @@ func Test_Table_Relation_WithAll_AttributeStructAlsoHasWithTag_MoreDeep(t *testi
|
||||
tableUserDetail = "user_detail"
|
||||
tableUserScores = "user_scores"
|
||||
)
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
id int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
name varchar(45) NOT NULL,
|
||||
@ -1362,7 +1362,7 @@ PRIMARY KEY (id)
|
||||
}
|
||||
defer dropTable(tableUser)
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
uid int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
address varchar(45) NOT NULL,
|
||||
@ -1373,7 +1373,7 @@ PRIMARY KEY (uid)
|
||||
}
|
||||
defer dropTable(tableUserDetail)
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
id int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
uid int(10) unsigned NOT NULL,
|
||||
@ -1434,20 +1434,20 @@ PRIMARY KEY (id)
|
||||
var err error
|
||||
for i := 1; i <= 5; i++ {
|
||||
// User.
|
||||
_, err = db.Insert(tableUser, g.Map{
|
||||
_, err = db.Insert(ctx, tableUser, g.Map{
|
||||
"id": i,
|
||||
"name": fmt.Sprintf(`name_%d`, i),
|
||||
})
|
||||
gtest.Assert(err, nil)
|
||||
// Detail.
|
||||
_, err = db.Insert(tableUserDetail, g.Map{
|
||||
_, err = db.Insert(ctx, tableUserDetail, g.Map{
|
||||
"uid": i,
|
||||
"address": fmt.Sprintf(`address_%d`, i),
|
||||
})
|
||||
gtest.Assert(err, nil)
|
||||
// Scores.
|
||||
for j := 1; j <= 5; j++ {
|
||||
_, err = db.Insert(tableUserScores, g.Map{
|
||||
_, err = db.Insert(ctx, tableUserScores, g.Map{
|
||||
"uid": i,
|
||||
"score": j,
|
||||
})
|
||||
@ -1497,7 +1497,7 @@ func Test_Table_Relation_With_AttributeStructAlsoHasWithTag_MoreDeep(t *testing.
|
||||
tableUserDetail = "user_detail"
|
||||
tableUserScores = "user_scores"
|
||||
)
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
id int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
name varchar(45) NOT NULL,
|
||||
@ -1508,7 +1508,7 @@ PRIMARY KEY (id)
|
||||
}
|
||||
defer dropTable(tableUser)
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
uid int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
address varchar(45) NOT NULL,
|
||||
@ -1519,7 +1519,7 @@ PRIMARY KEY (uid)
|
||||
}
|
||||
defer dropTable(tableUserDetail)
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
id int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
uid int(10) unsigned NOT NULL,
|
||||
@ -1580,20 +1580,20 @@ PRIMARY KEY (id)
|
||||
var err error
|
||||
for i := 1; i <= 5; i++ {
|
||||
// User.
|
||||
_, err = db.Insert(tableUser, g.Map{
|
||||
_, err = db.Insert(ctx, tableUser, g.Map{
|
||||
"id": i,
|
||||
"name": fmt.Sprintf(`name_%d`, i),
|
||||
})
|
||||
gtest.Assert(err, nil)
|
||||
// Detail.
|
||||
_, err = db.Insert(tableUserDetail, g.Map{
|
||||
_, err = db.Insert(ctx, tableUserDetail, g.Map{
|
||||
"uid": i,
|
||||
"address": fmt.Sprintf(`address_%d`, i),
|
||||
})
|
||||
gtest.Assert(err, nil)
|
||||
// Scores.
|
||||
for j := 1; j <= 5; j++ {
|
||||
_, err = db.Insert(tableUserScores, g.Map{
|
||||
_, err = db.Insert(ctx, tableUserScores, g.Map{
|
||||
"uid": i,
|
||||
"score": j,
|
||||
})
|
||||
@ -1644,7 +1644,7 @@ func Test_Table_Relation_With_MultipleDepends1(t *testing.T) {
|
||||
dropTable("table_c")
|
||||
}()
|
||||
for _, v := range gstr.SplitAndTrim(gfile.GetContents(gdebug.TestDataPath("with_multiple_depends.sql")), ";") {
|
||||
if _, err := db.Exec(v); err != nil {
|
||||
if _, err := db.Exec(ctx, v); err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
}
|
||||
@ -1716,7 +1716,7 @@ func Test_Table_Relation_With_MultipleDepends2(t *testing.T) {
|
||||
dropTable("table_c")
|
||||
}()
|
||||
for _, v := range gstr.SplitAndTrim(gfile.GetContents(gdebug.TestDataPath("with_multiple_depends.sql")), ";") {
|
||||
if _, err := db.Exec(v); err != nil {
|
||||
if _, err := db.Exec(ctx, v); err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
}
|
||||
@ -1803,7 +1803,7 @@ func Test_Table_Relation_With_MultipleDepends_Embedded(t *testing.T) {
|
||||
dropTable("table_c")
|
||||
}()
|
||||
for _, v := range gstr.SplitAndTrim(gfile.GetContents(gdebug.TestDataPath("with_multiple_depends.sql")), ";") {
|
||||
if _, err := db.Exec(v); err != nil {
|
||||
if _, err := db.Exec(ctx, v); err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
}
|
||||
@ -1874,7 +1874,7 @@ func Test_Table_Relation_WithAll_Embedded_Meta_NameMatchingRule(t *testing.T) {
|
||||
tableUserDetail = "user_detail1"
|
||||
tableUserScores = "user_scores1"
|
||||
)
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
id int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
name varchar(45) NOT NULL,
|
||||
@ -1885,7 +1885,7 @@ PRIMARY KEY (id)
|
||||
}
|
||||
defer dropTable(tableUser)
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
user_id int(10) unsigned NOT NULL,
|
||||
address varchar(45) NOT NULL,
|
||||
@ -1896,7 +1896,7 @@ PRIMARY KEY (user_id)
|
||||
}
|
||||
defer dropTable(tableUserDetail)
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
id int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
user_id int(10) unsigned NOT NULL,
|
||||
@ -1938,20 +1938,20 @@ PRIMARY KEY (id)
|
||||
var err error
|
||||
for i := 1; i <= 5; i++ {
|
||||
// User.
|
||||
_, err = db.Insert(tableUser, g.Map{
|
||||
_, err = db.Insert(ctx, tableUser, g.Map{
|
||||
"id": i,
|
||||
"name": fmt.Sprintf(`name_%d`, i),
|
||||
})
|
||||
gtest.AssertNil(err)
|
||||
// Detail.
|
||||
_, err = db.Insert(tableUserDetail, g.Map{
|
||||
_, err = db.Insert(ctx, tableUserDetail, g.Map{
|
||||
"user_id": i,
|
||||
"address": fmt.Sprintf(`address_%d`, i),
|
||||
})
|
||||
gtest.AssertNil(err)
|
||||
// Scores.
|
||||
for j := 1; j <= 5; j++ {
|
||||
_, err = db.Insert(tableUserScores, g.Map{
|
||||
_, err = db.Insert(ctx, tableUserScores, g.Map{
|
||||
"user_id": i,
|
||||
"score": j,
|
||||
})
|
||||
@ -2000,7 +2000,7 @@ func Test_With_Feature_Issue1401(t *testing.T) {
|
||||
)
|
||||
array := gstr.SplitAndTrim(gtest.TestDataContent(`issue1401.sql`), ";")
|
||||
for _, v := range array {
|
||||
if _, err := db.Exec(v); err != nil {
|
||||
if _, err := db.Exec(ctx, v); err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
}
|
||||
@ -2042,7 +2042,7 @@ func Test_With_Feature_Issue1412(t *testing.T) {
|
||||
)
|
||||
array := gstr.SplitAndTrim(gtest.TestDataContent(`issue1412.sql`), ";")
|
||||
for _, v := range array {
|
||||
if _, err := db.Exec(v); err != nil {
|
||||
if _, err := db.Exec(ctx, v); err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -36,12 +36,12 @@ func Test_Ctx_Query(t *testing.T) {
|
||||
defer db.SetDebug(false)
|
||||
ctx := context.WithValue(context.Background(), "TraceId", "12345678")
|
||||
ctx = context.WithValue(ctx, "SpanId", "0.1")
|
||||
db.Ctx(ctx).Query("select 1")
|
||||
db.Query(ctx, "select 1")
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
db.SetDebug(true)
|
||||
defer db.SetDebug(false)
|
||||
db.Query("select 2")
|
||||
db.Query(ctx, "select 2")
|
||||
})
|
||||
}
|
||||
|
||||
@ -62,23 +62,3 @@ func Test_Ctx_Model(t *testing.T) {
|
||||
db.Model(table).All()
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Ctx_Strict(t *testing.T) {
|
||||
table := createInitTableWithDb(dbCtxStrict)
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
_, err := dbCtxStrict.Query("select 1")
|
||||
t.AssertNE(err, nil)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
r, err := dbCtxStrict.Model(table).All()
|
||||
t.AssertNE(err, nil)
|
||||
t.Assert(len(r), 0)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
r, err := dbCtxStrict.Model(table).Ctx(context.TODO()).All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(r), TableSize)
|
||||
})
|
||||
}
|
||||
|
||||
@ -138,7 +138,7 @@ func Test_Model_Fields_AutoFilterInJoinStatement(t *testing.T) {
|
||||
table1 := "user"
|
||||
table2 := "score"
|
||||
table3 := "info"
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
id int(11) NOT NULL AUTO_INCREMENT,
|
||||
name varchar(500) NOT NULL DEFAULT '',
|
||||
@ -155,7 +155,7 @@ func Test_Model_Fields_AutoFilterInJoinStatement(t *testing.T) {
|
||||
})
|
||||
t.AssertNil(err)
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
id int(11) NOT NULL AUTO_INCREMENT,
|
||||
user_id int(11) NOT NULL DEFAULT 0,
|
||||
@ -174,7 +174,7 @@ func Test_Model_Fields_AutoFilterInJoinStatement(t *testing.T) {
|
||||
})
|
||||
t.AssertNil(err)
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
id int(11) NOT NULL AUTO_INCREMENT,
|
||||
user_id int(11) NOT NULL DEFAULT 0,
|
||||
|
||||
@ -7,6 +7,7 @@
|
||||
package gdb
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/container/gvar"
|
||||
"github.com/gogf/gf/os/gcmd"
|
||||
@ -23,6 +24,7 @@ const (
|
||||
|
||||
var (
|
||||
db DB
|
||||
ctx = context.TODO()
|
||||
configNode ConfigNode
|
||||
)
|
||||
|
||||
@ -54,14 +56,14 @@ func init() {
|
||||
db = r
|
||||
}
|
||||
schemaTemplate := "CREATE DATABASE IF NOT EXISTS `%s` CHARACTER SET UTF8"
|
||||
if _, err := db.Exec(fmt.Sprintf(schemaTemplate, SCHEMA)); err != nil {
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(schemaTemplate, SCHEMA)); err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
db.SetSchema(SCHEMA)
|
||||
}
|
||||
|
||||
func dropTable(table string) {
|
||||
if _, err := db.Exec(fmt.Sprintf("DROP TABLE IF EXISTS `%s`", table)); err != nil {
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf("DROP TABLE IF EXISTS `%s`", table)); err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
}
|
||||
@ -173,7 +175,7 @@ func Test_Func_addTablePrefix(t *testing.T) {
|
||||
|
||||
func Test_Model_getSoftFieldName(t *testing.T) {
|
||||
table1 := "soft_deleting_table_" + gtime.TimestampNanoStr()
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE %s (
|
||||
id int(11) NOT NULL,
|
||||
name varchar(45) DEFAULT NULL,
|
||||
@ -188,7 +190,7 @@ CREATE TABLE %s (
|
||||
defer dropTable(table1)
|
||||
|
||||
table2 := "soft_deleting_table_" + gtime.TimestampNanoStr()
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE %s (
|
||||
id int(11) NOT NULL,
|
||||
name varchar(45) DEFAULT NULL,
|
||||
@ -212,7 +214,7 @@ CREATE TABLE %s (
|
||||
|
||||
func Test_Model_getConditionForSoftDeleting(t *testing.T) {
|
||||
table1 := "soft_deleting_table_" + gtime.TimestampNanoStr()
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE %s (
|
||||
id1 int(11) NOT NULL,
|
||||
name1 varchar(45) DEFAULT NULL,
|
||||
@ -227,7 +229,7 @@ CREATE TABLE %s (
|
||||
defer dropTable(table1)
|
||||
|
||||
table2 := "soft_deleting_table_" + gtime.TimestampNanoStr()
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE %s (
|
||||
id2 int(11) NOT NULL,
|
||||
name2 varchar(45) DEFAULT NULL,
|
||||
|
||||
@ -34,16 +34,16 @@ func Test_DB_Ping(t *testing.T) {
|
||||
|
||||
func Test_DB_Query(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
_, err := db.Query("SELECT ?", 1)
|
||||
_, err := db.Query(ctx, "SELECT ?", 1)
|
||||
t.AssertNil(err)
|
||||
|
||||
_, err = db.Query("SELECT ?+?", 1, 2)
|
||||
_, err = db.Query(ctx, "SELECT ?+?", 1, 2)
|
||||
t.AssertNil(err)
|
||||
|
||||
_, err = db.Query("SELECT ?+?", g.Slice{1, 2})
|
||||
_, err = db.Query(ctx, "SELECT ?+?", g.Slice{1, 2})
|
||||
t.AssertNil(err)
|
||||
|
||||
_, err = db.Query("ERROR")
|
||||
_, err = db.Query(ctx, "ERROR")
|
||||
t.AssertNE(err, nil)
|
||||
})
|
||||
|
||||
@ -51,10 +51,10 @@ func Test_DB_Query(t *testing.T) {
|
||||
|
||||
func Test_DB_Exec(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
_, err := db.Exec("SELECT ?", 1)
|
||||
_, err := db.Exec(ctx, "SELECT ?", 1)
|
||||
t.AssertNil(err)
|
||||
|
||||
_, err = db.Exec("ERROR")
|
||||
_, err = db.Exec(ctx, "ERROR")
|
||||
t.AssertNE(err, nil)
|
||||
})
|
||||
|
||||
@ -62,7 +62,7 @@ func Test_DB_Exec(t *testing.T) {
|
||||
|
||||
func Test_DB_Prepare(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
st, err := db.Prepare("SELECT 100")
|
||||
st, err := db.Prepare(ctx, "SELECT 100")
|
||||
t.AssertNil(err)
|
||||
|
||||
rows, err := st.Query()
|
||||
@ -82,7 +82,7 @@ func Test_DB_Insert(t *testing.T) {
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
_, err := db.Insert(table, g.Map{
|
||||
_, err := db.Insert(ctx, table, g.Map{
|
||||
"id": 1,
|
||||
"passport": "t1",
|
||||
"password": "25d55ad283aa400af464c76d713c07ad",
|
||||
@ -92,7 +92,7 @@ func Test_DB_Insert(t *testing.T) {
|
||||
t.AssertNil(err)
|
||||
|
||||
// normal map
|
||||
result, err := db.Insert(table, g.Map{
|
||||
result, err := db.Insert(ctx, table, g.Map{
|
||||
"id": "2",
|
||||
"passport": "t2",
|
||||
"password": "25d55ad283aa400af464c76d713c07ad",
|
||||
@ -112,7 +112,7 @@ func Test_DB_Insert(t *testing.T) {
|
||||
CreateTime string `json:"create_time"`
|
||||
}
|
||||
timeStr := gtime.Now().String()
|
||||
result, err = db.Insert(table, User{
|
||||
result, err = db.Insert(ctx, table, User{
|
||||
Id: 3,
|
||||
Passport: "user_3",
|
||||
Password: "25d55ad283aa400af464c76d713c07ad",
|
||||
@ -134,7 +134,7 @@ func Test_DB_Insert(t *testing.T) {
|
||||
|
||||
// *struct
|
||||
timeStr = gtime.Now().String()
|
||||
result, err = db.Insert(table, &User{
|
||||
result, err = db.Insert(ctx, table, &User{
|
||||
Id: 4,
|
||||
Passport: "t4",
|
||||
Password: "25d55ad283aa400af464c76d713c07ad",
|
||||
@ -155,7 +155,7 @@ func Test_DB_Insert(t *testing.T) {
|
||||
|
||||
// batch with Insert
|
||||
timeStr = gtime.Now().String()
|
||||
r, err := db.Insert(table, g.Slice{
|
||||
r, err := db.Insert(ctx, table, g.Slice{
|
||||
g.Map{
|
||||
"id": 200,
|
||||
"passport": "t200",
|
||||
@ -202,10 +202,10 @@ func Test_DB_Insert_WithStructAndSliceAttribute(t *testing.T) {
|
||||
"nickname": []string{"A", "B", "C"},
|
||||
"create_time": gtime.Now().String(),
|
||||
}
|
||||
_, err := db.Insert(table, data)
|
||||
_, err := db.Insert(ctx, table, data)
|
||||
t.AssertNil(err)
|
||||
|
||||
one, err := db.GetOne(fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 1)
|
||||
one, err := db.GetOne(ctx, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 1)
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], data["passport"])
|
||||
t.Assert(one["create_time"], data["create_time"])
|
||||
@ -232,10 +232,10 @@ func Test_DB_Insert_KeyFieldNameMapping(t *testing.T) {
|
||||
Nickname: "name_1",
|
||||
CreateTime: "2020-10-10 12:00:01",
|
||||
}
|
||||
_, err := db.Insert(table, data)
|
||||
_, err := db.Insert(ctx, table, data)
|
||||
t.AssertNil(err)
|
||||
|
||||
one, err := db.GetOne(fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 1)
|
||||
one, err := db.GetOne(ctx, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 1)
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], data.Passport)
|
||||
t.Assert(one["create_time"], data.CreateTime)
|
||||
@ -262,10 +262,10 @@ func Test_DB_Upadte_KeyFieldNameMapping(t *testing.T) {
|
||||
Nickname: "name_10",
|
||||
CreateTime: "2020-10-10 12:00:01",
|
||||
}
|
||||
_, err := db.Update(table, data, "id=1")
|
||||
_, err := db.Update(ctx, table, data, "id=1")
|
||||
t.AssertNil(err)
|
||||
|
||||
one, err := db.GetOne(fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 1)
|
||||
one, err := db.GetOne(ctx, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 1)
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], data.Passport)
|
||||
t.Assert(one["create_time"], data.CreateTime)
|
||||
@ -294,7 +294,7 @@ func Test_DB_Upadte_KeyFieldNameMapping(t *testing.T) {
|
||||
// Nickname: "name_1",
|
||||
// CreateTime: "2020-10-10 12:00:01",
|
||||
// }
|
||||
// _, err := db.Insert(table, data)
|
||||
// _, err := db.Insert(ctx, table, data)
|
||||
// t.AssertNE(err, nil)
|
||||
// })
|
||||
//}
|
||||
@ -303,7 +303,7 @@ func Test_DB_InsertIgnore(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
_, err := db.Insert(table, g.Map{
|
||||
_, err := db.Insert(ctx, table, g.Map{
|
||||
"id": 1,
|
||||
"passport": "t1",
|
||||
"password": "25d55ad283aa400af464c76d713c07ad",
|
||||
@ -313,7 +313,7 @@ func Test_DB_InsertIgnore(t *testing.T) {
|
||||
t.AssertNE(err, nil)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
_, err := db.InsertIgnore(table, g.Map{
|
||||
_, err := db.InsertIgnore(ctx, table, g.Map{
|
||||
"id": 1,
|
||||
"passport": "t1",
|
||||
"password": "25d55ad283aa400af464c76d713c07ad",
|
||||
@ -328,7 +328,7 @@ func Test_DB_BatchInsert(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
r, err := db.Insert(table, g.List{
|
||||
r, err := db.Insert(ctx, table, g.List{
|
||||
{
|
||||
"id": 2,
|
||||
"passport": "t2",
|
||||
@ -356,7 +356,7 @@ func Test_DB_BatchInsert(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
// []interface{}
|
||||
r, err := db.Insert(table, g.Slice{
|
||||
r, err := db.Insert(ctx, table, g.Slice{
|
||||
g.Map{
|
||||
"id": 2,
|
||||
"passport": "t2",
|
||||
@ -381,7 +381,7 @@ func Test_DB_BatchInsert(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
result, err := db.Insert(table, g.Map{
|
||||
result, err := db.Insert(ctx, table, g.Map{
|
||||
"id": 1,
|
||||
"passport": "t1",
|
||||
"password": "p1",
|
||||
@ -415,7 +415,7 @@ func Test_DB_BatchInsert_Struct(t *testing.T) {
|
||||
NickName: "T1",
|
||||
CreateTime: gtime.Now(),
|
||||
}
|
||||
result, err := db.Insert(table, user)
|
||||
result, err := db.Insert(ctx, table, user)
|
||||
t.AssertNil(err)
|
||||
n, _ := result.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
@ -428,7 +428,7 @@ func Test_DB_Save(t *testing.T) {
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
timeStr := gtime.Now().String()
|
||||
_, err := db.Save(table, g.Map{
|
||||
_, err := db.Save(ctx, table, g.Map{
|
||||
"id": 1,
|
||||
"passport": "t1",
|
||||
"password": "25d55ad283aa400af464c76d713c07ad",
|
||||
@ -453,7 +453,7 @@ func Test_DB_Replace(t *testing.T) {
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
timeStr := gtime.Now().String()
|
||||
_, err := db.Replace(table, g.Map{
|
||||
_, err := db.Replace(ctx, table, g.Map{
|
||||
"id": 1,
|
||||
"passport": "t1",
|
||||
"password": "25d55ad283aa400af464c76d713c07ad",
|
||||
@ -477,7 +477,7 @@ func Test_DB_Update(t *testing.T) {
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, err := db.Update(table, "password='987654321'", "id=3")
|
||||
result, err := db.Update(ctx, table, "password='987654321'", "id=3")
|
||||
t.AssertNil(err)
|
||||
n, _ := result.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
@ -496,19 +496,19 @@ func Test_DB_GetAll(t *testing.T) {
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, err := db.GetAll(fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 1)
|
||||
result, err := db.GetAll(ctx, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 1)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(result), 1)
|
||||
t.Assert(result[0]["id"].Int(), 1)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, err := db.GetAll(fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), g.Slice{1})
|
||||
result, err := db.GetAll(ctx, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), g.Slice{1})
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(result), 1)
|
||||
t.Assert(result[0]["id"].Int(), 1)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, err := db.GetAll(fmt.Sprintf("SELECT * FROM %s WHERE id in(?)", table), g.Slice{1, 2, 3})
|
||||
result, err := db.GetAll(ctx, fmt.Sprintf("SELECT * FROM %s WHERE id in(?)", table), g.Slice{1, 2, 3})
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(result), 3)
|
||||
t.Assert(result[0]["id"].Int(), 1)
|
||||
@ -516,7 +516,7 @@ func Test_DB_GetAll(t *testing.T) {
|
||||
t.Assert(result[2]["id"].Int(), 3)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, err := db.GetAll(fmt.Sprintf("SELECT * FROM %s WHERE id in(?,?,?)", table), g.Slice{1, 2, 3})
|
||||
result, err := db.GetAll(ctx, fmt.Sprintf("SELECT * FROM %s WHERE id in(?,?,?)", table), g.Slice{1, 2, 3})
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(result), 3)
|
||||
t.Assert(result[0]["id"].Int(), 1)
|
||||
@ -524,7 +524,7 @@ func Test_DB_GetAll(t *testing.T) {
|
||||
t.Assert(result[2]["id"].Int(), 3)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, err := db.GetAll(fmt.Sprintf("SELECT * FROM %s WHERE id in(?,?,?)", table), g.Slice{1, 2, 3}...)
|
||||
result, err := db.GetAll(ctx, fmt.Sprintf("SELECT * FROM %s WHERE id in(?,?,?)", table), g.Slice{1, 2, 3}...)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(result), 3)
|
||||
t.Assert(result[0]["id"].Int(), 1)
|
||||
@ -532,7 +532,7 @@ func Test_DB_GetAll(t *testing.T) {
|
||||
t.Assert(result[2]["id"].Int(), 3)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, err := db.GetAll(fmt.Sprintf("SELECT * FROM %s WHERE id>=? AND id <=?", table), g.Slice{1, 3})
|
||||
result, err := db.GetAll(ctx, fmt.Sprintf("SELECT * FROM %s WHERE id>=? AND id <=?", table), g.Slice{1, 3})
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(result), 3)
|
||||
t.Assert(result[0]["id"].Int(), 1)
|
||||
@ -545,7 +545,7 @@ func Test_DB_GetOne(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
record, err := db.GetOne(fmt.Sprintf("SELECT * FROM %s WHERE passport=?", table), "user_1")
|
||||
record, err := db.GetOne(ctx, fmt.Sprintf("SELECT * FROM %s WHERE passport=?", table), "user_1")
|
||||
t.AssertNil(err)
|
||||
t.Assert(record["nickname"].String(), "name_1")
|
||||
})
|
||||
@ -555,7 +555,7 @@ func Test_DB_GetValue(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
value, err := db.GetValue(fmt.Sprintf("SELECT id FROM %s WHERE passport=?", table), "user_3")
|
||||
value, err := db.GetValue(ctx, fmt.Sprintf("SELECT id FROM %s WHERE passport=?", table), "user_3")
|
||||
t.AssertNil(err)
|
||||
t.Assert(value.Int(), 3)
|
||||
})
|
||||
@ -565,7 +565,7 @@ func Test_DB_GetCount(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
count, err := db.GetCount(fmt.Sprintf("SELECT * FROM %s", table))
|
||||
count, err := db.GetCount(ctx, fmt.Sprintf("SELECT * FROM %s", table))
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, TableSize)
|
||||
})
|
||||
@ -583,7 +583,7 @@ func Test_DB_GetStruct(t *testing.T) {
|
||||
CreateTime gtime.Time
|
||||
}
|
||||
user := new(User)
|
||||
err := db.GetScan(user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 3)
|
||||
err := db.GetScan(ctx, user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 3)
|
||||
t.AssertNil(err)
|
||||
t.Assert(user.NickName, "name_3")
|
||||
})
|
||||
@ -596,7 +596,7 @@ func Test_DB_GetStruct(t *testing.T) {
|
||||
CreateTime *gtime.Time
|
||||
}
|
||||
user := new(User)
|
||||
err := db.GetScan(user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 3)
|
||||
err := db.GetScan(ctx, user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 3)
|
||||
t.AssertNil(err)
|
||||
t.Assert(user.NickName, "name_3")
|
||||
})
|
||||
@ -614,7 +614,7 @@ func Test_DB_GetStructs(t *testing.T) {
|
||||
CreateTime gtime.Time
|
||||
}
|
||||
var users []User
|
||||
err := db.GetScan(&users, fmt.Sprintf("SELECT * FROM %s WHERE id>?", table), 1)
|
||||
err := db.GetScan(ctx, &users, fmt.Sprintf("SELECT * FROM %s WHERE id>?", table), 1)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(users), TableSize-1)
|
||||
t.Assert(users[0].Id, 2)
|
||||
@ -634,7 +634,7 @@ func Test_DB_GetStructs(t *testing.T) {
|
||||
CreateTime *gtime.Time
|
||||
}
|
||||
var users []User
|
||||
err := db.GetScan(&users, fmt.Sprintf("SELECT * FROM %s WHERE id>?", table), 1)
|
||||
err := db.GetScan(ctx, &users, fmt.Sprintf("SELECT * FROM %s WHERE id>?", table), 1)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(users), TableSize-1)
|
||||
t.Assert(users[0].Id, 2)
|
||||
@ -658,7 +658,7 @@ func Test_DB_GetScan(t *testing.T) {
|
||||
CreateTime gtime.Time
|
||||
}
|
||||
user := new(User)
|
||||
err := db.GetScan(user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 3)
|
||||
err := db.GetScan(ctx, user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 3)
|
||||
t.AssertNil(err)
|
||||
t.Assert(user.NickName, "name_3")
|
||||
})
|
||||
@ -671,7 +671,7 @@ func Test_DB_GetScan(t *testing.T) {
|
||||
CreateTime *gtime.Time
|
||||
}
|
||||
user := new(User)
|
||||
err := db.GetScan(user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 3)
|
||||
err := db.GetScan(ctx, user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 3)
|
||||
t.AssertNil(err)
|
||||
t.Assert(user.NickName, "name_3")
|
||||
})
|
||||
@ -685,7 +685,7 @@ func Test_DB_GetScan(t *testing.T) {
|
||||
CreateTime gtime.Time
|
||||
}
|
||||
var users []User
|
||||
err := db.GetScan(&users, fmt.Sprintf("SELECT * FROM %s WHERE id>?", table), 1)
|
||||
err := db.GetScan(ctx, &users, fmt.Sprintf("SELECT * FROM %s WHERE id>?", table), 1)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(users), TableSize-1)
|
||||
t.Assert(users[0].Id, 2)
|
||||
@ -705,7 +705,7 @@ func Test_DB_GetScan(t *testing.T) {
|
||||
CreateTime *gtime.Time
|
||||
}
|
||||
var users []User
|
||||
err := db.GetScan(&users, fmt.Sprintf("SELECT * FROM %s WHERE id>?", table), 1)
|
||||
err := db.GetScan(ctx, &users, fmt.Sprintf("SELECT * FROM %s WHERE id>?", table), 1)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(users), TableSize-1)
|
||||
t.Assert(users[0].Id, 2)
|
||||
@ -721,7 +721,7 @@ func Test_DB_Delete(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, err := db.Delete(table, 1)
|
||||
result, err := db.Delete(ctx, table, 1)
|
||||
t.AssertNil(err)
|
||||
n, _ := result.RowsAffected()
|
||||
t.Assert(n, TableSize)
|
||||
@ -733,7 +733,7 @@ func Test_DB_Time(t *testing.T) {
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, err := db.Insert(table, g.Map{
|
||||
result, err := db.Insert(ctx, table, g.Map{
|
||||
"id": 200,
|
||||
"passport": "t200",
|
||||
"password": "123456",
|
||||
@ -745,14 +745,14 @@ func Test_DB_Time(t *testing.T) {
|
||||
}
|
||||
n, _ := result.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
value, err := db.GetValue(fmt.Sprintf("select `passport` from `%s` where id=?", table), 200)
|
||||
value, err := db.GetValue(ctx, fmt.Sprintf("select `passport` from `%s` where id=?", table), 200)
|
||||
t.AssertNil(err)
|
||||
t.Assert(value.String(), "t200")
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
t1 := time.Now()
|
||||
result, err := db.Insert(table, g.Map{
|
||||
result, err := db.Insert(ctx, table, g.Map{
|
||||
"id": 300,
|
||||
"passport": "t300",
|
||||
"password": "123456",
|
||||
@ -764,13 +764,13 @@ func Test_DB_Time(t *testing.T) {
|
||||
}
|
||||
n, _ := result.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
value, err := db.GetValue(fmt.Sprintf("select `passport` from `%s` where id=?", table), 300)
|
||||
value, err := db.GetValue(ctx, fmt.Sprintf("select `passport` from `%s` where id=?", table), 300)
|
||||
t.AssertNil(err)
|
||||
t.Assert(value.String(), "t300")
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, err := db.Delete(table, 1)
|
||||
result, err := db.Delete(ctx, table, 1)
|
||||
t.AssertNil(err)
|
||||
n, _ := result.RowsAffected()
|
||||
t.Assert(n, 2)
|
||||
@ -780,7 +780,7 @@ func Test_DB_Time(t *testing.T) {
|
||||
func Test_DB_ToJson(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
_, err := db.Update(table, "create_time='2010-10-10 00:00:01'", "id=?", 1)
|
||||
_, err := db.Update(ctx, table, "create_time='2010-10-10 00:00:01'", "id=?", 1)
|
||||
gtest.AssertNil(err)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
@ -854,7 +854,7 @@ func Test_DB_ToJson(t *testing.T) {
|
||||
func Test_DB_ToXml(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
_, err := db.Update(table, "create_time='2010-10-10 00:00:01'", "id=?", 1)
|
||||
_, err := db.Update(ctx, table, "create_time='2010-10-10 00:00:01'", "id=?", 1)
|
||||
gtest.AssertNil(err)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
@ -920,7 +920,7 @@ func Test_DB_ToStringMap(t *testing.T) {
|
||||
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
_, err := db.Update(table, "create_time='2010-10-10 00:00:01'", "id=?", 1)
|
||||
_, err := db.Update(ctx, table, "create_time='2010-10-10 00:00:01'", "id=?", 1)
|
||||
gtest.AssertNil(err)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
id := "1"
|
||||
@ -956,7 +956,7 @@ func Test_DB_ToIntMap(t *testing.T) {
|
||||
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
_, err := db.Update(table, "create_time='2010-10-10 00:00:01'", "id=?", 1)
|
||||
_, err := db.Update(ctx, table, "create_time='2010-10-10 00:00:01'", "id=?", 1)
|
||||
gtest.AssertNil(err)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
@ -992,7 +992,7 @@ func Test_DB_ToIntMap(t *testing.T) {
|
||||
func Test_DB_ToUintMap(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
_, err := db.Update(table, "create_time='2010-10-10 00:00:01'", "id=?", 1)
|
||||
_, err := db.Update(ctx, table, "create_time='2010-10-10 00:00:01'", "id=?", 1)
|
||||
gtest.AssertNil(err)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
@ -1029,7 +1029,7 @@ func Test_DB_ToUintMap(t *testing.T) {
|
||||
func Test_DB_ToStringRecord(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
_, err := db.Update(table, "create_time='2010-10-10 00:00:01'", "id=?", 1)
|
||||
_, err := db.Update(ctx, table, "create_time='2010-10-10 00:00:01'", "id=?", 1)
|
||||
gtest.AssertNil(err)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
@ -1067,7 +1067,7 @@ func Test_DB_ToStringRecord(t *testing.T) {
|
||||
func Test_DB_ToIntRecord(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
_, err := db.Update(table, "create_time='2010-10-10 00:00:01'", "id=?", 1)
|
||||
_, err := db.Update(ctx, table, "create_time='2010-10-10 00:00:01'", "id=?", 1)
|
||||
gtest.AssertNil(err)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
@ -1104,7 +1104,7 @@ func Test_DB_ToIntRecord(t *testing.T) {
|
||||
func Test_DB_ToUintRecord(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
_, err := db.Update(table, "create_time='2010-10-10 00:00:01'", "id=?", 1)
|
||||
_, err := db.Update(ctx, table, "create_time='2010-10-10 00:00:01'", "id=?", 1)
|
||||
gtest.AssertNil(err)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
@ -1141,7 +1141,7 @@ func Test_DB_TableField(t *testing.T) {
|
||||
name := "field_test"
|
||||
dropTable(name)
|
||||
defer dropTable(name)
|
||||
_, err := db.Exec(fmt.Sprintf(`
|
||||
_, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE %s (
|
||||
field_tinyint tinyint(8) NULL ,
|
||||
field_int int(8) NULL ,
|
||||
@ -1198,7 +1198,7 @@ func Test_DB_Prefix(t *testing.T) {
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
id := 10000
|
||||
result, err := db.Insert(name, g.Map{
|
||||
result, err := db.Insert(ctx, name, g.Map{
|
||||
"id": id,
|
||||
"passport": fmt.Sprintf(`user_%d`, id),
|
||||
"password": fmt.Sprintf(`pass_%d`, id),
|
||||
@ -1214,7 +1214,7 @@ func Test_DB_Prefix(t *testing.T) {
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
id := 10000
|
||||
result, err := db.Replace(name, g.Map{
|
||||
result, err := db.Replace(ctx, name, g.Map{
|
||||
"id": id,
|
||||
"passport": fmt.Sprintf(`user_%d`, id),
|
||||
"password": fmt.Sprintf(`pass_%d`, id),
|
||||
@ -1230,7 +1230,7 @@ func Test_DB_Prefix(t *testing.T) {
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
id := 10000
|
||||
result, err := db.Save(name, g.Map{
|
||||
result, err := db.Save(ctx, name, g.Map{
|
||||
"id": id,
|
||||
"passport": fmt.Sprintf(`user_%d`, id),
|
||||
"password": fmt.Sprintf(`pass_%d`, id),
|
||||
@ -1246,7 +1246,7 @@ func Test_DB_Prefix(t *testing.T) {
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
id := 10000
|
||||
result, err := db.Update(name, g.Map{
|
||||
result, err := db.Update(ctx, name, g.Map{
|
||||
"id": id,
|
||||
"passport": fmt.Sprintf(`user_%d`, id),
|
||||
"password": fmt.Sprintf(`pass_%d`, id),
|
||||
@ -1262,7 +1262,7 @@ func Test_DB_Prefix(t *testing.T) {
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
id := 10000
|
||||
result, err := db.Delete(name, "id=?", id)
|
||||
result, err := db.Delete(ctx, name, "id=?", id)
|
||||
t.AssertNil(err)
|
||||
|
||||
n, e := result.RowsAffected()
|
||||
@ -1282,7 +1282,7 @@ func Test_DB_Prefix(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
result, err := db.Insert(name, array.Slice())
|
||||
result, err := db.Insert(ctx, name, array.Slice())
|
||||
t.AssertNil(err)
|
||||
|
||||
n, e := result.RowsAffected()
|
||||
@ -1402,7 +1402,7 @@ func Test_Empty_Slice_Argument(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, err := db.GetAll(fmt.Sprintf(`select * from %s where id in(?)`, table), g.Slice{})
|
||||
result, err := db.GetAll(ctx, fmt.Sprintf(`select * from %s where id in(?)`, table), g.Slice{})
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(result), 0)
|
||||
})
|
||||
@ -1411,7 +1411,7 @@ func Test_Empty_Slice_Argument(t *testing.T) {
|
||||
// update counter test
|
||||
func Test_DB_UpdateCounter(t *testing.T) {
|
||||
tableName := "gf_update_counter_test_" + gtime.TimestampNanoStr()
|
||||
_, err := db.Exec(fmt.Sprintf(`
|
||||
_, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
id int(10) unsigned NOT NULL,
|
||||
views int(8) unsigned DEFAULT '0' NOT NULL ,
|
||||
@ -1429,7 +1429,7 @@ func Test_DB_UpdateCounter(t *testing.T) {
|
||||
"views": 0,
|
||||
"updated_time": 0,
|
||||
}
|
||||
_, err = db.Insert(tableName, insertData)
|
||||
_, err = db.Insert(ctx, tableName, insertData)
|
||||
t.AssertNil(err)
|
||||
})
|
||||
|
||||
@ -1441,7 +1441,7 @@ func Test_DB_UpdateCounter(t *testing.T) {
|
||||
updateData := g.Map{
|
||||
"views": gdbCounter,
|
||||
}
|
||||
result, err := db.Update(tableName, updateData, "id", 1)
|
||||
result, err := db.Update(ctx, tableName, updateData, "id", 1)
|
||||
t.AssertNil(err)
|
||||
n, _ := result.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
@ -1460,7 +1460,7 @@ func Test_DB_UpdateCounter(t *testing.T) {
|
||||
"views": gdbCounter,
|
||||
"updated_time": gtime.Now().Unix(),
|
||||
}
|
||||
result, err := db.Update(tableName, updateData, "id", 1)
|
||||
result, err := db.Update(ctx, tableName, updateData, "id", 1)
|
||||
t.AssertNil(err)
|
||||
n, _ := result.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
@ -1475,7 +1475,7 @@ func Test_DB_Ctx(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||
defer cancel()
|
||||
_, err := db.Ctx(ctx).Query("SELECT SLEEP(10)")
|
||||
_, err := db.Query(ctx, "SELECT SLEEP(10)")
|
||||
t.Assert(gstr.Contains(err.Error(), "deadline"), true)
|
||||
})
|
||||
}
|
||||
@ -1485,7 +1485,7 @@ func Test_DB_Ctx_Logger(t *testing.T) {
|
||||
defer db.SetDebug(db.GetDebug())
|
||||
db.SetDebug(true)
|
||||
ctx := context.WithValue(context.Background(), "Trace-Id", "123456789")
|
||||
_, err := db.Ctx(ctx).Query("SELECT 1")
|
||||
_, err := db.Query(ctx, "SELECT 1")
|
||||
t.AssertNil(err)
|
||||
})
|
||||
}
|
||||
|
||||
@ -153,7 +153,7 @@ func Test_Model_Insert_KeyFieldNameMapping(t *testing.T) {
|
||||
_, err := db.Model(table).Data(data).Insert()
|
||||
t.AssertNil(err)
|
||||
|
||||
one, err := db.Model(table).FindOne(1)
|
||||
one, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], data.Passport)
|
||||
t.Assert(one["create_time"], data.CreateTime)
|
||||
@ -183,7 +183,7 @@ func Test_Model_Update_KeyFieldNameMapping(t *testing.T) {
|
||||
_, err := db.Model(table).Data(data).WherePri(1).Update()
|
||||
t.AssertNil(err)
|
||||
|
||||
one, err := db.Model(table).FindOne(1)
|
||||
one, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], data.Passport)
|
||||
t.Assert(one["create_time"], data.CreateTime)
|
||||
@ -573,7 +573,7 @@ func Test_Model_Fields(t *testing.T) {
|
||||
defer dropTable(tableName1)
|
||||
|
||||
tableName2 := "user_" + gtime.Now().TimestampNanoStr()
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE %s (
|
||||
id int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
name varchar(45) NULL,
|
||||
@ -586,7 +586,7 @@ func Test_Model_Fields(t *testing.T) {
|
||||
}
|
||||
defer dropTable(tableName2)
|
||||
|
||||
r, err := db.Insert(tableName2, g.Map{
|
||||
r, err := db.Insert(ctx, tableName2, g.Map{
|
||||
"id": 1,
|
||||
"name": "table2_1",
|
||||
"age": 18,
|
||||
@ -627,74 +627,6 @@ func Test_Model_Fields(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_FindAll(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, err := db.Model(table).FindAll(5)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(result), 1)
|
||||
t.Assert(result[0]["id"].Int(), 5)
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, err := db.Model(table).Order("id asc").FindAll("id", 8)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(result), 1)
|
||||
t.Assert(result[0]["id"].Int(), 8)
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, err := db.Model(table).Order("id asc").FindAll(g.Slice{3, 9})
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(result), 2)
|
||||
t.Assert(result[0]["id"].Int(), 3)
|
||||
t.Assert(result[1]["id"].Int(), 9)
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, err := db.Model(table).FindAll()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(result), TableSize)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, err := db.Model(table).Where("id<0").FindAll()
|
||||
t.Assert(result, nil)
|
||||
t.AssertNil(err)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_FindAll_GTime(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, err := db.Model(table).FindAll("create_time < ?", gtime.NewFromStr("2000-01-01 00:00:00"))
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(result), 0)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, err := db.Model(table).FindAll("create_time > ?", gtime.NewFromStr("2000-01-01 00:00:00"))
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(result), TableSize)
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
v := g.NewVar("2000-01-01 00:00:00")
|
||||
result, err := db.Model(table).FindAll("create_time < ?", v)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(result), 0)
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
v := g.NewVar("2000-01-01 00:00:00")
|
||||
result, err := db.Model(table).FindAll("create_time > ?", v)
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(result), TableSize)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_One(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
@ -711,41 +643,6 @@ func Test_Model_One(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_FindOne(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
record, err := db.Model(table).FindOne(1)
|
||||
t.AssertNil(err)
|
||||
t.Assert(record["nickname"].String(), "name_1")
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
record, err := db.Model(table).FindOne(3)
|
||||
t.AssertNil(err)
|
||||
t.Assert(record["nickname"].String(), "name_3")
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
record, err := db.Model(table).Where("id", 1).FindOne()
|
||||
t.AssertNil(err)
|
||||
t.Assert(record["nickname"].String(), "name_1")
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
record, err := db.Model(table).FindOne("id", 9)
|
||||
t.AssertNil(err)
|
||||
t.Assert(record["nickname"].String(), "name_9")
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
record, err := db.Model(table).Where("id", 0).FindOne()
|
||||
t.AssertNil(err)
|
||||
t.Assert(record, nil)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Value(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
@ -783,45 +680,6 @@ func Test_Model_Array(t *testing.T) {
|
||||
t.AssertNil(err)
|
||||
t.Assert(array, g.Slice{"name_1", "name_2", "name_3"})
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array, err := db.Model(table).FindArray("nickname", "id", g.Slice{1, 2, 3})
|
||||
t.AssertNil(err)
|
||||
t.Assert(array, g.Slice{"name_1", "name_2", "name_3"})
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
array, err := db.Model(table).FindArray("nickname", g.Slice{1, 2, 3})
|
||||
t.AssertNil(err)
|
||||
t.Assert(array, g.Slice{"name_1", "name_2", "name_3"})
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_FindValue(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
value, err := db.Model(table).FindValue("nickname", 1)
|
||||
t.AssertNil(err)
|
||||
t.Assert(value.String(), "name_1")
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
value, err := db.Model(table).Order("id desc").FindValue("nickname")
|
||||
t.AssertNil(err)
|
||||
t.Assert(value.String(), "name_10")
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
value, err := db.Model(table).Fields("nickname").Where("id", 1).FindValue()
|
||||
t.AssertNil(err)
|
||||
t.Assert(value.String(), "name_1")
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
value, err := db.Model(table).Fields("nickname").Where("id", 0).FindValue()
|
||||
t.AssertNil(err)
|
||||
t.Assert(value, nil)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Count(t *testing.T) {
|
||||
@ -860,26 +718,6 @@ func Test_Model_Count(t *testing.T) {
|
||||
//})
|
||||
}
|
||||
|
||||
func Test_Model_FindCount(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
count, err := db.Model(table).FindCount(g.Slice{1, 3})
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, 2)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
count, err := db.Model(table).FindCount(g.Slice{1, 300000})
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, 1)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
count, err := db.Model(table).FindCount()
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, TableSize)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Select(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
@ -2154,7 +1992,7 @@ func Test_Model_Option_List(t *testing.T) {
|
||||
|
||||
func Test_Model_OmitEmpty(t *testing.T) {
|
||||
table := fmt.Sprintf(`table_%s`, gtime.TimestampNanoStr())
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
id int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
name varchar(45) NOT NULL,
|
||||
@ -2190,7 +2028,7 @@ func Test_Model_OmitEmpty(t *testing.T) {
|
||||
|
||||
func Test_Model_OmitNil(t *testing.T) {
|
||||
table := fmt.Sprintf(`table_%s`, gtime.TimestampNanoStr())
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
id int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
name varchar(45) NOT NULL,
|
||||
@ -2310,7 +2148,7 @@ func Test_Model_FieldsEx(t *testing.T) {
|
||||
func Test_Model_FieldsEx_WithReservedWords(t *testing.T) {
|
||||
table := "fieldsex_test_table"
|
||||
sqlTpcPath := gdebug.TestDataPath("reservedwords_table_tpl.sql")
|
||||
if _, err := db.Exec(fmt.Sprintf(gfile.GetContents(sqlTpcPath), table)); err != nil {
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(gfile.GetContents(sqlTpcPath), table)); err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
defer dropTable(table)
|
||||
@ -2610,7 +2448,7 @@ func Test_Model_DryRun(t *testing.T) {
|
||||
defer db.SetDryRun(false)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
one, err := db.Model(table).FindOne(1)
|
||||
one, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["id"], 1)
|
||||
})
|
||||
@ -2641,7 +2479,7 @@ func Test_Model_Cache(t *testing.T) {
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
one, err := db.Model(table).Cache(time.Second, "test1").FindOne(1)
|
||||
one, err := db.Model(table).Cache(time.Second, "test1").WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], "user_1")
|
||||
|
||||
@ -2651,18 +2489,18 @@ func Test_Model_Cache(t *testing.T) {
|
||||
t.AssertNil(err)
|
||||
t.Assert(n, 1)
|
||||
|
||||
one, err = db.Model(table).Cache(time.Second, "test1").FindOne(1)
|
||||
one, err = db.Model(table).Cache(time.Second, "test1").WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], "user_1")
|
||||
|
||||
time.Sleep(time.Second * 2)
|
||||
|
||||
one, err = db.Model(table).Cache(time.Second, "test1").FindOne(1)
|
||||
one, err = db.Model(table).Cache(time.Second, "test1").WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], "user_100")
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
one, err := db.Model(table).Cache(time.Second, "test2").FindOne(2)
|
||||
one, err := db.Model(table).Cache(time.Second, "test2").WherePri(2).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], "user_2")
|
||||
|
||||
@ -2672,14 +2510,14 @@ func Test_Model_Cache(t *testing.T) {
|
||||
t.AssertNil(err)
|
||||
t.Assert(n, 1)
|
||||
|
||||
one, err = db.Model(table).Cache(time.Second, "test2").FindOne(2)
|
||||
one, err = db.Model(table).Cache(time.Second, "test2").WherePri(2).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], "user_200")
|
||||
})
|
||||
// transaction.
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// make cache for id 3
|
||||
one, err := db.Model(table).Cache(time.Second, "test3").FindOne(3)
|
||||
one, err := db.Model(table).Cache(time.Second, "test3").WherePri(3).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], "user_3")
|
||||
|
||||
@ -2690,20 +2528,20 @@ func Test_Model_Cache(t *testing.T) {
|
||||
t.Assert(n, 1)
|
||||
|
||||
err = db.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
|
||||
one, err := tx.Model(table).Cache(time.Second, "test3").FindOne(3)
|
||||
one, err := tx.Model(table).Cache(time.Second, "test3").WherePri(3).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], "user_300")
|
||||
return nil
|
||||
})
|
||||
t.AssertNil(err)
|
||||
|
||||
one, err = db.Model(table).Cache(time.Second, "test3").FindOne(3)
|
||||
one, err = db.Model(table).Cache(time.Second, "test3").WherePri(3).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], "user_3")
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
// make cache for id 4
|
||||
one, err := db.Model(table).Cache(time.Second, "test4").FindOne(4)
|
||||
one, err := db.Model(table).Cache(time.Second, "test4").WherePri(4).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], "user_4")
|
||||
|
||||
@ -2715,7 +2553,7 @@ func Test_Model_Cache(t *testing.T) {
|
||||
|
||||
err = db.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
|
||||
// Cache feature disabled.
|
||||
one, err := tx.Model(table).Cache(time.Second, "test4").FindOne(4)
|
||||
one, err := tx.Model(table).Cache(time.Second, "test4").WherePri(4).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], "user_400")
|
||||
// Update the cache.
|
||||
@ -2729,7 +2567,7 @@ func Test_Model_Cache(t *testing.T) {
|
||||
})
|
||||
t.AssertNil(err)
|
||||
// Read from db.
|
||||
one, err = db.Model(table).Cache(time.Second, "test4").FindOne(4)
|
||||
one, err = db.Model(table).Cache(time.Second, "test4").WherePri(4).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], "user_4000")
|
||||
})
|
||||
@ -2948,7 +2786,7 @@ func Test_Model_NullField(t *testing.T) {
|
||||
t.AssertNil(err)
|
||||
n, _ := result.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
one, err := db.Model(table).FindOne(1)
|
||||
one, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
|
||||
var user *User
|
||||
@ -3031,12 +2869,7 @@ func Test_Model_Issue1002(t *testing.T) {
|
||||
t.Assert(v.Int(), 1)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
v, err := db.Model(table).Fields("id").Where("create_time>'2020-10-27 19:03:32' and create_time<'2020-10-27 19:03:34'").FindValue()
|
||||
t.AssertNil(err)
|
||||
t.Assert(v.Int(), 1)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
v, err := db.Model(table).Where("create_time>'2020-10-27 19:03:32' and create_time<'2020-10-27 19:03:34'").FindValue("id")
|
||||
v, err := db.Model(table).Fields("id").Where("create_time>'2020-10-27 19:03:32' and create_time<'2020-10-27 19:03:34'").Value()
|
||||
t.AssertNil(err)
|
||||
t.Assert(v.Int(), 1)
|
||||
})
|
||||
@ -3046,32 +2879,12 @@ func Test_Model_Issue1002(t *testing.T) {
|
||||
t.AssertNil(err)
|
||||
t.Assert(v.Int(), 1)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
v, err := db.Model(table).Fields("id").Where("create_time>? and create_time<?", "2020-10-27 19:03:32", "2020-10-27 19:03:34").FindValue()
|
||||
t.AssertNil(err)
|
||||
t.Assert(v.Int(), 1)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
v, err := db.Model(table).Where("create_time>? and create_time<?", "2020-10-27 19:03:32", "2020-10-27 19:03:34").FindValue("id")
|
||||
t.AssertNil(err)
|
||||
t.Assert(v.Int(), 1)
|
||||
})
|
||||
// where + gtime.Time arguments.
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
v, err := db.Model(table).Fields("id").Where("create_time>? and create_time<?", gtime.New("2020-10-27 19:03:32"), gtime.New("2020-10-27 19:03:34")).Value()
|
||||
t.AssertNil(err)
|
||||
t.Assert(v.Int(), 1)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
v, err := db.Model(table).Fields("id").Where("create_time>? and create_time<?", gtime.New("2020-10-27 19:03:32"), gtime.New("2020-10-27 19:03:34")).FindValue()
|
||||
t.AssertNil(err)
|
||||
t.Assert(v.Int(), 1)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
v, err := db.Model(table).Where("create_time>? and create_time<?", gtime.New("2020-10-27 19:03:32"), gtime.New("2020-10-27 19:03:34")).FindValue("id")
|
||||
t.AssertNil(err)
|
||||
t.Assert(v.Int(), 1)
|
||||
})
|
||||
// where + time.Time arguments, UTC.
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
t1, _ := time.Parse("2006-01-02 15:04:05", "2020-10-27 19:03:32")
|
||||
@ -3081,16 +2894,6 @@ func Test_Model_Issue1002(t *testing.T) {
|
||||
t.AssertNil(err)
|
||||
t.Assert(v.Int(), 1)
|
||||
}
|
||||
{
|
||||
v, err := db.Model(table).Fields("id").Where("create_time>? and create_time<?", t1, t2).FindValue()
|
||||
t.AssertNil(err)
|
||||
t.Assert(v.Int(), 1)
|
||||
}
|
||||
{
|
||||
v, err := db.Model(table).Where("create_time>? and create_time<?", t1, t2).FindValue("id")
|
||||
t.AssertNil(err)
|
||||
t.Assert(v.Int(), 1)
|
||||
}
|
||||
})
|
||||
// where + time.Time arguments, +8.
|
||||
//gtest.C(t, func(t *gtest.T) {
|
||||
@ -3119,7 +2922,7 @@ func Test_Model_Issue1002(t *testing.T) {
|
||||
|
||||
func createTableForTimeZoneTest() string {
|
||||
tableName := "user_" + gtime.Now().TimestampNanoStr()
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE %s (
|
||||
id int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
passport varchar(45) NULL,
|
||||
@ -3636,7 +3439,7 @@ func Test_Model_OnDuplicate(t *testing.T) {
|
||||
}
|
||||
_, err := db.Model(table).OnDuplicate("passport,password").Data(data).Save()
|
||||
t.AssertNil(err)
|
||||
one, err := db.Model(table).FindOne(1)
|
||||
one, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], data["passport"])
|
||||
t.Assert(one["password"], data["password"])
|
||||
@ -3654,7 +3457,7 @@ func Test_Model_OnDuplicate(t *testing.T) {
|
||||
}
|
||||
_, err := db.Model(table).OnDuplicate(g.Slice{"passport", "password"}).Data(data).Save()
|
||||
t.AssertNil(err)
|
||||
one, err := db.Model(table).FindOne(1)
|
||||
one, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], data["passport"])
|
||||
t.Assert(one["password"], data["password"])
|
||||
@ -3675,7 +3478,7 @@ func Test_Model_OnDuplicate(t *testing.T) {
|
||||
"password": "nickname",
|
||||
}).Data(data).Save()
|
||||
t.AssertNil(err)
|
||||
one, err := db.Model(table).FindOne(1)
|
||||
one, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], data["nickname"])
|
||||
t.Assert(one["password"], data["nickname"])
|
||||
@ -3696,7 +3499,7 @@ func Test_Model_OnDuplicate(t *testing.T) {
|
||||
"password": gdb.Raw("CONCAT(VALUES(`password`), '2')"),
|
||||
}).Data(data).Save()
|
||||
t.AssertNil(err)
|
||||
one, err := db.Model(table).FindOne(1)
|
||||
one, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], data["passport"]+"1")
|
||||
t.Assert(one["password"], data["password"]+"2")
|
||||
@ -3719,7 +3522,7 @@ func Test_Model_OnDuplicateEx(t *testing.T) {
|
||||
}
|
||||
_, err := db.Model(table).OnDuplicateEx("nickname,create_time").Data(data).Save()
|
||||
t.AssertNil(err)
|
||||
one, err := db.Model(table).FindOne(1)
|
||||
one, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], data["passport"])
|
||||
t.Assert(one["password"], data["password"])
|
||||
@ -3737,7 +3540,7 @@ func Test_Model_OnDuplicateEx(t *testing.T) {
|
||||
}
|
||||
_, err := db.Model(table).OnDuplicateEx(g.Slice{"nickname", "create_time"}).Data(data).Save()
|
||||
t.AssertNil(err)
|
||||
one, err := db.Model(table).FindOne(1)
|
||||
one, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], data["passport"])
|
||||
t.Assert(one["password"], data["password"])
|
||||
@ -3758,7 +3561,7 @@ func Test_Model_OnDuplicateEx(t *testing.T) {
|
||||
"create_time": "nickname",
|
||||
}).Data(data).Save()
|
||||
t.AssertNil(err)
|
||||
one, err := db.Model(table).FindOne(1)
|
||||
one, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["passport"], data["passport"])
|
||||
t.Assert(one["password"], data["password"])
|
||||
|
||||
@ -114,7 +114,7 @@ func Test_Struct_Pointer_Attribute(t *testing.T) {
|
||||
}
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
one, err := db.Model(table).FindOne(1)
|
||||
one, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
user := new(User)
|
||||
err = one.Struct(user)
|
||||
@ -554,7 +554,7 @@ func Test_Scan_JsonAttributes(t *testing.T) {
|
||||
)
|
||||
array := gstr.SplitAndTrim(gtest.TestDataContent(`issue1380.sql`), ";")
|
||||
for _, v := range array {
|
||||
if _, err := db.Exec(v); err != nil {
|
||||
if _, err := db.Exec(ctx, v); err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -20,7 +20,7 @@ import (
|
||||
// CreateAt/UpdateAt/DeleteAt
|
||||
func Test_SoftCreateUpdateDeleteTime(t *testing.T) {
|
||||
table := "time_test_table_" + gtime.TimestampNanoStr()
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE %s (
|
||||
id int(11) NOT NULL,
|
||||
name varchar(45) DEFAULT NULL,
|
||||
@ -45,7 +45,7 @@ CREATE TABLE %s (
|
||||
n, _ := r.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
|
||||
oneInsert, err := db.Model(table).FindOne(1)
|
||||
oneInsert, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(oneInsert["id"].Int(), 1)
|
||||
t.Assert(oneInsert["name"].String(), "name_1")
|
||||
@ -66,7 +66,7 @@ CREATE TABLE %s (
|
||||
n, _ = r.RowsAffected()
|
||||
t.Assert(n, 2)
|
||||
|
||||
oneSave, err := db.Model(table).FindOne(1)
|
||||
oneSave, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(oneSave["id"].Int(), 1)
|
||||
t.Assert(oneSave["name"].String(), "name_10")
|
||||
@ -87,7 +87,7 @@ CREATE TABLE %s (
|
||||
n, _ = r.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
|
||||
oneUpdate, err := db.Model(table).FindOne(1)
|
||||
oneUpdate, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(oneUpdate["id"].Int(), 1)
|
||||
t.Assert(oneUpdate["name"].String(), "name_1000")
|
||||
@ -105,7 +105,7 @@ CREATE TABLE %s (
|
||||
n, _ = r.RowsAffected()
|
||||
t.Assert(n, 2)
|
||||
|
||||
oneReplace, err := db.Model(table).FindOne(1)
|
||||
oneReplace, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(oneReplace["id"].Int(), 1)
|
||||
t.Assert(oneReplace["name"].String(), "name_100")
|
||||
@ -122,18 +122,18 @@ CREATE TABLE %s (
|
||||
n, _ = r.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
// Delete Select
|
||||
one4, err := db.Model(table).FindOne(1)
|
||||
one4, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(one4), 0)
|
||||
one5, err := db.Model(table).Unscoped().FindOne(1)
|
||||
one5, err := db.Model(table).Unscoped().WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one5["id"].Int(), 1)
|
||||
t.AssertGE(one5["delete_at"].GTime().Timestamp(), gtime.Timestamp()-2)
|
||||
// Delete Count
|
||||
i, err := db.Model(table).FindCount()
|
||||
i, err := db.Model(table).Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(i, 0)
|
||||
i, err = db.Model(table).Unscoped().FindCount()
|
||||
i, err = db.Model(table).Unscoped().Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(i, 1)
|
||||
|
||||
@ -142,10 +142,10 @@ CREATE TABLE %s (
|
||||
t.AssertNil(err)
|
||||
n, _ = r.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
one6, err := db.Model(table).Unscoped().FindOne(1)
|
||||
one6, err := db.Model(table).Unscoped().WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(one6), 0)
|
||||
i, err = db.Model(table).Unscoped().FindCount()
|
||||
i, err = db.Model(table).Unscoped().Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(i, 0)
|
||||
})
|
||||
@ -154,7 +154,7 @@ CREATE TABLE %s (
|
||||
// CreatedAt/UpdatedAt/DeletedAt
|
||||
func Test_SoftCreatedUpdatedDeletedTime_Map(t *testing.T) {
|
||||
table := "time_test_table_" + gtime.TimestampNanoStr()
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE %s (
|
||||
id int(11) NOT NULL,
|
||||
name varchar(45) DEFAULT NULL,
|
||||
@ -179,7 +179,7 @@ CREATE TABLE %s (
|
||||
n, _ := r.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
|
||||
oneInsert, err := db.Model(table).FindOne(1)
|
||||
oneInsert, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(oneInsert["id"].Int(), 1)
|
||||
t.Assert(oneInsert["name"].String(), "name_1")
|
||||
@ -200,7 +200,7 @@ CREATE TABLE %s (
|
||||
n, _ = r.RowsAffected()
|
||||
t.Assert(n, 2)
|
||||
|
||||
oneSave, err := db.Model(table).FindOne(1)
|
||||
oneSave, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(oneSave["id"].Int(), 1)
|
||||
t.Assert(oneSave["name"].String(), "name_10")
|
||||
@ -221,7 +221,7 @@ CREATE TABLE %s (
|
||||
n, _ = r.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
|
||||
oneUpdate, err := db.Model(table).FindOne(1)
|
||||
oneUpdate, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(oneUpdate["id"].Int(), 1)
|
||||
t.Assert(oneUpdate["name"].String(), "name_1000")
|
||||
@ -239,7 +239,7 @@ CREATE TABLE %s (
|
||||
n, _ = r.RowsAffected()
|
||||
t.Assert(n, 2)
|
||||
|
||||
oneReplace, err := db.Model(table).FindOne(1)
|
||||
oneReplace, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(oneReplace["id"].Int(), 1)
|
||||
t.Assert(oneReplace["name"].String(), "name_100")
|
||||
@ -256,18 +256,18 @@ CREATE TABLE %s (
|
||||
n, _ = r.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
// Delete Select
|
||||
one4, err := db.Model(table).FindOne(1)
|
||||
one4, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(one4), 0)
|
||||
one5, err := db.Model(table).Unscoped().FindOne(1)
|
||||
one5, err := db.Model(table).Unscoped().WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one5["id"].Int(), 1)
|
||||
t.AssertGE(one5["deleted_at"].GTime().Timestamp(), gtime.Timestamp()-2)
|
||||
// Delete Count
|
||||
i, err := db.Model(table).FindCount()
|
||||
i, err := db.Model(table).Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(i, 0)
|
||||
i, err = db.Model(table).Unscoped().FindCount()
|
||||
i, err = db.Model(table).Unscoped().Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(i, 1)
|
||||
|
||||
@ -276,10 +276,10 @@ CREATE TABLE %s (
|
||||
t.AssertNil(err)
|
||||
n, _ = r.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
one6, err := db.Model(table).Unscoped().FindOne(1)
|
||||
one6, err := db.Model(table).Unscoped().WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(one6), 0)
|
||||
i, err = db.Model(table).Unscoped().FindCount()
|
||||
i, err = db.Model(table).Unscoped().Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(i, 0)
|
||||
})
|
||||
@ -288,7 +288,7 @@ CREATE TABLE %s (
|
||||
// CreatedAt/UpdatedAt/DeletedAt
|
||||
func Test_SoftCreatedUpdatedDeletedTime_Struct(t *testing.T) {
|
||||
table := "time_test_table_" + gtime.TimestampNanoStr()
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE %s (
|
||||
id int(11) NOT NULL,
|
||||
name varchar(45) DEFAULT NULL,
|
||||
@ -320,7 +320,7 @@ CREATE TABLE %s (
|
||||
n, _ := r.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
|
||||
oneInsert, err := db.Model(table).FindOne(1)
|
||||
oneInsert, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(oneInsert["id"].Int(), 1)
|
||||
t.Assert(oneInsert["name"].String(), "name_1")
|
||||
@ -341,7 +341,7 @@ CREATE TABLE %s (
|
||||
n, _ = r.RowsAffected()
|
||||
t.Assert(n, 2)
|
||||
|
||||
oneSave, err := db.Model(table).FindOne(1)
|
||||
oneSave, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(oneSave["id"].Int(), 1)
|
||||
t.Assert(oneSave["name"].String(), "name_10")
|
||||
@ -362,7 +362,7 @@ CREATE TABLE %s (
|
||||
n, _ = r.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
|
||||
oneUpdate, err := db.Model(table).FindOne(1)
|
||||
oneUpdate, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(oneUpdate["id"].Int(), 1)
|
||||
t.Assert(oneUpdate["name"].String(), "name_1000")
|
||||
@ -380,7 +380,7 @@ CREATE TABLE %s (
|
||||
n, _ = r.RowsAffected()
|
||||
t.Assert(n, 2)
|
||||
|
||||
oneReplace, err := db.Model(table).FindOne(1)
|
||||
oneReplace, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(oneReplace["id"].Int(), 1)
|
||||
t.Assert(oneReplace["name"].String(), "name_100")
|
||||
@ -397,18 +397,18 @@ CREATE TABLE %s (
|
||||
n, _ = r.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
// Delete Select
|
||||
one4, err := db.Model(table).FindOne(1)
|
||||
one4, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(one4), 0)
|
||||
one5, err := db.Model(table).Unscoped().FindOne(1)
|
||||
one5, err := db.Model(table).Unscoped().WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one5["id"].Int(), 1)
|
||||
t.AssertGE(one5["deleted_at"].GTime().Timestamp(), gtime.Timestamp()-2)
|
||||
// Delete Count
|
||||
i, err := db.Model(table).FindCount()
|
||||
i, err := db.Model(table).Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(i, 0)
|
||||
i, err = db.Model(table).Unscoped().FindCount()
|
||||
i, err = db.Model(table).Unscoped().Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(i, 1)
|
||||
|
||||
@ -417,10 +417,10 @@ CREATE TABLE %s (
|
||||
t.AssertNil(err)
|
||||
n, _ = r.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
one6, err := db.Model(table).Unscoped().FindOne(1)
|
||||
one6, err := db.Model(table).Unscoped().WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(one6), 0)
|
||||
i, err = db.Model(table).Unscoped().FindCount()
|
||||
i, err = db.Model(table).Unscoped().Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(i, 0)
|
||||
})
|
||||
@ -428,7 +428,7 @@ CREATE TABLE %s (
|
||||
|
||||
func Test_SoftUpdateTime(t *testing.T) {
|
||||
table := "time_test_table_" + gtime.TimestampNanoStr()
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE %s (
|
||||
id int(11) NOT NULL,
|
||||
num int(11) DEFAULT NULL,
|
||||
@ -453,7 +453,7 @@ CREATE TABLE %s (
|
||||
n, _ := r.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
|
||||
oneInsert, err := db.Model(table).FindOne(1)
|
||||
oneInsert, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(oneInsert["id"].Int(), 1)
|
||||
t.Assert(oneInsert["num"].Int(), 10)
|
||||
@ -468,7 +468,7 @@ CREATE TABLE %s (
|
||||
|
||||
func Test_SoftDelete(t *testing.T) {
|
||||
table := "time_test_table_" + gtime.TimestampNanoStr()
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE %s (
|
||||
id int(11) NOT NULL,
|
||||
name varchar(45) DEFAULT NULL,
|
||||
@ -495,14 +495,14 @@ CREATE TABLE %s (
|
||||
}
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
one, err := db.Model(table).FindOne(1)
|
||||
one, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.AssertNE(one["create_at"].String(), "")
|
||||
t.AssertNE(one["update_at"].String(), "")
|
||||
t.Assert(one["delete_at"].String(), "")
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
one, err := db.Model(table).FindOne(10)
|
||||
one, err := db.Model(table).WherePri(10).One()
|
||||
t.AssertNil(err)
|
||||
t.AssertNE(one["create_at"].String(), "")
|
||||
t.AssertNE(one["update_at"].String(), "")
|
||||
@ -515,11 +515,11 @@ CREATE TABLE %s (
|
||||
n, _ := r.RowsAffected()
|
||||
t.Assert(n, 3)
|
||||
|
||||
count, err := db.Model(table).FindCount(ids)
|
||||
count, err := db.Model(table).Where("id", ids).Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(count, 0)
|
||||
|
||||
all, err := db.Model(table).Unscoped().FindAll(ids)
|
||||
all, err := db.Model(table).Unscoped().Where("id", ids).All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(all), 3)
|
||||
t.AssertNE(all[0]["create_at"].String(), "")
|
||||
@ -536,7 +536,7 @@ CREATE TABLE %s (
|
||||
|
||||
func Test_SoftDelete_Join(t *testing.T) {
|
||||
table1 := "time_test_table1"
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE %s (
|
||||
id int(11) NOT NULL,
|
||||
name varchar(45) DEFAULT NULL,
|
||||
@ -551,7 +551,7 @@ CREATE TABLE %s (
|
||||
defer dropTable(table1)
|
||||
|
||||
table2 := "time_test_table2"
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE %s (
|
||||
id int(11) NOT NULL,
|
||||
name varchar(45) DEFAULT NULL,
|
||||
@ -585,7 +585,7 @@ CREATE TABLE %s (
|
||||
n, _ = r.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
|
||||
one, err := db.Model(table1, "t1").LeftJoin(table2, "t2", "t2.id=t1.id").Fields("t1.name").FindOne()
|
||||
one, err := db.Model(table1, "t1").LeftJoin(table2, "t2", "t2.id=t1.id").Fields("t1.name").One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one["name"], "name_1")
|
||||
|
||||
@ -595,11 +595,11 @@ CREATE TABLE %s (
|
||||
n, _ = r.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
|
||||
one, err = db.Model(table1, "t1").LeftJoin(table2, "t2", "t2.id=t1.id").Fields("t1.name").FindOne()
|
||||
one, err = db.Model(table1, "t1").LeftJoin(table2, "t2", "t2.id=t1.id").Fields("t1.name").One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one.IsEmpty(), true)
|
||||
|
||||
one, err = db.Model(table2, "t2").LeftJoin(table1, "t1", "t2.id=t1.id").Fields("t2.name").FindOne()
|
||||
one, err = db.Model(table2, "t2").LeftJoin(table1, "t1", "t2.id=t1.id").Fields("t2.name").One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one.IsEmpty(), true)
|
||||
})
|
||||
@ -607,7 +607,7 @@ CREATE TABLE %s (
|
||||
|
||||
func Test_SoftDelete_WhereAndOr(t *testing.T) {
|
||||
table := "time_test_table_" + gtime.TimestampNanoStr()
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE %s (
|
||||
id int(11) NOT NULL,
|
||||
name varchar(45) DEFAULT NULL,
|
||||
@ -649,7 +649,7 @@ CREATE TABLE %s (
|
||||
|
||||
func Test_CreateUpdateTime_Struct(t *testing.T) {
|
||||
table := "time_test_table_" + gtime.TimestampNanoStr()
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE %s (
|
||||
id int(11) NOT NULL,
|
||||
name varchar(45) DEFAULT NULL,
|
||||
@ -687,7 +687,7 @@ CREATE TABLE %s (
|
||||
n, _ := r.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
|
||||
oneInsert, err := db.Model(table).FindOne(1)
|
||||
oneInsert, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(oneInsert["id"].Int(), 1)
|
||||
t.Assert(oneInsert["name"].String(), "name_1")
|
||||
@ -710,7 +710,7 @@ CREATE TABLE %s (
|
||||
n, _ = r.RowsAffected()
|
||||
t.Assert(n, 2)
|
||||
|
||||
oneSave, err := db.Model(table).FindOne(1)
|
||||
oneSave, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(oneSave["id"].Int(), 1)
|
||||
t.Assert(oneSave["name"].String(), "name_10")
|
||||
@ -734,7 +734,7 @@ CREATE TABLE %s (
|
||||
n, _ = r.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
|
||||
oneUpdate, err := db.Model(table).FindOne(1)
|
||||
oneUpdate, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(oneUpdate["id"].Int(), 1)
|
||||
t.Assert(oneUpdate["name"].String(), "name_1000")
|
||||
@ -755,7 +755,7 @@ CREATE TABLE %s (
|
||||
n, _ = r.RowsAffected()
|
||||
t.Assert(n, 2)
|
||||
|
||||
oneReplace, err := db.Model(table).FindOne(1)
|
||||
oneReplace, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(oneReplace["id"].Int(), 1)
|
||||
t.Assert(oneReplace["name"].String(), "name_100")
|
||||
@ -771,18 +771,18 @@ CREATE TABLE %s (
|
||||
n, _ = r.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
// Delete Select
|
||||
one4, err := db.Model(table).FindOne(1)
|
||||
one4, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(one4), 0)
|
||||
one5, err := db.Model(table).Unscoped().FindOne(1)
|
||||
one5, err := db.Model(table).Unscoped().WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one5["id"].Int(), 1)
|
||||
t.AssertGE(one5["delete_at"].GTime().Timestamp(), gtime.Timestamp()-2)
|
||||
// Delete Count
|
||||
i, err := db.Model(table).FindCount()
|
||||
i, err := db.Model(table).Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(i, 0)
|
||||
i, err = db.Model(table).Unscoped().FindCount()
|
||||
i, err = db.Model(table).Unscoped().Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(i, 1)
|
||||
|
||||
@ -791,10 +791,10 @@ CREATE TABLE %s (
|
||||
t.AssertNil(err)
|
||||
n, _ = r.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
one6, err := db.Model(table).Unscoped().FindOne(1)
|
||||
one6, err := db.Model(table).Unscoped().WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(one6), 0)
|
||||
i, err = db.Model(table).Unscoped().FindCount()
|
||||
i, err = db.Model(table).Unscoped().Count()
|
||||
t.AssertNil(err)
|
||||
t.Assert(i, 0)
|
||||
})
|
||||
|
||||
@ -21,35 +21,29 @@ import (
|
||||
)
|
||||
|
||||
func Test_TX_Query(t *testing.T) {
|
||||
tx, err := db.Begin()
|
||||
tx, err := db.Begin(ctx)
|
||||
if err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
if rows, err := tx.Query("SELECT ?", 1); err != nil {
|
||||
if _, err = tx.Query("SELECT ?", 1); err != nil {
|
||||
gtest.Error(err)
|
||||
} else {
|
||||
rows.Close()
|
||||
}
|
||||
if rows, err := tx.Query("SELECT ?+?", 1, 2); err != nil {
|
||||
if _, err = tx.Query("SELECT ?+?", 1, 2); err != nil {
|
||||
gtest.Error(err)
|
||||
} else {
|
||||
rows.Close()
|
||||
}
|
||||
if rows, err := tx.Query("SELECT ?+?", g.Slice{1, 2}); err != nil {
|
||||
if _, err = tx.Query("SELECT ?+?", g.Slice{1, 2}); err != nil {
|
||||
gtest.Error(err)
|
||||
} else {
|
||||
rows.Close()
|
||||
}
|
||||
if _, err := tx.Query("ERROR"); err == nil {
|
||||
if _, err = tx.Query("ERROR"); err == nil {
|
||||
gtest.Error("FAIL")
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
if err = tx.Commit(); err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_TX_Exec(t *testing.T) {
|
||||
tx, err := db.Begin()
|
||||
tx, err := db.Begin(ctx)
|
||||
if err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
@ -71,7 +65,7 @@ func Test_TX_Exec(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_TX_Commit(t *testing.T) {
|
||||
tx, err := db.Begin()
|
||||
tx, err := db.Begin(ctx)
|
||||
if err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
@ -81,7 +75,7 @@ func Test_TX_Commit(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_TX_Rollback(t *testing.T) {
|
||||
tx, err := db.Begin()
|
||||
tx, err := db.Begin(ctx)
|
||||
if err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
@ -92,7 +86,7 @@ func Test_TX_Rollback(t *testing.T) {
|
||||
|
||||
func Test_TX_Prepare(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
tx, err := db.Begin()
|
||||
tx, err := db.Begin(ctx)
|
||||
t.AssertNil(err)
|
||||
|
||||
st, err := tx.Prepare("SELECT 100")
|
||||
@ -118,7 +112,7 @@ func Test_TX_Insert(t *testing.T) {
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
tx, err := db.Begin()
|
||||
tx, err := db.Begin(ctx)
|
||||
if err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
@ -160,7 +154,7 @@ func Test_TX_BatchInsert(t *testing.T) {
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
tx, err := db.Begin()
|
||||
tx, err := db.Begin(ctx)
|
||||
if err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
@ -198,7 +192,7 @@ func Test_TX_BatchReplace(t *testing.T) {
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
tx, err := db.Begin()
|
||||
tx, err := db.Begin(ctx)
|
||||
if err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
@ -241,7 +235,7 @@ func Test_TX_BatchSave(t *testing.T) {
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
tx, err := db.Begin()
|
||||
tx, err := db.Begin(ctx)
|
||||
if err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
@ -279,7 +273,7 @@ func Test_TX_Replace(t *testing.T) {
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
tx, err := db.Begin()
|
||||
tx, err := db.Begin(ctx)
|
||||
if err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
@ -308,7 +302,7 @@ func Test_TX_Save(t *testing.T) {
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
tx, err := db.Begin()
|
||||
tx, err := db.Begin(ctx)
|
||||
if err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
@ -337,7 +331,7 @@ func Test_TX_Update(t *testing.T) {
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
tx, err := db.Begin()
|
||||
tx, err := db.Begin(ctx)
|
||||
if err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
@ -366,7 +360,7 @@ func Test_TX_GetAll(t *testing.T) {
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
tx, err := db.Begin()
|
||||
tx, err := db.Begin(ctx)
|
||||
if err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
@ -386,7 +380,7 @@ func Test_TX_GetOne(t *testing.T) {
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
tx, err := db.Begin()
|
||||
tx, err := db.Begin(ctx)
|
||||
if err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
@ -409,7 +403,7 @@ func Test_TX_GetValue(t *testing.T) {
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
tx, err := db.Begin()
|
||||
tx, err := db.Begin(ctx)
|
||||
if err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
@ -430,7 +424,7 @@ func Test_TX_GetCount(t *testing.T) {
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
tx, err := db.Begin()
|
||||
tx, err := db.Begin(ctx)
|
||||
if err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
@ -450,7 +444,7 @@ func Test_TX_GetStruct(t *testing.T) {
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
tx, err := db.Begin()
|
||||
tx, err := db.Begin(ctx)
|
||||
if err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
@ -472,7 +466,7 @@ func Test_TX_GetStruct(t *testing.T) {
|
||||
}
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
tx, err := db.Begin()
|
||||
tx, err := db.Begin(ctx)
|
||||
if err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
@ -500,7 +494,7 @@ func Test_TX_GetStructs(t *testing.T) {
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
tx, err := db.Begin()
|
||||
tx, err := db.Begin(ctx)
|
||||
if err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
@ -529,7 +523,7 @@ func Test_TX_GetStructs(t *testing.T) {
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
tx, err := db.Begin()
|
||||
tx, err := db.Begin(ctx)
|
||||
if err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
@ -563,7 +557,7 @@ func Test_TX_GetScan(t *testing.T) {
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
tx, err := db.Begin()
|
||||
tx, err := db.Begin(ctx)
|
||||
if err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
@ -585,7 +579,7 @@ func Test_TX_GetScan(t *testing.T) {
|
||||
}
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
tx, err := db.Begin()
|
||||
tx, err := db.Begin(ctx)
|
||||
if err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
@ -608,7 +602,7 @@ func Test_TX_GetScan(t *testing.T) {
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
tx, err := db.Begin()
|
||||
tx, err := db.Begin(ctx)
|
||||
if err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
@ -637,7 +631,7 @@ func Test_TX_GetScan(t *testing.T) {
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
tx, err := db.Begin()
|
||||
tx, err := db.Begin(ctx)
|
||||
if err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
@ -670,7 +664,7 @@ func Test_TX_Delete(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
tx, err := db.Begin()
|
||||
tx, err := db.Begin(ctx)
|
||||
if err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
@ -692,7 +686,7 @@ func Test_TX_Delete(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
tx, err := db.Begin()
|
||||
tx, err := db.Begin(ctx)
|
||||
if err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
@ -804,7 +798,7 @@ func Test_Transaction_Nested_Begin_Rollback_Commit(t *testing.T) {
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
tx, err := db.Begin()
|
||||
tx, err := db.Begin(ctx)
|
||||
t.AssertNil(err)
|
||||
// tx begin.
|
||||
err = tx.Begin()
|
||||
@ -1086,7 +1080,7 @@ func Test_Transaction_Nested_SavePoint_RollbackTo(t *testing.T) {
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
tx, err := db.Begin()
|
||||
tx, err := db.Begin(ctx)
|
||||
t.AssertNil(err)
|
||||
// tx save point.
|
||||
_, err = tx.Model(table).Data(g.Map{
|
||||
@ -1134,7 +1128,7 @@ func Test_Transaction_Method(t *testing.T) {
|
||||
}).Insert()
|
||||
t.AssertNil(err)
|
||||
|
||||
_, err = db.Ctx(ctx).Exec(fmt.Sprintf(
|
||||
_, err = db.Ctx(ctx).Exec(ctx, fmt.Sprintf(
|
||||
"insert into %s(`passport`,`password`,`nickname`,`create_time`,`id`) "+
|
||||
"VALUES('t2','25d55ad283aa400af464c76d713c07ad','T2','2021-08-25 21:53:00',2) ",
|
||||
table))
|
||||
|
||||
@ -19,7 +19,7 @@ import (
|
||||
// All types testing.
|
||||
func Test_Types(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS types (
|
||||
id int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
%s blob NOT NULL,
|
||||
|
||||
Reference in New Issue
Block a user