good morning!!!!

Skip to content
Snippets Groups Projects
Commit 67773dbb authored by José Carlos Nieto's avatar José Carlos Nieto
Browse files

Using reflectiong and adding *All() methods.

parent ba16155d
No related branches found
No related tags found
No related merge requests found
...@@ -36,8 +36,14 @@ type Offset uint ...@@ -36,8 +36,14 @@ type Offset uint
type Set map[string] interface{} type Set map[string] interface{}
type Upsert map[string] interface{} type Upsert map[string] interface{}
type Query interface { type Item map[string] interface {}
type DataSource struct {
Host string
Port int
Database string
User string
Password string
} }
type Database interface { type Database interface {
...@@ -48,24 +54,19 @@ type Database interface { ...@@ -48,24 +54,19 @@ type Database interface {
Collections() []string Collections() []string
} }
type Collection interface { type Collection interface {
Append(...interface{}) bool Append(...interface{}) bool
Find(...interface{}) interface{} Find(...interface{}) Item
FindAll(...interface{}) []interface{} FindAll(...interface{}) []Item
Update(...interface{}) bool Update(...interface{}) bool
UpdateAll(...interface{}) bool UpdateAll(...interface{}) bool
Remove(...interface{}) bool Remove(...interface{}) bool
RemoveAll(...interface{}) bool RemoveAll(...interface{}) bool
}
type DataSource struct { Truncate() bool
Host string
Port int
Database string
User string
Password string
} }
...@@ -28,6 +28,7 @@ import ( ...@@ -28,6 +28,7 @@ import (
"strings" "strings"
"reflect" "reflect"
"launchpad.net/mgo" "launchpad.net/mgo"
"launchpad.net/mgo/bson"
) )
type MongoDB struct { type MongoDB struct {
...@@ -63,15 +64,35 @@ func (w Where) Marshal() map[string] interface{} { ...@@ -63,15 +64,35 @@ func (w Where) Marshal() map[string] interface{} {
return conds return conds
} }
func (c *MongoDBCollection) Truncate() bool {
err := c.collection.DropCollection()
if err == nil {
return false
}
return true
}
func (c *MongoDBCollection) Append(items ...interface {}) bool { func (c *MongoDBCollection) Append(items ...interface {}) bool {
// TODO: use reflection
length := len(items) parent := reflect.TypeOf(c.collection)
for i := 0; i < length; i++ { method, _ := parent.MethodByName("Insert")
err := c.collection.Insert(items[i])
if err != nil { args := make([]reflect.Value, 1 + len(items))
panic(err) args[0] = reflect.ValueOf(c.collection)
itop := len(items)
for i := 0; i < itop; i++ {
args[i + 1] = reflect.ValueOf(items[i])
} }
exec := method.Func.Call(args)
if exec[0].Interface() != nil {
return false
} }
return true return true
} }
...@@ -140,7 +161,7 @@ func (c *MongoDBCollection) RemoveAll(terms ...interface{}) bool { ...@@ -140,7 +161,7 @@ func (c *MongoDBCollection) RemoveAll(terms ...interface{}) bool {
result := c.Invoke("Remove", terms) result := c.Invoke("Remove", terms)
return true return result[0].Bool()
} }
func (c *MongoDBCollection) Remove(terms ...interface{}) bool { func (c *MongoDBCollection) Remove(terms ...interface{}) bool {
...@@ -176,9 +197,7 @@ func (c *MongoDBCollection) UpdateAll(terms ...interface{}) bool { ...@@ -176,9 +197,7 @@ func (c *MongoDBCollection) UpdateAll(terms ...interface{}) bool {
result := c.Invoke("Update", terms) result := c.Invoke("Update", terms)
// TODO: catch response. return result[0].Bool()
return true
} }
func (c *MongoDBCollection) Update(terms ...interface{}) bool { func (c *MongoDBCollection) Update(terms ...interface{}) bool {
...@@ -186,10 +205,12 @@ func (c *MongoDBCollection) Update(terms ...interface{}) bool { ...@@ -186,10 +205,12 @@ func (c *MongoDBCollection) Update(terms ...interface{}) bool {
var set interface{} var set interface{}
var upsert interface{} var upsert interface{}
var modify interface{} var modify interface{}
var multi interface{}
set = nil set = nil
upsert = nil upsert = nil
modify = nil modify = nil
multi = nil
// TODO: make use Multi // TODO: make use Multi
...@@ -210,21 +231,41 @@ func (c *MongoDBCollection) Update(terms ...interface{}) bool { ...@@ -210,21 +231,41 @@ func (c *MongoDBCollection) Update(terms ...interface{}) bool {
case Modify: { case Modify: {
modify = term.(Modify) modify = term.(Modify)
} }
case Multi: {
multi = term.(Multi)
} }
} }
}
if multi != nil {
if set != nil { if set != nil {
c.collection.UpdateAll(query, Tuple { "$set": set }) c.collection.UpdateAll(query, Tuple { "$set": set })
return true return true
} }
if upsert != nil { if modify != nil {
c.collection.Upsert(query, upsert) c.collection.UpdateAll(query, modify)
return true
}
} else {
if set != nil {
c.collection.Update(query, Tuple { "$set": set })
return true return true
} }
if modify != nil { if modify != nil {
c.collection.UpdateAll(query, modify) c.collection.Update(query, modify)
return true
}
}
if upsert != nil {
c.collection.Upsert(query, upsert)
return true return true
} }
...@@ -250,40 +291,26 @@ func (c *MongoDBCollection) Invoke(fn string, terms []interface{}) []reflect.Val ...@@ -250,40 +291,26 @@ func (c *MongoDBCollection) Invoke(fn string, terms []interface{}) []reflect.Val
return exec return exec
} }
func (c *MongoDBCollection) Find(terms ...interface{}) interface{} { func (c *MongoDBCollection) Find(terms ...interface{}) Item {
var result interface{} var item Item
typeOf := reflect.TypeOf(c) terms = append(terms, Limit(1))
method, _ := typeOf.MethodByName("FindAll")
args := make([]reflect.Value, 2 + len(terms)) result := c.Invoke("FindAll", terms)
// First argument is mandatory. if len(result) > 0 {
args[0] = reflect.ValueOf(c) response := result[0].Interface().([]Item)
if len(response) > 0 {
// Forced constraint. item = response[0]
args[1] = reflect.ValueOf(Limit(1))
itop := len(terms)
for i := 0; i < itop; i++ {
args[i+2] = reflect.ValueOf(terms[i])
} }
exec := method.Func.Call(args)
results := exec[0].Interface().([] interface {})
if (len(results) > 0) {
// We expect only the first result.
result = results[0]
} }
return result return item
} }
func (c *MongoDBCollection) FindAll(terms ...interface{}) []interface{} { func (c *MongoDBCollection) FindAll(terms ...interface{}) []Item {
var items []Item
var result []interface {} var result []interface {}
var sort interface {} var sort interface {}
...@@ -331,7 +358,18 @@ func (c *MongoDBCollection) FindAll(terms ...interface{}) []interface{} { ...@@ -331,7 +358,18 @@ func (c *MongoDBCollection) FindAll(terms ...interface{}) []interface{} {
// Retrieving data // Retrieving data
p.All(&result) p.All(&result)
return result itop = len(result)
items = make([]Item, itop)
for i := 0; i < itop; i++ {
item := Item{}
for key, val := range result[i].(bson.M) {
item[key] = val
}
items = append(items, item)
}
return items
} }
func NewMongoDB(config *DataSource) *MongoDB { func NewMongoDB(config *DataSource) *MongoDB {
......
package db package db
import ( import (
. "github.com/xiam/gosexy" //. "github.com/xiam/gosexy"
"testing"
"fmt" "fmt"
"testing"
) )
func TestAll(t *testing.T) { func TestAll(t *testing.T) {
...@@ -22,24 +22,17 @@ func TestAll(t *testing.T) { ...@@ -22,24 +22,17 @@ func TestAll(t *testing.T) {
// Choose collection // Choose collection
col := db.Collection("people") col := db.Collection("people")
// Testing insert all := col.FindAll()
col.Append(Tuple { "Name": "Tucket11", "LastName": "Nancy" })
/*
col.Find()
col.Find( for i := 0; i < len(all); i++ {
Where { "Name": "Tucket" }, for key, val := range all[i] {
) fmt.Printf("key(%v) = (%v)\n", key, val)
}
}
col.Find( /*
Where { "Name": "Tucket" }, // Testing insert
Where { "LastName $ne": "Barr" }, col.Append(Tuple { "Name": "Tucket11", "LastName": "Nancy" })
Limit (2),
Offset (5),
Sort { "Name": -1 },
)
*/
found := col.Find( found := col.Find(
Where { "Name": "Tucket3" }, Where { "Name": "Tucket3" },
...@@ -48,9 +41,10 @@ func TestAll(t *testing.T) { ...@@ -48,9 +41,10 @@ func TestAll(t *testing.T) {
fmt.Printf("Find: %v\n", found) fmt.Printf("Find: %v\n", found)
col.Update(Where {"Name": "Tucket" }, Set { "FooSet": "Bar", "Name": "Tucket3" }) col.Update(Where {"Name": "Tucket" }, Set { "FooSet": "Bar", "Name": "Tucket3" })
col.Update(Where {"Name": "Tucket5" }, Upsert { "Heh": "Bar" }) col.UpdateAll(Where {"Name": "Tucket5" }, Upsert { "Heh": "Bar" })
col.Update(Where {"Name": "Tucket3" }, Modify { "$unset": "FooSet" }) col.Update(Where {"Name": "Tucket3" }, Modify { "$unset": "FooSet" })
col.Remove(Where { "Name": "Tucket" }) col.Remove(Where { "Name": "Tucket" })
col.RemoveAll(Where { "Name": "Tucket" }) col.RemoveAll(Where { "Name": "Tucket" })
*/
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment