mirror of
https://github.com/rbock/sqlpp11.git
synced 2024-11-15 20:31:16 +08:00
Adjusted insert and update to work with sqlite
Introduced first connector trait to change query generation.
This commit is contained in:
parent
81f1735de8
commit
9c1e75cd89
@ -59,6 +59,12 @@ namespace sqlpp
|
||||
os << Table::_name_t::_get_name() << '.' << _name_t::_get_name();
|
||||
}
|
||||
|
||||
template<typename Db>
|
||||
void serialize_name(std::ostream& os, Db& db) const
|
||||
{
|
||||
os << _name_t::_get_name();
|
||||
}
|
||||
|
||||
template<typename alias_provider>
|
||||
expression_alias_t<column_t, typename std::decay<alias_provider>::type> as(alias_provider&&) const
|
||||
{
|
||||
|
@ -39,11 +39,12 @@ namespace sqlpp
|
||||
{
|
||||
using _is_assignment = tag_yes;
|
||||
using column_type = Lhs;
|
||||
using value_type = Rhs;
|
||||
|
||||
template<typename Db>
|
||||
void serialize(std::ostream& os, Db& db) const
|
||||
{
|
||||
_lhs.serialize(os, db);
|
||||
_lhs.serialize_name(os, db);
|
||||
if (trivial_value_is_null_t<Lhs>::value and _rhs._is_trivial())
|
||||
{
|
||||
os << "=NULL";
|
||||
|
@ -30,7 +30,7 @@
|
||||
#include <sstream>
|
||||
#include <sqlpp11/noop.h>
|
||||
#include <sqlpp11/select_fwd.h>
|
||||
#include <sqlpp11/assignment_list.h>
|
||||
#include <sqlpp11/insert_list.h>
|
||||
#include <sqlpp11/type_traits.h>
|
||||
#include <sqlpp11/detail/serialize_tuple.h>
|
||||
|
||||
@ -43,29 +43,29 @@ namespace sqlpp
|
||||
// insert.
|
||||
template<
|
||||
typename Table = noop,
|
||||
typename Assignments = noop
|
||||
typename InsertList = noop
|
||||
>
|
||||
struct insert_t;
|
||||
|
||||
template<
|
||||
typename Table,
|
||||
typename Assignments
|
||||
typename InsertList
|
||||
>
|
||||
struct insert_t
|
||||
{
|
||||
static_assert(is_noop<Table>::value or is_table_t<Table>::value, "invalid 'Table' argument");
|
||||
static_assert(is_noop<Assignments>::value or is_assignment_list_t<Assignments>::value, "invalid 'Assignments' arguments");
|
||||
static_assert(is_noop<InsertList>::value or is_insert_list_t<InsertList>::value, "invalid 'InsertList' argument");
|
||||
|
||||
template<typename... Assignment>
|
||||
using add_assignments_t = insert_t<Table, assignment_list_t<must_not_insert_t, typename std::decay<Assignment>::type...>>;
|
||||
using add_insert_list_t = insert_t<Table, insert_list_t<must_not_insert_t, typename std::decay<Assignment>::type...>>;
|
||||
|
||||
template<typename... Assignment>
|
||||
add_assignments_t<Assignment...> set(Assignment&&... assignment)
|
||||
add_insert_list_t<Assignment...> set(Assignment&&... assignment)
|
||||
{
|
||||
static_assert(std::is_same<Assignments, noop>::value, "cannot call set() twice");
|
||||
static_assert(std::is_same<InsertList, noop>::value, "cannot call set() twice");
|
||||
return {
|
||||
_table,
|
||||
{std::tuple<typename std::decay<Assignment>::type...>{std::forward<Assignment>(assignment)...}},
|
||||
insert_list_t<must_not_insert_t, Assignment...>{std::forward<Assignment>(assignment)...},
|
||||
};
|
||||
}
|
||||
|
||||
@ -74,10 +74,15 @@ namespace sqlpp
|
||||
{
|
||||
os << "INSERT INTO ";
|
||||
_table.serialize(os, db);
|
||||
if (is_noop<Assignments>::value)
|
||||
os << "() VALUES()";
|
||||
if (is_noop<InsertList>::value)
|
||||
{
|
||||
if (connector_has_empty_list_insert_t<typename std::decay<Db>::type>::value)
|
||||
os << " () VALUES()";
|
||||
else
|
||||
_assignments.serialize(os, db);
|
||||
os << " DEFAULT VALUES";
|
||||
}
|
||||
else
|
||||
_insert_list.serialize(os, db);
|
||||
return *this;
|
||||
}
|
||||
|
||||
@ -91,7 +96,7 @@ namespace sqlpp
|
||||
template<typename Db>
|
||||
std::size_t run(Db& db) const
|
||||
{
|
||||
constexpr bool calledSet = not is_noop<Assignments>::value;
|
||||
constexpr bool calledSet = not is_noop<InsertList>::value;
|
||||
constexpr bool requireSet = Table::_required_insert_columns::size::value > 0;
|
||||
static_assert(calledSet or not requireSet, "calling set() required for given table");
|
||||
std::ostringstream oss;
|
||||
@ -100,7 +105,7 @@ namespace sqlpp
|
||||
}
|
||||
|
||||
Table _table;
|
||||
Assignments _assignments;
|
||||
InsertList _insert_list;
|
||||
};
|
||||
|
||||
template<typename Table>
|
||||
|
96
include/sqlpp11/insert_list.h
Normal file
96
include/sqlpp11/insert_list.h
Normal file
@ -0,0 +1,96 @@
|
||||
/*
|
||||
* 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_INSERT_LIST_H
|
||||
#define SQLPP_INSERT_LIST_H
|
||||
|
||||
#include <sqlpp11/detail/set.h>
|
||||
#include <sqlpp11/detail/serialize_tuple.h>
|
||||
#include <sqlpp11/type_traits.h>
|
||||
|
||||
namespace sqlpp
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
template<typename Column>
|
||||
struct insert_column
|
||||
{
|
||||
template<typename Db>
|
||||
void serialize(std::ostream& os, Db& db) const
|
||||
{
|
||||
_column.serialize_name(os, db);
|
||||
}
|
||||
|
||||
Column _column;
|
||||
};
|
||||
}
|
||||
template<template<typename> class ProhibitPredicate, typename... Assignment>
|
||||
struct insert_list_t
|
||||
{
|
||||
// check for at least one order expression
|
||||
static_assert(sizeof...(Assignment), "at least one select expression required in set()");
|
||||
|
||||
// check for duplicate assignments
|
||||
static_assert(not detail::has_duplicates<Assignment...>::value, "at least one duplicate argument detected in set()");
|
||||
|
||||
// check for invalid assignments
|
||||
using _assignment_set = typename detail::make_set_if<is_assignment_t, Assignment...>::type;
|
||||
static_assert(_assignment_set::size::value == sizeof...(Assignment), "at least one argument is not an assignment in set()");
|
||||
|
||||
// check for prohibited assignments
|
||||
using _prohibited_assignment_set = typename detail::make_set_if<ProhibitPredicate, typename Assignment::column_type...>::type;
|
||||
static_assert(_prohibited_assignment_set::size::value == 0, "at least one assignment is prohibited by its column definition in set()");
|
||||
|
||||
using _is_insert_list = tag_yes;
|
||||
|
||||
insert_list_t(Assignment... assignment):
|
||||
_columns({assignment._lhs}...),
|
||||
_values(assignment._rhs...)
|
||||
{}
|
||||
|
||||
insert_list_t(const insert_list_t&) = default;
|
||||
insert_list_t(insert_list_t&&) = default;
|
||||
insert_list_t& operator=(const insert_list_t&) = default;
|
||||
insert_list_t& operator=(insert_list_t&&) = default;
|
||||
~insert_list_t() = default;
|
||||
|
||||
template<typename Db>
|
||||
void serialize(std::ostream& os, Db& db) const
|
||||
{
|
||||
os << " (";
|
||||
detail::serialize_tuple(os, db, _columns, ',');
|
||||
os << ") VALUES (";
|
||||
detail::serialize_tuple(os, db, _values, ',');
|
||||
os << ")";
|
||||
}
|
||||
|
||||
std::tuple<detail::insert_column<typename Assignment::column_type>...> _columns;
|
||||
std::tuple<typename Assignment::value_type...> _values;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
@ -64,6 +64,17 @@ namespace sqlpp
|
||||
template<typename T>\
|
||||
struct name##_t: detail::name##_impl<T> {};
|
||||
|
||||
#define SQLPP_CONNECTOR_TRAIT_GENERATOR(name) \
|
||||
namespace detail\
|
||||
{\
|
||||
template<typename T, typename Enable = void>\
|
||||
struct connector_##name##_impl: std::false_type {};\
|
||||
template<typename T>\
|
||||
struct connector_##name##_impl<T, typename std::enable_if<std::is_same<typename T::_tags::_##name, tag>::value>::type>: std::true_type {};\
|
||||
}\
|
||||
template<typename T>\
|
||||
struct connector_##name##_t: detail::connector_##name##_impl<T> {};
|
||||
|
||||
SQLPP_IS_VALUE_TRAIT_GENERATOR(boolean);
|
||||
SQLPP_IS_VALUE_TRAIT_GENERATOR(numeric);
|
||||
SQLPP_IS_VALUE_TRAIT_GENERATOR(text);
|
||||
@ -96,9 +107,12 @@ namespace sqlpp
|
||||
SQLPP_TYPE_TRAIT_GENERATOR(is_value_list);
|
||||
SQLPP_TYPE_TRAIT_GENERATOR(is_assignment);
|
||||
SQLPP_TYPE_TRAIT_GENERATOR(is_assignment_list);
|
||||
SQLPP_TYPE_TRAIT_GENERATOR(is_insert_list);
|
||||
SQLPP_TYPE_TRAIT_GENERATOR(is_sort_order);
|
||||
SQLPP_TYPE_TRAIT_GENERATOR(requires_braces);
|
||||
|
||||
SQLPP_CONNECTOR_TRAIT_GENERATOR(has_empty_list_insert);
|
||||
|
||||
template<typename T, template<typename> class IsTag>
|
||||
using copy_type_trait = typename std::conditional<IsTag<T>::value, detail::tag, void>::type;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user