Permalink
Browse files

Merge pull request #6 from danielb2/master

Adds rudimentary count
  • Loading branch information...
2 parents b45da3c + 577f206 commit e9bf14b2ea1b33968dac9f97751e29bd6446202a @martintajur committed Feb 9, 2013
Showing with 33 additions and 1 deletion.
  1. +19 −0 README.md
  2. +13 −0 index.js
  3. +1 −1 package.json
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
--------------------------------------------
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,
View
@@ -1,6 +1,6 @@
{
"name" : "mysql-activerecord",
- "version": "0.7.2",
+ "version": "0.7.3",
"author": "Martin Tajur <martin@tajur.ee>",
"description": "MySQL ActiveRecord pattern implementation on top of the mysql module.",
"homepage": "https://github.com/martintajur/node-mysql-activerecord",

0 comments on commit e9bf14b

Please sign in to comment.