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

Moved result_field implementation to result_field_base

Much less boiler plate code this way
This commit is contained in:
rbock 2015-11-01 09:32:47 +01:00
parent 76af86a572
commit 30f073d02d
8 changed files with 100 additions and 388 deletions

View File

@ -28,81 +28,24 @@
#define SQLPP_BOOLEAN_RESULT_FIELD_H
#include <sqlpp11/basic_expression_operators.h>
#include <sqlpp11/exception.h>
#include <sqlpp11/result_field.h>
#include <sqlpp11/result_field_base.h>
#include <sqlpp11/type_traits.h>
#include <sqlpp11/data_types/boolean/data_type.h>
#include <ostream>
namespace sqlpp
{
template <typename Db, typename FieldSpec>
struct result_field_t<boolean, Db, FieldSpec> : public result_field_base_t<result_field_t<boolean, Db, FieldSpec>>
struct result_field_t<boolean, Db, FieldSpec> : public result_field_base<Db, FieldSpec, signed char>
{
#warning : need to get rid of this static assert by removing the boolean parameter from result_field
static_assert(std::is_same<value_type_of<FieldSpec>, 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<Db>::value and not null_is_trivial_value_t<FieldSpec>::value)
{
throw exception("accessing value of NULL field");
}
else
{
return false;
}
}
return _value;
}
template <typename Target>
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 <typename Db, typename FieldSpec>

View File

@ -28,81 +28,23 @@
#define SQLPP_DAY_POINT_RESULT_FIELD_H
#include <sqlpp11/basic_expression_operators.h>
#include <sqlpp11/exception.h>
#include <sqlpp11/result_field.h>
#include <sqlpp11/result_field_base.h>
#include <sqlpp11/type_traits.h>
#include <sqlpp11/data_types/day_point/data_type.h>
#include <ostream>
namespace sqlpp
{
template <typename Db, typename FieldSpec>
struct result_field_t<day_point, Db, FieldSpec> : public result_field_base_t<result_field_t<day_point, Db, FieldSpec>>
struct result_field_t<day_point, Db, FieldSpec> : public result_field_base<Db, FieldSpec>
{
static_assert(std::is_same<value_type_of<FieldSpec>, 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<Db>::value and not null_is_trivial_value_t<FieldSpec>::value)
{
throw exception("accessing value of NULL field");
}
else
{
return _cpp_value_type{};
}
}
return _value;
}
template <typename Target>
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 <typename Db, typename FieldSpec>

View File

@ -38,72 +38,15 @@
namespace sqlpp
{
template <typename Db, typename FieldSpec>
struct result_field_t<floating_point, Db, FieldSpec>
: public result_field_base_t<result_field_t<floating_point, Db, FieldSpec>>
struct result_field_t<floating_point, Db, FieldSpec> : public result_field_base<Db, FieldSpec>
{
static_assert(std::is_same<value_type_of<FieldSpec>, 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<Db>::value and not null_is_trivial_value_t<FieldSpec>::value)
{
throw exception("accessing value of NULL field");
}
else
{
return 0;
}
}
return _value;
}
template <typename Target>
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 <typename Db, typename FieldSpec>

View File

@ -28,81 +28,23 @@
#define SQLPP_INTEGRAL_RESULT_FIELD_H
#include <sqlpp11/basic_expression_operators.h>
#include <sqlpp11/exception.h>
#include <sqlpp11/result_field.h>
#include <sqlpp11/result_field_base.h>
#include <sqlpp11/type_traits.h>
#include <sqlpp11/data_types/integral/data_type.h>
#include <ostream>
namespace sqlpp
{
template <typename Db, typename FieldSpec>
struct result_field_t<integral, Db, FieldSpec> : public result_field_base_t<result_field_t<integral, Db, FieldSpec>>
struct result_field_t<integral, Db, FieldSpec> : public result_field_base<Db, FieldSpec>
{
static_assert(std::is_same<value_type_of<FieldSpec>, 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<Db>::value and not null_is_trivial_value_t<FieldSpec>::value)
{
throw exception("accessing value of NULL field");
}
else
{
return 0;
}
}
return _value;
}
template <typename Target>
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 <typename Db, typename FieldSpec>

View File

@ -28,90 +28,31 @@
#define SQLPP_TEXT_RESULT_FIELD_H
#include <sqlpp11/basic_expression_operators.h>
#include <sqlpp11/exception.h>
#include <sqlpp11/result_field.h>
#include <sqlpp11/result_field_base.h>
#include <sqlpp11/type_traits.h>
#include <sqlpp11/data_types/text/data_type.h>
#include <ostream>
namespace sqlpp
{
namespace detail
{
}
template <typename Db, typename FieldSpec>
struct result_field_t<text, Db, FieldSpec> : public result_field_base_t<result_field_t<text, Db, FieldSpec>>
struct result_field_t<text, Db, FieldSpec> : public result_field_base<Db, FieldSpec>
{
static_assert(std::is_same<value_type_of<FieldSpec>, 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<Db>::value and not null_is_trivial_value_t<FieldSpec>::value)
{
throw exception("accessing value of NULL field");
}
else
{
return "";
}
}
return std::string(_value_ptr, _value_ptr + _len);
}
template <typename Target>
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 <typename Db, typename FieldSpec>

View File

@ -28,83 +28,22 @@
#define SQLPP_TIME_POINT_RESULT_FIELD_H
#include <sqlpp11/basic_expression_operators.h>
#include <sqlpp11/exception.h>
#include <sqlpp11/result_field.h>
#include <sqlpp11/result_field_base.h>
#include <sqlpp11/type_traits.h>
#include <sqlpp11/data_types/time_point/data_type.h>
#include <ostream>
namespace sqlpp
{
// time_point result field
template <typename Db, typename FieldSpec>
struct result_field_t<time_point, Db, FieldSpec>
: public result_field_base_t<result_field_t<time_point, Db, FieldSpec>>
struct result_field_t<time_point, Db, FieldSpec> : public result_field_base<Db, FieldSpec>
{
static_assert(std::is_same<value_type_of<FieldSpec>, 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<Db>::value and not null_is_trivial_value_t<FieldSpec>::value)
{
throw exception("accessing value of NULL field");
}
else
{
return _cpp_value_type{};
}
}
return _value;
}
template <typename Target>
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 <typename Db, typename FieldSpec>

View File

@ -33,6 +33,7 @@
namespace sqlpp
{
#warning : The value type IS in the FieldSpec!
template <typename ValueType, typename Db, typename FieldSpec>
struct result_field_t
{

View File

@ -31,21 +31,18 @@
#include <sqlpp11/basic_expression_operators.h>
#include <sqlpp11/alias_operators.h>
#include <sqlpp11/result_field.h>
#include <sqlpp11/exception.h>
#include <sqlpp11/type_traits.h>
namespace sqlpp
{
template <typename Field>
struct result_field_base_t
template <typename Db, typename FieldSpec, typename StorageType = typename value_type_of<FieldSpec>::_cpp_value_type>
struct result_field_base
{
static_assert(wrong_t<result_field_base_t>::value, "Invalid argument for result_field_base");
};
template <typename ValueType, typename Db, typename FieldSpec>
struct result_field_base_t<result_field_t<ValueType, Db, FieldSpec>>
{
using _derived_t = result_field_t<ValueType, Db, FieldSpec>;
using _field_spec_t = FieldSpec;
using _db_t = Db;
using _field_spec_t = FieldSpec;
using _cpp_value_type = typename value_type_of<FieldSpec>::_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<const _derived_t&>(*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