Skip to content

Commit a22b2b2

Browse files
committed
Result object keys are no longer ordered alphabetically, but rather maintain insertion order. The behaviour now matches other libraries.
1 parent c60d3de commit a22b2b2

File tree

2 files changed

+39
-33
lines changed

2 files changed

+39
-33
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

+34-33
Original file line numberDiff line numberDiff line change
@@ -143,45 +143,46 @@ 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
{
151-
jsi::Object rowObject = jsi::Object(rt);
152-
auto row = results->at(i);
153-
for (auto const &entry : row)
154-
{
155-
std::string columnName = entry.first;
156-
QuickValue value = entry.second;
157-
if (value.dataType == TEXT)
158-
{
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)
151+
jsi::Object rowObject = jsi::Object(rt);
152+
auto row = results -> at(i);
153+
// Iterate over metadata to maintain column order
154+
for (const auto & column: * metadata)
167155
{
168-
rowObject.setProperty(rt, columnName.c_str(), jsi::Value(value.doubleOrIntValue));
156+
std::string columnName = column.colunmName;
157+
auto it = row.find(columnName);
158+
if (it != row.end())
159+
{
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 || value.dataType == DOUBLE)
166+
{
167+
rowObject.setProperty(rt, columnName.c_str(), jsi::Value(value.doubleOrIntValue));
168+
} else if (value.dataType == ARRAY_BUFFER)
169+
{
170+
jsi::Function array_buffer_ctor = rt.global().getPropertyAsFunction(rt, "ArrayBuffer");
171+
jsi::Object o = array_buffer_ctor.callAsConstructor(rt, (int) value.arrayBufferSize).getObject(rt);
172+
jsi::ArrayBuffer buf = o.getArrayBuffer(rt);
173+
// 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.
174+
memcpy(buf.data(rt), value.arrayBufferValue.get(), value.arrayBufferSize);
175+
rowObject.setProperty(rt, columnName.c_str(), o);
176+
} else
177+
{
178+
rowObject.setProperty(rt, columnName.c_str(), jsi::Value(nullptr));
179+
}
180+
} else
181+
{
182+
rowObject.setProperty(rt, columnName.c_str(), jsi::Value(nullptr));
183+
}
169184
}
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
180-
{
181-
rowObject.setProperty(rt, columnName.c_str(), jsi::Value(nullptr));
182-
}
183-
}
184-
array.setValueAtIndex(rt, i, move(rowObject));
185+
array.setValueAtIndex(rt, i, move(rowObject));
185186
}
186187
rows.setProperty(rt, "_array", move(array));
187188
res.setProperty(rt, "rows", move(rows));

0 commit comments

Comments
 (0)