34 lines
1.1 KiB
Python
34 lines
1.1 KiB
Python
import psycopg as pg
|
|
from .connection import g_conn_dict as conn
|
|
|
|
# no undo/redo
|
|
|
|
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 copy_project(source: str, new: str) -> None:
|
|
with pg.connect(conninfo="dbname=postgres", autocommit=True) as conn:
|
|
with conn.cursor() as cur:
|
|
cur.execute(f"create database {new} with template = {source}")
|
|
|
|
def create_project(name: str) -> None:
|
|
return copy_project('project', name)
|
|
|
|
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]
|