diff --git a/.appveyor.yml b/.appveyor.yml new file mode 100644 index 00000000..6a32db7b --- /dev/null +++ b/.appveyor.yml @@ -0,0 +1,27 @@ +os: Visual Studio 2015 + +platform: + - x64 + +configuration: + - Debug + - Release + +matrix: + fast_finish: true + +build_script: + - CD + - cd .. + - CD + - git clone https://github.com/HowardHinnant/date + - cd sqlpp11 + - CD + - echo %configuration% + - mkdir build + - cd build + - cmake --version + - cmake .. -DCMAKE_CXX_FLAGS="/EHsc /wd4503" + - cmake --build . --config %configuration% + - ctest . --build-config %configuration% + diff --git a/.clang-format b/.clang-format index f94da0a4..1d9d0f26 100644 --- a/.clang-format +++ b/.clang-format @@ -14,7 +14,7 @@ BreakBeforeBinaryOperators: false BreakBeforeTernaryOperators: true BreakConstructorInitializersBeforeComma: false BinPackParameters: false -ColumnLimit: 160 +ColumnLimit: 120 ConstructorInitializerAllOnOneLineOrOnePerLine: true DerivePointerAlignment: false ExperimentalAutoDetectBinPacking: false diff --git a/.travis.yml b/.travis.yml index 24420177..cf0c1753 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,39 +2,54 @@ language: cpp os: - linux + - osx + +osx_image: xcode7 compiler: - # - clang # disabled clang due to missing libc++ + - clang - gcc +env: + - CONFIG=Release + - CONFIG=Debug + notifications: email: on_success: change on_failure: always -before_install: - # install boost 1.50 (headers only), travis currently offers 1.46 and 1.48 - - wget http://sourceforge.net/projects/boost/files/boost/1.50.0/boost_1_50_0.tar.bz2/download -O /tmp/boost.tar.bz2 - - mkdir -p temp - - cd temp - - tar jxf /tmp/boost.tar.bz2 boost_1_50_0/boost # extract headers only - - sudo mkdir -p /usr/local/include/ - - sudo ln -s $PWD/boost_1_50_0/boost /usr/local/include - - cd .. - #install g++-4.8 - - sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y - - sudo apt-get update - - if [ "$CXX" = "g++" ]; then sudo apt-get install g++-4.8; fi - - if [ "$CXX" = "g++" ]; then export CXX="g++-4.8" CC="gcc-4.8"; fi - install: - - "mkdir -p $TRAVIS_BUILD_DIR/build/scripts" - - "cd $TRAVIS_BUILD_DIR/build/scripts" - - "cmake $TRAVIS_BUILD_DIR" + - CMAKE_VERSION_MM=3.2 + - CMAKE_VERSION_FULL=$CMAKE_VERSION_MM.2 + - git clone https://github.com/HowardHinnant/date + - if [ "$TRAVIS_OS_NAME" == "linux" ]; then + sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test + && sudo add-apt-repository -y ppa:apokluda/boost1.53 + && sudo apt-get update -qq + && sudo apt-get install -qq g++-4.8 libboost1.53-dev --no-install-recommends + && sudo update-alternatives --quiet --install /usr/bin/gcc gcc /usr/bin/gcc-4.6 60 --slave /usr/bin/g++ g++ /usr/bin/g++-4.6 --slave /usr/bin/gcov gcov /usr/bin/gcov-4.6 + && sudo update-alternatives --quiet --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 40 --slave /usr/bin/g++ g++ /usr/bin/g++-4.8 --slave /usr/bin/gcov gcov /usr/bin/gcov-4.8 + && sudo update-alternatives --quiet --set gcc /usr/bin/gcc-4.8 + && wget --no-check-certificate http://www.cmake.org/files/v${CMAKE_VERSION_MM}/cmake-${CMAKE_VERSION_FULL}-Linux-x86_64.sh + && sudo sh cmake-${CMAKE_VERSION_FULL}-Linux-x86_64.sh --prefix=/usr/local --exclude-subdir; + fi + - if [ "$TRAVIS_OS_NAME" = "osx" ]; then + brew update + && ((brew list -1 | grep -q "^$cmake\$") || brew install cmake) + && (brew outdated cmake || brew upgrade cmake) + && cmake --version; + fi + +before_script: + - mkdir build + - cd build + - if [[ "$CXX" = "g++" && "$CONFIG" = "Debug" && "$TRAVIS_OS_NAME" = "linux" ]]; then export CXXFLAGS="--coverage"; fi + - cmake .. -DCMAKE_BUILD_TYPE=$CONFIG -DDATE_INCLUDE_DIR=$PWD/../date script: - - "cd $TRAVIS_BUILD_DIR/build/scripts" - - "make -j3" - # test compile-time constraints - - "make test_sqlpp_constraints" + - cmake --build . --config $CONFIG + - ctest --output-on-failure +after_script: + - ../coveralls diff --git a/CMakeLists.txt b/CMakeLists.txt index 4f5266c2..f606df00 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,16 +1,16 @@ # Copyright (c) 2013-2015, 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 @@ -22,26 +22,50 @@ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -cmake_minimum_required(VERSION 2.6) +cmake_minimum_required(VERSION 3.2 FATAL_ERROR) +project(sqlpp11 VERSION 0.1 LANGUAGES CXX) -# Cygwin does not define WIN32 and warn if not use with this flag -set(CMAKE_LEGACY_CYGWIN_WIN32 0) # Remove when CMake >= 2.8.4 is required - -project (sqlpp11) enable_testing() -message(STATUS "Using ${CMAKE_CXX_COMPILER} (compiler id: ${CMAKE_CXX_COMPILER_ID})") -if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") - set(CMAKE_CXX_FLAGS "-std=c++11 -stdlib=libc++ -Wall ${CMAKE_CXX_FLAGS}") -elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") - set(CMAKE_CXX_FLAGS "-std=c++11 -Wall ${CMAKE_CXX_FLAGS}") -endif () -set(CMAKE_CXX_FLAGS "-Wconversion -Wpedantic -Wshadow ${CMAKE_CXX_FLAGS}") +add_library(sqlpp11 INTERFACE) + +set(DATE_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../date" CACHE FILEPATH "Path to Howard Hinnant's date library") + +if(NOT EXISTS ${DATE_INCLUDE_DIR}/date.h) + message(SEND_ERROR "Can't find date.h in ${DATE_INCLUDE_DIR}") +endif() + +target_include_directories(sqlpp11 INTERFACE + $ + $ + ) + +if (NOT MSVC) +target_compile_features(sqlpp11 INTERFACE + cxx_alias_templates + cxx_auto_type + cxx_constexpr + cxx_decltype + cxx_defaulted_functions + cxx_defaulted_move_initializers + cxx_deleted_functions + cxx_explicit_conversions + cxx_lambdas + cxx_noexcept + cxx_nullptr + cxx_range_for + cxx_right_angle_brackets + cxx_rvalue_references + cxx_static_assert + cxx_trailing_return_types + cxx_uniform_initialization + cxx_template_template_parameters + cxx_variadic_templates + ) +endif () -set(include_dir "${PROJECT_SOURCE_DIR}/include") -file(GLOB_RECURSE sqlpp_headers "${include_dir}/*.h") -include_directories("${include_dir}") add_subdirectory(tests) +add_subdirectory(test_static_asserts) add_subdirectory(test_constraints) add_subdirectory(examples) diff --git a/CREDITS b/CREDITS index 3675cef0..fdfc9b58 100644 --- a/CREDITS +++ b/CREDITS @@ -3,14 +3,15 @@ Credits: This library evolved through several stages and would probably not exist without input from other people: * Michael Gmelin: Interface and requirements discussions - * Paul Körbitz: Feedback and extensions - * Peter Knoblach: Initial ideas - * Ulrich Küttler: Feedback and extensions + * Paul Körbitz: Feedback and extensions + * Peter Knoblach: Initial ideas + * Ulrich Küttler: Feedback and extensions + * Daniel Pfeifer: Buildsystem, Travis, Coveralls - * Metafeed GmbH: Production code using a forerunner version - * PPRO Financial Ltd: Production code using sqlpp11 and a forerunner version - * The boost community: Invaluable suggestions and critiques + * Metafeed GmbH: Production code using a forerunner version + * PPRO Financial Ltd: Production code using sqlpp11 and a forerunner version + * The boost community: Invaluable suggestions and critiques * Meeting C++ Munich: Hosted the first talk about sqlpp11 diff --git a/README.md b/README.md index 525187fb..a8b0eb20 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,28 @@ sqlpp11 ======= +Branch / Compiler | clang-3.4, gcc-4.9, Xcode-7 | MSVC 2015 | Test Coverage +------------------| -------------------------------|-------------|--------------- +master | [![Build Status](https://travis-ci.org/rbock/sqlpp11.svg?branch=master)](https://travis-ci.org/rbock/sqlpp11?branch=master) | [![Build status](https://ci.appveyor.com/api/projects/status/eid7mwqgavo0h61h/branch/master?svg=true)](https://ci.appveyor.com/project/rbock/sqlpp11/branch/master) | [![Coverage Status](https://coveralls.io/repos/rbock/sqlpp11/badge.svg?branch=master)](https://coveralls.io/r/rbock/sqlpp11?branch=master) +develop | [![Build Status](https://travis-ci.org/rbock/sqlpp11.svg?branch=develop)](https://travis-ci.org/rbock/sqlpp11?branch=develop) | [![Build status](https://ci.appveyor.com/api/projects/status/eid7mwqgavo0h61h/branch/develop?svg=true)](https://ci.appveyor.com/project/rbock/sqlpp11/branch/develop) | [![Coverage Status](https://coveralls.io/repos/rbock/sqlpp11/badge.svg?branch=develop)](https://coveralls.io/r/rbock/sqlpp11?branch=develop) + A type safe embedded domain specific language for SQL queries and results in C++ -Extensive documentation is found in the wiki, https://github.com/rbock/sqlpp11/wiki +Documentation is found in the wiki, https://github.com/rbock/sqlpp11/wiki -Talks/workshops - * MeetingC++ (Berlin, Germany): http://meetingcpp.com/index.php/mcpp2014.html - * 2014-12-05: http://meetingcpp.com/index.php/tv14/items/4.html - * CppCon (Bellevue, Washington, USA): http://cppcon.org - * 2014-09-08: http://sched.co/1qhngYK (Workshop Part 1) - * 2014-09-11: http://sched.co/1r4lue3 (Talk) - * 2014-09-12: http://sched.co/Wi8aWM (Workshop Part 2) +Past talks about sqlpp11 and some coding concepts used within the library: - -You can contact me + * [CppCast:](http://cppcast.com) + * 2015-05-07: http://cppcast.com/2015/05/roland-bock/ + * [CppCon:](http://cppcon.org) + * 2015-09-24: [Pruning Error Messages From Your C++ Template Code](https://www.youtube.com/watch?v=2ISqFW9fRws), with examples from sqlpp11 + * 2014-09-11: [sqlpp11, An SQL Library Worthy Of Modern C++](https://www.youtube.com/watch?v=cJPAjhBm-HQ) + * [Meeting C++:](http://meetingcpp.com) + * 2014-12-05: [sqlpp11, An EDSL For Type-Safe SQL In C++11](https://www.youtube.com/watch?v=9Hjfg9IfzhU) + * [MUC++:](http://www.meetup.com/MUCplusplus/) + * 2014-02-27: [Selected C++11 Template Toffees From sqlpp11, Part1](https://www.youtube.com/watch?v=hXnGFYNbmXg), [Part2](https://www.youtube.com/watch?v=WPCV6dvxZ_U), [Part 3](https://www.youtube.com/watch?v=eB7hd_KjTig), [Part 4](https://www.youtube.com/watch?v=NBfqzcN0_EQ) + +You can contact me * by posting issues at https://github.com/rbock/sqlpp11/issues * or via email at rbock at eudoxos dot de @@ -40,7 +48,7 @@ sqlpp11 is vendor-neutral. Specific traits of databases (e.g. unsupported or non Examples: --------- -For the examples, lets assume you have a table class representing something like +For the examples, lets assume you have a table class representing something like ```SQL CREATE TABLE foo ( @@ -104,11 +112,12 @@ Requirements: __Compiler:__ sqlpp11 makes heavy use of C++11 and requires a recent compiler and STL. The following compilers are known to compile the test programs: - * clang-3.2 on Ubuntu-12.4 - * clang-3.4 on Ubuntu-12.4 - * g++-4.8 on Ubuntu-12.4 - * g++-4.8 on cygwin 64bit - * g++-4.9 on Debian Unstable + * clang-3.4+ on Ubuntu-12.4 + * g++-4.8+ on Ubuntu-12.4 + * g++-4.8+ on cygwin 64bit + * g++-4.9+ on Debian Unstable + * Xcode-7 on OS X + * MSVC 2015 Update 1 on Windows Server 2012 __Database Connector:__ sqlpp11 requires a certain api in order to connect with the database, see database/api.h. @@ -116,7 +125,10 @@ sqlpp11 requires a certain api in order to connect with the database, see databa * MySQL: https://github.com/rbock/sqlpp11-connector-mysql * Sqlite3: https://github.com/rbock/sqlpp11-connector-sqlite3 * PostgreSQL: https://github.com/matthijs/sqlpp11-connector-postgresql - + +__Date Library:__ +sqlpp11 requires [Howard Hinnant's date library](https://github.com/HowardHinnant/date) for `date` and `date_time` data types. + To demonstrate that sqlpp11 can work with other backends as well, here is an experimental backend for structs in standard containers: * STL Container: https://github.com/rbock/sqlpp11-connector-stl diff --git a/connector_api/bind_result.h b/connector_api/bind_result.h index d14de772..c269b925 100644 --- a/connector_api/bind_result.h +++ b/connector_api/bind_result.h @@ -1,17 +1,17 @@ /* * Copyright (c) 2013-2015, 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 @@ -24,7 +24,6 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ - #ifndef SQLPP_DATABASE_BIND_RESULT_H #define SQLPP_DATABASE_BIND_RESULT_H @@ -32,62 +31,61 @@ namespace sqlpp { - namespace database - { - /* - * bind_result_t binds values of a sqlpp11 result row - * to the results of a statement - */ - class bind_result_t - { - public: - bind_result_t(); // default constructor for a result that will not yield a valid row - bind_result_t(...); - bind_result_t(const bind_result_t&) = delete; - bind_result_t(bind_result_t&& rhs); - bind_result_t& operator=(const bind_result_t&) = delete; - bind_result_t& operator=(bind_result_t&&); - ~bind_result_t(); + namespace database + { + /* + * bind_result_t binds values of a sqlpp11 result row + * to the results of a statement + */ + class bind_result_t + { + public: + bind_result_t(); // default constructor for a result that will not yield a valid row + bind_result_t(...); + bind_result_t(const bind_result_t&) = delete; + bind_result_t(bind_result_t&& rhs); + bind_result_t& operator=(const bind_result_t&) = delete; + bind_result_t& operator=(bind_result_t&&); + ~bind_result_t(); - bool operator==(const bind_result_t& rhs) const; + bool operator==(const bind_result_t& rhs) const; - template - void next(ResultRow& result_row); + template + void next(ResultRow& result_row); - // something similar to this: - /* - { - if (!_handle) - { - result_row.invalidate(); - return; - } + // something similar to this: + /* + { + if (!_handle) + { + result_row.invalidate(); + return; + } - if (next_impl()) - { - if (not result_row) - { - result_row.validate(); - } - result_row._bind(*this); // bind result row values to results - } - else - { - if (result_row) - result_row.invalidate(); - } - }; - */ + if (next_impl()) + { + if (not result_row) + { + result_row.validate(); + } + result_row._bind(*this); // bind result row values to results + } + else + { + if (result_row) + result_row.invalidate(); + } + }; + */ - // These are called by the result row to bind individual result values - // More will be added over time - void _bind_boolean_result(size_t index, signed char* value, bool* is_null); - void _bind_floating_point_result(size_t index, double* value, bool* is_null); - void _bind_integral_result(size_t index, int64_t* value, bool* is_null); - void _bind_text_result(size_t index, const char** text, size_t* len); - ... - }; - - } + // These are called by the result row to bind individual result values + // More will be added over time + void _bind_boolean_result(size_t index, signed char* value, bool* is_null); + void _bind_floating_point_result(size_t index, double* value, bool* is_null); + void _bind_integral_result(size_t index, int64_t* value, bool* is_null); + void _bind_text_result(size_t index, const char** text, size_t* len); + ... + }; + } } #endif diff --git a/connector_api/connection.h b/connector_api/connection.h index 9a23d8b7..dc2f81c2 100644 --- a/connector_api/connection.h +++ b/connector_api/connection.h @@ -1,17 +1,17 @@ /* * Copyright (c) 2013-2015, 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 @@ -24,122 +24,125 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ - #ifndef SQLPP_DATABASE_CONNECTION_H #define SQLPP_DATABASE_CONNECTION_H #include #include -#include // You may use char result or bind result or both -#include // to represent results of select and prepared select +#include // You may use char result or bind result or both +#include // to represent results of select and prepared select namespace sqlpp { - namespace database - { - // The context is not a requirement, but if the database requires - // any deviations from the SQL standard, you should use your own - // context in order to specialize the behaviour, see also interpreter.h - struct context_t - { - template - std::ostream& operator<<(T t); + namespace database + { + // The context is not a requirement, but if the database requires + // any deviations from the SQL standard, you should use your own + // context in order to specialize the behaviour, see also interpreter.h + struct context_t + { + template + std::ostream& operator<<(T t); - std::string escape(std::string arg); - }; + std::string escape(std::string arg); + }; - class connection: public sqlpp::connection // this inheritance helps with ADL for dynamic_select, for instance - { - public: - using _traits = ::sqlpp::make_traits<::sqlpp::no_value_t, - ::sqlpp::tag::enforce_null_result_treatment // If that is what you really want, leave it out otherwise - >; + class connection : public sqlpp::connection // this inheritance helps with ADL for dynamic_select, for instance + { + public: + using _traits = ::sqlpp::make_traits< + ::sqlpp::no_value_t, + ::sqlpp::tag::enforce_null_result_treatment // If that is what you really want, leave it out otherwise + >; - using _prepared_statement_t = << handle to a prepared statement of the database >>; - using _serializer_context_t = << This context is used to serialize a statement >> - using _interpreter_context_t = << This context is used interpret a statement >>; - // serializer and interpreter are typically the same for string based connectors - // the types are required for dynamic statement components, see sqlpp11/interpretable.h + using _prepared_statement_t = << handle to a prepared statement of the database >> ; + using _serializer_context_t = << This context is used to serialize a statement >> using _interpreter_context_t = + << This context is used interpret a statement >> + ; + // serializer and interpreter are typically the same for string based connectors + // the types are required for dynamic statement components, see sqlpp11/interpretable.h - connection(...); - ~connection(); - connection(const connection&) = delete; - connection(connection&&) = delete; - connection& operator=(const connection&) = delete; - connection& operator=(connection&&) = delete; + connection(...); + ~connection(); + connection(const connection&) = delete; + connection(connection&&) = delete; + connection& operator=(const connection&) = delete; + connection& operator=(connection&&) = delete; - //! "direct" select - template - <> select(const Select& s); + //! "direct" select + template + << bind_result_t >> + select(const Select& s); - //! prepared select - template - _prepared_statement_t prepare_select(Select& s); + //! prepared select + template + _prepared_statement_t prepare_select(Select& s); - template - <> run_prepared_select(const PreparedSelect& s); // call s._bind_params() + template + << bind_result_t >> + run_prepared_select(const PreparedSelect& s); // call s._bind_params() - //! "direct insert - template - size_t insert(const Insert& i); + //! "direct insert + template + size_t insert(const Insert& i); - //! prepared insert - template - _prepared_statement_t prepare_insert(Insert& i); + //! prepared insert + template + _prepared_statement_t prepare_insert(Insert& i); - template - size_t run_prepared_insert(const PreparedInsert& i); // call i._bind_params() + template + size_t run_prepared_insert(const PreparedInsert& i); // call i._bind_params() - //! "direct" update - template - size_t update(const Update& u); + //! "direct" update + template + size_t update(const Update& u); - //! "prepared" update - template - _prepared_statement_t prepare_update(Update& u); + //! "prepared" update + template + _prepared_statement_t prepare_update(Update& u); - template - size_t run_prepared_update(const PreparedUpdate& u); // call u._bind_params() + template + size_t run_prepared_update(const PreparedUpdate& u); // call u._bind_params() - //! "direct" remove - template - size_t remove(const Remove& r) + //! "direct" remove + template + size_t remove(const Remove& r) - //! prepared remove - template - _prepared_statement_t prepare_remove(Remove& r); + //! prepared remove + template + _prepared_statement_t prepare_remove(Remove& r); - template - size_t run_prepared_remove(const PreparedRemove& r); // call r._bind_params() + template + size_t run_prepared_remove(const PreparedRemove& r); // call r._bind_params() - //! call run on the argument - template - auto operator() (const T& t) -> decltype(t._run(*this)) - { - return t._run(*this); - } + //! call run on the argument + template + auto operator()(const T& t) -> decltype(t._run(*this)) + { + return t._run(*this); + } - //! call prepare on the argument - template - auto prepare(const T& t) -> decltype(t._prepare(*this)) - { - return t._prepare(*this); - } + //! call prepare on the argument + template + auto prepare(const T& t) -> decltype(t._prepare(*this)) + { + return t._prepare(*this); + } - //! start transaction - void start_transaction(); + //! start transaction + void start_transaction(); - //! commit transaction (or throw transaction if the transaction has been finished already) - void commit_transaction(); + //! commit transaction (or throw transaction if the transaction has been finished already) + void commit_transaction(); - //! rollback transaction with or without reporting the rollback (or throw if the transaction has been finished already) - void rollback_transaction(bool report); + //! rollback transaction with or without reporting the rollback (or throw if the transaction has been finished + // already) + void rollback_transaction(bool report); - //! report a rollback failure (will be called by transactions in case of a rollback failure in the destructor) - void report_rollback_failure(const std::string message) noexcept; - }; - - } + //! report a rollback failure (will be called by transactions in case of a rollback failure in the destructor) + void report_rollback_failure(const std::string message) noexcept; + }; + } } #include diff --git a/connector_api/interpreter.h b/connector_api/interpreter.h index d59add96..fd694635 100644 --- a/connector_api/interpreter.h +++ b/connector_api/interpreter.h @@ -1,17 +1,17 @@ /* * Copyright (c) 2013-2015, 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 @@ -31,32 +31,31 @@ #include /* - * sqlpp11 offers an interpreter that can be used to serialize the expression tree + * sqlpp11 offers an interpreter that can be used to serialize the expression tree * into a standard SQL string. * - * The connector library can specialize the interpreter template to partially or + * The connector library can specialize the interpreter template to partially or * completely change the way the expression tree is interpreted. * * For example, this specialization will produce indexed parameters instead of just '?' */ namespace sqlpp { - namespace vendor - { - template - struct interpreter_t> - { - using T = parameter_t; + namespace vendor + { + template + struct interpreter_t> + { + using T = parameter_t; - static database::context_t& _(const T& t, database::context_t& context) - { - context << "?" << context.count(); - context.pop_count(); - return context; - } - }; - - } + static database::context_t& _(const T& t, database::context_t& context) + { + context << "?" << context.count(); + context.pop_count(); + return context; + } + }; + } } #endif diff --git a/connector_api/prepared_statement.h b/connector_api/prepared_statement.h index 1524e0f0..7a329788 100644 --- a/connector_api/prepared_statement.h +++ b/connector_api/prepared_statement.h @@ -1,17 +1,17 @@ /* * Copyright (c) 2013-2015, 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 @@ -24,7 +24,6 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ - #ifndef SQLPP_DATABASE_PREPARED_STATEMENT_H #define SQLPP_DATABASE_PREPARED_STATEMENT_H @@ -33,28 +32,28 @@ namespace sqlpp { - namespace database - { - class prepared_statement_t - { - public: - prepared_statement_t() = delete; - prepared_statement_t(...); - prepared_statement_t(const prepared_statement_t&) = delete; - prepared_statement_t(prepared_statement_t&& rhs); - prepared_statement_t& operator=(const prepared_statement_t&) = delete; - prepared_statement_t& operator=(prepared_statement_t&&); - ~prepared_statement_t(); + namespace database + { + class prepared_statement_t + { + public: + prepared_statement_t() = delete; + prepared_statement_t(...); + prepared_statement_t(const prepared_statement_t&) = delete; + prepared_statement_t(prepared_statement_t&& rhs); + prepared_statement_t& operator=(const prepared_statement_t&) = delete; + prepared_statement_t& operator=(prepared_statement_t&&); + ~prepared_statement_t(); - bool operator==(const prepared_statement_t& rhs) const; + bool operator==(const prepared_statement_t& rhs) const; - // These are called by the sqlpp11::prepared_*_t to bind the individual parameters - // More will be added over time - void _bind_boolean_parameter(size_t index, const signed char* value, bool is_null); - void _bind_floating_point_parameter(size_t index, const double* value, bool is_null); - void _bind_integral_parameter(size_t index, const int64_t* value, bool is_null); - void _bind_text_parameter(size_t index, const std::string* value, bool is_null); - }; - } + // These are called by the sqlpp11::prepared_*_t to bind the individual parameters + // More will be added over time + void _bind_boolean_parameter(size_t index, const signed char* value, bool is_null); + void _bind_floating_point_parameter(size_t index, const double* value, bool is_null); + void _bind_integral_parameter(size_t index, const int64_t* value, bool is_null); + void _bind_text_parameter(size_t index, const std::string* value, bool is_null); + }; + } } #endif diff --git a/coveralls b/coveralls new file mode 100755 index 00000000..301d67f7 --- /dev/null +++ b/coveralls @@ -0,0 +1,31 @@ +#!/bin/bash + +if [ 0 -eq $(find -iname *.gcda | wc -l) ] +then + exit 0 +fi + +gcov --source-prefix ${TRAVIS_BUILD_DIR} --preserve-paths --relative-only $(find -iname *.gcda) 1>/dev/null || exit 0 + +cat >coverage.json <>coverage.json <coverage.json <(head -n -1 coverage.json.tmp) <(echo -e " }\n ]\n}") +curl -F json_file=@coverage.json https://coveralls.io/api/v1/jobs diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 33b8e65f..9046f915 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,28 +1,55 @@ +# Copyright (c) 2013-2015, 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. -macro (build arg) - # Add headers to sources to enable file browsing in IDEs - include_directories("${CMAKE_CURRENT_SOURCE_DIR}/../tests") - add_executable("Sqlpp11Example${arg}" "${arg}.cpp" ${sqlpp_headers} "${CMAKE_CURRENT_SOURCE_DIR}/../tests/MockDb.h" "${CMAKE_CURRENT_LIST_DIR}/Sample.h") - add_test("${arg}" "Sqlpp11Example${arg}") -endmacro () +set(example_names + #sample + insert + update + remove + select + ) -#build(sample) -build(insert) -build(update) -build(remove) -build(select) find_package(Boost 1.50) if(Boost_FOUND) - MESSAGE(${Boost_INCLUDE_DIRS}) include_directories(${Boost_INCLUDE_DIRS}) - build(ppgen) + list(APPEND example_names ppgen) endif() +create_test_sourcelist(example_sources example_main.cpp ${example_names}) +add_executable(sqlpp11_examples ${example_sources}) +target_link_libraries(sqlpp11_examples PRIVATE sqlpp11 sqlpp11_testing) + +foreach(example IN LISTS example_names) + add_test(NAME sqlpp11.examples.${example} + COMMAND sqlpp11_examples ${example} + ) +endforeach() #find_package(PythonInterp REQUIRED) #add_custom_command( -# OUTPUT ${CMAKE_CURRENT_LIST_DIR}/Sample.h +# OUTPUT ${CMAKE_CURRENT_LIST_DIR}/Sample.h # COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_SOURCE_DIR}/scripts/ddl2cpp ${CMAKE_CURRENT_LIST_DIR}/sample.sql Sample test # DEPENDS ${CMAKE_CURRENT_LIST_DIR}/sample.sql # ) diff --git a/examples/Sample.h b/examples/Sample.h index 01e7d92d..da542f0d 100644 --- a/examples/Sample.h +++ b/examples/Sample.h @@ -2,9 +2,10 @@ #define TEST_SAMPLE_H #include -#include +#include #include +// clang-format off namespace test { namespace TabPerson_ diff --git a/examples/TabSample.h b/examples/TabSample.h index 09e7f1ea..9cadacf7 100644 --- a/examples/TabSample.h +++ b/examples/TabSample.h @@ -1,17 +1,17 @@ /* * Copyright (c) 2013-2015, 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 @@ -30,7 +30,7 @@ #include #include - +// clang-format off namespace TabFoo_ { struct Omega @@ -60,7 +60,7 @@ namespace TabFoo_ } struct TabFoo: sqlpp::table_base_t< - TabFoo, + TabFoo, TabFoo_::Omega > { @@ -151,8 +151,8 @@ namespace TabSample_ } struct TabSample: sqlpp::table_base_t< - TabSample, - TabSample_::Alpha, + TabSample, + TabSample_::Alpha, TabSample_::Beta, TabSample_::Gamma > diff --git a/examples/insert.cpp b/examples/insert.cpp index 2cf32f68..525339f7 100644 --- a/examples/insert.cpp +++ b/examples/insert.cpp @@ -1,25 +1,25 @@ /* * Copyright (c) 2014-2015 Roland Bock * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, + * + * 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, + * + * * 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 + * * 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 + * + * 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. */ @@ -27,41 +27,40 @@ #include "MockDb.h" #include -int main() +int insert(int, char* []) { - MockDb db; + MockDb db{}; - test::TabPerson p; - test::TabFeature f; + const auto p = test::TabPerson{}; + const auto f = test::TabFeature{}; - db(insert_into(f).set(f.name = "loves c++", f.fatal = false)); + db(insert_into(f).set(f.name = "loves c++", f.fatal = false)); - //db(insert_into(f).set(f.nahme = "loves c++", f.fatal = false)); + // db(insert_into(f).set(f.nahme = "loves c++", f.fatal = false)); - //db(insert_into(f).set(f.name == "loves c++", f.fatal = false)); + // db(insert_into(f).set(f.name == "loves c++", f.fatal = false)); - //db(insert_into(f).set(f.name = "loves c++", f.fatal = "false")); + // db(insert_into(f).set(f.name = "loves c++", f.fatal = "false")); - //db(insert_into(p).set(f.name = "loves c++", f.fatal = false)); + // db(insert_into(p).set(f.name = "loves c++", f.fatal = false)); - //db(insert_into(f).set(f.name = "loves c++", p.feature = 7)); + // db(insert_into(f).set(f.name = "loves c++", p.feature = 7)); - //db(insert_into(f).set(f.id = 42, f.name = "loves c++", f.fatal = false)); - - //db(insert_into(f).set(f.name = "loves c++")); - + // db(insert_into(f).set(f.id = 42, f.name = "loves c++", f.fatal = false)); - db(insert_into(f).default_values()); + // db(insert_into(f).set(f.name = "loves c++")); - auto i = insert_into(p).columns(p.name, p.feature); - i.values.add(p.name = "Roland", p.feature = 1); - i.values.add(p.name = "Zaphod", p.feature = sqlpp::default_value); - db(i); + db(insert_into(f).default_values()); + auto i = insert_into(p).columns(p.name, p.feature); + i.values.add(p.name = "Roland", p.feature = 1); + i.values.add(p.name = "Zaphod", p.feature = sqlpp::default_value); + db(i); - auto pi = db.prepare(insert_into(p).set(p.name = parameter(f.name), p.feature = parameter(p.feature))); - pi.params.name = "likes java"; - pi.params.feature = true; + auto pi = db.prepare(insert_into(p).set(p.name = parameter(f.name), p.feature = parameter(p.feature))); + pi.params.name = "likes java"; + pi.params.feature = true; - db(pi); + db(pi); + return 0; } diff --git a/examples/ppgen.cpp b/examples/ppgen.cpp index 58b43222..7e8ca072 100644 --- a/examples/ppgen.cpp +++ b/examples/ppgen.cpp @@ -24,7 +24,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#if 0 // syntax example +#if 0 // syntax example SQLPP_DECLARE_TABLE( (table, \ SQLPP_DROP_IF_EXISTS \ @@ -45,56 +45,57 @@ SQLPP_DECLARE_TABLE( #include "MockDb.h" +// clang-format off SQLPP_DECLARE_TABLE( (tab_person) , - (id , int , SQLPP_AUTO_INCREMENT) - (name , varchar(255), SQLPP_NOT_NULL ) - (feature, int , SQLPP_NOT_NULL ) + (id , int , SQLPP_PRIMARY_KEY) + (name , varchar(255), SQLPP_NOT_NULL ) + (feature, int , SQLPP_NOT_NULL ) ) SQLPP_DECLARE_TABLE( (tab_feature) , - (id , int , SQLPP_AUTO_INCREMENT) - (name , varchar(255), SQLPP_NULL ) - (fatal, bool , SQLPP_NOT_NULL ) + (id , int , SQLPP_PRIMARY_KEY) + (name , varchar(255), SQLPP_NULL ) + (fatal, bool , SQLPP_NOT_NULL ) ) +// clang-format on -int main() +int ppgen(int, char* []) { - MockDb db; - tab_person::tab_person p; - tab_feature::tab_feature f; + MockDb db{}; + const auto p = tab_person::tab_person{}; + const auto f = tab_feature::tab_feature{}; - db(insert_into(f).set(f.name = "loves c++", f.fatal = false)); + db(insert_into(f).set(f.name = "loves c++", f.fatal = false)); - //db(insert_into(f).set(f.nahme = "loves c++", f.fatal = false)); + // db(insert_into(f).set(f.nahme = "loves c++", f.fatal = false)); - //db(insert_into(f).set(f.name == "loves c++", f.fatal = false)); + // db(insert_into(f).set(f.name == "loves c++", f.fatal = false)); - //db(insert_into(f).set(f.name = "loves c++", f.fatal = "false")); + // db(insert_into(f).set(f.name = "loves c++", f.fatal = "false")); - //db(insert_into(p).set(f.name = "loves c++", f.fatal = false)); + // db(insert_into(p).set(f.name = "loves c++", f.fatal = false)); - //db(insert_into(f).set(f.name = "loves c++", p.feature = 7)); + // db(insert_into(f).set(f.name = "loves c++", p.feature = 7)); - //db(insert_into(f).set(f.id = 42, f.name = "loves c++", f.fatal = false)); + // db(insert_into(f).set(f.id = 42, f.name = "loves c++", f.fatal = false)); - //db(insert_into(f).set(f.name = "loves c++")); + // db(insert_into(f).set(f.name = "loves c++")); + db(insert_into(f).default_values()); - db(insert_into(f).default_values()); + auto i = insert_into(p).columns(p.name, p.feature); + i.values.add(p.name = "Roland", p.feature = 1); + i.values.add(p.name = "Zaphod", p.feature = sqlpp::default_value); + db(i); - auto i = insert_into(p).columns(p.name, p.feature); - i.values.add(p.name = "Roland", p.feature = 1); - i.values.add(p.name = "Zaphod", p.feature = sqlpp::default_value); - db(i); + auto pi = db.prepare(insert_into(p).set(p.name = parameter(f.name), p.feature = parameter(p.feature))); + pi.params.name = "likes java"; + pi.params.feature = true; - - auto pi = db.prepare(insert_into(p).set(p.name = parameter(f.name), p.feature = parameter(p.feature))); - pi.params.name = "likes java"; - pi.params.feature = true; - - db(pi); + db(pi); + return 0; } diff --git a/examples/remove.cpp b/examples/remove.cpp index 8bec39c3..73b7b073 100644 --- a/examples/remove.cpp +++ b/examples/remove.cpp @@ -1,25 +1,25 @@ /* * Copyright (c) 2014-2015 Roland Bock * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, + * + * 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, + * + * * 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 + * * 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 + * + * 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. */ @@ -27,14 +27,13 @@ #include "MockDb.h" #include -int main() +int remove(int, char* []) { - MockDb db; + MockDb db{}; - test::TabPerson p; - test::TabFeature q; + const auto p = test::TabPerson{}; + const auto q = test::TabFeature{}; - db(remove_from(p) - .using_(p, q) - .where(p.feature == q.id and q.fatal == true)); + db(remove_from(p).using_(p, q).where(p.feature == q.id and q.fatal == true)); + return 0; } diff --git a/examples/sample.cpp b/examples/sample.cpp index d3b71849..c356b8ec 100644 --- a/examples/sample.cpp +++ b/examples/sample.cpp @@ -1,25 +1,25 @@ /* * Copyright (c) 2014-2015 Roland Bock * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, + * + * 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, + * + * * 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 + * * 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 + * + * 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. */ @@ -29,31 +29,28 @@ int main() { - MockDb db; + MockDb db{}; - test::TabPerson p; - test::TabFeature f; + const auto p = test::TabPerson{}; + const auto f = test::TabFeature{}; - db(insert_into(f).set(f.name = "Loves C++", p.fatal = false)); + db(insert_into(f).set(f.name = "Loves C++", p.fatal = false)); - db(insert_into(f).set(p.name = "Roland", p.feature = 1)); + db(insert_into(f).set(p.name = "Roland", p.feature = 1)); - auto s = select(all_of(p)) - .from(p, q) - .where(p.name == any(select(q.name) - .from(q) - .where(true))) - .group_by(q.name) - .having(p.name.like("%Bee%")) - .order_by(p.name.asc()) - .limit(3).offset(7); + auto s = select(all_of(p)) + .from(p, q) + .where(p.name == any(select(q.name).from(q).where(true))) + .group_by(q.name) + .having(p.name.like("%Bee%")) + .order_by(p.name.asc()) + .limit(3) + .offset(7); - auto x = s.as(sqlpp::alias::x); - for (const auto& row : db(select(p.id, x.name) - .from(p.join(x).on(p.feature == x.feature)) - .where(true))) - { - int id = row.id; - std::string name = row.name; - } + auto x = s.as(sqlpp::alias::x); + for (const auto& row : db(select(p.id, x.name).from(p.join(x).on(p.feature == x.feature)).where(true))) + { + int id = row.id; + std::string name = row.name; + } } diff --git a/examples/select.cpp b/examples/select.cpp index 10d756bb..d6bdfdf6 100644 --- a/examples/select.cpp +++ b/examples/select.cpp @@ -1,25 +1,25 @@ /* * Copyright (c) 2014-2015 Roland Bock * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, + * + * 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, + * + * * 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 + * * 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 + * + * 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. */ @@ -36,25 +36,26 @@ SQLPP_ALIAS_PROVIDER(cheesecake) -int main() +int select(int, char* []) { - static constexpr bool some_condition = true; - static constexpr bool some_other_condition = false; + static constexpr bool some_condition = true; + static constexpr bool some_other_condition = false; - MockDb db; + MockDb db{}; - test::TabPerson p; - test::TabFeature f; + const auto p = test::TabPerson{}; +#if 0 + const auto f = test::TabFeature{}; +#endif - for (const auto& row : db(select(all_of(p)).from(p).where(p.id > 7))) - { - int64_t id = row.id; - std::string name = row.name; - int64_t feature = row.feature; - } + for (const auto& row : db(select(all_of(p)).from(p).where(p.id > 7))) + { + int64_t id = row.id; + std::string name = row.name; + int64_t feature = row.feature; + } - -#if 0 +#if 0 for (const auto& row : db(select(p.name).from(p).where(p.name.like("Herb%")))) { int64_t id = row.id; @@ -63,8 +64,6 @@ int main() } #endif - - #if 0 for (const auto& row : db(select(p.name, f.name.as(cheesecake)).from(p,f).where(p.id > 7 and p.feature == 3))) { @@ -75,8 +74,6 @@ int main() } #endif - - #if 0 for (const auto& row : db(select(multi_column(all_of(p)).as(p), multi_column(f.name, f.id).as(f)).from(p,f).where(true))) { @@ -87,15 +84,9 @@ int main() //int64_t feature = row.feature; } #endif - - - - - - #if 0 - auto s = select(p.id, p.name, f.id.as(cheesecake)) + auto s = select(p.id, p.name, f.id.as(cheesecake)) .from(p, f) .where(p.name == any(select(f.name) .from(f) @@ -117,40 +108,21 @@ int main() } #endif - - #if !0 - auto dysel = dynamic_select(db).dynamic_columns(p.name).from(p).dynamic_where(); + auto dysel = dynamic_select(db).dynamic_columns(p.name).from(p).dynamic_where(); - if (some_condition) - dysel.selected_columns.add(p.feature); + if (some_condition) + dysel.selected_columns.add(p.feature); - if (some_other_condition) - dysel.where.add(p.id > 17); + if (some_other_condition) + dysel.where.add(p.id > 17); - for (const auto& row : db(dysel)) - { - std::string name = row.name; - std::string feature = row.at("feature"); - } + for (const auto& row : db(dysel)) + { + std::string name = row.name; + std::string feature = row.at("feature"); + } #endif - - - - - - - - - - - - - - - - - - + return 0; } diff --git a/examples/update.cpp b/examples/update.cpp index e9bad556..d5562c25 100644 --- a/examples/update.cpp +++ b/examples/update.cpp @@ -1,25 +1,25 @@ /* * Copyright (c) 2014-2015 Roland Bock * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, + * + * 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, + * + * * 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 + * * 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 + * + * 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. */ @@ -27,12 +27,12 @@ #include "MockDb.h" #include -int main() +int update(int, char* []) { - MockDb db; + MockDb db{}; - test::TabPerson p; - //test::TabFeature q; + const auto p = test::TabPerson{}; - db(update(p).set(p.feature = 7).where(p.id == 23)); + db(update(p).set(p.feature = 7).where(p.id == 23)); + return 0; } diff --git a/include/sqlpp11/aggregate_functions.h b/include/sqlpp11/aggregate_functions.h new file mode 100644 index 00000000..4d250a77 --- /dev/null +++ b/include/sqlpp11/aggregate_functions.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2013-2015, 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_AGGREGATE_FUNCTIONS_H +#define SQLPP_AGGREGATE_FUNCTIONS_H + +#include +#include +#include +#include +#include + +#endif diff --git a/include/sqlpp11/aggregate_functions/avg.h b/include/sqlpp11/aggregate_functions/avg.h new file mode 100644 index 00000000..4d274d46 --- /dev/null +++ b/include/sqlpp11/aggregate_functions/avg.h @@ -0,0 +1,128 @@ +/* + * Copyright (c) 2013-2015, 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_AVG_H +#define SQLPP_AVG_H + +#include +#include + +namespace sqlpp +{ + struct avg_alias_t + { + struct _alias_t + { + static constexpr const char _literal[] = "avg_"; + using _name_t = sqlpp::make_char_sequence; + template + struct _member_t + { + T avg; + T& operator()() + { + return avg; + } + const T& operator()() const + { + return avg; + } + }; + }; + }; + + template + struct avg_t : public expression_operators, floating_point>, + public alias_operators> + { + using _traits = make_traits; + using _nodes = detail::type_vector; + using _can_be_null = std::true_type; + using _is_aggregate_expression = std::true_type; + + static_assert(is_noop::value or std::is_same::value, + "avg() used with flag other than 'distinct'"); + static_assert(is_numeric_t::value, "avg() requires a value expression as argument"); + + using _auto_alias_t = avg_alias_t; + + avg_t(Expr expr) : _expr(expr) + { + } + + avg_t(const avg_t&) = default; + avg_t(avg_t&&) = default; + avg_t& operator=(const avg_t&) = default; + avg_t& operator=(avg_t&&) = default; + ~avg_t() = default; + + Expr _expr; + }; + + template + struct serializer_t> + { + using _serialize_check = serialize_check_of; + using T = avg_t; + + static Context& _(const T& t, Context& context) + { + context << "AVG("; + if (std::is_same::value) + { + serialize(Flag(), context); + context << ' '; + serialize_operand(t._expr, context); + } + else + { + serialize(t._expr, context); + } + context << ")"; + return context; + } + }; + + template + auto avg(T t) -> avg_t> + { + static_assert(not contains_aggregate_function_t>::value, + "avg() cannot be used on an aggregate function"); + static_assert(is_numeric_t>::value, "avg() requires a value expression as argument"); + return {t}; + } + + template + auto avg(const distinct_t&, T t) -> avg_t> + { + static_assert(not contains_aggregate_function_t>::value, + "avg() cannot be used on an aggregate function"); + static_assert(is_numeric_t>::value, "avg() requires a value expression as argument"); + return {t}; + } +} + +#endif diff --git a/include/sqlpp11/aggregate_functions/count.h b/include/sqlpp11/aggregate_functions/count.h new file mode 100644 index 00000000..225aa6a5 --- /dev/null +++ b/include/sqlpp11/aggregate_functions/count.h @@ -0,0 +1,129 @@ +/* + * Copyright (c) 2013-2015, 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_COUNT_H +#define SQLPP_COUNT_H + +#include +#include +#include + +namespace sqlpp +{ + struct count_alias_t + { + struct _alias_t + { + static constexpr const char _literal[] = "count_"; + using _name_t = sqlpp::make_char_sequence; + template + struct _member_t + { + T count; + T& operator()() + { + return count; + } + const T& operator()() const + { + return count; + } + }; + }; + }; + + template + struct count_t : public expression_operators, integral>, + public alias_operators> + { + using _traits = make_traits; + + using _nodes = detail::type_vector; + using _can_be_null = std::false_type; + using _is_aggregate_expression = std::true_type; + + static_assert(is_noop::value or std::is_same::value, + "count() used with flag other than 'distinct'"); + + using _auto_alias_t = count_alias_t; + + count_t(const Expr expr) : _expr(expr) + { + } + + count_t(const count_t&) = default; + count_t(count_t&&) = default; + count_t& operator=(const count_t&) = default; + count_t& operator=(count_t&&) = default; + ~count_t() = default; + + Expr _expr; + }; + + template + struct serializer_t> + { + using _serialize_check = serialize_check_of; + using T = count_t; + + static Context& _(const T& t, Context& context) + { + context << "COUNT("; + if (std::is_same::value) + { + serialize(Flag(), context); + context << ' '; + serialize_operand(t._expr, context); + } + else + { + serialize(t._expr, context); + } + context << ")"; + return context; + } + }; + + template + auto count(T t) -> count_t> + { + static_assert(not contains_aggregate_function_t>::value, + "count() cannot be used on an aggregate function"); + static_assert(is_expression_t>::value, "count() requires an expression as argument"); + return {t}; + } + + template + auto count(const distinct_t&, T t) -> count_t> + { + static_assert(not contains_aggregate_function_t>::value, + "count() cannot be used on an aggregate function"); + static_assert(is_expression_t>::value, "count() requires an expression as argument"); + return {t}; + } +} + +#endif diff --git a/include/sqlpp11/aggregate_functions/max.h b/include/sqlpp11/aggregate_functions/max.h new file mode 100644 index 00000000..abe29d6a --- /dev/null +++ b/include/sqlpp11/aggregate_functions/max.h @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2013-2015, 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_MAX_H +#define SQLPP_MAX_H + +#include +#include + +namespace sqlpp +{ + struct max_alias_t + { + struct _alias_t + { + static constexpr const char _literal[] = "max_"; + using _name_t = sqlpp::make_char_sequence; + template + struct _member_t + { + T max; + T& operator()() + { + return max; + } + const T& operator()() const + { + return max; + } + }; + }; + }; + + template + struct max_t : public expression_operators, value_type_of>, public alias_operators> + { + using _traits = make_traits, tag::is_expression, tag::is_selectable>; + using _nodes = detail::type_vector; + using _can_be_null = std::true_type; + using _is_aggregate_expression = std::true_type; + + using _auto_alias_t = max_alias_t; + + max_t(Expr expr) : _expr(expr) + { + } + + max_t(const max_t&) = default; + max_t(max_t&&) = default; + max_t& operator=(const max_t&) = default; + max_t& operator=(max_t&&) = default; + ~max_t() = default; + + Expr _expr; + }; + + template + struct serializer_t> + { + using _serialize_check = serialize_check_of; + using T = max_t; + + static Context& _(const T& t, Context& context) + { + context << "MAX("; + serialize(t._expr, context); + context << ")"; + return context; + } + }; + + template + auto max(T t) -> max_t> + { + static_assert(not contains_aggregate_function_t>::value, + "max() cannot be used on an aggregate function"); + static_assert(is_expression_t>::value, "max() requires an expression as argument"); + return {t}; + } +} + +#endif diff --git a/include/sqlpp11/aggregate_functions/min.h b/include/sqlpp11/aggregate_functions/min.h new file mode 100644 index 00000000..a82bf9c7 --- /dev/null +++ b/include/sqlpp11/aggregate_functions/min.h @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2013-2015, 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_MIN_H +#define SQLPP_MIN_H + +#include +#include + +namespace sqlpp +{ + struct min_alias_t + { + struct _alias_t + { + static constexpr const char _literal[] = "min_"; + using _name_t = sqlpp::make_char_sequence; + template + struct _member_t + { + T min; + T& operator()() + { + return min; + } + const T& operator()() const + { + return min; + } + }; + }; + }; + + template + struct min_t : public expression_operators, value_type_of>, public alias_operators> + { + using _traits = make_traits, tag::is_expression, tag::is_selectable>; + using _nodes = detail::type_vector; + using _can_be_null = std::true_type; + using _is_aggregate_expression = std::true_type; + + using _auto_alias_t = min_alias_t; + + min_t(Expr expr) : _expr(expr) + { + } + + min_t(const min_t&) = default; + min_t(min_t&&) = default; + min_t& operator=(const min_t&) = default; + min_t& operator=(min_t&&) = default; + ~min_t() = default; + + Expr _expr; + }; + + template + struct serializer_t> + { + using _serialize_check = serialize_check_of; + using T = min_t; + + static Context& _(const T& t, Context& context) + { + context << "MIN("; + serialize(t._expr, context); + context << ")"; + return context; + } + }; + + template + auto min(T t) -> min_t> + { + static_assert(not contains_aggregate_function_t>::value, + "min() cannot be used on an aggregate function"); + static_assert(is_expression_t>::value, "min() requires an expression as argument"); + return {t}; + } +} + +#endif diff --git a/include/sqlpp11/aggregate_functions/sum.h b/include/sqlpp11/aggregate_functions/sum.h new file mode 100644 index 00000000..cee32aed --- /dev/null +++ b/include/sqlpp11/aggregate_functions/sum.h @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2013-2015, 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_SUM_H +#define SQLPP_SUM_H + +#include +#include + +namespace sqlpp +{ + struct sum_alias_t + { + struct _alias_t + { + static constexpr const char _literal[] = "sum_"; + using _name_t = sqlpp::make_char_sequence; + template + struct _member_t + { + T sum; + T& operator()() + { + return sum; + } + const T& operator()() const + { + return sum; + } + }; + }; + }; + + template + struct sum_t : public expression_operators, value_type_of>, + public alias_operators> + { + using _traits = make_traits, tag::is_expression, tag::is_selectable>; + using _nodes = detail::type_vector; + using _can_be_null = std::true_type; + using _is_aggregate_expression = std::true_type; + + static_assert(is_noop::value or std::is_same::value, + "sum() used with flag other than 'distinct'"); + static_assert(is_numeric_t::value, "sum() requires a numeric expression as argument"); + + using _auto_alias_t = sum_alias_t; + + sum_t(Expr expr) : _expr(expr) + { + } + + sum_t(const sum_t&) = default; + sum_t(sum_t&&) = default; + sum_t& operator=(const sum_t&) = default; + sum_t& operator=(sum_t&&) = default; + ~sum_t() = default; + + Expr _expr; + }; + + template + struct serializer_t> + { + using _serialize_check = serialize_check_of; + using T = sum_t; + + static Context& _(const T& t, Context& context) + { + context << "SUM("; + if (std::is_same::value) + { + serialize(Flag(), context); + context << ' '; + serialize_operand(t._expr, context); + } + else + serialize(t._expr, context); + + context << ")"; + return context; + } + }; + + template + auto sum(T t) -> sum_t> + { + static_assert(not contains_aggregate_function_t>::value, + "sum() cannot be used on an aggregate function"); + static_assert(is_numeric_t>::value, "sum() requires a numeric expression as argument"); + return {t}; + } + + template + auto sum(const distinct_t&, T t) -> sum_t> + { + static_assert(not contains_aggregate_function_t>::value, + "sum() cannot be used on an aggregate function"); + static_assert(is_numeric_t>::value, "sum() requires a numeric expression as argument"); + return {t}; + } +} + +#endif diff --git a/include/sqlpp11/alias.h b/include/sqlpp11/alias.h index 013d2e6f..0a041a9e 100644 --- a/include/sqlpp11/alias.h +++ b/include/sqlpp11/alias.h @@ -1,17 +1,17 @@ /* * Copyright (c) 2013-2015, 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 @@ -32,35 +32,44 @@ namespace sqlpp { - template - struct expression_alias_t - { - using _traits = make_traits, tag::is_selectable, tag::is_alias>; - using _nodes = detail::type_vector; + template + struct expression_alias_t + { + using _traits = make_traits, tag::is_selectable, tag::is_alias>; + using _nodes = detail::type_vector; - static_assert(is_expression_t::value, "invalid argument for an expression alias"); - static_assert(not is_alias_t::value, "cannot create an alias of an alias"); + static_assert(is_expression_t::value, "invalid argument for an expression alias"); + static_assert(not is_alias_t::value, "cannot create an alias of an alias"); - using _alias_t = typename AliasProvider::_alias_t; + using _alias_t = typename AliasProvider::_alias_t; - Expression _expression; - }; + expression_alias_t(Expression expression) : _expression(expression) + { + } - template - struct serializer_t> - { - using _serialize_check = serialize_check_of; - using T = expression_alias_t; + expression_alias_t(const expression_alias_t&) = default; + expression_alias_t(expression_alias_t&&) = default; + expression_alias_t& operator=(const expression_alias_t&) = default; + expression_alias_t& operator=(expression_alias_t&&) = default; + ~expression_alias_t() = default; - static Context& _(const T& t, Context& context) - { - serialize_operand(t._expression, context); - context << " AS "; - context << name_of::char_ptr(); - return context; - } - }; + Expression _expression; + }; + template + struct serializer_t> + { + using _serialize_check = serialize_check_of; + using T = expression_alias_t; + + static Context& _(const T& t, Context& context) + { + serialize_operand(t._expression, context); + context << " AS "; + context << name_of::char_ptr(); + return context; + } + }; } #endif diff --git a/include/sqlpp11/alias_operators.h b/include/sqlpp11/alias_operators.h new file mode 100644 index 00000000..0a03eb3f --- /dev/null +++ b/include/sqlpp11/alias_operators.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2013-2015, 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_ALIAS_OPERATORS_H +#define SQLPP_ALIAS_OPERATORS_H + +#include + +namespace sqlpp +{ + template + struct alias_operators + { + template + expression_alias_t as(const alias_provider&) const + { + return {*static_cast(this)}; + } + }; +} +#endif diff --git a/include/sqlpp11/alias_provider.h b/include/sqlpp11/alias_provider.h index 86dba643..38bbf528 100644 --- a/include/sqlpp11/alias_provider.h +++ b/include/sqlpp11/alias_provider.h @@ -1,17 +1,17 @@ /* * Copyright (c) 2013-2015, 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 @@ -30,69 +30,76 @@ #include #include -#define SQLPP_ALIAS_PROVIDER(name) \ - struct name##_t\ -{\ - struct _alias_t\ - {\ - static constexpr const char _literal[] = #name;\ - using _name_t = sqlpp::make_char_sequence;\ - template\ - struct _member_t\ - {\ - T name;\ - T& operator()() { return name; }\ - const T& operator()() const { return name; }\ - };\ - };\ -};\ -constexpr name##_t name = {}; +#define SQLPP_ALIAS_PROVIDER(name) \ + struct name##_t \ + { \ + struct _alias_t \ + { \ + static constexpr const char _literal[] = #name; \ + using _name_t = sqlpp::make_char_sequence; \ + template \ + struct _member_t \ + { \ + T name; \ + T& operator()() \ + { \ + return name; \ + } \ + const T& operator()() const \ + { \ + return name; \ + } \ + }; \ + }; \ + }; \ + constexpr name##_t name = {}; namespace sqlpp { - template - struct is_alias_provider_t - { - static constexpr bool value = false; - }; + template + struct is_alias_provider_t + { + static constexpr bool value = false; + }; - template - struct is_alias_provider_t>::value, void>::type> - { - static constexpr bool value = true; - }; + template + struct is_alias_provider_t< + T, + typename std::enable_if>::value, void>::type> + { + static constexpr bool value = true; + }; - namespace alias - { - SQLPP_ALIAS_PROVIDER(a) - SQLPP_ALIAS_PROVIDER(b) - SQLPP_ALIAS_PROVIDER(c) - SQLPP_ALIAS_PROVIDER(d) - SQLPP_ALIAS_PROVIDER(e) - SQLPP_ALIAS_PROVIDER(f) - SQLPP_ALIAS_PROVIDER(g) - SQLPP_ALIAS_PROVIDER(h) - SQLPP_ALIAS_PROVIDER(i) - SQLPP_ALIAS_PROVIDER(j) - SQLPP_ALIAS_PROVIDER(k) - SQLPP_ALIAS_PROVIDER(l) - SQLPP_ALIAS_PROVIDER(m) - SQLPP_ALIAS_PROVIDER(n) - SQLPP_ALIAS_PROVIDER(o) - SQLPP_ALIAS_PROVIDER(p) - SQLPP_ALIAS_PROVIDER(q) - SQLPP_ALIAS_PROVIDER(s) - SQLPP_ALIAS_PROVIDER(t) - SQLPP_ALIAS_PROVIDER(u) - SQLPP_ALIAS_PROVIDER(v) - SQLPP_ALIAS_PROVIDER(w) - SQLPP_ALIAS_PROVIDER(x) - SQLPP_ALIAS_PROVIDER(y) - SQLPP_ALIAS_PROVIDER(z) - SQLPP_ALIAS_PROVIDER(left) - SQLPP_ALIAS_PROVIDER(right) - } + namespace alias + { + SQLPP_ALIAS_PROVIDER(a) + SQLPP_ALIAS_PROVIDER(b) + SQLPP_ALIAS_PROVIDER(c) + SQLPP_ALIAS_PROVIDER(d) + SQLPP_ALIAS_PROVIDER(e) + SQLPP_ALIAS_PROVIDER(f) + SQLPP_ALIAS_PROVIDER(g) + SQLPP_ALIAS_PROVIDER(h) + SQLPP_ALIAS_PROVIDER(i) + SQLPP_ALIAS_PROVIDER(j) + SQLPP_ALIAS_PROVIDER(k) + SQLPP_ALIAS_PROVIDER(l) + SQLPP_ALIAS_PROVIDER(m) + SQLPP_ALIAS_PROVIDER(n) + SQLPP_ALIAS_PROVIDER(o) + SQLPP_ALIAS_PROVIDER(p) + SQLPP_ALIAS_PROVIDER(q) + SQLPP_ALIAS_PROVIDER(s) + SQLPP_ALIAS_PROVIDER(t) + SQLPP_ALIAS_PROVIDER(u) + SQLPP_ALIAS_PROVIDER(v) + SQLPP_ALIAS_PROVIDER(w) + SQLPP_ALIAS_PROVIDER(x) + SQLPP_ALIAS_PROVIDER(y) + SQLPP_ALIAS_PROVIDER(z) + SQLPP_ALIAS_PROVIDER(left) + SQLPP_ALIAS_PROVIDER(right) + } } #endif - diff --git a/include/sqlpp11/all_of.h b/include/sqlpp11/all_of.h index 55e2c206..32dacf55 100644 --- a/include/sqlpp11/all_of.h +++ b/include/sqlpp11/all_of.h @@ -1,17 +1,17 @@ /* * Copyright (c) 2013-2015, 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 @@ -30,51 +30,41 @@ #include #include #include +#include namespace sqlpp { - template - struct all_of_t - { - using _column_tuple_t = typename Table::_column_tuple_t; + template + struct all_of_t + { + using _column_tuple_t = typename Table::_column_tuple_t; - template - detail::copy_tuple_args_t as(const AliasProvider& alias) - { - return multi_column(_column_tuple_t{}).as(alias); - } - }; + template + detail::copy_tuple_args_t as(const AliasProvider& alias) + { + return multi_column(_column_tuple_t{}).as(alias); + } + }; - template - auto all_of(Table t) -> all_of_t - { - return {}; - } + template + auto all_of(Table) -> all_of_t
+ { + return {}; + } - struct assert_no_stand_alone_all_of_t - { - using type = std::false_type; + SQLPP_PORTABLE_STATIC_ASSERT(assert_no_stand_alone_all_of_t, "all_of(table) seems to be used outside of select"); - template - static void _() - { - static_assert(wrong_t::value, "all_of(table) seems to be used outside of select"); - } - }; - - template - struct serializer_t> - { - using _serialize_check = assert_no_stand_alone_all_of_t; - using T = all_of_t
; - - static Context& _(const T& t, const Context&) - { - _serialize_check::_(); - } - }; + template + struct serializer_t> + { + using _serialize_check = assert_no_stand_alone_all_of_t; + using T = all_of_t
; + static Context& _(const T&, const Context&) + { + _serialize_check::_(); + } + }; } #endif - diff --git a/include/sqlpp11/any.h b/include/sqlpp11/any.h index 8585de1d..d3f125c2 100644 --- a/include/sqlpp11/any.h +++ b/include/sqlpp11/any.h @@ -1,17 +1,17 @@ /* * Copyright (c) 2013-2015, 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 @@ -27,68 +27,55 @@ #ifndef SQLPP_ANY_H #define SQLPP_ANY_H -#include +#include #include #include namespace sqlpp { - template - struct any_t - { - using _traits = make_traits, tag::is_multi_expression>; - using _nodes = detail::type_vector; - struct _alias_t - { - static constexpr const char _literal[] = "any_"; - using _name_t = sqlpp::make_char_sequence; - template - struct _member_t - { - T any; - T& operator()() { return any; } - const T& operator()() const { return any; } - }; - }; + any_t(Select select) : _select(select) + { + } - any_t(Select select): - _select(select) - {} + any_t(const any_t&) = default; + any_t(any_t&&) = default; + any_t& operator=(const any_t&) = default; + any_t& operator=(any_t&&) = default; + ~any_t() = default; - any_t(const any_t&) = default; - any_t(any_t&&) = default; - any_t& operator=(const any_t&) = default; - any_t& operator=(any_t&&) = default; - ~any_t() = default; + Select _select; + }; - Select _select; - }; + template + struct serializer_t> + { + using _serialize_check = serialize_check_of; + using T = any_t; - - static Context& _(const T& t, Context& context) - { - context << "ANY("; - serialize(t._select, context); - context << ")"; - return context; - } - }; - - template - auto any(T t) -> any_t> - { - static_assert(is_select_t>::value, "any() requires a select expression as argument"); - static_assert(is_expression_t>::value, "any() requires a single column select expression as argument"); - // FIXME: can we accept non-values like NULL here? - return { t }; - } + static Context& _(const T& t, Context& context) + { + context << "ANY("; + serialize(t._select, context); + context << ")"; + return context; + } + }; + template + auto any(T t) -> any_t> + { + static_assert(is_select_t>::value, "any() requires a select expression as argument"); + static_assert(is_expression_t>::value, + "any() requires a single column select expression as argument"); + // FIXME: can we accept non-values like NULL here? + return {t}; + } } #endif diff --git a/include/sqlpp11/assignment.h b/include/sqlpp11/assignment.h index 29e3dbd8..7517f8f5 100644 --- a/include/sqlpp11/assignment.h +++ b/include/sqlpp11/assignment.h @@ -1,17 +1,17 @@ /* * Copyright (c) 2013-2015, 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 @@ -37,46 +37,46 @@ namespace sqlpp { - template - struct assignment_t - { - using _traits = make_traits; - using _lhs_t = Lhs; - using _rhs_t = rhs_wrap_t, trivial_value_is_null_t<_lhs_t>::value>; - using _nodes = detail::type_vector<_lhs_t, _rhs_t>; + template + struct assignment_t + { + using _traits = make_traits; + using _lhs_t = Lhs; + using _rhs_t = rhs_wrap_t, trivial_value_is_null_t<_lhs_t>::value>; + using _nodes = detail::type_vector<_lhs_t, _rhs_t>; - static_assert(can_be_null_t<_lhs_t>::value ? true : not (std::is_same<_rhs_t, null_t>::value or is_tvin_t<_rhs_t>::value), "column must not be null"); + static_assert(can_be_null_t<_lhs_t>::value ? true + : not(std::is_same<_rhs_t, null_t>::value or is_tvin_t<_rhs_t>::value), + "column must not be null"); - assignment_t(_lhs_t lhs, _rhs_t rhs): - _lhs(lhs), - _rhs(rhs) - {} + assignment_t(_lhs_t lhs, _rhs_t 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; + 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_t _lhs; - _rhs_t _rhs; - }; + _lhs_t _lhs; + _rhs_t _rhs; + }; - template - struct serializer_t> - { - using T = assignment_t; - using _serialize_check = serialize_check_of; - - static Context& _(const T& t, Context& context) - { - serialize(simple_column(t._lhs), context); - context << "="; - serialize_operand(t._rhs, context); - return context; - } - }; + template + struct serializer_t> + { + using T = assignment_t; + using _serialize_check = serialize_check_of; + static Context& _(const T& t, Context& context) + { + serialize(simple_column(t._lhs), context); + context << "="; + serialize_operand(t._rhs, context); + return context; + } + }; } #endif diff --git a/include/sqlpp11/auto_alias.h b/include/sqlpp11/auto_alias.h new file mode 100644 index 00000000..43ce044c --- /dev/null +++ b/include/sqlpp11/auto_alias.h @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2013-2015, 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_AUTO_ALIAS_H +#define SQLPP_AUTO_ALIAS_H + +#include + +namespace sqlpp +{ + template + struct has_auto_alias_t + { + static constexpr bool value = false; + }; + + template + struct has_auto_alias_t::value>::type> + { + static constexpr bool value = true; + }; + + namespace detail + { + template + struct auto_alias_impl + { + using type = T; + }; + + template + struct auto_alias_impl::value>::type> + { + using type = expression_alias_t; + }; + } + + template + using auto_alias_t = typename detail::auto_alias_impl::type; +} + +#endif diff --git a/include/sqlpp11/avg.h b/include/sqlpp11/avg.h deleted file mode 100644 index d332cf33..00000000 --- a/include/sqlpp11/avg.h +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright (c) 2013-2015, 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_AVG_H -#define SQLPP_AVG_H - -#include -#include - -namespace sqlpp -{ - template - struct avg_t: - public expression_operators, floating_point>, - public alias_operators> - { - using _traits = make_traits; - using _nodes = detail::type_vector; - - static_assert(is_noop::value or std::is_same::value, "avg() used with flag other than 'distinct'"); - static_assert(is_numeric_t::value, "avg() requires a value expression as argument"); - - struct _alias_t - { - static constexpr const char _literal[] = "avg_"; - using _name_t = sqlpp::make_char_sequence; - template - struct _member_t - { - T avg; - T& operator()() { return avg; } - const T& operator()() const { return avg; } - }; - }; - - avg_t(Expr expr): - _expr(expr) - {} - - avg_t(const avg_t&) = default; - avg_t(avg_t&&) = default; - avg_t& operator=(const avg_t&) = default; - avg_t& operator=(avg_t&&) = default; - ~avg_t() = default; - - Expr _expr; - }; - - template - struct serializer_t> - { - using _serialize_check = serialize_check_of; - using T = avg_t; - - static Context& _(const T& t, Context& context) - { - context << "AVG("; - if (std::is_same::value) - { - serialize(Flag(), context); - context << ' '; - serialize_operand(t._expr, context); - } - else - { - serialize(t._expr, context); - } - context << ")"; - return context; - } - }; - - template - auto avg(T t) -> avg_t> - { - static_assert(not contains_aggregate_function_t>::value, "avg() cannot be used on an aggregate function"); - static_assert(is_numeric_t>::value, "avg() requires a value expression as argument"); - return { t }; - } - - template - auto avg(const distinct_t&, T t) -> avg_t> - { - static_assert(not contains_aggregate_function_t>::value, "avg() cannot be used on an aggregate function"); - static_assert(is_numeric_t>::value, "avg() requires a value expression as argument"); - return { t }; - } - -} - -#endif diff --git a/include/sqlpp11/bad_expression.h b/include/sqlpp11/bad_expression.h new file mode 100644 index 00000000..3aa43f0c --- /dev/null +++ b/include/sqlpp11/bad_expression.h @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2013-2015, 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_BAD_EXPRESSION_H +#define SQLPP_BAD_EXPRESSION_H + +#include +#include + +namespace sqlpp +{ + SQLPP_PORTABLE_STATIC_ASSERT(assert_valid_operands, "Invalid operand(s)"); + + template + struct bad_expression + { + template + bad_expression(T&&...) + { + } + using _traits = make_traits; + using _nodes = detail::type_vector<>; + }; + + template + struct serializer_t> + { + using _serialize_check = assert_valid_operands; + using T = bad_expression; + + static Context& _(const T&, Context&); + }; +} + +#endif diff --git a/include/sqlpp11/bad_statement.h b/include/sqlpp11/bad_statement.h index c4094591..03635a28 100644 --- a/include/sqlpp11/bad_statement.h +++ b/include/sqlpp11/bad_statement.h @@ -1,17 +1,17 @@ /* * Copyright (c) 2013-2015, 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 @@ -29,13 +29,13 @@ namespace sqlpp { - struct bad_statement - { - template - bad_statement(T&&...) {} - }; + struct bad_statement + { + template + bad_statement(T&&...) + { + } + }; } #endif - - diff --git a/include/sqlpp11/basic_expression_operators.h b/include/sqlpp11/basic_expression_operators.h index 3bfeafe1..23dd5b6c 100644 --- a/include/sqlpp11/basic_expression_operators.h +++ b/include/sqlpp11/basic_expression_operators.h @@ -1,17 +1,17 @@ /* * Copyright (c) 2013-2015, 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 @@ -28,129 +28,306 @@ #define SQLPP_DETAIL_BASIC_EXPRESSION_OPERATORS_H #include +#include +#include +#include #include #include #include #include #include -#include +#include #include namespace sqlpp { - // basic operators - template - struct basic_expression_operators - { - template - struct _is_valid_comparison_operand - { - static constexpr bool value = - (is_expression_t::value // expressions are OK - or is_multi_expression_t::value) // multi-expressions like ANY are OK for comparisons, too - and ValueType::template _is_valid_operand::value // the correct value type is required, of course - ; - }; + SQLPP_PORTABLE_STATIC_ASSERT(assert_valid_rhs_comparison_operand_t, "invalid rhs operand in comparison"); - template - equal_to_t> operator==(T t) const - { - using rhs = wrap_operand_t; - static_assert(_is_valid_comparison_operand::value, "invalid rhs operand in comparison"); + template + using check_rhs_comparison_operand_t = + static_check_t<(is_expression_t>::value // expressions are OK + or + is_multi_expression_t>::value) // multi-expressions like ANY are + // OK for comparisons, too + and + LhsValueType::template _is_valid_operand< + sqlpp::wrap_operand_t>::value, // the correct value type is required, of course + assert_valid_rhs_comparison_operand_t>; - return { *static_cast(this), {rhs{t}} }; - } + SQLPP_PORTABLE_STATIC_ASSERT(assert_valid_in_arguments_t, "at least one operand of in() is not valid"); - template - not_equal_to_t> operator!=(T t) const - { - using rhs = wrap_operand_t; - static_assert(_is_valid_comparison_operand::value, "invalid rhs operand in comparison"); + template + using check_rhs_in_arguments_t = + static_check_t::value...>::value, + assert_valid_in_arguments_t>; - return { *static_cast(this), {rhs{t}} }; - } + namespace detail + { + template class Expr, typename Lhs> + struct new_unary_expression_impl + { + using type = bad_statement; + }; - template - less_than_t> operator<(T t) const - { - using rhs = wrap_operand_t; - static_assert(_is_valid_comparison_operand::value, "invalid rhs operand in comparison"); + template