MCPcopy Index your code
hub / github.com/dbcli/pgcli / populate_scoped_cols

Method populate_scoped_cols

pgcli/pgcompleter.py:885–929  ·  view source on GitHub ↗

Find all columns in a set of scoped_tables. :param scoped_tbls: list of TableReference namedtuples :param local_tbls: tuple(TableMetadata) :return: {TableReference:{colname:ColumnMetaData}}

(self, scoped_tbls, local_tbls=())

Source from the content-addressed store, hash-verified

883 }
884
885 def populate_scoped_cols(self, scoped_tbls, local_tbls=()):
886 """Find all columns in a set of scoped_tables.
887
888 :param scoped_tbls: list of TableReference namedtuples
889 :param local_tbls: tuple(TableMetadata)
890 :return: {TableReference:{colname:ColumnMetaData}}
891
892 """
893 ctes = {normalize_ref(t.name): t.columns for t in local_tbls}
894 columns = OrderedDict()
895 meta = self.dbmetadata
896
897 def addcols(schema, rel, alias, reltype, cols):
898 tbl = TableReference(schema, rel, alias, reltype == "functions")
899 if tbl not in columns:
900 columns[tbl] = []
901 columns[tbl].extend(cols)
902
903 for tbl in scoped_tbls:
904 # Local tables should shadow database tables
905 if tbl.schema is None and normalize_ref(tbl.name) in ctes:
906 cols = ctes[normalize_ref(tbl.name)]
907 addcols(None, tbl.name, "CTE", tbl.alias, cols)
908 continue
909 schemas = [tbl.schema] if tbl.schema else self.search_path
910 for schema in schemas:
911 relname = self.escape_name(tbl.name)
912 schema = self.escape_name(schema)
913 if tbl.is_function:
914 # Return column names from a set-returning function
915 # Get an array of FunctionMetadata objects
916 functions = meta["functions"].get(schema, {}).get(relname)
917 for func in functions or []:
918 # func is a FunctionMetadata object
919 cols = func.fields()
920 addcols(schema, relname, tbl.alias, "functions", cols)
921 else:
922 for reltype in ("tables", "views"):
923 cols = meta[reltype].get(schema, {}).get(relname)
924 if cols:
925 cols = cols.values()
926 addcols(schema, relname, tbl.alias, reltype, cols)
927 break
928
929 return columns
930
931 def _get_schemas(self, obj_typ, schema):
932 """Returns a list of schemas from which to suggest objects.

Callers 3

get_column_matchesMethod · 0.95
get_join_matchesMethod · 0.95

Calls 3

escape_nameMethod · 0.95
normalize_refFunction · 0.85
fieldsMethod · 0.80

Tested by

no test coverage detected