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

Changed NULL handling in results.

If the column or the connector indicates that NULL corresponds to a
trivial value, then the trivial value is returned. Otherwise, an
assert fails or an exception is thrown, depending on the preferences of
the connector.
This commit is contained in:
rbock 2014-03-13 08:52:22 +01:00
parent 78d2d9e67a
commit 1824f9cde0
5 changed files with 74 additions and 17 deletions

View File

@ -28,6 +28,7 @@
#define SQLPP_BOOLEAN_H
#include <cstdlib>
#include <cassert>
#include <ostream>
#include <sqlpp11/basic_operators.h>
#include <sqlpp11/type_traits.h>
@ -98,7 +99,7 @@ namespace sqlpp
bool _is_null;
};
template<typename Db, bool TrivialIsNull = false>
template<typename Db, bool NullIsTrivial = false>
struct _result_entry_t
{
_result_entry_t():
@ -134,15 +135,28 @@ namespace sqlpp
bool is_null() const
{
if (not _is_valid)
if (connector_assert_result_validity_t<Db>::value)
assert(_is_valid);
else if (not _is_valid)
throw exception("accessing is_null in non-existing row");
return _is_null;
}
_cpp_value_type value() const
{
if (not _is_valid)
throw exception("accessing value in non-existing row");
const bool null_value = _is_null and not NullIsTrivial and not connector_null_result_is_trivial_value_t<Db>::value;
if (connector_assert_result_validity_t<Db>::value)
{
assert(_is_valid);
assert(not null_value);
}
else
{
if (not _is_valid)
throw exception("accessing value in non-existing row");
if (null_value)
throw exception("accessing value of NULL field");
}
return _value;
}

View File

@ -28,6 +28,7 @@
#define SQLPP_FLOATING_POINT_H
#include <cstdlib>
#include <cassert>
#include <sqlpp11/basic_operators.h>
#include <sqlpp11/type_traits.h>
#include <sqlpp11/exception.h>
@ -98,7 +99,7 @@ namespace sqlpp
bool _is_null;
};
template<typename Db, bool TrivialIsNull = false>
template<typename Db, bool NullIsTrivial = false>
struct _result_entry_t
{
using _value_type = integral;
@ -136,15 +137,28 @@ namespace sqlpp
bool is_null() const
{
if (not _is_valid)
if (connector_assert_result_validity_t<Db>::value)
assert(_is_valid);
else if (not _is_valid)
throw exception("accessing is_null in non-existing row");
return _is_null;
}
_cpp_value_type value() const
{
if (not _is_valid)
throw exception("accessing value in non-existing row");
const bool null_value = _is_null and not NullIsTrivial and not connector_null_result_is_trivial_value_t<Db>::value;
if (connector_assert_result_validity_t<Db>::value)
{
assert(_is_valid);
assert(not null_value);
}
else
{
if (not _is_valid)
throw exception("accessing value in non-existing row");
if (null_value)
throw exception("accessing value of NULL field");
}
return _value;
}

View File

@ -28,6 +28,7 @@
#define SQLPP_INTEGRAL_H
#include <cstdlib>
#include <cassert>
#include <sqlpp11/basic_operators.h>
#include <sqlpp11/type_traits.h>
#include <sqlpp11/exception.h>
@ -136,15 +137,28 @@ namespace sqlpp
bool is_null() const
{
if (not _is_valid)
if (connector_assert_result_validity_t<Db>::value)
assert(_is_valid);
else if (not _is_valid)
throw exception("accessing is_null in non-existing row");
return _is_null;
}
_cpp_value_type value() const
{
if (not _is_valid)
throw exception("accessing value in non-existing row");
const bool null_value = _is_null and not NullIsTrivial and not connector_null_result_is_trivial_value_t<Db>::value;
if (connector_assert_result_validity_t<Db>::value)
{
assert(_is_valid);
assert(not null_value);
}
else
{
if (not _is_valid)
throw exception("accessing value in non-existing row");
if (null_value)
throw exception("accessing value of NULL field");
}
return _value;
}

View File

@ -27,6 +27,7 @@
#ifndef SQLPP_TEXT_H
#define SQLPP_TEXT_H
#include <cassert>
#include <sqlpp11/basic_operators.h>
#include <sqlpp11/type_traits.h>
#include <sqlpp11/exception.h>
@ -97,7 +98,7 @@ namespace sqlpp
bool _is_null;
};
template<typename Db, bool TrivialIsNull = false>
template<typename Db, bool NullIsTrivial = false>
struct _result_entry_t
{
_result_entry_t():
@ -136,16 +137,29 @@ namespace sqlpp
bool is_null() const
{
if (not _is_valid)
if (connector_assert_result_validity_t<Db>::value)
assert(_is_valid);
else if (not _is_valid)
throw exception("accessing is_null in non-existing row");
return _value_ptr == nullptr;
}
_cpp_value_type value() const
{
if (not _is_valid)
throw exception("accessing value in non-existing row");
if (_value_ptr)
const bool null_value = _value_ptr == nullptr and not NullIsTrivial and not connector_null_result_is_trivial_value_t<Db>::value;
if (connector_assert_result_validity_t<Db>::value)
{
assert(_is_valid);
assert(not null_value);
}
else
{
if (not _is_valid)
throw exception("accessing value in non-existing row");
if (null_value)
throw exception("accessing value of NULL field");
}
if (_value_ptr)
return std::string(_value_ptr, _value_ptr + _len);
else
return "";

View File

@ -124,7 +124,8 @@ namespace sqlpp
SQLPP_TYPE_TRAIT_GENERATOR(requires_braces);
SQLPP_TYPE_TRAIT_GENERATOR(is_parameter);
SQLPP_CONNECTOR_TRAIT_GENERATOR(has_empty_list_insert);
SQLPP_CONNECTOR_TRAIT_GENERATOR(null_result_is_trivial_value);
SQLPP_CONNECTOR_TRAIT_GENERATOR(assert_result_validity);
template<typename T, template<typename> class IsTag>
using copy_type_trait = typename std::conditional<IsTag<T>::value, std::true_type, std::false_type>::type;