On 08/25/2016 03:17 PM, Dominik George wrote:
To be more precise:

I have a query like this one:

|
session.query(osmalchemy.node).filter(osmalchemy.node.tags.any(key="name",value="Grill-Corner")).all()
|

It ultimately compiles to this QL expression:

|
query =SELECT osm_nodes.element_id AS
osm_nodes_element_id,osm_elements.element_id AS
osm_elements_element_id,osm_elements.osmalchemy_updated AS
osm_elements_osmalchemy_updated,osm_elements.type AS
osm_elements_type,osm_elements.id AS
osm_elements_id,osm_elements.version AS
osm_elements_version,osm_elements.changeset AS
osm_elements_changeset,osm_elements.user AS
osm_elements_user,osm_elements.uid AS
osm_elements_uid,osm_elements.visible AS
osm_elements_visible,osm_elements.timestamp AS
osm_elements_timestamp,osm_nodes.latitude AS
osm_nodes_latitude,osm_nodes.longitude AS osm_nodes_longitude
FROM osm_elements JOIN osm_nodes ON osm_elements.element_id
=osm_nodes.element_id
WHERE EXISTS (SELECT 1
FROM osm_elements_tags
WHERE osm_elements.element_id =osm_elements_tags.element_id AND (EXISTS
(SELECT 1
FROM osm_tags
WHERE osm_tags.tag_id =osm_elements_tags.tag_id AND osm_tags.value =?AND
osm_tags."key"=?)))
|

I want to programmatically get to the last two comparisons, i.e. the
criterion of the .any() call in the initial query.

I am receiving this query in a before_compile trigger in a library using
SQLAlchemy.

so before_compile() does not receive a core Select, it receives the Query before anything has been done to it yet. However, any() generate a SELECT. So you need to look in both.

You can generically iterate through the elements of a Core element using get_children():


@event.listens_for(Query, "before_compile")
def show_any_filter(query):
    stack = [query._criterion]

    while stack:
        elem = stack.pop(-1)
        if isinstance(elem, sql.selectable.Select):
            print("WHERECLAUSE: %s" % elem._whereclause)
        else:
            stack.extend(elem.get_children())







-nik

--
You received this message because you are subscribed to the Google
Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to sqlalchemy+unsubscr...@googlegroups.com
<mailto:sqlalchemy+unsubscr...@googlegroups.com>.
To post to this group, send email to sqlalchemy@googlegroups.com
<mailto:sqlalchemy@googlegroups.com>.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to