query progress

This commit is contained in:
2024-02-28 18:02:12 +01:00
parent b9709d14c2
commit 7e1713ddd3
25 changed files with 405 additions and 221 deletions
+16 -28
View File
@@ -6,7 +6,7 @@
#include "matador/utils/access.hpp"
#include "query_helper.hpp"
#include "matador/sql/query_helper.hpp"
#include <iostream>
#include <string>
@@ -20,8 +20,9 @@ struct author
unsigned short year_of_birth{};
bool distinguished{false};
template < typename Operator >
void process(Operator &op) {
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);
@@ -39,8 +40,9 @@ struct book
std::string title;
unsigned short published_in{};
template < typename Operator >
void process(Operator &op) {
template<typename Operator>
void process(Operator &op)
{
namespace field = matador::utils::access;
field::primary_key(op, "id", id);
field::attribute(op, "title", title, 511);
@@ -60,7 +62,6 @@ int main()
const std::string env_var{"MATADOR_BACKENDS_PATH"};
std::string dns{"sqlite://demo.db"};
// std::string dns{"memory://test"};
auto s = std::make_shared<schema>("main");
s->attach<author>("authors");
s->attach<book>("books");
@@ -71,10 +72,12 @@ int main()
auto create_authors_sql = c.query(s)
.create()
.table<author>(qh::authors)
// .str();
.execute();
c.query(s).create().table<book>(qh::books).execute();
c.query(s)
.create()
.table<book>(qh::books)
.execute();
std::cout << "SQL: " << create_authors_sql << "\n";
@@ -90,7 +93,6 @@ int main()
.into<author>(qh::authors)
.values(*mc)
.execute();
// .str();
std::cout << "SQL: " << insert_authors_sql << "\n";
@@ -99,16 +101,16 @@ int main()
.from(qh::authors)
.fetch_all();
for (const auto &row : result) {
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"}})
.set({{qh::authors.first_name, "Stephen"},
{qh::authors.last_name, "King"}})
.where(qh::authors.last_name == "Crichton")
.execute();
// .str();
std::cout << "SQL: " << update_authors_sql << "\n";
@@ -117,7 +119,7 @@ int main()
.from(qh::authors)
.fetch_all<author>();
for (const auto &a : authors) {
for (const auto &a: authors) {
std::cout << "Author " << a.first_name << "\n";
}
@@ -143,13 +145,9 @@ int main()
.order_by(qh::books.title).asc()
.limit(5)
.offset(2)
// .str();
.fetch_all();
// .fetch_all<book>();
// std::cout << "SQL: " << select_books_sql << "\n";
for (const auto &r : select_books_sql) {
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
@@ -159,21 +157,11 @@ int main()
// ORDER BY "book.title" ASC
// OFFSET 2 LIMIT 5
// char var[1024];
// size_t len{};
// const auto error = getenv_s(&len, var, 1024, env_var.c_str());
// if (error > 0) {
// std::cout << "error: unknown env var " << env_var << "\n";
// } else {
// std::cout << "env var: " << var << "\n";
// }
c.query(s).drop().table(qh::books).execute();
auto drop_authors_sql = c.query(s)
.drop()
.table(qh::authors)
// .str();
.execute();
std::cout << "SQL: " << drop_authors_sql << "\n";
-24
View File
@@ -1,24 +0,0 @@
#ifndef QUERY_QUERY_HELPER_HPP
#define QUERY_QUERY_HELPER_HPP
#include "matador/utils/macro_map.hpp"
#include "matador/sql/table.hpp"
#include "matador/sql/column.hpp"
#include <string>
#include <ostream>
#define FIELD(x) const sql::column x{this->name, #x, ""};
#define QUERY_HELPER(C, ...) \
namespace matador::qh { \
namespace internal { \
struct C##_query : sql::table { \
C##_query() : table(#C) {} \
MAP(FIELD, __VA_ARGS__) \
}; } \
static const internal:: C##_query C; \
}
#endif //QUERY_QUERY_HELPER_HPP