def render_result(self, cursor, paginate=True, filepath=None, sqlformat=None): """Render a result set and pipe through less. Args: cursor: iterable of tuples, with one special method: cursor.keys() which returns a list of string columns headings for the tuples. """ if not sqlformat: sqlformat = self.sqlformat if filepath: out = open(filepath, 'w') sqlformat = 'csv' else: out = pager() with out as stdout: if sqlformat == 'csv': self.format_result_csv(cursor, out=stdout) else: asciitable.draw(cursor, out=stdout, paginate=paginate, max_fieldsize=self.max_fieldsize)
def describe(self, table): """Print information about a table.""" if table not in self.get_metadata().tables: print("Table not found: %s" % table) return tbl = self.get_metadata().tables[table] def nullstr(nullable): return 'NULL' if nullable else 'NOT NULL' def namestr(c): return ('*%s' if c.primary_key else '%s') % c.name with pager() as out: items = ((namestr(c), c.type, nullstr(c.nullable)) for c in tbl.columns) out.write(b'Columns' + b'\n') asciitable.draw(FakedResult(sorted(items), 'Name Type Nullable'.split()), out, paginate=True, max_fieldsize=5000) out.write(b'\n') out.write(b'Primary Key (*)\n') out.write(b'---------------\n') pk = ', '.join(c.name for c in tbl.columns if c.primary_key) out.write(b' ') if not pk: out.write(b'(None Found!)') else: out.write(pk.encode('utf8')) out.write(b'\n\n') out.write(b'Foreign Keys\n') out.write(b'------------\n') fks = self.get_metadata().foreign_keys(table) fk = None for fk in fks: out.write((' %s\n' % str(fk)).encode('utf8')) if fk is None: out.write(b' (None Found)') out.write(('\n\nReferences to %s\n' % table).encode('utf8')) out.write(b'--------------' + b'-' * len(table) + b'\n') fks = self.get_metadata().fields_referencing(table) fk = None for fk in fks: out.write(b' ' + str(fk).encode('utf8') + b'\n') if fk is None: out.write(b' (None found)\n') out.write(b'\n\nIndexes\n') def items(): for idx in self.get_metadata().indexes(table): yield (idx.name, ', '.join(c.name for c in idx.columns), idx.unique) asciitable.draw(FakedResult(sorted(items()), 'Name Columns Unique'.split()), out, paginate=True, max_fieldsize=5000)
def describe(self, table): """Print information about a table.""" if table not in self.get_metadata().tables: print("Table not found: %s" % table) return tbl = self.get_metadata().tables[table] def nullstr(nullable): return 'NULL' if nullable else 'NOT NULL' def namestr(c): return ('*%s' if c.primary_key else '%s') % c.name with pager() as out: items = ((namestr(c), c.type, nullstr(c.nullable)) for c in tbl.columns) out.write(b'Columns' + b'\n') asciitable.draw( FakedResult(sorted(items), 'Name Type Nullable'.split()), out, paginate=True, max_fieldsize=5000) out.write(b'\n') out.write(b'Primary Key (*)\n') out.write(b'---------------\n') pk = ', '.join(c.name for c in tbl.columns if c.primary_key) out.write(b' ') if not pk: out.write(b'(None Found!)') else: out.write(pk.encode('utf8')) out.write(b'\n\n') out.write(b'Foreign Keys\n') out.write(b'------------\n') fks = self.get_metadata().foreign_keys(table) fk = None for fk in fks: out.write((' %s\n' % str(fk)).encode('utf8')) if fk is None: out.write(b' (None Found)') out.write(('\n\nReferences to %s\n' % table).encode('utf8')) out.write(b'--------------' + b'-' * len(table) + b'\n') fks = self.get_metadata().fields_referencing(table) fk = None for fk in fks: out.write(b' ' + str(fk).encode('utf8') + b'\n') if fk is None: out.write(b' (None found)\n') out.write(b'\n\nIndexes\n') def items(): for idx in self.get_metadata().indexes(table): yield (idx.name, ', '.join(c.name for c in idx.columns), idx.unique) asciitable.draw( FakedResult(sorted(items()), 'Name Columns Unique'.split()), out, paginate=True, max_fieldsize=5000)