Refine control and rule
This commit is contained in:
@@ -2,22 +2,30 @@ from .operation import *
|
||||
|
||||
|
||||
def get_control_schema(name: str) -> dict[str, dict[str, Any]]:
|
||||
return { 'control' : {'type': 'str' , 'optional': False , 'readonly': False} }
|
||||
return { 'controls' : {'type': 'str_list' , 'optional': False , 'readonly': False} }
|
||||
|
||||
|
||||
def get_control(name: str) -> dict[str, Any]:
|
||||
e = read(name, f"select * from controls")
|
||||
return { 'control': e['control'] }
|
||||
cs = read_all(name, f"select * from controls")
|
||||
ds = []
|
||||
for c in cs:
|
||||
ds.append(c['line'])
|
||||
return { 'controls': ds }
|
||||
|
||||
|
||||
def set_control_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
|
||||
old = get_control(name)
|
||||
|
||||
redo_sql = f"update controls set control = '{cs.operations[0]['control']}' where control = '{old['control']}';"
|
||||
undo_sql = f"update controls set control = '{old['control']}' where control = '{cs.operations[0]['control']}';"
|
||||
redo_sql = 'delete from controls;'
|
||||
for line in cs.operations[0]['controls']:
|
||||
redo_sql += f"\ninsert into controls (line) values ('{line}');"
|
||||
|
||||
redo_cs = g_update_prefix | { 'type': 'control', 'control': cs.operations[0]['control'] }
|
||||
undo_cs = g_update_prefix | { 'type': 'control', 'control': old['control'] }
|
||||
undo_sql = 'delete from controls;'
|
||||
for line in old['controls']:
|
||||
undo_sql += f"\ninsert into controls (line) values ('{line}');"
|
||||
|
||||
redo_cs = g_update_prefix | { 'type': 'control', 'controls': cs.operations[0]['controls'] }
|
||||
undo_cs = g_update_prefix | { 'type': 'control', 'controls': old['controls'] }
|
||||
|
||||
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
|
||||
@@ -26,17 +34,10 @@ def set_control(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
return execute_command(name, set_control_cache(name, cs))
|
||||
|
||||
|
||||
class InpControl:
|
||||
def __init__(self, section) -> None:
|
||||
self.control = '\n'.join(section)
|
||||
|
||||
|
||||
def inp_in_control(section: list[str]) -> ChangeSet:
|
||||
obj = InpControl(section)
|
||||
cs = ChangeSet(g_update_prefix | {'type': 'control', 'control' : obj.control})
|
||||
cs = ChangeSet(g_update_prefix | {'type': 'control', 'controls' : section})
|
||||
return cs
|
||||
|
||||
|
||||
def inp_out_control(name: str) -> list[str]:
|
||||
obj = str(get_control(name)['control'])
|
||||
return obj.split('\n')
|
||||
return get_control(name)['controls']
|
||||
|
||||
@@ -2,22 +2,30 @@ from .operation import *
|
||||
|
||||
|
||||
def get_rule_schema(name: str) -> dict[str, dict[str, Any]]:
|
||||
return { 'rule' : {'type': 'str' , 'optional': False , 'readonly': False} }
|
||||
return { 'rules' : {'type': 'str_list' , 'optional': False , 'readonly': False} }
|
||||
|
||||
|
||||
def get_rule(name: str) -> dict[str, Any]:
|
||||
e = read(name, f"select * from rules")
|
||||
return { 'rule': e['rule'] }
|
||||
cs = read_all(name, f"select * from rules")
|
||||
ds = []
|
||||
for c in cs:
|
||||
ds.append(c['line'])
|
||||
return { 'rules': ds }
|
||||
|
||||
|
||||
def set_rule_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
|
||||
old = get_rule(name)
|
||||
|
||||
redo_sql = f"update rules set rule = '{cs.operations[0]['rule']}' where rule = '{old['rule']}';"
|
||||
undo_sql = f"update rules set rule = '{old['rule']}' where rule = '{cs.operations[0]['rule']}';"
|
||||
redo_sql = 'delete from rules;'
|
||||
for line in cs.operations[0]['rules']:
|
||||
redo_sql += f"\ninsert into rules (line) values ('{line}');"
|
||||
|
||||
redo_cs = g_update_prefix | { 'type': 'rule', 'rule': cs.operations[0]['rule'] }
|
||||
undo_cs = g_update_prefix | { 'type': 'rule', 'rule': old['rule'] }
|
||||
undo_sql = 'delete from rules;'
|
||||
for line in old['rules']:
|
||||
undo_sql += f"\ninsert into rules (line) values ('{line}');"
|
||||
|
||||
redo_cs = g_update_prefix | { 'type': 'rule', 'rules': cs.operations[0]['rules'] }
|
||||
undo_cs = g_update_prefix | { 'type': 'rule', 'rules': old['rules'] }
|
||||
|
||||
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
|
||||
@@ -26,17 +34,10 @@ def set_rule(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
return execute_command(name, set_rule_cache(name, cs))
|
||||
|
||||
|
||||
class InpRule:
|
||||
def __init__(self, section) -> None:
|
||||
self.rule = '\n'.join(section)
|
||||
|
||||
|
||||
def inp_in_rule(section: list[str]) -> ChangeSet:
|
||||
obj = InpRule(section)
|
||||
cs = ChangeSet(g_update_prefix | {'type': 'rule', 'rule' : obj.rule})
|
||||
cs = ChangeSet(g_update_prefix | {'type': 'rule', 'rules' : section})
|
||||
return cs
|
||||
|
||||
|
||||
def inp_out_rule(name: str) -> list[str]:
|
||||
obj = str(get_rule(name)['rule'])
|
||||
return obj.split('\n')
|
||||
return get_rule(name)['rules']
|
||||
Reference in New Issue
Block a user