#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/utils/access.hpp" #include "query_helper.hpp" #include #include 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); } }; struct book { unsigned long id{}; matador::sql::entity 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, "authors", 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, book_author, title, published_in) int main() { using namespace matador::sql; const std::string env_var{"MATADOR_BACKENDS_PATH"}; std::string dns{"sqlite://demo.db"}; // std::string dns{"memory://test"}; auto s = std::make_shared(); s->attach("authors"); s->attach("books"); connection c(dns, s); auto books = c.query() .select({matador::qh::books.id.name}) .from({"book"}) .join_left({"authors"}) .on("book.author_id"_col == "author.id"_col) .where("book.published_in"_col < 2008 && "author.name"_col == "Michael Crichton") .order_by("book.title").asc() .offset(2) .limit(5) .str(); // .fetch_all(); std::cout << "SQL: " << books << "\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 // 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"; // } return 0; }