Huge refactor to api and add batch api

This commit is contained in:
WQY\qiong
2022-10-14 23:18:01 +08:00
parent 200aaaca99
commit c5480d55ca
20 changed files with 1811 additions and 1510 deletions

View File

@@ -1,105 +1,108 @@
from typing import Any
from psycopg.rows import Row
from .operation import *
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, dict[str, Any]]:
return schema
return { 'id' : {'type': 'str' , 'optional': False , 'readonly': True },
'x' : {'type': 'float' , 'optional': False , 'readonly': False},
'y' : {'type': 'float' , 'optional': False , 'readonly': False},
'elevation' : {'type': "float" , 'optional': False , 'readonly': False},
'demand' : {'type': "float" , 'optional': True , 'readonly': False},
'pattern' : {'type': "str" , 'optional': True , 'readonly': False},
'links' : {'type': "str_list" , 'optional': False , 'readonly': True } }
def _query_junction(name: str, id: str) -> Row | None:
return read(name, f"select * from junctions where id = '{id}'")
def get_junction(name: str, id: str) -> dict[str, Any]:
j = read(name, f"select * from junctions where id = '{id}'")
xy = get_node_coord(name, id)
d = {}
d['id'] = str(j['id'])
d['x'] = float(xy['x'])
d['y'] = float(xy['y'])
d['elevation'] = float(j['elevation'])
d['demand'] = float(j['demand']) if j['demand'] != None else None
d['pattern'] = str(j['pattern']) if j['pattern'] != None else None
d['links'] = get_node_links(name, id)
return d
def add_junction(name: str, id: str, x: float, y: float, elevation: float) -> ChangeSet:
if is_junction(name, id):
return ChangeSet()
class Junction(object):
def __init__(self, input: dict[str, Any]) -> None:
self.type = 'junction'
self.id = str(input['id'])
self.x = float(input['x'])
self.y = float(input['y'])
self.elevation = float(input['elevation'])
self.demand = float(input['demand']) if 'demand' in input and input['demand'] != None else None
self.pattern = str(input['pattern']) if 'pattern' in input and input['pattern'] != None else None
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})');"
self.f_type = f"'{self.type}'"
self.f_id = f"'{self.id}'"
self.f_coord = f"'({self.x}, {self.y})'"
self.f_elevation = self.elevation
self.f_demand = self.demand if self.demand != None else 'null'
self.f_pattern = f"'{self.pattern}'" if self.pattern != None else 'null'
undo = f"delete from coordinates where node = ''{id}'';"
undo += f"\ndelete from junctions where id = ''{id}'';"
undo += f"\ndelete from _node where id = ''{id}'';"
def as_dict(self) -> dict[str, Any]:
return { 'type': self.type, 'id': self.id, 'x': self.x, 'y': self.y, 'elevation': self.elevation, 'demand': self.demand, 'pattern': self.pattern }
write(name, sql)
add_operation(name, sql.replace("'", "''"), undo, 'add_junction', API_ADD, JUNCTION, id)
return get_current_change_set(name)
def as_id_dict(self) -> dict[str, Any]:
return { 'type': self.type, 'id': self.id }
def get_junction(name: str, id: str) -> dict[str, Any] | None:
row = _query_junction(name, id)
if row == None:
return None
def set_junction(name: str, cs: ChangeSet) -> ChangeSet:
old = Junction(get_junction(name, cs.operations[0]['id']))
raw_new = get_junction(name, cs.operations[0]['id'])
ps: dict[str, str] = {}
ps['id'] = id
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
new_dict = cs.operations[0]
schema = get_junction_schema(name)
for key, value in schema.items():
if key in new_dict and not value['readonly']:
raw_new[key] = new_dict[key]
new = Junction(raw_new)
redo_sql = f"update junctions set elevation = {new.f_elevation}, demand = {new.f_demand}, pattern = {new.f_pattern} where id = {new.f_id};"
redo_sql += f"\nupdate coordinates set coord = {new.f_coord} where node = {new.f_id};"
undo_sql = f"update coordinates set coord = {old.f_coord} where node = {old.f_id};"
undo_sql += f"\nupdate junctions set elevation = {old.f_elevation}, demand = {old.f_demand}, pattern = {old.f_pattern} where id = {old.f_id};"
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)
def set_junction(name: str, id: str, properties: dict[str, Any]) -> ChangeSet:
if not is_junction(name, id):
return ChangeSet()
if 'pattern' in properties:
if not is_pattern(properties['pattern']):
return ChangeSet()
def add_junction(name: str, cs: ChangeSet) -> ChangeSet:
new = Junction(cs.operations[0])
old = Serialize(get_junction(name, id), schema).to_storage()
redo_sql = f"insert into _node (id, type) values ({new.f_id}, {new.f_type});"
redo_sql += f"\ninsert into junctions (id, elevation, demand, pattern) values ({new.f_id}, {new.f_elevation}, {new.f_demand}, {new.f_pattern});"
redo_sql += f"\ninsert into coordinates (node, coord) values ({new.f_id}, {new.f_coord});"
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()
undo_sql = f"delete from coordinates where node = {new.f_id};"
undo_sql += f"\ndelete from junctions where id = {new.f_id};"
undo_sql += f"\ndelete from _node where id = {new.f_id};"
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}'';"
redo_cs = g_add_prefix | new.as_dict()
undo_cs = g_delete_prefix | new.as_id_dict()
write(name, sql)
add_operation(name, sql.replace("'", "''"), undo, 'set_junction', API_UPDATE, JUNCTION, id, ps)
return get_current_change_set(name)
return execute_command(name, redo_sql, undo_sql, redo_cs, undo_cs)
def delete_junction(name: str, id: str) -> ChangeSet:
row = get_junction(name, id)
if row == None:
return ChangeSet()
def delete_junction(name: str, cs: ChangeSet) -> ChangeSet:
old = Junction(get_junction(name, cs.operations[0]['id']))
old = Serialize(get_junction(name, id), schema).to_storage()
redo_sql = f"delete from coordinates where node = {old.f_id};"
redo_sql += f"\ndelete from junctions where id = {old.f_id};"
redo_sql += f"\ndelete from _node where id = {old.f_id};"
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_sql = f"insert into _node (id, type) values ({old.f_id}, {old.f_type});"
undo_sql += f"\ninsert into junctions (id, elevation, demand, pattern) values ({old.f_id}, {old.f_elevation}, {old.f_demand}, {old.f_pattern});"
undo_sql += f"\ninsert into coordinates (node, coord) values ({old.f_id}, {old.f_coord});"
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']});"
redo_cs = g_delete_prefix | old.as_id_dict()
undo_cs = g_add_prefix | old.as_dict()
write(name, sql)
add_operation(name, sql.replace("'", "''"), undo, 'delete_junction', API_DELETE, JUNCTION, id)
return get_current_change_set(name)
return execute_command(name, redo_sql, undo_sql, redo_cs, undo_cs)