Skip to content

Commit 43ca4e6

Browse files
ssinghiSannis
authored andcommitted
fetchAll and fetchAllSync takes a boolean option, which if true returns array of results, instead of objects
1 parent 4f1db77 commit 43ca4e6

File tree

4 files changed

+94
-7
lines changed

4 files changed

+94
-7
lines changed

src/mysql_bindings_result.cc

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -350,11 +350,19 @@ int MysqlResult::EIO_After_FetchAll(eio_req *req) {
350350

351351
i = 0;
352352
while ( (result_row = mysql_fetch_row(fetchAll_req->res->_res)) ) {
353-
js_result_row = Object::New();
353+
if(fetchAll_req->results_array) {
354+
js_result_row = Array::New();
355+
} else {
356+
js_result_row = Object::New();
357+
}
354358

355359
for ( j = 0; j < fetchAll_req->num_fields; j++ ) {
356360
js_field = GetFieldValue(fetchAll_req->fields[j], result_row[j]);
357-
js_result_row->Set(V8STR(fetchAll_req->fields[j].name), js_field);
361+
if(fetchAll_req->results_array) {
362+
js_result_row->Set(Integer::New(j), js_field);
363+
} else {
364+
js_result_row->Set(V8STR(fetchAll_req->fields[j].name), js_field);
365+
}
358366
}
359367

360368
js_result->Set(Integer::New(i), js_result_row);
@@ -412,7 +420,13 @@ Handle<Value> MysqlResult::FetchAll(const Arguments& args) {
412420
#ifdef MYSQL_NON_THREADSAFE
413421
return THREXC(MYSQL_NON_THREADSAFE_ERRORSTRING);
414422
#else
415-
REQ_FUN_ARG(0, callback);
423+
int arg_pos = 0;
424+
bool results_array = false;
425+
if(args.Length() > arg_pos && args[arg_pos]->IsBoolean()) {
426+
results_array = args[arg_pos]->BooleanValue();
427+
arg_pos++;
428+
}
429+
REQ_FUN_ARG(arg_pos, callback)
416430

417431
MysqlResult *res = OBJUNWRAP<MysqlResult>(args.This()); // NOLINT
418432

@@ -428,6 +442,7 @@ Handle<Value> MysqlResult::FetchAll(const Arguments& args) {
428442

429443
fetchAll_req->callback = Persistent<Function>::New(callback);
430444
fetchAll_req->res = res;
445+
fetchAll_req->results_array = results_array;
431446

432447
eio_custom(EIO_FetchAll, EIO_PRI_DEFAULT, EIO_After_FetchAll, fetchAll_req);
433448

@@ -450,6 +465,8 @@ Handle<Value> MysqlResult::FetchAllSync(const Arguments& args) {
450465

451466
MYSQLRES_MUSTBE_VALID;
452467

468+
bool results_array = (args.Length() > 0 && args[0]->IsBoolean()) ? args[0]->BooleanValue() : false;
469+
453470
MYSQL_FIELD *fields = mysql_fetch_fields(res->_res);
454471
uint32_t num_fields = mysql_num_fields(res->_res);
455472
MYSQL_ROW result_row;
@@ -461,12 +478,19 @@ Handle<Value> MysqlResult::FetchAllSync(const Arguments& args) {
461478

462479
i = 0;
463480
while ( (result_row = mysql_fetch_row(res->_res)) ) {
464-
js_result_row = Object::New();
481+
if(results_array) {
482+
js_result_row = Array::New();
483+
} else {
484+
js_result_row = Object::New();
485+
}
465486

466487
for ( j = 0; j < num_fields; j++ ) {
467-
js_field = GetFieldValue(fields[j], result_row[j]);
468-
488+
js_field = GetFieldValue(fields[j], result_row[j]);
489+
if(results_array) {
490+
js_result_row->Set(Integer::New(j), js_field);
491+
} else {
469492
js_result_row->Set(V8STR(fields[j].name), js_field);
493+
}
470494
}
471495

472496
js_result->Set(Integer::New(i), js_result_row);

src/mysql_bindings_result.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ class MysqlResult : public node::EventEmitter {
8484

8585
MYSQL_FIELD *fields;
8686
uint32_t num_fields;
87+
bool results_array;
8788
};
8889
static int EIO_After_FetchAll(eio_req *req);
8990
static int EIO_FetchAll(eio_req *req);

tests/simple/test-class-mysqlresult.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,26 @@ exports.FetchAll = function (test) {
127127
});
128128
};
129129

130+
exports.FetchAllWithArrayOption = function (test) {
131+
test.expect(5);
132+
133+
var conn = mysql_libmysqlclient.createConnectionSync(cfg.host, cfg.user, cfg.password, cfg.database),
134+
res;
135+
test.ok(conn, "mysql_libmysqlclient.createConnectionSync(host, user, password, database)");
136+
137+
res = conn.querySync("SHOW TABLES;");
138+
test.ok(res, "conn.querySync('SHOW TABLES;')");
139+
140+
res.fetchAll(true,function (err, tables) {
141+
test.ok(err === null, "res.fetchAll() err===null");
142+
test.ok(tables, "res.fetchAll() result");
143+
test.ok(Array.isArray(tables[0]), "Result returns an array of array");
144+
conn.closeSync();
145+
146+
test.done();
147+
});
148+
};
149+
130150
exports.FetchAllSync = function (test) {
131151
test.expect(7);
132152

@@ -168,6 +188,48 @@ exports.FetchAllSync = function (test) {
168188
test.done();
169189
};
170190

191+
exports.FetchAllSyncWithArrayOption = function (test) {
192+
test.expect(8);
193+
194+
var conn = mysql_libmysqlclient.createConnectionSync(cfg.host, cfg.user, cfg.password, cfg.database),
195+
res,
196+
tables,
197+
rows;
198+
test.ok(conn, "mysql_libmysqlclient.createConnectionSync(host, user, password, database)");
199+
200+
res = conn.querySync("SHOW TABLES;");
201+
test.ok(res, "conn.querySync('SHOW TABLES;')");
202+
tables = res.fetchAllSync();
203+
test.ok(tables, "res.fetchAllSync()");
204+
res = false;
205+
tables.forEach(function (table) {
206+
if (table.Tables_in_test === cfg.test_table) {
207+
res = true;
208+
}
209+
});
210+
test.ok(res, "res.fetchAllSync() show test table");
211+
212+
res = conn.querySync("DELETE FROM " + cfg.test_table + ";");
213+
res = conn.querySync("INSERT INTO " + cfg.test_table +
214+
" (random_number, random_boolean) VALUES ('1', '1');") && res;
215+
res = conn.querySync("INSERT INTO " + cfg.test_table +
216+
" (random_number, random_boolean) VALUES ('2', '1');") && res;
217+
res = conn.querySync("INSERT INTO " + cfg.test_table +
218+
" (random_number, random_boolean) VALUES ('3', '0');") && res;
219+
test.ok(res, "INSERT");
220+
221+
res = conn.querySync("SELECT random_number from " + cfg.test_table +
222+
" WHERE random_boolean='0';");
223+
test.ok(res, "SELECT");
224+
rows = res.fetchAllSync(true);
225+
test.ok(Array.isArray(rows[0]), "Result returns an array of array");
226+
test.same(rows, [[3]], "conn.querySync('SELECT ...').fetchAllSync()");
227+
228+
conn.closeSync();
229+
230+
test.done();
231+
};
232+
171233
exports.FetchArraySync = function (test) {
172234
test.expect(5);
173235

wscript

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def test(tst):
6161
Utils.exec_command('node ./tools/nodeunit/bin/nodeunit tests/slow')
6262
else:
6363
if Options.options.ignored:
64-
Utils.exec_command('node_g ./tools/nodeunit/bin/nodeunit tests/ignored')
64+
Utils.exec_command('node ./tools/nodeunit/bin/nodeunit tests/ignored')
6565
else:
6666
if Options.options.all:
6767
Utils.exec_command('node ./tools/nodeunit/bin/nodeunit tests/simple tests/complex tests/slow')

0 commit comments

Comments
 (0)