]> granicus.if.org Git - postgresql/blob - src/pl/plpgsql/src/pl_handler.c
elog mop-up.
[postgresql] / src / pl / plpgsql / src / pl_handler.c
1 /**********************************************************************
2  * pl_handler.c         - Handler for the PL/pgSQL
3  *                        procedural language
4  *
5  * IDENTIFICATION
6  *        $Header: /cvsroot/pgsql/src/pl/plpgsql/src/pl_handler.c,v 1.15 2003/07/27 17:10:07 tgl Exp $
7  *
8  *        This software is copyrighted by Jan Wieck - Hamburg.
9  *
10  *        The author hereby grants permission  to  use,  copy,  modify,
11  *        distribute,  and      license this software and its documentation
12  *        for any purpose, provided that existing copyright notices are
13  *        retained      in      all  copies  and  that  this notice is included
14  *        verbatim in any distributions. No written agreement, license,
15  *        or  royalty  fee      is required for any of the authorized uses.
16  *        Modifications to this software may be  copyrighted  by  their
17  *        author  and  need  not  follow  the licensing terms described
18  *        here, provided that the new terms are  clearly  indicated  on
19  *        the first page of each file where they apply.
20  *
21  *        IN NO EVENT SHALL THE AUTHOR OR DISTRIBUTORS BE LIABLE TO ANY
22  *        PARTY  FOR  DIRECT,   INDIRECT,       SPECIAL,   INCIDENTAL,   OR
23  *        CONSEQUENTIAL   DAMAGES  ARISING      OUT  OF  THE  USE  OF  THIS
24  *        SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF, EVEN
25  *        IF  THE  AUTHOR  HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
26  *        DAMAGE.
27  *
28  *        THE  AUTHOR  AND      DISTRIBUTORS  SPECIFICALLY       DISCLAIM       ANY
29  *        WARRANTIES,  INCLUDING,  BUT  NOT  LIMITED  TO,  THE  IMPLIED
30  *        WARRANTIES  OF  MERCHANTABILITY,      FITNESS  FOR  A  PARTICULAR
31  *        PURPOSE,      AND NON-INFRINGEMENT.  THIS SOFTWARE IS PROVIDED ON
32  *        AN "AS IS" BASIS, AND THE AUTHOR      AND  DISTRIBUTORS  HAVE  NO
33  *        OBLIGATION   TO       PROVIDE   MAINTENANCE,   SUPPORT,  UPDATES,
34  *        ENHANCEMENTS, OR MODIFICATIONS.
35  *
36  **********************************************************************/
37
38 #include "plpgsql.h"
39 #include "pl.tab.h"
40
41 #include "access/heapam.h"
42 #include "catalog/pg_proc.h"
43 #include "catalog/pg_type.h"
44 #include "utils/builtins.h"
45 #include "utils/syscache.h"
46
47
48 /* ----------
49  * plpgsql_call_handler
50  *
51  * This is the only visible function of the PL interpreter.
52  * The PostgreSQL function manager and trigger manager
53  * call this function for execution of PL/pgSQL procedures.
54  * ----------
55  */
56 PG_FUNCTION_INFO_V1(plpgsql_call_handler);
57
58 Datum
59 plpgsql_call_handler(PG_FUNCTION_ARGS)
60 {
61         PLpgSQL_function *func;
62         Datum           retval;
63
64         /*
65          * Connect to SPI manager
66          */
67         if (SPI_connect() != SPI_OK_CONNECT)
68                 elog(ERROR, "SPI_connect failed");
69
70         /* Find or compile the function */
71         func = plpgsql_compile(fcinfo);
72
73         /*
74          * Determine if called as function or trigger and call appropriate
75          * subhandler
76          */
77         if (CALLED_AS_TRIGGER(fcinfo))
78                 retval = PointerGetDatum(plpgsql_exec_trigger(func,
79                                                                            (TriggerData *) fcinfo->context));
80         else
81                 retval = plpgsql_exec_function(func, fcinfo);
82
83         /*
84          * Disconnect from SPI manager
85          */
86         if (SPI_finish() != SPI_OK_FINISH)
87                 elog(ERROR, "SPI_finish failed");
88
89         return retval;
90 }