summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/sql_util.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2006-05-25 14:20:23 +0000
committerMike Bayer <mike_mp@zzzcomputing.com>2006-05-25 14:20:23 +0000
commitbb79e2e871d0a4585164c1a6ed626d96d0231975 (patch)
tree6d457ba6c36c408b45db24ec3c29e147fe7504ff /lib/sqlalchemy/sql_util.py
parent4fc3a0648699c2b441251ba4e1d37a9107bd1986 (diff)
downloadsqlalchemy-bb79e2e871d0a4585164c1a6ed626d96d0231975.tar.gz
merged 0.2 branch into trunk; 0.1 now in sqlalchemy/branches/rel_0_1
Diffstat (limited to 'lib/sqlalchemy/sql_util.py')
-rw-r--r--lib/sqlalchemy/sql_util.py59
1 files changed, 59 insertions, 0 deletions
diff --git a/lib/sqlalchemy/sql_util.py b/lib/sqlalchemy/sql_util.py
new file mode 100644
index 000000000..e3170ebce
--- /dev/null
+++ b/lib/sqlalchemy/sql_util.py
@@ -0,0 +1,59 @@
+import sqlalchemy.sql as sql
+import sqlalchemy.schema as schema
+
+"""utility functions that build upon SQL and Schema constructs"""
+
+
+class TableCollection(object):
+ def __init__(self):
+ self.tables = []
+ def add(self, table):
+ self.tables.append(table)
+ def sort(self, reverse=False ):
+ import sqlalchemy.orm.topological
+ tuples = []
+ class TVisitor(schema.SchemaVisitor):
+ def visit_foreign_key(self, fkey):
+ parent_table = fkey.column.table
+ child_table = fkey.parent.table
+ tuples.append( ( parent_table, child_table ) )
+ vis = TVisitor()
+ for table in self.tables:
+ table.accept_schema_visitor(vis)
+ sorter = sqlalchemy.orm.topological.QueueDependencySorter( tuples, self.tables )
+ head = sorter.sort()
+ sequence = []
+ def to_sequence( node, seq=sequence):
+ seq.append( node.item )
+ for child in node.children:
+ to_sequence( child )
+ to_sequence( head )
+ if reverse:
+ sequence.reverse()
+ return sequence
+
+
+class TableFinder(TableCollection, sql.ClauseVisitor):
+ """given a Clause, locates all the Tables within it into a list."""
+ def __init__(self, table, check_columns=False):
+ TableCollection.__init__(self)
+ self.check_columns = check_columns
+ if table is not None:
+ table.accept_visitor(self)
+ def visit_table(self, table):
+ self.tables.append(table)
+ def __len__(self):
+ return len(self.tables)
+ def __getitem__(self, i):
+ return self.tables[i]
+ def __iter__(self):
+ return iter(self.tables)
+ def __contains__(self, obj):
+ return obj in self.tables
+ def __add__(self, obj):
+ return self.tables + list(obj)
+ def visit_column(self, column):
+ if self.check_columns:
+ column.table.accept_visitor(self)
+
+ \ No newline at end of file