Skip to content

Commit

Permalink
Merge pull request #6 from danielb2/master
Browse files Browse the repository at this point in the history
Adds rudimentary count
  • Loading branch information
martintajur committed Feb 9, 2013
2 parents b45da3c + 577f206 commit e9bf14b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
19 changes: 19 additions & 0 deletions README.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Basic support of MySQL commands
* LIMIT and OFFSET * LIMIT and OFFSET
* ORDER BY * ORDER BY
* GROUP BY * GROUP BY
* COUNT


Methods Methods
======= =======
Expand Down Expand Up @@ -204,6 +205,12 @@ Produces and executes a SELECT query.
db.get('people', function(err, rows, fields) { ... }); db.get('people', function(err, rows, fields) { ... });
// This would produce: SELECT … FROM people … // 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) ## .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. 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.


Expand Down Expand Up @@ -289,6 +296,18 @@ SELECT query with custom fields, WHERE, JOIN and LIMIT
console.log(results); 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 SELECT query with custom fields and GROUP BY
-------------------------------------------- --------------------------------------------


Expand Down
13 changes: 13 additions & 0 deletions index.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -198,6 +198,19 @@ exports.Adapter = function(settings) {
return that; 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) { this.join = function(tableName, relation, direction) {
joinClause.push({ joinClause.push({
table: tableName, table: tableName,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,6 @@
{ {
"name" : "mysql-activerecord", "name" : "mysql-activerecord",
"version": "0.7.2", "version": "0.7.3",
"author": "Martin Tajur <martin@tajur.ee>", "author": "Martin Tajur <martin@tajur.ee>",
"description": "MySQL ActiveRecord pattern implementation on top of the mysql module.", "description": "MySQL ActiveRecord pattern implementation on top of the mysql module.",
"homepage": "https://github.com/martintajur/node-mysql-activerecord", "homepage": "https://github.com/martintajur/node-mysql-activerecord",
Expand Down

0 comments on commit e9bf14b

Please sign in to comment.