当前位置:网站首页>pgsql_ UDF01_ jx

pgsql_ UDF01_ jx

2022-06-26 08:40:00 Algorithmic Pilgrim

  1. Need new sequence. Because some tables already have data , At this time , New sequence The starting position of cannot be from 1 Start .

CREATE OR REPLACE FUNCTION create_sequence(sequence_name character varying, table_name character varying, column_name character varying)
–sequence_name It needs to be created sequence name
–table_name yes sequence Action table
–column_name It is the column value that counts the data volume of the current table
RETURNS bigint AS
B O D Y BODY BODY
DECLARE
startNum int8;
createsequence “varchar” := ‘’;
selectsequence “varchar” := ‘’;

BEGIN
    -- First, get how much data has been saved in the face-to-face table 
    selectsequence := 'select COALESCE(max('||column_name||'), 0) + 1 FROM '|| table_name ||'';
    -- Put the query results into startNum in 
execute selectsequence into startNum;
    -- Statement   establish  sequence The sentence of .
    createsequence := 'CREATE SEQUENCE '||sequence_name||'
 INCREMENT 1
 MINVALUE 1
 MAXVALUE 9223372036854775807
 START '|| startNum ||' 
 CACHE 1';
-- Execution creation sequence Of SQL sentence 
execute createsequence;
  
    RETURN startNum;
END;

B O D Y BODY BODY
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION create_sequence(character varying, character varying, character varying) OWNER TO postgres;

  1. demand , These two lines of data need to be Add each column .
    data  Insert picture description here
    2.1 First will string Convert to array . Such as 1,2,3,4,5 Convert to [1,2,3,4,5]
    CREATE OR REPLACE FUNCTION tools_str2array(_originstr text)
    RETURNS integer[] AS
    B O D Y BODY BODY
    declare _cindex INTEGER;
    declare _arrIndex INTEGER;
    DECLARE _arr_str int4[];
    DECLARE _tmp_str VARCHAR(10);
    DECLARE _delimeter VARCHAR(1);
    BEGIN
    _arrIndex:=1;
    _cindex:=1;
    _delimeter:=’,’;

–_strres:=_strres||‘ The original string is :’||_originStr;

while _cindex<“length”(_originStr) loop
–_strres :=_strres||’【 What is this? ?】’||split_part(_originStr, _delimeter, _arrIndex);
_tmp_str:=split_part(_originStr, _delimeter, _arrIndex);
if “character_length”(_tmp_str)<1 then
exit;
end if;
_arr_str:=_arr_str|| CAST(_tmp_str as int4);
_arrIndex:=_arrIndex+1;
END loop;
return _arr_str;
end;
B O D Y BODY BODY
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION tools_str2array(text) OWNER TO postgres;

2.2. New aggregate function .
CREATE AGGREGATE hot_map_test(
BASETYPE = TEXT,– The input values
SFUNC = csk_test_start,– The function that was originally executed
STYPE = TEXT[],– The output type
FINALFUNC = count_hot_map– The last function executed
);

2.3 New calculation function The result of the calculation is
CREATE AGGREGATE count_test(
BASETYPE = TEXT,
SFUNC = csk_test_start,
STYPE = TEXT[],
FINALFUNC = count_hot_map
);

CREATE OR REPLACE FUNCTION csk_test_start(a text[], s text)
RETURNS text[] AS
B O D Y BODY BODY
BEGIN
RETURN a || s;
END;
B O D Y BODY BODY
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION csk_test_start(text[], text) OWNER TO postgres;

CREATE OR REPLACE FUNCTION count_hot_map(text[])
RETURNS integer[] AS
B O D Y BODY BODY
DECLARE
startNum int8;
spilt_sql “varchar” := ‘’;
selectsequence “varchar” := ‘’;
temp_array int4[];
result_array int4[];

BEGIN
    for index in 1..array_length($1 , 1) loop
        spilt_sql = 'select tools_str2Array('''||$1[index]||''')';
        execute spilt_sql into temp_array;
         
        for array_items_index in 1..array_length(temp_array, 1) loop
            if result_array[array_items_index] is null then
               result_array[array_items_index] = 0;
            end if;  

            result_array[array_items_index] = result_array[array_items_index] + temp_array[array_items_index];
        end loop;
    end loop;
RETURN result_array;
END;

B O D Y BODY BODY
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION count_hot_map(text[]) OWNER TO postgres;

原网站

版权声明
本文为[Algorithmic Pilgrim]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202170555218825.html