Add more dummy code for orm.go

This commit is contained in:
Jinzhu 2013-10-26 07:14:57 +08:00
parent 7f9d486d5a
commit 540345f552
3 changed files with 64 additions and 27 deletions

View File

@ -56,9 +56,9 @@ func (s *DB) Order(value interface{}) (orm *Orm) {
return return
} }
func (s *DB) Select(querystring string) (orm *Orm) { func (s *DB) Select(value interface{}) (orm *Orm) {
orm = s.buildORM() orm = s.buildORM()
orm.Select(querystring) orm.Select(value)
return return
} }

76
orm.go
View File

@ -1,17 +1,23 @@
package gorm package gorm
import "database/sql" import (
"errors"
"strconv"
"database/sql"
)
type Orm struct { type Orm struct {
TableName string TableName string
PrimaryKey string PrimaryKey string
Error bool Error error
db *sql.DB db *sql.DB
whereClause []interface{} whereClause []interface{}
selectStr string
orderStr string orderStr string
offsetInt int64 offsetInt int
limitInt int64 limitInt int
} }
func (s *Orm) Where(querystring interface{}, args ...interface{}) *Orm { func (s *Orm) Where(querystring interface{}, args ...interface{}) *Orm {
@ -19,31 +25,37 @@ func (s *Orm) Where(querystring interface{}, args ...interface{}) *Orm {
return s 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 { 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 return s
} }
func (s *Orm) Offset(value interface{}) *Orm { 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 return s
} }
func (s *Orm) Order(value interface{}) *Orm { func (s *Orm) Order(value interface{}) *Orm {
return s switch value := value.(type) {
} case string:
s.orderStr = value
func (s *Orm) Or(querystring interface{}, args ...interface{}) *Orm { default:
return s s.Error = errors.New("Can' understand the value of Order, Should be string")
} }
func (s *Orm) Not(querystring interface{}, args ...interface{}) *Orm {
return s return s
} }
@ -51,7 +63,13 @@ func (s *Orm) Count() int64 {
return 0 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 return s
} }
@ -75,6 +93,18 @@ func (s *Orm) Exec(sql string) *Orm {
return s return s
} }
func (s *Orm) Explain() string { func (s *Orm) First(out interface{}) *Orm {
return "" 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
} }

View File

@ -6,12 +6,19 @@ import (
"testing" "testing"
) )
type User struct {
Name string
}
func TestWhere(t *testing.T) { func TestWhere(t *testing.T) {
db, err := Open("postgres", "user=gorm dbname=gorm") db, err := Open("postgres", "user=gorm dbname=gorm")
if err != err { if err != err {
t.Errorf("Error should be nil") t.Errorf("Error should be nil")
} }
orm := db.Where("id = $1", 1).Where("name = $1", "jinzhu") orm := db.Where("id = $1", 1, 3, 4, []int64{1, 2, 3}).Where("name = $1", "jinzhu")
fmt.Println(orm.whereClause)
user := &User{}
orm.First(user)
fmt.Println(user)
} }