Added python code generator as an example

This commit is contained in:
rbock
2014-01-28 20:53:22 +01:00
parent 375dec25f8
commit ec2a8587d4
11 changed files with 259 additions and 216 deletions

View File

@@ -1,6 +1,8 @@
macro (build_and_run arg)
# Add headers to sources to enable file browsing in IDEs
add_executable(${arg} ${arg}.cpp ${sqlpp_headers})
include_directories(${CMAKE_BINARY_DIR}/tests)
add_executable(${arg} ${arg}.cpp ${sqlpp_headers} ${CMAKE_CURRENT_LIST_DIR}/Sample.h)
add_test(${arg} ${arg})
endmacro ()
@@ -12,3 +14,8 @@ build_and_run(SelectTest)
build_and_run(FunctionTest)
build_and_run(PreparedTest)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_LIST_DIR}/Sample.h
COMMAND ${CMAKE_SOURCE_DIR}/scripts/ddl2cpp ${CMAKE_CURRENT_LIST_DIR}/sample.sql Sample test
)

View File

@@ -23,7 +23,7 @@
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "TabSample.h"
#include "Sample.h"
#include "MockDb.h"
#include <sqlpp11/alias_provider.h>
#include <sqlpp11/select.h>
@@ -37,8 +37,8 @@ SQLPP_ALIAS_PROVIDER(kaesekuchen);
int main()
{
TabSample t;
TabFoo f;
test::TabFoo f;
test::TabBar t;
// MEMBER FUNCTIONS
// ----------------

View File

@@ -23,7 +23,7 @@
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "TabSample.h"
#include "Sample.h"
#include "MockDb.h"
#include "is_regular.h"
#include <sqlpp11/insert.h>
@@ -34,7 +34,7 @@ DbMock::_context_t printer(std::cerr);
int main()
{
TabSample t;
test::TabBar t;
auto x = t.alpha = 7;
auto y = t.beta = "kaesekuchen";

View File

@@ -23,7 +23,7 @@
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "TabSample.h"
#include "Sample.h"
#include "MockDb.h"
#include <sqlpp11/alias_provider.h>
#include <sqlpp11/insert.h>
@@ -40,8 +40,8 @@ SQLPP_ALIAS_PROVIDER(kaesekuchen);
int main()
{
TabSample t;
TabFoo f;
test::TabFoo f;
test::TabBar t;
interpret(insert_into(t).columns(t.gamma, t.beta), printer).flush();
interpret(insert_into(t).columns(t.gamma, t.beta).add_values(t.gamma = true, t.beta = "cheesecake"), printer).flush();

View File

@@ -23,7 +23,7 @@
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "TabSample.h"
#include "Sample.h"
#include "MockDb.h"
#include "is_regular.h"
#include <sqlpp11/functions.h>
@@ -35,8 +35,8 @@ DbMock db = {};
int main()
{
TabSample t;
TabFoo f;
test::TabFoo f;
test::TabBar t;
// empty parameter lists
{

View File

@@ -26,7 +26,7 @@
#include <iostream>
#include <sqlpp11/remove.h>
#include <sqlpp11/select.h>
#include "TabSample.h"
#include "Sample.h"
#include "MockDb.h"
#include "is_regular.h"
@@ -36,7 +36,7 @@ DbMock::_context_t printer(std::cerr);
int main()
{
TabSample t;
test::TabBar t;
auto x = t.alpha = 7;
auto y = t.beta = "kaesekuchen";

View File

@@ -23,7 +23,7 @@
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "TabSample.h"
#include "Sample.h"
#include "MockDb.h"
#include "is_regular.h"
#include <sqlpp11/alias_provider.h>
@@ -46,8 +46,8 @@ namespace alias
int main()
{
TabSample t;
TabFoo f;
test::TabFoo f;
test::TabBar t;
// Test a table
{
@@ -342,7 +342,7 @@ int main()
static_assert(T::_is_expression, "T has to be an expression");
static_assert(std::is_same<typename T::_value_type::_is_numeric, std::true_type>::value, "T has to be a numeric");
static_assert(sqlpp::is_numeric_t<T>::value, "T has to be a numeric");
static_assert(sqlpp::is_numeric_t<decltype(t.alpha)>::value, "TabSample.alpha has to be a numeric");
static_assert(sqlpp::is_numeric_t<decltype(t.alpha)>::value, "TabBar.alpha has to be a numeric");
((t.alpha + 7) + 4).asc();
static_assert(sqlpp::is_boolean_t<decltype(t.gamma == t.gamma)>::value, "Comparison expression have to be boolean");
auto x = (t.gamma == true) and (t.alpha == 7);
@@ -352,7 +352,7 @@ int main()
interpret(t.beta + "hallenhalma", printer).flush();
static_assert(sqlpp::must_not_insert_t<decltype(t.alpha)>::value, "alpha must not be inserted");
interpret(t.alpha, printer).flush();
std::cerr << "\n" << sizeof(TabSample) << std::endl;
std::cerr << "\n" << sizeof(test::TabBar) << std::endl;
static_assert(std::is_same<typename decltype(t.alpha)::_value_type::_is_named_expression, std::true_type>::value, "alpha should be a named expression");
static_assert(sqlpp::is_named_expression_t<decltype(t.alpha)>::value, "alpha should be a named expression");
static_assert(sqlpp::is_named_expression_t<decltype(t.alpha.as(alias::a))>::value, "an alias of alpha should be a named expression");

View File

@@ -1,195 +0,0 @@
/*
* Copyright (c) 2013, Roland Bock
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef SQLPP_TAB_SAMPLE_H
#define SQLPP_TAB_SAMPLE_H
#include <sqlpp11/table.h>
#include <sqlpp11/column_types.h>
namespace TabFoo_
{
struct Epsilon
{
struct _name_t
{
static constexpr const char* _get_name() { return "epsilon"; }
template<typename T>
struct _member_t
{
T epsilon;
T& operator()() { return epsilon; }
const T& operator()() const { return epsilon; }
};
};
using _value_type = sqlpp::bigint;
struct _column_type
{
};
};
struct Omega
{
struct _name_t
{
static constexpr const char* _get_name() { return "omega"; }
template<typename T>
struct _member_t
{
T omega;
T& operator()() { return omega; }
const T& operator()() const { return omega; }
};
};
using _value_type = sqlpp::floating_point;
struct _column_type
{
};
};
}
struct TabFoo: sqlpp::table_t<
TabFoo,
TabFoo_::Epsilon,
TabFoo_::Omega
>
{
using _value_type = sqlpp::no_value_t;
struct _name_t
{
static constexpr const char* _get_name() { return "tab_foo"; }
};
template<typename T>
struct _member_t
{
T tabFoo;
T& operator()() { return tabFoo; }
const T& operator()() const { return tabFoo; }
};
template<typename Db>
void serialize_impl(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<typename T>
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;
using _trivial_value_is_null = std::true_type;
using _foreign_key = decltype(TabFoo::omega);
};
};
struct Beta
{
struct _name_t
{
static constexpr const char* _get_name() { return "beta"; }
template<typename T>
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;
using _trivial_value_is_null = std::true_type;
using _must_not_update = std::true_type;
};
};
struct Gamma
{
struct _name_t
{
static constexpr const char* _get_name() { return "gamma"; }
template<typename T>
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 TabSample: sqlpp::table_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<typename T>
struct _member_t
{
T tabSample;
T& operator()() { return tabSample; }
const T& operator()() const { return tabSample; }
};
};
template<typename Db>
void serialize_impl(std::ostream& os, Db& db) const
{
os << _name_t::_get_name();
}
};
#endif

View File

@@ -25,7 +25,7 @@
#include <iostream>
#include <sqlpp11/update.h>
#include "TabSample.h"
#include "Sample.h"
#include "MockDb.h"
#include "is_regular.h"
@@ -34,7 +34,7 @@ DbMock::_context_t printer(std::cerr);
int main()
{
TabSample t;
test::TabBar t;
auto x = t.alpha = 7;
auto y = t.beta = "kaesekuchen";

39
tests/sample.sql Normal file
View File

@@ -0,0 +1,39 @@
/*
* 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_foo
(
epsilon bigint,
omega double
);
CREATE TABLE tab_bar
(
alpha bigint AUTO_INCREMENT,
beta varchar(255) NULL DEFAULT "",
gamma bool NOT NULL
);