Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/nitro-sqlite-core/cpp/SQLiteQueryResults.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "sqlite/sqlite3.h"
#include <algorithm>
#include <string_view>
#include <unordered_set>

namespace margelo::nitro::nitrosqlite {

Expand Down Expand Up @@ -75,6 +76,10 @@ bool SQLiteQueryResultBuilder::startJSON() {
key.push_back(':');
_keys.push_back(std::move(key));
}
_isLastColumnWithName.resize(_columnNames.size());
std::unordered_set<std::string_view> laterNames;
for (size_t columnIndex = _columnNames.size(); columnIndex-- > 0;)
_isLastColumnWithName[columnIndex] = laterNames.insert(_columnNames[columnIndex]).second;
}

auto& json = _json.emplace().json.stringRepresentation;
Expand Down Expand Up @@ -216,7 +221,7 @@ void SQLiteQueryResultBuilder::appendBufferedValue(size_t rowIndex, size_t colum
void SQLiteQueryResultBuilder::appendPatch(size_t rowIndex, size_t columnIndex, SQLiteValue value) {
_json->json.stringRepresentation.append("null");
// A later column with the same alias wins, including over a BLOB or infinity.
if (std::find(_columnNames.begin() + columnIndex + 1, _columnNames.end(), _columnNames[columnIndex]) == _columnNames.end()) {
if (_isLastColumnWithName[columnIndex]) {
_json->patches.push_back({rowIndex, _columnNames[columnIndex], std::move(value)});
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/nitro-sqlite-core/cpp/SQLiteQueryResults.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ class SQLiteQueryResultBuilder final {
SQLiteExecutionMode _mode;
// Quoted column names followed by ':', built when the first batch switches to JSON.
std::vector<std::string> _keys;
// Whether no later column shares this column's name, built with _keys. JSON keeps the last value of a repeated key.
std::vector<bool> _isLastColumnWithName;
bool _hasMalformedColumnName = false;
std::vector<SQLiteQueryResultRow> _nativeRows;
std::vector<SQLiteCompactResultBlock> _compactBlocks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ int main() {
R"(["toString",true,true,true,"number",5]]])";
expectEqual("direct duplicate aliases", describeRows("SELECT " + duplicates), "[" + duplicateRow + "]");
expectEqual("JSON duplicate aliases", describeRows(repeatRow(duplicates, 7)), repeatJSON(duplicateRow, 7));
const std::string blobFirst = "X'01' AS b, 2 AS b, 3 AS c, 4 AS d, 5 AS e";
const std::string blobFirstRow = R"([true,[["b",true,true,true,"number",2],["c",true,true,true,"number",3],)"
R"(["d",true,true,true,"number",4],["e",true,true,true,"number",5]]])";
expectEqual("JSON duplicate aliases after a BLOB", describeRows(repeatRow(blobFirst, 7)), repeatJSON(blobFirstRow, 7));

expectEqual("ordinary names", describeRows("SELECT 1 AS id, NULL AS name"),
R"([[true,[["id",true,true,true,"number",1],["name",true,true,true,"object",null]]]])");
Expand Down