@@ -442,9 +442,7 @@ void Statement::Work_AfterGet(napi_env e, napi_status status, void* data) {
442442 if (!cb.IsUndefined () && cb.IsFunction ()) {
443443 if (stmt->status == SQLITE_ROW) {
444444 // 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 ) };
448446 TRY_CATCH_CALL (stmt->Value (), cb, 2 , argv);
449447 }
450448 else {
@@ -586,23 +584,27 @@ void Statement::Work_AfterAll(napi_env e, napi_status status, void* data) {
586584 // Fire callbacks.
587585 Napi::Function cb = baton->callback .Value ();
588586 if (!cb.IsUndefined () && cb.IsFunction ()) {
589- Napi::Array result (Napi::Array::New (env, baton->rows .size ()));
590-
591587 if (baton->rows .size ()) {
592- std::vector<Napi::String> names;
593- FETCH_COLUMN_NAMES (stmt->_handle , names);
594-
595588 // Create the result array from the data we acquired.
589+ Napi::Array result (Napi::Array::New (env, baton->rows .size ()));
596590 Rows::const_iterator it = baton->rows .begin ();
597591 Rows::const_iterator end = baton->rows .end ();
598592 for (int i = 0 ; it < end; ++it, i++) {
599593 std::unique_ptr<Row> row (*it);
600- result.Set (i, RowToJS (env, row.get (), names ));
594+ ( result) .Set (i, RowToJS (env,row.get ()));
601595 }
602- }
603596
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+ }
606608 }
607609 }
608610
@@ -698,7 +700,6 @@ void Statement::AsyncEach(uv_async_t* handle) {
698700
699701 Napi::Env env = async->stmt ->Env ();
700702 Napi::HandleScope scope (env);
701- Napi::Function cb = async->item_cb .Value ();
702703
703704 while (true ) {
704705 // 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) {
711712 break ;
712713 }
713714
715+ Napi::Function cb = async->item_cb .Value ();
714716 if (!cb.IsUndefined () && cb.IsFunction ()) {
715- if (async->stmt ->columns .size () == 0 ) {
716- FETCH_COLUMN_NAMES (async->stmt ->_handle , async->stmt ->columns );
717- }
718-
719717 Napi::Value argv[2 ];
720718 argv[0 ] = env.Null ();
721719
722720 Rows::const_iterator it = rows.begin ();
723721 Rows::const_iterator end = rows.end ();
724722 for (int i = 0 ; it < end; ++it, i++) {
725723 std::unique_ptr<Row> row (*it);
726- argv[1 ] = RowToJS (env, row.get (), async-> stmt -> columns );
724+ argv[1 ] = RowToJS (env,row.get ());
727725 async->retrieved ++;
728726 TRY_CATCH_CALL (async->stmt ->Value (), cb, 2 , argv);
729727 }
730728 }
731729 }
732730
731+ Napi::Function cb = async->completed_cb .Value ();
733732 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 ()) {
737735 Napi::Value argv[] = {
738736 env.Null (),
739737 Napi::Number::New (env, async->retrieved )
740738 };
741- TRY_CATCH_CALL (async->stmt ->Value (), completed_cb , 2 , argv);
739+ TRY_CATCH_CALL (async->stmt ->Value (), cb , 2 , argv);
742740 }
743741 uv_close (reinterpret_cast <uv_handle_t *>(handle), CloseCallback);
744742 }
@@ -798,7 +796,7 @@ void Statement::Work_AfterReset(napi_env e, napi_status status, void* data) {
798796 STATEMENT_END ();
799797}
800798
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) {
802800 Napi::EscapableHandleScope scope (env);
803801
804802 Napi::Object result = Napi::Object::New (env);
@@ -828,7 +826,7 @@ Napi::Value Statement::RowToJS(Napi::Env env, Row* row, std::vector<Napi::String
828826 } break ;
829827 }
830828
831- result.Set (names[i] , value);
829+ ( result) .Set (Napi::String::New (env, field-> name . c_str ()) , value);
832830
833831 DELETE_FIELD (field);
834832 }
@@ -841,25 +839,26 @@ void Statement::GetRow(Row* row, sqlite3_stmt* stmt) {
841839
842840 for (int i = 0 ; i < cols; i++) {
843841 int type = sqlite3_column_type (stmt, i);
842+ const char * name = sqlite3_column_name (stmt, i);
844843 switch (type) {
845844 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)));
847846 } break ;
848847 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)));
850849 } break ;
851850 case SQLITE_TEXT: {
852851 const char * text = (const char *)sqlite3_column_text (stmt, i);
853852 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));
855854 } break ;
856855 case SQLITE_BLOB: {
857856 const void * blob = sqlite3_column_blob (stmt, i);
858857 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));
860859 } break ;
861860 case SQLITE_NULL: {
862- row->push_back (new Values::Null (i ));
861+ row->push_back (new Values::Null (name ));
863862 } break ;
864863 default :
865864 assert (false );
0 commit comments