Skip to content

Commit 8ce124a

Browse files
committed
fix: properly construct HybridNitroSQLiteQueryResult
1 parent 89e753b commit 8ce124a

1 file changed

Lines changed: 32 additions & 24 deletions

File tree

package/cpp/specs/HybridNitroSQLiteQueryResult.hpp

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,40 @@ namespace margelo::nitro::rnnitrosqlite {
1111
class HybridNitroSQLiteQueryResult : public HybridNitroSQLiteQueryResultSpec {
1212
public:
1313
HybridNitroSQLiteQueryResult() : HybridObject(TAG) {}
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-
});
14+
HybridNitroSQLiteQueryResult(SQLiteQueryResults results,
15+
std::optional<double> insertId,
16+
double rowsAffected,
17+
std::optional<SQLiteQueryTableMetadata> metadata)
18+
: HybridObject(TAG),
19+
_insertId(insertId),
20+
_rowsAffected(rowsAffected),
21+
_results(std::move(results)),
22+
_metadata(metadata) {
23+
if (_results.empty()) {
24+
// Empty rows: empty vector, length 0, item callback always returns null
25+
auto emptyItem = [](double /* idx */) -> std::shared_ptr<Promise<std::optional<SQLiteQueryResultRow>>> {
26+
return Promise<std::optional<SQLiteQueryResultRow>>::async(
27+
[]() -> std::optional<SQLiteQueryResultRow> { return std::nullopt; });
3328
};
34-
35-
const auto length = static_cast<double>(rows.size());
36-
_rows = NitroSQLiteQueryResultRows(std::move(rows), length, itemFunction);
29+
_rows = NitroSQLiteQueryResultRows(SQLiteQueryResults{}, 0.0, std::move(emptyItem));
30+
return;
3731
}
38-
39-
32+
33+
auto rows = _results;
34+
auto itemFunction =
35+
[rows](double idx) -> std::shared_ptr<Promise<std::optional<SQLiteQueryResultRow>>> {
36+
return Promise<std::optional<SQLiteQueryResultRow>>::async(
37+
[rows, idx]() -> std::optional<SQLiteQueryResultRow> {
38+
const auto index = static_cast<size_t>(idx);
39+
if (index >= rows.size()) {
40+
return std::nullopt;
41+
}
42+
return rows[index];
43+
});
44+
};
45+
46+
const auto length = static_cast<double>(rows.size());
47+
_rows = NitroSQLiteQueryResultRows(std::move(rows), length, std::move(itemFunction));
4048
}
4149

4250
private:

0 commit comments

Comments
 (0)