Kirill Simonov avatar Kirill Simonov committed 7ae8dd7

Added container interface to catalog objects.

Comments (0)

Files changed (15)

src/htsql/core/classify.py

 
     def __call__(self):
         catalog = introspect()
-        for schema in catalog.schemas:
-            for table in schema.tables:
+        for schema in catalog:
+            for table in schema:
                 yield TableArc(table)
 
 

src/htsql/core/entity.py

 
     __slots__ = ('schemas', '__weakref__')
 
+    def __contains__(self, name):
+        return (name in self.schemas)
+
+    def __getitem__(self, name):
+        return self.schemas[name]
+
+    def __iter__(self):
+        return iter(self.schemas)
+
+    def __len__(self):
+        return len(self.schemas)
+
+    def get(self, name, default=None):
+        return self.schemas.get(name, default)
+
 
 class MutableCatalogEntity(CatalogEntity, MutableEntity):
 
     def catalog(self):
         return self.owner()
 
+    def __contains__(self, name):
+        return (name in self.tables)
+
+    def __getitem__(self, name):
+        return self.tables[name]
+
+    def __iter__(self):
+        return iter(self.tables)
+
+    def __len__(self):
+        return len(self.tables)
+
+    def get(self, name, default=None):
+        return self.tables.get(name, default)
+
 
 class MutableSchemaEntity(SchemaEntity, MutableEntity):
 
             return self.name
         return u"%s.%s" % (self.schema, self.name)
 
+    def __contains__(self, name):
+        return (name in self.columns)
+
+    def __getitem__(self, name):
+        return self.columns[name]
+
+    def __iter__(self):
+        return iter(self.columns)
+
+    def __len__(self):
+        return len(self.columns)
+
+    def get(self, name, default=None):
+        return self.columns.get(name, default)
+
 
 class MutableTableEntity(TableEntity, MutableEntity):
 
                             u",".join(column.name
                                       for column in self.origin_columns))
 
+    def __contains__(self, column):
+        return (column in self.origin_columns)
+
+    def __getitem__(self, index):
+        return self.origin_columns[index]
+
+    def __iter__(self):
+        return iter(self.origin_columns)
+
+    def __len__(self):
+        return len(self.origin_columns)
+
 
 class MutableUniqueKeyEntity(UniqueKeyEntity, MutableEntity):
 
                    self.target,
                    u",".join(column.name for column in self.target_columns)))
 
+    def __contains__(self, column_pair):
+        return (column_pair in zip(self.origin_columns, self.target_columns))
+
+    def __getitem__(self, index):
+        return (self.origin_columns[index], self.target_columns[index])
+
+    def __iter__(self):
+        return iter(zip(self.origin_columns, self.target_columns))
+
+    def __len__(self):
+        return len(self.origin_columns)
+
 
 class MutableForeignKeyEntity(ForeignKeyEntity, MutableEntity):
 
     def __str__(self):
         return unicode(self).encode('utf-8')
 
