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), \ 'head' : define_property(float_type), \ 'pattern' : define_property(str_type, True), \ 'coord' : define_property(client_point_type), \ 'links' : define_property(str_list_type, False, True)} def get_reservoir_schema(name: str) -> dict[str, dict[str, Any]]: return schema def _query_reservoir(name: str, id: str) -> Row | None: return read(name, f"select * from reservoirs where id = '{id}'") def add_reservoir(name: str, id: str, x: float, y: float, head: float) -> ChangeSet: if is_reservoir(name, id): return ChangeSet() sql = f"insert into _node (id, type) values ('{id}', '{RESERVOIR}');" sql += f"\ninsert into reservoirs (id, head) values ('{id}', {head});" sql += f"\ninsert into coordinates (node, coord) values ('{id}', '({x}, {y})');" undo = f"delete from coordinates where node = ''{id}'';" undo += f"\ndelete from reservoirs where id = ''{id}'';" undo += f"\ndelete from _node where id = ''{id}'';" write(name, sql) add_operation(name, sql.replace("'", "''"), undo, 'add_reservoir', API_ADD, RESERVOIR, id) return get_current_change_set(name) def get_reservoir(name: str, id: str) -> dict[str, Any] | None: row = _query_reservoir(name, id) if row == None: return None ps: dict[str, str] = {} ps['id'] = id ps['head'] = float(row['head']) ps['pattern'] = row['pattern'] ps['coord'] = get_node_coord(name, id) ps['links'] = get_node_links(name, id) return ps def set_reservoir(name: str, id: str, properties: dict[str, Any]) -> ChangeSet: if not is_reservoir(name, id): return ChangeSet() if 'pattern' in properties: if not is_pattern(properties['pattern']): return ChangeSet() old = Serialize(get_reservoir(name, id), schema).to_storage() new = get_reservoir(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 reservoirs set head = {new['head']}, 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 reservoirs set head = {old['head']}, pattern = {old['pattern']} where id = ''{id}'';" write(name, sql) add_operation(name, sql.replace("'", "''"), undo, 'set_reservoir', API_UPDATE, RESERVOIR, id, ps) return get_current_change_set(name) def delete_reservoir(name: str, id: str) -> ChangeSet: row = get_reservoir(name, id) if row == None: return ChangeSet() old = Serialize(get_reservoir(name, id), schema).to_storage() sql = f"delete from coordinates where node = '{id}';" sql += f"\ndelete from reservoirs where id = '{id}';" sql += f"\ndelete from _node where id = '{id}';" undo = f"insert into _node (id, type) values (''{id}'', ''{RESERVOIR}'');" undo += f"\ninsert into reservoirs (id, head, pattern) values (''{id}'', {old['head']}, {old['pattern']});" undo += f"\ninsert into coordinates (node, coord) values (''{id}'', {old['coord']});" write(name, sql) add_operation(name, sql.replace("'", "''"), undo, 'delete_reservoir', API_DELETE, RESERVOIR, id) return get_current_change_set(name)