Refine pattern
This commit is contained in:
@@ -16,52 +16,78 @@ def get_pattern(name: str, id: str) -> dict[str, Any]:
|
||||
|
||||
def set_pattern_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
|
||||
id = cs.operations[0]['id']
|
||||
f_id = f"'{id}'"
|
||||
|
||||
old = get_pattern(name, id)
|
||||
new = { 'id': id, 'factors': [] }
|
||||
|
||||
f_id = f"'{id}'"
|
||||
new = { 'id': id }
|
||||
if 'factors' in cs.operations[0]:
|
||||
new['factors'] = cs.operations[0]['factors']
|
||||
else:
|
||||
new['factors'] = old['factors']
|
||||
|
||||
# TODO: transaction ?
|
||||
redo_sql = f"delete from patterns where id = {f_id};"
|
||||
redo_sql += f"\ndelete from _pattern where id = {f_id};"
|
||||
if len(cs.operations[0]['factors']) > 0:
|
||||
redo_sql += f"\ninsert into _pattern (id) values ({f_id});"
|
||||
for factor in cs.operations[0]['factors']:
|
||||
f_factor = float(factor)
|
||||
for f_factor in new['factors']:
|
||||
redo_sql += f"\ninsert into patterns (id, factor) values ({f_id}, {f_factor});"
|
||||
new['factors'].append(factor)
|
||||
|
||||
undo_sql = f"delete from patterns where id = {f_id};"
|
||||
undo_sql += f"\ndelete from _pattern where id = {f_id};"
|
||||
if len(old['factors']) > 0:
|
||||
undo_sql += f"\ninsert into _pattern (id) values ({f_id});"
|
||||
for f_factor in old['factors']:
|
||||
undo_sql += f"\ninsert into patterns (id, factor) values ({f_id}, {f_factor});"
|
||||
|
||||
redo_cs = { 'type': 'pattern' } | new
|
||||
undo_cs = { 'type': 'pattern' } | old
|
||||
redo_cs = g_update_prefix | { 'type': 'pattern' } | new
|
||||
undo_cs = g_update_prefix | { 'type': 'pattern' } | old
|
||||
|
||||
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
|
||||
|
||||
def set_pattern(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
result = set_pattern_cache(name, cs)
|
||||
result.redo_cs |= g_update_prefix
|
||||
result.undo_cs |= g_update_prefix
|
||||
return execute_command(name, result)
|
||||
return execute_command(name, set_pattern_cache(name, cs))
|
||||
|
||||
|
||||
def add_pattern_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
|
||||
id = cs.operations[0]['id']
|
||||
f_id = f"'{id}'"
|
||||
|
||||
new = { 'id': id, 'factors': cs.operations[0]['factors'] }
|
||||
|
||||
# TODO: transaction ?
|
||||
redo_sql = f"insert into _pattern (id) values ({f_id});"
|
||||
for f_factor in new['factors']:
|
||||
redo_sql += f"\ninsert into patterns (id, factor) values ({f_id}, {f_factor});"
|
||||
|
||||
undo_sql = f"delete from patterns where id = {f_id};"
|
||||
undo_sql += f"\ndelete from _pattern where id = {f_id};"
|
||||
|
||||
redo_cs = g_add_prefix | { 'type': 'pattern' } | new
|
||||
undo_cs = g_delete_prefix | { 'type': 'pattern' } | { 'id': id }
|
||||
|
||||
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
|
||||
|
||||
def add_pattern(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
result = set_pattern_cache(name, cs)
|
||||
result.redo_cs |= g_add_prefix
|
||||
result.undo_cs |= g_delete_prefix
|
||||
return execute_command(name, result)
|
||||
return execute_command(name, add_pattern_cache(name, cs))
|
||||
|
||||
|
||||
def delete_pattern_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
|
||||
id = cs.operations[0]['id']
|
||||
f_id = f"'{id}'"
|
||||
|
||||
old = get_pattern(name, id)
|
||||
|
||||
redo_sql = f"delete from patterns where id = {f_id};"
|
||||
redo_sql += f"\ndelete from _pattern where id = {f_id};"
|
||||
|
||||
# TODO: transaction ?
|
||||
undo_sql = f"insert into _pattern (id) values ({f_id});"
|
||||
for f_factor in old['factors']:
|
||||
undo_sql += f"\ninsert into patterns (id, factor) values ({f_id}, {f_factor});"
|
||||
|
||||
redo_cs = g_delete_prefix | { 'type': 'pattern' } | { 'id': id }
|
||||
undo_cs = g_add_prefix | { 'type': 'pattern' } | old
|
||||
|
||||
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
|
||||
|
||||
def delete_pattern(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
cs.operations[0]['factors'] = []
|
||||
result = set_pattern_cache(name, cs)
|
||||
result.redo_cs |= g_delete_prefix
|
||||
result.undo_cs |= g_add_prefix
|
||||
return execute_command(name, result)
|
||||
return execute_command(name, delete_pattern_cache(name, cs))
|
||||
|
||||
Reference in New Issue
Block a user