Skip to content

Commit 33ad124

Browse files
committed
Refactor: introduce MysqlResult::GetFetchOptions
Refs #143
1 parent a9ad851 commit 33ad124

File tree

3 files changed

+54
-46
lines changed

3 files changed

+54
-46
lines changed

src/mysql_bindings_result.cc

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,26 @@ Local<Value> MysqlResult::GetFieldValue(MYSQL_FIELD field, char* field_value, un
231231
return scope.Close(js_field);
232232
}
233233

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+
234254
void MysqlResult::Free() {
235255
if (_res) {
236256
mysql_free_result(_res);
@@ -337,7 +357,7 @@ void MysqlResult::EIO_After_FetchAll(uv_work_t *req) {
337357
while ((result_row = mysql_fetch_row(fetchAll_req->res->_res))) {
338358
field_lengths = mysql_fetch_lengths(fetchAll_req->res->_res);
339359

340-
if (fetchAll_req->results_as_array) {
360+
if (fetchAll_req->fo.results_as_array) {
341361
js_result_row = Array::New();
342362
} else {
343363
js_result_row = Object::New();
@@ -346,9 +366,9 @@ void MysqlResult::EIO_After_FetchAll(uv_work_t *req) {
346366
for (j = 0; j < num_fields; j++) {
347367
js_field = GetFieldValue(fields[j], result_row[j], field_lengths[j]);
348368

349-
if (fetchAll_req->results_as_array) {
369+
if (fetchAll_req->fo.results_as_array) {
350370
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) {
352372
if (!js_result_row->Has(V8STR(fields[j].table))) {
353373
js_result_row->Set(V8STR(fields[j].table), Object::New());
354374
}
@@ -438,19 +458,13 @@ Handle<Value> MysqlResult::FetchAll(const Arguments& args) {
438458
HandleScope scope;
439459

440460
int arg_pos = 0;
441-
bool results_as_array = false;
442-
bool results_nest_tables = false;
461+
fetch_options fo = {};
443462
bool throw_wrong_arguments_exception = false;
444463

445464
if (args.Length() > 0) {
446465
if (args[0]->IsObject()) { // Simple Object or Function
447466
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());
454468
arg_pos++;
455469
}
456470
} else { // Not an options Object or a Function
@@ -471,13 +485,14 @@ Handle<Value> MysqlResult::FetchAll(const Arguments& args) {
471485
return Undefined();
472486
}
473487

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
475490
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();
481496
}
482497

483498
MysqlResult *res = OBJUNWRAP<MysqlResult>(args.Holder()); // NOLINT
@@ -490,8 +505,7 @@ Handle<Value> MysqlResult::FetchAll(const Arguments& args) {
490505
fetchAll_req->res = res;
491506
res->Ref();
492507

493-
fetchAll_req->results_as_array = results_as_array;
494-
fetchAll_req->results_nest_tables = results_nest_tables;
508+
fetchAll_req->fo = fo;
495509

496510
uv_work_t *_req = new uv_work_t;
497511
_req->data = fetchAll_req;
@@ -513,22 +527,16 @@ Handle<Value> MysqlResult::FetchAllSync(const Arguments& args) {
513527

514528
MYSQLRES_MUSTBE_VALID;
515529

516-
bool results_as_array = false;
517-
bool results_nest_tables = false;
530+
fetch_options fo = {false, false};
518531

519532
if (args.Length() > 0) {
520533
if (!args[0]->IsObject()) {
521534
return THREXC("fetchAllSync can handle only (options) or none arguments");
522535
}
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());
529537
}
530538

531-
if (results_as_array && results_nest_tables) {
539+
if (fo.results_as_array && fo.results_nest_tables) {
532540
return THREXC("You can't mix 'asArray' and 'nestTables' options");
533541
}
534542

@@ -546,7 +554,7 @@ Handle<Value> MysqlResult::FetchAllSync(const Arguments& args) {
546554
while ( (result_row = mysql_fetch_row(res->_res)) ) {
547555
field_lengths = mysql_fetch_lengths(res->_res);
548556

549-
if (results_as_array) {
557+
if (fo.results_as_array) {
550558
js_result_row = Array::New();
551559
} else {
552560
js_result_row = Object::New();
@@ -555,9 +563,9 @@ Handle<Value> MysqlResult::FetchAllSync(const Arguments& args) {
555563
for (j = 0; j < num_fields; j++) {
556564
js_field = GetFieldValue(fields[j], result_row[j], field_lengths[j]);
557565

558-
if (results_as_array) {
566+
if (fo.results_as_array) {
559567
js_result_row->Set(Integer::NewFromUnsigned(j), js_field);
560-
} else if (results_nest_tables) {
568+
} else if (fo.results_nest_tables) {
561569
if (!js_result_row->Has(V8STR(fields[j].table))) {
562570
js_result_row->Set(V8STR(fields[j].table), Object::New());
563571
}
@@ -709,22 +717,16 @@ Handle<Value> MysqlResult::FetchRowSync(const Arguments& args) {
709717

710718
MYSQLRES_MUSTBE_VALID;
711719

712-
bool results_as_array = false;
713-
bool results_nest_tables = false;
720+
fetch_options fo = {};
714721

715722
if (args.Length() > 0) {
716723
if (!args[0]->IsObject()) {
717724
return THREXC("fetchRowSync can handle only (options) or none arguments");
718725
}
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());
725727
}
726728

727-
if (results_as_array && results_nest_tables) {
729+
if (fo.results_as_array && fo.results_nest_tables) {
728730
return THREXC("You can't mix 'asArray' and 'nestTables' options");
729731
}
730732

@@ -743,18 +745,18 @@ Handle<Value> MysqlResult::FetchRowSync(const Arguments& args) {
743745

744746
unsigned long *field_lengths = mysql_fetch_lengths(res->_res);
745747

746-
if (results_as_array) {
748+
if (fo.results_as_array) {
747749
js_result_row = Array::New();
748750
} else {
749751
js_result_row = Object::New();
750752
}
751753

752-
for ( j = 0; j < num_fields; j++ ) {
754+
for (j = 0; j < num_fields; j++) {
753755
js_field = GetFieldValue(fields[j], result_row[j], field_lengths[j]);
754756

755-
if (results_as_array) {
757+
if (fo.results_as_array) {
756758
js_result_row->Set(Integer::NewFromUnsigned(j), js_field);
757-
} else if (results_nest_tables) {
759+
} else if (fo.results_nest_tables) {
758760
if (!js_result_row->Has(V8STR(fields[j].table))) {
759761
js_result_row->Set(V8STR(fields[j].table), Object::New());
760762
}

src/mysql_bindings_result.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ class MysqlResult : public node::ObjectWrap {
4242

4343
static Local<Value> GetFieldValue(MYSQL_FIELD field, char* field_value, unsigned long field_length);
4444

45+
struct fetch_options {
46+
bool results_as_array;
47+
bool results_nest_tables;
48+
};
49+
static fetch_options GetFetchOptions(Local<Object> options);
50+
4551
void Free();
4652

4753
protected:
@@ -80,8 +86,8 @@ class MysqlResult : public node::ObjectWrap {
8086

8187
MYSQL_FIELD *fields;
8288
uint32_t num_fields;
83-
bool results_as_array;
84-
bool results_nest_tables;
89+
90+
fetch_options fo;
8591
};
8692
static void EIO_After_FetchAll(uv_work_t *req);
8793
static void EIO_FetchAll(uv_work_t *req);

0 commit comments

Comments
 (0)