GithubHelp home page GithubHelp logo

Programmatic ConditionalPhrase about nut HOT 6 CLOSED

hamedmasafi avatar hamedmasafi commented on September 18, 2024
Programmatic ConditionalPhrase

from nut.

Comments (6)

HamedMasafi avatar HamedMasafi commented on September 18, 2024

Hi, there is an example for you:

auto q = _db.tbl.query();
if (some_candidate)
   q = q->where( Table::fieldField() == value);
if (some_other_candidate)
   q = q->where( Table::field2Field() == value2);

//do somthing with q->toList();

If this does not solve your problem let me know

from nut.

yusufmk avatar yusufmk commented on September 18, 2024

Thank you for the reply !
YES. I found this on my own when I was looking for a solution. I figured the difference between setWhere and where functions.
BUT "where" function always use "&&" operator while combining phrases. However, it would be better if I had a way to create more flexible programmatic queries. Here is an example:

The user might want to search for entries that match:
SELECT * FROM TBL WHERE ID=1234 OR ID=6789;

currently I can only combine with AND operator:
SELECT * FROM TBL WHERE ID = 1234 AND ID = 6789;

which doesnt make sense in this case. Because it is impossible.

Also the library should allow me to combine queries like below:

  1. select * from tbl where ID=2703828 or ID=8080912 and TestDate > "2020-10-16 22:59:00";
  2. select * from tblReports where (ID=2703828 or ID=8080912) and TestDate > "2020-10-16 22:59:00";

These two queries return different results because 2nd one contains parentheses.

Currently I am generating the query so that it only contains AND conditions. Then I filter results in returned QList once again. It is ok in small db. But im afraid it would be very slow on large db.

Looking to hear from you. Regards...

from nut.

HamedMasafi avatar HamedMasafi commented on September 18, 2024

Ok, you can create ConditionalPhrase separately, eg:

auto p = Table::nameField() == "1";
p ||= Table::lastNameField() == "2";
auto list = _db.tbl.query()->where(p)->toList();

from nut.

yusufmk avatar yusufmk commented on September 18, 2024

This doesn't compile on my code. I'm using Nut version you used in OrmTest repo:
Nut commit hash: Commit 6ce50e2 by Jack Lilhammers, 07/07/2020 05:50 PM
Serializer commit hash: Commit b0194da3 by Miklós Márton, 06/13/2020 12:00 AM

my code is:
ConditionalPhrase cp;
cp ||= Report::patientIdField() == id;

error is: expected primary-expression before ‘=’ token
cp ||= (Report::patientIdField() == id);
^

Maybe you didn't implemented this operator overload in this commit yet? If there is stable version with better features, can you guide me how to use it? For example, can you update OrmTest repo such that it uses updated version of Nut? OrmTest repo helped me a lot to understand how to use this lib.

from nut.

HamedMasafi avatar HamedMasafi commented on September 18, 2024

Hi @yusufmk
I've tested and that is my mistake to write code without test in comment!
please change it to:

auto p = Table::nameField() == "1";
p = p || Table::lastNameField() == "2";
auto list = _db.tbl.query()->where(p)->toList();

None: New version of nut currently is under development in dev branch is a Qt module, please continue with it, It will be merged into master branch soon.

from nut.

yusufmk avatar yusufmk commented on September 18, 2024

!!! SOLVED !!!
Thank you! This works.

from nut.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.