Skip to content

Commit 9ac4ce7

Browse files
authored
Result key order is now sorted by insertion time and not alphabetical (#60)
* Result object keys are no longer ordered alphabetically, but rather maintain insertion order. The behaviour now matches other libraries. * Formatting. * Fixed columnName typo.
1 parent c60d3de commit 9ac4ce7

File tree

4 files changed

+39
-30
lines changed

4 files changed

+39
-30
lines changed

.changeset/selfish-worms-buy.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@journeyapps/react-native-quick-sqlite": patch
3+
---
4+
5+
Result object keys are no longer ordered alphabetically, but rather maintain insertion order. The behaviour now matches other libraries.

cpp/JSIHelper.cpp

+32-28
Original file line numberDiff line numberDiff line change
@@ -143,40 +143,44 @@ jsi::Value createSequelQueryExecutionResult(jsi::Runtime &rt, SQLiteOPResult sta
143143
// Converting row results into objects
144144
size_t rowCount = results->size();
145145
jsi::Object rows = jsi::Object(rt);
146-
if (rowCount > 0)
146+
if (rowCount > 0 && metadata != NULL)
147147
{
148148
auto array = jsi::Array(rt, rowCount);
149149
for (int i = 0; i < rowCount; i++)
150150
{
151151
jsi::Object rowObject = jsi::Object(rt);
152-
auto row = results->at(i);
153-
for (auto const &entry : row)
152+
auto row = results -> at(i);
153+
// Iterate over metadata to maintain column order
154+
for (const auto &column : *metadata)
154155
{
155-
std::string columnName = entry.first;
156-
QuickValue value = entry.second;
157-
if (value.dataType == TEXT)
156+
std::string columnName = column.columnName;
157+
auto it = row.find(columnName);
158+
if (it != row.end())
158159
{
159-
// using value.textValue (std::string) directly allows jsi::String to use length property of std::string (allowing strings with NULLs in them like SQLite does)
160-
rowObject.setProperty(rt, columnName.c_str(), jsi::String::createFromUtf8(rt, value.textValue));
161-
}
162-
else if (value.dataType == INTEGER)
163-
{
164-
rowObject.setProperty(rt, columnName.c_str(), jsi::Value(value.doubleOrIntValue));
165-
}
166-
else if (value.dataType == DOUBLE)
167-
{
168-
rowObject.setProperty(rt, columnName.c_str(), jsi::Value(value.doubleOrIntValue));
169-
}
170-
else if (value.dataType == ARRAY_BUFFER)
171-
{
172-
jsi::Function array_buffer_ctor = rt.global().getPropertyAsFunction(rt, "ArrayBuffer");
173-
jsi::Object o = array_buffer_ctor.callAsConstructor(rt, (int)value.arrayBufferSize).getObject(rt);
174-
jsi::ArrayBuffer buf = o.getArrayBuffer(rt);
175-
// It's a shame we have to copy here: see https://github.com/facebook/hermes/pull/419 and https://github.com/facebook/hermes/issues/564.
176-
memcpy(buf.data(rt), value.arrayBufferValue.get(), value.arrayBufferSize);
177-
rowObject.setProperty(rt, columnName.c_str(), o);
178-
}
179-
else
160+
QuickValue value = it -> second;
161+
if (value.dataType == TEXT)
162+
{
163+
// using value.textValue (std::string) directly allows jsi::String to use length property of std::string (allowing strings with NULLs in them like SQLite does)
164+
rowObject.setProperty(rt, columnName.c_str(), jsi::String::createFromUtf8(rt, value.textValue));
165+
} else if (value.dataType == INTEGER)
166+
{
167+
rowObject.setProperty(rt, columnName.c_str(), jsi::Value(value.doubleOrIntValue));
168+
} else if (value.dataType == DOUBLE)
169+
{
170+
rowObject.setProperty(rt, columnName.c_str(), jsi::Value(value.doubleOrIntValue));
171+
} else if (value.dataType == ARRAY_BUFFER)
172+
{
173+
jsi::Function array_buffer_ctor = rt.global().getPropertyAsFunction(rt, "ArrayBuffer");
174+
jsi::Object o = array_buffer_ctor.callAsConstructor(rt, (int) value.arrayBufferSize).getObject(rt);
175+
jsi::ArrayBuffer buf = o.getArrayBuffer(rt);
176+
// It's a shame we have to copy here: see https://github.com/facebook/hermes/pull/419 and https://github.com/facebook/hermes/issues/564.
177+
memcpy(buf.data(rt), value.arrayBufferValue.get(), value.arrayBufferSize);
178+
rowObject.setProperty(rt, columnName.c_str(), o);
179+
} else
180+
{
181+
rowObject.setProperty(rt, columnName.c_str(), jsi::Value(nullptr));
182+
}
183+
} else
180184
{
181185
rowObject.setProperty(rt, columnName.c_str(), jsi::Value(nullptr));
182186
}
@@ -194,7 +198,7 @@ jsi::Value createSequelQueryExecutionResult(jsi::Runtime &rt, SQLiteOPResult sta
194198
for (int i = 0; i < column_count; i++) {
195199
auto column = metadata->at(i);
196200
jsi::Object column_object = jsi::Object(rt);
197-
column_object.setProperty(rt, "columnName", jsi::String::createFromUtf8(rt, column.colunmName.c_str()));
201+
column_object.setProperty(rt, "columnName", jsi::String::createFromUtf8(rt, column.columnName.c_str()));
198202
column_object.setProperty(rt, "columnDeclaredType", jsi::String::createFromUtf8(rt, column.columnDeclaredType.c_str()));
199203
column_object.setProperty(rt, "columnIndex", jsi::Value(column.columnIndex));
200204
column_array.setValueAtIndex(rt, i, move(column_object));

cpp/JSIHelper.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ struct SequelBatchOperationResult
9191
*/
9292
struct QuickColumnMetadata
9393
{
94-
string colunmName;
94+
string columnName;
9595
int columnIndex;
9696
string columnDeclaredType;
9797
};

cpp/sqliteExecute.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ sqliteExecuteWithDB(sqlite3 *db, std::string const &query,
139139
const char *tp = sqlite3_column_decltype(statement, i);
140140
column_declared_type = tp != NULL ? tp : "UNKNOWN";
141141
QuickColumnMetadata meta = {
142-
.colunmName = column_name,
142+
.columnName = column_name,
143143
.columnIndex = i,
144144
.columnDeclaredType = column_declared_type,
145145
};

0 commit comments

Comments
 (0)