Merge branch 'master' into region

This commit is contained in:
WQY\qiong
2023-03-31 15:00:30 +08:00
13 changed files with 605 additions and 126 deletions

View File

@@ -16,10 +16,10 @@ from .database import update_snapshot, update_snapshot_for_current_operation
from .database import delete_snapshot, delete_snapshot_by_operation
from .database import get_operation_by_snapshot, get_snapshot_by_operation
from .database import pick_snapshot
from .database import pick_operation, sync_with_server, get_restore_operation, set_restore_operation, set_restore_operation_to_current
from .database import pick_operation, sync_with_server
from .database import get_restore_operation, set_restore_operation, set_restore_operation_to_current, restore
from .batch_cmd import execute_batch_command
from .batch_cmds import execute_batch_commands
from .batch_cmds import execute_batch_commands, execute_batch_command
from .s0_base import JUNCTION, RESERVOIR, TANK, PIPE, PUMP, VALVE, PATTERN, CURVE
from .s0_base import is_node, is_junction, is_reservoir, is_tank

View File

@@ -1,5 +1,6 @@
from typing import Any
from .sections import *
from .database import API_ADD, API_UPDATE, API_DELETE, ChangeSet
from .database import API_ADD, API_UPDATE, API_DELETE, ChangeSet, write, read, read_all, get_current_operation
from .s1_title import set_title
from .s2_junctions import set_junction, add_junction, delete_junction
from .s3_reservoirs import set_reservoir, add_reservoir, delete_reservoir
@@ -290,7 +291,71 @@ def execute_batch_commands(name: str, cs: ChangeSet) -> ChangeSet:
elif operation == API_DELETE:
result.merge(execute_delete_command(name, ChangeSet(op)))
except:
print(f'ERROR: Fail to execute {todo}!')
pass
print(f'ERROR: Fail to execute {todo}')
return result
def execute_batch_command(name: str, cs: ChangeSet) -> ChangeSet:
write(name, 'delete from batch_operation where id > 0')
write(name, "update operation_table set option = 'batch_operation' where option = 'operation'")
# for delete, generate cascade command
new_cs = ChangeSet()
for op in cs.operations:
if op['operation'] == API_DELETE:
new_cs.merge(del_cascade_cmd(name, ChangeSet(op)))
else:
new_cs.merge(ChangeSet(op))
result = ChangeSet()
todo = {}
try:
for op in new_cs.operations:
todo = op
operation = op['operation']
if operation == API_ADD:
result.merge(execute_add_command(name, ChangeSet(op)))
elif operation == API_UPDATE:
result.merge(execute_update_command(name, ChangeSet(op)))
elif operation == API_DELETE:
result.merge(execute_delete_command(name, ChangeSet(op)))
except:
print(f'ERROR: Fail to execute {todo}!')
count = read(name, 'select count(*) as count from batch_operation')['count']
if count == 1:
write(name, 'delete from batch_operation where id > 0')
write(name, "update operation_table set option = 'operation' where option = 'batch_operation'")
return ChangeSet()
redo_list: list[str] = []
redo_cs_list: list[dict[str, Any]] = []
redo_rows = read_all(name, 'select redo, redo_cs from batch_operation where id > 0 order by id asc')
for row in redo_rows:
redo_list.append(row['redo'])
redo_cs_list += eval(row['redo_cs'])
undo_list: list[str] = []
undo_cs_list: list[dict[str, Any]] = []
undo_rows = read_all(name, 'select undo, undo_cs from batch_operation where id > 0 order by id desc')
for row in undo_rows:
undo_list.append(row['undo'])
undo_cs_list += eval(row['undo_cs'])
redo = '\n'.join(redo_list).replace("'", "''")
redo_cs = str(redo_cs_list).replace("'", "''")
undo = '\n'.join(undo_list).replace("'", "''")
undo_cs = str(undo_cs_list).replace("'", "''")
parent = get_current_operation(name)
write(name, f"insert into operation (id, redo, undo, parent, redo_cs, undo_cs) values (default, '{redo}', '{undo}', {parent}, '{redo_cs}', '{undo_cs}')")
current = read(name, 'select max(id) as id from operation')['id']
write(name, f"update current_operation set id = {current}")
write(name, 'delete from batch_operation where id > 0')
write(name, "update operation_table set option = 'operation' where option = 'batch_operation'")
return result

View File

@@ -113,6 +113,8 @@ def get_current_operation(name: str) -> int:
def execute_command(name: str, command: DbChangeSet) -> ChangeSet:
op_table = read(name, "select * from operation_table")['option']
write(name, command.redo_sql)
parent = get_current_operation(name)
@@ -120,10 +122,11 @@ def execute_command(name: str, command: DbChangeSet) -> ChangeSet:
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}')")
write(name, f"insert into {op_table} (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}")
if op_table == 'operation':
current = read(name, 'select max(id) as id from operation')['id']
write(name, f"update current_operation set id = {current}")
return ChangeSet.from_list(command.redo_cs)
@@ -339,3 +342,8 @@ def set_restore_operation(name: str, operation: int) -> None:
def set_restore_operation_to_current(name: str) -> None:
return set_restore_operation(name, get_current_operation(name))
def restore(name: str, discard: bool) -> ChangeSet:
op = get_restore_operation(name)
return pick_operation(name, op, discard)