progress on in conditions with queries and enums

This commit is contained in:
Sascha Kühl
2024-08-21 15:35:29 +02:00
parent ac8b2ed4e4
commit 12e5b27a8c
12 changed files with 305 additions and 186 deletions
+187 -128
View File
@@ -11,160 +11,219 @@
#include <iostream>
#include <string>
struct author
{
unsigned long id{};
std::string first_name;
std::string last_name;
std::string date_of_birth;
unsigned short year_of_birth{};
bool distinguished{false};
struct author {
unsigned long id{};
std::string first_name;
std::string last_name;
std::string date_of_birth;
unsigned short year_of_birth{};
bool distinguished{false};
template<typename Operator>
void process(Operator &op)
{
namespace field = matador::utils::access;
field::primary_key(op, "id", id);
field::attribute(op, "first_name", first_name, 63);
field::attribute(op, "last_name", last_name, 63);
field::attribute(op, "date_of_birth", date_of_birth, 31);
field::attribute(op, "year_of_birth", year_of_birth);
field::attribute(op, "distinguished", distinguished);
}
template<typename Operator>
void process( Operator& op ) {
namespace field = matador::utils::access;
field::primary_key( op, "id", id );
field::attribute( op, "first_name", first_name, 63 );
field::attribute( op, "last_name", last_name, 63 );
field::attribute( op, "date_of_birth", date_of_birth, 31 );
field::attribute( op, "year_of_birth", year_of_birth );
field::attribute( op, "distinguished", distinguished );
}
};
struct book
{
unsigned long id{};
matador::sql::entity<author> book_author;
std::string title;
unsigned short published_in{};
struct book {
unsigned long id{};
matador::sql::entity<author> book_author;
std::string title;
unsigned short published_in{};
template<typename Operator>
void process(Operator &op)
{
namespace field = matador::utils::access;
field::primary_key(op, "id", id);
field::attribute(op, "title", title, 511);
field::has_one(op, "author_id", book_author, matador::utils::default_foreign_attributes);
field::attribute(op, "published_in", published_in);
}
template<typename Operator>
void process( Operator& op ) {
namespace field = matador::utils::access;
field::primary_key( op, "id", id );
field::attribute( op, "title", title, 511 );
field::has_one( op, "author_id", book_author, matador::utils::default_foreign_attributes );
field::attribute( op, "published_in", published_in );
}
};
QUERY_HELPER(authors, id, first_name, last_name, date_of_birth, year_of_birth, distinguished)
QUERY_HELPER(books, id, author_id, title, published_in)
struct payload {
unsigned long id{};
int main()
{
using namespace matador::sql;
using namespace matador;
template<typename Operator>
void process( Operator& op ) {
namespace field = matador::utils::access;
field::primary_key( op, "id", id );
}
};
const std::string env_var{"MATADOR_BACKENDS_PATH"};
struct job {
enum class job_state {
Pending,
Running,
Succeeded,
Failed,
Canceled
};
std::string dns{"sqlite://demo.db"};
schema s("main");
s.attach<author>("authors");
s.attach<book>("books");
enum class job_mode {
Foreground,
Background
};
connection c(dns);
c.open();
unsigned long id{};
matador::sql::entity<payload> payload;
std::string type;
std::string description;
job_state state;
job_mode mode;
auto create_authors_sql = c.query(s)
.create()
.table<author>(qh::authors)
.execute();
template<typename Operator>
void process( Operator& op ) {
namespace field = matador::utils::access;
field::primary_key( op, "id", id );
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 );
}
};
c.query(s)
.create()
.table<book>(qh::books)
.execute();
QUERY_HELPER( authors, id, first_name, last_name, date_of_birth, year_of_birth, distinguished )
std::cout << "SQL: " << create_authors_sql << "\n";
QUERY_HELPER( books, id, author_id, title, published_in )
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( job, id, payload, type, description, state, mode )
std::cout << "SQL: " << insert_authors_sql << "\n";
QUERY_HELPER( payload, id )
auto result = c.query(s)
.select(qh::authors.columns)
.from(qh::authors)
.fetch_all();
QUERY_HELPER( temporary_table, id );
for (const auto &row: result) {
std::cout << "Author " << row.at(qh::authors.first_name) << "\n";
}
int main() {
using namespace matador::sql;
using namespace matador;
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();
const std::string env_var{"MATADOR_BACKENDS_PATH"};
std::cout << "SQL: " << update_authors_sql << "\n";
std::string dns{"sqlite://demo.db"};
schema s( "main" );
s.attach<author>( "authors" );
s.attach<book>( "books" );
auto authors = c.query(s)
.select(qh::authors.columns)
.from(qh::authors)
.fetch_all<author>();
connection c( dns );
c.open();
s.create( c );
for (const auto &a: authors) {
std::cout << "Author " << a.first_name << "\n";
}
auto create_authors_sql = c.query( s )
.create()
.table<author>( qh::authors )
.execute();
c.query(s)
.insert()
.into(qh::books)
.values({2, "It", mc.id, 1980})
.execute();
c.query( s )
.create()
.table<book>( qh::books )
.execute();
c.query(s)
.insert()
.into(qh::books)
.values({3, "Misery", mc.id, 1984})
.execute();
std::cout << "SQL: " << create_authors_sql << "\n";
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();
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();
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
std::cout << "SQL: " << insert_authors_sql << "\n";
c.query(s).drop().table(qh::books).execute();
auto result = c.query( s )
.select( qh::authors.columns )
.from( qh::authors )
.fetch_all();
auto drop_authors_sql = c.query(s)
.drop()
.table(qh::authors)
.execute();
for (const auto& row: result) { std::cout << "Author " << row.at( qh::authors.first_name ) << "\n"; }
std::cout << "SQL: " << drop_authors_sql << "\n";
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();
return 0;
}
std::cout << "SQL: " << update_authors_sql << "\n";
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;
}