forked from mirror/gorm
Support scan into int, string data types
This commit is contained in:
parent
e583dfa196
commit
02fb382ec0
|
@ -384,7 +384,9 @@ func (db *DB) Pluck(column string, dest interface{}) (tx *DB) {
|
|||
|
||||
func (db *DB) ScanRows(rows *sql.Rows, dest interface{}) error {
|
||||
tx := db.getInstance()
|
||||
tx.Error = tx.Statement.Parse(dest)
|
||||
if err := tx.Statement.Parse(dest); !errors.Is(err, schema.ErrUnsupportedDataType) {
|
||||
tx.AddError(err)
|
||||
}
|
||||
tx.Statement.Dest = dest
|
||||
tx.Statement.ReflectValue = reflect.ValueOf(dest)
|
||||
for tx.Statement.ReflectValue.Kind() == reflect.Ptr {
|
||||
|
|
2
scan.go
2
scan.go
|
@ -82,7 +82,7 @@ func Scan(rows *sql.Rows, db *DB, initialized bool) {
|
|||
scanIntoMap(mapValue, values, columns)
|
||||
*dest = append(*dest, mapValue)
|
||||
}
|
||||
case *int, *int64, *uint, *uint64, *float32, *float64:
|
||||
case *int, *int64, *uint, *uint64, *float32, *float64, *string:
|
||||
for initialized || rows.Next() {
|
||||
initialized = false
|
||||
db.RowsAffected++
|
||||
|
|
|
@ -91,4 +91,14 @@ func TestScanRows(t *testing.T) {
|
|||
if !reflect.DeepEqual(results, []Result{{Name: "ScanRowsUser2", Age: 10}, {Name: "ScanRowsUser3", Age: 20}}) {
|
||||
t.Errorf("Should find expected results")
|
||||
}
|
||||
|
||||
var ages int
|
||||
if err := DB.Table("users").Where("name = ? or name = ?", user2.Name, user3.Name).Select("SUM(age)").Scan(&ages).Error; err != nil || ages != 30 {
|
||||
t.Fatalf("failed to scan ages, got error %v, ages: %v", err, ages)
|
||||
}
|
||||
|
||||
var name string
|
||||
if err := DB.Table("users").Where("name = ?", user2.Name).Select("name").Scan(&name).Error; err != nil || name != user2.Name {
|
||||
t.Fatalf("failed to scan ages, got error %v, ages: %v", err, name)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue