Commit 344190b7 authored by Bruce Momjian's avatar Bruce Momjian

Fixup for src/tutorial/func.c and src/tutorial/func.source

Removed char16 and replaced with an example using Point
as suggested by Tom Lane.  The dept field was changed to
the cubicle field denoting the row(x) and column(y) of
the employee's cube in the corporate jungle.  The C function
builds a 'compromise' cubicle from two suggested ones.

I'll try and patchup the documentation next.

Clark
parent f6a9ed04
...@@ -8,21 +8,21 @@ ...@@ -8,21 +8,21 @@
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include "postgres.h" /* for char16, etc. */ #include "postgres.h" /* for variable length type */
#include "utils/palloc.h" /* for palloc */ #include "utils/palloc.h" /* for palloc */
#include "libpq-fe.h" /* for TUPLE */
#include "executor/executor.h" /* for GetAttributeByName() */ #include "executor/executor.h" /* for GetAttributeByName() */
#include "utils/geo_decls.h" /* for point type */
/* The following prototypes declare what we assume the user declares to /* The following prototypes declare what we assume the user declares to
Postgres in his CREATE FUNCTION statement. Postgres in his CREATE FUNCTION statement.
*/ */
int add_one(int arg); int add_one(int arg);
char16 *concat16(char16 * arg1, char16 * arg2); Point *makepoint(Point *pointx, Point *pointy );
text *copytext(text *t); text *copytext(text *t);
bool c_overpaid(TUPLE t, /* the current instance of EMP */ bool c_overpaid(TupleTableSlot *t, /* the current instance of EMP */
int4 limit); int4 limit);
...@@ -32,14 +32,15 @@ add_one(int arg) ...@@ -32,14 +32,15 @@ add_one(int arg)
return arg + 1; return arg + 1;
} }
char16 * Point *
concat16(char16 * arg1, char16 * arg2) makepoint(Point *pointx, Point *pointy )
{ {
char16 *new_c16 = (char16 *) palloc(sizeof(char16)); Point *new_point = (Point *) palloc(sizeof(Point));
MemSet(new_c16, 0, sizeof(char16)); new_point->x = pointx->x;
strncpy((char *) new_c16, (char *) arg1, 16); new_point->y = pointy->y;
return (char16 *) (strncat((char *) new_c16, (char *) arg2, 16));
return new_point;
} }
text * text *
...@@ -66,7 +67,7 @@ copytext(text *t) ...@@ -66,7 +67,7 @@ copytext(text *t)
} }
bool bool
c_overpaid(TUPLE t, /* the current instance of EMP */ c_overpaid(TupleTableSlot *t, /* the current instance of EMP */
int4 limit) int4 limit)
{ {
bool isnull = false; bool isnull = false;
......
--------------------------------------------------------------------------- ---------------------------------------------------------------------------
-- --
-- funcs.sql- -- funcs.sql-
-- Tutorial on using functions in POSTGRES. -- Tutorial on using functions in POSTGRES.
-- --
-- --
-- Copyright (c) 1994-5, Regents of the University of California -- Copyright (c) 1994-5, Regents of the University of California
-- --
-- $Id: funcs.source,v 1.2 1998/02/11 03:51:38 thomas Exp $ -- $Id: funcs.source,v 1.3 1999/03/14 15:22:15 momjian Exp $
-- --
--------------------------------------------------------------------------- ---------------------------------------------------------------------------
...@@ -52,14 +52,14 @@ CREATE TABLE EMP ( ...@@ -52,14 +52,14 @@ CREATE TABLE EMP (
name text, name text,
salary int4, salary int4,
age int4, age int4,
dept char16 cubicle point
); );
INSERT INTO EMP VALUES ('Sam', 1200, 16, 'toy'); INSERT INTO EMP VALUES ('Sam', 1200, 16, '(1,1)');
INSERT INTO EMP VALUES ('Claire', 5000, 32, 'shoe'); INSERT INTO EMP VALUES ('Claire', 5000, 32, '(1,2)');
INSERT INTO EMP VALUES ('Andy', -1000, 2, 'candy'); INSERT INTO EMP VALUES ('Andy', -1000, 2, '(1,3)');
INSERT INTO EMP VALUES ('Bill', 4200, 36, 'shoe'); INSERT INTO EMP VALUES ('Bill', 4200, 36, '(2,1)');
INSERT INTO EMP VALUES ('Ginger', 4800, 30, 'candy'); INSERT INTO EMP VALUES ('Ginger', 4800, 30, '(2,4)');
-- the argument of a function can also be a tuple. For instance, -- the argument of a function can also be a tuple. For instance,
-- double_salary takes a tuple of the EMP table -- double_salary takes a tuple of the EMP table
...@@ -69,7 +69,7 @@ CREATE FUNCTION double_salary(EMP) RETURNS int4 ...@@ -69,7 +69,7 @@ CREATE FUNCTION double_salary(EMP) RETURNS int4
SELECT name, double_salary(EMP) AS dream SELECT name, double_salary(EMP) AS dream
FROM EMP FROM EMP
WHERE EMP.dept = 'toy'; WHERE EMP.cubicle ~= '(2,1)'::point;
-- the return value of a function can also be a tuple. However, make sure -- the return value of a function can also be a tuple. However, make sure
-- that the expressions in the target list is in the same order as the -- that the expressions in the target list is in the same order as the
...@@ -77,9 +77,9 @@ WHERE EMP.dept = 'toy'; ...@@ -77,9 +77,9 @@ WHERE EMP.dept = 'toy';
CREATE FUNCTION new_emp() RETURNS EMP CREATE FUNCTION new_emp() RETURNS EMP
AS 'SELECT \'None\'::text AS name, AS 'SELECT \'None\'::text AS name,
1000 AS salary, 1000 AS salary,
25 AS age, 25 AS age,
\'none\'::char16 AS dept' \'(2,2)\'::point AS cubicle'
LANGUAGE 'sql'; LANGUAGE 'sql';
-- you can then project a column out of resulting the tuple by using the -- you can then project a column out of resulting the tuple by using the
...@@ -99,33 +99,36 @@ SELECT name(high_pay()) AS overpaid; ...@@ -99,33 +99,36 @@ SELECT name(high_pay()) AS overpaid;
----------------------------- -----------------------------
-- Creating SQL Functions with multiple SQL statements -- Creating SQL Functions with multiple SQL statements
-- you can also create functions that do more than just a SELECT. -- you can also create functions that do more than just a SELECT.
--
-- 14MAR99 Clark Evans: Does not quite work, commented out for now.
--
----------------------------- -----------------------------
-- you may have noticed that Andy has a negative salary. We'll create a -- you may have noticed that Andy has a negative salary. We'll create a
-- function that removes employees with negative salaries. -- function that removes employees with negative salaries.
--
SELECT * FROM EMP; -- SELECT * FROM EMP;
--
CREATE FUNCTION clean_EMP () RETURNS int4 -- CREATE FUNCTION clean_EMP () RETURNS int4
AS 'DELETE FROM EMP WHERE EMP.salary <= 0\; -- AS 'DELETE FROM EMP WHERE EMP.salary <= 0\;
SELECT 1 AS ignore_this' -- SELECT 1 AS ignore_this'
LANGUAGE 'sql'; -- LANGUAGE 'sql';
--
SELECT clean_EMP(); -- SELECT clean_EMP();
--
SELECT * FROM EMP; -- SELECT * FROM EMP;
----------------------------- -----------------------------
-- Creating C Functions -- Creating C Functions
-- in addition to SQL functions, you can also create C functions. -- in addition to SQL functions, you can also create C functions.
-- See C-code/funcs.c for the definition of the C functions. -- See funcs.c for the definition of the C functions.
----------------------------- -----------------------------
CREATE FUNCTION add_one(int4) RETURNS int4 CREATE FUNCTION add_one(int4) RETURNS int4
AS '_OBJWD_/funcs.so' LANGUAGE 'c'; AS '_OBJWD_/funcs.so' LANGUAGE 'c';
CREATE FUNCTION concat16(char16, char16) RETURNS char16 CREATE FUNCTION makepoint(point, point) RETURNS point
AS '_OBJWD_/funcs.so' LANGUAGE 'c'; AS '_OBJWD_/funcs.so' LANGUAGE 'c';
CREATE FUNCTION copytext(text) RETURNS text CREATE FUNCTION copytext(text) RETURNS text
...@@ -136,7 +139,7 @@ CREATE FUNCTION c_overpaid(EMP, int4) RETURNS bool ...@@ -136,7 +139,7 @@ CREATE FUNCTION c_overpaid(EMP, int4) RETURNS bool
SELECT add_one(3) AS four; SELECT add_one(3) AS four;
SELECT concat16('abc', 'xyz') AS newchar16; SELECT makepoint('(1,2)'::point, '(3,4)'::point ) AS newpoint;
SELECT copytext('hello world!'); SELECT copytext('hello world!');
...@@ -148,9 +151,10 @@ WHERE name = 'Bill' or name = 'Sam'; ...@@ -148,9 +151,10 @@ WHERE name = 'Bill' or name = 'Sam';
DROP FUNCTION c_overpaid(EMP, int4); DROP FUNCTION c_overpaid(EMP, int4);
DROP FUNCTION copytext(text); DROP FUNCTION copytext(text);
DROP FUNCTION concat16(char16,char16); DROP FUNCTION makepoint(point,point);
DROP FUNCTION add_one(int4); DROP FUNCTION add_one(int4);
DROP FUNCTION clean_EMP(); DROP FUNCTION clean_EMP();
DROP FUNCTION high_pay();
DROP FUNCTION new_emp(); DROP FUNCTION new_emp();
DROP FUNCTION add_em(int4, int4); DROP FUNCTION add_em(int4, int4);
DROP FUNCTION one(); DROP FUNCTION one();
......
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