Skip to content

Commit 85bdd5c

Browse files
committed
feat: removing custom null type from C++ code
1 parent afd1f5c commit 85bdd5c

9 files changed

+81
-52
lines changed

package/cpp/operations.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ void bindStatement(sqlite3_stmt* statement, const SQLiteQueryParams& values) {
105105
for (int valueIndex = 0; valueIndex < values.size(); valueIndex++) {
106106
int sqliteIndex = valueIndex + 1;
107107
SQLiteValue value = values.at(valueIndex);
108-
if (std::holds_alternative<SQLiteNullValue>(value)) {
108+
if (std::holds_alternative<NullType>(value)) {
109109
sqlite3_bind_null(statement, sqliteIndex);
110110
} else if (std::holds_alternative<bool>(value)) {
111111
sqlite3_bind_int(statement, sqliteIndex, std::get<bool>(value));
@@ -191,7 +191,7 @@ SQLiteExecuteQueryResult sqliteExecute(const std::string& dbName, const std::str
191191
case SQLITE_NULL:
192192
// Intentionally left blank to switch to default case
193193
default:
194-
row[column_name] = SQLiteNullValue(true);
194+
row[column_name] = NullType::null;
195195
break;
196196
}
197197
i++;
@@ -205,7 +205,7 @@ SQLiteExecuteQueryResult sqliteExecute(const std::string& dbName, const std::str
205205
column_name = sqlite3_column_name(statement, i);
206206
const char* tp = sqlite3_column_decltype(statement, i);
207207
column_declared_type = mapSQLiteTypeToColumnType(tp);
208-
auto columnMeta = SQLiteQueryColumnMetadata(std::move(column_name), std::move(column_declared_type), i);
208+
auto columnMeta = NitroSQLiteQueryColumnMetadata(std::move(column_name), std::move(column_declared_type), i);
209209

210210
if (!metadata) {
211211
metadata = std::make_optional<SQLiteQueryTableMetadata>();

package/cpp/specs/HybridNativeQueryResult.cpp

Lines changed: 0 additions & 21 deletions
This file was deleted.

package/cpp/specs/HybridNitroSQLite.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include "HybridNitroSQLite.hpp"
2-
#include "HybridNativeQueryResult.hpp"
2+
#include "HybridNitroSQLiteQueryResult.hpp"
33
#include "NitroSQLiteException.hpp"
44
#include "importSqlFile.hpp"
55
#include "logs.hpp"
@@ -50,31 +50,32 @@ void HybridNitroSQLite::detach(const std::string& mainDbName, const std::string&
5050
sqliteDetachDb(mainDbName, alias);
5151
};
5252

53-
using ExecuteQueryResult = std::shared_ptr<HybridNativeQueryResultSpec>;
53+
using ExecuteQueryResult = std::shared_ptr<HybridNitroSQLiteQueryResultSpec>;
5454

5555
ExecuteQueryResult HybridNitroSQLite::execute(const std::string& dbName, const std::string& query,
5656
const std::optional<SQLiteQueryParams>& params) {
5757
SQLiteExecuteQueryResult result = sqliteExecute(dbName, query, params);
58-
return std::make_shared<HybridNativeQueryResult>(std::move(result));
58+
return std::make_shared<HybridNitroSQLiteQueryResult>(std::move(result));
5959
};
6060

61-
std::shared_ptr<Promise<std::shared_ptr<HybridNativeQueryResultSpec>>>
61+
std::shared_ptr<Promise<std::shared_ptr<HybridNitroSQLiteQueryResultSpec>>>
6262
HybridNitroSQLite::executeAsync(const std::string& dbName, const std::string& query, const std::optional<SQLiteQueryParams>& params) {
63-
return Promise<std::shared_ptr<HybridNativeQueryResultSpec>>::async([=, this]() -> std::shared_ptr<HybridNativeQueryResultSpec> {
64-
auto result = execute(dbName, query, params);
65-
return result;
66-
});
63+
return Promise<std::shared_ptr<HybridNitroSQLiteQueryResultSpec>>::async(
64+
[=, this]() -> std::shared_ptr<HybridNitroSQLiteQueryResultSpec> {
65+
auto result = execute(dbName, query, params);
66+
return result;
67+
});
6768
};
6869

69-
BatchQueryResult HybridNitroSQLite::executeBatch(const std::string& dbName, const std::vector<NativeBatchQueryCommand>& batchParams) {
70+
BatchQueryResult HybridNitroSQLite::executeBatch(const std::string& dbName, const std::vector<BatchQueryCommand>& batchParams) {
7071
const auto commands = batchParamsToCommands(batchParams);
7172

7273
auto result = sqliteExecuteBatch(dbName, commands);
7374
return BatchQueryResult(result.rowsAffected);
7475
};
7576

7677
std::shared_ptr<Promise<BatchQueryResult>> HybridNitroSQLite::executeBatchAsync(const std::string& dbName,
77-
const std::vector<NativeBatchQueryCommand>& batchParams) {
78+
const std::vector<BatchQueryCommand>& batchParams) {
7879
return Promise<BatchQueryResult>::async([=, this]() -> BatchQueryResult {
7980
auto result = executeBatch(dbName, batchParams);
8081
return result;

package/cpp/specs/HybridNitroSQLite.hpp

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

3-
#include "HybridNativeQueryResultSpec.hpp"
3+
#include "HybridNitroSQLiteQueryResultSpec.hpp"
44
#include "HybridNitroSQLiteSpec.hpp"
55
#include "types.hpp"
66

@@ -28,15 +28,15 @@ class HybridNitroSQLite : public HybridNitroSQLiteSpec {
2828

2929
void detach(const std::string& mainDbName, const std::string& alias) override;
3030

31-
std::shared_ptr<HybridNativeQueryResultSpec> execute(const std::string& dbName, const std::string& query,
32-
const std::optional<SQLiteQueryParams>& params) override;
31+
std::shared_ptr<HybridNitroSQLiteQueryResultSpec> execute(const std::string& dbName, const std::string& query,
32+
const std::optional<SQLiteQueryParams>& params) override;
3333

34-
std::shared_ptr<Promise<std::shared_ptr<HybridNativeQueryResultSpec>>>
34+
std::shared_ptr<Promise<std::shared_ptr<HybridNitroSQLiteQueryResultSpec>>>
3535
executeAsync(const std::string& dbName, const std::string& query, const std::optional<SQLiteQueryParams>& params) override;
3636

37-
BatchQueryResult executeBatch(const std::string& dbName, const std::vector<NativeBatchQueryCommand>& commands) override;
37+
BatchQueryResult executeBatch(const std::string& dbName, const std::vector<BatchQueryCommand>& commands) override;
3838
std::shared_ptr<Promise<BatchQueryResult>> executeBatchAsync(const std::string& dbName,
39-
const std::vector<NativeBatchQueryCommand>& commands) override;
39+
const std::vector<BatchQueryCommand>& commands) override;
4040

4141
FileLoadResult loadFile(const std::string& dbName, const std::string& location) override;
4242
std::shared_ptr<Promise<FileLoadResult>> loadFileAsync(const std::string& dbName, const std::string& location) override;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include "HybridNitroSQLiteQueryResult.hpp"
2+
#include <NitroModules/ArrayBuffer.hpp>
3+
#include <NitroModules/Null.hpp>
4+
#include <NitroModules/Promise.hpp>
5+
#include <unordered_map>
6+
#include <variant>
7+
#include <vector>
8+
9+
namespace margelo::nitro::rnnitrosqlite {
10+
11+
std::optional<double> HybridNitroSQLiteQueryResult::getInsertId() {
12+
return _result.insertId;
13+
}
14+
15+
double HybridNitroSQLiteQueryResult::getRowsAffected() {
16+
return _result.rowsAffected;
17+
}
18+
19+
SQLiteQueryResults HybridNitroSQLiteQueryResult::getResults() {
20+
return _result.results;
21+
};
22+
23+
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);
43+
}
44+
45+
std::optional<SQLiteQueryTableMetadata> HybridNitroSQLiteQueryResult::getMetadata() {
46+
return _result.metadata;
47+
}
48+
49+
} // namespace margelo::nitro::rnnitrosqlite

package/cpp/specs/HybridNativeQueryResult.hpp renamed to package/cpp/specs/HybridNitroSQLiteQueryResult.hpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
#pragma once
22

3-
#include "HybridNativeQueryResultSpec.hpp"
4-
#include "types.hpp"
53
#include <map>
4+
#include "HybridNitroSQLiteQueryResultSpec.hpp"
5+
#include "types.hpp"
66

77
using namespace margelo::rnnitrosqlite;
88

99
namespace margelo::nitro::rnnitrosqlite {
1010

11-
class HybridNativeQueryResult : public HybridNativeQueryResultSpec {
11+
class HybridNitroSQLiteQueryResult : public HybridNitroSQLiteQueryResultSpec {
1212
public:
13-
HybridNativeQueryResult() : HybridObject(TAG) {}
14-
HybridNativeQueryResult(SQLiteExecuteQueryResult&& result) : HybridObject(TAG), _result(std::move(result)) {}
13+
HybridNitroSQLiteQueryResult() : HybridObject(TAG) {}
14+
HybridNitroSQLiteQueryResult(SQLiteExecuteQueryResult&& result) : HybridObject(TAG), _result(std::move(result)) {}
1515

1616
private:
1717
SQLiteExecuteQueryResult _result;
@@ -21,6 +21,7 @@ class HybridNativeQueryResult : public HybridNativeQueryResultSpec {
2121
std::optional<double> getInsertId() override;
2222
double getRowsAffected() override;
2323
SQLiteQueryResults getResults() override;
24+
std::optional<NitroSQLiteQueryResultRows> getRows() override;
2425
std::optional<SQLiteQueryTableMetadata> getMetadata() override;
2526
};
2627

package/cpp/sqliteExecuteBatch.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace margelo::rnnitrosqlite {
1010

11-
std::vector<BatchQuery> batchParamsToCommands(const std::vector<NativeBatchQueryCommand>& batchParams) {
11+
std::vector<BatchQuery> batchParamsToCommands(const std::vector<BatchQueryCommand>& batchParams) {
1212
auto commands = std::vector<BatchQuery>();
1313

1414
for (auto& command : batchParams) {

package/cpp/sqliteExecuteBatch.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44
#pragma once
55

6-
#include "NativeBatchQueryCommand.hpp"
6+
#include "BatchQueryCommand.hpp"
77
#include "types.hpp"
88

99
using namespace facebook;
@@ -20,7 +20,7 @@ struct BatchQuery {
2020
* Local Helper method to translate JSI objects BatchQuery datastructure
2121
* MUST be called in the JavaScript Thread
2222
*/
23-
std::vector<BatchQuery> batchParamsToCommands(const std::vector<NativeBatchQueryCommand>& batchParams);
23+
std::vector<BatchQuery> batchParamsToCommands(const std::vector<BatchQueryCommand>& batchParams);
2424

2525
/**
2626
* Execute a batch of commands in a exclusive transaction

package/cpp/types.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#pragma once
22

33
#include "ColumnType.hpp"
4-
#include "SQLiteNullValue.hpp"
5-
#include "SQLiteQueryColumnMetadata.hpp"
4+
#include "NitroSQLiteQueryColumnMetadata.hpp"
65
#include <NitroModules/ArrayBuffer.hpp>
76
#include <string>
87

@@ -11,11 +10,11 @@ using namespace margelo::nitro::rnnitrosqlite;
1110

1211
namespace margelo::rnnitrosqlite {
1312

14-
using SQLiteValue = std::variant<std::string, double, bool, std::shared_ptr<ArrayBuffer>, SQLiteNullValue>;
13+
using SQLiteValue = std::variant<nitro::NullType, bool, std::shared_ptr<ArrayBuffer>, std::string, double>;
1514
using SQLiteQueryParams = std::vector<SQLiteValue>;
1615
using SQLiteQueryResultRow = std::unordered_map<std::string, SQLiteValue>;
1716
using SQLiteQueryResults = std::vector<SQLiteQueryResultRow>;
18-
using SQLiteQueryTableMetadata = std::unordered_map<std::string, SQLiteQueryColumnMetadata>;
17+
using SQLiteQueryTableMetadata = std::unordered_map<std::string, NitroSQLiteQueryColumnMetadata>;
1918

2019
struct SQLiteOperationResult {
2120
int rowsAffected;

0 commit comments

Comments
 (0)