Refine project operation
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
from .project import list_project, have_project, create_project, delete_project, clean_project
|
||||
from .project import is_project_open, get_project_open_count, open_project, close_project
|
||||
from .project import is_project_open, open_project, close_project
|
||||
from .project import copy_project
|
||||
|
||||
from .inp_in import read_inp, import_inp
|
||||
|
||||
@@ -4,10 +4,9 @@ from .connection import g_conn_dict as conn
|
||||
|
||||
# no undo/redo
|
||||
|
||||
_project_open_count: dict[str, int] = {}
|
||||
|
||||
_server_databases = ['template0', 'template1', 'postgres', 'project']
|
||||
|
||||
|
||||
def list_project() -> list[str]:
|
||||
ps = []
|
||||
with pg.connect(conninfo="dbname=postgres host=127.0.0.1", autocommit=True) as conn:
|
||||
@@ -16,25 +15,31 @@ def list_project() -> list[str]:
|
||||
ps.append(p['datname'])
|
||||
return ps
|
||||
|
||||
|
||||
def have_project(name: str) -> bool:
|
||||
with pg.connect(conninfo="dbname=postgres host=127.0.0.1", 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 host=127.0.0.1", 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 host=127.0.0.1", autocommit=True) as conn:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(f"select pg_terminate_backend(pid) from pg_stat_activity where datname = '{name}'")
|
||||
cur.execute(f"drop database {name}")
|
||||
|
||||
|
||||
def clean_project(excluded: list[str] = []) -> None:
|
||||
projects = list_project()
|
||||
with pg.connect(conninfo="dbname=postgres host=127.0.0.1", autocommit=True) as conn:
|
||||
@@ -50,25 +55,17 @@ def clean_project(excluded: list[str] = []) -> None:
|
||||
cur.execute(f"select pg_terminate_backend(pid) from pg_stat_activity where datname = '{project}'")
|
||||
cur.execute(f"drop database {project}")
|
||||
|
||||
def open_project(name: str) -> None:
|
||||
if name not in _project_open_count:
|
||||
conn[name] = pg.connect(conninfo=f"dbname={name} host=127.0.0.1", autocommit=True)
|
||||
_project_open_count[name] = 0
|
||||
|
||||
_project_open_count[name] += 1
|
||||
def open_project(name: str) -> None:
|
||||
if name not in conn:
|
||||
conn[name] = pg.connect(conninfo=f"dbname={name} host=127.0.0.1", autocommit=True)
|
||||
|
||||
|
||||
def is_project_open(name: str) -> bool:
|
||||
return name in conn
|
||||
|
||||
def get_project_open_count(name: str) -> int:
|
||||
if is_project_open(name):
|
||||
return _project_open_count[name]
|
||||
return 0
|
||||
|
||||
def close_project(name: str) -> None:
|
||||
if name in _project_open_count:
|
||||
_project_open_count[name] -= 1
|
||||
if _project_open_count[name] == 0:
|
||||
conn[name].close()
|
||||
del conn[name]
|
||||
del _project_open_count[name]
|
||||
if name in conn:
|
||||
conn[name].close()
|
||||
del conn[name]
|
||||
|
||||
Reference in New Issue
Block a user