mirror of https://github.com/go-gorm/gorm.git
Make Query works
This commit is contained in:
parent
0da8191f60
commit
1403ee70c3
|
@ -1,6 +1,7 @@
|
||||||
package callbacks
|
package callbacks
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"database/sql"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
|
@ -15,9 +16,31 @@ func Query(db *gorm.DB) {
|
||||||
}
|
}
|
||||||
|
|
||||||
rows, err := db.DB.QueryContext(db.Context, db.Statement.SQL.String(), db.Statement.Vars...)
|
rows, err := db.DB.QueryContext(db.Context, db.Statement.SQL.String(), db.Statement.Vars...)
|
||||||
db.AddError(err)
|
if err != nil {
|
||||||
_ = rows
|
db.AddError(err)
|
||||||
// scan rows
|
return
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
columns, _ := rows.Columns()
|
||||||
|
values := make([]interface{}, len(columns))
|
||||||
|
|
||||||
|
for idx, column := range columns {
|
||||||
|
if field, ok := db.Statement.Schema.FieldsByDBName[column]; ok {
|
||||||
|
values[idx] = field.ReflectValueOf(db.Statement.ReflectValue).Addr().Interface()
|
||||||
|
} else {
|
||||||
|
values[idx] = sql.RawBytes{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for rows.Next() {
|
||||||
|
db.RowsAffected++
|
||||||
|
rows.Scan(values...)
|
||||||
|
}
|
||||||
|
|
||||||
|
if db.RowsAffected == 0 && db.Statement.RaiseErrorOnNotFound {
|
||||||
|
db.AddError(gorm.ErrRecordNotFound)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Preload(db *gorm.DB) {
|
func Preload(db *gorm.DB) {
|
||||||
|
|
|
@ -59,8 +59,10 @@ func (dialector Dialector) DataTypeOf(field *schema.Field) string {
|
||||||
}
|
}
|
||||||
case schema.Float:
|
case schema.Float:
|
||||||
return "real"
|
return "real"
|
||||||
case schema.String, schema.Time:
|
case schema.String:
|
||||||
return "text"
|
return "text"
|
||||||
|
case schema.Time:
|
||||||
|
return "datetime"
|
||||||
case schema.Bytes:
|
case schema.Bytes:
|
||||||
return "blob"
|
return "blob"
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,6 +28,7 @@ func (db *DB) First(out interface{}, where ...interface{}) (tx *DB) {
|
||||||
Column: clause.Column{Table: clause.CurrentTable, Name: clause.PrimaryKey},
|
Column: clause.Column{Table: clause.CurrentTable, Name: clause.PrimaryKey},
|
||||||
Desc: true,
|
Desc: true,
|
||||||
})
|
})
|
||||||
|
tx.Statement.RaiseErrorOnNotFound = true
|
||||||
tx.Statement.Dest = out
|
tx.Statement.Dest = out
|
||||||
tx.callbacks.Query().Execute(tx)
|
tx.callbacks.Query().Execute(tx)
|
||||||
return
|
return
|
||||||
|
@ -35,7 +36,8 @@ func (db *DB) First(out interface{}, where ...interface{}) (tx *DB) {
|
||||||
|
|
||||||
// Take return a record that match given conditions, the order will depend on the database implementation
|
// Take return a record that match given conditions, the order will depend on the database implementation
|
||||||
func (db *DB) Take(out interface{}, where ...interface{}) (tx *DB) {
|
func (db *DB) Take(out interface{}, where ...interface{}) (tx *DB) {
|
||||||
tx = db.getInstance()
|
tx = db.getInstance().Limit(1)
|
||||||
|
tx.Statement.RaiseErrorOnNotFound = true
|
||||||
tx.Statement.Dest = out
|
tx.Statement.Dest = out
|
||||||
tx.callbacks.Query().Execute(tx)
|
tx.callbacks.Query().Execute(tx)
|
||||||
return
|
return
|
||||||
|
@ -46,6 +48,7 @@ func (db *DB) Last(out interface{}, where ...interface{}) (tx *DB) {
|
||||||
tx = db.getInstance().Limit(1).Order(clause.OrderByColumn{
|
tx = db.getInstance().Limit(1).Order(clause.OrderByColumn{
|
||||||
Column: clause.Column{Table: clause.CurrentTable, Name: clause.PrimaryKey},
|
Column: clause.Column{Table: clause.CurrentTable, Name: clause.PrimaryKey},
|
||||||
})
|
})
|
||||||
|
tx.Statement.RaiseErrorOnNotFound = true
|
||||||
tx.Statement.Dest = out
|
tx.Statement.Dest = out
|
||||||
tx.callbacks.Query().Execute(tx)
|
tx.callbacks.Query().Execute(tx)
|
||||||
return
|
return
|
||||||
|
@ -54,6 +57,8 @@ func (db *DB) Last(out interface{}, where ...interface{}) (tx *DB) {
|
||||||
// Find find records that match given conditions
|
// Find find records that match given conditions
|
||||||
func (db *DB) Find(out interface{}, where ...interface{}) (tx *DB) {
|
func (db *DB) Find(out interface{}, where ...interface{}) (tx *DB) {
|
||||||
tx = db.getInstance()
|
tx = db.getInstance()
|
||||||
|
tx.Statement.Dest = out
|
||||||
|
tx.callbacks.Query().Execute(tx)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
21
statement.go
21
statement.go
|
@ -40,16 +40,17 @@ func (inst *Instance) AddError(err error) {
|
||||||
|
|
||||||
// Statement statement
|
// Statement statement
|
||||||
type Statement struct {
|
type Statement struct {
|
||||||
Table string
|
Table string
|
||||||
Model interface{}
|
Model interface{}
|
||||||
Dest interface{}
|
Dest interface{}
|
||||||
ReflectValue reflect.Value
|
ReflectValue reflect.Value
|
||||||
Clauses map[string]clause.Clause
|
Clauses map[string]clause.Clause
|
||||||
Selects []string // selected columns
|
Selects []string // selected columns
|
||||||
Omits []string // omit columns
|
Omits []string // omit columns
|
||||||
Settings sync.Map
|
Settings sync.Map
|
||||||
DB *DB
|
DB *DB
|
||||||
Schema *schema.Schema
|
Schema *schema.Schema
|
||||||
|
RaiseErrorOnNotFound bool
|
||||||
|
|
||||||
// SQL Builder
|
// SQL Builder
|
||||||
SQL strings.Builder
|
SQL strings.Builder
|
||||||
|
|
|
@ -18,7 +18,6 @@ func RunTestsSuit(t *testing.T, db *gorm.DB) {
|
||||||
|
|
||||||
func TestCreate(t *testing.T, db *gorm.DB) {
|
func TestCreate(t *testing.T, db *gorm.DB) {
|
||||||
db.AutoMigrate(&User{})
|
db.AutoMigrate(&User{})
|
||||||
db = db.Debug()
|
|
||||||
|
|
||||||
t.Run("Create", func(t *testing.T) {
|
t.Run("Create", func(t *testing.T) {
|
||||||
var user = User{
|
var user = User{
|
||||||
|
|
Loading…
Reference in New Issue