Files
TJWaterServer/api/_0_base.py
2022-09-01 22:03:09 +08:00

72 lines
1.9 KiB
Python

from _connection import _conn_dict as conn
_NODE = "_NODE"
_LINK = "_LINK"
_CURVE = "_CURVE"
_PATTERN = "_PATTERN"
JUNCTION = "JUNCTION"
RESERVOIR = "RESERVOIR"
TANK = "TANK"
PIPE = "PIPE"
PUMP = "PUMP"
VALVE = "VALVE"
# add
def add_node(name: str, id: str, type: str) -> None:
with conn[name].cursor() as cur:
cur.execute(f"INSERT INTO _NODE (ID, Type) VALUES ('{id}', '{type}')")
def add_link(name: str, id: str, type: str) -> None:
with conn[name].cursor() as cur:
cur.execute(f"INSERT INTO _LINK (ID, Type) VALUES ('{id}', '{type}')")
def add_curve(name: str, id: str) -> None:
with conn[name].cursor() as cur:
cur.execute(f"INSERT INTO _CURVE (ID) VALUES ('{id}')")
def add_pattern(name: str, id: str) -> None:
with conn[name].cursor() as cur:
cur.execute(f"INSERT INTO _PATTERN (ID) VALUES ('{id}')")
# have
def _have_impl(name: str, id: str, table: str) -> bool:
with conn[name].cursor() as cur:
cur.execute(f"SELECT * FROM {table} WHERE ID = '{id}'")
return cur.rowcount > 0
def have_node(name: str, id: str) -> bool:
return _have_impl(name, id, _NODE)
def have_link(name: str, id: str) -> bool:
return _have_impl(name, id, _LINK)
def have_curve(name: str, id: str) -> bool:
return _have_impl(name, id, _CURVE)
def have_pattern(name: str, id: str) -> bool:
return _have_impl(name, id, _PATTERN)
# delete
def _delete_impl(name: str, id: str, table: str) -> None:
with conn[name].cursor() as cur:
cur.execute(f"DELETE FROM {table} WHERE ID = '{id}'")
def delete_node(name: str, id: str) -> None:
return _delete_impl(name, id, _NODE)
def delete_link(name: str, id: str) -> None:
return _delete_impl(name, id, _LINK)
def delete_curve(name: str, id: str) -> None:
return _delete_impl(name, id, _CURVE)
def delete_pattern(name: str, id: str) -> None:
return _delete_impl(name, id, _PATTERN)