+    def __contains__(self, column_pair):
+        return (column_pair in zip(self.origin_columns, self.target_columns))
+
+    def __getitem__(self, index):
+        return (self.origin_columns[index], self.target_columns[index])
+
+    def __iter__(self):
+        return iter(zip(self.origin_columns, self.target_columns))
+
+    def __len__(self):
+        return len(self.origin_columns)
+
 
 class DirectJoin(Join):
     """

src/htsql/core/introspect.py

     def __call__(self):
         catalog = super(IntrospectCleanup, self).__call__()
 
-        for schema in reversed(list(catalog.schemas)):
-            for table in reversed(list(schema.tables)):
+        for schema in reversed(list(catalog)):
+            for table in reversed(list(schema)):
                 if not table.columns:
                     table.remove()
-            if not schema.tables:
+            if not schema:
                 schema.remove()
 
-        for schema in catalog.schemas:
-            for table in schema.tables:
+        for schema in catalog:
+            for table in schema:
                 seen = {}
                 for unique_key in list(table.unique_keys):
                     key = tuple(unique_key.origin_columns)

src/htsql/core/tr/dump.py

             if table.schema.priority > 0:
                 need_schema = False
                 if any(schema.priority > table.schema.priority
-                       for schema in table.schema.catalog.schemas):
+                       for schema in table.schema.catalog):
                     need_schema = True
         # Serialize the table name.
         if need_schema:

src/htsql/tweak/etl/cmd/insert.py

                     extract_by_column[column] = extract
         columns = []
         extracts = []
-        for column in table.columns:
+        for column in table:
             if column in extract_by_column:
                 columns.append(column)
                 extracts.append(extract_by_column[column])

src/htsql/tweak/filedb/introspect.py

     def __call__(self):
         catalog = super(IntrospectFileDBCleanup, self).__call__()
         table_names = set(name for name, file in build_names())
-        for schema in catalog.schemas:
-            for table in list(schema.tables):
+        for schema in catalog:
+            for table in list(schema):
                 if table.name not in table_names:
                     table.remove()
         return catalog

src/htsql/tweak/override/introspect.py

             exclude = addon.excluded_tables
             unused.update(include)
             unused.update(exclude)
-            for schema in catalog.schemas:
+            for schema in catalog:
                 schema_exclude = [pattern
                                   for pattern in exclude
                                   if pattern.matches(schema)]
                 if not (include or schema_exclude):
                     continue
-                for table in reversed(list(schema.tables)):
+                for table in reversed(list(schema)):
                     include_matches = [pattern
                                        for pattern in include
                                        if pattern.matches(table)]
             exclude = addon.excluded_columns
             unused.update(include)
             unused.update(exclude)
-            for schema in catalog.schemas:
+            for schema in catalog:
                 schema_exclude = [pattern
                                   for pattern in exclude
                                   if pattern.matches(schema)]
                 if not (include or schema_exclude):
                     continue
-                for table in schema.tables:
+                for table in schema:
                     table_exclude = [pattern
                                      for pattern in schema_exclude
                                      if pattern.matches(table)]
                     if not (include or table_exclude):
                         continue
-                    for column in reversed(list(table.columns)):
+                    for column in reversed(list(table)):
                         include_matches = [pattern
                                            for pattern in include
                                            if pattern.matches(column)]
 
         if addon.not_nulls:
             unused.update(addon.not_nulls)
-            for schema in catalog.schemas:
+            for schema in catalog:
                 schema_patterns = [pattern
                                    for pattern in addon.not_nulls
                                    if pattern.matches(schema)]
                 if not schema_patterns:
                     continue
-                for table in schema.tables:
+                for table in schema:
                     table_patterns = [pattern
                                       for pattern in schema_patterns
                                       if pattern.matches(table)]
                     if not table_patterns:
                         continue
-                    for column in reversed(list(table.columns)):
+                    for column in reversed(list(table)):
                         matches = [pattern
                                    for pattern in table_patterns
                                    if pattern.matches(column)]
 
         if addon.unique_keys:
             unused.update(addon.unique_keys)
-            for schema in catalog.schemas:
+            for schema in catalog:
                 schema_keys = [pattern
                                for pattern in addon.unique_keys
                                if pattern.matches(schema)]
                 if not schema_keys:
                     continue
-                for table in schema.tables:
+                for table in schema:
                     table_keys = [pattern
                                   for pattern in schema_keys
                                   if pattern.matches(table)]
 
         if addon.foreign_keys:
             unused.update(addon.foreign_keys)
-            for schema in catalog.schemas:
+            for schema in catalog:
                 schema_keys = [pattern
                                for pattern in addon.foreign_keys
                                if pattern.matches(schema)]
                 if not schema_keys:
                     continue
-                for table in schema.tables:
+                for table in schema:
                     table_keys = [pattern
                                   for pattern in schema_keys
                                   if pattern.matches(table)]
                         if columns is None:
                             continue
                         targets = [target_table
-                                   for target_schema in catalog.schemas
+                                   for target_schema in catalog
                                    if pattern.matches_target(target_schema)
-                                   for target_table in target_schema.tables
+                                   for target_table in target_schema
                                    if pattern.matches_target(target_table)
                                    and pattern.extract_target(target_table)]
                         if len(targets) != 1:
 
         if addon.unlabeled_tables:
             unused.update(addon.unlabeled_tables)
-            for schema in catalog.schemas:
+            for schema in catalog:
                 schema_matches = [pattern
                                   for pattern in addon.unlabeled_tables
                                   if pattern.matches(schema)]
                 if not schema_matches:
                     continue
-                for table in schema.tables:
+                for table in schema:
                     matches = [pattern
                                for pattern in schema_matches
                                if pattern.matches(table)]
 
         if addon.unlabeled_columns:
             unused.update(addon.unlabeled_columns)
-            for schema in catalog.schemas:
+            for schema in catalog:
                 schema_matches = [pattern
                                   for pattern in addon.unlabeled_columns
                                   if pattern.matches(schema)]
                 if not schema_matches:
                     continue
-                for table in schema.tables:
+                for table in schema:
                     table_matches = [pattern
                                      for pattern in schema_matches
                                      if pattern.matches(table)]
                     if not table_matches:
                         continue
-                    for column in table.columns:
+                    for column in table:
                         matches = [pattern
                                    for pattern in table_matches
                                    if pattern.matches(column)]

src/htsql/tweak/override/pattern.py

         assert self.matches(table)
         columns = []
         for pattern in self.column_patterns:
-            matching = [column for column in table.columns
+            matching = [column for column in table
                                if matches(column, pattern)]
             if len(matching) != 1:
                 return
         assert self.matches(table)
         columns = []
         for pattern in self.column_patterns:
-            matching = [column for column in table.columns
+            matching = [column for column in table
                                if matches(column, pattern)]
             if len(matching) != 1:
                 return
         if self.target_column_patterns:
             columns = []
             for pattern in self.target_column_patterns:
-                matching = [column for column in table.columns
+                matching = [column for column in table
                                    if matches(column, pattern)]
                 if len(matching) != 1:
                     return
         if parameters is not None:
             return
         matched_column = None
-        for column in node.table.columns:
+        for column in node.table:
             if not matches(column, self.column_pattern):
                 continue
             if matched_column is not None:
             return
         catalog = introspect()
         matched_table = None
-        for schema in catalog.schemas:
+        for schema in catalog:
             if not matches(schema, self.schema_pattern):
                 continue
-            for table in schema.tables:
+            for table in schema:
                 if not matches(table, self.table_pattern):
                     continue
                 if matched_table is not None:

src/htsql/tweak/sqlalchemy/introspect.py

         for table_record in metadata.sorted_tables:
             schema_name = decode(table_record.schema,
                                  table_record.quote_schema)
-            if schema_name not in catalog.schemas:
+            if schema_name not in catalog:
                 catalog.add_schema(schema_name)
-            schema = catalog.schemas[schema_name]
+            schema = catalog[schema_name]
             name = decode(table_record.name, table_record.quote)
             table = schema.add_table(name)
 
         for table_record in metadata.sorted_tables:
             schema_name = decode(table_record.schema,
                                  table_record.quote_schema)
-            schema = catalog.schemas[schema_name]
+            schema = catalog[schema_name]
             name = decode(table_record.name, table_record.quote)
-            table = schema.tables[name]
+            table = schema[name]
 
             for key_record in table_record.constraints:
                 if isinstance(key_record, (PrimaryKeyConstraint,
                                            UniqueConstraint)):
                     names = [decode(column_record.name, column_record.quote)
                              for column_record in key_record.columns]
-                    if not all(name in table.columns for name in names):
+                    if not all(name in table for name in names):
                         continue
-                    columns = [table.columns[name] for name in names]
+                    columns = [table[name] for name in names]
                     is_primary = isinstance(key_record, PrimaryKeyConstraint)
                     table.add_unique_key(columns, is_primary)
                 elif isinstance(key_record, ForeignKeyConstraint):
                                       for column_record in key_record.columns]
                     names = [decode(column_record.name, column_record.quote)
                              for column_record in column_records]
-                    if not all(name in table.columns for name in names):
+                    if not all(name in table for name in names):
                         continue
-                    columns = [table.columns[name] for name in names]
+                    columns = [table[name] for name in names]
                     target_records = [element.column
                                       for element in key_record.elements]
                     target_table_record = target_records[0].table
                     target_schema_name = decode(target_table_record.schema,
                                             target_table_record.quote_schema)
-                    if target_schema_name not in catalog.schemas:
+                    if target_schema_name not in catalog:
                         continue
-                    target_schema = catalog.schemas[target_schema_name]
+                    target_schema = catalog[target_schema_name]
                     target_table_name = decode(target_table_record.name,
                                                target_table_record.quote)
-                    if target_table_name not in target_schema.tables:
+                    if target_table_name not in target_schema:
                         continue
-                    target_table = target_schema.tables[target_table_name]
+                    target_table = target_schema[target_table_name]
                     target_names = [decode(target_record.name,
                                            target_record.quote)
                                     for target_record in target_records]
-                    if not all(name in target_table.columns
+                    if not all(name in target_table
                                for name in target_names):
                         continue
-                    target_columns = [target_table.columns[name]
+                    target_columns = [target_table[name]
                                       for name in target_names]
                     table.add_foreign_key(columns, target_table, target_columns)
 

src/htsql_mssql/core/introspect.py

             WHERE principal_id = USER_ID()
         """)
         default_schema_name = cursor.fetchone()[0]
-        if default_schema_name in catalog.schemas:
-            catalog.schemas[default_schema_name].set_priority(1)
+        if default_schema_name in catalog:
+            catalog[default_schema_name].set_priority(1)
 
         table_by_id = {}
         cursor.execute("""

src/htsql_mysql/core/introspect.py

             SELECT DATABASE()
         """)
         database_name = cursor.fetchone()[0]
-        if database_name in catalog.schemas:
-            catalog.schemas[database_name].set_priority(1)
+        if database_name in catalog:
+            catalog[database_name].set_priority(1)
 
         cursor.execute("""
             SELECT t.table_schema, t.table_name
             ORDER BY 1, 2
         """)
         for row in cursor.fetchnamed():
-            if row.table_schema not in catalog.schemas:
+            if row.table_schema not in catalog:
                 continue
-            schema = catalog.schemas[row.table_schema]
+            schema = catalog[row.table_schema]
             schema.add_table(row.table_name)
 
         cursor.execute("""
             ORDER BY 1, 2, 3
         """)
         for row in cursor.fetchnamed():
-            if row.table_schema not in catalog.schemas:
+            if row.table_schema not in catalog:
                 continue
-            schema = catalog.schemas[row.table_schema]
-            if row.table_name not in schema.tables:
+            schema = catalog[row.table_schema]
+            if row.table_name not in schema:
                 continue
-            table = schema.tables[row.table_name]
+            table = schema[row.table_name]
             name = row.column_name
             is_nullable = (row.is_nullable == 'YES')
             has_default = (row.column_default is not None)
             if key not in usage_rows_by_constraint_key:
                 continue
             usage_rows = usage_rows_by_constraint_key[key]
-            if constraint_row.table_schema not in catalog.schemas:
+            if constraint_row.table_schema not in catalog:
                 continue
-            schema = catalog.schemas[constraint_row.table_schema]
-            if constraint_row.table_name not in schema.tables:
+            schema = catalog[constraint_row.table_schema]
+            if constraint_row.table_name not in schema:
                 continue
-            table = schema.tables[constraint_row.table_name]
+            table = schema[constraint_row.table_name]
             if not all(row.column_name in table.columns
                        for row in usage_rows):
                 continue
                 table.add_unique_key(columns, is_primary)
             elif constraint_row.constraint_type == 'FOREIGN KEY':
                 row = usage_rows[0]
-                if row.referenced_table_schema not in catalog.schemas:
+                if row.referenced_table_schema not in catalog:
                     continue
-                target_schema = catalog.schemas[row.referenced_table_schema]
-                if row.referenced_table_name not in target_schema.tables:
+                target_schema = catalog[row.referenced_table_schema]
+                if row.referenced_table_name not in target_schema:
                     continue
-                target_table = target_schema.tables[row.referenced_table_name]
+                target_table = target_schema[row.referenced_table_name]
                 if not all(row.referenced_column_name in target_table.columns
                            for row in usage_rows):
                     continue

src/htsql_oracle/core/introspect.py

             SELECT USER FROM DUAL
         """)
         current_user = cursor.fetchone()[0]
-        if current_user in catalog.schemas:
-            catalog.schemas[current_user].set_priority(1)
+        if current_user in catalog:
+            catalog[current_user].set_priority(1)
 
         cursor.execute("""
             SELECT owner, table_name
         for row in cursor.fetchnamed():
             if '$' in row.table_name:
                 continue
-            if row.owner not in catalog.schemas:
+            if row.owner not in catalog:
                 continue
