Mistři v programování na #hovnokod

SQL #370

CREATE OR REPLACE FUNCTION hex_inc(in_str character, n integer)
RETURNS character AS
$BODY$
DECLARE x varchar;
BEGIN
x=in_str;
if (substring(in_str from n for n+1)='0') or (substring(in_str from n for n+1)='1') or (substring(in_str from n for n+1)='2')
or (substring(in_str from n for n+1)='3') or (substring(in_str from n for n+1)='4') or (substring(in_str from n for n+1)='5')
or (substring(in_str from n for n+1)='6') or (substring(in_str from n for n+1)='7') or (substring(in_str from n for n+1)='8')
or (substring(in_str from n for n+1)='A') or (substring(in_str from n for n+1)='B') or (substring(in_str from n for n+1)='C')
or (substring(in_str from n for n+1)='D') or (substring(in_str from n for n+1)='E') THEN
x=raplace_char(n,x,chr((ascii(substring(in_str from n for n+1))+1))); -- inc() только для string
ELSIF (substring(in_str from n for n+1)='9') THEN
x=raplace_char(n,x,'A'); -- ну т.к. это уже 10
ELSIF (substring(in_str from n for n+1)='F') THEN
x=raplace_char(n,x,'0'); -- ну т.к. это +1 разряд :)
x=hex_inc(x,n-1);
END IF;
RETURN x;
END;
$BODY$
LANGUAGE 'plpgsql' VOLATILE
COST 100;

CREATE OR REPLACE FUNCTION raplace_char(n integer, in_str character, in_char character)
RETURNS character AS
$BODY$
Begin
IF ( n = char_length(in_str) ) THEN
RETURN substring(in_str from 1 for n-1) || in_char;
ELSIF (n = 1) THEN
RETURN in_char || substring(in_str from 2 for char_length(in_str)-1) ;
ELSE
RETURN substring(in_str from 1 for n-1) || in_char || substring(in_str from n+1 for char_length(in_str)-n);
END IF;
END;
$BODY$
LANGUAGE 'plpgsql' STABLE
COST 100;

HovnoKod,