Doc update from Roberto Mello: improved versions of instr() examples.

REL7_1_STABLE
Tom Lane 25 years ago
parent e458ebfd21
commit 3311e250cd
  1. 152
      doc/src/sgml/plsql.sgml

@ -1,5 +1,5 @@
<!-- <!--
$Header: /cvsroot/pgsql/doc/src/sgml/Attic/plsql.sgml,v 2.24 2001/03/17 18:08:14 petere Exp $ $Header: /cvsroot/pgsql/doc/src/sgml/Attic/plsql.sgml,v 2.25 2001/03/23 22:07:50 tgl Exp $
--> -->
<chapter id="plpgsql"> <chapter id="plpgsql">
@ -2407,13 +2407,6 @@ WITH (isstrict, iscachable);
This function should probably be integrated into the core. This function should probably be integrated into the core.
</comment> </comment>
<para>
The third function (that takes 4 parameters) is implemented in
PL/Tcl but I plan on porting it to PL/pgSQL so in case we want to
include it in OpenACS we don't need to require PL/Tcl. Plus
PL/pgSQL should be more efficient.
</para>
<programlisting> <programlisting>
-- --
-- instr functions that mimic Oracle's counterpart -- instr functions that mimic Oracle's counterpart
@ -2424,6 +2417,7 @@ WITH (isstrict, iscachable);
-- not passed, assume 1 (search starts at first character). -- not passed, assume 1 (search starts at first character).
-- --
-- by Roberto Mello (rmello@fslc.usu.edu) -- by Roberto Mello (rmello@fslc.usu.edu)
-- modified by Robert Gaszewski (graszew@poland.com)
-- Licensed under the GPL v2 or later. -- Licensed under the GPL v2 or later.
-- --
@ -2435,7 +2429,8 @@ BEGIN
pos:= instr($1,$2,1); pos:= instr($1,$2,1);
RETURN pos; RETURN pos;
END; END;
' LANGUAGE 'plpgsql'; ' language 'plpgsql';
DROP FUNCTION instr(varchar,varchar,integer); DROP FUNCTION instr(varchar,varchar,integer);
CREATE FUNCTION instr(varchar,varchar,integer) RETURNS integer AS ' CREATE FUNCTION instr(varchar,varchar,integer) RETURNS integer AS '
@ -2444,14 +2439,12 @@ DECLARE
string_to_search ALIAS FOR $2; string_to_search ALIAS FOR $2;
beg_index ALIAS FOR $3; beg_index ALIAS FOR $3;
pos integer NOT NULL DEFAULT 0; pos integer NOT NULL DEFAULT 0;
ending integer;
temp_str varchar; temp_str varchar;
beg integer; beg integer;
length integer; length integer;
temp_int integer; ss_length integer;
BEGIN BEGIN
IF beg_index > 0 THEN IF beg_index > 0 THEN
-- Get substring from 1 to beg_index
temp_str := substring(string FROM beg_index); temp_str := substring(string FROM beg_index);
pos := position(string_to_search IN temp_str); pos := position(string_to_search IN temp_str);
@ -2461,90 +2454,91 @@ BEGIN
ELSE ELSE
RETURN pos + beg_index - 1; RETURN pos + beg_index - 1;
END IF; END IF;
ELSE ELSE
ss_length := char_length(string_to_search);
length := char_length(string); length := char_length(string);
beg := length + beg_index - ss_length + 2;
IF beg_index = -1 THEN WHILE beg > 0 LOOP
ending := length;
beg := ending;
temp_int := 1;
ELSE
ending := length - abs(beg_index);
beg := ending;
temp_int := ending - beg;
END IF;
WHILE pos = 0 AND beg &lt;&gt; 1 LOOP
temp_str := substring(string FROM beg FOR temp_int); temp_str := substring(string FROM beg FOR ss_length);
pos := position(string_to_search IN temp_str); pos := position(string_to_search IN temp_str);
-- Keep moving left IF pos > 0 THEN
RETURN beg;
END IF;
beg := beg - 1; beg := beg - 1;
temp_int := (ending - beg) + 1;
END LOOP; END LOOP;
END IF;
IF pos = 0 THEN
RETURN 0; RETURN 0;
ELSE
RETURN beg + 1;
END IF; END IF;
END; END;
' LANGUAGE 'plpgsql'; ' language 'plpgsql';
-- --
-- The next one (where all four params are passed) is in PL/Tcl -- Written by Robert Gaszewski (graszew@poland.com)
-- because I had no more patience to do it in PL/pgSQL. -- Licensed under the GPL v2 or later.
-- It'd probably be faster in PL/pgSQL (that being the reason why
-- I implemented the first two functions in PL/pgSQL) so someday I'll do it.
-- --
DROP FUNCTION instr(varchar,varchar,integer,integer); DROP FUNCTION instr(varchar,varchar,integer,integer);
CREATE FUNCTION instr(varchar,varchar,integer,integer) RETURNS integer AS ' CREATE FUNCTION instr(varchar,varchar,integer,integer) RETURNS integer AS '
set string1 $1 DECLARE
set string2 $2 string ALIAS FOR $1;
set n $3 string_to_search ALIAS FOR $2;
set m $4 beg_index ALIAS FOR $3;
occur_index ALIAS FOR $4;
if { $n > 0 } { pos integer NOT NULL DEFAULT 0;
set pos [string first $string2 $string1 [expr $n -1]] occur_number integer NOT NULL DEFAULT 0;
if { $pos < 0 } { temp_str varchar;
return 0 beg integer;
} else { i integer;
for { set i 1 } { $i < $m } { incr i } { length integer;
set pos [string first $string2 $string1 [expr $pos + 1]] ss_length integer;
if { $pos < 0 } { BEGIN
return 0 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 { $n < 0 } {
set pos [string last $string2 $string1 [expr [string length $string1] + $n]] IF i = 1 THEN
beg := beg + pos - 1;
if { $pos < 0 } { ELSE
return 0 beg := beg + pos;
} else { END IF;
for { set i 1 } { $i < $m } { incr i } {
# n is negative so we add temp_str := substring(string FROM beg + 1);
set pos [string last $string2 $string1 [expr $pos - 1]] END LOOP;
if { $pos < 0 } {
return 0 IF pos = 0 THEN
} RETURN 0;
} ELSE
} RETURN beg;
} END IF;
ELSE
if { $pos < 0 } { ss_length := char_length(string_to_search);
return 0 length := char_length(string);
} else { beg := length + beg_index - ss_length + 2;
return [expr $pos + 1]
} WHILE beg > 0 LOOP
' LANGUAGE 'pltcl'; 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';
</programlisting> </programlisting>
</sect3> </sect3>
</sect2> </sect2>

Loading…
Cancel
Save