0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-11-16 04:47:18 +08:00

Extracted result_field_t from value types.

This is a preparation for adding serializer specializations for them
This commit is contained in:
rbock 2014-07-30 22:37:07 +02:00
parent 7c45e8b83f
commit f60f1504b8
6 changed files with 320 additions and 301 deletions

View File

@ -32,7 +32,7 @@
#include <sqlpp11/basic_expression_operators.h>
#include <sqlpp11/type_traits.h>
#include <sqlpp11/exception.h>
#include <sqlpp11/result_field_methods.h>
#include <sqlpp11/result_field.h>
namespace sqlpp
{
@ -97,73 +97,6 @@ namespace sqlpp
bool _is_null;
};
template<typename Db, typename FieldSpec>
struct _result_field_t: public result_field_methods_t<_result_field_t<Db, FieldSpec>>
{
_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)
{
target._bind_boolean_result(i, &_value, &_is_null);
}
private:
bool _is_valid;
bool _is_null;
signed char _value;
};
template<typename T>
struct _is_valid_operand
{
@ -206,13 +139,85 @@ namespace sqlpp
};
};
template<typename Db, typename FieldSpec>
inline std::ostream& operator<<(std::ostream& os, const boolean::_result_field_t<Db, FieldSpec>& e)
{
return os << e.value();
}
}
template<typename Db, typename FieldSpec>
struct result_field_t<detail::boolean, Db, FieldSpec>: public result_field_methods_t<result_field_t<detail::boolean, Db, FieldSpec>>
{
static_assert(std::is_same<value_type_of<FieldSpec>, 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<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)
{
target._bind_boolean_result(i, &_value, &_is_null);
}
private:
bool _is_valid;
bool _is_null;
signed char _value;
};
template<typename Db, typename FieldSpec>
inline std::ostream& operator<<(std::ostream& os, const result_field_t<detail::boolean, Db, FieldSpec>& e)
{
return os << e.value();
}
using boolean = detail::boolean;
}

View File

@ -31,7 +31,7 @@
#include <sqlpp11/basic_expression_operators.h>
#include <sqlpp11/type_traits.h>
#include <sqlpp11/exception.h>
#include <sqlpp11/result_field_methods.h>
#include <sqlpp11/result_field.h>
namespace sqlpp
{
@ -96,73 +96,6 @@ namespace sqlpp
bool _is_null;
};
template<typename Db, typename FieldSpec>
struct _result_field_t: public result_field_methods_t<_result_field_t<Db, FieldSpec>>
{
_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)
{
target._bind_floating_point_result(i, &_value, &_is_null);
}
private:
bool _is_valid;
bool _is_null;
_cpp_value_type _value;
};
template<typename T>
struct _is_valid_operand
{
@ -261,13 +194,85 @@ namespace sqlpp
};
};
template<typename Db, typename FieldSpec>
inline std::ostream& operator<<(std::ostream& os, const floating_point::_result_field_t<Db, FieldSpec>& e)
{
return os << e.value();
}
}
template<typename Db, typename FieldSpec>
struct result_field_t<detail::floating_point, Db, FieldSpec>: public result_field_methods_t<result_field_t<detail::floating_point, Db, FieldSpec>>
{
static_assert(std::is_same<value_type_of<FieldSpec>, 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<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)
{
target._bind_floating_point_result(i, &_value, &_is_null);
}
private:
bool _is_valid;
bool _is_null;
_cpp_value_type _value;
};
template<typename Db, typename FieldSpec>
inline std::ostream& operator<<(std::ostream& os, const result_field_t<detail::floating_point, Db, FieldSpec>& e)
{
return os << e.value();
}
using floating_point = detail::floating_point;
}

View File

@ -33,7 +33,7 @@
#include <sqlpp11/exception.h>
#include <sqlpp11/value_type.h>
#include <sqlpp11/assignment.h>
#include <sqlpp11/result_field_methods.h>
#include <sqlpp11/result_field.h>
namespace sqlpp
{
@ -97,73 +97,6 @@ namespace sqlpp
bool _is_null;
};
template<typename Db, typename FieldSpec>
struct _result_field_t: public result_field_methods_t<_result_field_t<Db, FieldSpec>>
{
_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)
{
target._bind_integral_result(i, &_value, &_is_null);
}
private:
bool _is_valid;
bool _is_null;
_cpp_value_type _value;
};
template<typename T>
struct _is_valid_operand
{
@ -273,13 +206,84 @@ namespace sqlpp
};
};
template<typename Db, typename FieldSpec>
inline std::ostream& operator<<(std::ostream& os, const integral::_result_field_t<Db, FieldSpec>& e)
{
return os << e.value();
}
}
template<typename Db, typename FieldSpec>
struct result_field_t<detail::integral, Db, FieldSpec>: public result_field_methods_t<result_field_t<detail::integral, Db, FieldSpec>>
{
static_assert(std::is_same<value_type_of<FieldSpec>, 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<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)
{
target._bind_integral_result(i, &_value, &_is_null);
}
private:
bool _is_valid;
bool _is_null;
_cpp_value_type _value;
};
template<typename Db, typename FieldSpec>
inline std::ostream& operator<<(std::ostream& os, const result_field_t<detail::integral, Db, FieldSpec>& e)
{
return os << e.value();
}
using tinyint = detail::integral;
using smallint = detail::integral;
using integer = detail::integral;

