Accept Merge Request #32: (api -> master)
Merge Request: Add project ref count to allow multiple connections Created By: @王琼钰 Accepted By: @王琼钰 URL: https://tjwater.coding.net/p/tjwatercloud/d/TJWaterServer/git/merge/32
This commit is contained in:
@@ -4,6 +4,7 @@ from .project import copy_project
|
||||
|
||||
from .change_set import ChangeSet
|
||||
|
||||
from .operation import get_current_operation
|
||||
from .operation import execute_undo as undo
|
||||
from .operation import execute_redo as redo
|
||||
from .operation import have_snapshot, take_snapshot, pick_snapshot
|
||||
|
||||
@@ -29,7 +29,7 @@ def _get_parents(name: str, id: int) -> list[int]:
|
||||
ids.append(int(cur.fetchone()['parent']))
|
||||
return ids
|
||||
|
||||
def _get_current_operation(name: str) -> int:
|
||||
def get_current_operation(name: str) -> int:
|
||||
with conn[name].cursor(row_factory=dict_row) as cur:
|
||||
cur.execute(f"select id from current_operation")
|
||||
return int(cur.fetchone()['id'])
|
||||
@@ -40,7 +40,7 @@ def _update_current_operation(name: str, old_id: int, id: int) -> None:
|
||||
|
||||
def _add_redo_undo(name: str, redo: str, undo: str) -> int:
|
||||
with conn[name].cursor(row_factory=dict_row) as cur:
|
||||
parent = _get_current_operation(name)
|
||||
parent = get_current_operation(name)
|
||||
cur.execute(f"insert into operation (id, redo, undo, parent) values (default, '{redo}', '{undo}', {parent})")
|
||||
cur.execute("select max(id) from operation")
|
||||
return int(cur.fetchone()['max'])
|
||||
@@ -73,11 +73,11 @@ def _execute(name: str, sql: str) -> None:
|
||||
|
||||
def add_operation(name: str, redo: str, undo: str) -> None:
|
||||
curr = _add_redo_undo(name, redo, undo)
|
||||
old = _get_current_operation(name)
|
||||
old = get_current_operation(name)
|
||||
_update_current_operation(name, old, curr)
|
||||
|
||||
def execute_undo(name: str, discard: bool = False) -> None:
|
||||
curr = _get_current_operation(name)
|
||||
curr = get_current_operation(name)
|
||||
|
||||
# transaction control
|
||||
if have_transaction(name):
|
||||
@@ -106,7 +106,7 @@ def execute_undo(name: str, discard: bool = False) -> None:
|
||||
_remove_operation(name, curr)
|
||||
|
||||
def execute_redo(name: str) -> None:
|
||||
curr = _get_current_operation(name)
|
||||
curr = get_current_operation(name)
|
||||
redoChild = _query_redo_child(name, curr)
|
||||
if redoChild == None:
|
||||
print("nothing to redo!")
|
||||
@@ -132,7 +132,7 @@ def take_snapshot(name: str, tag: str) -> None:
|
||||
print('Non empty tag is expected!')
|
||||
return
|
||||
|
||||
curr = _get_current_operation(name)
|
||||
curr = get_current_operation(name)
|
||||
|
||||
with conn[name].cursor() as cur:
|
||||
cur.execute(f"insert into snapshot_operation (id, tag) values ({curr}, '{tag}')")
|
||||
@@ -146,7 +146,7 @@ def pick_snapshot(name: str, tag: str) -> None:
|
||||
print('No such snapshot!')
|
||||
return
|
||||
|
||||
curr = _get_current_operation(name)
|
||||
curr = get_current_operation(name)
|
||||
curr_parents = _get_parents(name, curr)
|
||||
|
||||
with conn[name].cursor(row_factory=dict_row) as cur:
|
||||
@@ -192,7 +192,7 @@ def start_transaction(name: str, strict: bool = False) -> None:
|
||||
print("Only support single transaction now, please commit/abort current transaction!")
|
||||
return
|
||||
|
||||
curr = _get_current_operation(name)
|
||||
curr = get_current_operation(name)
|
||||
|
||||
with conn[name].cursor() as cur:
|
||||
cur.execute(f"insert into transaction_operation (id, strict) values ({curr}, {strict});")
|
||||
@@ -211,7 +211,7 @@ def abort_transaction(name: str) -> None:
|
||||
|
||||
tran = _get_current_transaction_id(name)
|
||||
|
||||
curr = _get_current_operation(name)
|
||||
curr = get_current_operation(name)
|
||||
curr_parents = _get_parents(name, curr)
|
||||
|
||||
for i in range(curr_parents.index(tran)):
|
||||
|
||||
@@ -3,6 +3,8 @@ from .connection import g_conn_dict as conn
|
||||
|
||||
# no undo/redo
|
||||
|
||||
_project_open_count: dict[str, int] = {}
|
||||
|
||||
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:
|
||||
@@ -23,11 +25,19 @@ def delete_project(name: str) -> None:
|
||||
cur.execute(f"drop database {name}")
|
||||
|
||||
def open_project(name: str) -> None:
|
||||
conn[name] = pg.connect(conninfo=f"dbname={name} host=127.0.0.1", autocommit=True)
|
||||
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 is_project_open(name: str) -> bool:
|
||||
return name in conn
|
||||
|
||||
def close_project(name: str) -> None:
|
||||
conn[name].close()
|
||||
del conn[name]
|
||||
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]
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
CREATE TYPE API_OPERATION AS ENUM ('add', 'delete', 'update');
|
||||
|
||||
CREATE TABLE OPERATION
|
||||
(
|
||||
ID SERIAL PRIMARY KEY
|
||||
@@ -5,6 +7,11 @@ CREATE TABLE OPERATION
|
||||
, Undo TEXT NOT NULL
|
||||
, Parent INTEGER REFERENCES OPERATION(ID)
|
||||
, Redo_Child INTEGER REFERENCES OPERATION(ID)
|
||||
, Api TEXT NOT NULL
|
||||
, Api_Operation API_OPERATION NOT NULL
|
||||
, Api_Operation_Type TEXT NOT NULL
|
||||
, Api_Operation_Id TEXT NOT NULL -- VARCHAR(32)
|
||||
, Api_Operation_Property TEXT NOT NULL
|
||||
);
|
||||
|
||||
INSERT INTO OPERATION (ID, Redo, Undo) VALUES (0, '', '');
|
||||
|
||||
@@ -64,6 +64,9 @@ def copy_project(source: str, new: str) -> None:
|
||||
# operation
|
||||
############################################################
|
||||
|
||||
def get_current_operation(name: str) -> int:
|
||||
return api.get_current_operation(name)
|
||||
|
||||
def undo(name: str) -> None:
|
||||
return api.undo(name)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user