ignore null parameter in a jpql query - jpql

I want to make a filter with a query in jpql.
My problem is: if I have 4, 5 ,6 , etc parameters I want to ignore one or more parameter when i make the query. Why? because I want to turn on or off some filters.
Example
I want to see all users with the state "fired" so I want to make the query like this:
"select u from Users as u where u.state = :parameterState"
Where parameterState is my parameter that contains the string "fired".
So far so good, but I want to make the SAME query sending a null (with parameterState) to ignore the filter and show me all the users.
"select u from Users as u where u.state = :parameterState"
Now the parameterState have a null value and I need to ignore the "where u.state = :parameterState" to make this query:
"select u from Users as u"
I know that I can make switch-cases with all the posible ways to make the 2 querys, but I want to make a GENERIC WAY to invoque 1 unique query and solve all.
Note that if I want to make 2 filters (with 2 parameters) I need to make 4 different querys (in a Switch-case), if I need 4 filters (with 4 parameters ) there is 16 different ways to make the query.
Thats why I want to make a ONE generic call with ignore a NULL parameter, Is it posible?

Related

flux query: filter out all records related to one matching the condition

I'm trying to filter an influx DB query (using the nodeJS influxdb-client library).
As far as I can tell, it only works with "flux" queries.
I would like to filter out all records that share a specific attribute with any record that matches a particular condition. I'm filtering using the filter-function, but I'm not sure how I can continue from there. Is this possible in a single query?
My filter looks something like this:
|> filter(fn:(r) => r["_value"] == 1 and r["button"] == "1" ) and I would like to leave out all the record that have the same r["session"] as any that match this filter.
Do I need two queries; one to get those r["session"]s and one to filter on those, or is it possible in one?
Update:
Trying the two-step process. Got the list of r["session"]s into an array, and attempting to use the contains() flux function now to filter values included in that array called sessionsExclude.
Flux query section:
|> filter(fn:(r) => contains(value: r["session"], set: ${sessionsExclude}))
Getting an error unexpected token for property key: INT ("102")'. Not sure why. Looks like flux tries to turn the values into Integers? The r["session"] is also a String (and the example in the docs also uses an array of Strings)...
Ended up doing it in two queries. Still confused about the Strings vs Integers, but casting the value as an Int and printing out the array of r["session"] within the query seems to work like this:
'|> filter(fn:(r) => not contains(value: int(v: r["session"]), set: [${sessionsExclude.join(",")}]))'
Added the "not" to exclude instead of retain the values matching the array...

How to stuff a result of a query into a variable and use it another query in a logic app

I haven't used logic apps a lot, my boss is having trouble stuffing the results of one query into a variable and then using that variable in another query.
Basically, all he wants to do is get a list of of Id's returned from the first query and use that list in the second.
Here is a picture of what his logic app looks like:
You can see at the end of the second query he wants to check if the id is in the list or not. He's out for the day and I'm not sure if that variable is even receiving the list of id's successfully, but is there anything from the picture that you can tell that needs to be corrected? Or any suggestions that he could try, to achieve what he's trying to achieve?
According to the image, no data is getting stored into the variable AppId. While in the query you can just directly use c.EntityId. Below query to check if c.id is present in c.EntityId.
SELECT c.Vechicle.GrossVechicleWeight as GVW, c.EntityId as ApplicationId FROM c where c.RiskTypeId = 1 and c.Discriminator = 'RiskEntity' and c.EntityTypeId = 4500 and c.id in (c.EntityId)
Consider if you are trying to store c.Entity into AppId variable then you can Query SELECT c.EntityId FROM c and then store the result into the variable using Append to array variable action by extracting only c.EntityId using Parse JSON.
Here is my logic app
RESULT:

Codeigniter 4 multiple database tables in 1 model file

I have 1 database and inside there are maybe say 3 tables, users, user_profile, user_stats. I would like to ask is it possible to have these 3 tables in 1 model file? If user were to visit User A profile, it will show all the User A records from these 3 tables. I can't find it anywhere whether is it possible in CI4. Hope someone here can guide me on this. Thanks in advance guys!
You will need to override the find method. I have done it for findAll and haven't tried for find a method. So you may need to improvise on the following logic. There are three places where based on the condition we are fetching the rows, one check for the array, another for a single row, and last is the default one. So you will need to modify this behavior.
Replace :
$row = $builder
With your custom builder query such as following:
$row = $builder->select("users.*");
$builder->join('user_profile', 'user_profile.id = users.id','inner');
$builder->join('user_stats', 'user_stats.id = users.id','inner');
->whereIn($this->table . '.' . $this->primaryKey, $id)
->get();
You will need to replace the last query according to the conditions in find method and also instead of mentioning users explicitly you should use $this->table and similar for the primary key instead of id, you should use $this->primaryKey.

NodeJS - azure-storage-node- , how to retrieve addition of two columns, and apply filtering condition

Sorry for being newbie for NodeJs and table query, my question's,
How I could create a query using Nodejs pakcage "azure-storage-node", which selects the sum/addition of two coloumns 'start' and 'period' , if the addition is greater than a threshold it will take the whole raw, my tries which didn't work is something like this,
var query = new azure.TableQuery();
total = query.select(['start']) + query.select(['period']);
query.where('total > ?' , 50000);
or may be something like this,
var query = new azure.TableQuery()
.where('start + period gt 50000');
but it throws an error of '+'.
Thanks
What you're trying to accomplish is not possible with Azure Tables at least as of today as Azure Tables has limited querying support and support for computed columns (if I may say so) is not there.
There are two possible solutions:
Have an attribute called total in your entities that will contain the value i.e. start + period. You calculate this value when you're inserting or updating the entity and store it at that time.
Do this filtering on the client side. For this you will need to download all related entities and then apply this filtering on the client side on the data that you fetched.

Select only one row in dql subquery

I have to execute following query:
create dm_myobject object
set my_id_attribute = (select r_object_id from dm_otherobject where <some clause here>)
where ...
But subquery in brackets returns more than one id. I can't make whereclause more detailed to retrieve only one value.
How to take first?
ENABLE(FETCH_ALL_RESULTS 1) or ENABLE(RETURN_TOP 1) doesn't help.
In my experience it is impossible to use DQL hints in a sub query like you suggested, because the hint is applied to the query as a whole. It is indeed possible to use, say, ENABLE(RETURN_TOP 1) on a query that contains a sub query, however that hint will then be used on the outer query and never on the inner one. In your case, however, you'll end up with an error message telling that the sub query returns more than one result.
Try using an aggregate function on the selected attribute instead:
CREATE dm_myobject OBJECT
SET my_id_attribute = (
SELECT MIN(r_object_id)
FROM dm_otherobject
WHERE <some clause>
)
The MIN and MAX functions work with ints and strings, and I suspect they work with IDs too. Since it is ok for you to set only the first ID that's returned from your sub query, I suspect you're returning them in a sorted order and want to use the first -- hence the usage of the MIN function.
An alternative approach would of course be to write a script or a small Java program that executes several DQL statements, but that might or might not work for you in your case.

Resources