@@ -442,9 +442,7 @@ void Statement::Work_AfterGet(napi_env e, napi_status status, void* data) {
442
442
if (!cb.IsUndefined () && cb.IsFunction ()) {
443
443
if (stmt->status == SQLITE_ROW) {
444
444
// Create the result array from the data we acquired.
445
- std::vector<Napi::String> names;
446
- FETCH_COLUMN_NAMES (stmt->_handle , names);
447
- Napi::Value argv[] = { env.Null (), RowToJS (env, &baton->row , names) };
445
+ Napi::Value argv[] = { env.Null (), RowToJS (env, &baton->row ) };
448
446
TRY_CATCH_CALL (stmt->Value (), cb, 2 , argv);
449
447
}
450
448
else {
@@ -586,23 +584,27 @@ void Statement::Work_AfterAll(napi_env e, napi_status status, void* data) {
586
584
// Fire callbacks.
587
585
Napi::Function cb = baton->callback .Value ();
588
586
if (!cb.IsUndefined () && cb.IsFunction ()) {
589
- Napi::Array result (Napi::Array::New (env, baton->rows .size ()));
590
-
591
587
if (baton->rows .size ()) {
592
- std::vector<Napi::String> names;
593
- FETCH_COLUMN_NAMES (stmt->_handle , names);
594
-
595
588
// Create the result array from the data we acquired.
589
+ Napi::Array result (Napi::Array::New (env, baton->rows .size ()));
596
590
Rows::const_iterator it = baton->rows .begin ();
597
591
Rows::const_iterator end = baton->rows .end ();
598
592
for (int i = 0 ; it < end; ++it, i++) {
599
593
std::unique_ptr<Row> row (*it);
600
- result.Set (i, RowToJS (env, row.get (), names ));
594
+ ( result) .Set (i, RowToJS (env,row.get ()));
601
595
}
602
- }
603
596
604
- Napi::Value argv[] = { env.Null (), result };
605
- TRY_CATCH_CALL (stmt->Value (), cb, 2 , argv);
597
+ Napi::Value argv[] = { env.Null (), result };
598
+ TRY_CATCH_CALL (stmt->Value (), cb, 2 , argv);
599
+ }
600
+ else {
601
+ // There were no result rows.
602
+ Napi::Value argv[] = {
603
+ env.Null (),
604
+ Napi::Array::New (env, 0 )
605
+ };
606
+ TRY_CATCH_CALL (stmt->Value (), cb, 2 , argv);
607
+ }
606
608
}
607
609
}
608
610
@@ -698,7 +700,6 @@ void Statement::AsyncEach(uv_async_t* handle) {
698
700
699
701
Napi::Env env = async->stmt ->Env ();
700
702
Napi::HandleScope scope (env);
701
- Napi::Function cb = async->item_cb .Value ();
702
703
703
704
while (true ) {
704
705
// Get the contents out of the data cache for us to process in the JS callback.
@@ -711,34 +712,31 @@ void Statement::AsyncEach(uv_async_t* handle) {
711
712
break ;
712
713
}
713
714
715
+ Napi::Function cb = async->item_cb .Value ();
714
716
if (!cb.IsUndefined () && cb.IsFunction ()) {
715
- if (async->stmt ->columns .size () == 0 ) {
716
- FETCH_COLUMN_NAMES (async->stmt ->_handle , async->stmt ->columns );
717
- }
718
-
719
717
Napi::Value argv[2 ];
720
718
argv[0 ] = env.Null ();
721
719
722
720
Rows::const_iterator it = rows.begin ();
723
721
Rows::const_iterator end = rows.end ();
724
722
for (int i = 0 ; it < end; ++it, i++) {
725
723
std::unique_ptr<Row> row (*it);
726
- argv[1 ] = RowToJS (env, row.get (), async-> stmt -> columns );
724
+ argv[1 ] = RowToJS (env,row.get ());
727
725
async->retrieved ++;
728
726
TRY_CATCH_CALL (async->stmt ->Value (), cb, 2 , argv);
729
727
}
730
728
}
731
729
}
732
730
731
+ Napi::Function cb = async->completed_cb .Value ();
733
732
if (async->completed ) {
734
- async->stmt ->columns .clear ();
735
- Napi::Function completed_cb = async->completed_cb .Value ();
736
- if (!completed_cb.IsEmpty () && completed_cb.IsFunction ()) {
733
+ if (!cb.IsEmpty () &&
734
+ cb.IsFunction ()) {
737
735
Napi::Value argv[] = {
738
736
env.Null (),
739
737
Napi::Number::New (env, async->retrieved )
740
738
};
741
- TRY_CATCH_CALL (async->stmt ->Value (), completed_cb , 2 , argv);
739
+ TRY_CATCH_CALL (async->stmt ->Value (), cb , 2 , argv);
742
740
}
743
741
uv_close (reinterpret_cast <uv_handle_t *>(handle), CloseCallback);
744
742
}
@@ -798,7 +796,7 @@ void Statement::Work_AfterReset(napi_env e, napi_status status, void* data) {
798
796
STATEMENT_END ();
799
797
}
800
798
801
- Napi::Value Statement::RowToJS (Napi::Env env, Row* row, std::vector<Napi::String> names ) {
799
+ Napi::Value Statement::RowToJS (Napi::Env env, Row* row) {
802
800
Napi::EscapableHandleScope scope (env);
803
801
804
802
Napi::Object result = Napi::Object::New (env);
@@ -828,7 +826,7 @@ Napi::Value Statement::RowToJS(Napi::Env env, Row* row, std::vector<Napi::String
828
826
} break ;
829
827
}
830
828
831
- result.Set (names[i] , value);
829
+ ( result) .Set (Napi::String::New (env, field-> name . c_str ()) , value);
832
830
833
831
DELETE_FIELD (field);
834
832
}
@@ -841,25 +839,26 @@ void Statement::GetRow(Row* row, sqlite3_stmt* stmt) {
841
839
842
840
for (int i = 0 ; i < cols; i++) {
843
841
int type = sqlite3_column_type (stmt, i);
842
+ const char * name = sqlite3_column_name (stmt, i);
844
843
switch (type) {
845
844
case SQLITE_INTEGER: {
846
- row->push_back (new Values::Integer (i , sqlite3_column_int64 (stmt, i)));
845
+ row->push_back (new Values::Integer (name , sqlite3_column_int64 (stmt, i)));
847
846
} break ;
848
847
case SQLITE_FLOAT: {
849
- row->push_back (new Values::Float (i , sqlite3_column_double (stmt, i)));
848
+ row->push_back (new Values::Float (name , sqlite3_column_double (stmt, i)));
850
849
} break ;
851
850
case SQLITE_TEXT: {
852
851
const char * text = (const char *)sqlite3_column_text (stmt, i);
853
852
int length = sqlite3_column_bytes (stmt, i);
854
- row->push_back (new Values::Text (i , length, text));
853
+ row->push_back (new Values::Text (name , length, text));
855
854
} break ;
856
855
case SQLITE_BLOB: {
857
856
const void * blob = sqlite3_column_blob (stmt, i);
858
857
int length = sqlite3_column_bytes (stmt, i);
859
- row->push_back (new Values::Blob (i , length, blob));
858
+ row->push_back (new Values::Blob (name , length, blob));
860
859
} break ;
861
860
case SQLITE_NULL: {
862
- row->push_back (new Values::Null (i ));
861
+ row->push_back (new Values::Null (name ));
863
862
} break ;
864
863
default :
865
864
assert (false );
0 commit comments