Skip to content

Commit d5c00a4

Browse files
committed
LibDatabase+RequestServer: Store ByteString data as a BLOB
We currently store ByteString as TEXT, but that is interpreted by SQLite as UTF-8. All string-related operations will try to operate on the text as if it is stored with UTF-8. We should use BLOB instead, which does not make any encoding assumptions. The only user of ByteString in the database currently is the HTTP disk cache. So we bump its version here to remove existing data. Again, we will want to handle changes like this more gracefully eventually.
1 parent 1c2c9a8 commit d5c00a4

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

Libraries/LibDatabase/Database.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,11 @@ void Database::apply_placeholder(StatementID statement_id, int index, ValueType
119119
{
120120
auto* statement = prepared_statement(statement_id);
121121

122-
if constexpr (IsOneOf<ValueType, String, ByteString>) {
122+
if constexpr (IsSame<ValueType, String>) {
123123
StringView string { value };
124124
SQL_MUST(sqlite3_bind_text(statement, index, string.characters_without_null_termination(), static_cast<int>(string.length()), SQLITE_TRANSIENT));
125+
} else if constexpr (IsSame<ValueType, ByteString>) {
126+
SQL_MUST(sqlite3_bind_blob(statement, index, value.characters(), static_cast<int>(value.length()), SQLITE_TRANSIENT));
125127
} else if constexpr (IsSame<ValueType, UnixDateTime>) {
126128
apply_placeholder(statement_id, index, value.offset_to_epoch().to_milliseconds());
127129
} else if constexpr (IsIntegral<ValueType>) {
@@ -148,8 +150,9 @@ ValueType Database::result_column(StatementID statement_id, int column)
148150
auto const* text = reinterpret_cast<char const*>(sqlite3_column_text(statement, column));
149151
return MUST(String::from_utf8(StringView { text, strlen(text) }));
150152
} else if constexpr (IsSame<ValueType, ByteString>) {
151-
auto const* text = reinterpret_cast<char const*>(sqlite3_column_text(statement, column));
152-
return ByteString { text, strlen(text) };
153+
auto length = sqlite3_column_bytes(statement, column);
154+
auto const* text = sqlite3_column_blob(statement, column);
155+
return ByteString { reinterpret_cast<char const*>(text), static_cast<size_t>(length) };
153156
} else if constexpr (IsSame<ValueType, UnixDateTime>) {
154157
auto milliseconds = result_column<sqlite3_int64>(statement_id, column);
155158
return UnixDateTime::from_milliseconds_since_epoch(milliseconds);

Services/RequestServer/Cache/Version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111
namespace RequestServer {
1212

1313
// Increment this version when a breaking change is made to the cache index or cache entry formats.
14-
static constexpr inline u32 CACHE_VERSION = 2u;
14+
static constexpr inline u32 CACHE_VERSION = 3u;
1515

1616
}

0 commit comments

Comments
 (0)