query compiler progress
This commit is contained in:
+69
-4
@@ -1,9 +1,74 @@
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#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>
|
||||
|
||||
int main() {
|
||||
const std::string env_var {"MATADOR_BACKENDS_PATH"};
|
||||
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{};
|
||||
|
||||
Reference in New Issue
Block a user