Skip to content

Commit 58ebc85

Browse files
committed
fix: c++ hybrid objects not constructed properly
1 parent 3cd301f commit 58ebc85

7 files changed

Lines changed: 48 additions & 47 deletions

File tree

package/cpp/operations.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <sqlite3.h>
1212
#include <sstream>
1313
#include <unistd.h>
14+
#include "specs/HybridNitroSQLiteQueryResult.hpp"
1415

1516
using namespace facebook;
1617
using namespace margelo::nitro;
@@ -121,7 +122,7 @@ void bindStatement(sqlite3_stmt* statement, const SQLiteQueryParams& values) {
121122
}
122123
}
123124

124-
SQLiteExecuteQueryResult sqliteExecute(const std::string& dbName, const std::string& query,
125+
std::shared_ptr<HybridNitroSQLiteQueryResult> sqliteExecute(const std::string& dbName, const std::string& query,
125126
const std::optional<SQLiteQueryParams>& params) {
126127
if (dbMap.count(dbName) == 0) {
127128
throw NitroSQLiteException::DatabaseNotOpen(dbName);
@@ -229,10 +230,7 @@ SQLiteExecuteQueryResult sqliteExecute(const std::string& dbName, const std::str
229230

230231
int rowsAffected = sqlite3_changes(db);
231232
long long latestInsertRowId = sqlite3_last_insert_rowid(db);
232-
return {.rowsAffected = rowsAffected,
233-
.insertId = static_cast<double>(latestInsertRowId),
234-
.results = std::move(results),
235-
.metadata = std::move(metadata)};
233+
return std::make_shared<HybridNitroSQLiteQueryResult>(results, static_cast<double>(latestInsertRowId), rowsAffected, metadata);
236234
}
237235

238236
SQLiteOperationResult sqliteExecuteLiteral(const std::string& dbName, const std::string& query) {
@@ -288,3 +286,4 @@ SQLiteOperationResult sqliteExecuteLiteral(const std::string& dbName, const std:
288286
}
289287

290288
} // namespace margelo::rnnitrosqlite
289+

package/cpp/operations.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include "types.hpp"
4+
#include "specs/HybridNitroSQLiteQueryResult.hpp"
45

56
namespace margelo::rnnitrosqlite {
67

@@ -15,7 +16,7 @@ void sqliteAttachDb(const std::string& mainDBName, const std::string& docPath, c
1516

1617
void sqliteDetachDb(const std::string& mainDBName, const std::string& alias);
1718

18-
SQLiteExecuteQueryResult sqliteExecute(const std::string& dbName, const std::string& query, const std::optional<SQLiteQueryParams>& params);
19+
std::shared_ptr<HybridNitroSQLiteQueryResult> sqliteExecute(const std::string& dbName, const std::string& query, const std::optional<SQLiteQueryParams>& params);
1920

2021
SQLiteOperationResult sqliteExecuteLiteral(const std::string& dbName, const std::string& query);
2122

package/cpp/specs/HybridNitroSQLite.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <map>
1111
#include <string>
1212
#include <vector>
13+
#include "HybridNitroSQLiteQueryResult.hpp"
1314

1415
namespace margelo::nitro::rnnitrosqlite {
1516

@@ -50,12 +51,9 @@ void HybridNitroSQLite::detach(const std::string& mainDbName, const std::string&
5051
sqliteDetachDb(mainDbName, alias);
5152
};
5253

53-
using ExecuteQueryResult = std::shared_ptr<HybridNitroSQLiteQueryResultSpec>;
54-
55-
ExecuteQueryResult HybridNitroSQLite::execute(const std::string& dbName, const std::string& query,
54+
std::shared_ptr<HybridNitroSQLiteQueryResultSpec> HybridNitroSQLite::execute(const std::string& dbName, const std::string& query,
5655
const std::optional<SQLiteQueryParams>& params) {
57-
SQLiteExecuteQueryResult result = sqliteExecute(dbName, query, params);
58-
return std::make_shared<HybridNitroSQLiteQueryResult>(std::move(result));
56+
return sqliteExecute(dbName, query, params);
5957
};
6058

6159
std::shared_ptr<Promise<std::shared_ptr<HybridNitroSQLiteQueryResultSpec>>>

package/cpp/specs/HybridNitroSQLiteQueryResult.cpp

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,41 +9,23 @@
99
namespace margelo::nitro::rnnitrosqlite {
1010

1111
std::optional<double> HybridNitroSQLiteQueryResult::getInsertId() {
12-
return _result.insertId;
12+
return _insertId;
1313
}
1414

1515
double HybridNitroSQLiteQueryResult::getRowsAffected() {
16-
return _result.rowsAffected;
16+
return _rowsAffected;
1717
}
1818

1919
SQLiteQueryResults HybridNitroSQLiteQueryResult::getResults() {
20-
return _result.results;
20+
return _results;
2121
};
2222

2323
std::optional<NitroSQLiteQueryResultRows> HybridNitroSQLiteQueryResult::getRows() {
24-
if (_result.results.empty()) {
25-
return std::nullopt;
26-
}
27-
28-
auto rows = _result.results;
29-
30-
// Create the item function that returns a Promise
31-
auto itemFunction = [rows](double idx) -> std::shared_ptr<Promise<std::optional<SQLiteQueryResultRow>>> {
32-
return Promise<std::optional<SQLiteQueryResultRow>>::async([rows, idx]() -> std::optional<SQLiteQueryResultRow> {
33-
const auto index = static_cast<size_t>(idx);
34-
if (index >= rows.size()) {
35-
return std::nullopt;
36-
}
37-
return rows[index];
38-
});
39-
};
40-
41-
const auto length = static_cast<double>(rows.size());
42-
return NitroSQLiteQueryResultRows(std::move(rows), length, itemFunction);
24+
return _rows;
4325
}
4426

4527
std::optional<SQLiteQueryTableMetadata> HybridNitroSQLiteQueryResult::getMetadata() {
46-
return _result.metadata;
28+
return _metadata;
4729
}
4830

4931
} // namespace margelo::nitro::rnnitrosqlite

package/cpp/specs/HybridNitroSQLiteQueryResult.hpp

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,40 @@ namespace margelo::nitro::rnnitrosqlite {
1111
class HybridNitroSQLiteQueryResult : public HybridNitroSQLiteQueryResultSpec {
1212
public:
1313
HybridNitroSQLiteQueryResult() : HybridObject(TAG) {}
14-
HybridNitroSQLiteQueryResult(SQLiteExecuteQueryResult&& result) : HybridObject(TAG), _result(std::move(result)) {}
14+
HybridNitroSQLiteQueryResult(SQLiteQueryResults results, std::optional<double> insertId, double rowsAffected, std::optional<SQLiteQueryTableMetadata> metadata) {
15+
HybridNitroSQLiteQueryResult();
16+
17+
_results = results;
18+
_insertId = insertId;
19+
_metadata = metadata;
20+
21+
if (!_results.empty()) {
22+
auto rows = _results;
23+
24+
// Create the item function that returns a Promise
25+
auto itemFunction = [rows](double idx) -> std::shared_ptr<Promise<std::optional<SQLiteQueryResultRow>>> {
26+
return Promise<std::optional<SQLiteQueryResultRow>>::async([rows, idx]() -> std::optional<SQLiteQueryResultRow> {
27+
const auto index = static_cast<size_t>(idx);
28+
if (index >= rows.size()) {
29+
return std::nullopt;
30+
}
31+
return rows[index];
32+
});
33+
};
34+
35+
const auto length = static_cast<double>(rows.size());
36+
_rows = NitroSQLiteQueryResultRows(std::move(rows), length, itemFunction);
37+
}
38+
39+
40+
}
1541

1642
private:
17-
SQLiteExecuteQueryResult _result;
43+
std::optional<double> _insertId;
44+
double _rowsAffected;
45+
SQLiteQueryResults _results;
46+
std::optional<NitroSQLiteQueryResultRows> _rows;
47+
std::optional<SQLiteQueryTableMetadata>_metadata;
1848

1949
public:
2050
// Properties

package/cpp/sqliteExecuteBatch.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ SQLiteOperationResult sqliteExecuteBatch(const std::string& dbName, const std::v
4949
auto metadata = std::optional<SQLiteQueryTableMetadata>(std::nullopt);
5050
try {
5151
auto result = sqliteExecute(dbName, command.sql, command.params);
52-
rowsAffected += result.rowsAffected;
52+
rowsAffected += result->getRowsAffected();
5353
} catch (NitroSQLiteException& e) {
5454
sqliteExecuteLiteral(dbName, "ROLLBACK");
5555
throw e;

package/cpp/types.hpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,7 @@ using SQLiteQueryTableMetadata = std::unordered_map<std::string, NitroSQLiteQuer
1818

1919
struct SQLiteOperationResult {
2020
int rowsAffected;
21-
double insertId;
22-
int commands;
23-
};
24-
25-
struct SQLiteExecuteQueryResult {
26-
int rowsAffected;
27-
double insertId;
28-
29-
SQLiteQueryResults results;
30-
std::optional<SQLiteQueryTableMetadata> metadata;
21+
int commands = 0;
3122
};
3223

3324
// constexpr function that maps SQLiteColumnType to string literals

0 commit comments

Comments
 (0)