From 30f073d02d0719e1d5d4e61f68122f174dfa284d Mon Sep 17 00:00:00 2001 From: rbock Date: Sun, 1 Nov 2015 09:32:47 +0100 Subject: [PATCH] Moved result_field implementation to result_field_base Much less boiler plate code this way --- .../sqlpp11/data_types/boolean/result_field.h | 65 +------------- .../data_types/day_point/result_field.h | 64 +------------- .../data_types/floating_point/result_field.h | 63 +------------- .../data_types/integral/result_field.h | 64 +------------- .../sqlpp11/data_types/text/result_field.h | 81 +++--------------- .../data_types/time_point/result_field.h | 65 +------------- include/sqlpp11/result_field.h | 1 + include/sqlpp11/result_field_base.h | 85 ++++++++++++++++--- 8 files changed, 100 insertions(+), 388 deletions(-) diff --git a/include/sqlpp11/data_types/boolean/result_field.h b/include/sqlpp11/data_types/boolean/result_field.h index 27810558..3a4862a8 100644 --- a/include/sqlpp11/data_types/boolean/result_field.h +++ b/include/sqlpp11/data_types/boolean/result_field.h @@ -28,81 +28,24 @@ #define SQLPP_BOOLEAN_RESULT_FIELD_H #include -#include #include #include -#include #include #include namespace sqlpp { template - struct result_field_t : public result_field_base_t> + struct result_field_t : public result_field_base { +#warning : need to get rid of this static assert by removing the boolean parameter from result_field static_assert(std::is_same, boolean>::value, "field type mismatch"); - using _cpp_value_type = typename 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) + void _bind(Target& target, size_t index) { - target._bind_boolean_result(i, &_value, &_is_null); + target._bind_boolean_result(index, &this->_value, &this->_is_null); } - - private: - bool _is_valid; - bool _is_null; - signed char _value; }; template diff --git a/include/sqlpp11/data_types/day_point/result_field.h b/include/sqlpp11/data_types/day_point/result_field.h index 53970002..8b0710b3 100644 --- a/include/sqlpp11/data_types/day_point/result_field.h +++ b/include/sqlpp11/data_types/day_point/result_field.h @@ -28,81 +28,23 @@ #define SQLPP_DAY_POINT_RESULT_FIELD_H #include -#include #include #include -#include #include #include namespace sqlpp { template - struct result_field_t : public result_field_base_t> + struct result_field_t : public result_field_base { static_assert(std::is_same, day_point>::value, "field type mismatch"); - using _cpp_value_type = typename sqlpp::day_point::_cpp_value_type; - - result_field_t() : _is_valid(false), _is_null(true), _value{} - { - } - - void _invalidate() - { - _is_valid = false; - _is_null = true; - _value = _cpp_value_type{}; - } - - 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() == _cpp_value_type{}; - } - - _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 _cpp_value_type{}; - } - } - return _value; - } template - void _bind(Target& target, size_t i) + void _bind(Target& target, size_t index) { - target._bind_date_result(i, &_value, &_is_null); + target._bind_date_result(index, &this->_value, &this->_is_null); } - - private: - bool _is_valid; - bool _is_null; - _cpp_value_type _value; }; template diff --git a/include/sqlpp11/data_types/floating_point/result_field.h b/include/sqlpp11/data_types/floating_point/result_field.h index c36754b7..d96f0c23 100644 --- a/include/sqlpp11/data_types/floating_point/result_field.h +++ b/include/sqlpp11/data_types/floating_point/result_field.h @@ -38,72 +38,15 @@ namespace sqlpp { template - struct result_field_t - : public result_field_base_t> + struct result_field_t : public result_field_base { static_assert(std::is_same, floating_point>::value, "field type mismatch"); - using _cpp_value_type = typename 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) + void _bind(Target& target, size_t index) { - target._bind_floating_point_result(i, &_value, &_is_null); + target._bind_floating_point_result(index, &this->_value, &this->_is_null); } - - private: - bool _is_valid; - bool _is_null; - _cpp_value_type _value; }; template diff --git a/include/sqlpp11/data_types/integral/result_field.h b/include/sqlpp11/data_types/integral/result_field.h index 02df24ff..fc9aec5b 100644 --- a/include/sqlpp11/data_types/integral/result_field.h +++ b/include/sqlpp11/data_types/integral/result_field.h @@ -28,81 +28,23 @@ #define SQLPP_INTEGRAL_RESULT_FIELD_H #include -#include #include #include -#include #include #include namespace sqlpp { template - struct result_field_t : public result_field_base_t> + struct result_field_t : public result_field_base { static_assert(std::is_same, integral>::value, "field type mismatch"); - using _cpp_value_type = typename 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) + void _bind(Target& target, size_t index) { - target._bind_integral_result(i, &_value, &_is_null); + target._bind_integral_result(index, &this->_value, &this->_is_null); } - - private: - bool _is_valid; - bool _is_null; - _cpp_value_type _value; }; template diff --git a/include/sqlpp11/data_types/text/result_field.h b/include/sqlpp11/data_types/text/result_field.h index 593bfd21..c49959e2 100644 --- a/include/sqlpp11/data_types/text/result_field.h +++ b/include/sqlpp11/data_types/text/result_field.h @@ -28,90 +28,31 @@ #define SQLPP_TEXT_RESULT_FIELD_H #include -#include #include #include -#include #include #include namespace sqlpp { + namespace detail + { + } + template - struct result_field_t : public result_field_base_t> + struct result_field_t : public result_field_base { static_assert(std::is_same, text>::value, "field type mismatch"); - using _cpp_value_type = typename 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) + void _bind(Target& target, size_t index) { - target._bind_text_result(i, &_value_ptr, &_len); + const char* text{nullptr}; + size_t len{}; + target._bind_text_result(index, &text, &len); + this->_value = {text, len}; + this->_is_null = (len == 0); } - - private: - bool _is_valid; - const char* _value_ptr; - size_t _len; }; template diff --git a/include/sqlpp11/data_types/time_point/result_field.h b/include/sqlpp11/data_types/time_point/result_field.h index 1f1f3680..2c5219da 100644 --- a/include/sqlpp11/data_types/time_point/result_field.h +++ b/include/sqlpp11/data_types/time_point/result_field.h @@ -28,83 +28,22 @@ #define SQLPP_TIME_POINT_RESULT_FIELD_H #include -#include #include #include -#include #include #include namespace sqlpp { - // time_point result field template - struct result_field_t - : public result_field_base_t> + struct result_field_t : public result_field_base { static_assert(std::is_same, time_point>::value, "field type mismatch"); - using _cpp_value_type = typename time_point::_cpp_value_type; - - result_field_t() : _is_valid(false), _is_null(true), _value{} - { - } - - void _invalidate() - { - _is_valid = false; - _is_null = true; - _value = _cpp_value_type{}; - } - - 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() == _cpp_value_type{}; - } - - _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 _cpp_value_type{}; - } - } - return _value; - } - template void _bind(Target& target, size_t i) { - target._bind_date_time_result(i, &_value, &_is_null); + target._bind_date_time_result(i, &this->_value, &this->_is_null); } - - private: - bool _is_valid; - bool _is_null; - _cpp_value_type _value; }; template diff --git a/include/sqlpp11/result_field.h b/include/sqlpp11/result_field.h index a277078e..27ef54d7 100644 --- a/include/sqlpp11/result_field.h +++ b/include/sqlpp11/result_field.h @@ -33,6 +33,7 @@ namespace sqlpp { +#warning : The value type IS in the FieldSpec! template struct result_field_t { diff --git a/include/sqlpp11/result_field_base.h b/include/sqlpp11/result_field_base.h index ed9034ca..6d33ae35 100644 --- a/include/sqlpp11/result_field_base.h +++ b/include/sqlpp11/result_field_base.h @@ -31,21 +31,18 @@ #include #include #include +#include +#include namespace sqlpp { - template - struct result_field_base_t + template ::_cpp_value_type> + struct result_field_base { - static_assert(wrong_t::value, "Invalid argument for result_field_base"); - }; - - template - struct result_field_base_t> - { - using _derived_t = result_field_t; - using _field_spec_t = FieldSpec; using _db_t = Db; + using _field_spec_t = FieldSpec; + using _cpp_value_type = typename value_type_of::_cpp_value_type; + using _cpp_storage_type = StorageType; static constexpr bool _null_is_trivial = column_spec_can_be_null_t<_field_spec_t>::value and @@ -58,10 +55,74 @@ namespace sqlpp using _nodes = detail::type_vector<>; using _can_be_null = column_spec_can_be_null_t<_field_spec_t>; - operator cpp_value_type_of<_field_spec_t>() const + result_field_base() : _is_valid{false}, _is_null{true}, _value{} { - return static_cast(*this).value(); } + + bool operator==(const _cpp_value_type& rhs) const + { + return value() == rhs; + } + + bool operator!=(const _cpp_value_type& rhs) const + { + return not operator==(rhs); + } + + void _validate() + { + _is_valid = true; + } + + void _invalidate() + { + _is_valid = false; + _is_null = true; + _value = {}; + } + + 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() == _cpp_storage_type{}; + } + + _cpp_value_type value() const + { + if (not _is_valid) + throw exception("accessing value in non-existing row"); + + if (_is_null) + { + if (not _null_is_trivial) + { + throw exception("accessing value of NULL field"); + } + else + { + return {}; + } + } + return _value; + } + + operator _cpp_value_type() const + { + return value(); + } + + bool _is_valid; + bool _is_null; + _cpp_storage_type _value; }; } #endif