good morning!!!!

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

Return last id.

parent db9f10a0
Branches
Tags
No related merge requests found
...@@ -190,7 +190,7 @@ type Database interface { ...@@ -190,7 +190,7 @@ type Database interface {
// Collection methods. // Collection methods.
type Collection interface { type Collection interface {
Append(...interface{}) error Append(...interface{}) ([]Id, error)
Count(...interface{}) (int, error) Count(...interface{}) (int, error)
......
...@@ -80,7 +80,11 @@ func (c *MongoDataSourceCollection) Truncate() error { ...@@ -80,7 +80,11 @@ func (c *MongoDataSourceCollection) Truncate() error {
} }
// Appends an item to the collection. // Appends an item to the collection.
func (c *MongoDataSourceCollection) Append(items ...interface{}) error { func (c *MongoDataSourceCollection) Append(items ...interface{}) ([]db.Id, error) {
var err error
ids := []db.Id{}
parent := reflect.TypeOf(c.collection) parent := reflect.TypeOf(c.collection)
method, _ := parent.MethodByName("Insert") method, _ := parent.MethodByName("Insert")
...@@ -91,16 +95,26 @@ func (c *MongoDataSourceCollection) Append(items ...interface{}) error { ...@@ -91,16 +95,26 @@ func (c *MongoDataSourceCollection) Append(items ...interface{}) error {
itop := len(items) itop := len(items)
for i := 0; i < itop; i++ { for i := 0; i < itop; i++ {
id := db.Id(bson.NewObjectId().Hex())
switch items[i].(type) {
case map[string] interface{}:
if items[i].(map[string]interface{})["_id"] == nil {
items[i].(map[string]interface{})["_id"] = id
}
}
args[i+1] = reflect.ValueOf(toInternal(items[i])) args[i+1] = reflect.ValueOf(toInternal(items[i]))
ids = append(ids, id)
} }
exec := method.Func.Call(args) exec := method.Func.Call(args)
if exec[0].Interface() != nil { if exec[0].Interface() != nil {
return exec[0].Interface().(error) err = exec[0].Interface().(error)
} }
return nil return ids, err
} }
// Compiles terms into something *mgo.Session can understand. // Compiles terms into something *mgo.Session can understand.
......
...@@ -310,7 +310,7 @@ func TestDataTypes(t *testing.T) { ...@@ -310,7 +310,7 @@ func TestDataTypes(t *testing.T) {
data := getTestData() data := getTestData()
err = col.Append(data) _, err = col.Append(data)
if err != nil { if err != nil {
t.Errorf("Could not append test data.") t.Errorf("Could not append test data.")
...@@ -320,7 +320,7 @@ func TestDataTypes(t *testing.T) { ...@@ -320,7 +320,7 @@ func TestDataTypes(t *testing.T) {
item := col.Find() item := col.Find()
err = col.Append(item) _, err = col.Append(item)
if err == nil { if err == nil {
t.Errorf("Expecting duplicated-key error.") t.Errorf("Expecting duplicated-key error.")
...@@ -328,12 +328,14 @@ func TestDataTypes(t *testing.T) { ...@@ -328,12 +328,14 @@ func TestDataTypes(t *testing.T) {
delete(item, "_id") delete(item, "_id")
err = col.Append(item) ids, err := col.Append(item)
if err != nil { if err != nil {
t.Errorf("Could not append second element.") t.Errorf("Could not append second element.")
} }
fmt.Printf("%v\n", ids)
// Testing rows // Testing rows
items := col.FindAll() items := col.FindAll()
......
...@@ -671,7 +671,7 @@ func (t *MysqlTable) Find(terms ...interface{}) db.Item { ...@@ -671,7 +671,7 @@ func (t *MysqlTable) Find(terms ...interface{}) db.Item {
} }
// Inserts rows into the currently active table. // Inserts rows into the currently active table.
func (t *MysqlTable) Append(items ...interface{}) error { func (t *MysqlTable) Append(items ...interface{}) ([]db.Id, error) {
itop := len(items) itop := len(items)
...@@ -684,7 +684,6 @@ func (t *MysqlTable) Append(items ...interface{}) error { ...@@ -684,7 +684,6 @@ func (t *MysqlTable) Append(items ...interface{}) error {
for field, value := range item.(db.Item) { for field, value := range item.(db.Item) {
fields = append(fields, field) fields = append(fields, field)
//values = append(values, fmt.Sprintf("%v", value))
values = append(values, toInternal(value)) values = append(values, toInternal(value))
} }
......
...@@ -310,7 +310,7 @@ func TestDataTypes(t *testing.T) { ...@@ -310,7 +310,7 @@ func TestDataTypes(t *testing.T) {
data := getTestData() data := getTestData()
err = col.Append(data) _, err = col.Append(data)
if err != nil { if err != nil {
t.Errorf("Could not append test data.") t.Errorf("Could not append test data.")
...@@ -319,7 +319,7 @@ func TestDataTypes(t *testing.T) { ...@@ -319,7 +319,7 @@ func TestDataTypes(t *testing.T) {
// Getting and reinserting. // Getting and reinserting.
item := col.Find() item := col.Find()
err = col.Append(item) _, err = col.Append(item)
if err == nil { if err == nil {
t.Errorf("Expecting duplicated-key error.") t.Errorf("Expecting duplicated-key error.")
...@@ -327,7 +327,7 @@ func TestDataTypes(t *testing.T) { ...@@ -327,7 +327,7 @@ func TestDataTypes(t *testing.T) {
delete(item, "id") delete(item, "id")
err = col.Append(item) _, err = col.Append(item)
if err != nil { if err != nil {
t.Errorf("Could not append second element.") t.Errorf("Could not append second element.")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment