2018-05-21 08:02:34 +00:00
|
|
|
import hashlib
|
|
|
|
|
2018-08-16 00:58:56 +00:00
|
|
|
from .utils import (
|
|
|
|
detect_spatialite,
|
|
|
|
detect_fts,
|
|
|
|
escape_sqlite,
|
|
|
|
get_all_foreign_keys,
|
2019-04-07 01:58:51 +00:00
|
|
|
table_columns,
|
2018-08-16 00:58:56 +00:00
|
|
|
sqlite3
|
|
|
|
)
|
2018-05-21 08:02:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
HASH_BLOCK_SIZE = 1024 * 1024
|
|
|
|
|
|
|
|
|
|
|
|
def inspect_hash(path):
|
|
|
|
" Calculate the hash of a database, efficiently. "
|
|
|
|
m = hashlib.sha256()
|
|
|
|
with path.open("rb") as fp:
|
|
|
|
while True:
|
|
|
|
data = fp.read(HASH_BLOCK_SIZE)
|
|
|
|
if not data:
|
|
|
|
break
|
|
|
|
m.update(data)
|
|
|
|
|
|
|
|
return m.hexdigest()
|
|
|
|
|
|
|
|
|
|
|
|
def inspect_views(conn):
|
|
|
|
" List views in a database. "
|
|
|
|
return [v[0] for v in conn.execute('select name from sqlite_master where type = "view"')]
|
|
|
|
|
|
|
|
|
|
|
|
def detect_primary_keys(conn, table):
|
|
|
|
" Figure out primary keys for a table. "
|
|
|
|
table_info_rows = [
|
|
|
|
row
|
|
|
|
for row in conn.execute(
|
|
|
|
'PRAGMA table_info("{}")'.format(table)
|
|
|
|
).fetchall()
|
|
|
|
if row[-1]
|
|
|
|
]
|
|
|
|
table_info_rows.sort(key=lambda row: row[-1])
|
|
|
|
return [str(r[1]) for r in table_info_rows]
|
|
|
|
|
|
|
|
|
|
|
|
def inspect_tables(conn, database_metadata):
|
|
|
|
" List tables and their row counts, excluding uninteresting tables. "
|
|
|
|
tables = {}
|
|
|
|
table_names = [
|
|
|
|
r["name"]
|
|
|
|
for r in conn.execute(
|
|
|
|
'select * from sqlite_master where type="table"'
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
|
|
|
for table in table_names:
|
|
|
|
table_metadata = database_metadata.get("tables", {}).get(
|
|
|
|
table, {}
|
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
|
|
|
count = conn.execute(
|
|
|
|
"select count(*) from {}".format(escape_sqlite(table))
|
|
|
|
).fetchone()[0]
|
|
|
|
except sqlite3.OperationalError:
|
|
|
|
# This can happen when running against a FTS virtual table
|
|
|
|
# e.g. "select count(*) from some_fts;"
|
|
|
|
count = 0
|
|
|
|
|
2019-04-07 01:58:51 +00:00
|
|
|
column_names = table_columns(conn, table)
|
2018-05-21 08:02:34 +00:00
|
|
|
|
|
|
|
tables[table] = {
|
|
|
|
"name": table,
|
|
|
|
"columns": column_names,
|
|
|
|
"primary_keys": detect_primary_keys(conn, table),
|
|
|
|
"count": count,
|
|
|
|
"hidden": table_metadata.get("hidden") or False,
|
|
|
|
"fts_table": detect_fts(conn, table),
|
|
|
|
}
|
|
|
|
|
|
|
|
foreign_keys = get_all_foreign_keys(conn)
|
|
|
|
for table, info in foreign_keys.items():
|
|
|
|
tables[table]["foreign_keys"] = info
|
|
|
|
|
|
|
|
# Mark tables 'hidden' if they relate to FTS virtual tables
|
|
|
|
hidden_tables = [
|
|
|
|
r["name"]
|
|
|
|
for r in conn.execute(
|
|
|
|
"""
|
|
|
|
select name from sqlite_master
|
|
|
|
where rootpage = 0
|
|
|
|
and sql like '%VIRTUAL TABLE%USING FTS%'
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
|
|
|
if detect_spatialite(conn):
|
|
|
|
# Also hide Spatialite internal tables
|
|
|
|
hidden_tables += [
|
|
|
|
"ElementaryGeometries",
|
|
|
|
"SpatialIndex",
|
|
|
|
"geometry_columns",
|
|
|
|
"spatial_ref_sys",
|
|
|
|
"spatialite_history",
|
|
|
|
"sql_statements_log",
|
|
|
|
"sqlite_sequence",
|
|
|
|
"views_geometry_columns",
|
|
|
|
"virts_geometry_columns",
|
|
|
|
] + [
|
|
|
|
r["name"]
|
|
|
|
for r in conn.execute(
|
|
|
|
"""
|
|
|
|
select name from sqlite_master
|
|
|
|
where name like "idx_%"
|
|
|
|
and type = "table"
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
|
|
|
for t in tables.keys():
|
|
|
|
for hidden_table in hidden_tables:
|
|
|
|
if t == hidden_table or t.startswith(hidden_table):
|
|
|
|
tables[t]["hidden"] = True
|
|
|
|
continue
|
|
|
|
|
|
|
|
return tables
|