Skip to content

Latest commit

 

History

History
67 lines (61 loc) · 1.51 KB

query.md

File metadata and controls

67 lines (61 loc) · 1.51 KB

Creating query

auto q = db.posts().query();

You can also create query in one command:

auto result = db.posts().query()
    ->where(Post::idField() == 1)
    ->toList();

Now, result contains QList<QSharedPointer<Post>> and can be used in code. query has other commands like: sum, avg, max, min and etc

Getting first record in query

auto post = db.posts().query()
    ->setWhere(Post::idField() == 1)
    ->first();

if(post)
    qDebug() << "Post found in database";
else 
    qDebug() << "No post found!";

Sorting result

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();

Selecting single field

auto ids = db.posts().query()
    ->select(Post::idField());
//ids is type of QList<int>

Getting sum, count, min, max

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());

Checking field exists in list of values

auto post = db.posts().query()
    ->where(Post::idField().in(QList<int>() << 1 << 2 << 3 << 4) || Post::isAccepted())
    ->first();

Or

auto post = db.posts().query()
    ->where(Post::idField().in({1, 2, 3, 4}) || Post::isAccepted())
    ->first();