Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #6 from danielb2/master

Adds rudimentary count
  • Loading branch information...
commit e9bf14b2ea1b33968dac9f97751e29bd6446202a 2 parents b45da3c + 577f206
Martin Tajur authored

Showing 3 changed files with 33 additions and 1 deletion. Show diff stats Hide diff stats

  1. +19 0 README.md
  2. +13 0 index.js
  3. +1 1  package.json
19 README.md
Source Rendered
@@ -48,6 +48,7 @@ Basic support of MySQL commands
48 48 * LIMIT and OFFSET
49 49 * ORDER BY
50 50 * GROUP BY
  51 + * COUNT
51 52
52 53 Methods
53 54 =======
@@ -204,6 +205,12 @@ Produces and executes a SELECT query.
204 205 db.get('people', function(err, rows, fields) { ... });
205 206 // This would produce: SELECT … FROM people …
206 207
  208 +## .count(tableName, responseCallback)
  209 +Produces and executes a SELECT query with count.
  210 +
  211 + db.get('people', function(err, rows, fields) { ... });
  212 + // This would produce: SELECT count(*) FROM people …
  213 +
207 214 ## .query(sqlQueryString, responseCallback)
208 215 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.
209 216
@@ -289,6 +296,18 @@ SELECT query with custom fields, WHERE, JOIN and LIMIT
289 296 console.log(results);
290 297 });
291 298
  299 +Basic counting
  300 +------------------------------------------------------
  301 +
  302 + db
  303 + .where({
  304 + 'people.name': 'Martin',
  305 + 'songs.title': 'Yesterday'
  306 + })
  307 + .count('people', function(err, results, fields) {
  308 + console.log(results);
  309 + });
  310 +
292 311 SELECT query with custom fields and GROUP BY
293 312 --------------------------------------------
294 313
13 index.js
@@ -198,6 +198,19 @@ exports.Adapter = function(settings) {
198 198 return that;
199 199 };
200 200
  201 + this.count = function(tableName, responseCallback) {
  202 + if (typeof tableName === 'string') {
  203 + var combinedQueryString = 'SELECT COUNT(*) as count FROM ' + escapeFieldName(tableName)
  204 + + buildJoinString()
  205 + + buildDataString(whereClause, ' AND ', 'WHERE');
  206 +
  207 + connection.query(combinedQueryString, function(err, res) { responseCallback(null, res[0]['count'])});
  208 + resetQuery(combinedQueryString);
  209 + }
  210 +
  211 + return that;
  212 + }
  213 +
201 214 this.join = function(tableName, relation, direction) {
202 215 joinClause.push({
203 216 table: tableName,
2  package.json
... ... @@ -1,6 +1,6 @@
1 1 {
2 2 "name" : "mysql-activerecord",
3   - "version": "0.7.2",
  3 + "version": "0.7.3",
4 4 "author": "Martin Tajur <martin@tajur.ee>",
5 5 "description": "MySQL ActiveRecord pattern implementation on top of the mysql module.",
6 6 "homepage": "https://github.com/martintajur/node-mysql-activerecord",

0 comments on commit e9bf14b

Please sign in to comment.
Something went wrong with that request. Please try again.