Refine api style for change set

This commit is contained in:
WQY\qiong
2022-10-09 23:25:31 +08:00
parent 69db29d981
commit 231304f73a
5 changed files with 112 additions and 41 deletions

View File

@@ -1,37 +1,71 @@
drop function if exists update_title;
drop function if exists get_title_update_template;
drop function if exists get_title;
create function get_title() returns text as
$$
select value from title as result;
$$
language sql;
drop function if exists get_title_schema;
drop function if exists set_title;
create function set_title(new_value text) returns json as
create function get_title_schema() returns jsonb as
$$
declare
old_value text;
redo_sql text;
undo_sql text;
redo_cs json;
undo_cs json;
begin
select value into old_value from title;
redo_sql = format('update title set value = ''%s''', new_value);
undo_sql = format('update title set value = ''%s''', old_value);
redo_cs = format('{"operation": "update", "type": "title", "value": "%s" }', new_value)::json;
undo_cs = format('{"operation": "update", "type": "title", "value": "%s" }', old_value)::json;
execute add_operation(redo_sql, undo_sql, redo_cs, undo_cs);
execute redo_sql;
return redo_cs;
return '{"value": {"opetional": false, "readonly": false}}'::jsonb;
end;
$$
language plpgsql;
language plpgsql;
create function get_title() returns jsonb as
$$
declare
v_value text;
begin
select value into v_value from title;
return format('{"value": "%s"}', v_value)::jsonb;
end;
$$
language plpgsql;
create function get_title_update_template() returns jsonb as
$$
declare
begin
return '{"operation": "update", "type": "title", "value": ""}'::jsonb;
end;
$$
language plpgsql;
create function update_title(new jsonb) returns jsonb as
$$
declare
old jsonb;
redo_sql text;
undo_sql text;
redo_cs jsonb;
undo_cs jsonb;
begin
assert new->>'operation' = 'update';
assert new->>'type' = 'title';
assert new ? 'value';
old := get_title();
if old->>'value' = new->>'value' then
return '{}'::jsonb;
end if;
redo_sql := format('update title set value = ''%s''', new->>'value');
undo_sql := format('update title set value = ''%s''', old->>'value');
redo_cs := format('{"operation": "update", "type": "title", "value": "%s" }', new->>'value')::jsonb;
undo_cs := format('{"operation": "update", "type": "title", "value": "%s" }', old->>'value')::jsonb;
execute redo_sql;
execute add_operation(redo_sql, undo_sql, redo_cs, undo_cs);
return redo_cs;
end;
$$
language plpgsql;