Let set_demand be a batch command
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
from .operation import *
|
||||
|
||||
from .s2_junctions import *
|
||||
|
||||
def get_demand_schema(name: str) -> dict[str, dict[str, Any]]:
|
||||
return { 'junction' : {'type': 'str' , 'optional': False , 'readonly': True },
|
||||
@@ -21,7 +21,15 @@ def get_demand(name: str, junction: str) -> dict[str, Any]:
|
||||
return { 'junction': junction, 'demands': ds }
|
||||
|
||||
|
||||
def set_demand_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
|
||||
class _SqlChangeSet:
|
||||
def __init__(self, redo_sql: str, undo_sql: str, redo_cs: list[dict[str, str]], undo_cs: list[dict[str, str]]) -> None:
|
||||
self.redo_sql = redo_sql
|
||||
self.undo_sql = undo_sql
|
||||
self.redo_cs = redo_cs
|
||||
self.undo_cs = undo_cs
|
||||
|
||||
|
||||
def set_demand_cache(name: str, cs: ChangeSet) -> _SqlChangeSet:
|
||||
junction = cs.operations[0]['junction']
|
||||
old = get_demand(name, junction)
|
||||
new = { 'junction': junction, 'demands': [] }
|
||||
@@ -40,7 +48,8 @@ def set_demand_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
|
||||
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 = ''
|
||||
_undo_sql = f"delete from demands where junction = {f_junction};"
|
||||
for r in old['demands']:
|
||||
demand = float(r['demand'])
|
||||
pattern = str(r['pattern'])
|
||||
@@ -48,13 +57,38 @@ def set_demand_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
|
||||
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});"
|
||||
|
||||
redo_cs = g_update_prefix | { 'type': 'demand' } | new
|
||||
undo_cs = g_update_prefix | { 'type': 'demand' } | old
|
||||
_undo_sql += f"\ninsert into demands (junction, demand, pattern, category) values ({f_junction}, {f_demand}, {f_pattern}, {f_category});"
|
||||
|
||||
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
redo_cs = []
|
||||
redo_cs.append(g_update_prefix | { 'type': 'demand' } | new)
|
||||
undo_cs = []
|
||||
undo_cs.append(g_update_prefix | { 'type': 'demand' } | old)
|
||||
|
||||
cache = None | SqlChangeSet
|
||||
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}))
|
||||
else:
|
||||
cache = set_junction_cache(name, ChangeSet({'id': junction, 'demand': None, 'pattern': None}))
|
||||
|
||||
if cache != None:
|
||||
redo_sql += '\n'
|
||||
redo_sql += cache.redo_sql
|
||||
|
||||
undo_sql += cache.undo_sql
|
||||
undo_sql += '\n'
|
||||
undo_sql += _undo_sql
|
||||
|
||||
redo_cs.append(cache.redo_cs)
|
||||
|
||||
undo_cs.append(cache.undo_cs)
|
||||
undo_cs.reverse()
|
||||
|
||||
return _SqlChangeSet(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))
|
||||
css = set_demand_cache(name, cs)
|
||||
return execute_batch(name, css.redo_sql, css.undo_sql, css.redo_cs, css.undo_cs)
|
||||
|
||||
Reference in New Issue
Block a user