Added sqlpp::null and sql::default_value for insert and update

This commit is contained in:
rbock
2014-01-25 22:38:16 +01:00
parent f443564d62
commit f802300685
14 changed files with 309 additions and 80 deletions

View File

@@ -160,20 +160,22 @@ namespace sqlpp
};
template<typename T>
using _constraint = operand_t<T, is_boolean_t>;
using _operand_t = operand_t<T, is_boolean_t>;
template<typename T>
using _constraint = is_boolean_t<T>;
template<typename Base>
struct operators: public basic_operators<Base, _constraint>
struct operators: public basic_operators<Base, _operand_t>
{
template<typename T>
vendor::logical_and_t<Base, typename _constraint<T>::type> operator and(T&& t) const
vendor::logical_and_t<Base, typename _operand_t<T>::type> operator and(T&& t) const
{
static_assert(not is_multi_expression_t<Base>::value, "multi-expression cannot be used as left hand side operand");
return { *static_cast<const Base*>(this), {std::forward<T>(t)} };
}
template<typename T>
vendor::logical_or_t<Base, typename _constraint<T>::type> operator or(T&& t) const
vendor::logical_or_t<Base, typename _operand_t<T>::type> operator or(T&& t) const
{
static_assert(not is_multi_expression_t<Base>::value, "multi-expression cannot be used as left hand side operand");
return { *static_cast<const Base*>(this), {std::forward<T>(t)} };

View File

@@ -29,10 +29,13 @@
#include <sqlpp11/alias.h>
#include <sqlpp11/column_fwd.h>
#include <sqlpp11/default_value.h>
#include <sqlpp11/null.h>
#include <sqlpp11/sort_order.h>
#include <sqlpp11/type_traits.h>
#include <sqlpp11/vendor/interpreter.h>
#include <sqlpp11/vendor/assignment.h>
#include <sqlpp11/vendor/expression.h>
#include <sqlpp11/vendor/interpreter.h>
#include <sqlpp11/detail/wrong.h>
namespace sqlpp
@@ -70,11 +73,24 @@ namespace sqlpp
}
template<typename T>
auto operator =(T&& t) const
-> typename std::enable_if<not std::is_same<column_t, typename std::decay<T>::type>::value,
vendor::assignment_t<column_t, typename _value_type::template _constraint<T>::type>>::type
auto operator =(T t) const
-> typename std::enable_if<_value_type::template _constraint<typename vendor::wrap_operand<T>::type>::value and not std::is_same<column_t, T>::value,
vendor::assignment_t<column_t, typename vendor::wrap_operand<T>::type>>::type
{
return { *this, {std::forward<T>(t)} };
return { *this, {t} };
}
auto operator =(sqlpp::null_t) const
->vendor::assignment_t<column_t, sqlpp::null_t>
{
static_assert(can_be_null_t<column_t>::value, "column cannot be null");
return { *this, {} };
}
auto operator =(sqlpp::default_value_t) const
->vendor::assignment_t<column_t, sqlpp::default_value_t>
{
return { *this, {} };
}
};

View File

