diff --git a/main.go b/main.go index 1ebe104f..53dcc807 100644 --- a/main.go +++ b/main.go @@ -56,9 +56,9 @@ func (s *DB) Order(value interface{}) (orm *Orm) { return } -func (s *DB) Select(querystring string) (orm *Orm) { +func (s *DB) Select(value interface{}) (orm *Orm) { orm = s.buildORM() - orm.Select(querystring) + orm.Select(value) return } diff --git a/orm.go b/orm.go index 1d9a0de5..c12a9968 100644 --- a/orm.go +++ b/orm.go @@ -1,17 +1,23 @@ package gorm -import "database/sql" +import ( + "errors" + "strconv" + + "database/sql" +) type Orm struct { TableName string PrimaryKey string - Error bool + Error error db *sql.DB whereClause []interface{} + selectStr string orderStr string - offsetInt int64 - limitInt int64 + offsetInt int + limitInt int } func (s *Orm) Where(querystring interface{}, args ...interface{}) *Orm { @@ -19,31 +25,37 @@ func (s *Orm) Where(querystring interface{}, args ...interface{}) *Orm { return s } -func (s *Orm) First(out interface{}) *Orm { - return s -} - -func (s *Orm) Find(out interface{}) *Orm { - return s -} - func (s *Orm) Limit(value interface{}) *Orm { + switch value := value.(type) { + case string: + s.limitInt, _ = strconv.Atoi(value) + case int: + s.limitInt = value + default: + s.Error = errors.New("Can' understand the value of Limit, Should be int") + } return s } func (s *Orm) Offset(value interface{}) *Orm { + switch value := value.(type) { + case string: + s.offsetInt, _ = strconv.Atoi(value) + case int: + s.offsetInt = value + default: + s.Error = errors.New("Can' understand the value of Offset, Should be int") + } return s } func (s *Orm) Order(value interface{}) *Orm { - return s -} - -func (s *Orm) Or(querystring interface{}, args ...interface{}) *Orm { - return s -} - -func (s *Orm) Not(querystring interface{}, args ...interface{}) *Orm { + switch value := value.(type) { + case string: + s.orderStr = value + default: + s.Error = errors.New("Can' understand the value of Order, Should be string") + } return s } @@ -51,7 +63,13 @@ func (s *Orm) Count() int64 { return 0 } -func (s *Orm) Select(querystring string) *Orm { +func (s *Orm) Select(value interface{}) *Orm { + switch value := value.(type) { + case string: + s.selectStr = value + default: + s.Error = errors.New("Can' understand the value of Select, Should be string") + } return s } @@ -75,6 +93,18 @@ func (s *Orm) Exec(sql string) *Orm { return s } -func (s *Orm) Explain() string { - return "" +func (s *Orm) First(out interface{}) *Orm { + return s +} + +func (s *Orm) Find(out interface{}) *Orm { + return s +} + +func (s *Orm) Or(querystring interface{}, args ...interface{}) *Orm { + return s +} + +func (s *Orm) Not(querystring interface{}, args ...interface{}) *Orm { + return s } diff --git a/orm_test.go b/orm_test.go index b8d0375d..6bf69931 100644 --- a/orm_test.go +++ b/orm_test.go @@ -6,12 +6,19 @@ import ( "testing" ) +type User struct { + Name string +} + func TestWhere(t *testing.T) { db, err := Open("postgres", "user=gorm dbname=gorm") if err != err { t.Errorf("Error should be nil") } - orm := db.Where("id = $1", 1).Where("name = $1", "jinzhu") - fmt.Println(orm.whereClause) + orm := db.Where("id = $1", 1, 3, 4, []int64{1, 2, 3}).Where("name = $1", "jinzhu") + + user := &User{} + orm.First(user) + fmt.Println(user) }