From 244b27a6a014c83d1cbc9b19a40320e886f2d161 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Sun, 27 Oct 2013 08:06:01 +0800 Subject: [PATCH] Refact main.go --- main.go | 73 ++++++++++++++++++++--------------------------------- orm.go | 9 +++++-- orm_test.go | 4 +-- 3 files changed, 36 insertions(+), 50 deletions(-) diff --git a/main.go b/main.go index 3cb3d95c..bda178d8 100644 --- a/main.go +++ b/main.go @@ -18,72 +18,53 @@ func Open(driver, source string) (db DB, err error) { } func (s *DB) buildORM() *Orm { - orm := &Orm{db: s.Db, driver: s.Driver} - return orm + return &Orm{db: s.Db, driver: s.Driver} } -func (s *DB) Where(querystring interface{}, args ...interface{}) (orm *Orm) { - orm = s.buildORM() - orm.Where(querystring, args...) - return +func (s *DB) Where(querystring interface{}, args ...interface{}) *Orm { + return s.buildORM().Where(querystring, args...) } -func (s *DB) First(out interface{}) (orm *Orm) { - orm = s.buildORM() - orm.First(out) - return +func (s *DB) First(out interface{}) *Orm { + return s.buildORM().First(out) } -func (s *DB) Find(out interface{}) (orm *Orm) { - orm = s.buildORM() - orm.Find(out) - return +func (s *DB) Find(out interface{}) *Orm { + return s.buildORM().Find(out) } -func (s *DB) Limit(value interface{}) (orm *Orm) { - orm = s.buildORM() - orm.Limit(value) - return +func (s *DB) Limit(value interface{}) *Orm { + return s.buildORM().Limit(value) } -func (s *DB) Offset(value interface{}) (orm *Orm) { - orm = s.buildORM() - orm.Offset(value) - return +func (s *DB) Offset(value interface{}) *Orm { + return s.buildORM().Offset(value) } -func (s *DB) Order(value string, reorder ...bool) (orm *Orm) { - orm = s.buildORM() - orm.Order(value, reorder...) - return +func (s *DB) Order(value string, reorder ...bool) *Orm { + return s.buildORM().Order(value, reorder...) } -func (s *DB) Select(value interface{}) (orm *Orm) { - orm = s.buildORM() - orm.Select(value) - return +func (s *DB) Select(value interface{}) *Orm { + return s.buildORM().Select(value) } -func (s *DB) Save(value interface{}) (orm *Orm) { - orm = s.buildORM() - orm.Save(value) - return +func (s *DB) Save(value interface{}) *Orm { + return s.buildORM().Save(value) } -func (s *DB) Delete(value interface{}) (orm *Orm) { - orm = s.buildORM() - orm.Delete(value) - return +func (s *DB) Delete(value interface{}) *Orm { + return s.buildORM().Delete(value) } -func (s *DB) Exec(sql string) (orm *Orm) { - orm = s.buildORM() - orm.Exec(sql) - return +func (s *DB) Exec(sql string) *Orm { + return s.buildORM().Exec(sql) } -func (s *DB) CreateTable(value interface{}) (orm *Orm) { - orm = s.buildORM() - orm.CreateTable(value) - return +func (s *DB) Model(value interface{}) *Orm { + return s.buildORM().Model(value) +} + +func (s *DB) CreateTable(value interface{}) *Orm { + return s.buildORM().CreateTable(value) } diff --git a/orm.go b/orm.go index 4007dd6e..26b4d268 100644 --- a/orm.go +++ b/orm.go @@ -26,11 +26,11 @@ type Orm struct { operation string } -func (s *Orm) Model(model interface{}) (err error) { +func (s *Orm) Model(model interface{}) *Orm { s.model = s.toModel(model) s.TableName = s.model.TableName() s.PrimaryKey = s.model.PrimaryKeyDb() - return + return s } func (s *Orm) Where(querystring interface{}, args ...interface{}) *Orm { @@ -127,6 +127,11 @@ func (s *Orm) Find(out interface{}) *Orm { return s } +func (s *Orm) Pluck(column string, value interface{}) (orm *Orm) { + s.explain(s.model, "Query").query(value) + return +} + func (s *Orm) Or(querystring interface{}, args ...interface{}) *Orm { return s } diff --git a/orm_test.go b/orm_test.go index 76d11dd9..aa98db06 100644 --- a/orm_test.go +++ b/orm_test.go @@ -226,6 +226,6 @@ func TestComplexWhere(t *testing.T) { } func TestOrder(t *testing.T) { - var users []User - db.Order("age desc").Find(&users) + // var ages []int64 + // db.Model(&User).Order("age desc").Pluck("age", &ages) }