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

Rewrote like() member function, added more type control to concat()

This commit is contained in:
Roland Bock 2013-09-19 09:04:07 +02:00
parent 1ab504f908
commit 0db405c28c
4 changed files with 111 additions and 23 deletions

View File

@ -28,22 +28,21 @@
#define SQLPP_CONCAT_H
#include <sstream>
#include <sqlpp11/text.h>
#include <sqlpp11/type_traits.h>
#include <sqlpp11/detail/set.h>
namespace sqlpp
{
namespace detail
{
struct text;
template<typename Text, typename... Args>
struct concat_t: public Text::template operators<concat_t<Text, Args...>>
template<typename First, typename... Args>
struct concat_t: public First::_value_type::template operators<concat_t<First, Args...>>
{
using _valid_args = typename detail::make_set_if_not<is_text_t, Args...>::type;
static_assert(sizeof...(Args) > 0, "concat requires two arguments at least");
using _valid_args = typename detail::make_set_if_not<is_text_t, First, Args...>::type;
static_assert(_valid_args::size::value == 0, "at least one non-text argument detected in concat()");
using _value_type = Text;
using _value_type = typename First::_value_type::_base_value_type;
struct _name_t
{
static constexpr const char* _get_name() { return "CONCAT"; }
@ -54,12 +53,12 @@ namespace sqlpp
};
};
concat_t(Args&&... args):
_args(std::move(args)...)
concat_t(First&& first, Args&&... args):
_args(std::move(first), std::move(args)...)
{}
concat_t(const Args&... args):
_args(args...)
concat_t(const First& first, const Args&... args):
_args(first, args...)
{}
concat_t(const concat_t&) = default;
@ -77,12 +76,12 @@ namespace sqlpp
}
private:
std::tuple<Args...> _args;
std::tuple<First, Args...> _args;
};
}
template<typename... T>
auto concat(T&&... t) -> typename detail::concat_t<detail::text, typename std::decay<T>::type...>::type
auto concat(T&&... t) -> typename detail::concat_t<typename operand_t<T, is_text_t>::type...>
{
return { std::forward<T>(t)... };
}

95
include/sqlpp11/like.h Normal file
View File

@ -0,0 +1,95 @@
/*
* 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_LIKE_H
#define SQLPP_LIKE_H
#include <sstream>
#include <sqlpp11/type_traits.h>
#include <sqlpp11/detail/set.h>
namespace sqlpp
{
namespace detail
{
struct text;
template<typename Operand, typename Pattern>
struct like_t: public Operand::_value_type::template operators<like_t<Operand, Pattern>>
{
static_assert(is_text_t<Operand>::value, "Operand for like() has to be a text");
static_assert(is_text_t<Pattern>::value, "Pattern for like() has to be a text");
using _value_type = typename Operand::_value_type::_base_value_type;
struct _name_t
{
static constexpr const char* _get_name() { return "LIKE"; }
template<typename T>
struct _member_t
{
T like;
};
};
like_t(Operand&& operand, Pattern&& pattern):
_operand(std::move(operand)),
_pattern(std::move(pattern))
{}
like_t(const Operand& operand, const Pattern& pattern):
_operand(operand),
_pattern(pattern)
{}
like_t(const like_t&) = default;
like_t(like_t&&) = default;
like_t& operator=(const like_t&) = default;
like_t& operator=(like_t&&) = default;
~like_t() = default;
template<typename Db>
void serialize(std::ostream& os, Db& db) const
{
_operand.serialize(os, db);
os << " LIKE(";
_pattern.serialize(os, db);
os << ")";
}
private:
Operand _operand;
Pattern _pattern;
};
}
template<typename... T>
auto like(T&&... t) -> typename detail::like_t<typename operand_t<T, is_text_t>::type...>
{
return { std::forward<T>(t)... };
}
}
#endif

View File

@ -33,19 +33,12 @@
#include <sqlpp11/raw_result_row.h>
#include <sqlpp11/exception.h>
#include <sqlpp11/concat.h>
#include <sqlpp11/like.h>
namespace sqlpp
{
namespace detail
{
struct text;
struct like_
{
using _value_type = boolean;
static constexpr const char* _name = "LIKE";
};
// text value type
struct text
{
@ -111,13 +104,13 @@ namespace sqlpp
struct operators: public basic_operators<Base, _constraint>
{
template<typename T>
detail::concat_t<text, Base, typename _constraint<T>::type> operator+(T&& t) const
detail::concat_t<Base, typename _constraint<T>::type> operator+(T&& t) const
{
return { *static_cast<const Base*>(this), std::forward<T>(t) };
}
template<typename T>
nary_member_function_t<Base, like_, typename _constraint<T>::type> like(T&& t) const
detail::like_t<Base, typename _constraint<T>::type> like(T&& t) const
{
return { *static_cast<const Base*>(this), std::forward<T>(t) };
}

View File

@ -276,6 +276,7 @@ int main()
!t.gamma;
t.beta < "kaesekuchen";
(t.beta + "hallenhalma").serialize(std::cerr, db);
concat(t.beta, "hallenhalma").serialize(std::cerr, db);
static_assert(sqlpp::must_not_insert_t<decltype(t.alpha)>::value, "alpha must not be inserted");
t.alpha.serialize(std::cerr, db);
std::cerr << "\n" << sizeof(TabSample) << std::endl;