PostgreSQL自定义函数处理特殊需求

前端之家收集整理的这篇文章主要介绍了PostgreSQL自定义函数处理特殊需求前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

工作中,在某些特殊需求下,基本的sql语句已经不能满足业务需求,需要使用函数来处理。

1. 需要新建sequence。由于部分表已经有数据,这时,新建的sequence的起始位置不能从1开始。

? @H_301_9@
1 @H_301_9@
2 @H_301_9@
3 @H_301_9@
4 @H_301_9@
5 @H_301_9@
6 @H_301_9@
7 @H_301_9@
8 @H_301_9@
9 @H_301_9@
10 @H_301_9@
11 @H_301_9@
12 @H_301_9@
13 @H_301_9@
14 @H_301_9@
15 @H_301_9@
16 @H_301_9@
17 @H_301_9@
18 @H_301_9@
19 @H_301_9@
20 @H_301_9@
21 @H_301_9@
22 @H_301_9@
23 @H_301_9@
24 @H_301_9@
25 @H_301_9@
26 @H_301_9@
27 @H_301_9@
28 @H_301_9@
29 @H_301_9@
30 @H_301_9@
31 @H_301_9@
32 @H_301_9@
CREATE OR REPLACE FUNCTION create_sequence(sequence_name character varying ,table_name sql keyword" style="border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,column_name ) @H_301_9@
--sequence_name 是需要新建的sequence名称 @H_301_9@
--table_name 是sequence作用的表 @H_301_9@
--column_name 是统计当前表的数据量的列值 @H_301_9@
RETURNS bigint AS @H_301_9@
$BODY$ @H_301_9@
DECLARE @H_301_9@
startNum int8; @H_301_9@
createsequence "varchar" := '' ; @H_301_9@
selectsequence ; @H_301_9@
@H_301_9@
BEGIN @H_301_9@
--先获取当面表中已经保存了多少数据 @H_301_9@
selectsequence := 'select COALESCE(max(' ||column_name|| '),0) + 1 FROM ' || table_name || ; @H_301_9@
--将查询出来的结果放入startNum中 @H_301_9@
execute selectsequence into startNum; @H_301_9@
--声明 创建 sequence的语句。 @H_301_9@
createsequence := 'CREATE SEQUENCE ' ||sequence_name|| ' @H_301_9@
INCREMENT 1 @H_301_9@
MINVALUE 1 @H_301_9@
MAXVALUE 9223372036854775807 @H_301_9@
START ' || startNum || ' @H_301_9@
CACHE 1' ; @H_301_9@
--执行创建sequence的sql语句 @H_301_9@
createsequence; @H_301_9@
@H_301_9@
RETURN startNum; @H_301_9@
END ; @H_301_9@
$BODY$ @H_301_9@
LANGUAGE plpgsql VOLATILE @H_301_9@
COST 100; @H_301_9@
ALTER create_sequence( sql keyword" style="border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas, ) OWNER TO postgres; @H_301_9@ @H_301_9@
@H_301_9@ @H_301_9@

2. 需求,需要将这两行数据 每列相加。

初始数据:


结果为<喎�"/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PGltZyBzcmM9"/uploadfile/Collfiles/20141219/2014121909224898.jpg" alt="\">

2.1 先将将string转换为数组。如1,2,3,4,5转换为 [1,5]