@@ -0,0 +1,61 @@
/*
* Copyright (c) 2013, Roland Bock
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef SQLPP_DEFAULT_VALUE_H
#define SQLPP_DEFAULT_VALUE_H
#include <sqlpp11/no_value.h>
namespace sqlpp
{
struct default_value_t
{
static constexpr bool _is_expression = true;
using _value_type = no_value_t;
static constexpr bool _is_trivial() { return false; }
};
namespace vendor
{
template<typename Context>
struct interpreter_t<Context, default_value_t>
{
using Operand = default_value_t;
static Context& _(const Operand& t, Context& context)
{
context << "DEFAULT";
return context;
}
};
}
constexpr default_value_t default_value = {};
}
#endif

View File

@@ -162,34 +162,36 @@ namespace sqlpp
};
template<typename T>
using _constraint = operand_t<T, is_numeric_t>;
using _operand_t = operand_t<T, is_numeric_t>;
template<typename T>
using _constraint = is_numeric_t<T>;
template<typename Base>
struct operators: public basic_operators<Base, _constraint>
struct operators: public basic_operators<Base, _operand_t>
{
template<typename T>
vendor::plus_t<Base, floating_point, typename _constraint<T>::type> operator +(T&& t) const
vendor::plus_t<Base, floating_point, typename _operand_t<T>::type> operator +(T&& t) const
{
static_assert(not is_multi_expression_t<Base>::value, "multi-expression cannot be used as left hand side operand");
return { *static_cast<const Base*>(this), {std::forward<T>(t)} };
}
template<typename T>
vendor::minus_t<Base, floating_point, typename _constraint<T>::type> operator -(T&& t) const
vendor::minus_t<Base, floating_point, typename _operand_t<T>::type> operator -(T&& t) const
{
static_assert(not is_multi_expression_t<Base>::value, "multi-expression cannot be used as left hand side operand");
return { *static_cast<const Base*>(this), {std::forward<T>(t)} };
}
template<typename T>
vendor::multiplies_t<Base, floating_point, typename _constraint<T>::type> operator *(T&& t) const
vendor::multiplies_t<Base, floating_point, typename _operand_t<T>::type> operator *(T&& t) const
{
static_assert(not is_multi_expression_t<Base>::value, "multi-expression cannot be used as left hand side operand");
return { *static_cast<const Base*>(this), {std::forward<T>(t)} };
}
template<typename T>
vendor::divides_t<Base, typename _constraint<T>::type> operator /(T&& t) const
vendor::divides_t<Base, typename _operand_t<T>::type> operator /(T&& t) const
{
static_assert(not is_multi_expression_t<Base>::value, "multi-expression cannot be used as left hand side operand");
return { *static_cast<const Base*>(this), {std::forward<T>(t)} };

View File

@@ -162,41 +162,43 @@ namespace sqlpp
};
template<typename T>
using _constraint = operand_t<T, is_numeric_t>;
using _operand_t = operand_t<T, is_numeric_t>;
template<typename T>
using _constraint = is_numeric_t<T>;
template<typename Base>
struct operators: public basic_operators<Base, _constraint>
struct operators: public basic_operators<Base, _operand_t>
{
template<typename T>
vendor::plus_t<Base, vendor::value_type_t<T>, typename _constraint<T>::type> operator +(T&& t) const
vendor::plus_t<Base, vendor::value_type_t<T>, typename _operand_t<T>::type> operator +(T&& t) const
{
static_assert(not is_multi_expression_t<Base>::value, "multi-expression cannot be used as left hand side operand");
return { *static_cast<const Base*>(this), {std::forward<T>(t)} };
}
template<typename T>
vendor::minus_t<Base, vendor::value_type_t<T>, typename _constraint<T>::type> operator -(T&& t) const
vendor::minus_t<Base, vendor::value_type_t<T>, typename _operand_t<T>::type> operator -(T&& t) const
{
static_assert(not is_multi_expression_t<Base>::value, "multi-expression cannot be used as left hand side operand");
return { *static_cast<const Base*>(this), {std::forward<T>(t)} };
}
template<typename T>
vendor::multiplies_t<Base, vendor::value_type_t<T>, typename _constraint<T>::type> operator *(T&& t) const
vendor::multiplies_t<Base, vendor::value_type_t<T>, typename _operand_t<T>::type> operator *(T&& t) const
{
static_assert(not is_multi_expression_t<Base>::value, "multi-expression cannot be used as left hand side operand");
return { *static_cast<const Base*>(this), {std::forward<T>(t)} };
}
template<typename T>
vendor::divides_t<Base, typename _constraint<T>::type> operator /(T&& t) const
vendor::divides_t<Base, typename _operand_t<T>::type> operator /(T&& t) const
{
static_assert(not is_multi_expression_t<Base>::value, "multi-expression cannot be used as left hand side operand");
return { *static_cast<const Base*>(this), {std::forward<T>(t)} };
}
template<typename T>
vendor::modulus_t<Base, typename _constraint<T>::type> operator %(T&& t) const
vendor::modulus_t<Base, typename _operand_t<T>::type> operator %(T&& t) const
{
static_assert(not is_multi_expression_t<Base>::value, "multi-expression cannot be used as left hand side operand");
return { *static_cast<const Base*>(this), {std::forward<T>(t)} };

61
include/sqlpp11/null.h Normal file
View File

@@ -0,0 +1,61 @@
/*
* Copyright (c) 2013, Roland Bock
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef SQLPP_NULL_H
#define SQLPP_NULL_H
#include <sqlpp11/no_value.h>
namespace sqlpp
{
struct null_t
{
static constexpr bool _is_expression = true;
using _value_type = no_value_t;
static constexpr bool _is_trivial() { return false; }
};
namespace vendor
{
template<typename Context>
struct interpreter_t<Context, null_t>
{
using Operand = null_t;
static Context& _(const Operand& t, Context& context)
{
context << "NULL";
return context;
}
};
}
constexpr null_t null = {};
}
#endif

View File

@@ -166,20 +166,22 @@ namespace sqlpp
};
template<typename T>
using _constraint = operand_t<T, is_text_t>;
using _operand_t = operand_t<T, is_text_t>;
template<typename T>
using _constraint = is_text_t<T>;
template<typename Base>
struct operators: public basic_operators<Base, _constraint>
struct operators: public basic_operators<Base, _operand_t>
{
template<typename T>
vendor::concat_t<Base, typename _constraint<T>::type> operator+(T&& t) const
vendor::concat_t<Base, typename _operand_t<T>::type> operator+(T&& t) const
{
static_assert(not is_multi_expression_t<Base>::value, "multi-expression cannot be used as left hand side operand");
return { *static_cast<const Base*>(this), {std::forward<T>(t)} };
}
template<typename T>
vendor::like_t<Base, typename _constraint<T>::type> like(T&& t) const
vendor::like_t<Base, typename _operand_t<T>::type> like(T&& t) const
{
static_assert(not is_multi_expression_t<Base>::value, "multi-expression cannot be used as left hand side operand");
return { *static_cast<const Base*>(this), {std::forward<T>(t)} };

126
include/sqlpp11/vendor/assignment.h vendored Normal file
View File

@@ -0,0 +1,126 @@
/*
* Copyright (c) 2013, Roland Bock
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef SQLPP_ASSIGNMENT_H
#define SQLPP_ASSIGNMENT_H
#include <sqlpp11/default_value.h>
#include <sqlpp11/null.h>
#include <sqlpp11/tvin.h>
#include <sqlpp11/vendor/interpreter.h>
namespace sqlpp
{
namespace vendor
{
template<typename Lhs, typename Rhs>
struct assignment_t
{
using _is_assignment = std::true_type;
using _column_t = Lhs;
using value_type = Rhs;
using _parameter_tuple_t = std::tuple<_column_t, Rhs>;
static_assert(not std::is_same<Rhs, null_t>::value or can_be_null_t<_column_t>::value, "column cannot be null");
assignment_t(_column_t lhs, value_type rhs):
_lhs(lhs),
_rhs(rhs)
{}
assignment_t(const assignment_t&) = default;
assignment_t(assignment_t&&) = default;
assignment_t& operator=(const assignment_t&) = default;
assignment_t& operator=(assignment_t&&) = default;
~assignment_t() = default;
_column_t _lhs;
value_type _rhs;
};
template<typename Context, typename Lhs, typename Rhs>
struct interpreter_t<Context, assignment_t<Lhs, Rhs>>
{
using T = assignment_t<Lhs, Rhs>;
static Context& _(const T& t, Context& context)
{
interpret(t._lhs, context);
context << "=";
interpret(t._rhs, context);
return context;
}
};
template<typename Lhs, typename Rhs>
struct assignment_t<Lhs, tvin_t<Rhs>>
{
using _is_assignment = std::true_type;
using _column_t = Lhs;
using value_type = tvin_t<Rhs>;
using _parameter_tuple_t = std::tuple<_column_t, Rhs>;
static_assert(can_be_null_t<_column_t>::value, "column cannot be null");
assignment_t(_column_t lhs, value_type rhs):
_lhs(lhs),
_rhs(rhs)
{}
assignment_t(const assignment_t&) = default;
assignment_t(assignment_t&&) = default;
assignment_t& operator=(const assignment_t&) = default;
assignment_t& operator=(assignment_t&&) = default;
~assignment_t() = default;
_column_t _lhs;
value_type _rhs;
};
template<typename Context, typename Lhs, typename Rhs>
struct interpreter_t<Context, assignment_t<Lhs, tvin_t<Rhs>>>
{
using T = assignment_t<Lhs, tvin_t<Rhs>>;
static Context& _(const T& t, Context& context)
{
interpret(t._lhs, context);
if (t._rhs._value._is_trivial())
{
context << "=NULL";
}
else
{
context << "=";
interpret(t._rhs._value, context);
}
return context;
}
};
}
}
#endif

View File

@@ -39,51 +39,6 @@ namespace sqlpp
{
namespace vendor
{
template<typename Lhs, typename Rhs>
struct assignment_t
{
using _is_assignment = std::true_type;
using column_type = Lhs;
using value_type = tvin_wrap_t<Rhs>;
using _parameter_tuple_t = std::tuple<Lhs, Rhs>;
assignment_t(Lhs lhs, Rhs rhs):
_lhs(lhs),
_rhs(rhs)
{}
assignment_t(const assignment_t&) = default;
assignment_t(assignment_t&&) = default;
assignment_t& operator=(const assignment_t&) = default;
assignment_t& operator=(assignment_t&&) = default;
~assignment_t() = default;
Lhs _lhs;
tvin_wrap_t<Rhs> _rhs;
};
template<typename Context, typename Lhs, typename Rhs>
struct interpreter_t<Context, assignment_t<Lhs, Rhs>>
{
using T = assignment_t<Lhs, Rhs>;
static Context& _(const T& t, Context& context)
{
interpret(t._lhs, context);
if (t._rhs._is_trivial())
{
context << "=NULL";
}
else
{
context << "=";
interpret(t._rhs, context);
}
return context;
}
};
template<typename Lhs, typename Rhs>
struct equal_t: public detail::boolean::template operators<equal_t<Lhs, Rhs>>
{

View File

@@ -31,9 +31,6 @@ namespace sqlpp
{
namespace vendor
{
template<typename Lhs, typename Rhs>
struct assignment_t;
template<typename Lhs, typename Rhs>
struct equal_t;

View File

@@ -91,7 +91,7 @@ namespace sqlpp
static_assert(_assignment_set::size::value == sizeof...(Assignments), "at least one argument is not an assignment in set()");
// check for prohibited assignments
using _prohibited_assignment_set = typename ::sqlpp::detail::make_set_if<must_not_insert_t, typename Assignments::column_type...>::type;
using _prohibited_assignment_set = typename ::sqlpp::detail::make_set_if<must_not_insert_t, typename Assignments::_column_t...>::type;
static_assert(_prohibited_assignment_set::size::value == 0, "at least one assignment is prohibited by its column definition in set()");
insert_list_t(Assignments... assignment):
@@ -110,12 +110,12 @@ namespace sqlpp
{
static_assert(is_assignment_t<typename std::decay<Assignment>::type>::value, "set() arguments require to be assigments");
static_assert(not must_not_insert_t<typename std::decay<Assignment>::type>::value, "set() argument must not be used in insert");
_dynamic_columns.emplace_back(insert_column_t<typename Assignment::column_type>{std::forward<typename Assignment::column_type>(assignment._lhs)});
_dynamic_columns.emplace_back(insert_column_t<typename Assignment::_column_t>{std::forward<typename Assignment::_column_t>(assignment._lhs)});
_dynamic_values.emplace_back(std::forward<typename Assignment::value_type>(assignment._rhs));
}
std::tuple<insert_column_t<typename Assignments::column_type>...> _columns;
std::tuple<insert_column_t<typename Assignments::_column_t>...> _columns;
std::tuple<typename Assignments::value_type...> _values;
typename vendor::interpretable_list_t<Database> _dynamic_columns;
typename vendor::interpretable_list_t<Database> _dynamic_values;

View File

@@ -54,14 +54,14 @@ namespace sqlpp
static_assert(_assignment_set::size::value == sizeof...(Assignments), "at least one argument is not an assignment in set()");
// check for prohibited assignments
using _prohibited_assignment_set = typename ::sqlpp::detail::make_set_if<must_not_update_t, typename Assignments::column_type...>::type;
using _prohibited_assignment_set = typename ::sqlpp::detail::make_set_if<must_not_update_t, typename Assignments::_column_t...>::type;
static_assert(_prohibited_assignment_set::size::value == 0, "at least one assignment is prohibited by its column definition in set()");
template<typename Assignment>
void add(Assignment&& assignment)
{
static_assert(is_assignment_t<typename std::decay<Assignment>::type>::value, "set() arguments require to be assigments");
static_assert(not must_not_update_t<typename std::decay<Assignment>::type::column_type>::value, "set() argument must not be updated");
static_assert(not must_not_update_t<typename std::decay<Assignment>::type::_column_t>::value, "set() argument must not be updated");
_dynamic_assignments.emplace_back(std::forward<Assignment>(assignment));
}

View File

@@ -162,6 +162,9 @@ namespace sqlpp
{
using type = text_operand;
};
// FIXME: Need to allow std::ref arguments
// FIXME: insert requires to work with value_list
}
}

View File

@@ -43,6 +43,8 @@ int main()
TabSample t;
TabFoo f;
interpret(t.alpha = sqlpp::null, printer).flush();
interpret(t.alpha = sqlpp::default_value, printer).flush();
interpret(t.alpha, printer).flush();
interpret(-t.alpha, printer).flush();
interpret(+t.alpha, printer).flush();
@@ -71,7 +73,7 @@ int main()
interpret(insert_into(t), printer).flush();
interpret(insert_into(f).default_values(), printer).flush();
interpret(insert_into(t).set(t.gamma = true), printer).flush();
interpret(insert_into(t).set(t.gamma = sqlpp::tvin(false)), printer).flush();
//interpret(insert_into(t).set(t.gamma = sqlpp::tvin(false)), printer).flush(); cannot test this since gamma cannot be null and a static assert is thrown
interpret(update(t), printer).flush();
interpret(update(t).set(t.gamma = true), printer).flush();