Compare commits
No commits in common. "27f6c81da2a27454a0942aa60b97fecace6c709a" and "ff3a2e4217c509eaa1db11df39adbdc1849b2d4d" have entirely different histories.
27f6c81da2
...
ff3a2e4217
|
|
@ -22,10 +22,6 @@ find_package(SQLite3 REQUIRED)
|
||||||
find_package(PostgreSQL REQUIRED)
|
find_package(PostgreSQL REQUIRED)
|
||||||
find_package(MySQL REQUIRED)
|
find_package(MySQL REQUIRED)
|
||||||
|
|
||||||
message(STATUS "Found ODBC config ${ODBC_CONFIG}")
|
|
||||||
message(STATUS "Adding ODBC include directory: ${ODBC_INCLUDE_DIRS}")
|
|
||||||
message(STATUS "Adding ODBC libs: ${ODBC_LIBRARIES}")
|
|
||||||
|
|
||||||
message(STATUS "Found SQLite3 ${SQLite3_VERSION}")
|
message(STATUS "Found SQLite3 ${SQLite3_VERSION}")
|
||||||
message(STATUS "Adding SQLite3 include directory: ${SQLite3_INCLUDE_DIRS}")
|
message(STATUS "Adding SQLite3 include directory: ${SQLite3_INCLUDE_DIRS}")
|
||||||
message(STATUS "Adding SQLite3 libs: ${SQLite3_LIBRARIES}")
|
message(STATUS "Adding SQLite3 libs: ${SQLite3_LIBRARIES}")
|
||||||
|
|
|
||||||
|
|
@ -52,32 +52,38 @@ static const matador::utils::enum_mapper<Color> color_enum({
|
||||||
{Color::Brown, "brown"}
|
{Color::Brown, "brown"}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
namespace matador::sql {
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
struct matador::sql::data_type_traits<Color, void>
|
struct data_type_traits<Color, void>
|
||||||
{
|
{
|
||||||
inline static data_type_t builtin_type(std::size_t size)
|
inline static data_type_t builtin_type(std::size_t size)
|
||||||
{ return data_type_traits<std::string>::builtin_type(size); }
|
{ return data_type_traits<std::string>::builtin_type(size); }
|
||||||
|
|
||||||
static void read_value(query_result_reader &reader, const char *id, size_t index, Color &value)
|
static void read_value(query_result_reader &reader, const char *id, size_t index, Color &value)
|
||||||
{
|
{
|
||||||
std::string enum_string;
|
std::string enum_string;
|
||||||
reader.read_value(id, index, enum_string, 64);
|
reader.read_value(id, index, enum_string, 64);
|
||||||
if (const auto enum_opt = color_enum.to_enum(enum_string)) {
|
auto enum_opt = color_enum.to_enum(enum_string);
|
||||||
value = enum_opt.value();
|
if (enum_opt) {
|
||||||
}
|
value = enum_opt.value();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static any_type create_value(const Color &value)
|
static any_type create_value(Color &value)
|
||||||
{
|
{
|
||||||
return color_enum.to_string(value);
|
return color_enum.to_string(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void bind_value(parameter_binder &binder, size_t index, Color &value)
|
static void bind_value(parameter_binder &binder, size_t index, Color &value)
|
||||||
{
|
{
|
||||||
binder.bind(index, color_enum.to_string(value));
|
binder.bind(index, color_enum.to_string(value));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
TEST_CASE_METHOD(TypeTraitsTestFixture, "Special handling of attributes with type traits", "[typetraits]")
|
TEST_CASE_METHOD(TypeTraitsTestFixture, "Special handling of attributes with type traits", "[typetraits]")
|
||||||
{
|
{
|
||||||
schema.attach<location>("location");
|
schema.attach<location>("location");
|
||||||
|
|
|
||||||
317
demo/main.cpp
317
demo/main.cpp
|
|
@ -11,219 +11,162 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
struct author {
|
struct author
|
||||||
unsigned long id{};
|
{
|
||||||
std::string first_name;
|
unsigned long id{};
|
||||||
std::string last_name;
|
std::string first_name;
|
||||||
std::string date_of_birth;
|
std::string last_name;
|
||||||
unsigned short year_of_birth{};
|
std::string date_of_birth;
|
||||||
bool distinguished{false};
|
unsigned short year_of_birth{};
|
||||||
|
bool distinguished{false};
|
||||||
|
|
||||||
template<typename Operator>
|
template<typename Operator>
|
||||||
void process( Operator& op ) {
|
void process(Operator &op)
|
||||||
namespace field = matador::utils::access;
|
{
|
||||||
field::primary_key( op, "id", id );
|
namespace field = matador::utils::access;
|
||||||
field::attribute( op, "first_name", first_name, 63 );
|
field::primary_key(op, "id", id);
|
||||||
field::attribute( op, "last_name", last_name, 63 );
|
field::attribute(op, "first_name", first_name, 63);
|
||||||
field::attribute( op, "date_of_birth", date_of_birth, 31 );
|
field::attribute(op, "last_name", last_name, 63);
|
||||||
field::attribute( op, "year_of_birth", year_of_birth );
|
field::attribute(op, "date_of_birth", date_of_birth, 31);
|
||||||
field::attribute( op, "distinguished", distinguished );
|
field::attribute(op, "year_of_birth", year_of_birth);
|
||||||
}
|
field::attribute(op, "distinguished", distinguished);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct book {
|
struct book
|
||||||
unsigned long id{};
|
{
|
||||||
matador::sql::entity<author> book_author;
|
unsigned long id{};
|
||||||
std::string title;
|
matador::sql::entity<author> book_author;
|
||||||
unsigned short published_in{};
|
std::string title;
|
||||||
|
unsigned short published_in{};
|
||||||
|
|
||||||
template<typename Operator>
|
template<typename Operator>
|
||||||
void process( Operator& op ) {
|
void process(Operator &op)
|
||||||
namespace field = matador::utils::access;
|
{
|
||||||
field::primary_key( op, "id", id );
|
namespace field = matador::utils::access;
|
||||||
field::attribute( op, "title", title, 511 );
|
field::primary_key(op, "id", id);
|
||||||
field::has_one( op, "author_id", book_author, matador::utils::default_foreign_attributes );
|
field::attribute(op, "title", title, 511);
|
||||||
field::attribute( op, "published_in", published_in );
|
field::has_one(op, "author_id", book_author, matador::utils::default_foreign_attributes);
|
||||||
}
|
field::attribute(op, "published_in", published_in);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct payload {
|
QUERY_HELPER(authors, id, first_name, last_name, date_of_birth, year_of_birth, distinguished)
|
||||||
unsigned long id{};
|
QUERY_HELPER(books, id, author_id, title, published_in)
|
||||||
|
|
||||||
template<typename Operator>
|
int main()
|
||||||
void process( Operator& op ) {
|
{
|
||||||
namespace field = matador::utils::access;
|
using namespace matador::sql;
|
||||||
field::primary_key( op, "id", id );
|
using namespace matador;
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct job {
|
const std::string env_var{"MATADOR_BACKENDS_PATH"};
|
||||||
enum class job_state {
|
|
||||||
Pending,
|
|
||||||
Running,
|
|
||||||
Succeeded,
|
|
||||||
Failed,
|
|
||||||
Canceled
|
|
||||||
};
|
|
||||||
|
|
||||||
enum class job_mode {
|
std::string dns{"sqlite://demo.db"};
|
||||||
Foreground,
|
schema s("main");
|
||||||
Background
|
s.attach<author>("authors");
|
||||||
};
|
s.attach<book>("books");
|
||||||
|
|
||||||
unsigned long id{};
|
connection c(dns);
|
||||||
matador::sql::entity<payload> payload;
|
c.open();
|
||||||
std::string type;
|
|
||||||
std::string description;
|
|
||||||
job_state state;
|
|
||||||
job_mode mode;
|
|
||||||
|
|
||||||
template<typename Operator>
|
auto create_authors_sql = c.query(s)
|
||||||
void process( Operator& op ) {
|
.create()
|
||||||
namespace field = matador::utils::access;
|
.table<author>(qh::authors)
|
||||||
field::primary_key( op, "id", id );
|
.execute();
|
||||||
field::belongs_to( op, "payload", payload, matador::utils::default_foreign_attributes );
|
|
||||||
field::attribute( op, "type", type, 511 );
|
|
||||||
field::attribute( op, "description", description, 511 );
|
|
||||||
field::attribute( op, "state", state );
|
|
||||||
field::attribute( op, "mode", mode );
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
QUERY_HELPER( authors, id, first_name, last_name, date_of_birth, year_of_birth, distinguished )
|
c.query(s)
|
||||||
|
.create()
|
||||||
|
.table<book>(qh::books)
|
||||||
|
.execute();
|
||||||
|
|
||||||
QUERY_HELPER( books, id, author_id, title, published_in )
|
std::cout << "SQL: " << create_authors_sql << "\n";
|
||||||
|
|
||||||
QUERY_HELPER( job, id, payload, type, description, state, mode )
|
author mc;
|
||||||
|
mc.id = 1;
|
||||||
|
mc.first_name = "Michael";
|
||||||
|
mc.last_name = "Crichton";
|
||||||
|
mc.date_of_birth = "19.8.1954";
|
||||||
|
mc.year_of_birth = 1954;
|
||||||
|
mc.distinguished = true;
|
||||||
|
auto insert_authors_sql = c.query(s)
|
||||||
|
.insert()
|
||||||
|
.into(qh::authors)
|
||||||
|
.values(mc)
|
||||||
|
.execute();
|
||||||
|
|
||||||
QUERY_HELPER( payload, id )
|
std::cout << "SQL: " << insert_authors_sql << "\n";
|
||||||
|
|
||||||
QUERY_HELPER( temporary_table, id );
|
auto result = c.query(s)
|
||||||
|
.select(qh::authors.columns)
|
||||||
|
.from(qh::authors)
|
||||||
|
.fetch_all();
|
||||||
|
|
||||||
int main() {
|
for (const auto &row: result) {
|
||||||
using namespace matador::sql;
|
std::cout << "Author " << row.at(qh::authors.first_name) << "\n";
|
||||||
using namespace matador;
|
}
|
||||||
|
|
||||||
const std::string env_var{"MATADOR_BACKENDS_PATH"};
|
auto update_authors_sql = c.query(s)
|
||||||
|
.update(qh::authors)
|
||||||
|
.set({
|
||||||
|
{qh::authors.first_name, "Stephen"},
|
||||||
|
{qh::authors.last_name, "King"}
|
||||||
|
})
|
||||||
|
.where(qh::authors.last_name == "Crichton")
|
||||||
|
.execute();
|
||||||
|
|
||||||
std::string dns{"sqlite://demo.db"};
|
std::cout << "SQL: " << update_authors_sql << "\n";
|
||||||
schema s( "main" );
|
|
||||||
s.attach<author>( "authors" );
|
|
||||||
s.attach<book>( "books" );
|
|
||||||
|
|
||||||
connection c( dns );
|
auto authors = c.query(s)
|
||||||
c.open();
|
.select(qh::authors.columns)
|
||||||
s.create( c );
|
.from(qh::authors)
|
||||||
|
.fetch_all<author>();
|
||||||
|
|
||||||
auto create_authors_sql = c.query( s )
|
for (const auto &a: authors) {
|
||||||
.create()
|
std::cout << "Author " << a.first_name << "\n";
|
||||||
.table<author>( qh::authors )
|
}
|
||||||
.execute();
|
|
||||||
|
|
||||||
c.query( s )
|
c.query(s)
|
||||||
.create()
|
.insert()
|
||||||
.table<book>( qh::books )
|
.into(qh::books)
|
||||||
.execute();
|
.values({2, "It", mc.id, 1980})
|
||||||
|
.execute();
|
||||||
|
|
||||||
std::cout << "SQL: " << create_authors_sql << "\n";
|
c.query(s)
|
||||||
|
.insert()
|
||||||
|
.into(qh::books)
|
||||||
|
.values({3, "Misery", mc.id, 1984})
|
||||||
|
.execute();
|
||||||
|
|
||||||
author mc;
|
auto select_books_sql = c.query(s)
|
||||||
mc.id = 1;
|
.select(qh::books.columns, {qh::authors.last_name})
|
||||||
mc.first_name = "Michael";
|
.from(qh::books)
|
||||||
mc.last_name = "Crichton";
|
.join_left(qh::authors)
|
||||||
mc.date_of_birth = "19.8.1954";
|
.on(qh::books.author_id == qh::authors.id)
|
||||||
mc.year_of_birth = 1954;
|
.where(qh::books.published_in < 2008 && qh::authors.last_name == "King")
|
||||||
mc.distinguished = true;
|
.group_by(qh::books.published_in)
|
||||||
auto insert_authors_sql = c.query( s )
|
.order_by(qh::books.title).asc()
|
||||||
.insert()
|
.limit(5)
|
||||||
.into( qh::authors )
|
.offset(2)
|
||||||
.values( mc )
|
.fetch_all();
|
||||||
.execute();
|
|
||||||
|
|
||||||
std::cout << "SQL: " << insert_authors_sql << "\n";
|
for (const auto &r: select_books_sql) {
|
||||||
|
std::cout << "R: " << r.at(qh::books.title) << ", " << r.at(qh::authors.last_name) << "\n";
|
||||||
|
}
|
||||||
|
// SELECT book.title, book.id, book.author_id, book.published_in, author.name
|
||||||
|
// FROM book
|
||||||
|
// INNER JOIN author ON book.author_id = author.id
|
||||||
|
// WHERE book.published_in < 2008 AND author.name = "Michael Crichton"
|
||||||
|
// ORDER BY "book.title" ASC
|
||||||
|
// OFFSET 2 LIMIT 5
|
||||||
|
|
||||||
auto result = c.query( s )
|
c.query(s).drop().table(qh::books).execute();
|
||||||
.select( qh::authors.columns )
|
|
||||||
.from( qh::authors )
|
|
||||||
.fetch_all();
|
|
||||||
|
|
||||||
for (const auto& row: result) { std::cout << "Author " << row.at( qh::authors.first_name ) << "\n"; }
|
auto drop_authors_sql = c.query(s)
|
||||||
|
.drop()
|
||||||
|
.table(qh::authors)
|
||||||
|
.execute();
|
||||||
|
|
||||||
auto update_authors_sql = c.query( s )
|
std::cout << "SQL: " << drop_authors_sql << "\n";
|
||||||
.update( qh::authors )
|
|
||||||
.set( {{qh::authors.first_name, "Stephen"},
|
|
||||||
{qh::authors.last_name, "King"}} )
|
|
||||||
.where( qh::authors.last_name == "Crichton" )
|
|
||||||
.execute();
|
|
||||||
|
|
||||||
std::cout << "SQL: " << update_authors_sql << "\n";
|
return 0;
|
||||||
|
}
|
||||||
auto authors = c.query( s )
|
|
||||||
.select( qh::authors.columns )
|
|
||||||
.from( qh::authors )
|
|
||||||
.fetch_all<author>();
|
|
||||||
|
|
||||||
for (const auto& a: authors) { std::cout << "Author " << a.first_name << "\n"; }
|
|
||||||
|
|
||||||
c.query( s )
|
|
||||||
.insert()
|
|
||||||
.into( qh::books )
|
|
||||||
.values( {2, "It", mc.id, 1980} )
|
|
||||||
.execute();
|
|
||||||
|
|
||||||
c.query( s )
|
|
||||||
.insert()
|
|
||||||
.into( qh::books )
|
|
||||||
.values( {3, "Misery", mc.id, 1984} )
|
|
||||||
.execute();
|
|
||||||
|
|
||||||
auto select_books_sql = c.query( s )
|
|
||||||
.select( qh::books.columns, {qh::authors.last_name} )
|
|
||||||
.from( qh::books )
|
|
||||||
.join_left( qh::authors )
|
|
||||||
.on( qh::books.author_id == qh::authors.id )
|
|
||||||
.where( qh::books.published_in < 2008 && qh::authors.last_name == "King" )
|
|
||||||
.group_by( qh::books.published_in )
|
|
||||||
.order_by( qh::books.title ).asc()
|
|
||||||
.limit( 5 )
|
|
||||||
.offset( 2 )
|
|
||||||
.fetch_all();
|
|
||||||
|
|
||||||
for (const auto& r: select_books_sql) { std::cout << "R: " << r.at( qh::books.title ) << ", " << r.at( qh::authors.last_name ) << "\n"; }
|
|
||||||
// SELECT book.title, book.id, book.author_id, book.published_in, author.name
|
|
||||||
// FROM book
|
|
||||||
// INNER JOIN author ON book.author_id = author.id
|
|
||||||
// WHERE book.published_in < 2008 AND author.name = "Michael Crichton"
|
|
||||||
// ORDER BY "book.title" ASC
|
|
||||||
// OFFSET 2 LIMIT 5
|
|
||||||
|
|
||||||
c.query( s ).drop().table( qh::books ).execute();
|
|
||||||
|
|
||||||
auto drop_authors_sql = c.query( s )
|
|
||||||
.drop()
|
|
||||||
.table( qh::authors )
|
|
||||||
.execute();
|
|
||||||
|
|
||||||
std::cout << "SQL: " << drop_authors_sql << "\n";
|
|
||||||
|
|
||||||
auto res = c.query( s )
|
|
||||||
.select( {qh::payload.id} )
|
|
||||||
.from( qh::payload )
|
|
||||||
.join_left( qh::job )
|
|
||||||
.on( qh::job.payload == qh::payload.id )
|
|
||||||
.where(
|
|
||||||
in( qh::payload.id, c.query( s )
|
|
||||||
.select( {qh::job.state} )
|
|
||||||
.from( qh::job )
|
|
||||||
.where( qh::job.state == job::job_state::Running )
|
|
||||||
) &&
|
|
||||||
in( qh::payload.id, c.query( s )
|
|
||||||
.select( {qh::temporary_table.id} )
|
|
||||||
.from( qh::temporary_table ) )
|
|
||||||
)
|
|
||||||
.build();
|
|
||||||
// .fetch_value<unsigned long>();
|
|
||||||
std::cout << "SQL: " << res.sql << "\n";
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,6 @@
|
||||||
#ifndef QUERY_CONDITION_HPP
|
#ifndef QUERY_CONDITION_HPP
|
||||||
#define QUERY_CONDITION_HPP
|
#define QUERY_CONDITION_HPP
|
||||||
|
|
||||||
#include "matador/sql/any_type_to_string_visitor.hpp"
|
|
||||||
#include "matador/sql/query_result.hpp"
|
|
||||||
#include "matador/sql/basic_condition.hpp"
|
#include "matador/sql/basic_condition.hpp"
|
||||||
#include "matador/sql/dialect.hpp"
|
#include "matador/sql/dialect.hpp"
|
||||||
#include "matador/sql/placeholder.hpp"
|
#include "matador/sql/placeholder.hpp"
|
||||||
|
|
@ -28,8 +26,6 @@ namespace matador::sql {
|
||||||
|
|
||||||
/// @cond MATADOR_DEV
|
/// @cond MATADOR_DEV
|
||||||
|
|
||||||
class query_select;
|
|
||||||
|
|
||||||
template<class L, class R, class Enabled = void>
|
template<class L, class R, class Enabled = void>
|
||||||
class condition;
|
class condition;
|
||||||
|
|
||||||
|
|
@ -47,7 +43,6 @@ public:
|
||||||
template<class T>
|
template<class T>
|
||||||
class condition<column, T, typename std::enable_if<
|
class condition<column, T, typename std::enable_if<
|
||||||
std::is_scalar<T>::value &&
|
std::is_scalar<T>::value &&
|
||||||
!std::is_enum<T>::value &&
|
|
||||||
!std::is_same<std::string, T>::value &&
|
!std::is_same<std::string, T>::value &&
|
||||||
!std::is_same<const char*, T>::value>::type> : public basic_column_condition
|
!std::is_same<const char*, T>::value>::type> : public basic_column_condition
|
||||||
{
|
{
|
||||||
|
|
@ -87,32 +82,10 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
class condition<column, T, std::enable_if_t<std::is_enum_v<T>>> final : public basic_column_condition
|
class condition<T, column, typename std::enable_if<
|
||||||
{
|
std::is_scalar<T>::value &&
|
||||||
public:
|
!std::is_same<std::string, T>::value &&
|
||||||
condition(const column &fld, basic_condition::operand_t op, T val)
|
!std::is_same<const char*, T>::value>::type> : public basic_column_condition
|
||||||
: basic_column_condition(fld, op)
|
|
||||||
, value(val)
|
|
||||||
{ }
|
|
||||||
|
|
||||||
T value;
|
|
||||||
|
|
||||||
std::string evaluate(const dialect &d, query_context &query) const override
|
|
||||||
{
|
|
||||||
auto at = data_type_traits<T>::create_value(value);
|
|
||||||
any_type_to_string_visitor value_to_string(d, query);
|
|
||||||
std::visit(value_to_string, at);
|
|
||||||
return "'" + value_to_string.result + "' " + operand + " " + d.prepare_identifier(field_);
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
template<class T>
|
|
||||||
class condition<T, column, std::enable_if_t<
|
|
||||||
std::is_scalar_v<T> &&
|
|
||||||
!std::is_same_v<std::string, T> &&
|
|
||||||
!std::is_same_v<const char*, T>>> final : public basic_column_condition
|
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
condition(T val, basic_condition::operand_t op, const column &fld)
|
condition(T val, basic_condition::operand_t op, const column &fld)
|
||||||
|
|
@ -242,7 +215,7 @@ public:
|
||||||
* @param op Operand of the condition
|
* @param op Operand of the condition
|
||||||
* @param q The query to be evaluated to the IN arguments
|
* @param q The query to be evaluated to the IN arguments
|
||||||
*/
|
*/
|
||||||
condition(column col, basic_condition::operand_t op, const query_context &q);
|
condition(column col, basic_condition::operand_t op, query_context &q);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Evaluates the condition
|
* @brief Evaluates the condition
|
||||||
|
|
@ -256,7 +229,7 @@ public:
|
||||||
std::string evaluate(const dialect &d, query_context &query) const override;
|
std::string evaluate(const dialect &d, query_context &query) const override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
query_context query_;
|
query_context &query_;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -442,8 +415,7 @@ condition<column, std::initializer_list<V>> in(const column &col, std::initializ
|
||||||
* @param q The query to be executes as sub select
|
* @param q The query to be executes as sub select
|
||||||
* @return The condition object
|
* @return The condition object
|
||||||
*/
|
*/
|
||||||
condition<column, query_context> in(const column &col, const query_context &q);
|
condition<column, query_context> in(const column &col, query_context &&q);
|
||||||
condition<column, query_context> in(const column &col, const query_select &q);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Creates a between condition.
|
* @brief Creates a between condition.
|
||||||
|
|
|
||||||
|
|
@ -59,7 +59,7 @@ template <> struct data_type_traits<nullptr_t, void>
|
||||||
static void read_value(query_result_reader &reader, const char *id, size_t index, nullptr_t &/*value*/);
|
static void read_value(query_result_reader &reader, const char *id, size_t index, nullptr_t &/*value*/);
|
||||||
static void bind_value(parameter_binder &binder, size_t index, nullptr_t &/*value*/);
|
static void bind_value(parameter_binder &binder, size_t index, nullptr_t &/*value*/);
|
||||||
static void bind_result_value(result_parameter_binder &binder, size_t index, nullptr_t &/*value*/);
|
static void bind_result_value(result_parameter_binder &binder, size_t index, nullptr_t &/*value*/);
|
||||||
inline static any_type create_value(const char &value) { return value; }
|
inline static any_type create_value(char &value) { return value; }
|
||||||
};
|
};
|
||||||
|
|
||||||
template <> struct data_type_traits<char, void>
|
template <> struct data_type_traits<char, void>
|
||||||
|
|
@ -68,7 +68,7 @@ template <> struct data_type_traits<char, void>
|
||||||
static void read_value(query_result_reader &reader, const char *id, size_t index, char &value);
|
static void read_value(query_result_reader &reader, const char *id, size_t index, char &value);
|
||||||
static void bind_value(parameter_binder &binder, size_t index, char &value);
|
static void bind_value(parameter_binder &binder, size_t index, char &value);
|
||||||
static void bind_result_value(result_parameter_binder &binder, size_t index, char &value);
|
static void bind_result_value(result_parameter_binder &binder, size_t index, char &value);
|
||||||
inline static any_type create_value(const char &value) { return value; }
|
inline static any_type create_value(char &value) { return value; }
|
||||||
};
|
};
|
||||||
|
|
||||||
template <> struct data_type_traits<short, void>
|
template <> struct data_type_traits<short, void>
|
||||||
|
|
@ -77,7 +77,7 @@ template <> struct data_type_traits<short, void>
|
||||||
static void read_value(query_result_reader &reader, const char *id, size_t index, short &value);
|
static void read_value(query_result_reader &reader, const char *id, size_t index, short &value);
|
||||||
static void bind_value(parameter_binder &binder, size_t index, short &value);
|
static void bind_value(parameter_binder &binder, size_t index, short &value);
|
||||||
static void bind_result_value(result_parameter_binder &binder, size_t index, short &value);
|
static void bind_result_value(result_parameter_binder &binder, size_t index, short &value);
|
||||||
inline static any_type create_value(const short &value) { return value; }
|
inline static any_type create_value(short &value) { return value; }
|
||||||
};
|
};
|
||||||
|
|
||||||
template <> struct data_type_traits<int, void>
|
template <> struct data_type_traits<int, void>
|
||||||
|
|
@ -86,7 +86,7 @@ template <> struct data_type_traits<int, void>
|
||||||
static void read_value(query_result_reader &reader, const char *id, size_t index, int &value);
|
static void read_value(query_result_reader &reader, const char *id, size_t index, int &value);
|
||||||
static void bind_value(parameter_binder &binder, size_t index, int &value);
|
static void bind_value(parameter_binder &binder, size_t index, int &value);
|
||||||
static void bind_result_value(result_parameter_binder &binder, size_t index, int &value);
|
static void bind_result_value(result_parameter_binder &binder, size_t index, int &value);
|
||||||
inline static any_type create_value(const int &value) { return value; }
|
inline static any_type create_value(int &value) { return value; }
|
||||||
};
|
};
|
||||||
|
|
||||||
template <> struct data_type_traits<long, void>
|
template <> struct data_type_traits<long, void>
|
||||||
|
|
@ -95,7 +95,7 @@ template <> struct data_type_traits<long, void>
|
||||||
static void read_value(query_result_reader &reader, const char *id, size_t index, long &value);
|
static void read_value(query_result_reader &reader, const char *id, size_t index, long &value);
|
||||||
static void bind_value(parameter_binder &binder, size_t index, long &value);
|
static void bind_value(parameter_binder &binder, size_t index, long &value);
|
||||||
static void bind_result_value(result_parameter_binder &binder, size_t index, long &value);
|
static void bind_result_value(result_parameter_binder &binder, size_t index, long &value);
|
||||||
inline static any_type create_value(const long &value) { return value; }
|
inline static any_type create_value(long &value) { return value; }
|
||||||
};
|
};
|
||||||
|
|
||||||
template <> struct data_type_traits<long long, void>
|
template <> struct data_type_traits<long long, void>
|
||||||
|
|
@ -104,7 +104,7 @@ template <> struct data_type_traits<long long, void>
|
||||||
static void read_value(query_result_reader &reader, const char *id, size_t index, long long &value);
|
static void read_value(query_result_reader &reader, const char *id, size_t index, long long &value);
|
||||||
static void bind_value(parameter_binder &binder, size_t index, long long &value);
|
static void bind_value(parameter_binder &binder, size_t index, long long &value);
|
||||||
static void bind_result_value(result_parameter_binder &binder, size_t index, long long &value);
|
static void bind_result_value(result_parameter_binder &binder, size_t index, long long &value);
|
||||||
inline static any_type create_value(const long long &value) { return value; }
|
inline static any_type create_value(long long &value) { return value; }
|
||||||
};
|
};
|
||||||
|
|
||||||
template <> struct data_type_traits<unsigned char, void>
|
template <> struct data_type_traits<unsigned char, void>
|
||||||
|
|
@ -113,7 +113,7 @@ template <> struct data_type_traits<unsigned char, void>
|
||||||
static void read_value(query_result_reader &reader, const char *id, size_t index, unsigned char &value);
|
static void read_value(query_result_reader &reader, const char *id, size_t index, unsigned char &value);
|
||||||
static void bind_value(parameter_binder &binder, size_t index, unsigned char &value);
|
static void bind_value(parameter_binder &binder, size_t index, unsigned char &value);
|
||||||
static void bind_result_value(result_parameter_binder &binder, size_t index, unsigned char &value);
|
static void bind_result_value(result_parameter_binder &binder, size_t index, unsigned char &value);
|
||||||
inline static any_type create_value(const unsigned char &value) { return value; }
|
inline static any_type create_value(unsigned char &value) { return value; }
|
||||||
};
|
};
|
||||||
|
|
||||||
template <> struct data_type_traits<unsigned short, void>
|
template <> struct data_type_traits<unsigned short, void>
|
||||||
|
|
@ -122,7 +122,7 @@ template <> struct data_type_traits<unsigned short, void>
|
||||||
static void read_value(query_result_reader &reader, const char *id, size_t index, unsigned short &value);
|
static void read_value(query_result_reader &reader, const char *id, size_t index, unsigned short &value);
|
||||||
static void bind_value(parameter_binder &binder, size_t index, unsigned short &value);
|
static void bind_value(parameter_binder &binder, size_t index, unsigned short &value);
|
||||||
static void bind_result_value(result_parameter_binder &binder, size_t index, unsigned short &value);
|
static void bind_result_value(result_parameter_binder &binder, size_t index, unsigned short &value);
|
||||||
inline static any_type create_value(const unsigned short &value) { return value; }
|
inline static any_type create_value(unsigned short &value) { return value; }
|
||||||
};
|
};
|
||||||
|
|
||||||
template <> struct data_type_traits<unsigned int, void>
|
template <> struct data_type_traits<unsigned int, void>
|
||||||
|
|
@ -131,7 +131,7 @@ template <> struct data_type_traits<unsigned int, void>
|
||||||
static void read_value(query_result_reader &reader, const char *id, size_t index, unsigned int &value);
|
static void read_value(query_result_reader &reader, const char *id, size_t index, unsigned int &value);
|
||||||
static void bind_value(parameter_binder &binder, size_t index, unsigned int &value);
|
static void bind_value(parameter_binder &binder, size_t index, unsigned int &value);
|
||||||
static void bind_result_value(result_parameter_binder &binder, size_t index, unsigned int &value);
|
static void bind_result_value(result_parameter_binder &binder, size_t index, unsigned int &value);
|
||||||
inline static any_type create_value(const unsigned int &value) { return value; }
|
inline static any_type create_value(unsigned int &value) { return value; }
|
||||||
};
|
};
|
||||||
|
|
||||||
template <> struct data_type_traits<unsigned long, void>
|
template <> struct data_type_traits<unsigned long, void>
|
||||||
|
|
@ -140,7 +140,7 @@ template <> struct data_type_traits<unsigned long, void>
|
||||||
static void read_value(query_result_reader &reader, const char *id, size_t index, unsigned long &value);
|
static void read_value(query_result_reader &reader, const char *id, size_t index, unsigned long &value);
|
||||||
static void bind_value(parameter_binder &binder, size_t index, unsigned long &value);
|
static void bind_value(parameter_binder &binder, size_t index, unsigned long &value);
|
||||||
static void bind_result_value(result_parameter_binder &binder, size_t index, unsigned long &value);
|
static void bind_result_value(result_parameter_binder &binder, size_t index, unsigned long &value);
|
||||||
inline static any_type create_value(const unsigned long &value) { return value; }
|
inline static any_type create_value(unsigned long &value) { return value; }
|
||||||
};
|
};
|
||||||
|
|
||||||
template <> struct data_type_traits<unsigned long long, void>
|
template <> struct data_type_traits<unsigned long long, void>
|
||||||
|
|
@ -149,7 +149,7 @@ template <> struct data_type_traits<unsigned long long, void>
|
||||||
static void read_value(query_result_reader &reader, const char *id, size_t index, unsigned long long &value);
|
static void read_value(query_result_reader &reader, const char *id, size_t index, unsigned long long &value);
|
||||||
static void bind_value(parameter_binder &binder, size_t index, unsigned long long &value);
|
static void bind_value(parameter_binder &binder, size_t index, unsigned long long &value);
|
||||||
static void bind_result_value(result_parameter_binder &binder, size_t index, unsigned long long &value);
|
static void bind_result_value(result_parameter_binder &binder, size_t index, unsigned long long &value);
|
||||||
inline static any_type create_value(const unsigned long long &value) { return value; }
|
inline static any_type create_value(unsigned long long &value) { return value; }
|
||||||
};
|
};
|
||||||
|
|
||||||
template <> struct data_type_traits<bool, void>
|
template <> struct data_type_traits<bool, void>
|
||||||
|
|
@ -158,7 +158,7 @@ template <> struct data_type_traits<bool, void>
|
||||||
static void read_value(query_result_reader &reader, const char *id, size_t index, bool &value);
|
static void read_value(query_result_reader &reader, const char *id, size_t index, bool &value);
|
||||||
static void bind_value(parameter_binder &binder, size_t index, bool &value);
|
static void bind_value(parameter_binder &binder, size_t index, bool &value);
|
||||||
static void bind_result_value(result_parameter_binder &binder, size_t index, bool &value);
|
static void bind_result_value(result_parameter_binder &binder, size_t index, bool &value);
|
||||||
inline static any_type create_value(const bool &value) { return value; }
|
inline static any_type create_value(bool &value) { return value; }
|
||||||
};
|
};
|
||||||
|
|
||||||
template <> struct data_type_traits<float, void>
|
template <> struct data_type_traits<float, void>
|
||||||
|
|
@ -167,7 +167,7 @@ template <> struct data_type_traits<float, void>
|
||||||
static void read_value(query_result_reader &reader, const char *id, size_t index, float &value);
|
static void read_value(query_result_reader &reader, const char *id, size_t index, float &value);
|
||||||
static void bind_result_value(result_parameter_binder &binder, size_t index, float &value);
|
static void bind_result_value(result_parameter_binder &binder, size_t index, float &value);
|
||||||
static void bind_value(parameter_binder &binder, size_t index, float &value);
|
static void bind_value(parameter_binder &binder, size_t index, float &value);
|
||||||
inline static any_type create_value(const float &value) { return value; }
|
inline static any_type create_value(float &value) { return value; }
|
||||||
};
|
};
|
||||||
|
|
||||||
template <> struct data_type_traits<double, void>
|
template <> struct data_type_traits<double, void>
|
||||||
|
|
@ -176,7 +176,7 @@ template <> struct data_type_traits<double, void>
|
||||||
static void read_value(query_result_reader &reader, const char *id, size_t index, double &value);
|
static void read_value(query_result_reader &reader, const char *id, size_t index, double &value);
|
||||||
static void bind_result_value(result_parameter_binder &binder, size_t index, double &value);
|
static void bind_result_value(result_parameter_binder &binder, size_t index, double &value);
|
||||||
static void bind_value(parameter_binder &binder, size_t index, double &value);
|
static void bind_value(parameter_binder &binder, size_t index, double &value);
|
||||||
inline static any_type create_value(const double &value) { return value; }
|
inline static any_type create_value(double &value) { return value; }
|
||||||
};
|
};
|
||||||
|
|
||||||
template <> struct data_type_traits<const char*, void>
|
template <> struct data_type_traits<const char*, void>
|
||||||
|
|
@ -203,7 +203,7 @@ template <> struct data_type_traits<std::string, void>
|
||||||
static void read_value(query_result_reader &reader, const char *id, size_t index, std::string &value, size_t size);
|
static void read_value(query_result_reader &reader, const char *id, size_t index, std::string &value, size_t size);
|
||||||
static void bind_value(parameter_binder &binder, size_t index, std::string &value, size_t size = 0);
|
static void bind_value(parameter_binder &binder, size_t index, std::string &value, size_t size = 0);
|
||||||
static void bind_result_value(result_parameter_binder &binder, size_t index, std::string &value, size_t size = 0);
|
static void bind_result_value(result_parameter_binder &binder, size_t index, std::string &value, size_t size = 0);
|
||||||
inline static any_type create_value(const std::string &value) { return value; }
|
inline static any_type create_value(std::string &value) { return value; }
|
||||||
};
|
};
|
||||||
|
|
||||||
template <> struct data_type_traits<utils::blob, void>
|
template <> struct data_type_traits<utils::blob, void>
|
||||||
|
|
@ -212,7 +212,7 @@ template <> struct data_type_traits<utils::blob, void>
|
||||||
static void read_value(query_result_reader &reader, const char *id, size_t index, utils::blob &value);
|
static void read_value(query_result_reader &reader, const char *id, size_t index, utils::blob &value);
|
||||||
static void bind_value(parameter_binder &binder, size_t index, utils::blob &value);
|
static void bind_value(parameter_binder &binder, size_t index, utils::blob &value);
|
||||||
static void bind_result_value(result_parameter_binder &binder, size_t index, utils::blob &value);
|
static void bind_result_value(result_parameter_binder &binder, size_t index, utils::blob &value);
|
||||||
inline static any_type create_value(const utils::blob &value) { return value; }
|
inline static any_type create_value(utils::blob &value) { return value; }
|
||||||
};
|
};
|
||||||
|
|
||||||
//template <> struct data_type_traits<matador::date>
|
//template <> struct data_type_traits<matador::date>
|
||||||
|
|
@ -232,7 +232,7 @@ template <> struct data_type_traits<utils::blob, void>
|
||||||
//};
|
//};
|
||||||
|
|
||||||
template < typename EnumType >
|
template < typename EnumType >
|
||||||
struct data_type_traits<EnumType, std::enable_if_t<std::is_enum_v<EnumType>>>
|
struct data_type_traits<EnumType, typename std::enable_if<std::is_enum<EnumType>::value>::type>
|
||||||
{
|
{
|
||||||
inline static data_type_t builtin_type(std::size_t /*size*/) { return data_type_t::type_int; }
|
inline static data_type_t builtin_type(std::size_t /*size*/) { return data_type_t::type_int; }
|
||||||
static void read_value(query_result_reader &reader, const char *id, size_t index, EnumType &value)
|
static void read_value(query_result_reader &reader, const char *id, size_t index, EnumType &value)
|
||||||
|
|
@ -241,15 +241,13 @@ struct data_type_traits<EnumType, std::enable_if_t<std::is_enum_v<EnumType>>>
|
||||||
}
|
}
|
||||||
static void bind_value(parameter_binder &binder, size_t index, EnumType &value)
|
static void bind_value(parameter_binder &binder, size_t index, EnumType &value)
|
||||||
{
|
{
|
||||||
data_type_traits<int>::bind_value(binder, index, static_cast<int&>(value));
|
data_type_traits<int>::bind_value(binder, index, (int&)value);
|
||||||
}
|
}
|
||||||
static void bind_result_value(result_parameter_binder &binder, size_t index, EnumType &value)
|
static void bind_result_value(result_parameter_binder &binder, size_t index, EnumType &value)
|
||||||
{
|
{
|
||||||
data_type_traits<int>::bind_result_value(binder, index, static_cast<int&>(value));
|
data_type_traits<int>::bind_result_value(binder, index, (int&)value);
|
||||||
}
|
|
||||||
static any_type create_value(const EnumType &value) {
|
|
||||||
return static_cast<int>(value);
|
|
||||||
}
|
}
|
||||||
|
inline static any_type create_value(EnumType &value) { return (int)value; }
|
||||||
};
|
};
|
||||||
/// @endcond
|
/// @endcond
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -136,12 +136,7 @@ public:
|
||||||
void on_primary_key(const char *id, V &, typename std::enable_if<std::is_integral<V>::value && !std::is_same<bool, V>::value>::type* = 0)
|
void on_primary_key(const char *id, V &, typename std::enable_if<std::is_integral<V>::value && !std::is_same<bool, V>::value>::type* = 0)
|
||||||
{
|
{
|
||||||
push(id);
|
push(id);
|
||||||
if (!is_root_entity()) {
|
if (is_root_entity() && pk_.is_integer()) {
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (pk_.is_null()) {
|
|
||||||
entity_query_data_.pk_column_ = id;
|
|
||||||
} else if (pk_.is_integer()) {
|
|
||||||
entity_query_data_.where_clause = make_condition(column{table_info_stack_.top().name, id, ""} == *pk_.as<V>());
|
entity_query_data_.where_clause = make_condition(column{table_info_stack_.top().name, id, ""} == *pk_.as<V>());
|
||||||
entity_query_data_.pk_column_ = id;
|
entity_query_data_.pk_column_ = id;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -36,8 +36,6 @@ public:
|
||||||
|
|
||||||
[[nodiscard]] std::string name() const;
|
[[nodiscard]] std::string name() const;
|
||||||
|
|
||||||
void create(connection &c);
|
|
||||||
|
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
const table_info& attach(const std::string &table_name)
|
const table_info& attach(const std::string &table_name)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,5 @@
|
||||||
#include "matador/sql/condition.hpp"
|
#include "matador/sql/condition.hpp"
|
||||||
|
|
||||||
#include "matador/sql/query_intermediates.hpp"
|
|
||||||
|
|
||||||
namespace matador::sql {
|
namespace matador::sql {
|
||||||
|
|
||||||
condition<column, placeholder, std::enable_if<true>::type>::condition(const column &fld, basic_condition::operand_t op, const placeholder &val)
|
condition<column, placeholder, std::enable_if<true>::type>::condition(const column &fld, basic_condition::operand_t op, const placeholder &val)
|
||||||
|
|
@ -14,33 +12,17 @@ std::string condition<column, placeholder, std::enable_if<true>::type>::evaluate
|
||||||
return d.prepare_identifier(field_) + " " + operand + " " + d.next_placeholder(query.bind_vars);
|
return d.prepare_identifier(field_) + " " + operand + " " + d.next_placeholder(query.bind_vars);
|
||||||
}
|
}
|
||||||
|
|
||||||
condition<column, query_context>::condition(column col, basic_condition::operand_t op, const query_context &q)
|
condition<column, query_context>::condition(column col, basic_condition::operand_t op, query_context &q)
|
||||||
: basic_column_condition(std::move(col), op), query_(q)
|
: basic_column_condition(std::move(col), op), query_(q)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
std::string condition<column, query_context>::evaluate(const dialect &d, query_context &query) const
|
std::string condition<column, query_context>::evaluate(const dialect &d, query_context &query) const
|
||||||
{
|
{
|
||||||
std::string result(d.prepare_identifier(field_) + " " + operand + " (");
|
std::string result(d.prepare_identifier(field_) + " " + operand + " (");
|
||||||
result += query_.sql + (")");
|
result += (")");
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
condition<column, query_context> in( const column& col, const query_context &q ) {
|
|
||||||
return {col, basic_condition::operand_t::IN_LIST, q};
|
|
||||||
}
|
|
||||||
|
|
||||||
condition<column, query_context> in( const column& col, const query_select& q ) {
|
|
||||||
return in(col, q.build());
|
|
||||||
}
|
|
||||||
|
|
||||||
condition<column, std::string> like( const column& col, const std::string& val ) {
|
|
||||||
return {col, basic_condition::operand_t::LIKE, val};
|
|
||||||
}
|
|
||||||
|
|
||||||
condition<column, query_context> equals( const column& col, query_context& q ) {
|
|
||||||
return {col, basic_condition::operand_t::EQUAL, q};
|
|
||||||
}
|
|
||||||
|
|
||||||
condition<column, column> operator==(const column &a, const column &b)
|
condition<column, column> operator==(const column &a, const column &b)
|
||||||
{
|
{
|
||||||
return {a, basic_condition::operand_t::EQUAL, b};
|
return {a, basic_condition::operand_t::EQUAL, b};
|
||||||
|
|
|
||||||
|
|
@ -91,7 +91,7 @@ size_t connection::execute(const std::string &sql) const
|
||||||
return connection_->execute(sql);
|
return connection_->execute(sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
sql::query connection::query(const schema &schema) const
|
sql::query connection::query(const sql::schema &schema) const
|
||||||
{
|
{
|
||||||
return sql::query(*const_cast<connection*>(this), schema);
|
return sql::query(*const_cast<connection*>(this), schema);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -51,8 +51,8 @@ std::string dialect::prepare_literal(const std::string &str) const
|
||||||
|
|
||||||
void dialect::quote_identifier(std::string &str) const
|
void dialect::quote_identifier(std::string &str) const
|
||||||
{
|
{
|
||||||
// str.insert(0, token_at(token_t::START_QUOTE));
|
str.insert(0, token_at(token_t::START_QUOTE));
|
||||||
// str += token_at(token_t::END_QUOTE);
|
str += token_at(token_t::END_QUOTE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void dialect::escape_quotes_in_identifier(std::string &str) const
|
void dialect::escape_quotes_in_identifier(std::string &str) const
|
||||||
|
|
|
||||||
|
|
@ -54,7 +54,7 @@ void query_compiler::visit(query_select_part &select_part)
|
||||||
void query_compiler::visit(query_from_part &from_part)
|
void query_compiler::visit(query_from_part &from_part)
|
||||||
{
|
{
|
||||||
query_.table = from_part.table();
|
query_.table = from_part.table();
|
||||||
if (!dialect_.default_schema_name().empty()) {
|
if (dialect_.default_schema_name().empty()) {
|
||||||
query_.sql += " " + dialect_.token_at(dialect::token_t::FROM) +
|
query_.sql += " " + dialect_.token_at(dialect::token_t::FROM) +
|
||||||
" " + dialect_.prepare_identifier(from_part.table().name) +
|
" " + dialect_.prepare_identifier(from_part.table().name) +
|
||||||
(from_part.table().alias.empty() ? "" : " AS " +
|
(from_part.table().alias.empty() ? "" : " AS " +
|
||||||
|
|
|
||||||
|
|
@ -8,13 +8,6 @@ namespace matador::sql {
|
||||||
schema::schema(std::string name)
|
schema::schema(std::string name)
|
||||||
: name_(std::move(name)) {}
|
: name_(std::move(name)) {}
|
||||||
|
|
||||||
void schema::create(connection &c) {
|
|
||||||
for (const auto &ti : repository_) {
|
|
||||||
// ti.second.prototype
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string schema::name() const
|
std::string schema::name() const
|
||||||
{
|
{
|
||||||
return name_;
|
return name_;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue