# -*- coding: utf-8 -*- # Copyright (c) 2015, Françoise Conil # Copyright (c) 2011, Fredrik Karlsson # All rights reserved. # # Redistribution and use in source and binary forms, with or without modification, # are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its contributors # may be used to endorse or promote products derived from this software without # specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. # IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, # EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ This is a python version of Fredrik Karlsson sqlite2dot TCL script. Those scripts aim at providing a visualization of a SQLite schema by creating a .dot file for use with the Graphwiz program. """ import sys import os import sqlite3 def dict_factory(cursor, row): d = {} for idx, col in enumerate(cursor.description): d[col[0]] = row[idx] return d def sqlite_db_tables(c=None): """ List the tables of a sqlite database. How do I list all tables/indices contained in an SQLite database : https://www.sqlite.org/faq.html#q7 """ db_tables = [] if c is not None: c.execute('select name from sqlite_master where type = "table" and name NOT LIKE "%sqlite_%"') rows = c.fetchall() for r in rows: db_tables.append(r['name']) return db_tables def sqlite_table_columns(c=None, table=None): """ The PRAGMA statement is an SQL extension specific to SQLite and used to modify the operation of the SQLite library or to query the SQLite library for internal (non-table) data. https://sqlite.org/pragma.html """ db_columns = [] if c is not None and table is not None: c.execute('PRAGMA table_info("{0}")'.format(table)) rows = c.fetchall() for r in rows: db_columns.append(r) return db_columns def sqlite_table_foreign_keys(c=None, table=None): """ """ db_fk = [] if c is not None and table is not None: c.execute('PRAGMA foreign_key_list({0})'.format(table)) rows = c.fetchall() for r in rows: db_fk.append(r) return db_fk def sqlite_table_indexes(c=None, table=None): """ """ db_index = {} if c is not None and table is not None: c.execute('PRAGMA index_list({0})'.format(table)) rows = c.fetchall() for r in rows: db_index[r['name']] = {} db_index[r['name']]['infos'] = r for idx in db_index.keys(): c.execute('PRAGMA index_info({0})'.format(idx)) rows = c.fetchall() db_index[idx]['composed_of'] = [] for r in rows: db_index[idx]['composed_of'].append(r) return db_index def write_graphiz_graph(db_struct=None, db_filename=None): """ http://graphviz.org/Documentation.php Would it be interesting to use the python graphviz package ? https://pypi.python.org/pypi/graphviz/ """ gviz_filename = None if db_struct is not None and db_filename is not None: # Generates a .dot filename from the db filename gviz_filename = '{0}.dot'.format(os.path.splitext(db_filename)[0]) with open(gviz_filename, 'w') as f: #f.write('digraph structs {\n') f.write('digraph {\n') f.write('\t\trankdir=LR;\n') f.write('\t\tlabel="{0}";\n'.format(os.path.split(db_filename)[1])) f.write('\t\tlabelloc="t";\n') for tname, tstruct in db_struct.items(): #f.write('\tsubgraph cluster_{0} {1}\n'.format(tname, '{')) f.write('\t\tnode [shape=none];\n') #f.write('\t\tlabel=\"{0}\";\n'.format(tname)) #f.write('\t\trank=same;\n') #f.write('\t\tclusterrank=local;\n') #f.write('\t\tlabeljust=l;\n') #f.write('\t\tstyle=dotted;\n') rows = [] for cols in tstruct['columns']: rows.append('
{0} |