def print_schema(self, schema): with self.conn as conn: total = 0 tables = util.get_schema_info(conn, schema) pprint(tables) total += len(tables) print('Inspected %d tables' % total)
def print_schema_graph(self, schema): with self.conn as conn: out = [] tables = util.get_schema_info(conn, schema) for name, kfields, vfields, fkeys in tables: if fkeys: out.extend(_children(name, fkeys.values())) return 'digraph G {\n%s\n}' % '\n'.join(out)
def print_schemas(self): with self.conn as conn: schemas = conn.all("select nspname from pg_namespace " "where nspname != 'information_schema' " "and nspname not like 'pg_%'") total = 0 for schema, in schemas: print(schema, '**********************************') tables = util.get_schema_info(conn, schema) pprint(tables) total += len(tables) print('Inspected %d tables' % total)
def test_introspection(): from sqlplain import core, util, _util with core.ScratchDB( 'config://scratch/psycopg2_uri', _util.createbook) as db: assert_equal( util.get_table_info(db.conn, 'book_author'), ['book_author', ['code', 'author_id'], [], {'author_id': 'public.author.id', 'code': 'public.book.code'}]) assert_equal( util.get_schema_info(db.conn), [['author', ['id'], ['firstname', 'lastname'], {}], ['book', ['code'], ['title', 'score'], {'score': 'public.score.value'}], ['book_author', ['code', 'author_id'], [], {'author_id': 'public.author.id', 'code': 'public.book.code'}], ['reading_history', ['code'], ['date'], {'code': 'public.book.code'}], ['score', ['value'], ['evaluation'], {}]])
def reflect(cls, conn, schema_name): 'Map a database schema into a Schema object' return cls(util.get_schema_info(conn, schema_name), schema_name)