Skip to content

Commit

Permalink
ModelRelation to query from model
Browse files Browse the repository at this point in the history
  • Loading branch information
zetaben committed Mar 28, 2010
1 parent 2592dda commit 2867762
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 3 deletions.
60 changes: 59 additions & 1 deletion 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 @@ -19,6 +19,11 @@ type Model struct {
connection *Connection
}

type ModelRelation struct {
model *Model
relation *Relation
}

type ModelStore map[string]*Model

var _ModelStore = make(ModelStore)
Expand Down Expand Up @@ -158,3 +163,56 @@ func (st *ModelStore) RegisterModelWithConnection(m ModelInterface, conn *Connec
(*st)[modelname] = mod
return mod
}

/** Model RelationLike methods**/

func (m *Model) newRelation() *ModelRelation{
mr:=new(ModelRelation)
mr.model=m
mr.relation=new(Relation)
mr.relation.Table(m.tablename)
return mr
}

func (m *Model) Where(x string) *ModelRelation{
return m.newRelation().Where(x)
}

func (m *Model) Order(x,y string) *ModelRelation{
return m.newRelation().Order(x,y)
}

/** ModelRelation **/

func (r *ModelRelation) Where(x string) *ModelRelation {
r.relation.Where(x)
return r
}

func (r *ModelRelation) Order(x,y string) *ModelRelation {
r.relation.Order(x,y)
return r
}

func (r *ModelRelation) First() interface{} {
q:=r.relation.First()
ret := r.model.connection.Query(q)
v := ret.At(0).(map[string]Value)
return r.model.translateObject(v)
}

func (r *ModelRelation) Last() interface{} {
q:=r.relation.Order(r.model.identifier,"DESC").First()
ret := r.model.connection.Query(q)
v := ret.At(0).(map[string]Value)
return r.model.translateObject(v)
}

func (r *ModelRelation) All() []interface{} {
ret := r.model.connection.Query(r.relation)
v := make([]interface{}, ret.Len())
for i := 0; i < ret.Len(); i++ {
v[i] = r.model.translateObject(ret.At(i).(map[string]Value))
}
return v
}
45 changes: 45 additions & 0 deletions Model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,51 @@ func TestModelFetch(t *testing.T) {
}
}

func TestModelRelationFetch(t *testing.T) {
var p Personne;
Personnes:=gouda.M(p)
toto:=Personnes.Where("nom = 'toto'").First().(Personne)
if toto.Nom != "toto" || toto.Id != 1 {
t.Error("Not Found toto")
}
toto=Personnes.Where("nom = 'toto'").Last().(Personne)
if toto.Nom != "toto" || toto.Id != 1 {
t.Error("Not Found toto")
}

totos:=Personnes.Where("nom = 'toto'").All()

if len(totos) != 1 {
t.Error("Wrong Fetched Size, fetched :"+fmt.Sprint(len(totos)))
}
toto=totos[0].(Personne)
if toto.Nom != "toto" || toto.Id != 1 {
t.Error("Not Found toto")
}
}

func TestModelRelationFetchOrder(t *testing.T) {
var p Personne;
Personnes:=gouda.M(p);
need_connection();
totos:=Personnes.Order("nom","asc").All()

if len(totos) != 2 {
t.Error("Wrong Fetched Size, fetched :"+fmt.Sprint(len(totos)))
}
i:=0

if totos[i].(Personne).Nom!="titi" {
t.Error("Not Found titi "+fmt.Sprint(totos[i].(Personne)))
}

i++

if totos[i].(Personne).Nom!="toto" {
t.Error("Not Found toto "+fmt.Sprint(totos[i].(Personne)))
}
}

func need_connection() {
if !conn_ok {
init_mysql()
Expand Down
4 changes: 2 additions & 2 deletions MysqlConnection.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func (e *MysqlConnector) Query(r *Relation) *vector.Vector {

// fmt.Println(len(res.ResultSet.Rows))
ret :=new(vector.Vector)
tmp := make(map[string]Value)
for rowmap := res.FetchRowMap(); rowmap != nil; rowmap = res.FetchRowMap() {
tmp := make(map[string]Value)
// fmt.Printf("%#v\n", rowmap)
// fmt.Printf("%#v\n", res.ResultSet.Fields)
for i := 0; i < len(rowmap); i++ {
Expand Down Expand Up @@ -107,7 +107,7 @@ func mysql_query(r * Relation) (sql string) {
if r.limit_count > 0 {
sql+=" LIMIT "+fmt.Sprint(r.limit_offset)+", "+fmt.Sprint(r.limit_count)
}
// fmt.Println(sql)
fmt.Println(sql)
sql +=";"
return
}

0 comments on commit 2867762

Please sign in to comment.