From a177edbfcbe8f35dd0b53e7b2e28989d34a90e22 Mon Sep 17 00:00:00 2001 From: wqy Date: Thu, 1 Sep 2022 22:03:09 +0800 Subject: [PATCH] Add base api --- api/_0_base.py | 71 ++++++++++++++++++++++++++++++++++++++++++++++ api/_connection.py | 3 ++ api/_project.py | 28 ++++++++++++++++++ api/test.py | 14 +++++++++ 4 files changed, 116 insertions(+) create mode 100644 api/_0_base.py create mode 100644 api/_connection.py create mode 100644 api/_project.py create mode 100644 api/test.py diff --git a/api/_0_base.py b/api/_0_base.py new file mode 100644 index 0000000..5e3f5b0 --- /dev/null +++ b/api/_0_base.py @@ -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) diff --git a/api/_connection.py b/api/_connection.py new file mode 100644 index 0000000..fa81f21 --- /dev/null +++ b/api/_connection.py @@ -0,0 +1,3 @@ +import psycopg as pg + +_conn_dict : dict[str, pg.Connection] = {} \ No newline at end of file diff --git a/api/_project.py b/api/_project.py new file mode 100644 index 0000000..d0ef3c9 --- /dev/null +++ b/api/_project.py @@ -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] diff --git a/api/test.py b/api/test.py new file mode 100644 index 0000000..30ceed0 --- /dev/null +++ b/api/test.py @@ -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")) \ No newline at end of file