schema progress

This commit is contained in:
Sascha Kühl
2025-02-04 15:19:01 +01:00
parent 6a5974337d
commit f01e9ff87f
17 changed files with 416 additions and 276 deletions
+2 -1
View File
@@ -2,7 +2,8 @@
add_executable(demo main.cpp)
target_link_libraries(demo PRIVATE
matador
matador-core
matador-orm
${CMAKE_DL_LIBS}
${SQLite3_LIBRARIES}
)
+138 -151
View File
@@ -1,13 +1,18 @@
#include "matador/sql/column.hpp"
#include "matador/sql/condition.hpp"
#include "matador/sql/schema.hpp"
#include "matador/sql/connection.hpp"
#include "matador/sql/entity.hpp"
#include "matador/sql/query_macro.hpp"
#include "matador/query/condition.hpp"
#include "matador/object/object_ptr.hpp"
#include "matador/object/schema.hpp"
#include "matador/utils/access.hpp"
#include "matador/utils/default_type_traits.hpp"
#include "matador/utils/enum_mapper.hpp"
#include "matador/utils/types.hpp"
#include "matador/sql/query_helper.hpp"
#include "matador/sql/query_macro.hpp"
#include <iostream>
#include <string>
@@ -22,7 +27,7 @@ struct author {
template<typename Operator>
void process( Operator& op ) {
namespace field = matador::utils::access;
namespace field = matador::access;
field::primary_key( op, "id", id );
field::attribute( op, "first_name", first_name, 63 );
field::attribute( op, "last_name", last_name, 63 );
@@ -34,13 +39,13 @@ struct author {
struct book {
unsigned long id{};
matador::sql::entity<author> book_author;
matador::object::object_ptr<author> book_author;
std::string title;
unsigned short published_in{};
template<typename Operator>
void process( Operator& op ) {
namespace field = matador::utils::access;
namespace field = matador::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 );
@@ -53,7 +58,7 @@ struct payload {
template<typename Operator>
void process( Operator& op ) {
namespace field = matador::utils::access;
namespace field = matador::access;
field::primary_key( op, "id", id );
}
};
@@ -73,7 +78,7 @@ struct job {
};
unsigned long id{};
matador::sql::entity<payload> payload;
matador::object::object_ptr<payload> payload;
std::string type;
std::string description;
job_state state;
@@ -81,7 +86,7 @@ struct job {
template<typename Operator>
void process( Operator& op ) {
namespace field = matador::utils::access;
namespace field = matador::access;
field::primary_key( op, "id", id );
field::belongs_to( op, "payload", payload, matador::utils::default_foreign_attributes );
field::attribute( op, "type", type, 511 );
@@ -105,13 +110,10 @@ static const matador::utils::enum_mapper<job::job_mode> job_mode_enum({
});
template<>
struct matador::sql::data_type_traits<job::job_state, void>
{
inline static data_type_t builtin_type(std::size_t size)
{ return data_type_traits<std::string>::builtin_type(size); }
struct matador::utils::data_type_traits<job::job_state, void> {
static basic_type type(const std::size_t size) { return data_type_traits<std::string>::type(size); }
static void read_value(query_result_reader &reader, const char *id, size_t index, job::job_state &value)
{
static void read_value(attribute_reader &reader, const char *id, const size_t index, job::job_state &value) {
std::string enum_string;
reader.read_value(id, index, enum_string, 64);
if (const auto enum_opt = job_state_enum.to_enum(enum_string)) {
@@ -119,25 +121,16 @@ struct matador::sql::data_type_traits<job::job_state, void>
}
}
static any_type create_value(const job::job_state &value)
{
return job_state_enum.to_string(value);
}
static void bind_value(parameter_binder &binder, size_t index, job::job_state &value)
{
binder.bind(index, job_state_enum.to_string(value));
static void write_value(attribute_writer &binder, const size_t index, const job::job_state &value) {
binder.write_value(index, job_state_enum.to_string(value));
}
};
template<>
struct matador::sql::data_type_traits<job::job_mode, void>
{
inline static data_type_t builtin_type(std::size_t size)
{ return data_type_traits<std::string>::builtin_type(size); }
struct matador::utils::data_type_traits<job::job_mode, void> {
static basic_type type( const std::size_t size) { return data_type_traits<std::string>::type(size); }
static void read_value(query_result_reader &reader, const char *id, size_t index, job::job_mode &value)
{
static void read_value(attribute_reader &reader, const char *id, const size_t index, job::job_mode &value) {
std::string enum_string;
reader.read_value(id, index, enum_string, 64);
if (const auto enum_opt = job_mode_enum.to_enum(enum_string)) {
@@ -145,14 +138,8 @@ struct matador::sql::data_type_traits<job::job_mode, void>
}
}
static any_type create_value(const job::job_mode &value)
{
return job_mode_enum.to_string(value);
}
static void bind_value(parameter_binder &binder, size_t index, job::job_mode &value)
{
binder.bind(index, job_mode_enum.to_string(value));
static void bind_value(attribute_writer &binder, const size_t index, const job::job_mode &value) {
binder.write_value(index, job_mode_enum.to_string(value));
}
};
@@ -168,7 +155,7 @@ QUERY_HELPER( temporary_table, id );
int main() {
using namespace matador::sql;
using namespace matador;
using namespace matador::object;
const std::string env_var{"MATADOR_BACKENDS_PATH"};
@@ -178,118 +165,118 @@ int main() {
s.attach<book>( "books" );
connection c( dns );
c.open();
s.create( c );
auto create_authors_sql = c.query( s )
.create()
.table<author>( qh::authors )
.execute();
c.query( s )
.create()
.table<book>( qh::books )
.execute();
std::cout << "SQL: " << create_authors_sql << "\n";
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();
std::cout << "SQL: " << insert_authors_sql << "\n";
auto result = c.query( s )
.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 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::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";
auto result = c.open();
// s.create( c );
//
// auto create_authors_sql = c.query( s )
// .create()
// .table<author>( qh::authors )
// .execute();
//
// c.query( s )
// .create()
// .table<book>( qh::books )
// .execute();
//
// std::cout << "SQL: " << create_authors_sql << "\n";
//
// 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();
//
// std::cout << "SQL: " << insert_authors_sql << "\n";
//
// auto result = c.query( s )
// .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 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::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;
}