move row, rows, scan to scope

This commit is contained in:
Jinzhu 2014-01-28 09:48:44 +08:00
parent 549d7a8af2
commit 6e848fc987
3 changed files with 35 additions and 22 deletions

View File

@ -1,9 +1,7 @@
package gorm package gorm
import ( import (
"fmt"
"reflect" "reflect"
"strings"
"time" "time"
) )
@ -25,6 +23,9 @@ func Query(scope *Scope) {
) )
var dest = reflect.Indirect(reflect.ValueOf(scope.Value)) var dest = reflect.Indirect(reflect.ValueOf(scope.Value))
if value, ok := scope.Get("gorm:query_destination"); ok {
dest = reflect.Indirect(reflect.ValueOf(value))
}
if dest.Kind() == reflect.Slice { if dest.Kind() == reflect.Slice {
isSlice = true isSlice = true
@ -33,11 +34,7 @@ func Query(scope *Scope) {
scope.Search = scope.Search.clone().limit(1) scope.Search = scope.Search.clone().limit(1)
} }
if scope.Search.raw { scope.prepareQuerySql()
scope.Raw(strings.TrimLeft(scope.CombinedConditionSql(), "WHERE "))
} else {
scope.Raw(fmt.Sprintf("SELECT %v FROM %v %v", scope.SelectSql(), scope.TableName(), scope.CombinedConditionSql()))
}
if !scope.HasError() { if !scope.HasError() {
rows, err := scope.DB().Query(scope.Sql, scope.SqlVars...) rows, err := scope.DB().Query(scope.Sql, scope.SqlVars...)

24
main.go
View File

@ -109,6 +109,14 @@ func (s *DB) Unscoped() *DB {
return s.clone().search.unscoped().db return s.clone().search.unscoped().db
} }
func (s *DB) Attrs(attrs ...interface{}) *DB {
return s.clone().search.attrs(attrs...).db
}
func (s *DB) Assign(attrs ...interface{}) *DB {
return s.clone().search.assign(attrs...).db
}
func (s *DB) First(out interface{}, where ...interface{}) *DB { func (s *DB) First(out interface{}, where ...interface{}) *DB {
scope := s.clone().NewScope(out) scope := s.clone().NewScope(out)
scope.Search = scope.Search.clone().order(scope.PrimaryKey()).limit(1) scope.Search = scope.Search.clone().order(scope.PrimaryKey()).limit(1)
@ -126,23 +134,17 @@ func (s *DB) Find(out interface{}, where ...interface{}) *DB {
} }
func (s *DB) Row() *sql.Row { func (s *DB) Row() *sql.Row {
return s.do(s.Value).row() return s.NewScope(s.Value).row()
} }
func (s *DB) Rows() (*sql.Rows, error) { func (s *DB) Rows() (*sql.Rows, error) {
return s.do(s.Value).rows() return s.NewScope(s.Value).rows()
} }
func (s *DB) Scan(dest interface{}) *DB { func (s *DB) Scan(dest interface{}) *DB {
return s.do(s.Value).query(dest).db scope := s.clone().NewScope(s.Value).Set("gorm:query_destination", dest)
} Query(scope)
return scope.db
func (s *DB) Attrs(attrs ...interface{}) *DB {
return s.clone().search.attrs(attrs...).db
}
func (s *DB) Assign(attrs ...interface{}) *DB {
return s.clone().search.assign(attrs...).db
} }
func (s *DB) FirstOrInit(out interface{}, where ...interface{}) *DB { func (s *DB) FirstOrInit(out interface{}, where ...interface{}) *DB {

View File

@ -1,6 +1,7 @@
package gorm package gorm
import ( import (
"database/sql"
"errors" "errors"
"fmt" "fmt"
"github.com/jinzhu/gorm/dialect" "github.com/jinzhu/gorm/dialect"
@ -378,10 +379,23 @@ func (scope *Scope) CommitOrRollback() *Scope {
return scope return scope
} }
func (scope *Scope) SelectSql() string { func (scope *Scope) prepareQuerySql() {
if len(scope.Search.selectStr) == 0 { if scope.Search.raw {
return "*" scope.Raw(strings.TrimLeft(scope.CombinedConditionSql(), "WHERE "))
} else { } else {
return scope.Search.selectStr scope.Raw(fmt.Sprintf("SELECT %v FROM %v %v", scope.selectSql(), scope.TableName(), scope.CombinedConditionSql()))
} }
return
}
func (scope *Scope) row() *sql.Row {
defer scope.Trace(time.Now())
scope.prepareQuerySql()
return scope.DB().QueryRow(scope.Sql, scope.SqlVars...)
}
func (scope *Scope) rows() (*sql.Rows, error) {
defer scope.Trace(time.Now())
scope.prepareQuerySql()
return scope.DB().Query(scope.Sql, scope.SqlVars...)
} }