Fix module in terms of python style

This commit is contained in:
wqy
2022-09-02 18:33:33 +08:00
parent 4474f12d2c
commit bed5741558
11 changed files with 214 additions and 185 deletions

28
api/project.py Normal file
View File

@@ -0,0 +1,28 @@
import psycopg as pg
from api.connection import g_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]