Files
capy-bearer/scripts/test_sqlite_text_native.cpp
T

46 lines
2.1 KiB
C++

#include <cassert>
#include <iostream>
#include "src/lib/types.cpp"
#include "src/lib/dvalue.cpp"
#include "src/lib/functionlib.cpp"
#include "src/lib/sqlite-connector.cpp"
int main()
{
SQLite db;
assert(db.connect(":memory:"));
db.query("CREATE TABLE sample (value TEXT)");
String value = json_decode("\"left\\u0000right ä\"")._String;
db.query("INSERT INTO sample VALUES (:value)", {{"value", value}});
assert(db.error_code == SQLITE_OK);
String sql = "SELECT value, hex(value) AS hex, length(CAST(value AS BLOB)) AS octets FROM sample";
auto row = db.query(sql)[0];
std::cout << sql << "\nSQL HEX=" << row["hex"]._String
<< " octets=" << row["octets"].to_s64() << "\nnative value="
<< json_encode(row["value"]) << " bytes=" << row["value"]._String.size() << std::endl;
assert(row["hex"]._String == "6C65667400726967687420C3A4");
assert(row["octets"].type == 'F' && row["octets"].to_s64() == 13);
bool complete = row["value"]._String == value;
row = db.query("SELECT '' AS empty, 'Grüße 🐻' AS unicode, CAST(X'006100FF' AS BLOB) AS blob, X'' AS empty_blob, NULL AS missing, 9223372036854775807 AS integer, 1.25 AS real")[0];
assert(row["empty"].type == 'S' && row["empty"]._String.empty());
assert(row["unicode"]._String == "Grüße 🐻");
assert(row["blob"]._String == String("\0a\0\xff", 4));
assert(row["empty_blob"].type == 'S' && row["empty_blob"]._String.empty());
assert(!row.has("missing"));
assert(row["integer"].type == 'F' && row["integer"]._float == (f64)9223372036854775807LL);
assert(row["real"].type == 'F' && row["real"]._float == 1.25);
for(const String wire : {"\"\\u0000\"", "\"\\u0000tail\"", "\"tail\\u0000\"", "\"a\\u0000b\\u0000c\"", "\"\"", "\"Grüße 🐻\""})
{
String expected = json_decode(wire)._String;
auto actual = db.query("SELECT :value AS value", {{"value", expected}})[0]["value"];
complete = actual.type == 'S' && actual._String == expected && complete;
}
std::cout << "NULL, empty, Unicode, BLOB, integer and float controls passed\n";
if(!complete)
{
std::cerr << "FAIL SQLite TEXT byte preservation\n";
return 1;
}
std::cout << "PASS SQLite TEXT byte preservation\n";
}