mirror of https://github.com/go-gorm/gorm.git
make pluck works
This commit is contained in:
parent
7a99f37ba4
commit
5fd2e0d54f
2
main.go
2
main.go
|
@ -224,7 +224,7 @@ func (s *DB) Related(value interface{}, foreign_keys ...string) *DB {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DB) Pluck(column string, value interface{}) *DB {
|
func (s *DB) Pluck(column string, value interface{}) *DB {
|
||||||
return s.do(s.Value).pluck(column, value).db
|
return s.NewScope(s.Value).pluck(column, value).db
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DB) Count(value interface{}) *DB {
|
func (s *DB) Count(value interface{}) *DB {
|
||||||
|
|
40
scope.go
40
scope.go
|
@ -191,6 +191,10 @@ func (scope *Scope) TableName() string {
|
||||||
if len(scope.Search.tableName) > 0 {
|
if len(scope.Search.tableName) > 0 {
|
||||||
return scope.Search.tableName
|
return scope.Search.tableName
|
||||||
} else {
|
} else {
|
||||||
|
if scope.Value == nil {
|
||||||
|
scope.Err(errors.New("can't get table name"))
|
||||||
|
return ""
|
||||||
|
}
|
||||||
data := reflect.Indirect(reflect.ValueOf(scope.Value))
|
data := reflect.Indirect(reflect.ValueOf(scope.Value))
|
||||||
|
|
||||||
if data.Kind() == reflect.Slice {
|
if data.Kind() == reflect.Slice {
|
||||||
|
@ -380,22 +384,6 @@ func (scope *Scope) CommitOrRollback() *Scope {
|
||||||
return scope
|
return scope
|
||||||
}
|
}
|
||||||
|
|
||||||
func (scope *Scope) prepareQuerySql() {
|
|
||||||
if scope.Search.raw {
|
|
||||||
scope.Raw(strings.TrimLeft(scope.CombinedConditionSql(), "WHERE "))
|
|
||||||
} else {
|
|
||||||
scope.Raw(fmt.Sprintf("SELECT %v FROM %v %v", scope.selectSql(), scope.TableName(), scope.CombinedConditionSql()))
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (scope *Scope) inlineCondition(values []interface{}) *Scope {
|
|
||||||
if len(values) > 0 {
|
|
||||||
scope.Search = scope.Search.clone().where(values[0], values[1:]...)
|
|
||||||
}
|
|
||||||
return scope
|
|
||||||
}
|
|
||||||
|
|
||||||
func (scope *Scope) row() *sql.Row {
|
func (scope *Scope) row() *sql.Row {
|
||||||
defer scope.Trace(time.Now())
|
defer scope.Trace(time.Now())
|
||||||
scope.prepareQuerySql()
|
scope.prepareQuerySql()
|
||||||
|
@ -416,3 +404,23 @@ func (scope *Scope) initialize() *Scope {
|
||||||
scope.updatedAttrsWithValues(convertInterfaceToMap(scope.Search.assignAttrs), false)
|
scope.updatedAttrsWithValues(convertInterfaceToMap(scope.Search.assignAttrs), false)
|
||||||
return scope
|
return scope
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (scope *Scope) pluck(column string, value interface{}) *Scope {
|
||||||
|
dest := reflect.Indirect(reflect.ValueOf(value))
|
||||||
|
scope.Search = scope.Search.clone().selects(column)
|
||||||
|
if dest.Kind() != reflect.Slice {
|
||||||
|
scope.Err(errors.New("Results should be a slice"))
|
||||||
|
return scope
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, err := scope.rows()
|
||||||
|
if scope.Err(err) == nil {
|
||||||
|
defer rows.Close()
|
||||||
|
for rows.Next() {
|
||||||
|
elem := reflect.New(dest.Type().Elem()).Interface()
|
||||||
|
scope.Err(rows.Scan(elem))
|
||||||
|
dest.Set(reflect.Append(dest, reflect.ValueOf(elem).Elem()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return scope
|
||||||
|
}
|
||||||
|
|
|
@ -233,3 +233,19 @@ func (s *Scope) havingSql() string {
|
||||||
func (s *Scope) joinsSql() string {
|
func (s *Scope) joinsSql() string {
|
||||||
return s.Search.joinsStr + " "
|
return s.Search.joinsStr + " "
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (scope *Scope) prepareQuerySql() {
|
||||||
|
if scope.Search.raw {
|
||||||
|
scope.Raw(strings.TrimLeft(scope.CombinedConditionSql(), "WHERE "))
|
||||||
|
} else {
|
||||||
|
scope.Raw(fmt.Sprintf("SELECT %v FROM %v %v", scope.selectSql(), scope.TableName(), scope.CombinedConditionSql()))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (scope *Scope) inlineCondition(values []interface{}) *Scope {
|
||||||
|
if len(values) > 0 {
|
||||||
|
scope.Search = scope.Search.clone().where(values[0], values[1:]...)
|
||||||
|
}
|
||||||
|
return scope
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue