Huge refactor to api and add batch api

This commit is contained in:
WQY\qiong
2022-10-14 23:18:01 +08:00
parent 200aaaca99
commit c5480d55ca
20 changed files with 1811 additions and 1510 deletions

View File

@@ -1,7 +1,6 @@
from psycopg.rows import dict_row, Row
from .connection import g_conn_dict as conn
from .operation import *
from .change_set import ChangeSet
_NODE = "_node"
@@ -94,93 +93,6 @@ def get_patterns(name: str) -> list[str]:
return _get_all(name, _PATTERN)
def add_node(name: str, node_type: str, id: str, x: float, y: float, table_sql: str, table_undo_sql: str) -> ChangeSet:
if is_node(name, id):
return ChangeSet()
with conn[name].cursor() as cur:
sql = f"insert into _node (id, type) values ('{id}', '{node_type}');"
sql += table_sql
sql += f"insert into coordinates (node, coord) values ('{id}', '({x}, {y})');"
cur.execute(sql)
redo = sql.replace("'", '"')
undo = f'delete from coordinates where node = "{id}";'
undo += table_undo_sql
undo += f'delete from _node where id = "{id}";'
add_operation(name, redo, undo)
change = ChangeSet()
change.add(node_type, id)
return change
def delete_node(name: str, node_type: str, id: str, table_sql: str, table_undo_sql: str) -> ChangeSet:
if not is_node(name, id):
return ChangeSet()
with conn[name].cursor(row_factory=dict_row) as cur:
cur.execute(f"select * from coordinates where node = '{id}'")
row = cur.fetchone()
if row == None:
return ChangeSet()
coord = row['coord']
sql = f"delete from coordinates where node = '{id}'; "
sql += table_sql
sql += f" delete from _node where id = '{id}';"
cur.execute(sql)
redo = sql.replace("'", '"')
undo = f'insert into _node (id, type) values ("{id}", "{node_type}");'
undo += table_undo_sql
undo += f'insert into coordinates (node, coord) values ("{id}", "{coord}");'
add_operation(name, redo, undo)
change = ChangeSet()
change.delete(node_type, id)
return change
def add_link(name: str, link_type: str, id: str, table_sql: str, table_undo_sql: str) -> ChangeSet:
if is_link(name, id):
return ChangeSet()
with conn[name].cursor() as cur:
sql = f"insert into _link (id, type) values ('{id}', '{link_type}');"
sql += table_sql
cur.execute(sql)
redo = sql.replace("'", '"')
undo = table_undo_sql
undo += f'delete from _link where id = "{id}";'
add_operation(name, redo, undo)
change = ChangeSet()
change.add(link_type, id)
return change
def delete_link(name: str, link_type: str, id: str, table_sql: str, table_undo_sql: str) -> ChangeSet:
if not is_link(name, id):
return ChangeSet()
with conn[name].cursor(row_factory=dict_row) as cur:
sql = table_sql
sql += f" delete from _link where id = '{id}';"
cur.execute(sql)
redo = sql.replace("'", '"')
undo = f'insert into _link (id, type) values ("{id}", "{link_type}"); '
undo += table_undo_sql
add_operation(name, redo, undo)
change = ChangeSet()
change.delete(link_type, id)
return change
def get_node_links(name: str, id: str) -> list[str]:
with conn[name].cursor(row_factory=dict_row) as cur:
links: list[str] = []