bspkrs

Untitled

Sep 22nd, 2014
490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ------------------------------------------------------------------
  2. --  FUNCTION mcp.commit_mappings
  3. --  Author: bspkrs
  4. ------------------------------------------------------------------
  5.  
  6. CREATE OR REPLACE FUNCTION mcp.commit_mappings (
  7.     IN member_type_in text, -- 'method', 'method_param', 'field', or null (for all)
  8.     IN command_in text,
  9.     IN irc_nick_in text,
  10.     IN params_in text,
  11.     IN srg_name_in text -- if non-null only staged records for this member will be committed
  12. )
  13. RETURNS text AS $BODY$
  14. DECLARE
  15.     current_mcp_version_pid   integer;
  16.     commit_ts TIMESTAMP with time zone;
  17.     commit_cmd_hist_pid BIGINT;
  18.     is_method boolean;
  19.     is_field boolean;
  20.     is_param boolean;
  21.     member_pid integer;
  22.     row_data record;
  23.     method_cnt integer;
  24.     field_cnt integer;
  25.     param_cnt integer;
  26.     new_name text;
  27.     new_desc text;
  28.     ret_val text;
  29. BEGIN
  30.     commit_ts := now();
  31.     current_mcp_version_pid = mcp.get_current_mcp_version_pid();
  32.  
  33.     is_method := member_type_in = 'method';
  34.     is_field := member_type_in = 'field';
  35.     is_param := member_type_in = 'method_param';
  36.     method_cnt := 0;
  37.     field_cnt := 0;
  38.     param_cnt := 0;
  39.  
  40.     if srg_name_in is not null THEN
  41.         if member_type_in is null THEN
  42.             is_method := srg_name_in like 'func_%';
  43.             is_field := srg_name_in like 'field_%';
  44.             is_param := srg_name_in like 'p_%';
  45.         end if;
  46.  
  47.         if (is_method and srg_name_in not like 'func_%')
  48.            or (is_field and srg_name_in not like 'field_%')
  49.            or (is_param and srg_name_in not like 'p_%') THEN
  50.             return 'COMMIT: SRG name given does not match member type given.';
  51.         end if;
  52.  
  53.         if is_method THEN
  54.             select mcp.get_method_pid(mcp.get_current_mcp_version_pid(), srg_name_in) into member_pid;
  55.         elsif is_field THEN
  56.             select mcp.get_field_pid(mcp.get_current_mcp_version_pid(), srg_name_in) into member_pid;
  57.         elsif is_param THEN
  58.             select mcp.get_method_param_pid(mcp.get_current_mcp_version_pid(), srg_name_in) into member_pid;
  59.         end if;
  60.     end if;
  61.  
  62.     -- Only audit the command after checking for issues with the input
  63.     select mcp.audit_command(mcp.get_command_pid(command_in), irc_nick_in, params_in, commit_ts) into commit_cmd_hist_pid;
  64.     ret_val := 'Total: ';
  65.  
  66.     if srg_name_in is not null THEN
  67.         -- Commit mappings for given SRG name only
  68.         if is_method THEN
  69.             FOR row_data IN
  70.                 SELECT command_history_pid, undo_command_history_pid, method_pid, old_mcp_name,
  71.                     old_mcp_desc, new_mcp_name, new_mcp_desc, is_forced, CREATED_TS
  72.                 FROM mcp.staged_method where method_pid = member_pid
  73.             LOOP
  74.                 insert into mcp.method_history (commit_command_history_pid, command_history_pid,
  75.                     undo_command_history_pid, method_pid, old_mcp_name, old_mcp_desc, new_mcp_name,
  76.                     new_mcp_desc, is_forced, CREATED_TS)
  77.                     values (commit_cmd_hist_pid, row_data.command_history_pid, row_data.undo_command_history_pid,
  78.                     row_data.method_pid, row_data.old_mcp_name, row_data.old_mcp_desc, row_data.new_mcp_name,
  79.                     row_data.new_mcp_desc, row_data.is_forced, row_data.CREATED_TS);
  80.                 method_cnt := method_cnt + 1;
  81.             end loop;
  82.  
  83.             -- Update mcp.method with latest data
  84.             select mcp_name, comment into new_name, new_desc from mcp.method_vw
  85.             where method_pid = member_pid AND class_srg_name = srg_member_base_class;
  86.             update mcp.method set mcp_name = new_name, comment = new_desc, last_modified_ts = commit_ts
  87.             where method_pid = member_pid;
  88.  
  89.             -- Remove mcp.staged_method records
  90.             delete from mcp.staged_method where method_pid = member_pid;
  91.  
  92.             ret_val := ret_val || method_cnt || ' method changes committed';
  93.         elsif is_field THEN
  94.             FOR row_data IN
  95.                 SELECT command_history_pid, undo_command_history_pid, field_pid, old_mcp_name,
  96.                     old_mcp_desc, new_mcp_name, new_mcp_desc, is_forced, CREATED_TS
  97.                 FROM mcp.staged_field where field_pid = member_pid
  98.             LOOP
  99.                 insert into mcp.field_history (commit_command_history_pid, command_history_pid,
  100.                     undo_command_history_pid, field_pid, old_mcp_name, old_mcp_desc, new_mcp_name,
  101.                     new_mcp_desc, is_forced, CREATED_TS)
  102.                     values (commit_cmd_hist_pid, row_data.command_history_pid, row_data.undo_command_history_pid,
  103.                     row_data.field_pid, row_data.old_mcp_name, row_data.old_mcp_desc, row_data.new_mcp_name,
  104.                     row_data.new_mcp_desc, row_data.is_forced, row_data.CREATED_TS);
  105.                 field_cnt := field_cnt + 1;
  106.             end loop;
  107.  
  108.             -- Update mcp.field with latest data
  109.             select mcp_name, comment into new_name, new_desc from mcp.field_vw
  110.             where field_pid = member_pid AND class_srg_name = srg_member_base_class;
  111.             update mcp.field set mcp_name = new_name, comment = new_desc, last_modified_ts = commit_ts
  112.             where field_pid = member_pid;
  113.  
  114.             -- Remove mcp.staged_field records
  115.             delete from mcp.staged_field where field_pid = member_pid;
  116.  
  117.             ret_val := ret_val || field_cnt || ' field changes committed';
  118.         elsif is_param THEN
  119.             FOR row_data IN
  120.                 SELECT command_history_pid, undo_command_history_pid, method_param_pid, old_mcp_name,
  121.                     old_mcp_desc, new_mcp_name, new_mcp_desc, is_forced, CREATED_TS
  122.                 FROM mcp.staged_method_param where method_param_pid = member_pid
  123.             LOOP
  124.                 insert into mcp.method_param_history (commit_command_history_pid, command_history_pid,
  125.                     undo_command_history_pid, method_param_pid, old_mcp_name, old_mcp_desc, new_mcp_name,
  126.                     new_mcp_desc, is_forced, CREATED_TS)
  127.                     values (commit_cmd_hist_pid, row_data.command_history_pid, row_data.undo_command_history_pid,
  128.                     row_data.method_param_pid, row_data.old_mcp_name, row_data.old_mcp_desc, row_data.new_mcp_name,
  129.                     row_data.new_mcp_desc, row_data.is_forced, row_data.CREATED_TS);
  130.                 param_cnt := param_cnt + 1;
  131.             end loop;
  132.  
  133.             -- Update mcp.method_param with latest data
  134.             select mcp_name, comment into new_name, new_desc from mcp.method_param_vw
  135.             where method_param_pid = member_pid AND class_srg_name = srg_member_base_class;
  136.             update mcp.method_param set mcp_name = new_name, comment = new_desc, last_modified_ts = commit_ts
  137.             where method_param_pid = member_pid;
  138.  
  139.             -- Remove mcp.staged_method_param records
  140.             delete from mcp.staged_method_param where method_param_pid = member_pid;
  141.  
  142.             ret_val := ret_val || param_cnt || ' method parameter changes committed';
  143.         end if;
  144.     ELSE
  145.         -- Commit mappings for given member type, or all member types if no member type specified
  146.         if is_method or member_type_in is null THEN
  147.             FOR row_data IN
  148.                 SELECT command_history_pid, undo_command_history_pid, method_pid, old_mcp_name,
  149.                     old_mcp_desc, new_mcp_name, new_mcp_desc, is_forced, CREATED_TS
  150.                 FROM mcp.staged_method
  151.             LOOP
  152.                 insert into mcp.method_history (commit_command_history_pid, command_history_pid,
  153.                     undo_command_history_pid, method_pid, old_mcp_name, old_mcp_desc, new_mcp_name,
  154.                     new_mcp_desc, is_forced, CREATED_TS)
  155.                     values (commit_cmd_hist_pid, row_data.command_history_pid, row_data.undo_command_history_pid,
  156.                     row_data.method_pid, row_data.old_mcp_name, row_data.old_mcp_desc, row_data.new_mcp_name,
  157.                     row_data.new_mcp_desc, row_data.is_forced, row_data.CREATED_TS);
  158.                 method_cnt := method_cnt + 1;
  159.             end loop;
  160.  
  161.             -- Update mcp.method with latest data
  162.             for member_pid in
  163.                 select distinct method_pid from mcp.staged_method
  164.             loop
  165.                 select mcp_name, comment into new_name, new_desc from mcp.method_vw
  166.                 where method_pid = member_pid AND class_srg_name = srg_member_base_class;
  167.                 update mcp.method set mcp_name = new_name, comment = new_desc, last_modified_ts = commit_ts
  168.                 where method_pid = member_pid;
  169.             end loop;
  170.  
  171.             -- Remove mcp.staged_method records
  172.             truncate mcp.staged_method;
  173.  
  174.             ret_val := ret_val || method_cnt || ' method changes committed, ';
  175.         end if;
  176.  
  177.         if is_field or member_type_in is null THEN
  178.             FOR row_data IN
  179.                 SELECT command_history_pid, undo_command_history_pid, field_pid, old_mcp_name,
  180.                     old_mcp_desc, new_mcp_name, new_mcp_desc, is_forced, CREATED_TS
  181.                 FROM mcp.staged_field
  182.             LOOP
  183.                 insert into mcp.field_history (commit_command_history_pid, command_history_pid,
  184.                     undo_command_history_pid, field_pid, old_mcp_name, old_mcp_desc, new_mcp_name,
  185.                     new_mcp_desc, is_forced, CREATED_TS)
  186.                     values (commit_cmd_hist_pid, row_data.command_history_pid, row_data.undo_command_history_pid,
  187.                     row_data.field_pid, row_data.old_mcp_name, row_data.old_mcp_desc, row_data.new_mcp_name,
  188.                     row_data.new_mcp_desc, row_data.is_forced, row_data.CREATED_TS);
  189.                 field_cnt := field_cnt + 1;
  190.             end loop;
  191.  
  192.             -- Update mcp.field with latest data
  193.             for member_pid in
  194.                 select distinct field_pid from mcp.staged_field
  195.             loop
  196.                 select mcp_name, comment into new_name, new_desc from mcp.field_vw
  197.                 where field_pid = member_pid AND class_srg_name = srg_member_base_class;
  198.                 update mcp.field set mcp_name = new_name, comment = new_desc, last_modified_ts = commit_ts
  199.                 where field_pid = member_pid;
  200.             end loop;
  201.  
  202.             -- Remove mcp.staged_field records
  203.             truncate mcp.staged_field;
  204.  
  205.             ret_val := ret_val || field_cnt || ' field changes committed, ';
  206.         end if;
  207.  
  208.         if is_param or member_type_in is null THEN
  209.             FOR row_data IN
  210.                 SELECT command_history_pid, undo_command_history_pid, method_param_pid, old_mcp_name,
  211.                     old_mcp_desc, new_mcp_name, new_mcp_desc, is_forced, CREATED_TS
  212.                 FROM mcp.staged_method_param
  213.             LOOP
  214.                 insert into mcp.method_param_history (commit_command_history_pid, command_history_pid,
  215.                     undo_command_history_pid, method_param_pid, old_mcp_name, old_mcp_desc, new_mcp_name,
  216.                     new_mcp_desc, is_forced, CREATED_TS)
  217.                     values (commit_cmd_hist_pid, row_data.command_history_pid, row_data.undo_command_history_pid,
  218.                     row_data.method_param_pid, row_data.old_mcp_name, row_data.old_mcp_desc, row_data.new_mcp_name,
  219.                     row_data.new_mcp_desc, row_data.is_forced, row_data.CREATED_TS);
  220.                 param_cnt := param_cnt + 1;
  221.             end loop;
  222.  
  223.             -- Update mcp.method_param with latest data
  224.             for member_pid in
  225.                 select distinct method_param_pid from mcp.staged_method_param
  226.             loop
  227.                 select mcp_name, comment into new_name, new_desc from mcp.method_param_vw
  228.                 where method_param_pid = member_pid AND class_srg_name = srg_member_base_class;
  229.                 update mcp.method_param set mcp_name = new_name, comment = new_desc, last_modified_ts = commit_ts
  230.                 where method_param_pid = member_pid;
  231.             end loop;
  232.  
  233.             -- Remove mcp.staged_method_param records
  234.             truncate mcp.staged_method_param;
  235.  
  236.             ret_val := ret_val || param_cnt || ' method parameter changes committed';
  237.         end if;
  238.     end if;
  239.  
  240.     insert into mcp.version_control (mcp_version_pid, promoted_ts, command_history_pid)
  241.     values (mcp.get_current_mcp_version_pid(), commit_ts, commit_cmd_hist_pid);
  242.  
  243.     return trim(TRAILING ', ' from ret_val);
  244. END;
  245. $BODY$ LANGUAGE plpgsql VOLATILE COST 100;
Advertisement
Add Comment
Please, Sign In to add comment