From 04e7a36206444db77e472b2315afd3524ef8383c Mon Sep 17 00:00:00 2001 From: rbock Date: Mon, 7 Jul 2014 09:17:13 +0200 Subject: [PATCH 1/5] Adjusted api to recent changes. --- connector_api/bind_result.h | 2 + connector_api/char_result.h | 70 ------------------------------ connector_api/connection.h | 11 +++-- connector_api/prepared_statement.h | 2 + 4 files changed, 11 insertions(+), 74 deletions(-) delete mode 100644 connector_api/char_result.h diff --git a/connector_api/bind_result.h b/connector_api/bind_result.h index d1405e7d..c3b4e233 100644 --- a/connector_api/bind_result.h +++ b/connector_api/bind_result.h @@ -80,7 +80,9 @@ namespace sqlpp */ // 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); ... diff --git a/connector_api/char_result.h b/connector_api/char_result.h deleted file mode 100644 index 02ab2014..00000000 --- a/connector_api/char_result.h +++ /dev/null @@ -1,70 +0,0 @@ -/* - * 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_DATABASE_CHAR_RESULT_H -#define SQLPP_DATABASE_CHAR_RESULT_H - -#include - -namespace sqlpp -{ - namespace database - { - /* - * char_result_t yields results as - * sqlpp11::vendor::char_result_row_t - */ - class char_result_t - { - ::sqlpp11::vendor::char_result_row_t char_result_row; - public: - char_result_t(); // default constructor for a result that will not yield a valid row - char_result_t(...); - char_result_t(const char_result_t&) = delete; - char_result_t(char_result_t&& rhs); - char_result_t& operator=(const char_result_t&) = delete; - char_result_t& operator=(char_result_t&&); - ~char_result_t(); - - bool operator==(const char_result_t& rhs) const; - - template - void next(ResultRow& result_row); - - // Something like - /* - { - next_impl(); - if (_char_result_row.data) - result_row = _char_result_row; - else - result_row.invalidate(); - }; - */ - } -} -#endif diff --git a/connector_api/connection.h b/connector_api/connection.h index b6946324..14d22dd0 100644 --- a/connector_api/connection.h +++ b/connector_api/connection.h @@ -52,7 +52,10 @@ namespace sqlpp { public: using _prepared_statement_t = << handle to a prepared statement of the database >>; - using _context_t = << This context is used to interpret the dynamic parts of dynamic statemtents>>; + 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(); @@ -63,14 +66,14 @@ namespace sqlpp //! "direct" select template - <> select(const Select& s); + <> select(const Select& s); //! prepared select template _prepared_statement_t prepare_select(Select& s); template - <> run_prepared_select(const PreparedSelect& s); // call s._bind_params() + <> run_prepared_select(const PreparedSelect& s); // call s._bind_params() //! "direct insert template @@ -107,7 +110,7 @@ namespace sqlpp //! call run on the argument template - auto run(const T& t) -> decltype(t._run(*this)) + auto operator() (const T& t) -> decltype(t._run(*this)) { return t._run(*this); } diff --git a/connector_api/prepared_statement.h b/connector_api/prepared_statement.h index 5a3dc94a..bcd67293 100644 --- a/connector_api/prepared_statement.h +++ b/connector_api/prepared_statement.h @@ -49,7 +49,9 @@ namespace sqlpp 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); }; From 58b666f20fa344f6905b41722cdc6d1c342c0ec1 Mon Sep 17 00:00:00 2001 From: rbock Date: Thu, 3 Jul 2014 10:15:52 +0200 Subject: [PATCH 2/5] Update README.md Link to Matthijs' postgresql connector and minor updates in the examples --- README.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 4c081bab..4ff58a31 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ TabFoo foo; Db db(/* some arguments*/); // selecting zero or more results, iterating over the results -for (const auto& row : db.run(select(foo.name, foo.hasFun).from(foo).where(foo.id > 17 and foo.name.like("%bar%")))) +for (const auto& row : db(select(foo.name, foo.hasFun).from(foo).where(foo.id > 17 and foo.name.like("%bar%")))) { if (row.name.is_null()) std::cerr << "name is null, will convert to empty string" << std::endl; @@ -58,32 +58,32 @@ for (const auto& row : db.run(select(foo.name, foo.hasFun).from(foo).where(foo.i } // selecting ALL columns of a table -for (const auto& row : db.run(select(all_of(foo)).from(foo).where(foo.hasFun or foo.name == "joker"))) +for (const auto& row : db(select(all_of(foo)).from(foo).where(foo.hasFun or foo.name == "joker"))) { int64_t id = row.id; // numeric fields are implicitly convertible to numeric c++ types } // selecting zero or one row, showing off with an alias: SQLPP_ALIAS_PROVIDER(cheese); -if (const auto& row = db.run(select(foo.name.as(cheese)).from(foo).where(foo.id == 17))) +if (const auto& row = db(select(foo.name.as(cheese)).from(foo).where(foo.id == 17))) { std::cerr << "found: " << row.cheese << std::endl; } // selecting a single row with a single result: -return db.run(select(count(foo.id)).from(foo).where(true)).front().count; +return db(select(count(foo.id)).from(foo).where(true)).front().count; Of course there are joins and subqueries, more functions, order_by, group_by etc. These will be documented soon. // A sample insert -db.run(insert_into(foo).set(foo.id = 17, foo.name = "bar", foo.hasFun = true)); +db(insert_into(foo).set(foo.id = 17, foo.name = "bar", foo.hasFun = true)); // A sample update -db.run(update(foo).set(foo.hasFun = not foo.hasFun).where(foo.name != "nobody")); +db(update(foo).set(foo.hasFun = not foo.hasFun).where(foo.name != "nobody")); // A sample delete -db.run(remove_from(foo).where(not foo.hasFun)); +db(remove_from(foo).where(not foo.hasFun)); ``` Requirements: @@ -99,5 +99,5 @@ 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/pbondo/sqlpp11-connector-postgresql + * PostgreSQL: https://github.com/matthijs/sqlpp11-connector-postgresql From 8ed0042b67f2d95b4eeceec023e295df45af0090 Mon Sep 17 00:00:00 2001 From: rbock Date: Mon, 14 Jul 2014 18:17:53 +0200 Subject: [PATCH 3/5] Added blob (currently synonym of text) --- include/sqlpp11/text.h | 1 + scripts/ddl2cpp | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/include/sqlpp11/text.h b/include/sqlpp11/text.h index 6cce5aea..9fff0b77 100644 --- a/include/sqlpp11/text.h +++ b/include/sqlpp11/text.h @@ -216,6 +216,7 @@ namespace sqlpp } using text = detail::text; + using blob = detail::text; using varchar = detail::text; using char_ = detail::text; diff --git a/scripts/ddl2cpp b/scripts/ddl2cpp index 13011825..e65cf233 100755 --- a/scripts/ddl2cpp +++ b/scripts/ddl2cpp @@ -93,6 +93,10 @@ types = { 'char': 'char_', 'varchar': 'varchar', 'text': 'text', + 'tinyblob': 'blob', + 'blob': 'blob', + 'mediumblob': 'blob', + 'longblob': 'blob', 'bool': 'boolean', 'double': 'floating_point', 'float': 'floating_point', From d94ab56fd836533b05615250b60c95ac1dc9b542 Mon Sep 17 00:00:00 2001 From: rbock Date: Sun, 20 Jul 2014 11:14:44 +0200 Subject: [PATCH 4/5] Made generated file available by default This lowers the threshold for those without python. --- tests/CMakeLists.txt | 14 ++-- tests/Sample.h | 190 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 197 insertions(+), 7 deletions(-) create mode 100644 tests/Sample.h diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index b7109ae7..13942a11 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -16,11 +16,11 @@ build_and_run(FunctionTest) build_and_run(PreparedTest) build_and_run(Minimalistic) -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 - ) +# if you want to use the generator, you can do something like this: +#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/tests/Sample.h b/tests/Sample.h new file mode 100644 index 00000000..33eb36e0 --- /dev/null +++ b/tests/Sample.h @@ -0,0 +1,190 @@ +#ifndef TEST_SAMPLE_H +#define TEST_SAMPLE_H + +#include +#include + +namespace test +{ + namespace TabFoo_ + { + struct Delta + { + struct _name_t + { + static constexpr const char* _get_name() { return "delta"; } + template + struct _member_t + { + T delta; + T& operator()() { return delta; } + const T& operator()() const { return delta; } + }; + }; + using _value_type = sqlpp::varchar; + struct _column_type + { + using _can_be_null = std::true_type; + }; + }; + struct Epsilon + { + struct _name_t + { + static constexpr const char* _get_name() { return "epsilon"; } + template + struct _member_t + { + T epsilon; + T& operator()() { return epsilon; } + const T& operator()() const { return epsilon; } + }; + }; + using _value_type = sqlpp::bigint; + struct _column_type + { + using _can_be_null = std::true_type; + }; + }; + struct Omega + { + struct _name_t + { + static constexpr const char* _get_name() { return "omega"; } + template + struct _member_t + { + T omega; + T& operator()() { return omega; } + const T& operator()() const { return omega; } + }; + }; + using _value_type = sqlpp::floating_point; + struct _column_type + { + using _can_be_null = std::true_type; + }; + }; + } + + struct TabFoo: sqlpp::table_t + { + 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; + T& operator()() { return tabFoo; } + const T& operator()() const { return tabFoo; } + }; + }; + }; + namespace TabBar_ + { + struct Alpha + { + struct _name_t + { + static constexpr const char* _get_name() { return "alpha"; } + template + struct _member_t + { + T alpha; + T& operator()() { return alpha; } + const T& operator()() const { return alpha; } + }; + }; + using _value_type = sqlpp::bigint; + struct _column_type + { + using _must_not_insert = std::true_type; + using _must_not_update = std::true_type; + using _can_be_null = std::true_type; + }; + }; + struct Beta + { + struct _name_t + { + static constexpr const char* _get_name() { return "beta"; } + template + struct _member_t + { + T beta; + T& operator()() { return beta; } + const T& operator()() const { return beta; } + }; + }; + using _value_type = sqlpp::varchar; + struct _column_type + { + using _can_be_null = std::true_type; + }; + }; + struct Gamma + { + struct _name_t + { + static constexpr const char* _get_name() { return "gamma"; } + template + struct _member_t + { + T gamma; + T& operator()() { return gamma; } + const T& operator()() const { return gamma; } + }; + }; + using _value_type = sqlpp::boolean; + struct _column_type + { + using _require_insert = std::true_type; + }; + }; + struct Delta + { + struct _name_t + { + static constexpr const char* _get_name() { return "delta"; } + template + struct _member_t + { + T delta; + T& operator()() { return delta; } + const T& operator()() const { return delta; } + }; + }; + using _value_type = sqlpp::integer; + struct _column_type + { + using _can_be_null = std::true_type; + }; + }; + } + + struct TabBar: sqlpp::table_t + { + using _value_type = sqlpp::no_value_t; + struct _name_t + { + static constexpr const char* _get_name() { return "tab_bar"; } + template + struct _member_t + { + T tabBar; + T& operator()() { return tabBar; } + const T& operator()() const { return tabBar; } + }; + }; + }; +} +#endif From ecda6bc986b2632c7f6cfc30c3f028c1961526f4 Mon Sep 17 00:00:00 2001 From: rbock Date: Sun, 20 Jul 2014 11:43:30 +0200 Subject: [PATCH 5/5] Update README.md Added links to wiki and container backend. --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 4ff58a31..adcb168c 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ sqlpp11 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 Motivation: ----------- @@ -92,7 +93,9 @@ __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.9 on Debian Unstable __Database Connector:__ sqlpp11 requires a certain api in order to connect with the database, see database/api.h. @@ -100,4 +103,8 @@ 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 + +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