mirror of
https://github.com/rbock/sqlpp11.git
synced 2024-11-15 20:31:16 +08:00
Added a few examples
This commit is contained in:
parent
10fa8693cc
commit
9c048786fd
@ -42,6 +42,7 @@ file(GLOB_RECURSE sqlpp_headers ${include_dir}/*.h)
|
|||||||
include_directories(${include_dir})
|
include_directories(${include_dir})
|
||||||
add_subdirectory(tests)
|
add_subdirectory(tests)
|
||||||
add_subdirectory(test_constraints)
|
add_subdirectory(test_constraints)
|
||||||
|
add_subdirectory(examples)
|
||||||
|
|
||||||
install(DIRECTORY "${PROJECT_SOURCE_DIR}/include/sqlpp11" DESTINATION include)
|
install(DIRECTORY "${PROJECT_SOURCE_DIR}/include/sqlpp11" DESTINATION include)
|
||||||
|
|
||||||
|
22
examples/CMakeLists.txt
Normal file
22
examples/CMakeLists.txt
Normal file
@ -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
|
||||||
|
# )
|
||||||
|
|
142
examples/Sample.h
Normal file
142
examples/Sample.h
Normal file
@ -0,0 +1,142 @@
|
|||||||
|
#ifndef TEST_SAMPLE_H
|
||||||
|
#define TEST_SAMPLE_H
|
||||||
|
|
||||||
|
#include <sqlpp11/table.h>
|
||||||
|
#include <sqlpp11/column_types.h>
|
||||||
|
|
||||||
|
namespace test
|
||||||
|
{
|
||||||
|
namespace TabPerson_
|
||||||
|
{
|
||||||
|
struct Id
|
||||||
|
{
|
||||||
|
struct _name_t
|
||||||
|
{
|
||||||
|
static constexpr const char* _get_name() { return "id"; }
|
||||||
|
template<typename T>
|
||||||
|
struct _member_t
|
||||||
|
{
|
||||||
|
T id;
|
||||||
|
T& operator()() { return id; }
|
||||||
|
const T& operator()() const { return id; }
|
||||||
|
};
|
||||||
|
};
|
||||||
|
using _traits = sqlpp::make_traits<sqlpp::integer, sqlpp::tag::must_not_insert, sqlpp::tag::must_not_update, sqlpp::tag::can_be_null>;
|
||||||
|
};
|
||||||
|
struct Name
|
||||||
|
{
|
||||||
|
struct _name_t
|
||||||
|
{
|
||||||
|
static constexpr const char* _get_name() { return "name"; }
|
||||||
|
template<typename T>
|
||||||
|
struct _member_t
|
||||||
|
{
|
||||||
|
T name;
|
||||||
|
T& operator()() { return name; }
|
||||||
|
const T& operator()() const { return name; }
|
||||||
|
};
|
||||||
|
};
|
||||||
|
using _traits = sqlpp::make_traits<sqlpp::varchar, sqlpp::tag::require_insert>;
|
||||||
|
};
|
||||||
|
struct Feature
|
||||||
|
{
|
||||||
|
struct _name_t
|
||||||
|
{
|
||||||
|
static constexpr const char* _get_name() { return "feature"; }
|
||||||
|
template<typename T>
|
||||||
|
struct _member_t
|
||||||
|
{
|
||||||
|
T feature;
|
||||||
|
T& operator()() { return feature; }
|
||||||
|
const T& operator()() const { return feature; }
|
||||||
|
};
|
||||||
|
};
|
||||||
|
using _traits = sqlpp::make_traits<sqlpp::integer, sqlpp::tag::require_insert>;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
struct TabPerson: sqlpp::table_t<TabPerson,
|
||||||
|
TabPerson_::Id,
|
||||||
|
TabPerson_::Name,
|
||||||
|
TabPerson_::Feature>
|
||||||
|
{
|
||||||
|
struct _name_t
|
||||||
|
{
|
||||||
|
static constexpr const char* _get_name() { return "tab_person"; }
|
||||||
|
template<typename T>
|
||||||
|
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<typename T>
|
||||||
|
struct _member_t
|
||||||
|
{
|
||||||
|
T id;
|
||||||
|
T& operator()() { return id; }
|
||||||
|
const T& operator()() const { return id; }
|
||||||
|
};
|
||||||
|
};
|
||||||
|
using _traits = sqlpp::make_traits<sqlpp::integer, sqlpp::tag::must_not_insert, sqlpp::tag::must_not_update, sqlpp::tag::can_be_null>;
|
||||||
|
};
|
||||||
|
struct Name
|
||||||
|
{
|
||||||
|
struct _name_t
|
||||||
|
{
|
||||||
|
static constexpr const char* _get_name() { return "name"; }
|
||||||
|
template<typename T>
|
||||||
|
struct _member_t
|
||||||
|
{
|
||||||
|
T name;
|
||||||
|
T& operator()() { return name; }
|
||||||
|
const T& operator()() const { return name; }
|
||||||
|
};
|
||||||
|
};
|
||||||
|
using _traits = sqlpp::make_traits<sqlpp::varchar, sqlpp::tag::can_be_null>;
|
||||||
|
};
|
||||||
|
struct Fatal
|
||||||
|
{
|
||||||
|
struct _name_t
|
||||||
|
{
|
||||||
|
static constexpr const char* _get_name() { return "fatal"; }
|
||||||
|
template<typename T>
|
||||||
|
struct _member_t
|
||||||
|
{
|
||||||
|
T fatal;
|
||||||
|
T& operator()() { return fatal; }
|
||||||
|
const T& operator()() const { return fatal; }
|
||||||
|
};
|
||||||
|
};
|
||||||
|
using _traits = sqlpp::make_traits<sqlpp::boolean, sqlpp::tag::require_insert>;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
struct TabFeature: sqlpp::table_t<TabFeature,
|
||||||
|
TabFeature_::Id,
|
||||||
|
TabFeature_::Name,
|
||||||
|
TabFeature_::Fatal>
|
||||||
|
{
|
||||||
|
struct _name_t
|
||||||
|
{
|
||||||
|
static constexpr const char* _get_name() { return "tab_feature"; }
|
||||||
|
template<typename T>
|
||||||
|
struct _member_t
|
||||||
|
{
|
||||||
|
T tabFeature;
|
||||||
|
T& operator()() { return tabFeature; }
|
||||||
|
const T& operator()() const { return tabFeature; }
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#endif
|
177
examples/TabSample.h
Normal file
177
examples/TabSample.h
Normal file
@ -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 <sqlpp11/table_base.h>
|
||||||
|
#include <sqlpp11/column_types.h>
|
||||||
|
|
||||||
|
|
||||||
|
namespace TabFoo_
|
||||||
|
{
|
||||||
|
struct Omega
|
||||||
|
{
|
||||||
|
struct _name_t
|
||||||
|
{
|
||||||
|
static constexpr const char* _get_name() { return "omega"; }
|
||||||
|
};
|
||||||
|
template<typename T>
|
||||||
|
struct _member_t
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
template<typename... TT>
|
||||||
|
_name_t(TT&&... t): omega(std::forward<TT>(t)...) {}
|
||||||
|
|
||||||
|
template<typename TT>
|
||||||
|
_name_t& operator=(TT&& t) { omega = std::forward<TT>(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<typename T>
|
||||||
|
struct _member_t
|
||||||
|
{
|
||||||
|
T tabFoo;
|
||||||
|
};
|
||||||
|
template<typename Db>
|
||||||
|
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<typename T>
|
||||||
|
struct _member_t
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
template<typename... TT>
|
||||||
|
_name_t(TT&&... t): alpha(std::forward<TT>(t)...) {}
|
||||||
|
|
||||||
|
template<typename TT>
|
||||||
|
_name_t& operator=(TT&& t) { alpha = std::forward<TT>(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<typename T>
|
||||||
|
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<typename T>
|
||||||
|
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<typename T>
|
||||||
|
struct _member_t
|
||||||
|
{
|
||||||
|
T tabSample;
|
||||||
|
};
|
||||||
|
template<typename Db>
|
||||||
|
void serialize(std::ostream& os, Db& db) const
|
||||||
|
{
|
||||||
|
os << _name_t::_get_name();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
52
examples/insert.cpp
Normal file
52
examples/insert.cpp
Normal file
@ -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 <sqlpp11/sqlpp11.h>
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
40
examples/remove.cpp
Normal file
40
examples/remove.cpp
Normal file
@ -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 <sqlpp11/sqlpp11.h>
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
59
examples/sample.cpp
Normal file
59
examples/sample.cpp
Normal file
@ -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 <sqlpp11/sqlpp11.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
40
examples/sample.sql
Normal file
40
examples/sample.sql
Normal file
@ -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
|
||||||
|
);
|
||||||
|
|
134
examples/select.cpp
Normal file
134
examples/select.cpp
Normal file
@ -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 <sqlpp11/sqlpp11.h>
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
38
examples/update.cpp
Normal file
38
examples/update.cpp
Normal file
@ -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 <sqlpp11/sqlpp11.h>
|
||||||
|
|
||||||
|
MockDb db;
|
||||||
|
|
||||||
|
test::TabPerson p;
|
||||||
|
test::TabFeature q;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
db(update(p).set(p.feature = 7).where(p.id == 23));
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user