Refactor base
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
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"
|
||||
_LINK = "_link"
|
||||
@@ -73,3 +75,50 @@ def get_curves(name: str) -> list[str]:
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user