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

Parameters of prepared statements can be null now

This commit is contained in:
Roland Bock 2013-12-26 19:05:05 +01:00
parent 3ef3faa0f4
commit b4baf38fab
7 changed files with 227 additions and 21 deletions

View File

@ -50,12 +50,6 @@ namespace sqlpp
static constexpr const char* _name = "AND";
};
struct not_
{
using _value_type = boolean;
static constexpr const char* _name = "NOT";
};
// boolean value type
struct boolean
{
@ -65,10 +59,58 @@ namespace sqlpp
using _is_expression = std::true_type;
using _cpp_value_type = bool;
struct plus_
template<bool TrivialValueIsNull>
struct _parameter_t
{
using _value_type = boolean;
static constexpr const char* _name = "+";
_parameter_t():
_value(false),
_is_null(TrivialValueIsNull and _is_trivial())
{}
_parameter_t(const _cpp_value_type& value):
_value(value),
_is_null(TrivialValueIsNull and _is_trivial())
{}
_parameter_t& operator=(const _cpp_value_type& value)
{
_value = value;
_is_null = (TrivialValueIsNull and _is_trivial());
return *this;
}
_parameter_t& operator=(const std::nullptr_t&)
{
_value = false;
_is_null = true;
return *this;
}
template<typename Db>
void serialize(std::ostream& os, Db& db) const
{
os << value();
}
bool _is_trivial() const { return value() == false; }
bool is_null() const
{
return _is_null;
}
_cpp_value_type value() const
{
return _value;
}
operator _cpp_value_type() const { return value(); }
private:
_cpp_value_type _value;
bool _is_null;
};
template<size_t index>

View File

@ -48,6 +48,60 @@ namespace sqlpp
using _is_expression = std::true_type;
using _cpp_value_type = double;
template<bool TrivialValueIsNull>
struct _parameter_t
{
using _value_type = floating_point;
_parameter_t():
_value(0),
_is_null(TrivialValueIsNull and _is_trivial())
{}
_parameter_t(const _cpp_value_type& value):
_value(value),
_is_null(TrivialValueIsNull and _is_trivial())
{}
_parameter_t& operator=(const _cpp_value_type& value)
{
_value = value;
_is_null = (TrivialValueIsNull and _is_trivial());
return *this;
}
_parameter_t& operator=(const std::nullptr_t&)
{
_value = 0;
_is_null = true;
return *this;
}
template<typename Db>
void serialize(std::ostream& os, Db& db) const
{
os << value();
}
bool _is_trivial() const { return value() == 0; }
bool is_null() const
{
return _is_null;
}
_cpp_value_type value() const
{
return _value;
}
operator _cpp_value_type() const { return value(); }
private:
_cpp_value_type _value;
bool _is_null;
};
template<size_t index>
struct _result_entry_t
{

View File

@ -48,6 +48,60 @@ namespace sqlpp
using _is_expression = std::true_type;
using _cpp_value_type = int64_t;
template<bool TrivialValueIsNull>
struct _parameter_t
{
using _value_type = integral;
_parameter_t():
_value(0),
_is_null(TrivialValueIsNull and _is_trivial())
{}
_parameter_t(const _cpp_value_type& value):
_value(value),
_is_null(TrivialValueIsNull and _is_trivial())
{}
_parameter_t& operator=(const _cpp_value_type& value)
{
_value = value;
_is_null = (TrivialValueIsNull and _is_trivial());
return *this;
}
_parameter_t& operator=(const std::nullptr_t&)
{
_value = 0;
_is_null = true;
return *this;
}
template<typename Db>
void serialize(std::ostream& os, Db& db) const
{
os << value();
}
bool _is_trivial() const { return value() == 0; }
bool is_null() const
{
return _is_null;
}
_cpp_value_type value() const
{
return _value;
}
operator _cpp_value_type() const { return value(); }
private:
_cpp_value_type _value;
bool _is_null;
};
template<size_t index>
struct _result_entry_t
{

View File

@ -33,29 +33,31 @@
namespace sqlpp
{
template<typename ValueType, typename NameType>
template<typename ValueType, typename NameType, bool TrivialValueIsNull>
struct parameter_t
{
using _is_parameter = std::true_type;
using _value_type = ValueType;
using _parameter_type = typename ValueType::template _parameter_t<TrivialValueIsNull>;
using _is_parameter = std::true_type;
using _is_expression_t = std::true_type;
template<typename Db>
void serialize(std::ostream& os, Db& db) const
{
static_assert(Db::_supports_parameter, "parameter not supported by current database");
os << " ? ";
os << " ? "; // FIXME: Need to support positional placeholders and also type indicators for postgres for instance
}
using _member_t = typename NameType::_name_t::template _member_t<typename ValueType::_cpp_value_type>;
using _member_t = typename NameType::_name_t::template _member_t<_parameter_type>;
};
template<typename NamedExpr>
template<typename NamedExpr, bool TrivialValueIsNull = trivial_value_is_null_t<typename std::decay<NamedExpr>::type>::value>
auto parameter(NamedExpr&& namedExpr)
-> parameter_t<typename std::decay<NamedExpr>::type::_value_type, typename std::decay<NamedExpr>::type>
-> parameter_t<typename std::decay<NamedExpr>::type::_value_type, typename std::decay<NamedExpr>::type, TrivialValueIsNull>
{
return {};
}
}
#endif

View File

@ -74,7 +74,7 @@ namespace sqlpp
~parameter_list_t() = default;
using parameter_tuple_t = std::tuple<typename Parameter::_value_type::_cpp_value_type&...>;
using parameter_tuple_t = std::tuple<typename Parameter::_parameter_type&...>;
using size = std::tuple_size<parameter_tuple_t>;
parameter_tuple_t _parameter_tuple;
};

View File

@ -48,6 +48,60 @@ namespace sqlpp
using _is_expression = std::true_type;
using _cpp_value_type = std::string;
template<bool TrivialValueIsNull>
struct _parameter_t
{
using _value_type = integral;
_parameter_t():
_value(""),
_is_null(TrivialValueIsNull and _is_trivial())
{}
_parameter_t(const _cpp_value_type& value):
_value(value),
_is_null(TrivialValueIsNull and _is_trivial())
{}
_parameter_t& operator=(const _cpp_value_type& value)
{
_value = value;
_is_null = (TrivialValueIsNull and _is_trivial());
return *this;
}
_parameter_t& operator=(const std::nullptr_t&)
{
_value = "";
_is_null = true;
return *this;
}
template<typename Db>
void serialize(std::ostream& os, Db& db) const
{
os << value();
}
bool _is_trivial() const { return value() == ""; }
bool is_null() const
{
return _is_null;
}
_cpp_value_type value() const
{
return _value;
}
operator _cpp_value_type() const { return value(); }
private:
_cpp_value_type _value;
bool _is_null;
};
template<size_t index>
struct _result_entry_t
{

View File

@ -80,9 +80,9 @@ int main()
using Exp = decltype(t.beta.like(parameter(t.beta)) and t.alpha == parameter(t.alpha) or t.gamma != parameter(t.gamma));
using T = sqlpp::make_parameter_list_t<Exp>::type;
T npl;
static_assert(std::is_same<typename decltype(t.alpha)::_value_type::_cpp_value_type, decltype(npl.alpha)>::value, "type requirement");
static_assert(std::is_same<typename decltype(t.beta)::_value_type::_cpp_value_type, decltype(npl.beta)>::value, "type requirement");
static_assert(std::is_same<typename decltype(t.gamma)::_value_type::_cpp_value_type, decltype(npl.gamma)>::value, "type requirement");
static_assert(std::is_same<typename decltype(t.alpha)::_value_type::_parameter_t<true>, decltype(npl.alpha)>::value, "type requirement");
static_assert(std::is_same<typename decltype(t.beta)::_value_type::_parameter_t<true>, decltype(npl.beta)>::value, "type requirement");
static_assert(std::is_same<typename decltype(t.gamma)::_value_type::_parameter_t<false>, decltype(npl.gamma)>::value, "type requirement");
static_assert(std::is_same<decltype(std::get<0>(npl._parameter_tuple)), decltype(npl.beta)&>::value, "type requirement");
static_assert(std::is_same<decltype(std::get<1>(npl._parameter_tuple)), decltype(npl.alpha)&>::value, "type requirement");
@ -96,9 +96,9 @@ int main()
using T = sqlpp::make_parameter_list_t<S>::type;
T npl;
static_assert(std::is_same<typename decltype(t.alpha)::_value_type::_cpp_value_type, decltype(npl.alpha)>::value, "type requirement");
static_assert(std::is_same<typename decltype(t.beta)::_value_type::_cpp_value_type, decltype(npl.beta)>::value, "type requirement");
static_assert(std::is_same<typename decltype(t.gamma)::_value_type::_cpp_value_type, decltype(npl.gamma)>::value, "type requirement");
static_assert(std::is_same<typename decltype(t.alpha)::_value_type::_parameter_t<true>, decltype(npl.alpha)>::value, "type requirement");
static_assert(std::is_same<typename decltype(t.beta)::_value_type::_parameter_t<true>, decltype(npl.beta)>::value, "type requirement");
static_assert(std::is_same<typename decltype(t.gamma)::_value_type::_parameter_t<false>, decltype(npl.gamma)>::value, "type requirement");
static_assert(std::is_same<decltype(std::get<0>(npl._parameter_tuple)), decltype(npl.beta)&>::value, "type requirement");
static_assert(std::is_same<decltype(std::get<1>(npl._parameter_tuple)), decltype(npl.alpha)&>::value, "type requirement");