Support compress batch commands as one command
This commit is contained in:
115
api/command.py
115
api/command.py
@@ -103,3 +103,118 @@ def execute_batch_commands(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
pass
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def cache_add_command(name: str, cs: ChangeSet) -> SqlChangeSet | None:
|
||||
type = cs.operations[0]['type']
|
||||
|
||||
if type == JUNCTION:
|
||||
return add_junction_cache(name, cs)
|
||||
elif type == RESERVOIR:
|
||||
return add_reservoir_cache(name, cs)
|
||||
elif type == TANK:
|
||||
return add_tank_cache(name, cs)
|
||||
elif type == PIPE:
|
||||
return add_pipe_cache(name, cs)
|
||||
elif type == PUMP:
|
||||
return add_pump_cache(name, cs)
|
||||
elif type == VALVE:
|
||||
return add_valve_cache(name, cs)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def cache_update_command(name: str, cs: ChangeSet) -> SqlChangeSet | None:
|
||||
type = cs.operations[0]['type']
|
||||
|
||||
if type == 'title':
|
||||
return set_title_cache(name, cs)
|
||||
if type == JUNCTION:
|
||||
return set_junction_cache(name, cs)
|
||||
elif type == RESERVOIR:
|
||||
return set_reservoir_cache(name, cs)
|
||||
elif type == TANK:
|
||||
return set_tank_cache(name, cs)
|
||||
elif type == PIPE:
|
||||
return set_pipe_cache(name, cs)
|
||||
elif type == PUMP:
|
||||
return set_pump_cache(name, cs)
|
||||
elif type == VALVE:
|
||||
return set_valve_cache(name, cs)
|
||||
elif type == 'demand':
|
||||
return set_demand_cache(name, cs)
|
||||
elif type == 'status':
|
||||
return set_status_cache(name, cs)
|
||||
elif type == PATTERN:
|
||||
return set_pattern_cache(name, cs)
|
||||
elif type == CURVE:
|
||||
return set_curve_cache(name, cs)
|
||||
elif type == 'emitter':
|
||||
return set_emitter_cache(name, cs)
|
||||
elif type == 'time':
|
||||
return set_time_cache(name, cs)
|
||||
elif type == 'option':
|
||||
return set_option_cache(name, cs)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def cache_delete_command(name: str, cs: ChangeSet) -> SqlChangeSet | None:
|
||||
type = cs.operations[0]['type']
|
||||
|
||||
if type == JUNCTION:
|
||||
return delete_junction_cache(name, cs)
|
||||
elif type == RESERVOIR:
|
||||
return delete_reservoir_cache(name, cs)
|
||||
elif type == TANK:
|
||||
return delete_tank_cache(name, cs)
|
||||
elif type == PIPE:
|
||||
return delete_pipe_cache(name, cs)
|
||||
elif type == PUMP:
|
||||
return delete_pump_cache(name, cs)
|
||||
elif type == VALVE:
|
||||
return delete_valve_cache(name, cs)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def execute_batch_command(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
redo_sql_s = []
|
||||
undo_sql_s = []
|
||||
redo_cs_s = []
|
||||
undo_cs_s = []
|
||||
|
||||
try:
|
||||
for op in cs.operations:
|
||||
operation = op['operation']
|
||||
|
||||
r = None
|
||||
|
||||
if operation == API_ADD:
|
||||
r = cache_add_command(name, ChangeSet(op))
|
||||
elif operation == API_UPDATE:
|
||||
r = cache_update_command(name, ChangeSet(op))
|
||||
elif operation == API_DELETE:
|
||||
r = cache_delete_command(name, ChangeSet(op))
|
||||
|
||||
if r == None:
|
||||
return ChangeSet()
|
||||
|
||||
redo_sql_s.append(r.redo_sql)
|
||||
undo_sql_s.append(r.undo_sql)
|
||||
redo_cs_s.append(r.redo_cs)
|
||||
undo_cs_s.append(r.undo_cs)
|
||||
except:
|
||||
pass
|
||||
|
||||
redo_sql = '\n'.join(redo_sql_s)
|
||||
|
||||
undo_sql_s.reverse()
|
||||
undo_sql = '\n'.join(undo_sql_s)
|
||||
|
||||
undo_cs_s.reverse()
|
||||
|
||||
try:
|
||||
return execute_batch(name, redo_sql, undo_sql, redo_cs_s, undo_cs_s)
|
||||
except:
|
||||
return ChangeSet()
|
||||
|
||||
Reference in New Issue
Block a user