diff --git a/CMakeLists.txt b/CMakeLists.txt index 556cdf06..a31fbbc7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -42,6 +42,7 @@ file(GLOB_RECURSE sqlpp_headers ${include_dir}/*.h) include_directories(${include_dir}) add_subdirectory(tests) add_subdirectory(test_constraints) +add_subdirectory(examples) install(DIRECTORY "${PROJECT_SOURCE_DIR}/include/sqlpp11" DESTINATION include) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt new file mode 100644 index 00000000..2633d91f --- /dev/null +++ b/examples/CMakeLists.txt @@ -0,0 +1,22 @@ + +macro (build arg) + # Add headers to sources to enable file browsing in IDEs + include_directories("${CMAKE_BINARY_DIR}/tests") + add_executable("Sqlpp11Example${arg}" "${arg}.cpp" ${sqlpp_headers} "${CMAKE_SOURCE_DIR}/tests/MockDb.h" "${CMAKE_CURRENT_LIST_DIR}/Sample.h") + add_test("${arg}" "${arg}") +endmacro () + +#build(sample) +build(insert) +build(update) +build(remove) +build(select) + +#find_package(PythonInterp REQUIRED) + +#add_custom_command( +# 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 new file mode 100644 index 00000000..43aa3dc2 --- /dev/null +++ b/examples/Sample.h @@ -0,0 +1,142 @@ +#ifndef TEST_SAMPLE_H +#define TEST_SAMPLE_H + +#include +#include + +namespace test +{ + namespace TabPerson_ + { + struct Id + { + struct _name_t + { + static constexpr const char* _get_name() { return "id"; } + template + struct _member_t + { + T id; + T& operator()() { return id; } + const T& operator()() const { return id; } + }; + }; + using _traits = sqlpp::make_traits; + }; + struct Name + { + struct _name_t + { + static constexpr const char* _get_name() { return "name"; } + template + struct _member_t + { + T name; + T& operator()() { return name; } + const T& operator()() const { return name; } + }; + }; + using _traits = sqlpp::make_traits; + }; + struct Feature + { + struct _name_t + { + static constexpr const char* _get_name() { return "feature"; } + template + struct _member_t + { + T feature; + T& operator()() { return feature; } + const T& operator()() const { return feature; } + }; + }; + using _traits = sqlpp::make_traits; + }; + } + + struct TabPerson: sqlpp::table_t + { + struct _name_t + { + static constexpr const char* _get_name() { return "tab_person"; } + template + struct _member_t + { + T tabPerson; + T& operator()() { return tabPerson; } + const T& operator()() const { return tabPerson; } + }; + }; + }; + namespace TabFeature_ + { + struct Id + { + struct _name_t + { + static constexpr const char* _get_name() { return "id"; } + template + struct _member_t + { + T id; + T& operator()() { return id; } + const T& operator()() const { return id; } + }; + }; + using _traits = sqlpp::make_traits; + }; + struct Name + { + struct _name_t + { + static constexpr const char* _get_name() { return "name"; } + template + struct _member_t + { + T name; + T& operator()() { return name; } + const T& operator()() const { return name; } + }; + }; + using _traits = sqlpp::make_traits; + }; + struct Fatal + { + struct _name_t + { + static constexpr const char* _get_name() { return "fatal"; } + template + struct _member_t + { + T fatal; + T& operator()() { return fatal; } + const T& operator()() const { return fatal; } + }; + }; + using _traits = sqlpp::make_traits; + }; + } + + struct TabFeature: sqlpp::table_t + { + struct _name_t + { + static constexpr const char* _get_name() { return "tab_feature"; } + template + struct _member_t + { + T tabFeature; + T& operator()() { return tabFeature; } + const T& operator()() const { return tabFeature; } + }; + }; + }; +} +#endif diff --git a/examples/TabSample.h b/examples/TabSample.h new file mode 100644 index 00000000..377f9c2f --- /dev/null +++ b/examples/TabSample.h @@ -0,0 +1,177 @@ +/* + * Copyright (c) 2013-2014, Roland Bock + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef SQLPP_TAB_SAMPLE_H +#define SQLPP_TAB_SAMPLE_H + +#include +#include + + +namespace TabFoo_ +{ + struct Omega + { + struct _name_t + { + static constexpr const char* _get_name() { return "omega"; } + }; + template + struct _member_t + { + /* + template + _name_t(TT&&... t): omega(std::forward(t)...) {} + + template + _name_t& operator=(TT&& t) { omega = std::forward(t); return *this; } + */ + + T omega; + }; + using _value_type = sqlpp::bigint; + struct _column_type + { + }; + }; +} + +struct TabFoo: sqlpp::table_base_t< + TabFoo, + TabFoo_::Omega + > +{ + using _value_type = sqlpp::no_value_t; + struct _name_t + { + static constexpr const char* _get_name() { return "tab_foo"; } + }; + template + struct _member_t + { + T tabFoo; + }; + template + void serialize(std::ostream& os, Db& db) const + { + os << _name_t::_get_name(); + } +}; + +namespace TabSample_ +{ + struct Alpha + { + struct _name_t + { + static constexpr const char* _get_name() { return "alpha"; } + }; + template + struct _member_t + { + /* + template + _name_t(TT&&... t): alpha(std::forward(t)...) {} + + template + _name_t& operator=(TT&& t) { alpha = std::forward(t); return *this; } + */ + + T alpha; + }; + using _value_type = sqlpp::bigint; + struct _column_type + { + using _must_not_insert = sqlpp::tag_yes; + using _must_not_update = sqlpp::tag_yes; + using _can_be_null = sqlpp::tag_yes; + using _foreign_key = decltype(TabFoo::omega); + }; + }; + + struct Beta + { + struct _name_t + { + static constexpr const char* _get_name() { return "beta"; } + }; + template + struct _member_t + { + T beta; + }; + using _value_type = sqlpp::varchar; + struct _column_type + { + using _can_be_null = sqlpp::tag_yes; + using _must_not_update = sqlpp::tag_yes; + }; + }; + + struct Gamma + { + struct _name_t + { + static constexpr const char* _get_name() { return "gamma"; } + }; + template + struct _member_t + { + T gamma; + }; + using _value_type = sqlpp::boolean; + struct _column_type + { + using _require_insert = sqlpp::tag_yes; + }; + }; +} + +struct TabSample: sqlpp::table_base_t< + TabSample, + TabSample_::Alpha, + TabSample_::Beta, + TabSample_::Gamma + > +{ + using _value_type = sqlpp::no_value_t; + struct _name_t + { + static constexpr const char* _get_name() { return "tab_sample"; } + }; + template + struct _member_t + { + T tabSample; + }; + template + void serialize(std::ostream& os, Db& db) const + { + os << _name_t::_get_name(); + } +}; + +#endif diff --git a/examples/insert.cpp b/examples/insert.cpp new file mode 100644 index 00000000..70bf3109 --- /dev/null +++ b/examples/insert.cpp @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2014, Roland Bock + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "Sample.h" +#include "MockDb.h" +#include + +MockDb db; + +test::TabPerson p; +test::TabFeature f; + +int main() +{ + db(insert_into(f).set(f.name = "loves c++", f.fatal = false)); + + 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; + + db(pi); +} diff --git a/examples/remove.cpp b/examples/remove.cpp new file mode 100644 index 00000000..49b93799 --- /dev/null +++ b/examples/remove.cpp @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2014, Roland Bock + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "Sample.h" +#include "MockDb.h" +#include + +MockDb db; + +test::TabPerson p; +test::TabFeature q; + +int main() +{ + db(remove_from(p) + .using_(p, q) + .where(p.feature == q.id and q.fatal == true)); +} diff --git a/examples/sample.cpp b/examples/sample.cpp new file mode 100644 index 00000000..5328df05 --- /dev/null +++ b/examples/sample.cpp @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2014, Roland Bock + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "Sample.h" +#include "MockDb.h" +#include + +MockDb db; + +test::TabPerson p; +test::TabFeature f; + +int main() +{ + db(insert_into(f).set(f.name = "Loves C++", p.fatal = false)); + + 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 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/sample.sql b/examples/sample.sql new file mode 100644 index 00000000..adc255d6 --- /dev/null +++ b/examples/sample.sql @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2013, Roland Bock + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +CREATE TABLE tab_person +( + id int AUTO_INCREMENT PRIMARY KEY, + name varchar(255) NOT NULL, + feature int NOT NULL +); + +CREATE TABLE tab_feature +( + id int AUTO_INCREMENT PRIMARY KEY, + name varchar(255) NULL DEFAULT "", + fatal bool NOT NULL +); + diff --git a/examples/select.cpp b/examples/select.cpp new file mode 100644 index 00000000..b56ba61b --- /dev/null +++ b/examples/select.cpp @@ -0,0 +1,134 @@ +/* + * Copyright (c) 2014, Roland Bock + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#if defined(__clang__) +#pragma clang diagnostic ignored "-Wunused-variable" +#endif +#if defined(__GNUC__) +#pragma GCC diagnostic ignored "-Wunused-variable" +#endif + +#include "Sample.h" +#include "MockDb.h" +#include + + +MockDb db; + +test::TabPerson p; +test::TabFeature f; + +int main() +{ + for (const auto& row : db(select(all_of(p)).from(p).where(true))) + { + int64_t id = row.id; + std::string name = row.name; + int64_t feature = row.feature; + } + + +#if 0 + for (const auto& row : db(select(p.name).from(p).where(true))) + { + int64_t id = row.id; + std::string name = row.name; + int64_t feature = row.feature; + } +#endif + + + +#if 0 + for (const auto& row : db(select(p.name, f.name).from(p,f).where(true))) + { + //int64_t id = row.id; + //std::string a = row.a; + std::string name = row.name; + //int64_t feature = row.feature; + } +#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))) + { + //int64_t id = row.id; + //std::string a = row.a; + std::string name = row.tabPerson.name; + std::string name1 = row.tabFeature.name; + //int64_t feature = row.feature; + } +#endif + + + + + + + +#if 0 + auto s = select(all_of(p)) + .from(p, f) + .where(p.name == any(select(f.name) + .from(f) + .where(true))) + .group_by(f.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; + } +#endif + + + + + + + + + + + + + + + + + + + + + +} diff --git a/examples/update.cpp b/examples/update.cpp new file mode 100644 index 00000000..82ffa37e --- /dev/null +++ b/examples/update.cpp @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2014, Roland Bock + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "Sample.h" +#include "MockDb.h" +#include + +MockDb db; + +test::TabPerson p; +test::TabFeature q; + +int main() +{ + db(update(p).set(p.feature = 7).where(p.id == 23)); +}