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

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]