On Thu, Oct 25, 2018 at 3:59 PM Philip Martin
<[email protected]> wrote:
>
> I am trying to create a function that finds the dependent tables used in a
> select-able statement. My plan is to use this function in conjunction with
> t.add_is_dependent_on inside a modified version of the recipe to create
> views. Hopefully, this allows sorted_tables to use these dependency
> relationships. The function definitely seems to work when the selectable only
> involves base tables, but breaks when a view uses another view. I think if I
> modify the function to recursively cycle through elements that are
> Selectables it should do the trick, but I was wondering if there is a public
> method like _from_objects / already a function to do something similar to
> this in the library?
right so you never need to dig into the private elements like
_from_objects for this kind of thing, there are traversal functions
that will give you whatever elements you need, the base of which is
sqlalchemy.sql.visitors.traverse. it has a plain iterator as well
which looks like:
from sqlalchemy.sql import visitors
for elem in visitors.iterate(some_statement, {}):
print(repr(elem))
most of the ORM internals use the traverse() function instead which is
given a set of functions that receive different types of elements,
like tables:
all_tables = set()
_visitors = {"table": all_tables.add}
visitors.traverse(statement, {'column_collections': False}, _visitors)
a whole lot of functions that use visitors can be seen in
sqlalchemy/sql/util.py so take a look in there, in particular take a
look at find_tables as well as surface_selectables.
>
>
> def find_selectable_dependents(selectable):
> dependents = set()
> for part in selectable.froms:
> for obj in part._from_objects:
> if hasattr(obj, 'element'):
> table_ = obj.element
> dependents.add(table_)
>
> return list(dependents)
>
> def view(name, metadata, selectable):
> t = table(name)
>
> for c in selectable.c:
> c._make_proxy(t)
>
> CreateView(name, selectable).execute_at('after-create', metadata)
> DropView(name).execute_at('before-drop', metadata)
>
>
> dependents = find_selectable_dependents(selectable)
> for dependent in dependents:
> t.add_is_dependent_on(dependent)
>
> return t
>
>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full
> description.
> ---
> 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 [email protected].
> To post to this group, send email to [email protected].
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
http://www.sqlalchemy.org/
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable
Example. See http://stackoverflow.com/help/mcve for a full description.
---
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.