Code refactor for demand

This commit is contained in:
WQY\qiong
2022-12-17 09:30:31 +08:00
parent b715f330dd
commit 7a784d12c5

View File

@@ -21,7 +21,7 @@ def get_demand(name: str, junction: str) -> dict[str, Any]:
return { 'junction': junction, 'demands': ds } return { 'junction': junction, 'demands': ds }
def set_demand_cache(name: str, cs: ChangeSet) -> BatchSqlChangeSet: def set_demand_cache(name: str, cs: ChangeSet) -> DbChangeSet:
junction = cs.operations[0]['junction'] junction = cs.operations[0]['junction']
old = get_demand(name, junction) old = get_demand(name, junction)
new = { 'junction': junction, 'demands': [] } new = { 'junction': junction, 'demands': [] }
@@ -40,7 +40,6 @@ def set_demand_cache(name: str, cs: ChangeSet) -> BatchSqlChangeSet:
redo_sql += f"\ninsert into demands (junction, demand, pattern, category) values ({f_junction}, {f_demand}, {f_pattern}, {f_category});" 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 }) new['demands'].append({ 'demand': demand, 'pattern': pattern, 'category': category })
undo_sql = ''
_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']: for r in old['demands']:
demand = float(r['demand']) demand = float(r['demand'])
@@ -56,7 +55,7 @@ def set_demand_cache(name: str, cs: ChangeSet) -> BatchSqlChangeSet:
undo_cs = [] undo_cs = []
undo_cs.append(g_update_prefix | { 'type': 'demand' } | old) undo_cs.append(g_update_prefix | { 'type': 'demand' } | old)
cache = None | SqlChangeSet cache = None
if len(cs.operations[0]['demands']) > 0: if len(cs.operations[0]['demands']) > 0:
r = cs.operations[0]['demands'][0] r = cs.operations[0]['demands'][0]
demand = float(r['demand']) demand = float(r['demand'])
@@ -65,6 +64,7 @@ def set_demand_cache(name: str, cs: ChangeSet) -> BatchSqlChangeSet:
else: else:
cache = set_junction_cache(name, ChangeSet({'id': junction, 'demand': None, 'pattern': None})) cache = set_junction_cache(name, ChangeSet({'id': junction, 'demand': None, 'pattern': None}))
undo_sql = ''
if cache != None: if cache != None:
redo_sql += '\n' redo_sql += '\n'
redo_sql += cache.redo_sql redo_sql += cache.redo_sql
@@ -78,12 +78,11 @@ def set_demand_cache(name: str, cs: ChangeSet) -> BatchSqlChangeSet:
undo_cs.append(cache.undo_cs) undo_cs.append(cache.undo_cs)
undo_cs.reverse() undo_cs.reverse()
return BatchSqlChangeSet(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: def set_demand(name: str, cs: ChangeSet) -> ChangeSet:
css = set_demand_cache(name, cs) return execute_command(name, set_demand_cache(name, cs))
return execute_batch(name, css.redo_sql, css.undo_sql, css.redo_cs, css.undo_cs)
class InpDemand: class InpDemand: