Permalink
Browse files

Add MysqlResult::NumRows method, rename MysqlResult::FetchResult to F…

…etchAll
  • Loading branch information...
1 parent 23b092c commit 6412df877b60ef2f557117f8a31a3fed9f741196 @Sannis committed Apr 4, 2010
View
@@ -130,8 +130,8 @@ doBenchmark("Selects(" + selects_count + ", " + rows_to_select + ")", function (
i;
for (i = 0; i < selects_count; i += 1) {
- res = conn.query("SELECT * FROM " + test_table + " ORDER BY RAND() LIMIT " + limits[i] + ", " + rows_to_select + ";");
- rows[i] = res.fetchResult();
+ res = conn.query("SELECT * FROM " + test_table + " ORDER BY RAND() LIMIT " + limits[i] + ", " + rows_to_select + ";", 1);
+ rows[i] = res.fetchAll();
}
});
@@ -7,8 +7,8 @@ Introduction
This file contains overview of node-mysql-sync API.
-MysqlSyncConn class
--------------------
+MysqlConn class
+---------------
class MysqlConn {
/* Constructor */
@@ -114,10 +114,20 @@ Other
Boolean threadSafe();
-MysqlSyncStmt class
--------------------
+MysqlResult class
+-----------------
- class MysqlSyncStmt {
+ class MysqlResult {
+ /* Methods */
+ Array fetchAll();
+ Integer numRows();
+ }
+
+
+MysqlStatement class
+--------------------
+
+ class MysqlStatement {
/* Methods */
Boolean prepare(query);
}
@@ -19,7 +19,8 @@ void MysqlConn::MysqlResult::Init(Handle<Object> target) {
constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
constructor_template->SetClassName(String::NewSymbol("MysqlResult"));
- ADD_PROTOTYPE_METHOD(fetchResult, FetchResult);
+ ADD_PROTOTYPE_METHOD(fetchAll, FetchAll);
+ ADD_PROTOTYPE_METHOD(numRows, NumRows);
}
MysqlConn::MysqlResult::MysqlResult(): EventEmitter() {}
@@ -37,11 +38,12 @@ Handle<Value> MysqlConn::MysqlResult::New(const Arguments& args) {
return args.This();
}
-Handle<Value> MysqlConn::MysqlResult::FetchResult(const Arguments& args) {
+Handle<Value> MysqlConn::MysqlResult::FetchAll(const Arguments& args) {
HandleScope scope;
MysqlResult *res = OBJUNWRAP<MysqlResult>(args.This());
+ // TODO(Sannis): Is it possible?
if (!res->_res) {
return scope.Close(False());
}
@@ -111,3 +113,22 @@ Handle<Value> MysqlConn::MysqlResult::FetchResult(const Arguments& args) {
return scope.Close(js_result);
}
+Handle<Value> MysqlConn::MysqlResult::NumRows(const Arguments& args) {
+ HandleScope scope;
+
+ MysqlResult *res = OBJUNWRAP<MysqlResult>(args.This());
+
+ // TODO(Sannis): Is it possible?
+ if (!res->_res) {
+ return scope.Close(False());
+ }
+
+ if (mysqli_result_is_unbuffered(res->_res)) {
+ return THREXC("Function cannot be used with MYSQL_USE_RESULT");
+ }
+
+ Local<Value> js_result = Integer::New(mysql_num_rows(res->_res));
+
+ return scope.Close(js_result);
+}
+
@@ -13,7 +13,11 @@ See license text in LICENSE file
#include <node.h>
#include <node_events.h>
-static Persistent<String> fetchResult_symbol;
+#define mysqli_result_is_unbuffered(r) \
+((r)->handle && (r)->handle->status == MYSQL_STATUS_USE_RESULT)
+
+static Persistent<String> fetchAll_symbol;
+static Persistent<String> numRows_symbol;
class MysqlConn::MysqlResult : public node::EventEmitter {
public:
@@ -33,7 +37,9 @@ class MysqlConn::MysqlResult : public node::EventEmitter {
static Handle<Value> New(const Arguments& args);
- static Handle<Value> FetchResult(const Arguments& args);
+ static Handle<Value> FetchAll(const Arguments& args);
+
+ static Handle<Value> NumRows(const Arguments& args);
};
#endif // NODE_MYSQL_RESULT_H // NOLINT
@@ -40,8 +40,7 @@ exports.AffectedRows = function (test) {
" (autoincrement_id BIGINT NOT NULL AUTO_INCREMENT," +
" random_number INT(8) NOT NULL, random_boolean BOOLEAN NOT NULL," +
" PRIMARY KEY (autoincrement_id)) TYPE=MEMORY;");
-
- res = conn.query("DELETE FROM " + test_table + ";");
+
test.ok(res, "conn.query('DELETE FROM test_table')");
for (i = 0; i < insert_rows_count; i += 1)
@@ -13,7 +13,53 @@ var
sys = require("sys"),
mysql_libmysqlclient = require("../mysql-libmysqlclient");
-exports.FetchResult = function (test) {
+exports.NumRows = function (test) {
+ test.expect(9);
+
+ var conn = mysql_libmysqlclient.createConnection(host, user, password, database),
+ res,
+ rows;
+ test.ok(conn, "mysql_libmysqlclient.createConnection(host, user, password, database)");
+
+ res = conn.query("DROP TABLE IF EXISTS " + test_table + ";");
+ res = conn.query("CREATE TABLE " + test_table +
+ " (autoincrement_id BIGINT NOT NULL AUTO_INCREMENT," +
+ " random_number INT(8) NOT NULL, random_boolean BOOLEAN NOT NULL," +
+ " PRIMARY KEY (autoincrement_id)) TYPE=MEMORY;") && res;
+ test.ok(res, "conn.query('DELETE FROM test_table')");
+
+ res = conn.query("INSERT INTO " + test_table +
+ " (random_number, random_boolean) VALUES ('1', '1');") && res;
+ res = conn.query("INSERT INTO " + test_table +
+ " (random_number, random_boolean) VALUES ('2', '1');") && res;
+ res = conn.query("INSERT INTO " + test_table +
+ " (random_number, random_boolean) VALUES ('3', '0');") && res;
+ test.ok(res, "conn.query('INSERT INTO test_table ...')");
+
+ res = conn.query("SELECT random_number from " + test_table +
+ " WHERE random_boolean='0';", 1);
+ test.ok(res, "conn.query('SELECT ... 1')");
+ rows = res.numRows();
+ test.equals(rows, 1, "conn.query('SELECT ... 1').numRows()")
+
+ res = conn.query("SELECT random_number from " + test_table +
+ " WHERE random_boolean='1';", 1);
+ test.ok(res, "conn.query('SELECT ... 2')");
+ rows = res.numRows();
+ test.equals(rows, 2, "conn.query('SELECT ... 2').numRows()")
+
+ res = conn.query("SELECT random_number from " + test_table +
+ " WHERE random_number>'0';", 1);
+ test.ok(res, "conn.query('SELECT ... 3')");
+ rows = res.numRows();
+ test.equals(rows, 3, "conn.query('SELECT ... 3').numRows()")
+
+ conn.close();
+
+ test.done();
+};
+
+exports.FetchAll = function (test) {
test.expect(3);
var conn = mysql_libmysqlclient.createConnection(host, user, password, database),
@@ -22,8 +68,8 @@ exports.FetchResult = function (test) {
test.ok(conn, "mysql_libmysqlclient.createConnection(host, user, password, database)");
res = conn.query("SHOW TABLES;");
test.ok(res, "conn.query('SHOW TABLES;')");
- tables = res.fetchResult();
- test.ok(tables, "res.fetchResult()");
+ tables = res.fetchAll();
+ test.ok(tables, "res.fetchAll()");
conn.close();
test.done();
View
@@ -13,17 +13,35 @@ var
sys = require("sys"),
mysql_libmysqlclient = require("../mysql-libmysqlclient");
-exports.query_ShowTables_FetchResult = function (test) {
- test.expect(3);
+exports.query_FetchAll = function (test) {
+ test.expect(5);
var conn = mysql_libmysqlclient.createConnection(host, user, password, database),
res,
- tables;
+ rows;
test.ok(conn, "mysql_libmysqlclient.createConnection(host, user, password, database)");
- res = conn.query("SHOW TABLES;");
- test.ok(res, "conn.query('SHOW TABLES;')");
- tables = res.fetchResult();
- test.ok(tables, "res.fetchResult()");
+
+ res = conn.query("DROP TABLE IF EXISTS " + test_table + ";");
+ res = conn.query("CREATE TABLE " + test_table +
+ " (autoincrement_id BIGINT NOT NULL AUTO_INCREMENT," +
+ " random_number INT(8) NOT NULL, random_boolean BOOLEAN NOT NULL," +
+ " PRIMARY KEY (autoincrement_id)) TYPE=MEMORY;") && res;
+ test.ok(res, "conn.query('DELETE FROM test_table')");
+
+ res = conn.query("INSERT INTO " + test_table +
+ " (random_number, random_boolean) VALUES ('1', '1');") && res;
+ res = conn.query("INSERT INTO " + test_table +
+ " (random_number, random_boolean) VALUES ('2', '1');") && res;
+ res = conn.query("INSERT INTO " + test_table +
+ " (random_number, random_boolean) VALUES ('3', '0');") && res;
+ test.ok(res, "conn.query('INSERT INTO test_table ...')");
+
+ res = conn.query("SELECT random_number from " + test_table +
+ " WHERE random_boolean='0';");
+ test.ok(res, "conn.query('SELECT ...')");
+ rows = res.fetchAll();
+ test.same(rows, [{random_number: 3}], "conn.query('SELECT ...').fetchAll()")
+
conn.close();
test.done();
@@ -107,7 +125,7 @@ else
sys.print("Run end get results of query 'SELECT * FROM " + test_table + " ORDER BY RAND() LIMIT 10': ");
conn.query("SELECT * FROM " + test_table + " ORDER BY RAND() LIMIT 10;");
-var select_limit_result = conn.fetchResult();
+var select_limit_result = conn.fetchAll();
sys.print("OK\n");
for( var i in select_limit_result )
{

0 comments on commit 6412df8

Please sign in to comment.