Refine api to support one batch command
This commit is contained in:
@@ -42,6 +42,14 @@ class ChangeSet:
|
||||
return self
|
||||
|
||||
|
||||
class SqlChangeSet:
|
||||
def __init__(self, redo_sql: str, undo_sql: str, redo_cs: dict[str, str], undo_cs: dict[str, str]) -> None:
|
||||
self.redo_sql = redo_sql
|
||||
self.undo_sql = undo_sql
|
||||
self.redo_cs = redo_cs
|
||||
self.undo_cs = undo_cs
|
||||
|
||||
|
||||
def read(name: str, sql: str) -> Row:
|
||||
with conn[name].cursor(row_factory=dict_row) as cur:
|
||||
cur.execute(sql)
|
||||
@@ -72,20 +80,20 @@ def get_current_operation(name: str) -> int:
|
||||
return int(read(name, 'select id from current_operation')['id'])
|
||||
|
||||
|
||||
def execute_command(name: str, redo_sql: str, undo_sql: str, redo_cs: dict[str, str], undo_cs: dict[str, str]) -> ChangeSet:
|
||||
write(name, redo_sql)
|
||||
def execute_command(name: str, command: SqlChangeSet) -> ChangeSet:
|
||||
write(name, command.redo_sql)
|
||||
|
||||
parent = get_current_operation(name)
|
||||
redo_sql = redo_sql.replace("'", "''")
|
||||
undo_sql = undo_sql.replace("'", "''")
|
||||
redo_cs_str = str(redo_cs).replace("'", "''")
|
||||
undo_cs_str = str(undo_cs).replace("'", "''")
|
||||
redo_sql = command.redo_sql.replace("'", "''")
|
||||
undo_sql = command.undo_sql.replace("'", "''")
|
||||
redo_cs_str = str(command.redo_cs).replace("'", "''")
|
||||
undo_cs_str = str(command.undo_cs).replace("'", "''")
|
||||
write(name, f"insert into operation (id, redo, undo, parent, redo_cs, undo_cs) values (default, '{redo_sql}', '{undo_sql}', {parent}, '{redo_cs_str}', '{undo_cs_str}')")
|
||||
|
||||
current = read(name, 'select max(id) as id from operation')['id']
|
||||
write(name, f"update current_operation set id = {current}")
|
||||
|
||||
return ChangeSet(redo_cs)
|
||||
return ChangeSet(command.redo_cs)
|
||||
|
||||
|
||||
def execute_undo(name: str, discard: bool = False) -> ChangeSet:
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
from psycopg.rows import dict_row, Row
|
||||
from .connection import g_conn_dict as conn
|
||||
from .operation import *
|
||||
|
||||
|
||||
_NODE = '_node'
|
||||
|
||||
@@ -39,7 +39,7 @@ class Status(object):
|
||||
return { 'type': self.type, 'link': self.link, 'status': self.status, 'setting': self.setting }
|
||||
|
||||
|
||||
def set_status(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
def set_status_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
|
||||
old = Status(get_status(name, cs.operations[0]['link']))
|
||||
raw_new = get_status(name, cs.operations[0]['link'])
|
||||
|
||||
@@ -61,4 +61,8 @@ def set_status(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
redo_cs = g_update_prefix | new.as_dict()
|
||||
undo_cs = g_update_prefix | old.as_dict()
|
||||
|
||||
return execute_command(name, redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
|
||||
|
||||
def set_status(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
return execute_command(name, set_status_cache(name, cs))
|
||||
|
||||
@@ -14,7 +14,7 @@ def get_pattern(name: str, id: str) -> dict[str, Any]:
|
||||
return { 'id': id, 'factors': ps }
|
||||
|
||||
|
||||
def set_pattern(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
def set_pattern_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
|
||||
id = cs.operations[0]['id']
|
||||
|
||||
old = get_pattern(name, id)
|
||||
@@ -42,4 +42,8 @@ def set_pattern(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
redo_cs = g_update_prefix | { 'type': 'pattern' } | new
|
||||
undo_cs = g_update_prefix | { 'type': 'pattern' } | old
|
||||
|
||||
return execute_command(name, redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
|
||||
|
||||
def set_pattern(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
return execute_command(name, set_pattern_cache(name, cs))
|
||||
|
||||
@@ -16,7 +16,7 @@ def get_curve(name: str, id: str) -> dict[str, Any]:
|
||||
return { 'id': id, 'coords': cs }
|
||||
|
||||
|
||||
def set_curve(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
def set_curve_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
|
||||
id = cs.operations[0]['id']
|
||||
|
||||
old = get_curve(name, id)
|
||||
@@ -46,4 +46,8 @@ def set_curve(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
redo_cs = g_update_prefix | { 'type': 'curve' } | new
|
||||
undo_cs = g_update_prefix | { 'type': 'curve' } | old
|
||||
|
||||
return execute_command(name, redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
|
||||
|
||||
def set_curve(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
return execute_command(name, set_curve_cache(name, cs))
|
||||
|
||||
@@ -30,7 +30,7 @@ class Emitter(object):
|
||||
return { 'type': self.type, 'junction': self.junction, 'coefficient': self.coefficient }
|
||||
|
||||
|
||||
def set_emitter(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
def set_emitter_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
|
||||
old = Emitter(get_emitter(name, cs.operations[0]['junction']))
|
||||
raw_new = get_emitter(name, cs.operations[0]['junction'])
|
||||
|
||||
@@ -52,4 +52,8 @@ def set_emitter(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
redo_cs = g_update_prefix | new.as_dict()
|
||||
undo_cs = g_update_prefix | old.as_dict()
|
||||
|
||||
return execute_command(name, redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
|
||||
|
||||
def set_emitter(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
return execute_command(name, set_emitter_cache(name, cs))
|
||||
|
||||
@@ -10,7 +10,7 @@ def get_title(name: str) -> dict[str, Any]:
|
||||
return { 'value': title['value'] }
|
||||
|
||||
|
||||
def set_title(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
def set_title_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
|
||||
new = cs.operations[0]['value']
|
||||
old = get_title(name)['value']
|
||||
|
||||
@@ -20,4 +20,8 @@ def set_title(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
redo_cs = g_update_prefix | { 'type': 'title', 'value': new }
|
||||
undo_cs = g_update_prefix | { 'type': 'title', 'value': old }
|
||||
|
||||
return execute_command(name, redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
|
||||
|
||||
def set_title(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
return execute_command(name, set_title_cache(name ,cs))
|
||||
|
||||
@@ -29,7 +29,7 @@ def get_time(name: str) -> dict[str, Any]:
|
||||
return d
|
||||
|
||||
|
||||
def set_time(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
def set_time_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
|
||||
raw_old = get_time(name)
|
||||
|
||||
old = {}
|
||||
@@ -60,4 +60,8 @@ def set_time(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
undo_sql += f"update times set value = '{value}' where key = '{key}';"
|
||||
undo_cs |= { key: value }
|
||||
|
||||
return execute_command(name, redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
|
||||
|
||||
def set_time(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
return execute_command(name, set_time_cache(name, cs))
|
||||
|
||||
@@ -67,7 +67,7 @@ def get_option(name: str) -> dict[str, Any]:
|
||||
return d
|
||||
|
||||
|
||||
def set_option(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
def set_option_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
|
||||
raw_old = get_option(name)
|
||||
|
||||
old = {}
|
||||
@@ -98,4 +98,8 @@ def set_option(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
undo_sql += f"update options set value = '{value}' where key = '{key}';"
|
||||
undo_cs |= { key: value }
|
||||
|
||||
return execute_command(name, redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
|
||||
|
||||
def set_option(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
return execute_command(name, set_option_cache(name, cs))
|
||||
|
||||
@@ -51,7 +51,7 @@ class Junction(object):
|
||||
return { 'type': self.type, 'id': self.id }
|
||||
|
||||
|
||||
def set_junction(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
def set_junction_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
|
||||
old = Junction(get_junction(name, cs.operations[0]['id']))
|
||||
raw_new = get_junction(name, cs.operations[0]['id'])
|
||||
|
||||
@@ -71,10 +71,14 @@ def set_junction(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
redo_cs = g_update_prefix | new.as_dict()
|
||||
undo_cs = g_update_prefix | old.as_dict()
|
||||
|
||||
return execute_command(name, redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
|
||||
|
||||
def add_junction(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
def set_junction(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
return execute_command(name, set_junction_cache(name, cs))
|
||||
|
||||
|
||||
def add_junction_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
|
||||
new = Junction(cs.operations[0])
|
||||
|
||||
redo_sql = f"insert into _node (id, type) values ({new.f_id}, {new.f_type});"
|
||||
@@ -88,10 +92,14 @@ def add_junction(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
redo_cs = g_add_prefix | new.as_dict()
|
||||
undo_cs = g_delete_prefix | new.as_id_dict()
|
||||
|
||||
return execute_command(name, redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
|
||||
|
||||
def delete_junction(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
def add_junction(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
return execute_command(name, add_junction_cache(name, cs))
|
||||
|
||||
|
||||
def delete_junction_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
|
||||
old = Junction(get_junction(name, cs.operations[0]['id']))
|
||||
|
||||
redo_sql = f"delete from coordinates where node = {old.f_id};"
|
||||
@@ -105,4 +113,8 @@ def delete_junction(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
redo_cs = g_delete_prefix | old.as_id_dict()
|
||||
undo_cs = g_add_prefix | old.as_dict()
|
||||
|
||||
return execute_command(name, redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
|
||||
|
||||
def delete_junction(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
return execute_command(name, delete_junction_cache(name, cs))
|
||||
|
||||
@@ -47,7 +47,7 @@ class Reservoir(object):
|
||||
return { 'type': self.type, 'id': self.id }
|
||||
|
||||
|
||||
def set_reservoir(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
def set_reservoir_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
|
||||
old = Reservoir(get_reservoir(name, cs.operations[0]['id']))
|
||||
raw_new = get_reservoir(name, cs.operations[0]['id'])
|
||||
|
||||
@@ -67,10 +67,14 @@ def set_reservoir(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
redo_cs = g_update_prefix | new.as_dict()
|
||||
undo_cs = g_update_prefix | old.as_dict()
|
||||
|
||||
return execute_command(name, redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
|
||||
|
||||
def add_reservoir(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
def set_reservoir(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
return execute_command(name, set_reservoir_cache(name, cs))
|
||||
|
||||
|
||||
def add_reservoir_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
|
||||
new = Reservoir(cs.operations[0])
|
||||
|
||||
redo_sql = f"insert into _node (id, type) values ({new.f_id}, {new.f_type});"
|
||||
@@ -84,10 +88,14 @@ def add_reservoir(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
redo_cs = g_add_prefix | new.as_dict()
|
||||
undo_cs = g_delete_prefix | new.as_id_dict()
|
||||
|
||||
return execute_command(name, redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
|
||||
|
||||
def delete_reservoir(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
def add_reservoir(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
return execute_command(name, add_reservoir_cache(name, cs))
|
||||
|
||||
|
||||
def delete_reservoir_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
|
||||
old = Reservoir(get_reservoir(name, cs.operations[0]['id']))
|
||||
|
||||
redo_sql = f"delete from coordinates where node = {old.f_id};"
|
||||
@@ -101,4 +109,8 @@ def delete_reservoir(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
redo_cs = g_delete_prefix | old.as_id_dict()
|
||||
undo_cs = g_add_prefix | old.as_dict()
|
||||
|
||||
return execute_command(name, redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
|
||||
|
||||
def delete_reservoir(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
return execute_command(name, delete_reservoir_cache(name, cs))
|
||||
|
||||
@@ -75,7 +75,7 @@ class Tank(object):
|
||||
return { 'type': self.type, 'id': self.id }
|
||||
|
||||
|
||||
def set_tank(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
def set_tank_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
|
||||
old = Tank(get_tank(name, cs.operations[0]['id']))
|
||||
raw_new = get_tank(name, cs.operations[0]['id'])
|
||||
|
||||
@@ -95,10 +95,14 @@ def set_tank(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
redo_cs = g_update_prefix | new.as_dict()
|
||||
undo_cs = g_update_prefix | old.as_dict()
|
||||
|
||||
return execute_command(name, redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
|
||||
|
||||
def add_tank(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
def set_tank(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
return execute_command(name, set_tank_cache(name, cs))
|
||||
|
||||
|
||||
def add_tank_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
|
||||
new = Tank(cs.operations[0])
|
||||
|
||||
redo_sql = f"insert into _node (id, type) values ({new.f_id}, {new.f_type});"
|
||||
@@ -112,10 +116,14 @@ def add_tank(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
redo_cs = g_add_prefix | new.as_dict()
|
||||
undo_cs = g_delete_prefix | new.as_id_dict()
|
||||
|
||||
return execute_command(name, redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
|
||||
|
||||
def delete_tank(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
def add_tank(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
return execute_command(name, add_tank_cache(name, cs))
|
||||
|
||||
|
||||
def delete_tank_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
|
||||
old = Tank(get_tank(name, cs.operations[0]['id']))
|
||||
|
||||
redo_sql = f"delete from coordinates where node = {old.f_id};"
|
||||
@@ -129,4 +137,8 @@ def delete_tank(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
redo_cs = g_delete_prefix | old.as_id_dict()
|
||||
undo_cs = g_add_prefix | old.as_dict()
|
||||
|
||||
return execute_command(name, redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
|
||||
|
||||
def delete_tank(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
return execute_command(name, delete_tank_cache(name, cs))
|
||||
|
||||
@@ -61,7 +61,7 @@ class Pipe(object):
|
||||
return { 'type': self.type, 'id': self.id }
|
||||
|
||||
|
||||
def set_pipe(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
def set_pipe_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
|
||||
old = Pipe(get_pipe(name, cs.operations[0]['id']))
|
||||
raw_new = get_pipe(name, cs.operations[0]['id'])
|
||||
|
||||
@@ -78,10 +78,14 @@ def set_pipe(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
redo_cs = g_update_prefix | new.as_dict()
|
||||
undo_cs = g_update_prefix | old.as_dict()
|
||||
|
||||
return execute_command(name, redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
|
||||
|
||||
def add_pipe(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
def set_pipe(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
return execute_command(name, set_pipe_cache(name, cs))
|
||||
|
||||
|
||||
def add_pipe_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
|
||||
new = Pipe(cs.operations[0])
|
||||
|
||||
redo_sql = f"insert into _link (id, type) values ({new.f_id}, {new.f_type});"
|
||||
@@ -93,10 +97,14 @@ def add_pipe(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
redo_cs = g_add_prefix | new.as_dict()
|
||||
undo_cs = g_delete_prefix | new.as_id_dict()
|
||||
|
||||
return execute_command(name, redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
|
||||
|
||||
def delete_pipe(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
def add_pipe(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
return execute_command(name, add_pipe_cache(name, cs))
|
||||
|
||||
|
||||
def delete_pipe_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
|
||||
old = Pipe(get_pipe(name, cs.operations[0]['id']))
|
||||
|
||||
redo_sql = f"delete from pipes where id = {old.f_id};"
|
||||
@@ -108,4 +116,8 @@ def delete_pipe(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
redo_cs = g_delete_prefix | old.as_id_dict()
|
||||
undo_cs = g_add_prefix | old.as_dict()
|
||||
|
||||
return execute_command(name, redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
|
||||
|
||||
def delete_pipe(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
return execute_command(name, delete_pipe_cache(name, cs))
|
||||
|
||||
@@ -52,7 +52,7 @@ class Pump(object):
|
||||
return { 'type': self.type, 'id': self.id }
|
||||
|
||||
|
||||
def set_pump(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
def set_pump_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
|
||||
old = Pump(get_pump(name, cs.operations[0]['id']))
|
||||
raw_new = get_pump(name, cs.operations[0]['id'])
|
||||
|
||||
@@ -69,10 +69,14 @@ def set_pump(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
redo_cs = g_update_prefix | new.as_dict()
|
||||
undo_cs = g_update_prefix | old.as_dict()
|
||||
|
||||
return execute_command(name, redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
|
||||
|
||||
def add_pump(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
def set_pump(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
return execute_command(name, set_pump_cache(name, cs))
|
||||
|
||||
|
||||
def add_pump_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
|
||||
new = Pump(cs.operations[0])
|
||||
|
||||
redo_sql = f"insert into _link (id, type) values ({new.f_id}, {new.f_type});"
|
||||
@@ -84,10 +88,14 @@ def add_pump(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
redo_cs = g_add_prefix | new.as_dict()
|
||||
undo_cs = g_delete_prefix | new.as_id_dict()
|
||||
|
||||
return execute_command(name, redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
|
||||
|
||||
def delete_pump(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
def add_pump(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
return execute_command(name, add_pump_cache(name, cs))
|
||||
|
||||
|
||||
def delete_pump_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
|
||||
old = Pump(get_pump(name, cs.operations[0]['id']))
|
||||
|
||||
redo_sql = f"delete from pumps where id = {old.f_id};"
|
||||
@@ -99,4 +107,8 @@ def delete_pump(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
redo_cs = g_delete_prefix | old.as_id_dict()
|
||||
undo_cs = g_add_prefix | old.as_dict()
|
||||
|
||||
return execute_command(name, redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
|
||||
|
||||
def delete_pump(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
return execute_command(name, delete_pump_cache(name, cs))
|
||||
|
||||
151
api/s7_valves.py
151
api/s7_valves.py
@@ -60,7 +60,7 @@ class Valve(object):
|
||||
return { 'type': self.type, 'id': self.id }
|
||||
|
||||
|
||||
def set_valve(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
def set_valve_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
|
||||
old = Valve(get_valve(name, cs.operations[0]['id']))
|
||||
raw_new = get_valve(name, cs.operations[0]['id'])
|
||||
|
||||
@@ -77,10 +77,14 @@ def set_valve(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
redo_cs = g_update_prefix | new.as_dict()
|
||||
undo_cs = g_update_prefix | old.as_dict()
|
||||
|
||||
return execute_command(name, redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
|
||||
|
||||
def add_valve(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
def set_valve(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
return execute_command(name, set_valve_cache(name, cs))
|
||||
|
||||
|
||||
def add_valve_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
|
||||
new = Valve(cs.operations[0])
|
||||
|
||||
redo_sql = f"insert into _link (id, type) values ({new.f_id}, {new.f_type});"
|
||||
@@ -92,7 +96,26 @@ def add_valve(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
redo_cs = g_add_prefix | new.as_dict()
|
||||
undo_cs = g_delete_prefix | new.as_id_dict()
|
||||
|
||||
return execute_command(name, redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
|
||||
|
||||
def add_valve(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
return execute_command(name, add_valve_cache(name, cs))
|
||||
|
||||
|
||||
def delete_valve_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
|
||||
old = Valve(get_valve(name, cs.operations[0]['id']))
|
||||
|
||||
redo_sql = f"delete from valves where id = {old.f_id};"
|
||||
redo_sql += f"\ndelete from _link where id = {old.f_id};"
|
||||
|
||||
undo_sql = f"insert into _link (id, type) values ({old.f_id}, {old.f_type});"
|
||||
undo_sql += f"\ninsert into valves (id, node1, node2, diameter, type, setting, minor_loss) values ({old.f_id}, {old.f_node1}, {old.f_node2}, {old.f_diameter}, {old.f_v_type}, {old.f_setting}, {old.f_minor_loss});"
|
||||
|
||||
redo_cs = g_delete_prefix | old.as_id_dict()
|
||||
undo_cs = g_add_prefix | old.as_dict()
|
||||
|
||||
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
|
||||
|
||||
def delete_valve(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
@@ -107,122 +130,4 @@ def delete_valve(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
redo_cs = g_delete_prefix | old.as_id_dict()
|
||||
undo_cs = g_add_prefix | old.as_dict()
|
||||
|
||||
return execute_command(name, redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
|
||||
|
||||
'''
|
||||
schema: dict[str, dict[str, Any]] = {}
|
||||
|
||||
|
||||
def get_valve_schema(name: str) -> dict[str, dict[str, Any]]:
|
||||
return schema
|
||||
|
||||
|
||||
def _query_valve(name: str, id: str) -> Row | None:
|
||||
return read(name, f"select * from valves where id = '{id}'")
|
||||
|
||||
|
||||
def _get_valve_node1(name: str, id: str) -> str | None:
|
||||
row = _query_valve(name, id)
|
||||
return row['node1'] if row != None else None
|
||||
|
||||
|
||||
def _get_valve_node2(name: str, id: str) -> str | None:
|
||||
row = _query_valve(name, id)
|
||||
return row['node2'] if row != None else None
|
||||
|
||||
|
||||
def add_valve(name: str, id: str, node1: str, node2: str, diameter: float = 0, type: str = VALVES_TYPE_PRV, setting: float = 0, minor_loss: float = 0) -> ChangeSet:
|
||||
if is_valve(name, id):
|
||||
return ChangeSet()
|
||||
if not is_node(name, node1):
|
||||
return ChangeSet()
|
||||
if not is_node(name, node2):
|
||||
return ChangeSet()
|
||||
if node1 == node2:
|
||||
return ChangeSet()
|
||||
if type != VALVES_TYPE_PRV and type != VALVES_TYPE_PSV and type != VALVES_TYPE_PBV and type != VALVES_TYPE_FCV and type != VALVES_TYPE_TCV and type != VALVES_TYPE_GPV:
|
||||
return ChangeSet()
|
||||
|
||||
sql = f"insert into _link (id, type) values ('{id}', '{VALVE}');"
|
||||
sql += f"\ninsert into valves (id, node1, node2, diameter, type, setting, minor_loss) values ('{id}', '{node1}', '{node2}', {diameter}, '{type}', {setting}, {minor_loss});"
|
||||
|
||||
undo = f"delete from valves where id = ''{id}'';"
|
||||
undo += f"\ndelete from _link where id = ''{id}'';"
|
||||
|
||||
write(name, sql)
|
||||
add_operation(name, sql.replace("'", "''"), undo, 'add_valve', API_ADD, VALVE, id)
|
||||
return get_current_change_set(name)
|
||||
|
||||
|
||||
def get_valve(name: str, id: str) -> dict[str, Any] | None:
|
||||
row = _query_valve(name, id)
|
||||
if row == None:
|
||||
return None
|
||||
|
||||
ps: dict[str, str] = {}
|
||||
ps['id'] = id
|
||||
ps['node1'] = row['node1']
|
||||
ps['node2'] = row['node2']
|
||||
ps['diameter'] = float(row['diameter'])
|
||||
ps['type'] = row['type']
|
||||
ps['setting'] = float(row['setting'])
|
||||
ps['minor_loss'] = float(row['minor_loss'])
|
||||
return ps
|
||||
|
||||
|
||||
def set_valve(name: str, id: str, properties: dict[str, Any]) -> ChangeSet:
|
||||
if not is_valve(name, id):
|
||||
return ChangeSet()
|
||||
if 'node1' in properties:
|
||||
if not is_node(name, properties['node1']) or _get_valve_node2(name, id) == properties['node1']:
|
||||
return ChangeSet()
|
||||
if 'node2' in properties:
|
||||
if not is_node(name, properties['node2']) or _get_valve_node1(name, id) == properties['node2']:
|
||||
return ChangeSet()
|
||||
if 'node1' in properties and 'node2' in properties:
|
||||
if properties['node1'] == properties['node2']:
|
||||
return ChangeSet()
|
||||
if 'type' in properties:
|
||||
t = properties['type']
|
||||
if t != VALVES_TYPE_PRV and t != VALVES_TYPE_PSV and t != VALVES_TYPE_PBV and t != VALVES_TYPE_FCV and t != VALVES_TYPE_TCV and t != VALVES_TYPE_GPV:
|
||||
return ChangeSet()
|
||||
|
||||
old = Serialize(get_valve(name, id), schema).to_storage()
|
||||
|
||||
new = get_valve(name, id)
|
||||
ps: list[str] = []
|
||||
for key in properties:
|
||||
if key in schema and schema[key]['readonly'] == False:
|
||||
new[key] = properties[key]
|
||||
ps.append(key)
|
||||
new = Serialize(new, schema).to_execution()
|
||||
|
||||
sql = f"update valves set node1 = {new['node1']}, node2 = {new['node2']}, \
|
||||
diameter = {new['diameter']}, type = {new['type']}, setting = {new['setting']}, minor_loss = {new['minor_loss']} where id = '{id}';"
|
||||
undo = f"update valves set node1 = {old['node1']}, node2 = {old['node2']}, \
|
||||
diameter = {old['diameter']}, type = {old['type']}, setting = {old['setting']}, minor_loss = {old['minor_loss']} where id = ''{id}'';"
|
||||
|
||||
write(name, sql)
|
||||
add_operation(name, sql.replace("'", "''"), undo, 'set_valve', API_UPDATE, VALVE, id, ps)
|
||||
return get_current_change_set(name)
|
||||
|
||||
|
||||
def delete_valve(name: str, id: str) -> ChangeSet:
|
||||
row = get_valve(name, id)
|
||||
if row == None:
|
||||
return ChangeSet()
|
||||
|
||||
old = Serialize(get_valve(name, id), schema).to_storage()
|
||||
|
||||
sql = f"delete from valves where id = '{id}';"
|
||||
sql += f"\ndelete from _link where id = '{id}';"
|
||||
|
||||
undo = f"insert into _link (id, type) values (''{id}'', ''{VALVE}'');"
|
||||
undo += f"\ninsert into valves (id, node1, node2, diameter, type, setting, minor_loss) \
|
||||
values (''{id}'', {old['node1']}, {old['node2']}, {old['diameter']}, {old['type']}, {old['setting']}, {old['minor_loss']});"
|
||||
|
||||
write(name, sql)
|
||||
add_operation(name, sql.replace("'", "''"), undo, 'delete_valve', API_DELETE, VALVE, id)
|
||||
return get_current_change_set(name)
|
||||
'''
|
||||
return execute_command(name, delete_valve_cache(name, cs))
|
||||
|
||||
@@ -20,8 +20,8 @@ def get_demand(name: str, junction: str) -> dict[str, Any]:
|
||||
ds.append(d)
|
||||
return { 'junction': junction, 'demands': ds }
|
||||
|
||||
# { 'operation': 'update', 'type': 'demand', 'junction': 'j1', 'demands': [{'demand': 0.0, 'patten': None, 'category': None}] }
|
||||
def set_demand(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
|
||||
def set_demand_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
|
||||
junction = cs.operations[0]['junction']
|
||||
old = get_demand(name, junction)
|
||||
new = { 'junction': junction, 'demands': [] }
|
||||
@@ -53,4 +53,8 @@ def set_demand(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
redo_cs = g_update_prefix | { 'type': 'demand' } | new
|
||||
undo_cs = g_update_prefix | { 'type': 'demand' } | old
|
||||
|
||||
return execute_command(name, redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
|
||||
|
||||
def set_demand(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
return execute_command(name, set_demand_cache(name, cs))
|
||||
|
||||
Reference in New Issue
Block a user