Support cascade deletion
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
from .operation import *
|
||||
from .database import *
|
||||
from .s2_junctions import *
|
||||
|
||||
def get_demand_schema(name: str) -> dict[str, dict[str, Any]]:
|
||||
@@ -21,7 +21,7 @@ def get_demand(name: str, junction: str) -> dict[str, Any]:
|
||||
return { 'junction': junction, 'demands': ds }
|
||||
|
||||
|
||||
def set_demand_cache(name: str, cs: ChangeSet) -> DbChangeSet:
|
||||
def set_demand_cmd(name: str, cs: ChangeSet) -> DbChangeSet:
|
||||
junction = cs.operations[0]['junction']
|
||||
old = get_demand(name, junction)
|
||||
new = { 'junction': junction, 'demands': [] }
|
||||
@@ -55,34 +55,34 @@ def set_demand_cache(name: str, cs: ChangeSet) -> DbChangeSet:
|
||||
undo_cs = []
|
||||
undo_cs.append(g_update_prefix | { 'type': 'demand' } | old)
|
||||
|
||||
cache = None
|
||||
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
|
||||
cache = set_junction_cache(name, ChangeSet({'id': junction, 'demand': demand, 'pattern': pattern}))
|
||||
cmd = set_junction_cmd(name, ChangeSet({'id': junction, 'demand': demand, 'pattern': pattern}))
|
||||
else:
|
||||
cache = set_junction_cache(name, ChangeSet({'id': junction, 'demand': None, 'pattern': None}))
|
||||
cmd = set_junction_cmd(name, ChangeSet({'id': junction, 'demand': None, 'pattern': None}))
|
||||
|
||||
undo_sql = ''
|
||||
if cache != None:
|
||||
if cmd != None:
|
||||
redo_sql += '\n'
|
||||
redo_sql += cache.redo_sql
|
||||
redo_sql += cmd.redo_sql
|
||||
|
||||
undo_sql += cache.undo_sql
|
||||
undo_sql += cmd.undo_sql
|
||||
undo_sql += '\n'
|
||||
undo_sql += _undo_sql
|
||||
|
||||
redo_cs += cache.redo_cs
|
||||
redo_cs += cmd.redo_cs
|
||||
|
||||
undo_cs += cache.undo_cs
|
||||
undo_cs += cmd.undo_cs
|
||||
undo_cs.reverse()
|
||||
|
||||
return DbChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
|
||||
|
||||
def set_demand(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
return execute_command(name, set_demand_cache(name, cs))
|
||||
return execute_command(name, set_demand_cmd(name, cs))
|
||||
|
||||
|
||||
class InpDemand:
|
||||
@@ -152,3 +152,23 @@ def inp_out_demand(name: str) -> list[str]:
|
||||
category = f";{obj['category']}" if obj['category'] != None else ';'
|
||||
lines.append(f'{junction} {demand} {pattern} {category}')
|
||||
return lines
|
||||
|
||||
|
||||
def delete_demand_by_junction(name: str, junction: str) -> ChangeSet:
|
||||
row = try_read(name, f"select * from demands where junction = '{junction}'")
|
||||
if row == None:
|
||||
return ChangeSet()
|
||||
return ChangeSet(g_update_prefix | {'type': 'demand', 'junction': junction, 'demands': []})
|
||||
|
||||
|
||||
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}'")
|
||||
for row in rows:
|
||||
ds = get_demand(name, row['id'])
|
||||
for d in ds['demands']:
|
||||
d['pattern'] = None
|
||||
cs.append(g_update_prefix | {'type': 'demand', 'junction': row['junction'], 'demands': ds})
|
||||
|
||||
return cs
|
||||
|
||||
Reference in New Issue
Block a user