Add base api

This commit is contained in:
wqy
2022-09-01 22:03:09 +08:00
parent 5ac03bb9bf
commit a177edbfcb
4 changed files with 116 additions and 0 deletions

71
api/_0_base.py Normal file
View 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)

3
api/_connection.py Normal file
View File

@@ -0,0 +1,3 @@
import psycopg as pg
_conn_dict : dict[str, pg.Connection] = {}

28
api/_project.py Normal file
View File

@@ -0,0 +1,28 @@
import psycopg as pg
from _connection import _conn_dict as conn
def have_project(name: str) -> bool:
with pg.connect(conninfo="dbname=postgres", autocommit=True) as conn:
with conn.cursor() as cur:
cur.execute(f"select * from pg_database where datname = '{name}'")
return cur.rowcount > 0
def create_project(name: str) -> None:
with pg.connect(conninfo="dbname=postgres", autocommit=True) as conn:
with conn.cursor() as cur:
cur.execute(f"create database {name} with template = project")
def delete_project(name: str) -> None:
with pg.connect(conninfo="dbname=postgres", autocommit=True) as conn:
with conn.cursor() as cur:
cur.execute(f"drop database {name}")
def open_project(name: str) -> None:
conn[name] = pg.connect(conninfo=f"dbname={name}", autocommit=True)
def is_project_open(name: str) -> bool:
return name in conn
def close_project(name: str) -> None:
conn[name].close()
del conn[name]

14
api/test.py Normal file
View File

@@ -0,0 +1,14 @@
from _project import *
from _0_base import *
open_project("net")
add_node("net", "n-1", JUNCTION)
if have_node("net", "n-1"):
delete_node("net", "n-1")
print(have_node("net", "n-1"))
open_project("net")
add_link("net", "l-1", PIPE)
if have_link("net", "l-1"):
delete_link("net", "l-1")
print(have_link("net", "l-1"))