? @H_301_9@ 30 @H_301_9@
tools_str2array(_originstr text) @H_301_9@
integer [] AS @H_301_9@
$BODY$ @H_301_9@
declare _cindex INTEGER ; @H_301_9@
_arrIndex ; @H_301_9@
DECLARE _arr_str int4[]; @H_301_9@
_tmp_str VARCHAR (10); @H_301_9@
_delimeter (1); @H_301_9@
BEGIN @H_301_9@
_arrIndex:=1; @H_301_9@
_cindex:=1; @H_301_9@
_delimeter:= ',' ; @H_301_9@
@H_301_9@
--_strres:=_strres||'原始字符串是:'||_originStr; @H_301_9@
@H_301_9@
while _cindex< "length" (_originStr) loop @H_301_9@
--_strres :=_strres||'【这个是什么?】'||split_part(_originStr,_delimeter,_arrIndex); @H_301_9@
_tmp_str:=split_part(_originStr,_arrIndex); @H_301_9@
if "character_length" (_tmp_str)<1 then @H_301_9@
exit; @H_301_9@
end if; @H_301_9@
_arr_str:=_arr_str|| CAST (_tmp_str as int4); @H_301_9@
_arrIndex:=_arrIndex+1; @H_301_9@
loop; @H_301_9@
return _arr_str; @H_301_9@
; @H_301_9@
$BODY$ @H_301_9@
LANGUAGE plpgsql VOLATILE @H_301_9@
COST 100; @H_301_9@
tools_str2array(text) OWNER 2.2. 新增聚集函数

? @H_301_9@ 6 @H_301_9@
CREATE AGGREGATE hot_map_test( @H_301_9@
BASETYPE = TEXT, --输入值 @H_301_9@
SFUNC = csk_test_start,0)!important; background:none!important">--最初执行的函数 @H_301_9@
STYPE = TEXT[],0)!important; background:none!important">--输出类型 @H_301_9@
FINALFUNC = count_hot_map --最后执行函数 @H_301_9@
); @H_301_9@ @H_301_9@
@H_301_9@ @H_301_9@


2.3 新建计算函数 计算结果

? @H_301_9@ 32 @H_301_9@
33 @H_301_9@
34 @H_301_9@
35 @H_301_9@
36 @H_301_9@
37 @H_301_9@
38 @H_301_9@
39 @H_301_9@
40 @H_301_9@
41 @H_301_9@
42 @H_301_9@
43 @H_301_9@
44 @H_301_9@
45 @H_301_9@
46 @H_301_9@
47 @H_301_9@
AGGREGATE count_test( @H_301_9@
sql plain" style="border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas, @H_301_9@
sql plain" style="border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas, @H_301_9@
sql plain" style="border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas, @H_301_9@
FINALFUNC = count_hot_map @H_301_9@
); @H_301_9@
@H_301_9@
csk_test_start(a text[],s text) @H_301_9@
RETURNS text[] AS @H_301_9@
$BODY$ @H_301_9@
BEGIN @H_301_9@
a || s; @H_301_9@
; @H_301_9@
$BODY$ @H_301_9@
LANGUAGE plpgsql VOLATILE @H_301_9@
COST 100; @H_301_9@
csk_test_start(text[],text) OWNER postgres; @H_301_9@
@H_301_9@
count_hot_map(text[]) @H_301_9@
AS @H_301_9@
$BODY$ @H_301_9@
DECLARE @H_301_9@
startNum int8; @H_301_9@
spilt_sql ; @H_301_9@
; @H_301_9@
temp_array int4[]; @H_301_9@
result_array int4[]; @H_301_9@
@H_301_9@
BEGIN @H_301_9@
for index in 1..array_length($1,1) loop @H_301_9@
spilt_sql = 'select tools_str2Array(' ||$1[ index ]|| '' ')' ; @H_301_9@
spilt_sql temp_array; @H_301_9@
@H_301_9@
for array_items_index in 1..array_length(temp_array,1) loop @H_301_9@
if result_array[array_items_index] is null then @H_301_9@
result_array[array_items_index] = 0; @H_301_9@
if; @H_301_9@
@H_301_9@
result_array[array_items_index] = result_array[array_items_index] + temp_array[array_items_index]; @H_301_9@
loop; @H_301_9@
loop; @H_301_9@
result_array; @H_301_9@
; @H_301_9@
$BODY$ @H_301_9@
LANGUAGE plpgsql VOLATILE @H_301_9@
COST 100; @H_301_9@
count_hot_map(text[]) OWNER postgres; @H_301_9@ @H_301_9@
@H_301_9@ @H_301_9@

猜你在找的Postgre SQL相关文章