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