-
Notifications
You must be signed in to change notification settings - Fork 115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
QxSqlQuery: User defined columns #79
Comments
Hello, You can define your own SQL columns aliases, this is documented here : Example :
Then, in your WHERE condition, just use your custom alias. |
Hi, sorry for the delay, I was and am a little puzzled by what you suggested, possibly because i am not using relationships here at all so i don't really see how to "declare" a column alias (which is not really a simple alias, but more an operation between two, or more, columns) just for a custom query. Can you transpose your suggestion within the code I originally posted, using QxSqlQuery? |
Yes sorry, the aliases are just to force a table alias name for a relation table (for the JOINS), and you can then use your own alias in your SQL query. But in your case, I think I don't understand yet what is your problem exactly.
|
Hi, sorry for the confusion here. My goal is to use the RDBMS to perform filtering operations, hence the WHERE clause, to retrieve just some specific objects. If you look at my original piece of code, I am trying to use 2 columns ("start_timestamp + duration") in a arithmetic sum and compare the result in the where clause against an arbitrary value, in this case coming from a variable (relative_start). The expected SQL PS shall look like this in this case: You don't need to have any alias declared nor to retrieve the on-the-fly column from the database, hence the * works just fine. The sum will be performed within the query executor and discarded after preparing the result set. QxOrm shall "understand" that a given column (operand I'd say) might be more complex than a single column name and treat it internally as a whole (virtual? temporary?) column for the sake of binding the arguments instead of attempting to use the (first?) natural column appearing in the query creation ("start_timestamp + duration"). Point is: the nice part of SQL is to be able to request the DB to do arbitrary/onetime math (not just JOINS) sometimes before returning any data for the sake of filtering/ordering, possibly disregarding the math itself (not part of the selected columns). If I were to use such computations client side afterwards, I'd then use something like this: Let me know if I was unclear in any way, thanks. |
Here you have a use case, the commented code does not work, the following does the same (more or less, not sure anymore, dev ongoing :) and works as expected!
|
Hello, I think the way to do that is to use this method : This method supports placeholders, and as you can see, you can pass the values in the second parameter : const QVariantList & values = QVariantList().
|
Hi thanks for the prompt answer, appreciated! I guess this can be a workaround although it feels a little like an hack, in between the two previously proposed versions. Using the freeText method we leave the predictable/standard way of building queries leading to a sort of mix of techniques (PLAIN SQL vs QueryBuilder). Readability is slightly affected too as it requires more grammar. I suggest to consider this as a feature request for a next version of QxOrm. |
Hi,
when building queries with QxSqlQuery how can I use custom/on-the-fly columns?
QxOrm generated prepared statement SQL is wrong when not using just real column names, as in:
This leads to the following error:
It seems QxOrm fails to bind the argument start_timestamp and fails to use the whole statement as a single column.
The text was updated successfully, but these errors were encountered: