def foreignkeys(self): """ Yields (parent_schema, parent_table, parent_column, child_schema, child_table, child_column) typles""" query = mssqlqueries.get_foreignkeys() logger.info(u'Foreign keys query: {0}'.format(query)) for tabular_result in self.execute_single_batch_query(query): for row in tabular_result[0]: yield ForeignKey(*row)
def get_completer(self, settings=None, casing=None): metadata = self.metadata from mssqlcli.mssqlcompleter import MssqlCompleter comp = MssqlCompleter(smart_completion=True, settings=settings) schemas, tables, tbl_cols, views, view_cols = [], [], [], [], [] for sch, tbls in metadata['tables'].items(): schemas.append(sch) for tbl, cols in tbls.items(): tables.append((sch, tbl)) # Let all columns be text columns tbl_cols.extend( [self._make_col(sch, tbl, col) for col in cols]) for sch, tbls in metadata.get('views', {}).items(): for tbl, cols in tbls.items(): views.append((sch, tbl)) # Let all columns be text columns view_cols.extend( [self._make_col(sch, tbl, col) for col in cols]) functions = [ FunctionMetadata(sch, *func_meta, arg_defaults=None) for sch, funcs in metadata['functions'].items() for func_meta in funcs ] datatypes = [(sch, typ) for sch, datatypes in metadata['datatypes'].items() for typ in datatypes] foreignkeys = [ ForeignKey(*fk) for fks in metadata['foreignkeys'].values() for fk in fks ] comp.extend_schemas(schemas) comp.extend_relations(tables, kind='tables') comp.extend_relations(views, kind='views') comp.extend_columns(tbl_cols, kind='tables') comp.extend_columns(view_cols, kind='views') comp.extend_functions(functions) comp.extend_datatypes(datatypes) comp.extend_foreignkeys(foreignkeys) comp.set_search_path(['public']) comp.extend_casing(casing or []) return comp
def extend_foreignkeys(self, fk_data): # fk_data is a list of ForeignKey namedtuples, with fields # parentschema, childschema, parenttable, childtable, # parentcolumns, childcolumns # These are added as a list of ForeignKey namedtuples to the # ColumnMetadata namedtuple for both the child and parent meta = self.dbmetadata['tables'] for fk in fk_data: e = self.escaped_names parentschema, childschema = e([fk.parentschema, fk.childschema]) parenttable, childtable = e([fk.parenttable, fk.childtable]) childcol, parcol = e([fk.childcolumn, fk.parentcolumn]) childcolmeta = meta[childschema][childtable][childcol] parcolmeta = meta[parentschema][parenttable][parcol] fk = ForeignKey(parentschema, parenttable, parcol, childschema, childtable, childcol) childcolmeta.foreignkeys.append((fk)) parcolmeta.foreignkeys.append((fk))