11#include " HybridNitroSQLite.hpp"
22#include " HybridNativeQueryResult.hpp"
3- #include " ThreadPool.hpp"
43#include " NitroSQLiteException.hpp"
54#include " logs.hpp"
65#include " macros.hpp"
1413
1514namespace margelo ::nitro::rnnitrosqlite {
1615
17- auto pool = std::make_shared<margelo::rnnitrosqlite::ThreadPool>();
18-
1916const std::string getDocPath (const std::optional<std::string>& location) {
2017 std::string tempDocPath = std::string (HybridNitroSQLite::docPath);
2118 if (location) {
@@ -61,23 +58,16 @@ ExecuteQueryResult HybridNitroSQLite::execute(const std::string& dbName, const s
6158 return std::make_shared<HybridNativeQueryResult>(result.insertId , result.rowsAffected , *result.results , *result.metadata );
6259};
6360
64- std::future<ExecuteQueryResult> HybridNitroSQLite::executeAsync (const std::string& dbName, const std::string& query,
65- const std::optional<SQLiteQueryParams>& params) {
66- auto promise = std::make_shared<std::promise<ExecuteQueryResult>>();
67- auto future = promise->get_future ();
68-
69- auto task = [this , promise, dbName, query, params]() {
61+ std::shared_ptr<Promise<std::shared_ptr<HybridNativeQueryResultSpec>>> HybridNitroSQLite::executeAsync (const std::string& dbName, const std::string& query,
62+ const std::optional<SQLiteQueryParams>& params) {
63+ return Promise<std::shared_ptr<HybridNativeQueryResultSpec>>::async ([=, this ]() -> std::shared_ptr<HybridNativeQueryResultSpec> {
7064 try {
7165 auto result = execute (dbName, query, params);
72- promise-> set_value ( result) ;
66+ return result;
7367 } catch (...) {
74- promise-> set_exception ( std::current_exception () );
68+ throw std::current_exception ();
7569 }
76- };
77-
78- pool->queueWork (std::move (task));
79-
80- return future;
70+ });
8171};
8272
8373BatchQueryResult HybridNitroSQLite::executeBatch (const std::string& dbName, const std::vector<BatchQueryCommand>& batchParams) {
@@ -87,46 +77,32 @@ BatchQueryResult HybridNitroSQLite::executeBatch(const std::string& dbName, cons
8777 return BatchQueryResult (result.rowsAffected );
8878};
8979
90- std::future<BatchQueryResult> HybridNitroSQLite::executeBatchAsync (const std::string& dbName,
91- const std::vector<BatchQueryCommand>& batchParams) {
92- auto promise = std::make_shared<std::promise<BatchQueryResult>>();
93- auto future = promise->get_future ();
94-
95- auto task = [this , promise, dbName, batchParams]() {
80+ std::shared_ptr<Promise<BatchQueryResult>> HybridNitroSQLite::executeBatchAsync (const std::string& dbName,
81+ const std::vector<BatchQueryCommand>& batchParams) {
82+ return Promise<BatchQueryResult>::async ([=, this ]() -> BatchQueryResult {
9683 try {
9784 auto result = executeBatch (dbName, batchParams);
98- promise-> set_value ( result) ;
85+ return result;
9986 } catch (...) {
100- promise-> set_exception ( std::current_exception () );
87+ throw std::current_exception ();
10188 }
102- };
103-
104- pool->queueWork (std::move (task));
105-
106- return future;
89+ });
10790};
10891
10992FileLoadResult HybridNitroSQLite::loadFile (const std::string& dbName, const std::string& location) {
11093 const auto result = importSqlFile (dbName, location);
11194 return FileLoadResult (result.commands , result.rowsAffected );
11295};
11396
114- std::future<FileLoadResult> HybridNitroSQLite::loadFileAsync (const std::string& dbName, const std::string& location) {
115- auto promise = std::make_shared<std::promise<FileLoadResult>>();
116- auto future = promise->get_future ();
117-
118- auto task = [this , promise, dbName, location]() {
97+ std::shared_ptr<Promise<FileLoadResult>> HybridNitroSQLite::loadFileAsync (const std::string& dbName, const std::string& location) {
98+ return Promise<FileLoadResult>::async ([=, this ]() -> FileLoadResult {
11999 try {
120100 auto result = loadFile (dbName, location);
121- promise-> set_value ( result) ;
101+ return result;
122102 } catch (...) {
123- promise-> set_exception ( std::current_exception () );
103+ throw std::current_exception ();
124104 }
125- };
126-
127- pool->queueWork (std::move (task));
128-
129- return future;
105+ });
130106};
131107
132108} // namespace margelo::nitro::rnnitrosqlite
0 commit comments