Add some utils

This commit is contained in:
Jinzhu 2013-10-26 09:49:40 +08:00
parent 540345f552
commit 11758c647f
4 changed files with 109 additions and 6 deletions

22
orm.go
View File

@ -2,6 +2,7 @@ package gorm
import (
"errors"
"strconv"
"database/sql"
@ -11,6 +12,8 @@ type Orm struct {
TableName string
PrimaryKey string
Error error
Sql string
SqlVars []interface{}
db *sql.DB
whereClause []interface{}
@ -18,6 +21,13 @@ type Orm struct {
orderStr string
offsetInt int
limitInt int
operation string
}
func (s *Orm) setModel(model interface{}) (err error) {
s.TableName = "user"
s.PrimaryKey = "id"
return
}
func (s *Orm) Where(querystring interface{}, args ...interface{}) *Orm {
@ -74,10 +84,14 @@ func (s *Orm) Select(value interface{}) *Orm {
}
func (s *Orm) Save(value interface{}) *Orm {
s.explain(value, "Save")
s.Exec()
return s
}
func (s *Orm) Delete(value interface{}) *Orm {
s.explain(value, "Delete")
s.Exec()
return s
}
@ -89,11 +103,17 @@ func (s *Orm) Updates(values map[string]string) *Orm {
return s
}
func (s *Orm) Exec(sql string) *Orm {
func (s *Orm) Exec(sql ...string) *Orm {
if len(sql) == 0 {
s.db.Exec(s.Sql, s.SqlVars...)
} else {
s.db.Exec(sql[0])
}
return s
}
func (s *Orm) First(out interface{}) *Orm {
s.setModel(out)
return s
}

View File

@ -2,6 +2,7 @@ package gorm
import (
"fmt"
"time"
"testing"
)
@ -10,14 +11,28 @@ type User struct {
Name string
}
func TestWhere(t *testing.T) {
db, err := Open("postgres", "user=gorm dbname=gorm")
func getDB() DB {
db, _ := Open("postgres", "user=gorm dbname=gorm")
return db
}
if err != err {
t.Errorf("Error should be nil")
func TestSaveAndFirst(t *testing.T) {
db := getDB()
u := &User{Name: "jinzhu"}
fmt.Println(db.Save(u).Sql)
fmt.Println(time.Now().String())
user := &User{}
db.First(&user)
if user.Name != "jinzhu" {
t.Errorf("User should be saved and fetched correctly")
}
orm := db.Where("id = $1", 1, 3, 4, []int64{1, 2, 3}).Where("name = $1", "jinzhu")
}
func TestWhere(t *testing.T) {
db := getDB()
orm := db.Where("id = $1", 1, 3, 4, []int64{1, 2, 3}).Where("name = $1", "jinzhu")
user := &User{}
orm.First(user)
fmt.Println(user)

40
sql.go Normal file
View File

@ -0,0 +1,40 @@
package gorm
import (
"strings"
"fmt"
)
func (s *Orm) explain(value interface{}, operation string) {
s.setModel(value)
switch operation {
case "Save":
s.saveSql(value)
case "Delete":
s.deleteSql(value)
}
return
}
func (s *Orm) saveSql(value interface{}) {
columns, values := modelValues(value)
s.Sql = fmt.Sprintf(
"INSERT INTO %v (%v) VALUES (%v)",
s.TableName,
strings.Join(columns, ","),
valuesToBinVar(values),
)
s.SqlVars = values
return
}
func (s *Orm) deleteSql(value interface{}) {
s.Sql = fmt.Sprintf("DELETE FROM %v WHERE %v", s.TableName, s.whereSql)
return
}
func (s *Orm) whereSql() (sql string) {
sql = "1=1"
return
}

View File

@ -1 +1,29 @@
package gorm
import (
"fmt"
"reflect"
"strings"
)
func modelValues(m interface{}) (columns []string, values []interface{}) {
typ := reflect.TypeOf(m).Elem()
for i := 0; i < typ.NumField(); i++ {
p := typ.Field(i)
if !p.Anonymous {
columns = append(columns, strings.ToLower(p.Name))
value := reflect.ValueOf(m).Elem().FieldByName(p.Name)
values = append(values, value.Interface())
}
}
return
}
func valuesToBinVar(values []interface{}) string {
var sqls []string
for index, _ := range values {
sqls = append(sqls, fmt.Sprintf("$%d", index+1))
}
return strings.Join(sqls, ",")
}