-
Notifications
You must be signed in to change notification settings - Fork 76
Using queries
Hamed Masafi edited this page Dec 9, 2019
·
8 revisions
Calling method query on a TableSet return a Query class that can be used for query from database. For example:
auto q = db.posts().query(); // type of q is Query<Post>
Almost all methods of Query returns itself, so a complicate command can be written into single command. You can also create query in one command:
auto q = db.posts().query()
->where(Post::idField() == 1)
->toList();
Now, q contains Nut::RowList and can be used in code. query has other commands like: sum, avg, max, min and etc
auto post = db.posts().query()
->where(Post::idField() == 1)
->first();
if(post)
qDebug() << "Post found in database";
else
qDebug() << "No post found!";
auto posts = db.posts().query()
->where(Post::idField() == 1)
->orderBy(Post::idField())
->toList();
Also you can sort descending by adding ! to field name
auto posts = db.posts().query()
->where(Post::idField() == 1)
->orderBy(!Post::idField())
->toList();
auto ids = db.posts().query()
->select(Post::idField());
//ids is type of QList<int>
auto q = db.posts().query();
auto sum = q.sum(Post::idField());
auto max = q.max(Post::idField());
auto min = q.min(Post::idField());
auto count = q.count(Post::idField());
auto post = db.posts().query()
->where(Post::idField().in(QList<int>() << 1 << 2 << 3 << 4))
->toList();
auto post = db.posts().query()
->where(Post::idField().in({1, 2, 3, 4}))
->toList();
Table of contents