Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit414f94f

Browse files
committed
Change plpgsql's GET DIAGNOSTICS statement to use SQL99-compatible
syntax. Fix the RESULT_OID case, which never worked. Add documentation.
1 parent66858eb commit414f94f

File tree

8 files changed

+130
-120
lines changed

8 files changed

+130
-120
lines changed

‎doc/src/sgml/plsql.sgml

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/Attic/plsql.sgml,v 2.19 2001/02/10 05:32:33 tgl Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/Attic/plsql.sgml,v 2.20 2001/02/19 19:49:52 tgl Exp $
33
-->
44

55
<chapter id="plsql">
@@ -102,7 +102,7 @@ $Header: /cvsroot/pgsql/doc/src/sgml/Attic/plsql.sgml,v 2.19 2001/02/10 05:32:33
102102

103103
<para>
104104
The PL/pgSQL language is case insensitive. All keywords and
105-
identifiers can be used in mixed upper- andlowercase.
105+
identifiers can be used in mixed upper- andlower-case.
106106
</para>
107107
<para>
108108
PL/pgSQL is a block oriented language. A block is defined as
@@ -181,7 +181,7 @@ END;
181181
must also have a default value specified.
182182
</para>
183183
<para>
184-
The default value is evaluated every time thefunction iscalled. So
184+
The default value is evaluated every time theblock isentered. So
185185
assigning '<replaceable>now</replaceable>' to a variable of type
186186
<type>timestamp</type> causes the variable to have the
187187
time of the actual function call, not when the function was
@@ -203,7 +203,7 @@ END;
203203
corresponding identifier $n will be a rowtype, but it
204204
must be aliased using the ALIAS command described below. Only the user
205205
attributes of a table row are accessible in the row, no Oid or other
206-
system attributes (hence the row could be from a view and view rows
206+
system attributes (because the row could be from a view and view rows
207207
don't have useful system attributes).
208208
</para>
209209
<para>
@@ -311,7 +311,7 @@ RENAME <replaceable>oldname</replaceable> TO <replaceable>newname</replaceable>;
311311
Using the <replaceable>table.field</replaceable>%TYPE
312312
causes PL/pgSQL to look up the attributes definitions at the
313313
first call to the function during the lifetime of a backend.
314-
Have a table with a char(20) attribute and some PL/pgSQL functions
314+
Suppose we have a table with a char(20) attribute and some PL/pgSQL functions
315315
that deal with its content in local variables. Now someone
316316
decides that char(20) isn't enough, dumps the table, drops it,
317317
recreates it now with the attribute in question defined as
@@ -553,6 +553,26 @@ EXECUTE ''UPDATE tbl SET ''
553553
</listitem>
554554
</varlistentry>
555555

556+
<varlistentry>
557+
<term>Obtaining other results status</term>
558+
<listitem>
559+
<para>
560+
<programlisting>
561+
GET DIAGNOSTICS <replaceable>variable</replaceable> = <replaceable>item</replaceable> [ , ... ]
562+
</programlisting>
563+
This command allows retrieval of system status indicators. Each
564+
<replaceable>item</replaceable> is a keyword identifying a state
565+
value to be assigned to the specified variable (which should be of
566+
the right datatype to receive it). The currently available status
567+
items are <keyword>ROW_COUNT</>, the number of rows processed by
568+
the last SQL query sent down to the SQL engine; and
569+
<keyword>RESULT_OID</>, the Oid of the last row inserted by the
570+
most recent SQL query. Note that <keyword>RESULT_OID</> is only
571+
useful after an INSERT query.
572+
</para>
573+
</listitem>
574+
</varlistentry>
575+
556576
<varlistentry>
557577
<term>Returning from the function</term>
558578
<listitem>

‎src/backend/executor/spi.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,23 @@
33
* spi.c
44
*Server Programming Interface
55
*
6-
* $Id: spi.c,v 1.51 2001/01/04 02:36:52 tgl Exp $
6+
* $Id: spi.c,v 1.52 2001/02/19 19:49:52 tgl Exp $
77
*
88
*-------------------------------------------------------------------------
99
*/
1010
#include"executor/spi_priv.h"
1111
#include"access/printtup.h"
1212

13+
uint32SPI_processed=0;
14+
OidSPI_lastoid=InvalidOid;
15+
SPITupleTable*SPI_tuptable=NULL;
16+
intSPI_result;
17+
1318
static_SPI_connection*_SPI_stack=NULL;
1419
static_SPI_connection*_SPI_current=NULL;
1520
staticint_SPI_connected=-1;
1621
staticint_SPI_curid=-1;
1722

18-
DLLIMPORTuint32SPI_processed=0;
19-
DLLIMPORTSPITupleTable*SPI_tuptable=NULL;
20-
DLLIMPORTintSPI_result;
21-
2223
staticint_SPI_execute(char*src,inttcount,_SPI_plan*plan);
2324
staticint_SPI_pquery(QueryDesc*queryDesc,EState*state,inttcount);
2425

@@ -155,6 +156,7 @@ AtEOXact_SPI(void)
155156
_SPI_current=_SPI_stack=NULL;
156157
_SPI_connected=_SPI_curid=-1;
157158
SPI_processed=0;
159+
SPI_lastoid=InvalidOid;
158160
SPI_tuptable=NULL;
159161
}
160162

@@ -623,6 +625,7 @@ _SPI_execute(char *src, int tcount, _SPI_plan *plan)
623625
CommandCounterIncrement();
624626

625627
SPI_processed=0;
628+
SPI_lastoid=InvalidOid;
626629
SPI_tuptable=NULL;
627630
_SPI_current->tuptable=NULL;
628631
_SPI_current->qtlist=NULL;
@@ -723,6 +726,7 @@ _SPI_execute_plan(_SPI_plan *plan, Datum *Values, char *Nulls, int tcount)
723726
CommandCounterIncrement();
724727

725728
SPI_processed=0;
729+
SPI_lastoid=InvalidOid;
726730
SPI_tuptable=NULL;
727731
_SPI_current->tuptable=NULL;
728732
_SPI_current->qtlist=NULL;
@@ -786,6 +790,7 @@ _SPI_pquery(QueryDesc *queryDesc, EState *state, int tcount)
786790
boolisRetrieveIntoRelation= false;
787791
char*intoName=NULL;
788792
intres;
793+
Oidsave_lastoid;
789794

790795
switch (operation)
791796
{
@@ -840,6 +845,8 @@ _SPI_pquery(QueryDesc *queryDesc, EState *state, int tcount)
840845
ExecutorRun(queryDesc,state,EXEC_FOR, (long)tcount);
841846

842847
_SPI_current->processed=state->es_processed;
848+
save_lastoid=state->es_lastoid;
849+
843850
if (operation==CMD_SELECT&&queryDesc->dest==SPI)
844851
{
845852
if (_SPI_checktuples())
@@ -859,6 +866,7 @@ _SPI_pquery(QueryDesc *queryDesc, EState *state, int tcount)
859866
if (dest==SPI)
860867
{
861868
SPI_processed=_SPI_current->processed;
869+
SPI_lastoid=save_lastoid;
862870
SPI_tuptable=_SPI_current->tuptable;
863871
}
864872
queryDesc->dest=dest;

‎src/include/executor/spi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ typedef struct
7171
#defineSPI_OK_CURSOR10
7272

7373
externDLLIMPORTuint32SPI_processed;
74+
externDLLIMPORTOidSPI_lastoid;
7475
externDLLIMPORTSPITupleTable*SPI_tuptable;
7576
externDLLIMPORTintSPI_result;
7677

‎src/pl/plpgsql/src/gram.y

Lines changed: 37 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* procedural language
55
*
66
* IDENTIFICATION
7-
* $Header: /cvsroot/pgsql/src/pl/plpgsql/src/gram.y,v 1.15 2001/02/10 22:53:40 momjian Exp $
7+
* $Header: /cvsroot/pgsql/src/pl/plpgsql/src/gram.y,v 1.16 2001/02/19 19:49:53 tgl Exp $
88
*
99
* This software is copyrighted by Jan Wieck - Hamburg.
1010
*
@@ -63,7 +63,13 @@ staticPLpgSQL_expr*make_tupret_expr(PLpgSQL_row *row);
6363
{
6464
int nalloc;
6565
int nused;
66-
int *dtnums;
66+
int *nums;
67+
}intlist;
68+
struct
69+
{
70+
int nalloc;
71+
int nused;
72+
PLpgSQL_diag_item *dtitems;
6773
}dtlist;
6874
struct
6975
{
@@ -119,11 +125,11 @@ staticPLpgSQL_expr*make_tupret_expr(PLpgSQL_row *row);
119125
%type<stmt>stmt_fors,stmt_select,stmt_perform
120126
%type<stmt>stmt_dynexecute,stmt_dynfors,stmt_getdiag
121127

122-
%type<dtlist>raise_params
128+
%type<intlist>raise_params
123129
%type<ival>raise_level,raise_param
124130
%type<str>raise_msg
125131

126-
%type<dtlist>getdiag_items,getdiag_targets
132+
%type<dtlist>getdiag_list
127133
%type<ival>getdiag_item,getdiag_target
128134

129135
%type<ival>lno
@@ -156,11 +162,11 @@ staticPLpgSQL_expr*make_tupret_expr(PLpgSQL_row *row);
156162
%tokenK_NOTICE
157163
%tokenK_NULL
158164
%tokenK_PERFORM
159-
%tokenK_PROCESSED
165+
%tokenK_ROW_COUNT
160166
%tokenK_RAISE
161167
%tokenK_RECORD
162168
%tokenK_RENAME
163-
%tokenK_RESULT
169+
%tokenK_RESULT_OID
164170
%tokenK_RETURN
165171
%tokenK_REVERSE
166172
%tokenK_SELECT
@@ -607,7 +613,7 @@ stmt_assign: assign_var lno K_ASSIGN expr_until_semi
607613
}
608614
;
609615

610-
stmt_getdiag:K_GETK_DIAGNOSTICSlnoK_SELECTgetdiag_itemsK_INTOgetdiag_targets';'
616+
stmt_getdiag:K_GETK_DIAGNOSTICSlnogetdiag_list';'
611617
{
612618
PLpgSQL_stmt_getdiag *new;
613619

@@ -616,80 +622,50 @@ stmt_getdiag: K_GET K_DIAGNOSTICS lno K_SELECT getdiag_items K_INTO getdiag_tar
616622

617623
new->cmd_type = PLPGSQL_STMT_GETDIAG;
618624
new->lineno =$3;
619-
new->nitems =$5.nused;
620-
new->items = malloc(sizeof(int) *$5.nused);
621-
new->ntargets =$7.nused;
622-
new->targets = malloc(sizeof(int) *$7.nused);
623-
memcpy(new->items, $5.dtnums,sizeof(int) * $5.nused);
624-
memcpy(new->targets, $7.dtnums,sizeof(int) * $7.nused);
625-
626-
if (new->nitems !=new->ntargets)
627-
{
628-
plpgsql_error_lineno =new->lineno;
629-
plpgsql_comperrinfo();
630-
elog(ERROR,"number of diagnostic items does not match target list");
631-
};
625+
new->ndtitems =$4.nused;
626+
new->dtitems = malloc(sizeof(PLpgSQL_diag_item) *$4.nused);
627+
memcpy(new->dtitems, $4.dtitems,sizeof(PLpgSQL_diag_item) * $4.nused);
632628

633629
$$ = (PLpgSQL_stmt *)new;
634630
}
635631
;
636632

637-
getdiag_items :getdiag_items','getdiag_item
633+
getdiag_list :getdiag_list','getdiag_targetK_ASSIGNgetdiag_item
638634
{
639635
if ($1.nused ==$1.nalloc)
640636
{
641637
$1.nalloc *=2;
642-
$1.dtnums = repalloc($1.dtnums,sizeof(int) *$1.nalloc);
638+
$1.dtitems = repalloc($1.dtitems,sizeof(PLpgSQL_diag_item) *$1.nalloc);
643639
}
644-
$1.dtnums[$1.nused++] =$3;
640+
$1.dtitems[$1.nused].target =$3;
641+
$1.dtitems[$1.nused].item =$5;
642+
$1.nused++;
645643

646644
$$.nalloc =$1.nalloc;
647645
$$.nused =$1.nused;
648-
$$.dtnums =$1.dtnums;
646+
$$.dtitems =$1.dtitems;
649647
}
650-
|getdiag_item
648+
|getdiag_targetK_ASSIGNgetdiag_item
651649
{
652650
$$.nalloc =1;
653651
$$.nused =1;
654-
$$.dtnums = palloc(sizeof(int) *$$.nalloc);
655-
$$.dtnums[0] =$1;
656-
}
657-
;
658-
659-
getdiag_item :K_PROCESSED
660-
{
661-
$$ = PLPGSQL_GETDIAG_PROCESSED;
662-
}
663-
|K_RESULT
664-
{
665-
$$ = PLPGSQL_GETDIAG_RESULT;
652+
$$.dtitems = palloc(sizeof(PLpgSQL_diag_item) *$$.nalloc);
653+
$$.dtitems[0].target =$1;
654+
$$.dtitems[0].item =$3;
666655
}
667656
;
668657

669-
getdiag_targets :getdiag_targets','getdiag_target
658+
getdiag_item :K_ROW_COUNT
670659
{
671-
if ($1.nused ==$1.nalloc)
672-
{
673-
$1.nalloc *=2;
674-
$1.dtnums = repalloc($1.dtnums,sizeof(int) *$1.nalloc);
675-
}
676-
$1.dtnums[$1.nused++] =$3;
677-
678-
$$.nalloc =$1.nalloc;
679-
$$.nused =$1.nused;
680-
$$.dtnums =$1.dtnums;
660+
$$ = PLPGSQL_GETDIAG_ROW_COUNT;
681661
}
682-
|getdiag_target
662+
|K_RESULT_OID
683663
{
684-
$$.nalloc =1;
685-
$$.nused =1;
686-
$$.dtnums = palloc(sizeof(int) *$$.nalloc);
687-
$$.dtnums[0] =$1;
664+
$$ = PLPGSQL_GETDIAG_RESULT_OID;
688665
}
689666
;
690667

691-
692-
getdiag_target :T_VARIABLE
668+
getdiag_target:T_VARIABLE
693669
{
694670
if (yylval.var->isconst)
695671
{
@@ -1070,7 +1046,7 @@ stmt_raise: K_RAISE lno raise_level raise_msg raise_params ';'
10701046
new->message=$4;
10711047
new->nparams=$5.nused;
10721048
new->params= malloc(sizeof(int) *$5.nused);
1073-
memcpy(new->params, $5.dtnums,sizeof(int) * $5.nused);
1049+
memcpy(new->params, $5.nums,sizeof(int) * $5.nused);
10741050

10751051
$$ = (PLpgSQL_stmt *)new;
10761052
}
@@ -1116,20 +1092,20 @@ raise_params: raise_params raise_param
11161092
if ($1.nused ==$1.nalloc)
11171093
{
11181094
$1.nalloc *=2;
1119-
$1.dtnums = repalloc($1.dtnums,sizeof(int) *$1.nalloc);
1095+
$1.nums = repalloc($1.nums,sizeof(int) *$1.nalloc);
11201096
}
1121-
$1.dtnums[$1.nused++] =$2;
1097+
$1.nums[$1.nused++] =$2;
11221098

11231099
$$.nalloc =$1.nalloc;
11241100
$$.nused =$1.nused;
1125-
$$.dtnums=$1.dtnums;
1101+
$$.nums=$1.nums;
11261102
}
11271103
|raise_param
11281104
{
11291105
$$.nalloc =1;
11301106
$$.nused =1;
1131-
$$.dtnums = palloc(sizeof(int) *$$.nalloc);
1132-
$$.dtnums[0] =$1;
1107+
$$.nums = palloc(sizeof(int) *$$.nalloc);
1108+
$$.nums[0] =$1;
11331109
}
11341110
;
11351111

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp