From 80a06b85cfe5499de3235e2a3b1be566e7889487 Mon Sep 17 00:00:00 2001 From: Carlos Ruiz Date: Thu, 13 Aug 2009 20:00:51 +0000 Subject: [PATCH] BF [1986751] - RequestEmailProcessor InStr Postgresql https://sourceforge.net/tracker/?func=detail&atid=879332&aid=1986751&group_id=176962 --- db/ddlutils/postgresql/functions/INSTR.sql | 118 ++++++++++++++++++ .../353a-trunk/oracle/528_1986751_INSTR.sql | 1 + .../postgresql/528_1986751_INSTR.sql | 118 ++++++++++++++++++ 3 files changed, 237 insertions(+) create mode 100644 db/ddlutils/postgresql/functions/INSTR.sql create mode 100644 migration/353a-trunk/oracle/528_1986751_INSTR.sql create mode 100644 migration/353a-trunk/postgresql/528_1986751_INSTR.sql diff --git a/db/ddlutils/postgresql/functions/INSTR.sql b/db/ddlutils/postgresql/functions/INSTR.sql new file mode 100644 index 0000000000..d724be7690 --- /dev/null +++ b/db/ddlutils/postgresql/functions/INSTR.sql @@ -0,0 +1,118 @@ +-- Taken from http://developer.postgresql.org/pgdocs/postgres/plpgsql-porting.html#PLPGSQL-PORTING-APPENDIX + +-- +-- instr functions that mimic Oracle's counterpart +-- Syntax: instr(string1, string2, [n], [m]) where [] denotes optional parameters. +-- +-- Searches string1 beginning at the nth character for the mth occurrence +-- of string2. If n is negative, search backwards. If m is not passed, +-- assume 1 (search starts at first character). +-- + +CREATE FUNCTION instr(varchar, varchar) RETURNS integer AS $$ +DECLARE + pos integer; +BEGIN + pos:= instr($1, $2, 1); + RETURN pos; +END; +$$ LANGUAGE plpgsql STRICT IMMUTABLE; + + +CREATE FUNCTION instr(string varchar, string_to_search varchar, beg_index integer) +RETURNS integer AS $$ +DECLARE + pos integer NOT NULL DEFAULT 0; + temp_str varchar; + beg integer; + length integer; + ss_length integer; +BEGIN + IF beg_index > 0 THEN + temp_str := substring(string FROM beg_index); + pos := position(string_to_search IN temp_str); + + IF pos = 0 THEN + RETURN 0; + ELSE + RETURN pos + beg_index - 1; + END IF; + ELSE + ss_length := char_length(string_to_search); + length := char_length(string); + beg := length + beg_index - ss_length + 2; + + WHILE beg > 0 LOOP + temp_str := substring(string FROM beg FOR ss_length); + pos := position(string_to_search IN temp_str); + + IF pos > 0 THEN + RETURN beg; + END IF; + + beg := beg - 1; + END LOOP; + + RETURN 0; + END IF; +END; +$$ LANGUAGE plpgsql STRICT IMMUTABLE; + + +CREATE FUNCTION instr(string varchar, string_to_search varchar, + beg_index integer, occur_index integer) +RETURNS integer AS $$ +DECLARE + pos integer NOT NULL DEFAULT 0; + occur_number integer NOT NULL DEFAULT 0; + temp_str varchar; + beg integer; + i integer; + length integer; + ss_length integer; +BEGIN + IF beg_index > 0 THEN + beg := beg_index; + temp_str := substring(string FROM beg_index); + + FOR i IN 1..occur_index LOOP + pos := position(string_to_search IN temp_str); + + IF i = 1 THEN + beg := beg + pos - 1; + ELSE + beg := beg + pos; + END IF; + + temp_str := substring(string FROM beg + 1); + END LOOP; + + IF pos = 0 THEN + RETURN 0; + ELSE + RETURN beg; + END IF; + ELSE + ss_length := char_length(string_to_search); + length := char_length(string); + beg := length + beg_index - ss_length + 2; + + WHILE beg > 0 LOOP + temp_str := substring(string FROM beg FOR ss_length); + pos := position(string_to_search IN temp_str); + + IF pos > 0 THEN + occur_number := occur_number + 1; + + IF occur_number = occur_index THEN + RETURN beg; + END IF; + END IF; + + beg := beg - 1; + END LOOP; + + RETURN 0; + END IF; +END; +$$ LANGUAGE plpgsql STRICT IMMUTABLE; diff --git a/migration/353a-trunk/oracle/528_1986751_INSTR.sql b/migration/353a-trunk/oracle/528_1986751_INSTR.sql new file mode 100644 index 0000000000..57c0bd32bd --- /dev/null +++ b/migration/353a-trunk/oracle/528_1986751_INSTR.sql @@ -0,0 +1 @@ +-- Just for postgresql diff --git a/migration/353a-trunk/postgresql/528_1986751_INSTR.sql b/migration/353a-trunk/postgresql/528_1986751_INSTR.sql new file mode 100644 index 0000000000..d724be7690 --- /dev/null +++ b/migration/353a-trunk/postgresql/528_1986751_INSTR.sql @@ -0,0 +1,118 @@ +-- Taken from http://developer.postgresql.org/pgdocs/postgres/plpgsql-porting.html#PLPGSQL-PORTING-APPENDIX + +-- +-- instr functions that mimic Oracle's counterpart +-- Syntax: instr(string1, string2, [n], [m]) where [] denotes optional parameters. +-- +-- Searches string1 beginning at the nth character for the mth occurrence +-- of string2. If n is negative, search backwards. If m is not passed, +-- assume 1 (search starts at first character). +-- + +CREATE FUNCTION instr(varchar, varchar) RETURNS integer AS $$ +DECLARE + pos integer; +BEGIN + pos:= instr($1, $2, 1); + RETURN pos; +END; +$$ LANGUAGE plpgsql STRICT IMMUTABLE; + + +CREATE FUNCTION instr(string varchar, string_to_search varchar, beg_index integer) +RETURNS integer AS $$ +DECLARE + pos integer NOT NULL DEFAULT 0; + temp_str varchar; + beg integer; + length integer; + ss_length integer; +BEGIN + IF beg_index > 0 THEN + temp_str := substring(string FROM beg_index); + pos := position(string_to_search IN temp_str); + + IF pos = 0 THEN + RETURN 0; + ELSE + RETURN pos + beg_index - 1; + END IF; + ELSE + ss_length := char_length(string_to_search); + length := char_length(string); + beg := length + beg_index - ss_length + 2; + + WHILE beg > 0 LOOP + temp_str := substring(string FROM beg FOR ss_length); + pos := position(string_to_search IN temp_str); + + IF pos > 0 THEN + RETURN beg; + END IF; + + beg := beg - 1; + END LOOP; + + RETURN 0; + END IF; +END; +$$ LANGUAGE plpgsql STRICT IMMUTABLE; + + +CREATE FUNCTION instr(string varchar, string_to_search varchar, + beg_index integer, occur_index integer) +RETURNS integer AS $$ +DECLARE + pos integer NOT NULL DEFAULT 0; + occur_number integer NOT NULL DEFAULT 0; + temp_str varchar; + beg integer; + i integer; + length integer; + ss_length integer; +BEGIN + IF beg_index > 0 THEN + beg := beg_index; + temp_str := substring(string FROM beg_index); + + FOR i IN 1..occur_index LOOP + pos := position(string_to_search IN temp_str); + + IF i = 1 THEN + beg := beg + pos - 1; + ELSE + beg := beg + pos; + END IF; + + temp_str := substring(string FROM beg + 1); + END LOOP; + + IF pos = 0 THEN + RETURN 0; + ELSE + RETURN beg; + END IF; + ELSE + ss_length := char_length(string_to_search); + length := char_length(string); + beg := length + beg_index - ss_length + 2; + + WHILE beg > 0 LOOP + temp_str := substring(string FROM beg FOR ss_length); + pos := position(string_to_search IN temp_str); + + IF pos > 0 THEN + occur_number := occur_number + 1; + + IF occur_number = occur_index THEN + RETURN beg; + END IF; + END IF; + + beg := beg - 1; + END LOOP; + + RETURN 0; + END IF; +END; +$$ LANGUAGE plpgsql STRICT IMMUTABLE;