View File

@ -40,8 +40,8 @@ namespace sqlpp
static_assert(wrong_t<Field>::value, "Invalid argument for get_field_spec");
};
template<template<typename, typename> class Field, typename Db, typename FieldSpec>
struct get_field_spec_impl<Field<Db, FieldSpec>>
template<template<typename, typename, typename> class Field, typename ValueType, typename Db, typename FieldSpec>
struct get_field_spec_impl<Field<ValueType, Db, FieldSpec>>
{
using type = FieldSpec;
};
@ -58,9 +58,9 @@ namespace sqlpp
operator cpp_value_type_of<_field_spec_t>() const { return static_cast<const Field&>(*this).value(); }
};
template<template<typename, typename> class Field, typename Db, typename FieldSpec>
template<template<typename, typename, typename> class Field, typename ValueType, typename Db, typename FieldSpec>
struct result_field_methods_base_t<
Field<Db, FieldSpec>,
Field<ValueType, Db, FieldSpec>,
typename std::enable_if<enforce_null_result_treatment_t<Db>::value
and column_spec_can_be_null_t<FieldSpec>::value
and not null_is_trivial_value_t<FieldSpec>::value>::type>

View File

@ -41,9 +41,9 @@ namespace sqlpp
template<typename Db, std::size_t index, typename FieldSpec>
struct result_field:
public FieldSpec::_name_t::template _member_t<typename value_type_of<FieldSpec>::template _result_field_t<Db, FieldSpec>>
public FieldSpec::_name_t::template _member_t<result_field_t<value_type_of<FieldSpec>, Db, FieldSpec>>
{
using _field = typename FieldSpec::_name_t::template _member_t<typename value_type_of<FieldSpec>::template _result_field_t<Db, FieldSpec>>;
using _field = typename FieldSpec::_name_t::template _member_t<result_field_t<value_type_of<FieldSpec>, Db, FieldSpec>>;
result_field() = default;
@ -189,7 +189,7 @@ namespace sqlpp
struct _name_t {};
};
using _field_type = detail::text::_result_field_t<Db, _field_spec_t>;
using _field_type = result_field_t<detail::text, Db, _field_spec_t>;
static constexpr size_t _last_static_index = _impl::_last_index;
bool _is_valid;

View File

@ -32,7 +32,7 @@
#include <sqlpp11/exception.h>
#include <sqlpp11/concat.h>
#include <sqlpp11/like.h>
#include <sqlpp11/result_field_methods.h>
#include <sqlpp11/result_field.h>
namespace sqlpp
{
@ -96,75 +96,6 @@ namespace sqlpp
bool _is_null;
};
template<typename Db, typename FieldSpec>
struct _result_field_t: public result_field_methods_t<_result_field_t<Db, FieldSpec>>
{
_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)
{
target._bind_text_result(i, &_value_ptr, &_len);
}
private:
bool _is_valid;
const char* _value_ptr;
size_t _len;
};
template<typename T>
struct _is_valid_operand
@ -211,13 +142,87 @@ namespace sqlpp
};
};
template<typename Db, typename FieldSpec>
inline std::ostream& operator<<(std::ostream& os, const text::_result_field_t<Db, FieldSpec>& e)
{
return os << e.value();
}
}
template<typename Db, typename FieldSpec>
struct result_field_t<detail::text, Db, FieldSpec>: public result_field_methods_t<result_field_t<detail::text, Db, FieldSpec>>
{
static_assert(std::is_same<value_type_of<FieldSpec>, 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<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)
{
target._bind_text_result(i, &_value_ptr, &_len);
}
private:
bool _is_valid;
const char* _value_ptr;
size_t _len;
};
template<typename Db, typename FieldSpec>
inline std::ostream& operator<<(std::ostream& os, const result_field_t<detail::text, Db, FieldSpec>& e)
{
return os << e.value();
}
using text = detail::text;
using blob = detail::text;
using varchar = detail::text;