forked from mirror/gorm
Clean up code
This commit is contained in:
parent
23db784911
commit
788da015d1
|
@ -555,6 +555,9 @@ db.Model(&User{}).Pluck("name", &names)
|
||||||
// Set Table With Table
|
// Set Table With Table
|
||||||
db.Table("deleted_users").Pluck("name", &names)
|
db.Table("deleted_users").Pluck("name", &names)
|
||||||
//// SELECT name FROM deleted_users;
|
//// SELECT name FROM deleted_users;
|
||||||
|
|
||||||
|
// Pluck more than one column? Do it like this
|
||||||
|
db.Select("name, age").Find(&users)
|
||||||
```
|
```
|
||||||
|
|
||||||
## Callbacks
|
## Callbacks
|
||||||
|
@ -719,7 +722,7 @@ db.Where("email = ?", "x@example.org").Attrs(User{FromIp: "111.111.111.111"}).Fi
|
||||||
```
|
```
|
||||||
|
|
||||||
## TODO
|
## TODO
|
||||||
* Join, Having, Group, Includes, Pluck (distinct)
|
* Join, Having, Group, Includes
|
||||||
* Scopes
|
* Scopes
|
||||||
* Index, Unique, Valiations
|
* Index, Unique, Valiations
|
||||||
|
|
||||||
|
|
66
do.go
66
do.go
|
@ -52,10 +52,6 @@ func (s *Do) err(err error) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Do) hasError() bool {
|
|
||||||
return s.chain.hasError()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Do) setModel(value interface{}) *Do {
|
func (s *Do) setModel(value interface{}) *Do {
|
||||||
s.model = &Model{data: value, do: s}
|
s.model = &Model{data: value, do: s}
|
||||||
s.value = value
|
s.value = value
|
||||||
|
@ -72,7 +68,7 @@ func (s *Do) addToVars(value interface{}) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Do) exec(sqls ...string) (err error) {
|
func (s *Do) exec(sqls ...string) (err error) {
|
||||||
if s.hasError() {
|
if s.chain.hasError() {
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
if len(sqls) > 0 {
|
if len(sqls) > 0 {
|
||||||
|
@ -178,7 +174,7 @@ func (s *Do) create() (i interface{}) {
|
||||||
s.saveBeforeAssociations()
|
s.saveBeforeAssociations()
|
||||||
s.prepareCreateSql()
|
s.prepareCreateSql()
|
||||||
|
|
||||||
if !s.hasError() {
|
if !s.chain.hasError() {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
var id interface{}
|
var id interface{}
|
||||||
if s.chain.driver() == "postgres" {
|
if s.chain.driver() == "postgres" {
|
||||||
|
@ -191,7 +187,7 @@ func (s *Do) create() (i interface{}) {
|
||||||
}
|
}
|
||||||
s.chain.slog(s.sql, now, s.sqlVars...)
|
s.chain.slog(s.sql, now, s.sqlVars...)
|
||||||
|
|
||||||
if !s.hasError() {
|
if !s.chain.hasError() {
|
||||||
result := reflect.Indirect(reflect.ValueOf(s.value))
|
result := reflect.Indirect(reflect.ValueOf(s.value))
|
||||||
if !setFieldValue(result.FieldByName(s.model.primaryKey()), id) {
|
if !setFieldValue(result.FieldByName(s.model.primaryKey()), id) {
|
||||||
fmt.Printf("Can't set primary key for %#v\n", result.Interface())
|
fmt.Printf("Can't set primary key for %#v\n", result.Interface())
|
||||||
|
@ -272,7 +268,7 @@ func (s *Do) update() (i interface{}) {
|
||||||
s.saveBeforeAssociations()
|
s.saveBeforeAssociations()
|
||||||
s.prepareUpdateSql(update_attrs)
|
s.prepareUpdateSql(update_attrs)
|
||||||
|
|
||||||
if !s.hasError() {
|
if !s.chain.hasError() {
|
||||||
s.exec()
|
s.exec()
|
||||||
s.saveAfterAssociations()
|
s.saveAfterAssociations()
|
||||||
|
|
||||||
|
@ -291,7 +287,7 @@ func (s *Do) prepareDeleteSql() {
|
||||||
func (s *Do) delete() {
|
func (s *Do) delete() {
|
||||||
s.model.callMethod("BeforeDelete")
|
s.model.callMethod("BeforeDelete")
|
||||||
|
|
||||||
if !s.hasError() {
|
if !s.chain.hasError() {
|
||||||
if !s.unscoped && s.model.hasColumn("DeletedAt") {
|
if !s.unscoped && s.model.hasColumn("DeletedAt") {
|
||||||
delete_sql := "deleted_at=" + s.addToVars(time.Now())
|
delete_sql := "deleted_at=" + s.addToVars(time.Now())
|
||||||
s.sql = fmt.Sprintf("UPDATE %v SET %v %v", s.tableName(), delete_sql, s.combinedSql())
|
s.sql = fmt.Sprintf("UPDATE %v SET %v %v", s.tableName(), delete_sql, s.combinedSql())
|
||||||
|
@ -379,7 +375,7 @@ func (s *Do) query() {
|
||||||
}
|
}
|
||||||
|
|
||||||
s.prepareQuerySql()
|
s.prepareQuerySql()
|
||||||
if !s.hasError() {
|
if !s.chain.hasError() {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
rows, err := s.db.Query(s.sql, s.sqlVars...)
|
rows, err := s.db.Query(s.sql, s.sqlVars...)
|
||||||
s.chain.slog(s.sql, now, s.sqlVars...)
|
s.chain.slog(s.sql, now, s.sqlVars...)
|
||||||
|
@ -433,26 +429,12 @@ func (s *Do) query() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Do) count(value interface{}) {
|
func (s *Do) count(value interface{}) {
|
||||||
dest_out := reflect.Indirect(reflect.ValueOf(value))
|
|
||||||
|
|
||||||
s.prepareQuerySql()
|
s.prepareQuerySql()
|
||||||
if !s.hasError() {
|
if !s.chain.hasError() {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
rows, err := s.db.Query(s.sql, s.sqlVars...)
|
s.err(s.db.QueryRow(s.sql, s.sqlVars...).Scan(value))
|
||||||
s.chain.slog(s.sql, now, s.sqlVars...)
|
s.chain.slog(s.sql, now, s.sqlVars...)
|
||||||
if s.err(err) != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
defer rows.Close()
|
|
||||||
for rows.Next() {
|
|
||||||
var dest int64
|
|
||||||
if s.err(rows.Scan(&dest)) == nil {
|
|
||||||
setFieldValue(dest_out, dest)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Do) pluck(column string, value interface{}) {
|
func (s *Do) pluck(column string, value interface{}) {
|
||||||
|
@ -460,42 +442,26 @@ func (s *Do) pluck(column string, value interface{}) {
|
||||||
dest_out := reflect.Indirect(reflect.ValueOf(value))
|
dest_out := reflect.Indirect(reflect.ValueOf(value))
|
||||||
|
|
||||||
if dest_out.Kind() != reflect.Slice {
|
if dest_out.Kind() != reflect.Slice {
|
||||||
s.err(errors.New("Return results should be a slice"))
|
s.err(errors.New("Results should be a slice"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
dest_type := dest_out.Type().Elem()
|
|
||||||
|
|
||||||
s.prepareQuerySql()
|
s.prepareQuerySql()
|
||||||
|
|
||||||
if !s.hasError() {
|
if !s.chain.hasError() {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
rows, err := s.db.Query(s.sql, s.sqlVars...)
|
rows, err := s.db.Query(s.sql, s.sqlVars...)
|
||||||
s.chain.slog(s.sql, now, s.sqlVars...)
|
s.chain.slog(s.sql, now, s.sqlVars...)
|
||||||
if s.err(err) != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
defer rows.Close()
|
if s.err(err) == nil {
|
||||||
for rows.Next() {
|
defer rows.Close()
|
||||||
dest := reflect.New(dest_type).Elem().Interface()
|
for rows.Next() {
|
||||||
s.err(rows.Scan(&dest))
|
dest := reflect.New(dest_out.Type().Elem()).Interface()
|
||||||
|
s.err(rows.Scan(dest))
|
||||||
switch dest.(type) {
|
dest_out.Set(reflect.Append(dest_out, reflect.ValueOf(dest).Elem()))
|
||||||
case []uint8:
|
|
||||||
if dest_type.String() == "string" {
|
|
||||||
dest = string(dest.([]uint8))
|
|
||||||
} else if dest_type.String() == "int64" {
|
|
||||||
dest, _ = strconv.Atoi(string(dest.([]uint8)))
|
|
||||||
dest = int64(dest.(int))
|
|
||||||
}
|
|
||||||
|
|
||||||
dest_out.Set(reflect.Append(dest_out, reflect.ValueOf(dest)))
|
|
||||||
default:
|
|
||||||
dest_out.Set(reflect.Append(dest_out, reflect.ValueOf(dest)))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Do) where(where ...interface{}) *Do {
|
func (s *Do) where(where ...interface{}) *Do {
|
||||||
|
|
|
@ -505,6 +505,8 @@ func TestOrderAndPluck(t *testing.T) {
|
||||||
if !(names[0] == "1" && names[2] == "3" && names[3] == "3" && ages[2] == 24 && ages[3] == 22) {
|
if !(names[0] == "1" && names[2] == "3" && names[3] == "3" && ages[2] == 24 && ages[3] == 22) {
|
||||||
t.Errorf("Should be ordered correctly with multiple orders")
|
t.Errorf("Should be ordered correctly with multiple orders")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
db.Model(User{}).Select("name, age").Find(&[]User{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLimit(t *testing.T) {
|
func TestLimit(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue