0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-11-15 20:31:16 +08:00

Fixed global variable

This would have led to multiple definitions at best.
This commit is contained in:
Roland Bock 2021-10-11 10:03:58 +02:00
parent bde010351d
commit 4a8c941916
2 changed files with 87 additions and 47 deletions

View File

@ -68,12 +68,7 @@ namespace sqlpp
} }
}; };
const auto date_digits = std::vector<char>{1, 1, 1, 1, 0, 1, 1, 0, 1, 1}; // 2015-10-28 inline auto check_first_digit(const char* text, bool digitFlag) -> bool
const auto time_digits = std::vector<char>{0, 1, 1, 0, 1, 1, 0, 1, 1}; // T23:00:12
inline auto check_digits(const char* text, const std::vector<char>& digitFlags) -> bool
{
for (const auto digitFlag : digitFlags)
{ {
if (digitFlag) if (digitFlag)
{ {
@ -89,6 +84,26 @@ namespace sqlpp
return false; return false;
} }
} }
return true;
}
inline auto check_date_digits(const char* text) -> bool
{
for (const auto digitFlag : {true, true, true, true, false, true, true, false, true, true}) // YYYY-MM-DD
{
if (not check_first_digit(text, digitFlag))
return false;
++text;
}
return true;
}
inline auto check_time_digits(const char* text) -> bool
{
for (const auto digitFlag : {true, true, false, true, true, false, true, true}) // hh:mm:ss
{
if (not check_first_digit(text, digitFlag))
return false;
++text; ++text;
} }
return true; return true;
@ -206,7 +221,7 @@ namespace sqlpp
if (_handle->debug) if (_handle->debug)
std::cerr << "MySQL debug: date string: " << date_string << std::endl; std::cerr << "MySQL debug: date string: " << date_string << std::endl;
if (detail::check_digits(date_string, detail::date_digits)) if (detail::check_date_digits(date_string))
{ {
const auto ymd = ::date::year(std::atoi(date_string)) / atoi(date_string + 5) / atoi(date_string + 8); const auto ymd = ::date::year(std::atoi(date_string)) / atoi(date_string + 5) / atoi(date_string + 8);
*value = ::sqlpp::chrono::day_point(ymd); *value = ::sqlpp::chrono::day_point(ymd);
@ -235,7 +250,7 @@ namespace sqlpp
if (_handle->debug) if (_handle->debug)
std::cerr << "MySQL debug: date_time string: " << date_time_string << std::endl; std::cerr << "MySQL debug: date_time string: " << date_time_string << std::endl;
if (detail::check_digits(date_time_string, detail::date_digits)) if (detail::check_date_digits(date_time_string))
{ {
const auto ymd = const auto ymd =
::date::year(std::atoi(date_time_string)) / atoi(date_time_string + 5) / atoi(date_time_string + 8); ::date::year(std::atoi(date_time_string)) / atoi(date_time_string + 5) / atoi(date_time_string + 8);
@ -250,18 +265,18 @@ namespace sqlpp
return; return;
} }
const auto time_string = date_time_string + 10; const auto time_string = date_time_string + 11; // YYYY-MM-DDT
if (detail::check_digits(time_string, detail::time_digits)) if (detail::check_time_digits(time_string))
{ {
*value += ::std::chrono::hours(std::atoi(time_string + 1)) + *value += ::std::chrono::hours(std::atoi(time_string + 0)) +
std::chrono::minutes(std::atoi(time_string + 4)) + std::chrono::seconds(std::atoi(time_string + 7)); std::chrono::minutes(std::atoi(time_string + 3)) + std::chrono::seconds(std::atoi(time_string + 6));
} }
else else
{ {
return; return;
} }
const auto mu_string = time_string + 9; const auto mu_string = time_string + 8; // hh:mm:ss
if (mu_string[0] == '\0') if (mu_string[0] == '\0')
{ {
return; return;

View File

@ -45,13 +45,7 @@ namespace sqlpp
{ {
namespace detail namespace detail
{ {
const auto date_digits = std::vector<char>{1, 1, 1, 1, 0, 1, 1, 0, 1, 1}; // 2015-10-28 inline auto check_first_digit(const char* text, bool digitFlag) -> bool
const auto time_digits = std::vector<char>{0, 1, 1, 0, 1, 1, 0, 1, 1}; // T23:00:12
const auto ms_digits = std::vector<char>{0, 1, 1, 1}; // .123
inline auto check_digits(const char* text, const std::vector<char>& digitFlags) -> bool
{
for (const auto digitFlag : digitFlags)
{ {
if (digitFlag) if (digitFlag)
{ {
@ -67,6 +61,37 @@ namespace sqlpp
return false; return false;
} }
} }
return true;
}
inline auto check_date_digits(const char* text) -> bool
{
for (const auto digitFlag : {true, true, true, true, false, true, true, false, true, true}) // YYYY-MM-DD
{
if (not check_first_digit(text, digitFlag))
return false;
++text;
}
return true;
}
inline auto check_time_digits(const char* text) -> bool
{
for (const auto digitFlag : {true, true, false, true, true, false, true, true}) // hh:mm:ss
{
if (not check_first_digit(text, digitFlag))
return false;
++text;
}
return true;
}
inline auto check_ms_digits(const char* text) -> bool
{
for (const auto digitFlag : {true, true, true})
{
if (not check_first_digit(text, digitFlag))
return false;
++text; ++text;
} }
return true; return true;
@ -202,7 +227,7 @@ namespace sqlpp
if (_handle->debug) if (_handle->debug)
std::cerr << "Sqlite3 debug: date string: " << date_string << std::endl; std::cerr << "Sqlite3 debug: date string: " << date_string << std::endl;
if (detail::check_digits(date_string, detail::date_digits)) if (detail::check_date_digits(date_string))
{ {
const auto ymd = ::date::year(std::atoi(date_string)) / atoi(date_string + 5) / atoi(date_string + 8); const auto ymd = ::date::year(std::atoi(date_string)) / atoi(date_string + 5) / atoi(date_string + 8);
*value = ::sqlpp::chrono::day_point(ymd); *value = ::sqlpp::chrono::day_point(ymd);
@ -232,7 +257,7 @@ namespace sqlpp
if (_handle->debug) if (_handle->debug)
std::cerr << "Sqlite3 debug: date_time string: " << date_time_string << std::endl; std::cerr << "Sqlite3 debug: date_time string: " << date_time_string << std::endl;
if (detail::check_digits(date_time_string, detail::date_digits)) if (detail::check_date_digits(date_time_string))
{ {
const auto ymd = const auto ymd =
::date::year(std::atoi(date_time_string)) / atoi(date_time_string + 5) / atoi(date_time_string + 8); ::date::year(std::atoi(date_time_string)) / atoi(date_time_string + 5) / atoi(date_time_string + 8);
@ -247,20 +272,20 @@ namespace sqlpp
return; return;
} }
const auto time_string = date_time_string + 10; const auto time_string = date_time_string + 11; // YYYY-MM-DDT
if (detail::check_digits(time_string, detail::time_digits)) if (detail::check_time_digits(time_string))
{ {
*value += ::std::chrono::hours(std::atoi(time_string + 1)) + *value += ::std::chrono::hours(std::atoi(time_string + 0)) +
std::chrono::minutes(std::atoi(time_string + 4)) + std::chrono::seconds(std::atoi(time_string + 7)); std::chrono::minutes(std::atoi(time_string + 3)) + std::chrono::seconds(std::atoi(time_string + 6));
} }
else else
{ {
return; return;
} }
const auto ms_string = time_string + 9; const auto ms_string = time_string + 9; // hh:mm:ss.
if (detail::check_digits(ms_string, detail::ms_digits) and ms_string[4] == '\0') if (detail::check_ms_digits(ms_string) and ms_string[4] == '\0')
{ {
*value += ::std::chrono::milliseconds(std::atoi(ms_string + 1)); *value += ::std::chrono::milliseconds(std::atoi(ms_string));
} }
else else
{ {