martes, 28 de junio de 2016

Procedimientos almacenados en postgres


Language Structure

PL/pgSQL is termed a block-structured language. A block is a sequence of statements between a matched set (conjunto combinado) of DECLARE/BEGIN and END statements. Blocks can be nested meaning (lo que significa) that one block can entirely contain another block, which in turn can contain other blocks, and so on. For example, here is a PL/pgSQL function:
 1 --
 2 -- ch07.sql
 3 --
 4
 5 CREATE OR REPLACE FUNCTION my_factorial(value INTEGER) RETURNS INTEGER AS $$
 6   DECLARE
 7     arg INTEGER;
 8   BEGIN
 9
10     arg := value;
11
12     IF arg IS NULL OR arg < 0 THEN
13          RAISE NOTICE 'Invalid Number';
14          RETURN NULL;
15     ELSE
16         IF arg = 1 THEN
17           RETURN 1;
18         ELSE
19           DECLARE
20             next_value INTEGER;
21           BEGIN
22             next_value := my_factorial(arg - 1) * arg;
23             RETURN next_value;
24           END;
25        END IF;
26     END IF;
27   END;
28 $$ LANGUAGE 'plpgsql';

This function contains two blocks of code. The first block starts at line 6 and 
ends at line 27. The second block, which is nested inside the first, starts at 
line 19 and ends at line 24. The first block is called an outer block because it contains the inner block.
Source: PostgreSQL: The comprehensive guide to building, programming, and 
administering
--------------------- 


Que es eso de $$...$$ o $func$..$func$?

...the body of a PL/pgSQL function is itself a string...

The body of my_factorial() is actually the string between the opening dollar quotes (following the word AS) and the closing dollar quotes (just before the word LANGUAGE).



http://stackoverflow.com/questions/12144284/what-are-used-for-in-pl-pgsql
---------------
https://www.postgresql.org/docs/current/static/sql-syntax-lexical.html#SQL-SYNTAX-DOLLAR-QUOTING

PostgreSQL provides another way, called "dollar quoting", to write string constants. A dollar-quoted string constant consists of a dollar sign ($), an optional "tag" of zero or more characters, another dollar sign, an arbitrary sequence of characters that makes up the string content, a dollar sign, the same tag that began this dollar quote, and a dollar sign.
----------
http://stackoverflow.com/questions/12144284/what-are-used-for-in-pl-pgsql
In fact if you look under "4.1.2.2. Dollar-Quoted String Constants" in the manual, you will see that you can even use characters in between the dollar symbols and it will all count as one delimiter.
OSEA QUE ANTES TENIAN QUE SER VACIOS esas cadenas constantes $$
-----------
The dollar signs are used for dollar quoting and are in no way specific to function definitions. It can be used to replace single quotes practically anywhere in SQL scripts.
(yo vi en ebook antiguo de posgresql,que para delimitar el cuerpo de una función utilizaban  comillas simples)

The body of a function happens to be a string literal which has to be enclosed in single quotes. Dollar-quoting is a PostgreSQL-specific substitute for single quotes to avoid quoting issues inside the function body. You could write your function definition with single-quotes just as well. But then you'd have to escape all single-quotes in the body:
CREATE OR REPLACE FUNCTION check_phone_number(text)
RETURNS boolean AS
'
BEGIN
  IF NOT $1 ~  e''^\\+\\d{3}\\ \\d{3} \\d{3} \\d{3}$'' THEN
    RAISE EXCEPTION ''Malformed string "%". Expected format is +999 999'';
  END IF;
  RETURN true; 
END;
' LANGUAGE plpgsql STRICT IMMUTABLE;
This isn't such a good idea. Use dollar-quoting instead, more specifically also put a token between the$$ to make it unique - you might want to use $-quotes inside the function body, too. I do that a lot, actually.

CREATE OR REPLACE FUNCTION check_phone_number(text)
  RETURNS boolean  LANGUAGE plpgsql STRICT IMMUTABLE AS
$func$
BEGIN
 ...
END;
$func$;
***********

The SQL needed to create a new function is CREATE FUNCTION, which has the following basicsyntax:CREATE FUNCTION name ( [ ftype [, ...] ] )RETURNS rtype
AS definition
LANGUAGE 'langname'
The parts of the function definition do not need to be in this order, and a popular choice isto state the language being used before the definition, like this:
CREATE FUNCTION name ( [ ftype [, ...] ] )
RETURNS rtype
LANGUAGE 'langname'
AS definition


No hay comentarios:

Publicar un comentario