Skip to content
Merged
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
1 change: 0 additions & 1 deletion src/internal/string_utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,5 @@ class REFLECTION_EXPORT StringUtilities {
static std::wstring FromUtf8(const char* utf8_string, size_t byte_count);

static std::string Join(const std::vector<std::string>& list, const std::string& separator);
static std::string Join(const std::vector<std::string>& list, char c);
};
} // namespace sqlite_reflection
14 changes: 10 additions & 4 deletions src/queries.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <string.h>

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iterator>
#include <stdexcept>
Expand Down Expand Up @@ -374,10 +375,15 @@ std::wstring FetchRecordsQuery::GetColumnValue(const int col) const {
const int col_type = sqlite3_column_type(stmt_, col);
switch (col_type) {
case SQLITE_INTEGER:
return std::to_wstring(sqlite3_column_int(stmt_, col));

case SQLITE_FLOAT:
return std::to_wstring(sqlite3_column_double(stmt_, col));
return std::to_wstring(sqlite3_column_int64(stmt_, col));

case SQLITE_FLOAT: {
// %.17g round-trips any double exactly; to_wstring's fixed 6-decimal
// formatting would silently truncate precision here
char buffer[64];
std::snprintf(buffer, sizeof(buffer), "%.17g", sqlite3_column_double(stmt_, col));
return StringUtilities::FromUtf8(buffer, std::strlen(buffer));
}

case SQLITE_TEXT: {
const auto content = reinterpret_cast<const char*>(sqlite3_column_text(stmt_, col));
Expand Down
5 changes: 0 additions & 5 deletions src/string_utilities.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

#include <codecvt>
#include <numeric>
#include <sstream>
#ifndef _WIN32
#include <locale>
#endif
Expand Down Expand Up @@ -86,7 +85,3 @@ std::string StringUtilities::Join(const std::vector<std::string>& list, const st
return std::accumulate(list.begin() + 1, list.end(), list[0],
[&](const std::string& a, const std::string& b) { return a + separator + b; });
}

std::string StringUtilities::Join(const std::vector<std::string>& list, char c) {
return Join(list, std::string(1, c));
}
42 changes: 42 additions & 0 deletions tests/database_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,48 @@ TEST_F(DatabaseTest, FetchWithPredicateChaining) {
EXPECT_EQ(37, fetched_persons[1].age);
}

TEST_F(DatabaseTest, FetchPreservesInt64ValuesBeyondInt32Range) {
const auto db = Database::Instance();

// Values above INT32_MAX must survive a round-trip; sqlite3_column_int would
// truncate/wrap these to 32 bits
const int64_t above_int32_max = 5000000000LL;
const int64_t near_int64_max = 9000000000000000000LL;

std::vector<Company> company;
company.push_back({L"Big Corp", above_int32_max, L"Nowhere", 1.0, 1});
company.push_back({L"Huge Corp", near_int64_max, L"Nowhere", 1.0, 2});

db->Save(company);

const auto fetched_first = db->Fetch<Company>(1);
EXPECT_EQ(above_int32_max, fetched_first.age);

const auto fetched_second = db->Fetch<Company>(2);
EXPECT_EQ(near_int64_max, fetched_second.age);
}

TEST_F(DatabaseTest, FetchPreservesHighPrecisionDoubleValues) {
const auto db = Database::Instance();

// std::to_wstring(double) formats with a fixed 6 decimal places, which would
// silently drop precision here
const double high_precision = 0.12345678901234567;
const double large_magnitude = 123456789012345.67;

std::vector<Company> company;
company.push_back({L"Precision Inc", 1, L"Nowhere", high_precision, 1});
company.push_back({L"Large Corp", 2, L"Nowhere", large_magnitude, 2});

db->Save(company);

const auto fetched_first = db->Fetch<Company>(1);
EXPECT_DOUBLE_EQ(high_precision, fetched_first.salary);

const auto fetched_second = db->Fetch<Company>(2);
EXPECT_DOUBLE_EQ(large_magnitude, fetched_second.salary);
}

TEST_F(DatabaseTest, RawSqlQueryForPersistedRecord) {
const auto db = Database::Instance();

Expand Down
Loading