@@ -231,6 +231,26 @@ Local<Value> MysqlResult::GetFieldValue(MYSQL_FIELD field, char* field_value, un
231
231
return scope.Close (js_field);
232
232
}
233
233
234
+ MysqlResult::fetch_options MysqlResult::GetFetchOptions (Local<Object> options) {
235
+ fetch_options fo = {};
236
+
237
+ // Inherit from options object
238
+ if (options->Has (V8STR (" asArray" ))) {
239
+ DEBUG_PRINTF (" +asArray" );
240
+ fo.results_as_array = options->Get (V8STR (" asArray" ))->BooleanValue ();
241
+ }
242
+ if (options->Has (V8STR (" nestTables" ))) {
243
+ DEBUG_PRINTF (" +nestTables" );
244
+ fo.results_nest_tables = options->ToObject ()->Get (V8STR (" nestTables" ))->BooleanValue ();
245
+ }
246
+
247
+ if (fo.results_as_array || fo.results_nest_tables ) {
248
+ DEBUG_PRINTF (" \n " );
249
+ }
250
+
251
+ return fo;
252
+ }
253
+
234
254
void MysqlResult::Free () {
235
255
if (_res) {
236
256
mysql_free_result (_res);
@@ -337,7 +357,7 @@ void MysqlResult::EIO_After_FetchAll(uv_work_t *req) {
337
357
while ((result_row = mysql_fetch_row (fetchAll_req->res ->_res ))) {
338
358
field_lengths = mysql_fetch_lengths (fetchAll_req->res ->_res );
339
359
340
- if (fetchAll_req->results_as_array ) {
360
+ if (fetchAll_req->fo . results_as_array ) {
341
361
js_result_row = Array::New ();
342
362
} else {
343
363
js_result_row = Object::New ();
@@ -346,9 +366,9 @@ void MysqlResult::EIO_After_FetchAll(uv_work_t *req) {
346
366
for (j = 0 ; j < num_fields; j++) {
347
367
js_field = GetFieldValue (fields[j], result_row[j], field_lengths[j]);
348
368
349
- if (fetchAll_req->results_as_array ) {
369
+ if (fetchAll_req->fo . results_as_array ) {
350
370
js_result_row->Set (Integer::NewFromUnsigned (j), js_field);
351
- } else if (fetchAll_req->results_nest_tables ) {
371
+ } else if (fetchAll_req->fo . results_nest_tables ) {
352
372
if (!js_result_row->Has (V8STR (fields[j].table ))) {
353
373
js_result_row->Set (V8STR (fields[j].table ), Object::New ());
354
374
}
@@ -438,19 +458,13 @@ Handle<Value> MysqlResult::FetchAll(const Arguments& args) {
438
458
HandleScope scope;
439
459
440
460
int arg_pos = 0 ;
441
- bool results_as_array = false ;
442
- bool results_nest_tables = false ;
461
+ fetch_options fo = {};
443
462
bool throw_wrong_arguments_exception = false ;
444
463
445
464
if (args.Length () > 0 ) {
446
465
if (args[0 ]->IsObject ()) { // Simple Object or Function
447
466
if (!args[0 ]->IsFunction ()) { // Simple Object - options hash
448
- if (args[0 ]->ToObject ()->Has (V8STR (" asArray" ))) {
449
- results_as_array = args[0 ]->ToObject ()->Get (V8STR (" asArray" ))->BooleanValue ();
450
- }
451
- if (args[0 ]->ToObject ()->Has (V8STR (" nestTables" ))) {
452
- results_nest_tables = args[0 ]->ToObject ()->Get (V8STR (" nestTables" ))->BooleanValue ();
453
- }
467
+ fo = MysqlResult::GetFetchOptions (args[0 ]->ToObject ());
454
468
arg_pos++;
455
469
}
456
470
} else { // Not an options Object or a Function
@@ -471,13 +485,14 @@ Handle<Value> MysqlResult::FetchAll(const Arguments& args) {
471
485
return Undefined ();
472
486
}
473
487
474
- if (results_as_array && results_nest_tables) {
488
+ if (fo.results_as_array && fo.results_nest_tables ) {
489
+ // Cuz' this is not run-time error, just programmers mistake
475
490
return THREXC (" You can't mix 'asArray' and 'nestTables' options" );
476
- int argc = 1 ;
477
- Local<Value> argv[1 ];
478
- argv[0 ] = V8EXC (" You can't mix 'asArray' and 'nestTables' options" );
479
- node::MakeCallback (Context::GetCurrent ()->Global (), callback, argc, argv);
480
- return Undefined ();
491
+ // int argc = 1;
492
+ // Local<Value> argv[1];
493
+ // argv[0] = V8EXC("You can't mix 'asArray' and 'nestTables' options");
494
+ // node::MakeCallback(Context::GetCurrent()->Global(), callback, argc, argv);
495
+ // return Undefined();
481
496
}
482
497
483
498
MysqlResult *res = OBJUNWRAP<MysqlResult>(args.Holder ()); // NOLINT
@@ -490,8 +505,7 @@ Handle<Value> MysqlResult::FetchAll(const Arguments& args) {
490
505
fetchAll_req->res = res;
491
506
res->Ref ();
492
507
493
- fetchAll_req->results_as_array = results_as_array;
494
- fetchAll_req->results_nest_tables = results_nest_tables;
508
+ fetchAll_req->fo = fo;
495
509
496
510
uv_work_t *_req = new uv_work_t ;
497
511
_req->data = fetchAll_req;
@@ -513,22 +527,16 @@ Handle<Value> MysqlResult::FetchAllSync(const Arguments& args) {
513
527
514
528
MYSQLRES_MUSTBE_VALID;
515
529
516
- bool results_as_array = false ;
517
- bool results_nest_tables = false ;
530
+ fetch_options fo = {false , false };
518
531
519
532
if (args.Length () > 0 ) {
520
533
if (!args[0 ]->IsObject ()) {
521
534
return THREXC (" fetchAllSync can handle only (options) or none arguments" );
522
535
}
523
- if (args[0 ]->ToObject ()->Has (V8STR (" asArray" ))) {
524
- results_as_array = args[0 ]->ToObject ()->Get (V8STR (" asArray" ))->BooleanValue ();
525
- }
526
- if (args[0 ]->ToObject ()->Has (V8STR (" nestTables" ))) {
527
- results_nest_tables = args[0 ]->ToObject ()->Get (V8STR (" nestTables" ))->BooleanValue ();
528
- }
536
+ fo = MysqlResult::GetFetchOptions (args[0 ]->ToObject ());
529
537
}
530
538
531
- if (results_as_array && results_nest_tables) {
539
+ if (fo. results_as_array && fo. results_nest_tables ) {
532
540
return THREXC (" You can't mix 'asArray' and 'nestTables' options" );
533
541
}
534
542
@@ -546,7 +554,7 @@ Handle<Value> MysqlResult::FetchAllSync(const Arguments& args) {
546
554
while ( (result_row = mysql_fetch_row (res->_res )) ) {
547
555
field_lengths = mysql_fetch_lengths (res->_res );
548
556
549
- if (results_as_array) {
557
+ if (fo. results_as_array ) {
550
558
js_result_row = Array::New ();
551
559
} else {
552
560
js_result_row = Object::New ();
@@ -555,9 +563,9 @@ Handle<Value> MysqlResult::FetchAllSync(const Arguments& args) {
555
563
for (j = 0 ; j < num_fields; j++) {
556
564
js_field = GetFieldValue (fields[j], result_row[j], field_lengths[j]);
557
565
558
- if (results_as_array) {
566
+ if (fo. results_as_array ) {
559
567
js_result_row->Set (Integer::NewFromUnsigned (j), js_field);
560
- } else if (results_nest_tables) {
568
+ } else if (fo. results_nest_tables ) {
561
569
if (!js_result_row->Has (V8STR (fields[j].table ))) {
562
570
js_result_row->Set (V8STR (fields[j].table ), Object::New ());
563
571
}
@@ -709,22 +717,16 @@ Handle<Value> MysqlResult::FetchRowSync(const Arguments& args) {
709
717
710
718
MYSQLRES_MUSTBE_VALID;
711
719
712
- bool results_as_array = false ;
713
- bool results_nest_tables = false ;
720
+ fetch_options fo = {};
714
721
715
722
if (args.Length () > 0 ) {
716
723
if (!args[0 ]->IsObject ()) {
717
724
return THREXC (" fetchRowSync can handle only (options) or none arguments" );
718
725
}
719
- if (args[0 ]->ToObject ()->Has (V8STR (" asArray" ))) {
720
- results_as_array = args[0 ]->ToObject ()->Get (V8STR (" asArray" ))->BooleanValue ();
721
- }
722
- if (args[0 ]->ToObject ()->Has (V8STR (" nestTables" ))) {
723
- results_nest_tables = args[0 ]->ToObject ()->Get (V8STR (" nestTables" ))->BooleanValue ();
724
- }
726
+ fo = MysqlResult::GetFetchOptions (args[0 ]->ToObject ());
725
727
}
726
728
727
- if (results_as_array && results_nest_tables) {
729
+ if (fo. results_as_array && fo. results_nest_tables ) {
728
730
return THREXC (" You can't mix 'asArray' and 'nestTables' options" );
729
731
}
730
732
@@ -743,18 +745,18 @@ Handle<Value> MysqlResult::FetchRowSync(const Arguments& args) {
743
745
744
746
unsigned long *field_lengths = mysql_fetch_lengths (res->_res );
745
747
746
- if (results_as_array) {
748
+ if (fo. results_as_array ) {
747
749
js_result_row = Array::New ();
748
750
} else {
749
751
js_result_row = Object::New ();
750
752
}
751
753
752
- for ( j = 0 ; j < num_fields; j++ ) {
754
+ for (j = 0 ; j < num_fields; j++) {
753
755
js_field = GetFieldValue (fields[j], result_row[j], field_lengths[j]);
754
756
755
- if (results_as_array) {
757
+ if (fo. results_as_array ) {
756
758
js_result_row->Set (Integer::NewFromUnsigned (j), js_field);
757
- } else if (results_nest_tables) {
759
+ } else if (fo. results_nest_tables ) {
758
760
if (!js_result_row->Has (V8STR (fields[j].table ))) {
759
761
js_result_row->Set (V8STR (fields[j].table ), Object::New ());
760
762
}
0 commit comments