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

#471 serializers: ensure float precision (#472)

* value: add missing includes
* mysql, sqlite3: use explicitly *out* stringstreams for serialization
* postgresql: fix: use max_digits10 instead of digits10
* detail: add float_safe_ostringstream, ensuring floats are serialized with enough precision
* mysql, postgresql, sqlite3, tests: fix: set float-precision for all streams
* run clang-format on changed files
* inline remove_cvref.h
* replace unspecific "wrapper" with concrete type name
* tests: split connector specific tests out of serialize/Float
This commit is contained in:
Bernd Lörwald 2023-02-18 08:10:50 +01:00 committed by GitHub
parent dfa297f069
commit 9412851408
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 645 additions and 17 deletions

View File

@ -0,0 +1,75 @@
/*
* Copyright (c) 2023, Roland Bock
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef SQLPP11_COLUMN_TUPLE_FLOAT_SAFE_OSTRINGSTREAM_H
#define SQLPP11_COLUMN_TUPLE_FLOAT_SAFE_OSTRINGSTREAM_H
#include <limits>
#include <sstream>
#include <type_traits>
#include <utility>
#include <sqlpp11/detail/enable_if.h>
namespace sqlpp
{
namespace detail
{
template <typename T, typename = void>
struct float_safe_ostringstream_implementation
{
template <typename U>
void operator()(std::ostringstream& os, U&& x) const
{
os << std::forward<U>(x);
}
};
template <typename T>
struct float_safe_ostringstream_implementation<T, enable_if_t<std::is_floating_point<T>::value>>
{
template <typename U>
void operator()(std::ostringstream& os, U&& x) const
{
auto const old_precision{os.precision(std::numeric_limits<T>::max_digits10)};
os << std::forward<U>(x);
os.precision(old_precision);
}
};
struct float_safe_ostringstream : std::ostringstream
{
template <typename T>
friend float_safe_ostringstream& operator<<(float_safe_ostringstream& os, T&& x)
{
float_safe_ostringstream_implementation<typename std::decay<T>::type>{}(os, x);
return os;
}
};
} // namespace detail
} // namespace sqlpp
#endif

View File

@ -28,6 +28,7 @@
#define SQLPP_MYSQL_CONNECTION_H #define SQLPP_MYSQL_CONNECTION_H
#include <sqlpp11/connection.h> #include <sqlpp11/connection.h>
#include <sqlpp11/detail/float_safe_ostringstream.h>
#include <sqlpp11/exception.h> #include <sqlpp11/exception.h>
#include <sqlpp11/mysql/bind_result.h> #include <sqlpp11/mysql/bind_result.h>
#include <sqlpp11/mysql/char_result.h> #include <sqlpp11/mysql/char_result.h>
@ -247,7 +248,7 @@ namespace sqlpp
} }
const connection& _db; const connection& _db;
std::stringstream _os; sqlpp::detail::float_safe_ostringstream _os;
}; };
std::integral_constant<char, '`'> get_quote_left(const serializer_t&); std::integral_constant<char, '`'> get_quote_left(const serializer_t&);

View File

@ -33,6 +33,7 @@
#include <sstream> #include <sstream>
#include <sqlpp11/connection.h> #include <sqlpp11/connection.h>
#include <sqlpp11/detail/float_safe_ostringstream.h>
#include <sqlpp11/postgresql/bind_result.h> #include <sqlpp11/postgresql/bind_result.h>
#include <sqlpp11/postgresql/result_field.h> #include <sqlpp11/postgresql/result_field.h>
#include <sqlpp11/postgresql/connection_config.h> #include <sqlpp11/postgresql/connection_config.h>
@ -125,7 +126,7 @@ namespace sqlpp
} }
const connection& _db; const connection& _db;
std::ostringstream _os; sqlpp::detail::float_safe_ostringstream _os;
size_t _count{1}; size_t _count{1};
}; };

View File

@ -32,7 +32,7 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include <sqlpp11/chrono.h> #include <sqlpp11/chrono.h>
#include <sqlpp11/detail/float_safe_ostringstream.h>
#include <sqlpp11/postgresql/exception.h> #include <sqlpp11/postgresql/exception.h>
namespace sqlpp namespace sqlpp
@ -127,9 +127,8 @@ namespace sqlpp
_handle->nullValues[index] = is_null; _handle->nullValues[index] = is_null;
if (!is_null) if (!is_null)
{ {
std::ostringstream out; sqlpp::detail::float_safe_ostringstream out;
out.precision(std::numeric_limits<double>::digits10); out << *value;
out << std::fixed << *value;
_handle->paramValues[index] = out.str(); _handle->paramValues[index] = out.str();
} }
} }

View File

@ -33,6 +33,7 @@
#include <sqlite3.h> #include <sqlite3.h>
#endif #endif
#include <sqlpp11/connection.h> #include <sqlpp11/connection.h>
#include <sqlpp11/detail/float_safe_ostringstream.h>
#include <sqlpp11/exception.h> #include <sqlpp11/exception.h>
#include <sqlpp11/schema.h> #include <sqlpp11/schema.h>
#include <sqlpp11/serialize.h> #include <sqlpp11/serialize.h>
@ -184,7 +185,7 @@ namespace sqlpp
} }
const connection& _db; const connection& _db;
std::stringstream _os; sqlpp::detail::float_safe_ostringstream _os;
size_t _count; size_t _count;
}; };

View File

@ -27,7 +27,9 @@
#ifndef SQLPP11_VALUE_H #ifndef SQLPP11_VALUE_H
#define SQLPP11_VALUE_H #define SQLPP11_VALUE_H
#include <sqlpp11/expression_operators.h>
#include <sqlpp11/type_traits.h> #include <sqlpp11/type_traits.h>
#include <sqlpp11/wrap_operand.h>
namespace sqlpp namespace sqlpp
{ {

View File

@ -31,6 +31,7 @@ set(test_files
CustomQuery.cpp CustomQuery.cpp
DynamicWhere.cpp DynamicWhere.cpp
Exists.cpp Exists.cpp
Float.cpp
ForUpdate.cpp ForUpdate.cpp
From.cpp From.cpp
In.cpp In.cpp

View File

@ -0,0 +1,83 @@
/*
* Copyright (c) 2023, Roland Bock
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sqlpp11/data_types/floating_point.h>
#include <sqlpp11/detail/float_safe_ostringstream.h>
#include <sqlpp11/value.h>
#include "compare.h"
namespace
{
template <typename T>
void float_safe_ostringstream_serializes_value_as(int line, T value, std::string expected)
{
sqlpp::detail::float_safe_ostringstream os;
os << value;
assert_equal(line, os.str(), expected);
}
template <typename T>
void float_safe_ostringstream_serializes_in_deserializable_format(int line, T value)
{
sqlpp::detail::float_safe_ostringstream os;
os << value;
std::istringstream is{os.str()};
T deserialized;
is >> deserialized;
assert_equal(line, deserialized, value);
}
template <typename T>
std::string string_for_10_0000086()
{
switch (std::numeric_limits<T>::max_digits10)
{
case 9:
return "10.0000086";
case 17:
return "10.000008599999999";
case 21:
return "10.0000086000000000001";
default:
throw std::logic_error("Unknown floating point digit count");
}
}
} // namespace
int Float(int, char*[])
{
float_safe_ostringstream_serializes_value_as(__LINE__, 10.0000086f, string_for_10_0000086<float>());
float_safe_ostringstream_serializes_value_as(__LINE__, 10.0000086, string_for_10_0000086<double>());
float_safe_ostringstream_serializes_value_as(__LINE__, 10.0000086l, string_for_10_0000086<long double>());
float_safe_ostringstream_serializes_in_deserializable_format(__LINE__, 10.0000086f);
float_safe_ostringstream_serializes_in_deserializable_format(__LINE__, 10.0000086);
float_safe_ostringstream_serializes_in_deserializable_format(__LINE__, 10.0000086l);
compare(__LINE__, sqlpp::value(10.0000114), "10.0000114");
return 0;
}

View File

@ -49,8 +49,8 @@ int In(int, char* [])
compare(__LINE__, foo.omega.in(17, bar.alpha, sqlpp::value(19)), "tab_foo.omega IN(17,tab_bar.alpha,19)"); compare(__LINE__, foo.omega.in(17, bar.alpha, sqlpp::value(19)), "tab_foo.omega IN(17,tab_bar.alpha,19)");
// Lists // Lists
compare(__LINE__, foo.omega.in(sqlpp::value_list(std::vector<float>{1.7f, 2.5f, 17.f, 0.f})), compare(__LINE__, foo.omega.in(sqlpp::value_list(std::vector<float>{1.75f, 2.5f, 17.f, 0.f})),
"tab_foo.omega IN(1.7,2.5,17,0)"); "tab_foo.omega IN(1.75,2.5,17,0)");
// Sub select // Sub select
compare(__LINE__, foo.omega.in(select(bar.alpha).from(bar).unconditionally()), compare(__LINE__, foo.omega.in(select(bar.alpha).from(bar).unconditionally()),

View File

@ -31,6 +31,17 @@
namespace namespace
{ {
template <typename Result, typename Expected>
void assert_equal(int lineNo, const Result& result, const Expected& expected)
{
if (result != expected)
{
std::cerr << __FILE__ << " " << lineNo << '\n' << "Expected: -->|" << expected << "|<--\n"
<< "Received: -->|" << result << "|<--\n";
throw std::runtime_error("unexpected result");
}
}
template <typename Expression> template <typename Expression>
void compare(int lineNo, const Expression& expr, const std::string& expected) void compare(int lineNo, const Expression& expr, const std::string& expected)
{ {
@ -38,12 +49,7 @@ namespace
const auto result = serialize(expr, printer).str(); const auto result = serialize(expr, printer).str();
if (result != expected) assert_equal(lineNo, result, expected);
{
std::cerr << __FILE__ << " " << lineNo << '\n' << "Expected: -->|" << expected << "|<--\n"
<< "Received: -->|" << result << "|<--\n";
throw std::runtime_error("unexpected serialization result");
}
} }
} }

View File

@ -28,6 +28,7 @@
#include <iostream> #include <iostream>
#include <sqlpp11/connection.h> #include <sqlpp11/connection.h>
#include <sqlpp11/detail/float_safe_ostringstream.h>
#include <sqlpp11/transaction.h> #include <sqlpp11/transaction.h>
#include <sqlpp11/data_types/no_value.h> #include <sqlpp11/data_types/no_value.h>
#include <sqlpp11/schema.h> #include <sqlpp11/schema.h>
@ -49,7 +50,7 @@ struct MockDb : public sqlpp::connection
struct _serializer_context_t struct _serializer_context_t
{ {
std::ostringstream _os; ::sqlpp::detail::float_safe_ostringstream _os;
_serializer_context_t() = default; _serializer_context_t() = default;
_serializer_context_t(const _serializer_context_t& rhs) _serializer_context_t(const _serializer_context_t& rhs)
@ -68,7 +69,7 @@ struct MockDb : public sqlpp::connection
} }
template <typename T> template <typename T>
std::ostream& operator<<(T t) ::sqlpp::detail::float_safe_ostringstream& operator<<(T t)
{ {
return _os << t; return _os << t;
} }

View File

@ -22,4 +22,5 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
add_subdirectory(serialize)
add_subdirectory(usage) add_subdirectory(usage)

View File

@ -0,0 +1,51 @@
# Copyright (c) 2023, Roland Bock
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# Redistributions in binary form must reproduce the above copyright notice, this
# list of conditions and the following disclaimer in the documentation and/or
# other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
find_package(Threads REQUIRED)
set(test_files
Float.cpp
)
create_test_sourcelist(test_sources test_main.cpp ${test_files})
add_executable(sqlpp11_mysql_serialize_tests ${test_sources})
target_link_libraries(sqlpp11_mysql_serialize_tests PRIVATE sqlpp11::mysql sqlpp11_testing sqlpp11_mysql_testing)
target_link_libraries(sqlpp11_mysql_serialize_tests PRIVATE Threads::Threads)
if(NOT MSVC)
target_compile_options(sqlpp11_mysql_serialize_tests PRIVATE -Wall -Wextra -pedantic)
endif()
# conditionally bump to a higher C++ standard to test compatibility
if (SQLPP11_TESTS_CXX_STD)
set_property(TARGET sqlpp11_mysql_serialize_tests PROPERTY CXX_STANDARD ${SQLPP11_TESTS_CXX_STD})
set_property(TARGET sqlpp11_mysql_serialize_tests PROPERTY CXX_STANDARD_REQUIRED yes)
set_property(TARGET sqlpp11_mysql_serialize_tests PROPERTY CXX_EXTENSIONS no)
endif()
foreach(test_file IN LISTS test_files)
get_filename_component(test ${test_file} NAME_WLE)
add_test(NAME sqlpp11.mysql.serialize.${test}
COMMAND sqlpp11_mysql_serialize_tests ${test}
)
endforeach()

View File

@ -0,0 +1,36 @@
/*
* Copyright (c) 2023, Roland Bock
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sqlpp11/data_types/floating_point.h>
#include <sqlpp11/value.h>
#include "compare.h"
int Float(int, char*[])
{
compare(__LINE__, sqlpp::value(10.0000114), "10.0000114");
return 0;
}

View File

@ -0,0 +1,72 @@
/*
* Copyright (c) 2013-2015, Roland Bock
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef SQLPP_TESTS_MYSQL_COMPARE_H
#define SQLPP_TESTS_MYSQL_COMPARE_H
#include <sqlpp11/mysql/connection.h>
#include <iostream>
namespace
{
template <typename Result, typename Expected>
void assert_equal(int lineNo, const Result& result, const Expected& expected)
{
if (result != expected)
{
std::cerr << __FILE__ << " " << lineNo << '\n'
<< "Expected: -->|" << expected << "|<--\n"
<< "Received: -->|" << result << "|<--\n";
throw std::runtime_error("unexpected result");
}
}
template <typename Expression>
void compare(int lineNo, const Expression& expr, const std::string& expected)
{
auto config{std::make_shared<sqlpp::mysql::connection_config>()};
config->user = "root";
config->database = "sqlpp_mysql";
config->debug = true;
try
{
sqlpp::mysql::connection db(config);
}
catch (const sqlpp::exception& e)
{
std::cerr << e.what() << std::endl;
throw std::logic_error("For testing, you'll need to create a database sqlpp_mysql for user root (no password)");
}
sqlpp::mysql::connection connection{config};
sqlpp::mysql::serializer_t printer{connection};
const auto result = serialize(expr, printer).str();
assert_equal(lineNo, result, expected);
}
} // namespace
#endif

View File

@ -23,4 +23,5 @@
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
add_subdirectory(constraints) add_subdirectory(constraints)
add_subdirectory(serialize)
add_subdirectory(usage) add_subdirectory(usage)

View File

@ -0,0 +1,48 @@
# Copyright (c) 2023, Roland Bock
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# Redistributions in binary form must reproduce the above copyright notice, this
# list of conditions and the following disclaimer in the documentation and/or
# other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
set(test_files
Float.cpp
)
create_test_sourcelist(test_sources test_main.cpp ${test_files})
add_executable(sqlpp11_postgresql_serialize_tests ${test_sources})
target_link_libraries(sqlpp11_postgresql_serialize_tests PRIVATE sqlpp11::postgresql sqlpp11_testing sqlpp11_postgresql_testing)
if(NOT MSVC)
target_compile_options(sqlpp11_postgresql_serialize_tests PRIVATE -Wall -Wextra -pedantic)
endif()
# conditionally bump to a higher C++ standard to test compatibility
if (SQLPP11_TESTS_CXX_STD)
set_property(TARGET sqlpp11_postgresql_serialize_tests PROPERTY CXX_STANDARD ${SQLPP11_TESTS_CXX_STD})
set_property(TARGET sqlpp11_postgresql_serialize_tests PROPERTY CXX_STANDARD_REQUIRED yes)
set_property(TARGET sqlpp11_postgresql_serialize_tests PROPERTY CXX_EXTENSIONS no)
endif()
foreach(test_file IN LISTS test_files)
get_filename_component(test ${test_file} NAME_WLE)
add_test(NAME sqlpp11.postgresql.serialize.${test}
COMMAND sqlpp11_postgresql_serialize_tests ${test}
)
endforeach()

View File

@ -0,0 +1,36 @@
/*
* Copyright (c) 2023, Roland Bock
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sqlpp11/data_types/floating_point.h>
#include <sqlpp11/value.h>
#include "compare.h"
int Float(int, char*[])
{
compare(__LINE__, sqlpp::value(10.0000114), "10.0000114");
return 0;
}

View File

@ -0,0 +1,59 @@
/*
* Copyright (c) 2013-2015, Roland Bock
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef SQLPP_TESTS_POSTGRESQL_COMPARE_H
#define SQLPP_TESTS_POSTGRESQL_COMPARE_H
#include <sqlpp11/postgresql/connection.h>
#include <iostream>
namespace
{
template <typename Result, typename Expected>
void assert_equal(int lineNo, const Result& result, const Expected& expected)
{
if (result != expected)
{
std::cerr << __FILE__ << " " << lineNo << '\n'
<< "Expected: -->|" << expected << "|<--\n"
<< "Received: -->|" << result << "|<--\n";
throw std::runtime_error("unexpected result");
}
}
template <typename Expression>
void compare(int lineNo, const Expression& expr, const std::string& expected)
{
sqlpp::postgresql::connection connection;
sqlpp::postgresql::context_t printer{connection};
const auto result = serialize(expr, printer).str();
assert_equal(lineNo, result, expected);
}
} // namespace
#endif

View File

@ -22,4 +22,5 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
add_subdirectory(serialize)
add_subdirectory(usage) add_subdirectory(usage)

View File

@ -0,0 +1,54 @@
# Copyright (c) 2023, Roland Bock
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# Redistributions in binary form must reproduce the above copyright notice, this
# list of conditions and the following disclaimer in the documentation and/or
# other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
set(test_files
Float.cpp
)
create_test_sourcelist(test_sources test_main.cpp ${test_files})
add_executable(sqlpp11_sqlite3_serialize_tests ${test_sources})
target_link_libraries(sqlpp11_sqlite3_serialize_tests PRIVATE sqlpp11_testing sqlpp11_sqlite3_testing)
if (BUILD_SQLCIPHER_CONNECTOR)
target_link_libraries(sqlpp11_sqlite3_serialize_tests PRIVATE sqlpp11::sqlcipher)
if (SQLPP_DYNAMIC_LOADING)
target_include_directories(sqlpp11_sqlite3_serialize_tests PRIVATE ${SQLCIPHER_INCLUDE_DIRS})
endif()
else()
target_link_libraries(sqlpp11_sqlite3_serialize_tests PRIVATE sqlpp11::sqlite3)
endif()
# conditionally bump to a higher C++ standard to test compatibility
if (SQLPP11_TESTS_CXX_STD)
set_property(TARGET sqlpp11_sqlite3_serialize_tests PROPERTY CXX_STANDARD ${SQLPP11_TESTS_CXX_STD})
set_property(TARGET sqlpp11_sqlite3_serialize_tests PROPERTY CXX_STANDARD_REQUIRED ON)
set_property(TARGET sqlpp11_sqlite3_serialize_tests PROPERTY CXX_EXTENSIONS OFF)
endif()
foreach(test_file IN LISTS test_files)
get_filename_component(test ${test_file} NAME_WLE)
add_test(NAME sqlpp11.sqlite3.serialize.${test}
COMMAND sqlpp11_sqlite3_serialize_tests ${test}
)
endforeach()

View File

@ -0,0 +1,36 @@
/*
* Copyright (c) 2023, Roland Bock
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sqlpp11/data_types/floating_point.h>
#include <sqlpp11/value.h>
#include "compare.h"
int Float(int, char*[])
{
compare(__LINE__, sqlpp::value(10.0000114), "10.0000114");
return 0;
}

View File

@ -0,0 +1,63 @@
/*
* Copyright (c) 2013-2015, Roland Bock
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef SQLPP_TESTS_SQLITE3_COMPARE_H
#define SQLPP_TESTS_SQLITE3_COMPARE_H
#include <sqlpp11/sqlite3/connection.h>
#include <iostream>
namespace
{
template <typename Result, typename Expected>
void assert_equal(int lineNo, const Result& result, const Expected& expected)
{
if (result != expected)
{
std::cerr << __FILE__ << " " << lineNo << '\n'
<< "Expected: -->|" << expected << "|<--\n"
<< "Received: -->|" << result << "|<--\n";
throw std::runtime_error("unexpected result");
}
}
template <typename Expression>
void compare(int lineNo, const Expression& expr, const std::string& expected)
{
sqlpp::sqlite3::connection_config config;
config.path_to_database = ":memory:";
config.flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
config.debug = true;
sqlpp::sqlite3::connection connection{config};
sqlpp::sqlite3::serializer_t printer{connection};
const auto result = serialize(expr, printer).str();
assert_equal(lineNo, result, expected);
}
} // namespace
#endif