Multiple operation into a api

This commit is contained in:
wqy
2022-09-02 20:28:03 +08:00
parent 49bea820b8
commit 71c454c8cf
2 changed files with 32 additions and 17 deletions

View File

@@ -14,23 +14,32 @@ PIPE = "PIPE"
PUMP = "PUMP" PUMP = "PUMP"
VALVE = "VALVE" VALVE = "VALVE"
nodeTables = { JUNCTION: "junctions", RESERVOIR: "reservoirs", TANK: "tanks" }
linkTables = { PIPE: "pipes", PUMP: "pumps", VALVE: "valves" }
typeTales = { _NODE: nodeTables, _LINK: linkTables }
# add # add
def _add_id_type(name: str, id: str, type: str, table: str) -> None: def _add_id_type(name: str, id: str, type: str, base_type: str) -> None:
with conn[name].cursor() as cur: with conn[name].cursor() as cur:
sql = f"insert into {table} (id, type) values ('{id}', '{type}')" sql = f"insert into {base_type} (id, type) values ('{id}', '{type}'); insert into {typeTales[base_type][type]} (id) values ('{id}');"
if base_type == _NODE:
sql += f" insert into coordinates (node) values ('{id}');"
cur.execute(sql) cur.execute(sql)
redo = sql.replace("'", '"') redo = sql.replace("'", '"')
undo = f'delete from {table} where id = "{id}"' undo = ""
if base_type == _NODE:
undo = f'delete from coordinates where node = "{id}"; '
undo += f'delete from {typeTales[base_type][type]} where id = "{id}"; delete from {base_type} where id = "{id}";'
add_operation(name, redo, undo) add_operation(name, redo, undo)
def _add_id(name: str, id: str, table: str) -> None: def _add_id(name: str, id: str, base_type: str) -> None:
with conn[name].cursor() as cur: with conn[name].cursor() as cur:
sql = f"insert into {table} (id) values ('{id}')" sql = f"insert into {base_type} (id) values ('{id}')"
cur.execute(sql) cur.execute(sql)
redo = sql.replace("'", '"') redo = sql.replace("'", '"')
undo = f'delete from {table} where id = "{id}"' undo = f'delete from {base_type} where id = "{id}"'
add_operation(name, redo, undo) add_operation(name, redo, undo)
def add_node(name: str, id: str, type: str) -> None: def add_node(name: str, id: str, type: str) -> None:
@@ -48,9 +57,9 @@ def add_pattern(name: str, id: str) -> None:
# have # have
def _have_impl(name: str, id: str, table: str) -> bool: def _have_impl(name: str, id: str, base_type: str) -> bool:
with conn[name].cursor() as cur: with conn[name].cursor() as cur:
cur.execute(f"select * from {table} where id = '{id}'") cur.execute(f"select * from {base_type} where id = '{id}'")
return cur.rowcount > 0 return cur.rowcount > 0
def have_node(name: str, id: str) -> bool: def have_node(name: str, id: str) -> bool:
@@ -67,9 +76,9 @@ def have_pattern(name: str, id: str) -> bool:
# get # get
def _get_impl(name: str, id: str, table: str) -> dict[str, str]: def _get_impl(name: str, id: str, base_type: str) -> dict[str, str]:
with conn[name].cursor(row_factory=dict_row) as cur: with conn[name].cursor(row_factory=dict_row) as cur:
cur.execute(f"select * from {table} where id = '{id}'") cur.execute(f"select * from {base_type} where id = '{id}'")
if cur.rowcount > 0: if cur.rowcount > 0:
return cur.fetchone() return cur.fetchone()
else: else:
@@ -108,20 +117,26 @@ def get_pattern(name: str, id: str) -> dict[str, str]:
# delete # delete
def _delete_id_type(name: str, id: str, type: str, table: str) -> None: def _delete_id_type(name: str, id: str, type: str, base_type: str) -> None:
with conn[name].cursor() as cur: with conn[name].cursor() as cur:
sql = f"delete from {table} where id = '{id}'" sql = ""
if base_type == _NODE:
sql = f"delete from coordinates where node = '{id}'; "
sql += f"delete from {typeTales[base_type][type]} where id = '{id}'; delete from {base_type} where id = '{id}';"
cur.execute(sql) cur.execute(sql)
redo = sql.replace("'", '"') redo = sql.replace("'", '"')
undo = f'insert into {table} (id, type) values ("{id}", "{type}")' undo = f'insert into {base_type} (id, type) values ("{id}", "{type}"); insert into {typeTales[base_type][type]} (id) values ("{id}");'
if base_type == _NODE:
undo += f' insert into coordinates (node) values ("{id}");'
add_operation(name, redo, undo) add_operation(name, redo, undo)
def _delete_id(name:str, id: str, table: str) -> None: def _delete_id(name:str, id: str, base_type: str) -> None:
with conn[name].cursor() as cur: with conn[name].cursor() as cur:
sql = f"delete from {table} where id = '{id}'" sql = f"delete from {base_type} where id = '{id}'"
cur.execute(sql) cur.execute(sql)
redo = sql.replace("'", '"') redo = sql.replace("'", '"')
undo = f'insert into {table} (id) values ("{id}")' undo = f'insert into {base_type} (id) values ("{id}")'
add_operation(name, redo, undo) add_operation(name, redo, undo)
def delete_node(name: str, id: str) -> None: def delete_node(name: str, id: str) -> None:

View File

@@ -3,7 +3,7 @@
CREATE TABLE JUNCTIONS CREATE TABLE JUNCTIONS
( (
ID VARCHAR(32) PRIMARY KEY REFERENCES _NODE(ID) ID VARCHAR(32) PRIMARY KEY REFERENCES _NODE(ID)
, Elevation NUMERIC NOT NULL , Elevation NUMERIC NOT NULL DEFAULT(0.0)
, Demand NUMERIC , Demand NUMERIC
, Pattern VARCHAR(32) REFERENCES _PATTERN(ID) , Pattern VARCHAR(32) REFERENCES _PATTERN(ID)
); );