Skip to content

Commit 369d446

Browse files
committed
Use 'nestTables' fetchAll[Sync]() option name for compatibility with node-mysql, refs #140
Also drop boolean variant of options (refs #114) and improve error reporting
1 parent 7905c74 commit 369d446

File tree

5 files changed

+106
-123
lines changed

5 files changed

+106
-123
lines changed

src/mysql_bindings_result.cc

Lines changed: 63 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ void MysqlResult::EIO_After_FetchAll(uv_work_t *req) {
343343
while ((result_row = mysql_fetch_row(fetchAll_req->res->_res))) {
344344
field_lengths = mysql_fetch_lengths(fetchAll_req->res->_res);
345345

346-
if (fetchAll_req->results_array) {
346+
if (fetchAll_req->results_as_array) {
347347
js_result_row = Array::New();
348348
} else {
349349
js_result_row = Object::New();
@@ -353,19 +353,16 @@ void MysqlResult::EIO_After_FetchAll(uv_work_t *req) {
353353
js_field = GetFieldValue(fields[j],
354354
result_row[j],
355355
field_lengths[j]);
356-
if (fetchAll_req->results_array) {
356+
if (fetchAll_req->results_as_array) {
357357
js_result_row->Set(Integer::NewFromUnsigned(j), js_field);
358-
} else {
359-
if (fetchAll_req->results_structured) {
360-
if (!js_result_row->Has(V8STR(fields[j].table))) {
361-
js_result_row->Set(V8STR(fields[j].table),
362-
Object::New());
363-
}
364-
js_result_row->Get(V8STR(fields[j].table))->ToObject()
365-
->Set(V8STR(fields[j].name), js_field);
366-
} else {
367-
js_result_row->Set(V8STR(fields[j].name), js_field);
358+
} else if (fetchAll_req->results_nest_tables) {
359+
if (!js_result_row->Has(V8STR(fields[j].table))) {
360+
js_result_row->Set(V8STR(fields[j].table), Object::New());
368361
}
362+
js_result_row->Get(V8STR(fields[j].table))->ToObject()
363+
->Set(V8STR(fields[j].name), js_field);
364+
} else {
365+
js_result_row->Set(V8STR(fields[j].name), js_field);
369366
}
370367
}
371368

@@ -448,33 +445,47 @@ Handle<Value> MysqlResult::FetchAll(const Arguments& args) {
448445
HandleScope scope;
449446

450447
int arg_pos = 0;
451-
bool results_array = false;
452-
bool results_structured = false;
448+
bool results_as_array = false;
449+
bool results_nest_tables = false;
450+
bool throw_wrong_arguments_exception = false;
453451

454452
if (args.Length() > 0) {
455-
if (args[0]->IsBoolean()) {
456-
results_array = args[0]->BooleanValue();
457-
arg_pos++;
458-
} else if (args[0]->IsObject() && !args[0]->IsFunction()) {
459-
if (args[0]->ToObject()->Has(V8STR("array"))) {
460-
results_array = args[0]->ToObject()
461-
->Get(V8STR("array"))->BooleanValue();
462-
}
463-
if (args[0]->ToObject()->Has(V8STR("structured"))) {
464-
results_structured = args[0]->ToObject()
465-
->Get(V8STR("structured"))->BooleanValue();
453+
if (args[0]->IsObject()) { // Simple Object or Function
454+
if (!args[0]->IsFunction()) { // Simple Object - options hash
455+
if (args[0]->ToObject()->Has(V8STR("asArray"))) {
456+
results_as_array = args[0]->ToObject()->Get(V8STR("asArray"))->BooleanValue();
457+
}
458+
if (args[0]->ToObject()->Has(V8STR("nestTables"))) {
459+
results_nest_tables = args[0]->ToObject()->Get(V8STR("nestTables"))->BooleanValue();
460+
}
461+
arg_pos++;
466462
}
463+
} else { // Not an options Object or a Function
464+
throw_wrong_arguments_exception = true;
467465
arg_pos++;
468466
}
469467
// NOT here: any function is object
470468
// arg_pos++;
471469
}
472470

473-
if (results_array && results_structured) {
474-
return THREXC("You can't mix 'array' and 'structured' parameters");
471+
REQ_FUN_ARG(arg_pos, callback)
472+
473+
if (throw_wrong_arguments_exception) {
474+
int argc = 1;
475+
Local<Value> argv[1];
476+
argv[0] = V8EXC("fetchAllSync can handle only (options) or none arguments");
477+
node::MakeCallback(Context::GetCurrent()->Global(), callback, argc, argv);
478+
return Undefined();
475479
}
476480

477-
REQ_FUN_ARG(arg_pos, callback)
481+
if (results_as_array && results_nest_tables) {
482+
return THREXC("You can't mix 'asArray' and 'nestTables' options");
483+
int argc = 1;
484+
Local<Value> argv[1];
485+
argv[0] = V8EXC("You can't mix 'asArray' and 'nestTables' options");
486+
node::MakeCallback(Context::GetCurrent()->Global(), callback, argc, argv);
487+
return Undefined();
488+
}
478489

479490
MysqlResult *res = OBJUNWRAP<MysqlResult>(args.Holder()); // NOLINT
480491

@@ -486,8 +497,8 @@ Handle<Value> MysqlResult::FetchAll(const Arguments& args) {
486497
fetchAll_req->res = res;
487498
res->Ref();
488499

489-
fetchAll_req->results_array = results_array;
490-
fetchAll_req->results_structured = results_structured;
500+
fetchAll_req->results_as_array = results_as_array;
501+
fetchAll_req->results_nest_tables = results_nest_tables;
491502

492503
uv_work_t *_req = new uv_work_t;
493504
_req->data = fetchAll_req;
@@ -498,7 +509,7 @@ Handle<Value> MysqlResult::FetchAll(const Arguments& args) {
498509

499510
/**
500511
* MysqlResult#fetchAllSync([options]) -> Array
501-
* - options (Boolean|Object): Fetch style options (optional)
512+
* - options (Object): Fetch style options (optional)
502513
*
503514
* Fetches all result rows as an array
504515
**/
@@ -509,26 +520,23 @@ Handle<Value> MysqlResult::FetchAllSync(const Arguments& args) {
509520

510521
MYSQLRES_MUSTBE_VALID;
511522

512-
bool results_array = false;
513-
bool results_structured = false;
523+
bool results_as_array = false;
524+
bool results_nest_tables = false;
514525

515526
if (args.Length() > 0) {
516-
if (args[0]->IsBoolean()) {
517-
results_array = args[0]->BooleanValue();
518-
} else if (args[0]->IsObject()) {
519-
if (args[0]->ToObject()->Has(V8STR("array"))) {
520-
results_array = args[0]->ToObject()
521-
->Get(V8STR("array"))->BooleanValue();
522-
}
523-
if (args[0]->ToObject()->Has(V8STR("structured"))) {
524-
results_structured = args[0]->ToObject()
525-
->Get(V8STR("structured"))->BooleanValue();
526-
}
527+
if (!args[0]->IsObject()) {
528+
return THREXC("fetchAllSync can handle only (options) or none arguments");
529+
}
530+
if (args[0]->ToObject()->Has(V8STR("asArray"))) {
531+
results_as_array = args[0]->ToObject()->Get(V8STR("asArray"))->BooleanValue();
532+
}
533+
if (args[0]->ToObject()->Has(V8STR("nestTables"))) {
534+
results_nest_tables = args[0]->ToObject()->Get(V8STR("nestTables"))->BooleanValue();
527535
}
528536
}
529537

530-
if (results_array && results_structured) {
531-
return THREXC("You can't mix 'array' and 'structured' parameters");
538+
if (results_as_array && results_nest_tables) {
539+
return THREXC("You can't mix 'asArray' and 'nestTables' options");
532540
}
533541

534542
MYSQL_FIELD *fields = mysql_fetch_fields(res->_res);
@@ -545,7 +553,7 @@ Handle<Value> MysqlResult::FetchAllSync(const Arguments& args) {
545553
while ( (result_row = mysql_fetch_row(res->_res)) ) {
546554
field_lengths = mysql_fetch_lengths(res->_res);
547555

548-
if (results_array) {
556+
if (results_as_array) {
549557
js_result_row = Array::New();
550558
} else {
551559
js_result_row = Object::New();
@@ -555,19 +563,16 @@ Handle<Value> MysqlResult::FetchAllSync(const Arguments& args) {
555563
js_field = GetFieldValue(fields[j],
556564
result_row[j],
557565
field_lengths[j]);
558-
if (results_array) {
566+
if (results_as_array) {
559567
js_result_row->Set(Integer::NewFromUnsigned(j), js_field);
560-
} else {
561-
if (results_structured) {
562-
if (!js_result_row->Has(V8STR(fields[j].table))) {
563-
js_result_row->Set(V8STR(fields[j].table),
564-
Object::New());
565-
}
566-
js_result_row->Get(V8STR(fields[j].table))->ToObject()
567-
->Set(V8STR(fields[j].name), js_field);
568-
} else {
569-
js_result_row->Set(V8STR(fields[j].name), js_field);
568+
} else if (results_nest_tables) {
569+
if (!js_result_row->Has(V8STR(fields[j].table))) {
570+
js_result_row->Set(V8STR(fields[j].table), Object::New());
570571
}
572+
js_result_row->Get(V8STR(fields[j].table))->ToObject()
573+
->Set(V8STR(fields[j].name), js_field);
574+
} else {
575+
js_result_row->Set(V8STR(fields[j].name), js_field);
571576
}
572577
}
573578

src/mysql_bindings_result.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ class MysqlResult : public node::ObjectWrap {
8787

8888
MYSQL_FIELD *fields;
8989
uint32_t num_fields;
90-
bool results_array;
91-
bool results_structured;
90+
bool results_as_array;
91+
bool results_nest_tables;
9292
};
9393
static void EIO_After_FetchAll(uv_work_t *req);
9494
static void EIO_FetchAll(uv_work_t *req);

tests/complex/test-binary-buffers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ exports.FetchAllSyncWithBinaryFields = function (test) {
5656
res = conn.querySync("SELECT vc, vbi, bi, t, b FROM " + cfg.test_table2 + " ORDER BY id;");
5757
test.ok(res instanceof cfg.mysql_bindings.MysqlResult);
5858

59-
rows = res.fetchAllSync(true);
59+
rows = res.fetchAllSync({"asArray": true});
6060

6161
rowsExpected = [ [ 'qwerty', new Buffer('qwerty'), new Buffer('qwerty\0\0'), 'qwerty', new Buffer('qwerty') ],
6262
[ 'qwe\u0000\u0000', new Buffer('qwe\0\0'), new Buffer('qwe\0\0\0\0\0'), 'qwe\u0000\u0000', new Buffer('qwe\0\0') ],

0 commit comments

Comments
 (0)