Make SQL INSERT querys more resilient

Specify the column in which we insert otherwise downgrading Gajim is
not possible anymore because the query breaks if we modified the column
count in future
This commit is contained in:
Philipp Hörist 2018-09-19 20:11:45 +02:00 committed by Philipp Hörist
parent 0d858180df
commit fa45935c11
1 changed files with 27 additions and 26 deletions

View File

@ -177,7 +177,7 @@ class Logger:
con.close() con.close()
@staticmethod @staticmethod
def _get_user_version(con) -> int: def _get_user_version(con: sqlite.Connection) -> int:
""" Return the value of PRAGMA user_version. """ """ Return the value of PRAGMA user_version. """
return con.execute('PRAGMA user_version').fetchone()[0] return con.execute('PRAGMA user_version').fetchone()[0]
@ -547,7 +547,7 @@ class Logger:
if type_id == TypeConstant.NO_TRANSPORT: if type_id == TypeConstant.NO_TRANSPORT:
return 'jabber' return 'jabber'
def convert_human_subscription_values_to_db_api_values(self, sub): def convert_xmpp_sub(self, sub):
""" """
Convert from string style to constant ints for db Convert from string style to constant ints for db
""" """
@ -560,7 +560,7 @@ class Logger:
if sub == 'both': if sub == 'both':
return SubscriptionConstant.BOTH return SubscriptionConstant.BOTH
def convert_db_api_values_to_human_subscription_values(self, sub): def convert_db_sub(self, sub):
""" """
Convert from constant ints for db to string style Convert from constant ints for db to string style
""" """
@ -577,9 +577,10 @@ class Logger:
""" """
Add unread message with id: message_id Add unread message with id: message_id
""" """
sql = 'INSERT INTO unread_messages VALUES (%d, %d, 0)' % (message_id, sql = '''INSERT INTO unread_messages (message_id, jid_id, shown)
jid_id) VALUES (?, ?, 0)'''
self.simple_commit(sql) self._con.execute(sql, (message_id, jid_id))
self._timeout_commit()
def set_read_messages(self, message_ids): def set_read_messages(self, message_ids):
""" """
@ -947,8 +948,9 @@ class Logger:
(type_id, jid) (type_id, jid)
self.simple_commit(sql) self.simple_commit(sql)
return return
sql = 'INSERT INTO transports_cache VALUES ("%s", %d)' % (jid, type_id) sql = 'INSERT INTO transports_cache (transport, type) VALUES (?, ?)'
self.simple_commit(sql) self._con.execute(sql, (jid, type_id))
self._timeout_commit()
def get_transports_type(self): def get_transports_type(self):
""" """
@ -1113,7 +1115,7 @@ class Logger:
self._timeout_commit() self._timeout_commit()
def add_or_update_contact(self, account_jid, jid, name, sub, ask, groups, def add_or_update_contact(self, account_jid, jid, name, sub, ask, groups,
commit=True): commit=True):
""" """
Add or update a contact from account_jid roster Add or update a contact from account_jid roster
""" """
@ -1124,29 +1126,30 @@ class Logger:
try: try:
account_jid_id = self.get_jid_id(account_jid) account_jid_id = self.get_jid_id(account_jid)
jid_id = self.get_jid_id(jid, type_=JIDConstant.NORMAL_TYPE) jid_id = self.get_jid_id(jid, type_=JIDConstant.NORMAL_TYPE)
except exceptions.PysqliteOperationalError as e: except exceptions.PysqliteOperationalError as error:
raise exceptions.PysqliteOperationalError(str(e)) raise exceptions.PysqliteOperationalError(str(error))
# Update groups information # Update groups information
# First we delete all previous groups information # First we delete all previous groups information
self._con.execute( sql = 'DELETE FROM roster_group WHERE account_jid_id=? AND jid_id=?'
'DELETE FROM roster_group WHERE account_jid_id=? AND jid_id=?', self._con.execute(sql, (account_jid_id, jid_id))
(account_jid_id, jid_id))
# Then we add all new groups information # Then we add all new groups information
sql = '''INSERT INTO roster_group (account_jid_id, jid_id, group_name)
VALUES (?, ?, ?)'''
for group in groups: for group in groups:
self._con.execute('INSERT INTO roster_group VALUES(?, ?, ?)', self._con.execute(sql, (account_jid_id, jid_id, group))
(account_jid_id, jid_id, group))
if name is None: if name is None:
name = '' name = ''
self._con.execute(''' sql = '''REPLACE INTO roster_entry
REPLACE INTO roster_entry (account_jid_id, jid_id, name, subscription, ask)
(account_jid_id, jid_id, name, subscription, ask) VALUES(?, ?, ?, ?, ?)'''
VALUES(?, ?, ?, ?, ?)''', ( self._con.execute(sql, (account_jid_id,
account_jid_id, jid_id, name, jid_id,
self.convert_human_subscription_values_to_db_api_values(sub), name,
bool(ask))) self.convert_xmpp_sub(sub),
bool(ask)))
if commit: if commit:
self._timeout_commit() self._timeout_commit()
@ -1172,9 +1175,7 @@ class Logger:
data[jid]['name'] = name data[jid]['name'] = name
else: else:
data[jid]['name'] = None data[jid]['name'] = None
data[jid]['subscription'] = \ data[jid]['subscription'] = self.convert_db_sub(row.subscription)
self.convert_db_api_values_to_human_subscription_values(
row.subscription)
data[jid]['groups'] = [] data[jid]['groups'] = []
data[jid]['resources'] = {} data[jid]['resources'] = {}
if row.ask: if row.ask: