bspkrs icon

Untitled

bspkrs | PRO | 09/23/14 02:03:11 AM UTC | 0 ⭐ | 478 👁️ | Never ⏰ | []
PostgreSQL |

12.39 KB

|

None

|

0 👍

/

0 👎

------------------------------------------------------------------
--  FUNCTION mcp.commit_mappings
--  Author: bspkrs
------------------------------------------------------------------
 
CREATE OR REPLACE FUNCTION mcp.commit_mappings (
    IN member_type_in text, -- 'method', 'method_param', 'field', or null (for all)
    IN command_in text,
    IN irc_nick_in text,
    IN params_in text,
    IN srg_name_in text -- if non-null only staged records for this member will be committed
)
RETURNS text AS $BODY$
DECLARE
    current_mcp_version_pid   integer;
    commit_ts TIMESTAMP with time zone;
    commit_cmd_hist_pid BIGINT;
    is_method boolean;
    is_field boolean;
    is_param boolean;
    member_pid integer;
    row_data record;
    method_cnt integer;
    field_cnt integer;
    param_cnt integer;
    new_name text;
    new_desc text;
    ret_val text;
BEGIN
    commit_ts := now();
    current_mcp_version_pid = mcp.get_current_mcp_version_pid();
 
    is_method := member_type_in = 'method';
    is_field := member_type_in = 'field';
    is_param := member_type_in = 'method_param';
    method_cnt := 0;
    field_cnt := 0;
    param_cnt := 0;
 
    if srg_name_in is not null THEN
        if member_type_in is null THEN
            is_method := srg_name_in like 'func_%';
            is_field := srg_name_in like 'field_%';
            is_param := srg_name_in like 'p_%';
        end if;
 
        if (is_method and srg_name_in not like 'func_%')
           or (is_field and srg_name_in not like 'field_%')
           or (is_param and srg_name_in not like 'p_%') THEN
            return 'COMMIT: SRG name given does not match member type given.';
        end if;
 
        if is_method THEN
            select mcp.get_method_pid(mcp.get_current_mcp_version_pid(), srg_name_in) into member_pid;
        elsif is_field THEN
            select mcp.get_field_pid(mcp.get_current_mcp_version_pid(), srg_name_in) into member_pid;
        elsif is_param THEN
            select mcp.get_method_param_pid(mcp.get_current_mcp_version_pid(), srg_name_in) into member_pid;
        end if;
    end if;
 
    -- Only audit the command after checking for issues with the input
    select mcp.audit_command(mcp.get_command_pid(command_in), irc_nick_in, params_in, commit_ts) into commit_cmd_hist_pid;
    ret_val := 'Total: ';
 
    if srg_name_in is not null THEN
        -- Commit mappings for given SRG name only
        if is_method THEN
            FOR row_data IN
                SELECT command_history_pid, undo_command_history_pid, method_pid, old_mcp_name,
                    old_mcp_desc, new_mcp_name, new_mcp_desc, is_forced, CREATED_TS
                FROM mcp.staged_method where method_pid = member_pid
            LOOP
                insert into mcp.method_history (commit_command_history_pid, command_history_pid,
                    undo_command_history_pid, method_pid, old_mcp_name, old_mcp_desc, new_mcp_name,
                    new_mcp_desc, is_forced, CREATED_TS)
                    values (commit_cmd_hist_pid, row_data.command_history_pid, row_data.undo_command_history_pid,
                    row_data.method_pid, row_data.old_mcp_name, row_data.old_mcp_desc, row_data.new_mcp_name,
                    row_data.new_mcp_desc, row_data.is_forced, row_data.CREATED_TS);
                method_cnt := method_cnt + 1;
            end loop;
 
            -- Update mcp.method with latest data
            select mcp_name, comment into new_name, new_desc from mcp.method_vw
            where method_pid = member_pid AND class_srg_name = srg_member_base_class;
            update mcp.method set mcp_name = new_name, comment = new_desc, last_modified_ts = commit_ts
            where method_pid = member_pid;
 
            -- Remove mcp.staged_method records
            delete from mcp.staged_method where method_pid = member_pid;
 
            ret_val := ret_val || method_cnt || ' method changes committed';
        elsif is_field THEN
            FOR row_data IN
                SELECT command_history_pid, undo_command_history_pid, field_pid, old_mcp_name,
                    old_mcp_desc, new_mcp_name, new_mcp_desc, is_forced, CREATED_TS
                FROM mcp.staged_field where field_pid = member_pid
            LOOP
                insert into mcp.field_history (commit_command_history_pid, command_history_pid,
                    undo_command_history_pid, field_pid, old_mcp_name, old_mcp_desc, new_mcp_name,
                    new_mcp_desc, is_forced, CREATED_TS)
                    values (commit_cmd_hist_pid, row_data.command_history_pid, row_data.undo_command_history_pid,
                    row_data.field_pid, row_data.old_mcp_name, row_data.old_mcp_desc, row_data.new_mcp_name,
                    row_data.new_mcp_desc, row_data.is_forced, row_data.CREATED_TS);
                field_cnt := field_cnt + 1;
            end loop;
 
            -- Update mcp.field with latest data
            select mcp_name, comment into new_name, new_desc from mcp.field_vw
            where field_pid = member_pid AND class_srg_name = srg_member_base_class;
            update mcp.field set mcp_name = new_name, comment = new_desc, last_modified_ts = commit_ts
            where field_pid = member_pid;
 
            -- Remove mcp.staged_field records
            delete from mcp.staged_field where field_pid = member_pid;
 
            ret_val := ret_val || field_cnt || ' field changes committed';
        elsif is_param THEN
            FOR row_data IN
                SELECT command_history_pid, undo_command_history_pid, method_param_pid, old_mcp_name,
                    old_mcp_desc, new_mcp_name, new_mcp_desc, is_forced, CREATED_TS
                FROM mcp.staged_method_param where method_param_pid = member_pid
            LOOP
                insert into mcp.method_param_history (commit_command_history_pid, command_history_pid,
                    undo_command_history_pid, method_param_pid, old_mcp_name, old_mcp_desc, new_mcp_name,
                    new_mcp_desc, is_forced, CREATED_TS)
                    values (commit_cmd_hist_pid, row_data.command_history_pid, row_data.undo_command_history_pid,
                    row_data.method_param_pid, row_data.old_mcp_name, row_data.old_mcp_desc, row_data.new_mcp_name,
                    row_data.new_mcp_desc, row_data.is_forced, row_data.CREATED_TS);
                param_cnt := param_cnt + 1;
            end loop;
 
            -- Update mcp.method_param with latest data
            select mcp_name, comment into new_name, new_desc from mcp.method_param_vw
            where method_param_pid = member_pid AND class_srg_name = srg_member_base_class;
            update mcp.method_param set mcp_name = new_name, comment = new_desc, last_modified_ts = commit_ts
            where method_param_pid = member_pid;
 
            -- Remove mcp.staged_method_param records
            delete from mcp.staged_method_param where method_param_pid = member_pid;
 
            ret_val := ret_val || param_cnt || ' method parameter changes committed';
        end if;
    ELSE
        -- Commit mappings for given member type, or all member types if no member type specified
        if is_method or member_type_in is null THEN
            FOR row_data IN
                SELECT command_history_pid, undo_command_history_pid, method_pid, old_mcp_name,
                    old_mcp_desc, new_mcp_name, new_mcp_desc, is_forced, CREATED_TS
                FROM mcp.staged_method
            LOOP
                insert into mcp.method_history (commit_command_history_pid, command_history_pid,
                    undo_command_history_pid, method_pid, old_mcp_name, old_mcp_desc, new_mcp_name,
                    new_mcp_desc, is_forced, CREATED_TS)
                    values (commit_cmd_hist_pid, row_data.command_history_pid, row_data.undo_command_history_pid,
                    row_data.method_pid, row_data.old_mcp_name, row_data.old_mcp_desc, row_data.new_mcp_name,
                    row_data.new_mcp_desc, row_data.is_forced, row_data.CREATED_TS);
                method_cnt := method_cnt + 1;
            end loop;
 
            -- Update mcp.method with latest data
            for member_pid in
                select distinct method_pid from mcp.staged_method
            loop
                select mcp_name, comment into new_name, new_desc from mcp.method_vw
                where method_pid = member_pid AND class_srg_name = srg_member_base_class;
                update mcp.method set mcp_name = new_name, comment = new_desc, last_modified_ts = commit_ts
                where method_pid = member_pid;
            end loop;
 
            -- Remove mcp.staged_method records
            truncate mcp.staged_method;
 
            ret_val := ret_val || method_cnt || ' method changes committed, ';
        end if;
 
        if is_field or member_type_in is null THEN
            FOR row_data IN
                SELECT command_history_pid, undo_command_history_pid, field_pid, old_mcp_name,
                    old_mcp_desc, new_mcp_name, new_mcp_desc, is_forced, CREATED_TS
                FROM mcp.staged_field
            LOOP
                insert into mcp.field_history (commit_command_history_pid, command_history_pid,
                    undo_command_history_pid, field_pid, old_mcp_name, old_mcp_desc, new_mcp_name,
                    new_mcp_desc, is_forced, CREATED_TS)
                    values (commit_cmd_hist_pid, row_data.command_history_pid, row_data.undo_command_history_pid,
                    row_data.field_pid, row_data.old_mcp_name, row_data.old_mcp_desc, row_data.new_mcp_name,
                    row_data.new_mcp_desc, row_data.is_forced, row_data.CREATED_TS);
                field_cnt := field_cnt + 1;
            end loop;
 
            -- Update mcp.field with latest data
            for member_pid in
                select distinct field_pid from mcp.staged_field
            loop
                select mcp_name, comment into new_name, new_desc from mcp.field_vw
                where field_pid = member_pid AND class_srg_name = srg_member_base_class;
                update mcp.field set mcp_name = new_name, comment = new_desc, last_modified_ts = commit_ts
                where field_pid = member_pid;
            end loop;
 
            -- Remove mcp.staged_field records
            truncate mcp.staged_field;
 
            ret_val := ret_val || field_cnt || ' field changes committed, ';
        end if;
 
        if is_param or member_type_in is null THEN
            FOR row_data IN
                SELECT command_history_pid, undo_command_history_pid, method_param_pid, old_mcp_name,
                    old_mcp_desc, new_mcp_name, new_mcp_desc, is_forced, CREATED_TS
                FROM mcp.staged_method_param
            LOOP
                insert into mcp.method_param_history (commit_command_history_pid, command_history_pid,
                    undo_command_history_pid, method_param_pid, old_mcp_name, old_mcp_desc, new_mcp_name,
                    new_mcp_desc, is_forced, CREATED_TS)
                    values (commit_cmd_hist_pid, row_data.command_history_pid, row_data.undo_command_history_pid,
                    row_data.method_param_pid, row_data.old_mcp_name, row_data.old_mcp_desc, row_data.new_mcp_name,
                    row_data.new_mcp_desc, row_data.is_forced, row_data.CREATED_TS);
                param_cnt := param_cnt + 1;
            end loop;
 
            -- Update mcp.method_param with latest data
            for member_pid in
                select distinct method_param_pid from mcp.staged_method_param
            loop
                select mcp_name, comment into new_name, new_desc from mcp.method_param_vw
                where method_param_pid = member_pid AND class_srg_name = srg_member_base_class;
                update mcp.method_param set mcp_name = new_name, comment = new_desc, last_modified_ts = commit_ts
                where method_param_pid = member_pid;
            end loop;
 
            -- Remove mcp.staged_method_param records
            truncate mcp.staged_method_param;
 
            ret_val := ret_val || param_cnt || ' method parameter changes committed';
        end if;
    end if;
 
    insert into mcp.version_control (mcp_version_pid, promoted_ts, command_history_pid)
    values (mcp.get_current_mcp_version_pid(), commit_ts, commit_cmd_hist_pid);
 
    return trim(TRAILING ', ' from ret_val);
END;
$BODY$ LANGUAGE plpgsql VOLATILE COST 100;

Comments