query/demo/main.cpp

83 lines
2.2 KiB
C++

#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 <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};
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{};
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::attribute(op, "published_in", published_in);
}
};
int main()
{
using namespace matador::sql;
const std::string env_var{"MATADOR_BACKENDS_PATH"};
std::string dns;
auto s = std::make_shared<schema>();
connection c(dns, s);
auto books = c.query()
.select<book>({"author.name"})
.from("book")
.join_left("author")
.on("book.author_id"_col == "author.id"_col)
.where("book.published_in"_col < 2008 && "author.name"_col == "Michael Crichton")
.order_by("book.title").asc()
.fetch_all<book>();
// 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
// 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;
}