Add base api
This commit is contained in:
71
api/_0_base.py
Normal file
71
api/_0_base.py
Normal file
@@ -0,0 +1,71 @@
|
||||
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)
|
||||
Reference in New Issue
Block a user