diff --git a/include/sqlpp11/custom_query.h b/include/sqlpp11/custom_query.h index c5d320e0..f2d8eac3 100644 --- a/include/sqlpp11/custom_query.h +++ b/include/sqlpp11/custom_query.h @@ -96,19 +96,20 @@ namespace sqlpp template auto custom_query(Parts... parts) - -> custom_query_t + -> custom_query_t...> { static_assert(sizeof...(Parts) > 0, "custom query requires at least one argument"); - return custom_query_t(parts...); + return custom_query_t...>(parts...); } template - auto dynamic_custom_query(const Database&, Parts...) - -> custom_query_t + auto dynamic_custom_query(const Database&, Parts... parts) + -> custom_query_t...> { static_assert(sizeof...(Parts) > 0, "custom query requires at least one query argument"); static_assert(std::is_base_of::value, "Invalid database parameter"); - return { }; + + return custom_query_t...>(parts...); } } #endif diff --git a/include/sqlpp11/noop.h b/include/sqlpp11/noop.h index a28fcc95..e0b7a275 100644 --- a/include/sqlpp11/noop.h +++ b/include/sqlpp11/noop.h @@ -30,6 +30,7 @@ #include #include #include +#include namespace sqlpp { @@ -42,7 +43,52 @@ namespace sqlpp template struct _result_methods_t - {}; + { + using _statement_t = Statement; + + const _statement_t& _get_statement() const + { + return static_cast(*this); + } + + // Execute + template + auto _run(Db& db, const Composite& composite) const -> void + { + Composite::_check_consistency(); + static_assert(_statement_t::_get_static_no_of_parameters() == 0, "cannot run execute directly with parameters, use prepare instead"); + + return db.execute(composite); + } + + template + auto _run(Db& db) const -> void + { + _statement_t::_check_consistency(); + + static_assert(_statement_t::_get_static_no_of_parameters() == 0, "cannot run insert directly with parameters, use prepare instead"); + return db.execute(_get_statement()); + } + + // Prepare + template + auto _prepare(Db& db, const Composite& composite) const + -> prepared_execute_t + { + Composite::_check_consistency(); + + return {{}, db.prepare_execute(composite)}; + } + + template + auto _prepare(Db& db) const + -> prepared_execute_t + { + _statement_t::_check_consistency(); + + return {{}, db.prepare_execute(_get_statement())}; + } + }; }; template diff --git a/include/sqlpp11/prepared_execute.h b/include/sqlpp11/prepared_execute.h new file mode 100644 index 00000000..a72577ad --- /dev/null +++ b/include/sqlpp11/prepared_execute.h @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2013-2014, 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_PREPARED_EXECUTE_H +#define SQLPP_PREPARED_EXECUTE_H + +#include +#include + +namespace sqlpp +{ + template + struct prepared_execute_t + { + using _parameter_list_t = make_parameter_list_t; + using _prepared_statement_t = typename Db::_prepared_statement_t; + + auto _run(Db& db) const + -> size_t + { + return db.run_prepared_execute(*this); + } + + void _bind_params() const + { + params._bind(_prepared_statement); + } + + _parameter_list_t params; + mutable _prepared_statement_t _prepared_statement; + }; + +} + +#endif diff --git a/include/sqlpp11/verbatim.h b/include/sqlpp11/verbatim.h index d1b43006..d03ea4af 100644 --- a/include/sqlpp11/verbatim.h +++ b/include/sqlpp11/verbatim.h @@ -27,6 +27,7 @@ #ifndef SQLPP_VERBATIM_H #define SQLPP_VERBATIM_H +#include #include #include @@ -71,6 +72,11 @@ namespace sqlpp return { s }; } + auto verbatim(std::string s) -> verbatim_t + { + return { s }; + } + } #endif diff --git a/tests/CustomQueryTest.cpp b/tests/CustomQueryTest.cpp index ae2b11e2..1be8062c 100644 --- a/tests/CustomQueryTest.cpp +++ b/tests/CustomQueryTest.cpp @@ -37,10 +37,15 @@ int main() test::TabFoo f; test::TabBar t; - auto c = custom_query(select(all_of(t)).from(t)); + auto c = custom_query(select(all_of(t)).from(t), sqlpp::verbatim("INTO"), t); + std::cerr << serialize(c, printer).str() << std::endl; db(c); auto p = db.prepare(custom_query(select(all_of(t)).from(t).where(t.alpha > sqlpp::parameter(t.alpha)))); + printer.reset(); + auto x = custom_query(sqlpp::verbatim("PRAGMA writeable_schema") == true); + std::cerr << serialize(x, printer).str() << std::endl; + return 0; }