Permalink
Browse files

merged in pull requests, version bump, added .gitignore

  • Loading branch information...
2 parents 42bfd5a + e9bf14b commit 36429a29d5ea7a08d170a5e3a0ffc79ab8dcd852 @martintajur committed Feb 11, 2013
Showing with 36 additions and 1 deletion.
  1. +3 −0 .gitignore
  2. +20 −1 README.md
  3. +13 −0 index.js
View
@@ -0,0 +1,3 @@
+.DS_Store
+node_modules
+npm-debug*
View
@@ -48,6 +48,7 @@ Basic support of MySQL commands
* LIMIT and OFFSET
* ORDER BY
* GROUP BY
+ * COUNT
Methods
=======
@@ -204,6 +205,12 @@ Produces and executes a SELECT query.
db.get('people', function(err, rows, fields) { ... });
// This would produce: SELECT … FROM people …
+## .count(tableName, responseCallback)
+Produces and executes a SELECT query with count.
+
+ db.get('people', function(err, rows, fields) { ... });
+ // This would produce: SELECT count(*) FROM people …
+
## .query(sqlQueryString, responseCallback)
Produces and executes a raw query. Note that while no set query conditions will be used in this query, they will all be reset nevertheless with the execution.
@@ -289,6 +296,18 @@ SELECT query with custom fields, WHERE, JOIN and LIMIT
console.log(results);
});
+Basic counting
+------------------------------------------------------
+
+ db
+ .where({
+ 'people.name': 'Martin',
+ 'songs.title': 'Yesterday'
+ })
+ .count('people', function(err, results, fields) {
+ console.log(results);
+ });
+
SELECT query with custom fields and GROUP BY
--------------------------------------------
@@ -336,7 +355,7 @@ Advanced WHERE conditions
.where("date_created > '2012-03-10'")
.where({ owner_id: 32 })
.delete('records', function(err) {
- if (err) {
+ if (!err) {
console.log('Deleted!')
}
});
View
@@ -198,6 +198,19 @@ exports.Adapter = function(settings) {
return that;
};
+ this.count = function(tableName, responseCallback) {
+ if (typeof tableName === 'string') {
+ var combinedQueryString = 'SELECT COUNT(*) as count FROM ' + escapeFieldName(tableName)
+ + buildJoinString()
+ + buildDataString(whereClause, ' AND ', 'WHERE');
+
+ connection.query(combinedQueryString, function(err, res) { responseCallback(null, res[0]['count'])});
+ resetQuery(combinedQueryString);
+ }
+
+ return that;
+ }
+
this.join = function(tableName, relation, direction) {
joinClause.push({
table: tableName,

0 comments on commit 36429a2

Please sign in to comment.