Refine api style for change set
This commit is contained in:
@@ -1,37 +1,71 @@
|
|||||||
|
drop function if exists update_title;
|
||||||
|
drop function if exists get_title_update_template;
|
||||||
drop function if exists get_title;
|
drop function if exists get_title;
|
||||||
|
drop function if exists get_title_schema;
|
||||||
create function get_title() returns text as
|
|
||||||
$$
|
|
||||||
select value from title as result;
|
|
||||||
$$
|
|
||||||
language sql;
|
|
||||||
|
|
||||||
|
|
||||||
drop function if exists set_title;
|
create function get_title_schema() returns jsonb as
|
||||||
|
|
||||||
create function set_title(new_value text) returns json as
|
|
||||||
$$
|
$$
|
||||||
declare
|
declare
|
||||||
old_value text;
|
|
||||||
|
|
||||||
redo_sql text;
|
|
||||||
undo_sql text;
|
|
||||||
redo_cs json;
|
|
||||||
undo_cs json;
|
|
||||||
|
|
||||||
begin
|
begin
|
||||||
select value into old_value from title;
|
return '{"value": {"opetional": false, "readonly": false}}'::jsonb;
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
end;
|
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;
|
||||||
|
|||||||
37
script/sql/api/api.sql
Normal file
37
script/sql/api/api.sql
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
drop function if exists execute_command;
|
||||||
|
drop function if exists execute_update;
|
||||||
|
|
||||||
|
create function execute_update(cs jsonb) returns jsonb as
|
||||||
|
$$
|
||||||
|
declare
|
||||||
|
begin
|
||||||
|
assert cs->>'operation' = 'update';
|
||||||
|
|
||||||
|
case cs->>'type'
|
||||||
|
when 'title' then
|
||||||
|
return update_title(cs);
|
||||||
|
else
|
||||||
|
return '{}'::jsonb;
|
||||||
|
end case;
|
||||||
|
end;
|
||||||
|
$$
|
||||||
|
language plpgsql;
|
||||||
|
|
||||||
|
|
||||||
|
create function execute_command(cs jsonb) returns jsonb as
|
||||||
|
$$
|
||||||
|
declare
|
||||||
|
begin
|
||||||
|
case cs->>'operation'
|
||||||
|
when 'add' then
|
||||||
|
return '{}'::jsonb;
|
||||||
|
when 'update' then
|
||||||
|
return execute_update(cs);
|
||||||
|
when 'delete' then
|
||||||
|
return '{}'::jsonb;
|
||||||
|
else
|
||||||
|
return '{}'::jsonb;
|
||||||
|
end case;
|
||||||
|
end;
|
||||||
|
$$
|
||||||
|
language plpgsql;
|
||||||
@@ -1,5 +1,8 @@
|
|||||||
|
drop function if exists execute_undo;
|
||||||
|
drop function if exists add_operation;
|
||||||
drop function if exists get_current_operation;
|
drop function if exists get_current_operation;
|
||||||
|
|
||||||
|
|
||||||
create function get_current_operation() returns integer as
|
create function get_current_operation() returns integer as
|
||||||
$$
|
$$
|
||||||
select id from current_operation as result;
|
select id from current_operation as result;
|
||||||
@@ -7,9 +10,7 @@ $$
|
|||||||
language sql;
|
language sql;
|
||||||
|
|
||||||
|
|
||||||
drop function if exists add_operation;
|
create function add_operation(redo_sql text, undo_sql text, redo_cs jsonb, undo_cs jsonb) returns void as
|
||||||
|
|
||||||
create function add_operation(redo_sql text, undo_sql text, redo_cs json, undo_cs json) returns void as
|
|
||||||
$$
|
$$
|
||||||
declare
|
declare
|
||||||
parent_id integer;
|
parent_id integer;
|
||||||
@@ -30,9 +31,7 @@ $$
|
|||||||
language plpgsql;
|
language plpgsql;
|
||||||
|
|
||||||
|
|
||||||
drop function if exists execute_undo;
|
create function execute_undo(discard boolean) returns jsonb as
|
||||||
|
|
||||||
create function execute_undo(discard boolean) returns json as
|
|
||||||
$$
|
$$
|
||||||
#print_strict_params on
|
#print_strict_params on
|
||||||
|
|
||||||
@@ -42,7 +41,7 @@ declare
|
|||||||
begin
|
begin
|
||||||
select * into strict op_row from operation where id = get_current_operation();
|
select * into strict op_row from operation where id = get_current_operation();
|
||||||
if op_row.undo = '' then
|
if op_row.undo = '' then
|
||||||
return '{}'::json;
|
return '{}'::jsonb;
|
||||||
end if;
|
end if;
|
||||||
|
|
||||||
execute op_row.undo;
|
execute op_row.undo;
|
||||||
@@ -66,7 +65,7 @@ $$
|
|||||||
language plpgsql;
|
language plpgsql;
|
||||||
|
|
||||||
|
|
||||||
-- create function execute_redo() returns json as
|
-- create function execute_redo() returns jsonb as
|
||||||
-- $$
|
-- $$
|
||||||
-- #print_strict_params on
|
-- #print_strict_params on
|
||||||
--
|
--
|
||||||
|
|||||||
@@ -5,8 +5,8 @@ create table operation
|
|||||||
, undo text not null
|
, undo text not null
|
||||||
, parent integer references operation(id) on delete cascade
|
, parent integer references operation(id) on delete cascade
|
||||||
, redo_child integer references operation(id) -- must update before delete
|
, redo_child integer references operation(id) -- must update before delete
|
||||||
, redo_change_set json
|
, redo_change_set jsonb
|
||||||
, undo_change_set json
|
, undo_change_set jsonb
|
||||||
);
|
);
|
||||||
|
|
||||||
insert into operation (id, redo, undo) values (0, '', '');
|
insert into operation (id, redo, undo) values (0, '', '');
|
||||||
|
|||||||
@@ -30,12 +30,13 @@ sql_create = [
|
|||||||
"sql/create/26.labels.sql",
|
"sql/create/26.labels.sql",
|
||||||
"sql/create/27.backdrop.sql",
|
"sql/create/27.backdrop.sql",
|
||||||
"sql/create/28.end.sql",
|
"sql/create/28.end.sql",
|
||||||
"sql/create/operation.sql",
|
"sql/create/operation.sql"
|
||||||
]
|
]
|
||||||
|
|
||||||
sql_api = [
|
sql_api = [
|
||||||
"sql/api/operation.sql",
|
"sql/api/operation.sql",
|
||||||
"sql/api/1.title.sql"
|
"sql/api/1.title.sql",
|
||||||
|
"sql/api/api.sql"
|
||||||
]
|
]
|
||||||
|
|
||||||
sql_drop = [
|
sql_drop = [
|
||||||
|
|||||||
Reference in New Issue
Block a user