Implement or_ignore flag on inserts

This commit is contained in:
Erik Johnston 2015-04-07 12:06:01 +01:00
parent 304111afd0
commit c8d3f6486d
2 changed files with 10 additions and 4 deletions

View File

@ -393,7 +393,8 @@ class SQLBaseStore(object):
# "Simple" SQL API methods that operate on a single table with no JOINs, # "Simple" SQL API methods that operate on a single table with no JOINs,
# no complex WHERE clauses, just a dict of values for columns. # no complex WHERE clauses, just a dict of values for columns.
def _simple_insert(self, table, values, desc="_simple_insert"): def _simple_insert(self, table, values, or_ignore=False,
desc="_simple_insert"):
"""Executes an INSERT query on the named table. """Executes an INSERT query on the named table.
Args: Args:
@ -403,10 +404,11 @@ class SQLBaseStore(object):
return self.runInteraction( return self.runInteraction(
desc, desc,
self._simple_insert_txn, table, values, self._simple_insert_txn, table, values,
or_ignore=or_ignore
) )
@log_function @log_function
def _simple_insert_txn(self, txn, table, values): def _simple_insert_txn(self, txn, table, values, or_ignore=False):
sql = "INSERT INTO %s (%s) VALUES(%s)" % ( sql = "INSERT INTO %s (%s) VALUES(%s)" % (
table, table,
", ".join(k for k in values), ", ".join(k for k in values),
@ -418,8 +420,11 @@ class SQLBaseStore(object):
sql, values.values(), sql, values.values(),
) )
txn.execute(sql, values.values()) try:
return txn.lastrowid txn.execute(sql, values.values())
except self.database_engine.module.IntegrityError:
if not or_ignore:
raise
def _simple_upsert(self, table, keyvalues, values, desc="_simple_upsert"): def _simple_upsert(self, table, keyvalues, values, desc="_simple_upsert"):
""" """

View File

@ -57,6 +57,7 @@ class PresenceStore(SQLBaseStore):
values={"observed_user_id": observed_localpart, values={"observed_user_id": observed_localpart,
"observer_user_id": observer_userid}, "observer_user_id": observer_userid},
desc="allow_presence_visible", desc="allow_presence_visible",
or_ignore=True,
) )
def disallow_presence_visible(self, observed_localpart, observer_userid): def disallow_presence_visible(self, observed_localpart, observer_userid):