11---------------------------------------------------------------------------
22--
33-- funcs.sql-
4- -- Tutorial on using functions in POSTGRES.
4+ -- Tutorial on using functions in POSTGRES.
55--
66--
77-- Copyright (c) 1994-5, Regents of the University of California
88--
9- -- $Id: funcs.source,v 1.2 1998/02/11 03:51:38 thomas Exp $
9+ -- $Id: funcs.source,v 1.3 1999/03/14 15:22:15 momjian Exp $
1010--
1111---------------------------------------------------------------------------
1212
@@ -52,14 +52,14 @@ CREATE TABLE EMP (
5252nametext,
5353salaryint4,
5454ageint4,
55- deptchar16
55+ cubiclepoint
5656);
5757
58- INSERT INTO EMP VALUES ('Sam', 1200, 16, 'toy ');
59- INSERT INTO EMP VALUES ('Claire', 5000, 32, 'shoe ');
60- INSERT INTO EMP VALUES ('Andy', -1000, 2, 'candy ');
61- INSERT INTO EMP VALUES ('Bill', 4200, 36, 'shoe ');
62- INSERT INTO EMP VALUES ('Ginger', 4800, 30, 'candy ');
58+ INSERT INTO EMP VALUES ('Sam', 1200, 16, '(1,1) ');
59+ INSERT INTO EMP VALUES ('Claire', 5000, 32, '(1,2) ');
60+ INSERT INTO EMP VALUES ('Andy', -1000, 2, '(1,3) ');
61+ INSERT INTO EMP VALUES ('Bill', 4200, 36, '(2,1) ');
62+ INSERT INTO EMP VALUES ('Ginger', 4800, 30, '(2,4) ');
6363
6464-- the argument of a function can also be a tuple. For instance,
6565-- double_salary takes a tuple of the EMP table
@@ -69,17 +69,17 @@ CREATE FUNCTION double_salary(EMP) RETURNS int4
6969
7070SELECT name, double_salary(EMP) AS dream
7171FROM EMP
72- WHERE EMP.dept = 'toy' ;
72+ WHERE EMP.cubicle ~ = '(2,1)'::point ;
7373
7474-- the return value of a function can also be a tuple. However, make sure
7575-- that the expressions in the target list is in the same order as the
7676-- columns of EMP.
7777
7878CREATE FUNCTION new_emp() RETURNS EMP
7979 AS 'SELECT \'None\'::text AS name,
80- 1000 AS salary,
81- 25 AS age,
82- \'none \'::char16 ASdept '
80+ 1000 AS salary,
81+ 25 AS age,
82+ \'(2,2) \'::point AScubicle '
8383 LANGUAGE 'sql';
8484
8585-- you can then project a column out of resulting the tuple by using the
@@ -99,33 +99,36 @@ SELECT name(high_pay()) AS overpaid;
9999-----------------------------
100100-- Creating SQL Functions with multiple SQL statements
101101--you can also create functions that do more than just a SELECT.
102+ --
103+ -- 14MAR99 Clark Evans: Does not quite work, commented out for now.
104+ --
102105-----------------------------
103106
104107-- you may have noticed that Andy has a negative salary. We'll create a
105108-- function that removes employees with negative salaries.
106-
107- SELECT * FROM EMP;
108-
109- CREATE FUNCTION clean_EMP () RETURNS int4
110- AS 'DELETE FROM EMP WHERE EMP.salary <= 0\;
111- SELECT 1 AS ignore_this'
112- LANGUAGE 'sql';
113-
114- SELECT clean_EMP();
115-
116- SELECT * FROM EMP;
109+ --
110+ -- SELECT * FROM EMP;
111+ --
112+ -- CREATE FUNCTION clean_EMP () RETURNS int4
113+ -- AS 'DELETE FROM EMP WHERE EMP.salary <= 0\;
114+ -- SELECT 1 AS ignore_this'
115+ -- LANGUAGE 'sql';
116+ --
117+ -- SELECT clean_EMP();
118+ --
119+ -- SELECT * FROM EMP;
117120
118121
119122-----------------------------
120123-- Creating C Functions
121124--in addition to SQL functions, you can also create C functions.
122- --SeeC-code/ funcs.c for the definition of the C functions.
125+ --See funcs.c for the definition of the C functions.
123126-----------------------------
124127
125128CREATE FUNCTION add_one(int4) RETURNS int4
126129 AS '_OBJWD_/funcs.so' LANGUAGE 'c';
127130
128- CREATE FUNCTIONconcat16(char16, char16 ) RETURNSchar16
131+ CREATE FUNCTIONmakepoint(point, point ) RETURNSpoint
129132 AS '_OBJWD_/funcs.so' LANGUAGE 'c';
130133
131134CREATE FUNCTION copytext(text) RETURNS text
@@ -136,7 +139,7 @@ CREATE FUNCTION c_overpaid(EMP, int4) RETURNS bool
136139
137140SELECT add_one(3) AS four;
138141
139- SELECTconcat16('abc' , 'xyz' ) ASnewchar16 ;
142+ SELECTmakepoint('(1,2)'::point , '(3,4)'::point ) ASnewpoint ;
140143
141144SELECT copytext('hello world!');
142145
@@ -148,9 +151,10 @@ WHERE name = 'Bill' or name = 'Sam';
148151
149152DROP FUNCTION c_overpaid(EMP, int4);
150153DROP FUNCTION copytext(text);
151- DROP FUNCTIONconcat16(char16,char16 );
154+ DROP FUNCTIONmakepoint(point,point );
152155DROP FUNCTION add_one(int4);
153156DROP FUNCTION clean_EMP();
157+ DROP FUNCTION high_pay();
154158DROP FUNCTION new_emp();
155159DROP FUNCTION add_em(int4, int4);
156160DROP FUNCTION one();