Commit c24216be authored by Bruce Momjian's avatar Bruce Momjian

Fix reverse FOR loop to work properly, uppercase cleanup in file.

parent ec614c98
<!-- <!--
$Header: /cvsroot/pgsql/doc/src/sgml/Attic/plsql.sgml,v 2.42 2001/10/09 04:55:11 tgl Exp $ $Header: /cvsroot/pgsql/doc/src/sgml/Attic/plsql.sgml,v 2.43 2001/10/12 21:19:09 momjian Exp $
--> -->
<chapter id="plpgsql"> <chapter id="plpgsql">
...@@ -228,8 +228,8 @@ END; ...@@ -228,8 +228,8 @@ END;
when you reload the file, it'll drop your functions and then when you reload the file, it'll drop your functions and then
re-create them. For example: re-create them. For example:
<programlisting> <programlisting>
drop function testfunc(integer); DROP FUNCTION testfunc(integer);
create function testfunc(integer) returns integer as ' CREATE FUNCTION testfunc(INTEGER) RETURNS INTEGER AS '
.... ....
end; end;
' language 'plpgsql'; ' language 'plpgsql';
...@@ -503,7 +503,7 @@ BEGIN ...@@ -503,7 +503,7 @@ BEGIN
user_id := users_rec.user_id; user_id := users_rec.user_id;
... ...
create function cs_refresh_one_mv(integer) returns integer as ' CREATE FUNCTION cs_refresh_one_mv(INTEGER) RETURNS INTEGER AS '
DECLARE DECLARE
key ALIAS FOR $1; key ALIAS FOR $1;
table_data cs_materialized_views%ROWTYPE; table_data cs_materialized_views%ROWTYPE;
...@@ -585,7 +585,7 @@ SELECT <replaceable>expression</replaceable> ...@@ -585,7 +585,7 @@ SELECT <replaceable>expression</replaceable>
is a difference between what these two functions do: is a difference between what these two functions do:
<programlisting> <programlisting>
CREATE FUNCTION logfunc1 (text) RETURNS timestamp AS ' CREATE FUNCTION logfunc1 (TEXT) RETURNS TIMESTAMP AS '
DECLARE DECLARE
logtxt ALIAS FOR $1; logtxt ALIAS FOR $1;
BEGIN BEGIN
...@@ -598,7 +598,7 @@ CREATE FUNCTION logfunc1 (text) RETURNS timestamp AS ' ...@@ -598,7 +598,7 @@ CREATE FUNCTION logfunc1 (text) RETURNS timestamp AS '
and and
<programlisting> <programlisting>
CREATE FUNCTION logfunc2 (text) RETURNS timestamp AS ' CREATE FUNCTION logfunc2 (TEXT) RETURNS TIMESTAMP AS '
DECLARE DECLARE
logtxt ALIAS FOR $1; logtxt ALIAS FOR $1;
curtime timestamp; curtime timestamp;
...@@ -793,7 +793,7 @@ DECLARE ...@@ -793,7 +793,7 @@ DECLARE
a_output varchar(4000); a_output varchar(4000);
BEGIN BEGIN
a_output := ''CREATE FUNCTION cs_find_referrer_type(varchar,varchar,varchar) a_output := ''CREATE FUNCTION cs_find_referrer_type(varchar,varchar,varchar)
RETURNS varchar AS '''' RETURNS VARCHAR AS ''''
DECLARE DECLARE
v_host ALIAS FOR $1; v_host ALIAS FOR $1;
v_domain ALIAS FOR $2; v_domain ALIAS FOR $2;
...@@ -1154,7 +1154,7 @@ FOR i IN 1..10 LOOP ...@@ -1154,7 +1154,7 @@ FOR i IN 1..10 LOOP
RAISE NOTICE ''i is %'',i; RAISE NOTICE ''i is %'',i;
END LOOP; END LOOP;
FOR i IN REVERSE 1..10 LOOP FOR i IN REVERSE 10..1 LOOP
-- some expressions here -- some expressions here
END LOOP; END LOOP;
</programlisting> </programlisting>
...@@ -1289,7 +1289,7 @@ END LOOP; ...@@ -1289,7 +1289,7 @@ END LOOP;
<para> <para>
<programlisting> <programlisting>
create function cs_refresh_mviews () returns integer as ' create function cs_refresh_mviews () returns INTEGER as '
DECLARE DECLARE
mviews RECORD; mviews RECORD;
...@@ -1642,7 +1642,7 @@ CREATE TRIGGER emp_stamp BEFORE INSERT OR UPDATE ON emp ...@@ -1642,7 +1642,7 @@ CREATE TRIGGER emp_stamp BEFORE INSERT OR UPDATE ON emp
</para> </para>
<programlisting> <programlisting>
CREATE FUNCTION add_one (integer) RETURNS integer AS ' CREATE FUNCTION add_one (integer) RETURNS INTEGER AS '
BEGIN BEGIN
RETURN $1 + 1; RETURN $1 + 1;
END; END;
...@@ -1659,7 +1659,7 @@ CREATE FUNCTION add_one (integer) RETURNS integer AS ' ...@@ -1659,7 +1659,7 @@ CREATE FUNCTION add_one (integer) RETURNS integer AS '
</para> </para>
<programlisting> <programlisting>
CREATE FUNCTION concat_text (text, text) RETURNS text AS ' CREATE FUNCTION concat_text (TEXT, TEXT) RETURNS TEXT AS '
BEGIN BEGIN
RETURN $1 || $2; RETURN $1 || $2;
END; END;
...@@ -1682,7 +1682,7 @@ CREATE FUNCTION concat_text (text, text) RETURNS text AS ' ...@@ -1682,7 +1682,7 @@ CREATE FUNCTION concat_text (text, text) RETURNS text AS '
</para> </para>
<programlisting> <programlisting>
CREATE FUNCTION c_overpaid (EMP, integer) RETURNS boolean AS ' CREATE FUNCTION c_overpaid (EMP, INTEGER) RETURNS BOOLEAN AS '
DECLARE DECLARE
emprec ALIAS FOR $1; emprec ALIAS FOR $1;
sallim ALIAS FOR $2; sallim ALIAS FOR $2;
...@@ -1999,9 +1999,9 @@ SHOW ERRORS; ...@@ -1999,9 +1999,9 @@ SHOW ERRORS;
PostgreSQL: PostgreSQL:
<programlisting> <programlisting>
DROP FUNCTION cs_fmt_browser_version(varchar, varchar); DROP FUNCTION cs_fmt_browser_version(VARCHAR, VARCHAR);
CREATE FUNCTION cs_fmt_browser_version(varchar, varchar) CREATE FUNCTION cs_fmt_browser_version(VARCHAR, VARCHAR)
RETURNS varchar AS ' RETURNS VARCHAR AS '
DECLARE DECLARE
v_name ALIAS FOR $1; v_name ALIAS FOR $1;
v_version ALIAS FOR $2; v_version ALIAS FOR $2;
...@@ -2058,13 +2058,13 @@ show errors ...@@ -2058,13 +2058,13 @@ show errors
Here is how this function would end up in PostgreSQL: Here is how this function would end up in PostgreSQL:
<programlisting> <programlisting>
CREATE FUNCTION cs_update_referrer_type_proc() RETURNS integer AS ' CREATE FUNCTION cs_update_referrer_type_proc() RETURNS INTEGER AS '
DECLARE DECLARE
referrer_keys RECORD; -- Declare a generic record to be used in a FOR referrer_keys RECORD; -- Declare a generic record to be used in a FOR
a_output varchar(4000); a_output varchar(4000);
BEGIN BEGIN
a_output := ''CREATE FUNCTION cs_find_referrer_type(varchar,varchar,varchar) a_output := ''CREATE FUNCTION cs_find_referrer_type(VARCHAR,VARCHAR,VARCHAR)
RETURNS varchar AS '''' RETURNS VARCHAR AS ''''
DECLARE DECLARE
v_host ALIAS FOR $1; v_host ALIAS FOR $1;
v_domain ALIAS FOR $2; v_domain ALIAS FOR $2;
...@@ -2152,8 +2152,8 @@ show errors; ...@@ -2152,8 +2152,8 @@ show errors;
Here is how this procedure could be translated for PostgreSQL: Here is how this procedure could be translated for PostgreSQL:
<programlisting> <programlisting>
drop function cs_parse_url_host(varchar); drop function cs_parse_url_host(VARCHAR);
create function cs_parse_url_host(varchar) returns varchar as ' create function cs_parse_url_host(VARCHAR) RETURNS VARCHAR AS '
declare declare
v_url ALIAS FOR $1; v_url ALIAS FOR $1;
v_host varchar; v_host varchar;
...@@ -2282,36 +2282,41 @@ show errors ...@@ -2282,36 +2282,41 @@ show errors
So let's see one of the ways we could port this procedure to <application>PL/pgSQL</>: So let's see one of the ways we could port this procedure to <application>PL/pgSQL</>:
<programlisting> <programlisting>
drop function cs_create_job(integer); drop function cs_create_job(INTEGER);
create function cs_create_job(integer) returns integer as ' declare create function cs_create_job(INTEGER) RETURNS INTEGER AS ' DECLARE
v_job_id alias for $1; v_job_id ALIAS FOR $1;
a_running_job_count integer; a_running_job_count INTEGER;
a_num integer; a_num integer;
-- pragma autonomous_transaction; -- pragma autonomous_transaction;
begin BEGIN
lock table cs_jobs in exclusive mode; LOCK TABLE cs_jobs IN EXCLUSIVE MODE;
select count(*) into a_running_job_count from cs_jobs where end_stamp is null; SELECT count(*) INTO a_running_job_count
FROM cs_jobs
if a_running_job_count > 0 then WHERE end_stamp IS NULL;
-- commit; -- free lock
raise exception ''Unable to create a new job: a job is currently running.''; IF a_running_job_count > 0
end if; THEN
-- COMMIT; -- free lock
RAISE EXCEPTION ''Unable to create a new job: a job is currently running.'';
END IF;
delete from cs_active_job; DELETE FROM cs_active_job;
insert into cs_active_job(job_id) values(v_job_id); INSERT INTO cs_active_job(job_id) VALUES (v_job_id);
SELECT count(*) into a_num FROM cs_jobs WHERE job_id=v_job_id; SELECT count(*) into a_num
FROM cs_jobs
WHERE job_id=v_job_id;
IF NOT FOUND THEN -- If nothing was returned in the last query IF NOT FOUND THEN -- If nothing was returned in the last query
-- This job is not in the table so lets insert it. -- This job is not in the table so lets insert it.
insert into cs_jobs(job_id, start_stamp) values(v_job_id, sysdate()); INSERT INTO cs_jobs(job_id, start_stamp) VALUES (v_job_id, sysdate());
return 1; RETURN 1;
ELSE ELSE
raise NOTICE ''Job already running.'';<co id="co.plpgsql-porting-raise"> RAISE NOTICE ''Job already running.'';<co id="co.plpgsql-porting-raise">
END IF; END IF;
return 0; RETURN 0;
end; END;
' language 'plpgsql'; ' LANGUAGE 'plpgsql';
</programlisting> </programlisting>
<calloutlist> <calloutlist>
...@@ -2382,8 +2387,8 @@ show errors ...@@ -2382,8 +2387,8 @@ show errors
package would become something like this: package would become something like this:
<programlisting> <programlisting>
CREATE FUNCTION acs__add_user(integer,integer,varchar,datetime,integer,integer,...) CREATE FUNCTION acs__add_user(INTEGER,INTEGER,VARCHAR,DATETIME,INTEGER,INTEGER,...)
RETURNS integer AS ' RETURNS INTEGER AS '
DECLARE DECLARE
user_id ALIAS FOR $1; user_id ALIAS FOR $1;
object_type ALIAS FOR $2; object_type ALIAS FOR $2;
...@@ -2397,7 +2402,7 @@ BEGIN ...@@ -2397,7 +2402,7 @@ BEGIN
v_user_id := acs_user__new(user_id,object_type,creation_date,creation_user,creation_ip, ...); v_user_id := acs_user__new(user_id,object_type,creation_date,creation_user,creation_ip, ...);
... ...
return v_user_id; RETURN v_user_id;
END; END;
' LANGUAGE 'plpgsql'; ' LANGUAGE 'plpgsql';
</programlisting> </programlisting>
...@@ -2441,7 +2446,7 @@ END; ...@@ -2441,7 +2446,7 @@ END;
FUNCTION</command> statement. Something like: FUNCTION</command> statement. Something like:
<programlisting> <programlisting>
CREATE FUNCTION foo(...) RETURNS integer AS ' CREATE FUNCTION foo(...) RETURNS INTEGER AS '
... ...
' LANGUAGE 'plpgsql' ' LANGUAGE 'plpgsql'
WITH (isstrict, iscachable); WITH (isstrict, iscachable);
...@@ -2479,7 +2484,7 @@ WITH (isstrict, iscachable); ...@@ -2479,7 +2484,7 @@ WITH (isstrict, iscachable);
-- --
DROP FUNCTION instr(varchar,varchar); DROP FUNCTION instr(varchar,varchar);
CREATE FUNCTION instr(varchar,varchar) RETURNS integer AS ' CREATE FUNCTION instr(VARCHAR,VARCHAR) RETURNS INTEGER AS '
DECLARE DECLARE
pos integer; pos integer;
BEGIN BEGIN
...@@ -2489,8 +2494,8 @@ END; ...@@ -2489,8 +2494,8 @@ 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 '
DECLARE DECLARE
string ALIAS FOR $1; string ALIAS FOR $1;
string_to_search ALIAS FOR $2; string_to_search ALIAS FOR $2;
...@@ -2536,8 +2541,8 @@ END; ...@@ -2536,8 +2541,8 @@ END;
-- Written by Robert Gaszewski (graszew@poland.com) -- Written by Robert Gaszewski (graszew@poland.com)
-- Licensed under the GPL v2 or later. -- Licensed under the GPL v2 or later.
-- --
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 '
DECLARE DECLARE
string ALIAS FOR $1; string ALIAS FOR $1;
string_to_search ALIAS FOR $2; string_to_search ALIAS FOR $2;
...@@ -2545,11 +2550,11 @@ DECLARE ...@@ -2545,11 +2550,11 @@ DECLARE
occur_index ALIAS FOR $4; occur_index ALIAS FOR $4;
pos integer NOT NULL DEFAULT 0; pos integer NOT NULL DEFAULT 0;
occur_number integer NOT NULL DEFAULT 0; occur_number integer NOT NULL DEFAULT 0;
temp_str varchar; temp_str VARCHAR;
beg integer; beg INTEGER;
i integer; i INTEGER;
length integer; length INTEGER;
ss_length integer; ss_length INTEGER;
BEGIN BEGIN
IF beg_index > 0 THEN IF beg_index > 0 THEN
beg := beg_index; beg := beg_index;
...@@ -2595,7 +2600,7 @@ BEGIN ...@@ -2595,7 +2600,7 @@ BEGIN
RETURN 0; RETURN 0;
END IF; END IF;
END; END;
' language 'plpgsql'; ' LANGUAGE 'plpgsql';
</programlisting> </programlisting>
</sect3> </sect3>
</sect2> </sect2>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment