Big change to operation!

This commit is contained in:
wqy
2022-09-24 23:00:55 +08:00
parent 46df1beedb
commit 11e30cc49f
6 changed files with 286 additions and 256 deletions

View File

@@ -4,82 +4,98 @@ from .s0_base import *
from .change_set import ChangeSet
from .s24_coordinates import *
from .utility import *
from .schema import *
schema: dict[str, dict[str, Any]] = { \
'id' : define_property(str_type, False, True), \
'elevation' : define_property(float_type), \
'demand' : define_property(float_type, True), \
'pattern' : define_property(str_type, True), \
'coord' : define_property(client_point_type), \
'links' : define_property(str_list_type, False, True)}
def get_junction_schema(name: str) -> dict[str, str]:
return schema
def _query_junction(name: str, id: str) -> Row | None:
return read(name, f"select id, elevation, demand, pattern from junctions where id = '{id}'")
def add_junction(name: str, id: str, x: float, y: float, elevation: float) -> ChangeSet:
sql = f"insert into junctions (id, elevation) values ('{id}', {elevation});"
undo_sql = f'delete from junctions where id = "{id}";'
return add_node(name, JUNCTION, id, x, y, sql, undo_sql)
def _get_junction(name: str, id: str) -> Row | None:
return query(name, f"select elevation, demand, pattern from junctions where id = '{id}'")
def delete_junction(name: str, id: str) -> ChangeSet:
if not is_junction(name, id):
if is_junction(name, id):
return ChangeSet()
row = _get_junction(name, id)
if row == None:
return ChangeSet()
sql = f"insert into _node (id, type) values ('{id}', '{JUNCTION}');"
sql += f"\ninsert into junctions (id, elevation) values ('{id}', {elevation});"
sql += f"\ninsert into coordinates (node, coord) values ('{id}', '({x}, {y})');"
elevation = row['elevation']
demand = decorate(row['demand'], 'float', True)
pattern = decorate(row['pattern'], 'str', True)
undo = f"delete from coordinates where node = ''{id}'';"
undo += f"\ndelete from junctions where id = ''{id}'';"
undo += f"\ndelete from _node where id = ''{id}'';"
sql = f"delete from junctions where id = '{id}';"
undo_sql = f'insert into junctions (id, elevation, demand, pattern) values ("{id}", {elevation}, {demand}, {pattern});'
return delete_node(name, JUNCTION, id, sql, undo_sql)
write(name, sql)
add_operation(name, sql.replace("'", "''"), undo, 'add_junction', API_ADD, JUNCTION, id)
return get_current_change_set(name)
def _set_junction(name: str, id: str, key: str, key_type: str, value: str, optional: bool = False) -> ChangeSet:
if not is_junction(name, id):
return ChangeSet()
row = _get_junction(name, id)
if row == None:
return ChangeSet()
return update(name, JUNCTION, 'junctions', 'id', id, key, key_type, row[key], value, optional)
def set_junction_elevation(name: str, id: str, elevation: float) -> ChangeSet:
return _set_junction(name, id, 'elevation', 'float', str(elevation))
def set_junction_demand(name: str, id: str, demand: float) -> ChangeSet:
return _set_junction(name, id, 'demand', 'float', str(demand), True)
def set_junction_pattern(name: str, id: str, pattern: str) -> ChangeSet:
if not is_pattern(name, pattern):
return ChangeSet()
return _set_junction(name, id, 'pattern', 'str', pattern, True)
def set_junction_coord(name: str, id: str, x: float, y: float) -> ChangeSet:
if not is_junction(name, id):
return ChangeSet()
return set_node_coord(name, JUNCTION, id, x, y)
def get_junction_property_names(name: str) -> list[str]:
return ['elevation', 'demand', 'pattern', 'coord', 'links']
def get_junction_properties(name: str, id: str) -> dict[str, Any] | None:
row = _get_junction(name, id)
def get_junction(name: str, id: str) -> dict[str, Any] | None:
row = _query_junction(name, id)
if row == None:
return None
ps: dict[str, str] = {}
ps['elevation'] = float(row['elevation'])
ps['demand'] = float(row['demand']) if row != None and row['demand'] != None else None
ps['pattern'] = row['pattern'] if row != None and row['pattern'] != None else None
ps['demand'] = float(row['demand']) if row['demand'] != None else None
ps['pattern'] = row['pattern']
ps['coord'] = get_node_coord(name, id)
ps['links'] = get_node_links(name, id)
return ps
def delete_junction(name: str, id: str) -> ChangeSet:
row = get_junction(name, id)
if row == None:
return ChangeSet()
old = Serialize(get_junction(name, id), schema).to_storage()
sql = f"delete from coordinates where node = '{id}';"
sql += f"\ndelete from junctions where id = '{id}';"
sql += f"\ndelete from _node where id = '{id}';"
undo = f"insert into _node (id, type) values (''{id}'', ''{JUNCTION}'');"
undo += f"\ninsert into junctions (id, elevation, demand, pattern) values (''{id}'', {old['elevation']}, {old['demand']}, {old['pattern']});"
undo += f"\ninsert into coordinates (node, coord) values (''{id}'', {old['coord']});"
write(name, sql)
add_operation(name, sql.replace("'", "''"), undo, 'delete_junction', API_DELETE, JUNCTION, id)
return get_current_change_set(name)
def set_junction(name: str, id: str, properties: dict[str, Any]) -> ChangeSet:
if not is_junction(name, id):
return ChangeSet()
old = Serialize(get_junction(name, id), schema).to_storage()
new = get_junction(name, id)
ps: list[str] = []
for key in properties:
if key in schema and schema[key]['readonly'] == False:
new[key] = properties[key]
ps.append(key)
new = Serialize(new, schema).to_execution()
sql = f"update junctions set elevation = {new['elevation']}, demand = {new['demand']}, pattern = {new['pattern']} where id = '{id}';"
undo = ""
if 'coord' in ps:
sql += f"\nupdate coordinates set coord = {new['coord']} where node = '{id}';"
undo = f"update coordinates set coord = {old['coord']} where node = ''{id}'';"
undo += f"\nupdate junctions set elevation = {old['elevation']}, demand = {old['demand']}, pattern = {old['pattern']} where id = ''{id}'';"
write(name, sql)
add_operation(name, sql.replace("'", "''"), undo, 'set_junction', API_UPDATE, JUNCTION, id, ps)
return get_current_change_set(name)