Refine control and rule

This commit is contained in:
WQY\qiong
2022-11-18 22:50:33 +08:00
parent 0b180af5d5
commit 315e94f4ad
5 changed files with 52 additions and 52 deletions

View File

@@ -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']

View File

@@ -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']

View File

@@ -2,7 +2,6 @@
create table controls
(
control text primary key
_order serial primary key
, line text not null
);
insert into controls (control) values ('');

View File

@@ -2,7 +2,6 @@
create table rules
(
rule text primary key
_order serial primary key
, line text not null
);
insert into rules (rule) values ('');

View File

@@ -1851,10 +1851,10 @@ class TestApi:
p = 'test_control'
self.enter(p)
assert get_control(p)['control'] == ''
assert get_control(p)['controls'] == []
set_control(p, ChangeSet({'control': 'x'}))
assert get_control(p)['control'] == 'x'
set_control(p, ChangeSet({'controls': ['x']}))
assert get_control(p)['controls'] == ['x']
self.leave(p)
@@ -1863,20 +1863,20 @@ class TestApi:
p = 'test_control_op'
self.enter(p)
cs = set_control(p, ChangeSet({'control': 'x'})).operations[0]
cs = set_control(p, ChangeSet({'controls': ['x']})).operations[0]
assert cs['operation'] == API_UPDATE
assert cs['type'] == 'control'
assert cs['control'] == 'x'
assert cs['controls'] == ['x']
cs = execute_undo(p).operations[0]
assert cs['operation'] == API_UPDATE
assert cs['type'] == 'control'
assert cs['control'] == ''
assert cs['controls'] == []
cs = execute_redo(p).operations[0]
assert cs['operation'] == API_UPDATE
assert cs['type'] == 'control'
assert cs['control'] == 'x'
assert cs['controls'] == ['x']
self.leave(p)
@@ -1888,10 +1888,10 @@ class TestApi:
p = 'test_rule'
self.enter(p)
assert get_rule(p)['rule'] == ''
assert get_rule(p)['rules'] == []
set_rule(p, ChangeSet({'rule': 'x'}))
assert get_rule(p)['rule'] == 'x'
set_rule(p, ChangeSet({'rules': ['x']}))
assert get_rule(p)['rules'] == ['x']
self.leave(p)
@@ -1900,20 +1900,20 @@ class TestApi:
p = 'test_rule_op'
self.enter(p)
cs = set_rule(p, ChangeSet({'rule': 'x'})).operations[0]
cs = set_rule(p, ChangeSet({'rules': ['x']})).operations[0]
assert cs['operation'] == API_UPDATE
assert cs['type'] == 'rule'
assert cs['rule'] == 'x'
assert cs['rules'] == ['x']
cs = execute_undo(p).operations[0]
assert cs['operation'] == API_UPDATE
assert cs['type'] == 'rule'
assert cs['rule'] == ''
assert cs['rules'] == []
cs = execute_redo(p).operations[0]
assert cs['operation'] == API_UPDATE
assert cs['type'] == 'rule'
assert cs['rule'] == 'x'
assert cs['rules'] == ['x']
self.leave(p)