query parts progress
This commit is contained in:
+91
-10
@@ -61,26 +61,97 @@ int main()
|
||||
|
||||
std::string dns{"sqlite://demo.db"};
|
||||
// std::string dns{"memory://test"};
|
||||
auto s = std::make_shared<schema>();
|
||||
auto s = std::make_shared<schema>("main");
|
||||
s->attach<author>("authors");
|
||||
s->attach<book>("books");
|
||||
|
||||
connection c(dns, s);
|
||||
connection c(dns);
|
||||
c.open();
|
||||
|
||||
auto books = c.query()
|
||||
.select<book>({qh::authors.first_name})
|
||||
auto create_authors_sql = c.query(s)
|
||||
.create()
|
||||
.table<author>(qh::authors)
|
||||
// .str();
|
||||
.execute();
|
||||
|
||||
c.query(s).create().table<book>(qh::books).execute();
|
||||
|
||||
std::cout << "SQL: " << create_authors_sql << "\n";
|
||||
|
||||
auto mc = make_entity<author>();
|
||||
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<author>(qh::authors)
|
||||
.values(*mc)
|
||||
.execute();
|
||||
// .str();
|
||||
|
||||
std::cout << "SQL: " << insert_authors_sql << "\n";
|
||||
|
||||
auto result = c.query(s)
|
||||
.select<author>()
|
||||
.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();
|
||||
// .str();
|
||||
|
||||
std::cout << "SQL: " << update_authors_sql << "\n";
|
||||
|
||||
auto authors = c.query(s)
|
||||
.select<author>()
|
||||
.from(qh::authors)
|
||||
.fetch_all<author>();
|
||||
|
||||
for (const auto &a : authors) {
|
||||
std::cout << "Author " << a.first_name << "\n";
|
||||
}
|
||||
|
||||
c.query(s)
|
||||
.insert()
|
||||
.into<book>(qh::books)
|
||||
.values({2, "It", mc->id, 1980})
|
||||
.execute();
|
||||
|
||||
c.query(s)
|
||||
.insert()
|
||||
.into<book>(qh::books)
|
||||
.values({3, "Misery", mc->id, 1984})
|
||||
.execute();
|
||||
|
||||
auto select_books_sql = c.query(s)
|
||||
.select<book>({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.first_name == "Michael Crichton")
|
||||
.where(qh::books.published_in < 2008 && qh::authors.last_name == "King")
|
||||
.group_by(qh::books.published_in)
|
||||
.order_by(qh::books.title).asc()
|
||||
.offset(2)
|
||||
.limit(5)
|
||||
.str();
|
||||
// .offset(2)
|
||||
// .limit(5)
|
||||
// .str();
|
||||
.fetch_all();
|
||||
// .fetch_all<book>();
|
||||
|
||||
std::cout << "SQL: " << books << "\n";
|
||||
// std::cout << "SQL: " << select_books_sql << "\n";
|
||||
|
||||
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
|
||||
@@ -88,7 +159,6 @@ 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());
|
||||
@@ -97,5 +167,16 @@ int main()
|
||||
// } 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";
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user