Skip to content

Commit

Permalink
Adding All
Browse files Browse the repository at this point in the history
  • Loading branch information
zetaben committed Mar 27, 2010
1 parent e2bb9fd commit 2592dda
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 43 deletions.
51 changes: 31 additions & 20 deletions Model.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package gouda

import (
"reflect"
// "fmt"
// "fmt"
"strings"
)
/** Types **/
Expand All @@ -13,9 +13,9 @@ type ModelInterface interface {

type Model struct {
tablename string
identifier string
identifier string
attributes map[string]reflect.Type
runtype reflect.Type
runtype reflect.Type
connection *Connection
}

Expand All @@ -25,24 +25,24 @@ var _ModelStore = make(ModelStore)

/** NullModel **/

type NullModel struct {}
type NullModel struct{}

func (n NullModel) TableName() string {return "NilTable create a TableName"}
func (n NullModel) TableName() string { return "NilTable create a TableName" }

func (n NullModel) Identifier() string {return "Id"}
func (n NullModel) Identifier() string { return "Id" }


/** utils **/

func attributes(m interface{}) (map[string]reflect.Type,reflect.Type) {
func attributes(m interface{}) (map[string]reflect.Type, reflect.Type) {
var st *reflect.StructType
var typ reflect.Type
if _, ok := reflect.Typeof(m).(*reflect.PtrType); ok {
typ = reflect.Typeof(m).(*reflect.PtrType).Elem()
} else {
typ =reflect.Typeof(m);
typ = reflect.Typeof(m)
}
st = typ.(*reflect.StructType)
st = typ.(*reflect.StructType)

//fmt.Println(st.NumField())

Expand All @@ -56,7 +56,7 @@ func attributes(m interface{}) (map[string]reflect.Type,reflect.Type) {
}
}

return ret,typ
return ret, typ
}

/** Model **/
Expand All @@ -78,30 +78,40 @@ func (m *Model) AttributesNames() (ret []string) {
}

func (m *Model) Last() interface{} {
q := NewRelation(m.tablename).Order(strings.ToLower(m.identifier),"desc").First()
q := NewRelation(m.tablename).Order(strings.ToLower(m.identifier), "desc").First()
ret := m.connection.Query(q)
v := ret.At(0).(map[string]Value)
return m.translateObject(v);
return m.translateObject(v)
}

func (m *Model) First() interface{} {
q := NewRelation(m.tablename).First()
ret := m.connection.Query(q)
v := ret.At(0).(map[string]Value)
return m.translateObject(v);
return m.translateObject(v)
}

func (m * Model) translateObject(v map[string]Value) interface{} {
p:=reflect.MakeZero(m.runtype).(*reflect.StructValue)
func (m *Model) All() []interface{} {
q := NewRelation(m.tablename)
ret := m.connection.Query(q)
v := make([]interface{}, ret.Len())
for i := 0; i < ret.Len(); i++ {
v[i] = m.translateObject(ret.At(i).(map[string]Value))
}
return v
}

func (m *Model) translateObject(v map[string]Value) interface{} {
p := reflect.MakeZero(m.runtype).(*reflect.StructValue)
for lbl, _ := range m.Attributes() {
vl := v[strings.ToLower(lbl)]
switch vl.Kind() {
case IntKind:
tmp:=reflect.NewValue(1).(*reflect.IntValue)
tmp := reflect.NewValue(1).(*reflect.IntValue)
tmp.Set(int(vl.Int()))
p.FieldByName(lbl).SetValue(tmp)
case StringKind:
tmp:=reflect.NewValue("").(*reflect.StringValue)
tmp := reflect.NewValue("").(*reflect.StringValue)
tmp.Set(string(vl.String()))
p.FieldByName(lbl).SetValue(tmp)
}
Expand Down Expand Up @@ -135,14 +145,15 @@ func GetModelStore() *ModelStore { return &_ModelStore }
func (st *ModelStore) RegisterModel(m ModelInterface) *Model {
return st.RegisterModelWithConnection(m, GetConnectionStore().Last())
}

func (st *ModelStore) RegisterModelWithConnection(m ModelInterface, conn *Connection) *Model {
modelname := ModelName(m)
mod := new(Model)
mod.tablename = m.TableName()
mod.identifier = m.Identifier()
attr,run :=attributes(m)
mod.attributes = attr
mod.runtype = run
attr, run := attributes(m)
mod.attributes = attr
mod.runtype = run
mod.connection = conn
(*st)[modelname] = mod
return mod
Expand Down
51 changes: 28 additions & 23 deletions Model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,44 +83,49 @@ func TestModelFetch(t *testing.T) {
need_connection()
p := new(Personne)
z := gouda.M(p).First().(Personne)
// fmt.Println(z.Id)
// fmt.Println(z)
if(z.Id!=1){
t.Error("wrong personne found : Id : " +fmt.Sprint(z.Id))
// fmt.Println(z.Id)
// fmt.Println(z)
if z.Id != 1 {
t.Error("wrong personne found : Id : " + fmt.Sprint(z.Id))
}

z = gouda.M(p).Last().(Personne)
// fmt.Println(z.Id)
// fmt.Println(z)
if(z.Id!=2){
t.Error("wrong personne found : Id : " +fmt.Sprint(z.Id))
// fmt.Println(z.Id)
// fmt.Println(z)
if z.Id != 2 {
t.Error("wrong personne found : Id : " + fmt.Sprint(z.Id))
}

tab := gouda.M(p).All()
if len(tab) != 2 {
t.Error("Wrong Fetched Size")
}
}

func need_connection() {
if(!conn_ok){
init_mysql()
conn_ok=true
if !conn_ok {
init_mysql()
conn_ok = true
}

}


func init_mysql() {
r,w,err := os.Pipe()
if err !=nil {
panic("%v",err)
}
r, w, err := os.Pipe()
if err != nil {
panic("%v", err)
}

fmt.Print("Initializing DB... ")
pid,_:=os.ForkExec("/usr/bin/mysql", []string{"/usr/bin/mysql", "test_db"},os.Environ(),"/versatile",[]*os.File{r, os.Stdout, os.Stderr})
// fmt.Fprintln(w,"show tables;");
fmt.Fprintln(w,"DROP TABLE personne;");
fmt.Fprintln(w,"CREATE TABLE `personne` ( `id` int(11) NOT NULL, `nom` varchar(255) default NULL, PRIMARY KEY (`id`) );");
fmt.Fprintln(w,"INSERT INTO `personne` VALUES (1,'toto');");
fmt.Fprintln(w,"INSERT INTO `personne` VALUES (2,'titi');");
w.Close();
os.Wait(pid,0)
pid, _ := os.ForkExec("/usr/bin/mysql", []string{"/usr/bin/mysql", "test_db"}, os.Environ(), "/versatile", []*os.File{r, os.Stdout, os.Stderr})
// fmt.Fprintln(w,"show tables;");
fmt.Fprintln(w, "DROP TABLE personne;")
fmt.Fprintln(w, "CREATE TABLE `personne` ( `id` int(11) NOT NULL, `nom` varchar(255) default NULL, PRIMARY KEY (`id`) );")
fmt.Fprintln(w, "INSERT INTO `personne` VALUES (1,'toto');")
fmt.Fprintln(w, "INSERT INTO `personne` VALUES (2,'titi');")
w.Close()
os.Wait(pid, 0)
fmt.Println("Finished!")

conn := gouda.OpenMysql("mysql://root:@localhost:3306/test_db")
Expand Down

0 comments on commit 2592dda

Please sign in to comment.