from .operation import * def get_emitter_schema(name: str) -> dict[str, dict[str, Any]]: return { 'junction' : {'type': 'str' , 'optional': False , 'readonly': True }, 'coefficient' : {'type': 'float' , 'optional': True , 'readonly': False} } def get_emitter(name: str, junction: str) -> dict[str, Any]: e = try_read(name, f"select * from emitters where junction = '{junction}'") if e == None: return { 'junction': junction, 'coefficient': None } d = {} d['junction'] = str(e['junction']) d['coefficient'] = float(e['coefficient']) if e['coefficient'] != None else None return d class Emitter(object): def __init__(self, input: dict[str, Any]) -> None: self.type = 'emitter' self.junction = str(input['junction']) self.coefficient = float(input['coefficient']) if 'coefficient' in input and input['coefficient'] != None else None self.f_type = f"'{self.type}'" self.f_junction = f"'{self.junction}'" self.f_coefficient = self.coefficient if self.coefficient != None else 'null' def as_dict(self) -> dict[str, Any]: return { 'type': self.type, 'junction': self.junction, 'coefficient': self.coefficient } def set_emitter(name: str, cs: ChangeSet) -> ChangeSet: old = Emitter(get_emitter(name, cs.operations[0]['junction'])) raw_new = get_emitter(name, cs.operations[0]['junction']) new_dict = cs.operations[0] schema = get_emitter_schema(name) for key, value in schema.items(): if key in new_dict and not value['readonly']: raw_new[key] = new_dict[key] new = Emitter(raw_new) redo_sql = f"delete from emitters where junction = {new.f_junction};" if new.coefficient != None: redo_sql += f"\ninsert into emitters (junction, coefficient) values ({new.f_junction}, {new.f_coefficient});" undo_sql = f"delete from emitters where junction = {old.f_junction};" if old.coefficient != None: undo_sql += f"\ninsert into emitters (junction, coefficient) values ({old.f_junction}, {old.f_coefficient});" redo_cs = g_update_prefix | new.as_dict() undo_cs = g_update_prefix | old.as_dict() return execute_command(name, redo_sql, undo_sql, redo_cs, undo_cs)