66 lines
2.3 KiB
Python
66 lines
2.3 KiB
Python
from .operation import *
|
|
|
|
|
|
def get_vertex_schema(name: str) -> dict[str, dict[str, Any]]:
|
|
return { 'link' : {'type': 'str' , 'optional': False , 'readonly': True },
|
|
'coords' : {'type': 'list' , 'optional': False , 'readonly': False,
|
|
'element': { 'x' : {'type': 'float' , 'optional': False , 'readonly': False },
|
|
'y' : {'type': 'float' , 'optional': False , 'readonly': False } }}}
|
|
|
|
|
|
def get_vertex(name: str, link: str) -> dict[str, Any]:
|
|
cus = read_all(name, f"select * from vertices where link = '{link}' order by _order")
|
|
cs = []
|
|
for r in cus:
|
|
cs.append({ 'x': float(r['x']), 'y': float(r['y']) })
|
|
return { 'link': link, 'coords': cs }
|
|
|
|
|
|
def set_vertex_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
|
|
link = cs.operations[0]['link']
|
|
|
|
old = get_vertex(name, link)
|
|
new = { 'link': link, 'coords': [] }
|
|
|
|
f_link = f"'{link}'"
|
|
|
|
# TODO: transaction ?
|
|
redo_sql = f"delete from vertices where link = {f_link};"
|
|
for xy in cs.operations[0]['coords']:
|
|
x, y = float(xy['x']), float(xy['y'])
|
|
f_x, f_y = x, y
|
|
redo_sql += f"\ninsert into vertices (link, x, y) values ({f_link}, {f_x}, {f_y});"
|
|
new['coords'].append({ 'x': x, 'y': y })
|
|
|
|
undo_sql = f"delete from vertices where link = {f_link};"
|
|
for xy in old['coords']:
|
|
f_x, f_y = xy['x'], xy['y']
|
|
undo_sql += f"\ninsert into vertices (link, x, y) values ({f_link}, {f_x}, {f_y});"
|
|
|
|
redo_cs = { 'type': 'vertex' } | new
|
|
undo_cs = { 'type': 'vertex' } | old
|
|
|
|
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
|
|
|
|
|
|
def set_vertex(name: str, cs: ChangeSet) -> ChangeSet:
|
|
result = set_vertex_cache(name, cs)
|
|
result.redo_cs |= g_update_prefix
|
|
result.undo_cs |= g_update_prefix
|
|
return execute_command(name, result)
|
|
|
|
|
|
def add_vertex(name: str, cs: ChangeSet) -> ChangeSet:
|
|
result = set_vertex_cache(name, cs)
|
|
result.redo_cs |= g_add_prefix
|
|
result.undo_cs |= g_delete_prefix
|
|
return execute_command(name, result)
|
|
|
|
|
|
def delete_vertex(name: str, cs: ChangeSet) -> ChangeSet:
|
|
cs.operations[0]['coords'] = []
|
|
result = set_vertex_cache(name, cs)
|
|
result.redo_cs |= g_delete_prefix
|
|
result.undo_cs |= g_add_prefix
|
|
return execute_command(name, result)
|