Use without rowid only if supported (closes #523)

This commit is contained in:
Lonami Exo 2018-01-06 19:35:24 +01:00
parent f357d00911
commit 7745b8e7ee

View File

@ -107,36 +107,34 @@ class Session:
c.close() c.close()
else: else:
# Tables don't exist, create new ones # Tables don't exist, create new ones
c.execute("create table version (version integer)") self._create_table(
c.execute("insert into version values (?)", (CURRENT_VERSION,)) c,
c.execute( "version (version integer primary key)"
"""create table sessions ( ,
"""sessions (
dc_id integer primary key, dc_id integer primary key,
server_address text, server_address text,
port integer, port integer,
auth_key blob auth_key blob
) without rowid""" )"""
) ,
c.execute( """entities (
"""create table entities (
id integer primary key, id integer primary key,
hash integer not null, hash integer not null,
username text, username text,
phone integer, phone integer,
name text name text
) without rowid""" )"""
) ,
# Save file_size along with md5_digest """sent_files (
# to make collisions even more unlikely.
c.execute(
"""create table sent_files (
md5_digest blob, md5_digest blob,
file_size integer, file_size integer,
file_id integer, file_id integer,
part_count integer, part_count integer,
primary key(md5_digest, file_size) primary key(md5_digest, file_size)
) without rowid""" )"""
) )
c.execute("insert into version values (?)", (CURRENT_VERSION,))
# Migrating from JSON -> new table and may have entities # Migrating from JSON -> new table and may have entities
if entities: if entities:
c.executemany( c.executemany(
@ -170,17 +168,29 @@ class Session:
return [] # No entities return [] # No entities
def _upgrade_database(self, old): def _upgrade_database(self, old):
c = self._conn.cursor()
if old == 1: if old == 1:
self._conn.execute( self._create_table(c,"""sent_files (
"""create table sent_files (
md5_digest blob, md5_digest blob,
file_size integer, file_size integer,
file_id integer, file_id integer,
part_count integer, part_count integer,
primary key(md5_digest, file_size) primary key(md5_digest, file_size)
) without rowid""" )""")
)
old = 2 old = 2
c.close()
def _create_table(self, c, *definitions):
"""
Creates a table given its definition 'name (columns).
If the sqlite version is >= 3.8.2, it will use "without rowid".
See http://www.sqlite.org/releaselog/3_8_2.html.
"""
required = (3, 8, 2)
sqlite_v = tuple(int(x) for x in sqlite3.sqlite_version.split('.'))
extra = ' without rowid' if sqlite_v >= required else ''
for definition in definitions:
c.execute('create table {}{}'.format(definition, extra))
# Data from sessions should be kept as properties # Data from sessions should be kept as properties
# not to fetch the database every time we need it # not to fetch the database every time we need it