Drop demand & pattern from junction

This commit is contained in:
WQY\qiong
2023-03-01 22:59:40 +08:00
parent e83d3a83a5
commit 628258ef52
6 changed files with 18 additions and 133 deletions

View File

@@ -1,5 +1,4 @@
from .database import *
from .s2_junctions import *
def get_demand_schema(name: str) -> dict[str, dict[str, Any]]:
return { 'junction' : {'type': 'str' , 'optional': False , 'readonly': True },
@@ -40,7 +39,7 @@ def set_demand_cmd(name: str, cs: ChangeSet) -> DbChangeSet:
redo_sql += f"\ninsert into demands (junction, demand, pattern, category) values ({f_junction}, {f_demand}, {f_pattern}, {f_category});"
new['demands'].append({ 'demand': demand, 'pattern': pattern, 'category': category })
_undo_sql = f"delete from demands where junction = {f_junction};"
undo_sql = f"delete from demands where junction = {f_junction};"
for r in old['demands']:
demand = float(r['demand'])
pattern = str(r['pattern']) if 'pattern' in r and r['pattern'] != None else None
@@ -48,37 +47,12 @@ def set_demand_cmd(name: str, cs: ChangeSet) -> DbChangeSet:
f_demand = demand
f_pattern = f"'{pattern}'" if pattern != None else 'null'
f_category = f"'{category}'" if category != None else 'null'
_undo_sql += f"\ninsert into demands (junction, demand, pattern, category) values ({f_junction}, {f_demand}, {f_pattern}, {f_category});"
undo_sql += f"\ninsert into demands (junction, demand, pattern, category) values ({f_junction}, {f_demand}, {f_pattern}, {f_category});"
redo_cs = []
redo_cs.append(g_update_prefix | { 'type': 'demand' } | new)
undo_cs = []
undo_cs.append(g_update_prefix | { 'type': 'demand' } | old)
redo_cs = g_update_prefix | { 'type': 'demand' } | new
undo_cs = g_update_prefix | { 'type': 'demand' } | old
cmd = None
if len(cs.operations[0]['demands']) > 0:
r = cs.operations[0]['demands'][0]
demand = float(r['demand'])
pattern = str(r['pattern']) if 'pattern' in r and r['pattern'] != None else None
cmd = set_junction_cmd(name, ChangeSet({'id': junction, 'demand': demand, 'pattern': pattern}))
else:
cmd = set_junction_cmd(name, ChangeSet({'id': junction, 'demand': None, 'pattern': None}))
undo_sql = ''
if cmd != None:
redo_sql += '\n'
redo_sql += cmd.redo_sql
undo_sql += cmd.undo_sql
undo_sql += '\n'
undo_sql += _undo_sql
redo_cs += cmd.redo_cs
undo_cs += cmd.undo_cs
undo_cs.reverse()
return DbChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
return DbChangeSet(redo_sql, undo_sql, [redo_cs], [undo_cs])
def set_demand(name: str, cs: ChangeSet) -> ChangeSet:
@@ -123,29 +97,6 @@ def inp_in_demand(section: list[str]) -> ChangeSet:
return cs
def fill_demand(junction_cs : ChangeSet, demand_cs : ChangeSet) -> ChangeSet:
cs = ChangeSet()
for j_cs in junction_cs.operations:
if 'demand' not in j_cs:
continue
in_demand = False
for d_cs in demand_cs.operations:
if j_cs['id'] == d_cs['junction']:
in_demand = True
break
if not in_demand:
obj_cs : dict[str, Any] = g_update_prefix | {'type': 'demand', 'junction' : j_cs['id'], 'demands' : []}
j_demand = j_cs['demand']
j_pattern = j_cs['pattern'] if 'pattern' in j_cs else None
obj_cs['demands'].append({'demand': j_demand, 'pattern' : j_pattern, 'category': None})
cs.append(obj_cs)
return cs
def inp_out_demand(name: str) -> list[str]:
lines = []
objs = read_all(name, f"select * from demands order by _order")
@@ -168,11 +119,11 @@ def delete_demand_by_junction(name: str, junction: str) -> ChangeSet:
def unset_demand_by_pattern(name: str, pattern: str) -> ChangeSet:
cs = ChangeSet()
rows = read_all(name, f"select distinct id from junctions where pattern = '{pattern}'")
rows = read_all(name, f"select distinct junction from demands where pattern = '{pattern}'")
for row in rows:
ds = get_demand(name, row['id'])
ds = get_demand(name, row['junction'])
for d in ds['demands']:
d['pattern'] = None
cs.append(g_update_prefix | {'type': 'demand', 'junction': row['id'], 'demands': ds['demands']})
cs.append(g_update_prefix | {'type': 'demand', 'junction': row['junction'], 'demands': ds['demands']})
return cs