good morning!!!!

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

MongoDB: Adding placeholders for group by support. Currently not implemented.

parent 34385a3f
No related branches found
No related tags found
Loading
......@@ -45,6 +45,7 @@ type chunks struct {
Offset int
Sort []string
Conditions interface{}
GroupBy []interface{}
}
func (self *Collection) Find(terms ...interface{}) db.Result {
......
......@@ -24,6 +24,7 @@
package mongo
import (
"math/rand"
"os"
"reflect"
"strings"
......@@ -267,6 +268,68 @@ func TestResultCount(t *testing.T) {
}
func TestGroup(t *testing.T) {
var err error
var sess db.Database
var stats db.Collection
if sess, err = db.Open(Adapter, settings); err != nil {
t.Fatal(err)
}
type stats_t struct {
Numeric int `db:"numeric" bson:"numeric"`
Value int `db:"value" bson:"value"`
}
defer sess.Close()
if stats, err = sess.Collection("stats_test"); err != nil {
if err != db.ErrCollectionDoesNotExist {
t.Fatal(err)
}
}
// Truncating table.
if err == nil {
if err = stats.Truncate(); err != nil {
t.Fatal(err)
}
}
// Adding row append.
for i := 0; i < 1000; i++ {
numeric, value := rand.Intn(10), rand.Intn(100)
if _, err = stats.Append(stats_t{numeric, value}); err != nil {
t.Fatal(err)
}
}
// db.stats_test.group({key: {numeric: true}, initial: {sum: 0}, reduce: function(doc, prev) { prev.sum += 1}});
// Testing GROUP BY
res := stats.Find().Group(bson.M{
"key": bson.M{"numeric": true},
"initial": bson.M{"sum": 0},
"reduce": `function(doc, prev) { prev.sum += 1}`,
})
var results []map[string]interface{}
err = res.All(&results)
// Currently not supported.
if err != db.ErrUnsupported {
t.Fatal(err)
}
//if len(results) != 10 {
// t.Fatalf(`Expecting exactly 10 results, this could fail, but it's very unlikely to happen.`)
//}
}
// This test uses and result and tries to fetch items one by one.
func TestResultFetch(t *testing.T) {
......
......@@ -116,6 +116,13 @@ func (self *Result) All(dst interface{}) error {
return nil
}
// Used to group results that have the same value in the same column or
// columns.
func (self *Result) Group(fields ...interface{}) db.Result {
self.queryChunks.GroupBy = fields
return self
}
// Fetches only one result from the resultset.
func (self *Result) One(dst interface{}) error {
var err error
......@@ -187,6 +194,10 @@ func (self *Result) query() (*mgo.Query, error) {
q := self.c.collection.Find(self.queryChunks.Conditions)
if self.queryChunks.GroupBy != nil {
return nil, db.ErrUnsupported
}
if self.queryChunks.Offset > 0 {
q = q.Skip(self.queryChunks.Offset)
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment