from typing import Any from psycopg.rows import Row 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: if is_junction(name, id): 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})');" undo = f"delete from coordinates where node = ''{id}'';" undo += f"\ndelete from junctions where id = ''{id}'';" undo += f"\ndelete from _node where id = ''{id}'';" write(name, sql) add_operation(name, sql.replace("'", "''"), undo, 'add_junction', API_ADD, JUNCTION, id) return get_current_change_set(name) 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['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)