From f60f1504b86d7eac79cae38c844daacb6111b597 Mon Sep 17 00:00:00 2001 From: rbock Date: Wed, 30 Jul 2014 22:37:07 +0200 Subject: [PATCH] Extracted result_field_t from value types. This is a preparation for adding serializer specializations for them --- include/sqlpp11/boolean.h | 151 ++++++++++++------------ include/sqlpp11/floating_point.h | 151 ++++++++++++------------ include/sqlpp11/integral.h | 150 ++++++++++++------------ include/sqlpp11/result_field_methods.h | 8 +- include/sqlpp11/result_row.h | 6 +- include/sqlpp11/text.h | 155 +++++++++++++------------ 6 files changed, 320 insertions(+), 301 deletions(-) diff --git a/include/sqlpp11/boolean.h b/include/sqlpp11/boolean.h index d37b7546..626323cd 100644 --- a/include/sqlpp11/boolean.h +++ b/include/sqlpp11/boolean.h @@ -32,7 +32,7 @@ #include #include #include -#include +#include namespace sqlpp { @@ -97,73 +97,6 @@ namespace sqlpp bool _is_null; }; - template - struct _result_field_t: public result_field_methods_t<_result_field_t> - { - _result_field_t(): - _is_valid(false), - _is_null(true), - _value(false) - {} - - void _validate() - { - _is_valid = true; - } - - void _invalidate() - { - _is_valid = false; - _is_null = true; - _value = 0; - } - - bool is_null() const - { - if (not _is_valid) - throw exception("accessing is_null in non-existing row"); - return _is_null; - } - - bool _is_trivial() const - { - if (not _is_valid) - throw exception("accessing is_null in non-existing row"); - - return value() == false; - } - - _cpp_value_type value() const - { - if (not _is_valid) - throw exception("accessing value in non-existing row"); - - if (_is_null) - { - if (enforce_null_result_treatment_t::value and not null_is_trivial_value_t::value) - { - throw exception("accessing value of NULL field"); - } - else - { - return false; - } - } - return _value; - } - - template - void _bind(Target& target, size_t i) - { - target._bind_boolean_result(i, &_value, &_is_null); - } - - private: - bool _is_valid; - bool _is_null; - signed char _value; - }; - template struct _is_valid_operand { @@ -206,13 +139,85 @@ namespace sqlpp }; }; - template - inline std::ostream& operator<<(std::ostream& os, const boolean::_result_field_t& e) - { - return os << e.value(); - } } + template + struct result_field_t: public result_field_methods_t> + { + static_assert(std::is_same, detail::boolean>::value, "field type mismatch"); + using _cpp_value_type = typename detail::boolean::_cpp_value_type; + + result_field_t(): + _is_valid(false), + _is_null(true), + _value(false) + {} + + void _validate() + { + _is_valid = true; + } + + void _invalidate() + { + _is_valid = false; + _is_null = true; + _value = 0; + } + + bool is_null() const + { + if (not _is_valid) + throw exception("accessing is_null in non-existing row"); + return _is_null; + } + + bool _is_trivial() const + { + if (not _is_valid) + throw exception("accessing is_null in non-existing row"); + + return value() == false; + } + + _cpp_value_type value() const + { + if (not _is_valid) + throw exception("accessing value in non-existing row"); + + if (_is_null) + { + if (enforce_null_result_treatment_t::value and not null_is_trivial_value_t::value) + { + throw exception("accessing value of NULL field"); + } + else + { + return false; + } + } + return _value; + } + + template + void _bind(Target& target, size_t i) + { + target._bind_boolean_result(i, &_value, &_is_null); + } + + private: + bool _is_valid; + bool _is_null; + signed char _value; + }; + + template + inline std::ostream& operator<<(std::ostream& os, const result_field_t& e) + { + return os << e.value(); + } + + using boolean = detail::boolean; } diff --git a/include/sqlpp11/floating_point.h b/include/sqlpp11/floating_point.h index a9decec7..964b5cc1 100644 --- a/include/sqlpp11/floating_point.h +++ b/include/sqlpp11/floating_point.h @@ -31,7 +31,7 @@ #include #include #include -#include +#include namespace sqlpp { @@ -96,73 +96,6 @@ namespace sqlpp bool _is_null; }; - template - struct _result_field_t: public result_field_methods_t<_result_field_t> - { - _result_field_t(): - _is_valid(false), - _is_null(true), - _value(0) - {} - - void _validate() - { - _is_valid = true; - } - - void _invalidate() - { - _is_valid = false; - _is_null = true; - _value = 0; - } - - bool is_null() const - { - if (not _is_valid) - throw exception("accessing is_null in non-existing row"); - return _is_null; - } - - bool _is_trivial() const - { - if (not _is_valid) - throw exception("accessing is_null in non-existing row"); - - return value() == 0; - } - - _cpp_value_type value() const - { - if (not _is_valid) - throw exception("accessing value in non-existing row"); - - if (_is_null) - { - if (enforce_null_result_treatment_t::value and not null_is_trivial_value_t::value) - { - throw exception("accessing value of NULL field"); - } - else - { - return 0; - } - } - return _value; - } - - template - void _bind(Target& target, size_t i) - { - target._bind_floating_point_result(i, &_value, &_is_null); - } - - private: - bool _is_valid; - bool _is_null; - _cpp_value_type _value; - }; - template struct _is_valid_operand { @@ -261,13 +194,85 @@ namespace sqlpp }; }; - template - inline std::ostream& operator<<(std::ostream& os, const floating_point::_result_field_t& e) - { - return os << e.value(); - } } + template + struct result_field_t: public result_field_methods_t> + { + static_assert(std::is_same, detail::floating_point>::value, "field type mismatch"); + using _cpp_value_type = typename detail::floating_point::_cpp_value_type; + + result_field_t(): + _is_valid(false), + _is_null(true), + _value(0) + {} + + void _validate() + { + _is_valid = true; + } + + void _invalidate() + { + _is_valid = false; + _is_null = true; + _value = 0; + } + + bool is_null() const + { + if (not _is_valid) + throw exception("accessing is_null in non-existing row"); + return _is_null; + } + + bool _is_trivial() const + { + if (not _is_valid) + throw exception("accessing is_null in non-existing row"); + + return value() == 0; + } + + _cpp_value_type value() const + { + if (not _is_valid) + throw exception("accessing value in non-existing row"); + + if (_is_null) + { + if (enforce_null_result_treatment_t::value and not null_is_trivial_value_t::value) + { + throw exception("accessing value of NULL field"); + } + else + { + return 0; + } + } + return _value; + } + + template + void _bind(Target& target, size_t i) + { + target._bind_floating_point_result(i, &_value, &_is_null); + } + + private: + bool _is_valid; + bool _is_null; + _cpp_value_type _value; + }; + + template + inline std::ostream& operator<<(std::ostream& os, const result_field_t& e) + { + return os << e.value(); + } + + using floating_point = detail::floating_point; } diff --git a/include/sqlpp11/integral.h b/include/sqlpp11/integral.h index f99d772c..16695215 100644 --- a/include/sqlpp11/integral.h +++ b/include/sqlpp11/integral.h @@ -33,7 +33,7 @@ #include #include #include -#include +#include namespace sqlpp { @@ -97,73 +97,6 @@ namespace sqlpp bool _is_null; }; - template - struct _result_field_t: public result_field_methods_t<_result_field_t> - { - _result_field_t(): - _is_valid(false), - _is_null(true), - _value(0) - {} - - void _invalidate() - { - _is_valid = false; - _is_null = true; - _value = 0; - } - - void _validate() - { - _is_valid = true; - } - - bool is_null() const - { - if (not _is_valid) - throw exception("accessing is_null in non-existing row"); - return _is_null; - } - - bool _is_trivial() const - { - if (not _is_valid) - throw exception("accessing is_null in non-existing row"); - - return value() == 0; - } - - _cpp_value_type value() const - { - if (not _is_valid) - throw exception("accessing value in non-existing row"); - - if (_is_null) - { - if (enforce_null_result_treatment_t::value and not null_is_trivial_value_t::value) - { - throw exception("accessing value of NULL field"); - } - else - { - return 0; - } - } - return _value; - } - - template - void _bind(Target& target, size_t i) - { - target._bind_integral_result(i, &_value, &_is_null); - } - - private: - bool _is_valid; - bool _is_null; - _cpp_value_type _value; - }; - template struct _is_valid_operand { @@ -273,13 +206,84 @@ namespace sqlpp }; }; - template - inline std::ostream& operator<<(std::ostream& os, const integral::_result_field_t& e) - { - return os << e.value(); - } } + template + struct result_field_t: public result_field_methods_t> + { + static_assert(std::is_same, detail::integral>::value, "field type mismatch"); + using _cpp_value_type = typename detail::integral::_cpp_value_type; + + result_field_t(): + _is_valid(false), + _is_null(true), + _value(0) + {} + + void _invalidate() + { + _is_valid = false; + _is_null = true; + _value = 0; + } + + void _validate() + { + _is_valid = true; + } + + bool is_null() const + { + if (not _is_valid) + throw exception("accessing is_null in non-existing row"); + return _is_null; + } + + bool _is_trivial() const + { + if (not _is_valid) + throw exception("accessing is_null in non-existing row"); + + return value() == 0; + } + + _cpp_value_type value() const + { + if (not _is_valid) + throw exception("accessing value in non-existing row"); + + if (_is_null) + { + if (enforce_null_result_treatment_t::value and not null_is_trivial_value_t::value) + { + throw exception("accessing value of NULL field"); + } + else + { + return 0; + } + } + return _value; + } + + template + void _bind(Target& target, size_t i) + { + target._bind_integral_result(i, &_value, &_is_null); + } + + private: + bool _is_valid; + bool _is_null; + _cpp_value_type _value; + }; + + template + inline std::ostream& operator<<(std::ostream& os, const result_field_t& e) + { + return os << e.value(); + } + using tinyint = detail::integral; using smallint = detail::integral; using integer = detail::integral; diff --git a/include/sqlpp11/result_field_methods.h b/include/sqlpp11/result_field_methods.h index 280a5357..ad37b1ef 100644 --- a/include/sqlpp11/result_field_methods.h +++ b/include/sqlpp11/result_field_methods.h @@ -40,8 +40,8 @@ namespace sqlpp static_assert(wrong_t::value, "Invalid argument for get_field_spec"); }; - template class Field, typename Db, typename FieldSpec> - struct get_field_spec_impl> + template class Field, typename ValueType, typename Db, typename FieldSpec> + struct get_field_spec_impl> { using type = FieldSpec; }; @@ -58,9 +58,9 @@ namespace sqlpp operator cpp_value_type_of<_field_spec_t>() const { return static_cast(*this).value(); } }; - template class Field, typename Db, typename FieldSpec> + template class Field, typename ValueType, typename Db, typename FieldSpec> struct result_field_methods_base_t< - Field, + Field, typename std::enable_if::value and column_spec_can_be_null_t::value and not null_is_trivial_value_t::value>::type> diff --git a/include/sqlpp11/result_row.h b/include/sqlpp11/result_row.h index fe2a6c63..3ab024a0 100644 --- a/include/sqlpp11/result_row.h +++ b/include/sqlpp11/result_row.h @@ -41,9 +41,9 @@ namespace sqlpp template struct result_field: - public FieldSpec::_name_t::template _member_t::template _result_field_t> + public FieldSpec::_name_t::template _member_t, Db, FieldSpec>> { - using _field = typename FieldSpec::_name_t::template _member_t::template _result_field_t>; + using _field = typename FieldSpec::_name_t::template _member_t, Db, FieldSpec>>; result_field() = default; @@ -189,7 +189,7 @@ namespace sqlpp struct _name_t {}; }; - using _field_type = detail::text::_result_field_t; + using _field_type = result_field_t; static constexpr size_t _last_static_index = _impl::_last_index; bool _is_valid; diff --git a/include/sqlpp11/text.h b/include/sqlpp11/text.h index 1a264676..99e95e73 100644 --- a/include/sqlpp11/text.h +++ b/include/sqlpp11/text.h @@ -32,7 +32,7 @@ #include #include #include -#include +#include namespace sqlpp { @@ -96,75 +96,6 @@ namespace sqlpp bool _is_null; }; - template - struct _result_field_t: public result_field_methods_t<_result_field_t> - { - _result_field_t(): - _is_valid(false), - _value_ptr(nullptr), - _len(0) - {} - - void _validate() - { - _is_valid = true; - } - - void _invalidate() - { - _is_valid = false; - _value_ptr = nullptr; - _len = 0; - } - - bool operator==(const _cpp_value_type& rhs) const { return value() == rhs; } - bool operator!=(const _cpp_value_type& rhs) const { return not operator==(rhs); } - - bool is_null() const - { - if (not _is_valid) - throw exception("accessing is_null in non-existing row"); - return _value_ptr == nullptr; - } - - bool _is_trivial() const - { - if (not _is_valid) - throw exception("accessing is_null in non-existing row"); - - return value() == ""; - } - - _cpp_value_type value() const - { - if (not _is_valid) - throw exception("accessing value in non-existing row"); - - if (not _value_ptr) - { - if (enforce_null_result_treatment_t::value and not null_is_trivial_value_t::value) - { - throw exception("accessing value of NULL field"); - } - else - { - return ""; - } - } - return std::string(_value_ptr, _value_ptr + _len); - } - - template - void _bind(Target& target, size_t i) - { - target._bind_text_result(i, &_value_ptr, &_len); - } - - private: - bool _is_valid; - const char* _value_ptr; - size_t _len; - }; template struct _is_valid_operand @@ -211,13 +142,87 @@ namespace sqlpp }; }; - template - inline std::ostream& operator<<(std::ostream& os, const text::_result_field_t& e) - { - return os << e.value(); - } } + template + struct result_field_t: public result_field_methods_t> + { + static_assert(std::is_same, detail::text>::value, "field type mismatch"); + using _cpp_value_type = typename detail::text::_cpp_value_type; + + result_field_t(): + _is_valid(false), + _value_ptr(nullptr), + _len(0) + {} + + void _validate() + { + _is_valid = true; + } + + void _invalidate() + { + _is_valid = false; + _value_ptr = nullptr; + _len = 0; + } + + bool operator==(const _cpp_value_type& rhs) const { return value() == rhs; } + bool operator!=(const _cpp_value_type& rhs) const { return not operator==(rhs); } + + bool is_null() const + { + if (not _is_valid) + throw exception("accessing is_null in non-existing row"); + return _value_ptr == nullptr; + } + + bool _is_trivial() const + { + if (not _is_valid) + throw exception("accessing is_null in non-existing row"); + + return value() == ""; + } + + _cpp_value_type value() const + { + if (not _is_valid) + throw exception("accessing value in non-existing row"); + + if (not _value_ptr) + { + if (enforce_null_result_treatment_t::value and not null_is_trivial_value_t::value) + { + throw exception("accessing value of NULL field"); + } + else + { + return ""; + } + } + return std::string(_value_ptr, _value_ptr + _len); + } + + template + void _bind(Target& target, size_t i) + { + target._bind_text_result(i, &_value_ptr, &_len); + } + + private: + bool _is_valid; + const char* _value_ptr; + size_t _len; + }; + + template + inline std::ostream& operator<<(std::ostream& os, const result_field_t& e) + { + return os << e.value(); + } + using text = detail::text; using blob = detail::text; using varchar = detail::text;