Permalink
Browse files

Added a parameter for a query's cbComplete callback which contains th…

…e total number of rows the query returned.
  • Loading branch information...
1 parent 9e95983 commit 29e9200ae46016e5dc4d389b6959243aab32d3b5 @mscdex committed May 4, 2010
Showing with 19 additions and 14 deletions.
  1. +7 −6 README.md
  2. +8 −6 example.js
  3. +4 −2 node-poormansmysql.js
View
13 README.md
@@ -33,15 +33,16 @@ MysqlConnection Methods:
* **Parameters**:
* _config_: (Object) An object containing the necessary connection information to connect to the MySQL server and perform queries. At least the user and password should be supplied. See above for the default configuration.
* **Return Value**: None
-* **query**(_sql_ [, _cbRow_, _cbComplete_, _cbError_])
+* **query**(_sql_ [, _cbRow_[, _cbComplete_[, _cbError_]]])
* **Description**: Enqueues an SQL statement.
* **Parameters**:
* _sql_: (String) A valid SQL statement. Do not include a trailing semicolon at the end of the statement.
* _cbRow_ (Optional): (Function) A callback for when a row resulted from the query.
* **Parameters**:
* _row_: (Object) A hash containing a single row from the results of the query.
* _cbComplete_ (Optional): (Function) A callback for when the query has completed and all rows have been returned.
- * **Parameters**: None
+ * **Parameters**:
+ * _totalRows_: (Integer) The total number of rows returned by the query.
* _cbError_ (Optional): (Function) A callback for when the query results in a MySQL error.
* **Parameters**:
* _err_: (String) The entire MySQL error message.
@@ -92,8 +93,8 @@ Utility methods exposed:
function(row) {
sys.puts("Got result for first query: " + JSON.stringify(row));
},
- function() {
- sys.puts("First query is done!");
+ function(totalRows) {
+ sys.puts("First query is done! Retrieved " + totalRows + " row(s)");
},
function(err) {
sys.puts("First query resulted in error: " + err);
@@ -103,8 +104,8 @@ Utility methods exposed:
function(row) {
sys.puts("Got result for second query: " + JSON.stringify(row));
},
- function() {
- sys.puts("Second query is done!");
+ function(totalRows) {
+ sys.puts("Second query is done! Retrieved " + totalRows + " row(s)");
},
function(err) {
sys.puts("Second query resulted in error: " + err);
View
14 example.js
@@ -1,6 +1,8 @@
var pmm = require('./node-poormansmysql'), sys = require('sys');
+var fieldvalue = "o'reilly";
var conn = new pmm.MysqlConnection({user: 'foo', password: 'bar', db: 'baz'});
+
conn.addListener('error', function(err) {
sys.puts('Uh oh, ' + err);
});
@@ -9,22 +11,22 @@ conn.query("SELECT * FROM table",
function(row) {
sys.puts("Got result for first query: " + JSON.stringify(row));
},
- function() {
- sys.puts("First query is done!");
+ function(totalRows) {
+ sys.puts("First query is done! Retrieved " + totalRows + " row(s)");
},
function(err) {
sys.puts("First query resulted in error: " + err);
}
);
-conn.query("SELECT * FROM table2",
+conn.query("SELECT * FROM table2 WHERE field = '" + pmm.escape(fieldvalue) + "'",
function(row) {
sys.puts("Got result for second query: " + JSON.stringify(row));
},
- function() {
- sys.puts("Second query is done!");
+ function(totalRows) {
+ sys.puts("Second query is done! Retrieved " + totalRows + " row(s)");
},
function(err) {
sys.puts("Second query resulted in error: " + err);
}
);
-conn.execute();
+conn.execute();
View
6 node-poormansmysql.js
@@ -56,6 +56,7 @@ function MysqlConnection(newconfig) {
if (sql.substr(0, 6).toUpperCase() == "INSERT")
isInserting = true;
queries.push({query: sql,
+ numRows: 0,
hasError: false,
cbRow: (arguments.length >= 2 && typeof arguments[1] == 'function' ? arguments[1] : null),
cbComplete: (arguments.length >= 3 && typeof arguments[2] == 'function' ? arguments[2] : null),
@@ -135,7 +136,7 @@ function MysqlConnection(newconfig) {
// doesn't give any output for successful non-select queries in batch mode.
for (var i = 0, len = queries.length; i < len; i++) {
if (!queries[i].hasError && !(/^select/i.test(queries[i].query)) && queries[i].cbComplete)
- queries[i].cbComplete();
+ queries[i].cbComplete(queries[i].numRows);
else if (queries[i].hasError && !foundError)
foundError = true;
}
@@ -180,11 +181,12 @@ function MysqlConnection(newconfig) {
cb.onEndElementNS(function(elem, prefix, uri) {
if (elem == "resultset") {
if (queries[curQueryIdx].cbComplete)
- queries[curQueryIdx].cbComplete();
+ queries[curQueryIdx].cbComplete(queries[curQueryIdx].numRows);
curQueryIdx++;
} else if (elem == "row") {
if (queries[curQueryIdx].cbRow)
queries[curQueryIdx].cbRow(curRow);
+ queries[curQueryIdx].numRows++;
curRow = null;
} else if (elem == "field")
curField = null;

0 comments on commit 29e9200

Please sign in to comment.