Remove the PushRuleTable and PushRuleEnableTable objects

This commit is contained in:
Mark Haines 2016-01-12 14:22:24 +00:00
parent 42aa1f3f33
commit 31de2953a3
1 changed files with 33 additions and 50 deletions

View File

@ -27,11 +27,14 @@ class PushRuleStore(SQLBaseStore):
@cachedInlineCallbacks() @cachedInlineCallbacks()
def get_push_rules_for_user(self, user_name): def get_push_rules_for_user(self, user_name):
rows = yield self._simple_select_list( rows = yield self._simple_select_list(
table=PushRuleTable.table_name, table="push_rules",
keyvalues={ keyvalues={
"user_name": user_name, "user_name": user_name,
}, },
retcols=PushRuleTable.fields, retcols=(
"user_name", "rule_id", "priority_class", "priority",
"conditions", "actions",
),
desc="get_push_rules_enabled_for_user", desc="get_push_rules_enabled_for_user",
) )
@ -44,11 +47,13 @@ class PushRuleStore(SQLBaseStore):
@cachedInlineCallbacks() @cachedInlineCallbacks()
def get_push_rules_enabled_for_user(self, user_name): def get_push_rules_enabled_for_user(self, user_name):
results = yield self._simple_select_list( results = yield self._simple_select_list(
table=PushRuleEnableTable.table_name, table="push_rules_enable",
keyvalues={ keyvalues={
'user_name': user_name 'user_name': user_name
}, },
retcols=PushRuleEnableTable.fields, retcols=(
"user_name", "rule_id", "enabled",
),
desc="get_push_rules_enabled_for_user", desc="get_push_rules_enabled_for_user",
) )
defer.returnValue({ defer.returnValue({
@ -61,14 +66,15 @@ class PushRuleStore(SQLBaseStore):
def f(txn, user_ids_to_fetch): def f(txn, user_ids_to_fetch):
sql = ( sql = (
"SELECT " + "SELECT"
",".join("pr."+x for x in PushRuleTable.fields) + " pr.user_name, pr.rule_id, priority_class, priority,"
" FROM " + PushRuleTable.table_name + " pr " + " conditions, actions"
" LEFT JOIN " + PushRuleEnableTable.table_name + " pre " + " FROM push_rules AS pr"
" ON pr.user_name = pre.user_name and pr.rule_id = pre.rule_id " + " LEFT JOIN push_rules_enable AS pre"
" WHERE pr.user_name " + " ON pr.user_name = pre.user_name AND pr.rule_id = pre.rule_id"
" WHERE pr.user_name"
" IN (" + ",".join("?" for _ in user_ids_to_fetch) + ")" " IN (" + ",".join("?" for _ in user_ids_to_fetch) + ")"
" AND (pre.enabled is null or pre.enabled = 1)" " AND (pre.enabled IS NULL OR pre.enabled = 1)"
" ORDER BY pr.user_name, pr.priority_class DESC, pr.priority DESC" " ORDER BY pr.user_name, pr.priority_class DESC, pr.priority DESC"
) )
txn.execute(sql, user_ids_to_fetch) txn.execute(sql, user_ids_to_fetch)
@ -86,14 +92,15 @@ class PushRuleStore(SQLBaseStore):
"bulk_get_push_rules", f, batch_user_ids "bulk_get_push_rules", f, batch_user_ids
) )
for r in rows: cols = (
rawdict = { "user_name", "rule_id", "priority_class", "priority",
PushRuleTable.fields[i]: r[i] for i in range(len(r)) "conditions", "actions",
} )
for row in rows:
rawdict = dict(zip(cols, rows))
results.setdefault(rawdict["user_name"], []).append(rawdict)
if rawdict['user_name'] not in results:
results[rawdict['user_name']] = []
results[rawdict['user_name']].append(rawdict)
defer.returnValue(results) defer.returnValue(results)
@defer.inlineCallbacks @defer.inlineCallbacks
@ -131,7 +138,7 @@ class PushRuleStore(SQLBaseStore):
res = self._simple_select_one_txn( res = self._simple_select_one_txn(
txn, txn,
table=PushRuleTable.table_name, table="push_rules",
keyvalues={ keyvalues={
"user_name": user_name, "user_name": user_name,
"rule_id": relative_to_rule, "rule_id": relative_to_rule,
@ -170,7 +177,7 @@ class PushRuleStore(SQLBaseStore):
new_rule['priority'] = new_rule_priority new_rule['priority'] = new_rule_priority
sql = ( sql = (
"SELECT COUNT(*) FROM " + PushRuleTable.table_name + "SELECT COUNT(*) FROM push_rules"
" WHERE user_name = ? AND priority_class = ? AND priority = ?" " WHERE user_name = ? AND priority_class = ? AND priority = ?"
) )
txn.execute(sql, (user_name, priority_class, new_rule_priority)) txn.execute(sql, (user_name, priority_class, new_rule_priority))
@ -179,7 +186,7 @@ class PushRuleStore(SQLBaseStore):
# if there are conflicting rules, bump everything # if there are conflicting rules, bump everything
if num_conflicting: if num_conflicting:
sql = "UPDATE "+PushRuleTable.table_name+" SET priority = priority " sql = "UPDATE push_rules SET priority = priority "
if after: if after:
sql += "-1" sql += "-1"
else: else:
@ -202,7 +209,7 @@ class PushRuleStore(SQLBaseStore):
self._simple_insert_txn( self._simple_insert_txn(
txn, txn,
table=PushRuleTable.table_name, table="push_rules",
values=new_rule, values=new_rule,
) )
@ -210,7 +217,7 @@ class PushRuleStore(SQLBaseStore):
priority_class, **kwargs): priority_class, **kwargs):
# find the highest priority rule in that class # find the highest priority rule in that class
sql = ( sql = (
"SELECT COUNT(*), MAX(priority) FROM " + PushRuleTable.table_name + "SELECT COUNT(*), MAX(priority) FROM push_rules"
" WHERE user_name = ? and priority_class = ?" " WHERE user_name = ? and priority_class = ?"
) )
txn.execute(sql, (user_name, priority_class)) txn.execute(sql, (user_name, priority_class))
@ -237,7 +244,7 @@ class PushRuleStore(SQLBaseStore):
self._simple_insert_txn( self._simple_insert_txn(
txn, txn,
table=PushRuleTable.table_name, table="push_rules",
values=new_rule, values=new_rule,
) )
@ -253,7 +260,7 @@ class PushRuleStore(SQLBaseStore):
rule_id (str): The rule_id of the rule to be deleted rule_id (str): The rule_id of the rule to be deleted
""" """
yield self._simple_delete_one( yield self._simple_delete_one(
PushRuleTable.table_name, "push_rules",
{'user_name': user_name, 'rule_id': rule_id}, {'user_name': user_name, 'rule_id': rule_id},
desc="delete_push_rule", desc="delete_push_rule",
) )
@ -274,7 +281,7 @@ class PushRuleStore(SQLBaseStore):
new_id = self._push_rules_enable_id_gen.get_next_txn(txn) new_id = self._push_rules_enable_id_gen.get_next_txn(txn)
self._simple_upsert_txn( self._simple_upsert_txn(
txn, txn,
PushRuleEnableTable.table_name, "push_rules_enable",
{'user_name': user_name, 'rule_id': rule_id}, {'user_name': user_name, 'rule_id': rule_id},
{'enabled': 1 if enabled else 0}, {'enabled': 1 if enabled else 0},
{'id': new_id}, {'id': new_id},
@ -293,27 +300,3 @@ class RuleNotFoundException(Exception):
class InconsistentRuleException(Exception): class InconsistentRuleException(Exception):
pass pass
class PushRuleTable(object):
table_name = "push_rules"
fields = [
"id",
"user_name",
"rule_id",
"priority_class",
"priority",
"conditions",
"actions",
]
class PushRuleEnableTable(object):
table_name = "push_rules_enable"
fields = [
"user_name",
"rule_id",
"enabled"
]