From fd67f9bdec191f39d2ec3f1a25f39ef8f8fac9c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Carlos=20Nieto?= <xiam@menteslibres.org> Date: Mon, 9 Jul 2012 10:45:10 -0500 Subject: [PATCH] Pushing more docks, still lacks revisions. --- db/README.md | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++- db/mysql.go | 4 ++-- 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/db/README.md b/db/README.md index 6c19b15d..abe3c677 100644 --- a/db/README.md +++ b/db/README.md @@ -47,7 +47,7 @@ Use your recently configured ``db.Database`` to request the driver to actually c sess.Open() // Don't forget to close the connection when it's not required anymore. - defer sess.Close() + defer sess.Close() ### Database methods. @@ -122,18 +122,79 @@ When you request data from a Collection with ``Find()`` or ``FindAll()``, a spec #### db.Collection.Append(...interface{}) bool +Appends one or more items to the collection. + + collection.Append(Item { "name": "Peter" }) + #### db.Collection.Count(...interface{}) int +Returns the number of total items matching the provided conditions. + + total := collection.Count(Where { "name": "Peter" }) + #### db.Collection.Find(...interface{}) Item +Return the first Item of the collection that matches all the provided conditions. Ordering of the conditions does not matter, but you must take in account that they are evaluated from left to right and from top to bottom. + + // The following statement is equivalent to WHERE name = "John" AND last_name = "Doe" AND (age = 15 OR age = 20) + collection.Find( + Where { "name": "John" }, + Where { "last_name": "Doe" }, + Or { + Where { "age": 15 }, + Where { "age": 20 }, + }, + ) + #### db.Collection.FindAll(...interface{}) []Item +Returns all the Items ([]Item) of the collection that match all the provided conditions. See db.Collection.Find(). + +Be aware that there are some extra parameters that you can pass to FindAll() but not to Find(), like db.Limit(n) or db.Offset(n). + + // Just give me the the first 10 rows with last_name = "Smith" + collection.Find( + Where { "last_name": "Smith" }, + Limit(10), + ) + #### db.Collection.Update(...interface{}) bool +Updates all the items of the collection that match all the provided conditions. You can specify the modification type by using Set, Modify or Upsert. At the time of this writing Modify and Upsert are only available for MongoSession. + + // Example of assigning field values with Set: + collection.Update( + Where { "name": "José" }, + Set { "name": "Joseph"}, + ) + + // Example of custom modification with Modify (for MongoSession): + collection.Update( + Where { "times <": "10" }, + Modify { "$inc": { "times": 1 } }, + ) + + // Example of inserting if none matches with Upsert (for MongoSession): + collection.Update( + Where { "name": "Roberto" }, + Upsert { "name": "Robert"}, + ) + #### db.Collection.Remove(...interface{}) bool +Deletes all the items of the collection that match the provided conditions. + + collection.Remove( + Where { "name": "Peter" }, + Where { "last_name": "Parker" }, + ) + #### db.Collection.Truncate() bool +Deletes the whole collection. + + collection.Truncate() + ## Documentation You can read ``gosexy/db`` documentation from a terminal diff --git a/db/mysql.go b/db/mysql.go index d1b74a02..dfc83867 100644 --- a/db/mysql.go +++ b/db/mysql.go @@ -245,7 +245,7 @@ func (my *MysqlDataSource) Use(database string) error { return nil } -// Drops the currently active database. +// Deletes the currently active database. func (my *MysqlDataSource) Drop() error { my.session.Query(fmt.Sprintf("DROP DATABASE %s", my.config.Database)) return nil @@ -405,7 +405,7 @@ func (t *MysqlTable) Truncate() bool { return false } -// Removes all the rows in the table that match certain conditions. +// Deletes all the rows in the table that match certain conditions. func (t *MysqlTable) Remove(terms ...interface{}) bool { conditions, cargs := t.compileConditions(terms) -- GitLab