-            schema = catalog.schemas[row.owner]
+            schema = catalog[row.owner]
             schema.add_table(row.table_name)
 
         cursor.execute("""
         for row in cursor.fetchnamed():
             if '$' in row.column_name:
                 continue
-            if row.owner not in catalog.schemas:
+            if row.owner not in catalog:
                 continue
-            schema = catalog.schemas[row.owner]
-            if row.table_name not in schema.tables:
+            schema = catalog[row.owner]
+            if row.table_name not in schema:
                 continue
-            table = schema.tables[row.table_name]
+            table = schema[row.table_name]
             name = row.column_name
             check = None
             check_key = (row.owner, row.table_name)
             if key not in column_rows_by_constraint:
                 continue
             column_rows = column_rows_by_constraint[key]
-            if row.owner not in catalog.schemas:
+            if row.owner not in catalog:
                 continue
-            schema = catalog.schemas[row.owner]
-            if row.table_name not in schema.tables:
+            schema = catalog[row.owner]
+            if row.table_name not in schema:
                 continue
-            table = schema.tables[row.table_name]
+            table = schema[row.table_name]
             if not all(column_row.column_name in table.columns
                        for column_row in column_rows):
                 continue
                     continue
                 target_row = constraint_row_by_constraint[target_key]
                 target_column_rows = column_rows_by_constraint[target_key]
-                if target_row.owner not in catalog.schemas:
+                if target_row.owner not in catalog:
                     continue
-                target_schema = catalog.schemas[target_row.owner]
-                if target_row.table_name not in target_schema.tables:
+                target_schema = catalog[target_row.owner]
+                if target_row.table_name not in target_schema:
                     continue
-                target_table = target_schema.tables[target_row.table_name]
+                target_table = target_schema[target_row.table_name]
                 if not all(column_row.column_name in target_table.columns
                            for column_row in target_column_rows):
                     continue

src/htsql_pgsql/core/introspect.py

         search_path = cursor.fetchone()[0]
         for idx, name in enumerate(search_path):
             priority = len(search_path)-idx
-            if name in catalog.schemas:
-                catalog.schemas[name].set_priority(priority)
+            if name in catalog:
+                catalog[name].set_priority(priority)
 
         table_by_oid = {}
         cursor.execute("""

src/htsql_pgsql/tweak/system/introspect.py

             schema_name = unicode(schema_name)
             table_name = unicode(table_name)
             column_names = map(unicode, column_names)
-            if schema_name not in catalog.schemas:
+            if schema_name not in catalog:
                 continue
-            schema = catalog.schemas[schema_name]
-            if table_name not in schema.tables:
+            schema = catalog[schema_name]
+            if table_name not in schema:
                 continue
-            table = schema.tables[table_name]
+            table = schema[table_name]
             if any(column_name not in table.columns
                    for column_name in column_names):
                 continue
             keys = self.foreign_keys[origin_schema_name, origin_table_name]
             origin_schema_name = unicode(origin_schema_name)
             origin_table_name = unicode(origin_table_name)
-            if origin_schema_name not in catalog.schemas:
+            if origin_schema_name not in catalog:
                 continue
-            origin_schema = catalog.schemas[schema_name]
-            if origin_table_name not in origin_schema.tables:
+            origin_schema = catalog[schema_name]
+            if origin_table_name not in origin_schema:
                 continue
-            origin = origin_schema.tables[origin_table_name]
+            origin = origin_schema[origin_table_name]
             for (origin_column_names, (target_schema_name, target_table_name),
                     target_column_names) in keys:
                 origin_column_names = map(unicode, origin_column_names)
                     continue
                 origin_columns = [origin.columns[column_name]
                                   for column_name in origin_column_names]
-                if target_schema_name not in catalog.schemas:
+                if target_schema_name not in catalog:
                     continue
-                target_schema = catalog.schemas[schema_name]
-                if target_table_name not in target_schema.tables:
+                target_schema = catalog[schema_name]
+                if target_table_name not in target_schema:
                     continue
-                target = target_schema.tables[target_table_name]
+                target = target_schema[target_table_name]
                 if any(column_name not in target.columns
                        for column_name in target_column_names):
                     continue

src/htsql_sqlite/core/introspect.py

         for row in cursor.fetchnamed():
             schema.add_table(row.name)
 
-        for table in schema.tables:
+        for table in schema:
             cursor.execute("""PRAGMA table_info(%s)"""
                            % self.escape_name(table.name))
             primary_key_columns = []
                 else:
                     table.add_primary_key(primary_key_columns)
 
-        for table in schema.tables:
+        for table in schema:
             cursor.execute("""PRAGMA index_list(%s)"""
                            % self.escape_name(table.name))
             for index_row in cursor.fetchnamed():
                                % self.escape_name(index_row.name))
                 columns = []
                 for row in cursor.fetchnamed():
-                    columns.append(table.columns[row.name])
+                    columns.append(table[row.name])
                 table.add_unique_key(columns)
 
-        for table in schema.tables:
+        for table in schema:
             ids = set()
             columns_by_id = {}
             target_by_id = {}
                     if (target_name.startswith(u'"') and
                             target_name.endswith(u'"')):
                         target_name = target_name[1:-1].replace(u'""', u'"')
-                    target_by_id[row.id] = schema.tables[target_name]
+                    target_by_id[row.id] = schema[target_name]
                     target_columns_by_id[row.id] = []
                 target = target_by_id[row.id]
-                column = table.columns[row.from_]
+                column = table[row.from_]
                 target_column = target.columns[row.to]
                 columns_by_id[row.id].append(column)
                 target_columns_by_id[row.id].append(target_column)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.