diff --git a/README.md b/README.md index 85da605b3d35057d8b906a251614066c1723638c..fcf57fdc096f8abfddd402790f3b4b8af3429ca9 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,14 @@ We are going to use the [mysql](http://gosexy.org/db/wrappers/mysql) driver in o (such as ``mongo``) just replace ``mysql`` with the name of your driver and everything should work the same. ```go -sess := mysql.Session(db.DataSource{Host: "localhost", Database: "test", User: "myuser", Password: "mypass"}) +sess := mysql.Session( + db.DataSource{ + Host: "localhost", + Database: "test", + User: "myuser", + Password: "mypass", + }, +) ``` The ``db.DataSource`` is a generic structure than can store connection values for any database in a consistent way. @@ -61,7 +68,15 @@ Use your recently configured ``db.Database`` to request the driver to actually c ```go // Setting up database. -sess := mysql.Session(db.DataSource{Host: "localhost", Database: "test", User: "myuser", Password: "mypass"}) +sess := mysql.Session( + db.DataSource{ + Host: "localhost", + Database: "test", + User: "myuser", + Password: "mypass", + }, +) + err := sess.Open() // Don't forget to close the connection when it's not required anymore. diff --git a/mongo/mongo.go b/mongo/mongo.go index a2313c4ffacd140281d3c0e22483428f64854bbd..58b37538f1588df6b9769b987db25800290e04e0 100644 --- a/mongo/mongo.go +++ b/mongo/mongo.go @@ -54,8 +54,7 @@ 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]: toInternal(val)} diff --git a/mongo/mongo_test.go b/mongo/mongo_test.go index 7e227e7c9a413867552eb1741a479382d3cba352..a8d5a116fe68eab80a77ec5de7ec3c380a29958a 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/mysql/mysql.go b/mysql/mysql.go index f4c946af3fd1ef34081fdb41e4cc68a03dbb14e5..8fc16c1751cfcb32baebade0bd8dda162c91ccf5 100644 --- a/mysql/mysql.go +++ b/mysql/mysql.go @@ -395,22 +395,25 @@ func (t *MysqlTable) compileConditions(term interface{}) (string, db.SqlArgs) { // Converts db.Cond{} structures into SQL before processing them in a query. func (t *MysqlTable) marshal(where db.Cond) (string, []string) { + var placeholder string - for key, val := range where { - key = strings.Trim(key, " ") - chunks := strings.Split(key, " ") + 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. @@ -739,10 +742,13 @@ func (my *MysqlDataSource) Collection(name string) db.Collection { // Fetching table datatypes and mapping to internal gotypes. - rows, _ := t.parent.myExec( + rows, err := t.parent.myExec( "Query", "SHOW COLUMNS FROM", t.name, ) + if err != nil { + panic(err) + } columns := t.myFetchAll(rows) diff --git a/mysql/mysql_test.go b/mysql/mysql_test.go index 8c8fa63ac4d19e6b1dbde1c2458b73d79f4037e2..e972e92e30e59967dcd0deb652bc83b57fca8862 100644 --- a/mysql/mysql_test.go +++ b/mysql/mysql_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 3762922877e0357dc79b622491e72087287dfafa..9635c589856c8652e6d9fd2697ea41c4cecd474b 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 c31c5a8813efad233a4d348906d0f47c389deecb..7723419f803b22855c6b2a6e5f4609332c0e7484 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 Binary files a/sqlite/dumps/gotest.sqlite3.db and b/sqlite/dumps/gotest.sqlite3.db differ diff --git a/sqlite/sqlite_test.go b/sqlite/sqlite_test.go index 30f7d6dacd37cde7b2ae4c6c4832308d79c1587d..8d31aaf3ef6ef771fdfc1cba7e0b5eb61a7ce257 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()