Is it possible to write a query in PonyORM's syntax that will be translated into a SQL query with a WITH RECURSIVE CTE clause? If yes, how?
I know I can write raw SQL queries with select_by_sql() and select(), but I specifically want to know if I must use raw SQL or if I can use PonyORM's pythonic syntax, for writing recursive queries.
Related
What is the most memory-efficient way to join a list into a string in MICROPYTHON?
the list object in MICROPYTHON doesn't have the 'join' function, so I would like to know if there's any memory-efficient way to do so
As with standard Python, join is a method of strings not of lists. See:
Python join: why is it string.join(list) instead of list.join(string)?
and/or
https://docs.python.org/3/faq/design.html#why-is-join-a-string-method-instead-of-a-list-or-tuple-method
I overlooked, there's indeed a join function from the str class to join the list
e.g.
",".join(["a","b","c"])
I'm going to use the same value in lots of statements in the SQL Expression. So it is possible to declare and assign the value to a variable at the beginning of the query and refer the value by it?
(I'm writing an execution plan in WSO2 DAS)
This is not supported as of now. However, supporting this has been under discussion, hence this might be implemented in a future release.
If you want to store a value and use it in a query, the currently available ways are:
Putting that value into an indexed event table and then doing a join with the event table to read that value whenever required.
Indexed In-memory Event Table internally uses a Hash-Map, therefore you could use one to store your variables, in such a way that the key of the hashmap will be the name of your varaible and the value of the hashmap will be the value of your variable.
However I feel that above solution is too complicated for your requirement.
Using the Map Extension in Siddhi
I have certain conditions in my application that I need to query for regularly (like all users that have signed up in the last 24 hours), and I want to store that query for later use. What's a good way to store the query itself, not the results of the query?
A few different ways I've thought of:
Hash that maps keys to symbols that I use as functions, each function is defined on the User object, and its implementation defines the query I want.
Hash that maps keys to raw SQL queries.
Global hash of singleton methods that stores each query.
So I have three models.. a Crag has one or more CragLocations and each CragLocation has a Location. I can query for a certain subset of crags using
public List<Crag> getCragsWithGridRef() {
/**
* we want to query select c.* from crag c join CragLocation cl on c.id
* = cl.cragId join Location l on cl.locationId = l.id where
* len(l.gridReference)>1
*/
TypedQuery<Crag> query =
em.createQuery(
"SELECT c FROM Crag c JOIN c.CragLocations cl JOIN cl.location l where LENGTH(l.gridReference) > 1",
Crag.class);
return query.getResultList();
}
I'm largely querying this way because my brain can't handle criteria queries. I struggle to parse the meaning when I'm looking at them.
So is there a performance or maintainability (or other) reason to prefer criteria queries and if so how would you express this query?
No, there's no reason to prefer criteria queries over JPQL ones, especially if you consider JPQL queries easy to understand and thus to maintain, and criteria queries hard to understand and maintain (which I agree with).
Criteria queries, if you use the auto-generated metamodel, are hard to write, but once written, you can be sure that there is no syntax error. That doesn't mean that the query does what it's supposed to do, though. So in any case, you should unit-test the queries. If you have unit test covering the queries, then use what you find the most readable and maintainable. Even if there was a performance difference generating the underlying SQL query, this difference would be negligible compared to the cost of actually executing the query.
I use Criteria queries only in those two situations (and not even always):
the query is dynamically composed from a set of optional search criteria
There are many similar queries sharing a common part, and I want to avoid repeating this common part in each and every query. Using a criteria allows putting the common parts in a reusable method.
I need to build a generic method in coldfusion to compare two query result sets... Any Ideas???
If you are looking to simply decide whether two queries are exactly alike, then you can do this:
if(serializeJSON(query1) eq serializeJSON(query2)) ...
This will convert both queries to strings and compare the strings.
If you're looking for more nuance, I believe Sergii's approach (convert to struct, compare keys) is probably the right approach. You could "guard" it by adding in simple checks first.... do the column lists match? Is the recordcount the same? That way, if either of those checks fail, you know that the queries can't possibly be equivalent and so it's safe to return false, thereby avoiding the performance hit of a full compare.
If I understand you correctly, you have two result sets with same structure but different datasets (like selecting with different clauses).
If this is correct, I believe that better (more efficient) way is to try to solve this task on the database level. Maybe with temporarily/cumulative tables and/or stored procedure.
Using CF it is almost definitely will need a ton of loops, which can be inappropriate for the large datasets. Though I did something like this for the small datasets using intermediate storage: converted one result set into the structure, and looped over the second query with checking the structure keys.