From 21faa1aca9cdd29274fce0aef24402b237f77451 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Carlos=20Nieto?= <xiam@menteslibres.org> Date: Wed, 26 Sep 2012 19:52:18 -0500 Subject: [PATCH] db.Cond{} accepts more that one relation. --- mongo/mongo.go | 5 ++--- mongo/mongo_test.go | 8 +++++++- postgresql/postgresql.go | 18 +++++++++++------- postgresql/postgresql_test.go | 10 ++++++++-- sqlite/dumps/gotest.sqlite3.db | Bin 6144 -> 6144 bytes sqlite/sqlite_test.go | 8 +++++++- 6 files changed, 35 insertions(+), 14 deletions(-) diff --git a/mongo/mongo.go b/mongo/mongo.go index af185b63..58b37538 100644 --- a/mongo/mongo.go +++ b/mongo/mongo.go @@ -54,11 +54,10 @@ func (c *MongoDataSourceCollection) marshal(where db.Cond) map[string]interface{ conds := make(map[string]interface{}) for key, val := range where { - key = strings.Trim(key, " ") - chunks := strings.Split(key, " ") + chunks := strings.Split(strings.Trim(key, " "), " ") if len(chunks) >= 2 { - conds[chunks[0]] = map[string]interface{}{chunks[1]: val} + conds[chunks[0]] = map[string]interface{}{chunks[1]: toInternal(val)} } else { conds[key] = toInternal(val) } diff --git a/mongo/mongo_test.go b/mongo/mongo_test.go index 7e227e7c..a8d5a116 100644 --- a/mongo/mongo_test.go +++ b/mongo/mongo_test.go @@ -310,12 +310,18 @@ func TestDataTypes(t *testing.T) { data := getTestData() - _, err = col.Append(data) + ids, err := col.Append(data) if err != nil { t.Errorf("Could not append test data.") } + found, _ := col.Count(db.Cond{"_id": db.Id(ids[0])}) + + if found == 0 { + t.Errorf("Cannot find recently inserted item (by ID).") + } + // Getting and reinserting. item := col.Find() diff --git a/postgresql/postgresql.go b/postgresql/postgresql.go index 37629228..9635c589 100644 --- a/postgresql/postgresql.go +++ b/postgresql/postgresql.go @@ -362,21 +362,25 @@ func (t *PostgresqlTable) compileConditions(term interface{}) (string, db.SqlArg func (t *PostgresqlTable) marshal(where db.Cond) (string, []string) { - for key, val := range where { - key = strings.Trim(key, " ") - chunks := strings.Split(key, " ") + var placeholder string + + placeholders := []string{} + args := []string{} - strval := fmt.Sprintf("%v", val) + for key, val := range where { + chunks := strings.Split(strings.Trim(key, " "), " ") if len(chunks) >= 2 { - return fmt.Sprintf("%s %s ?", chunks[0], chunks[1]), []string{strval} + placeholder = fmt.Sprintf("%s %s ?", chunks[0], chunks[1]) } else { - return fmt.Sprintf("%s = ?", chunks[0]), []string{strval} + placeholder = fmt.Sprintf("%s = ?", chunks[0]) } + placeholders = append(placeholders, placeholder) + args = append(args, fmt.Sprintf("%v", val)) } - return "", []string{} + return strings.Join(placeholders, " AND "), args } // Deletes all the rows in the table. diff --git a/postgresql/postgresql_test.go b/postgresql/postgresql_test.go index c31c5a88..7723419f 100644 --- a/postgresql/postgresql_test.go +++ b/postgresql/postgresql_test.go @@ -289,10 +289,16 @@ func TestDataTypes(t *testing.T) { data := getTestData() - _, err = col.Append(data) + ids, err := col.Append(data) if err != nil { - panic(err) + t.Errorf("Could not append test data.") + } + + found, _ := col.Count(db.Cond{"id": db.Id(ids[0])}) + + if found == 0 { + t.Errorf("Cannot find recently inserted item (by ID).") } // Getting and reinserting. diff --git a/sqlite/dumps/gotest.sqlite3.db b/sqlite/dumps/gotest.sqlite3.db index 4e322c01b7cb8fe1f7c0a05faaa3f874a3953882..7ad4aa2060823a1f618ddd8fa972772562e5397e 100644 GIT binary patch delta 19 ZcmZoLXfT){#l+D(QN{^KZcJDp4gfhQ1>67t delta 19 ZcmZoLXfT){#l-%2qKp%e+?cRH8~{HS2ATi> diff --git a/sqlite/sqlite_test.go b/sqlite/sqlite_test.go index 30f7d6da..8d31aaf3 100644 --- a/sqlite/sqlite_test.go +++ b/sqlite/sqlite_test.go @@ -284,12 +284,18 @@ func TestDataTypes(t *testing.T) { data := getTestData() - _, err = col.Append(data) + ids, err := col.Append(data) if err != nil { t.Errorf("Could not append test data.") } + found, _ := col.Count(db.Cond{"id": db.Id(ids[0])}) + + if found == 0 { + t.Errorf("Cannot find recently inserted item (by ID).") + } + // Getting and reinserting. item := col.Find() -- GitLab