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

Commit061168d

Browse files
committed
Code review for FETCH/MOVE 0 changes. Improve documentation, do the
right thing with the destination when FETCH 0 can't return a row,don't try to stuff LONG_MAX into an int value.
1 parent1bc9e98 commit061168d

File tree

5 files changed

+113
-74
lines changed

5 files changed

+113
-74
lines changed

‎doc/src/sgml/ref/fetch.sgml

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/ref/fetch.sgml,v 1.22 2002/12/30 15:31:47 momjian Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/ref/fetch.sgml,v 1.23 2003/01/08 00:22:26 tgl Exp $
33
PostgreSQL documentation
44
-->
55

@@ -22,7 +22,7 @@ PostgreSQL documentation
2222
</refsynopsisdivinfo>
2323
<synopsis>
2424
FETCH [ <replaceable class="PARAMETER">direction</replaceable> ] [ <replaceable class="PARAMETER">count</replaceable> ] { IN | FROM } <replaceable class="PARAMETER">cursor</replaceable>
25-
FETCH [ FORWARD | BACKWARD | RELATIVE ] [ <replaceable class="PARAMETER">#</replaceable> | ALL | NEXT | PRIOR ]
25+
FETCH [ FORWARD | BACKWARD | RELATIVE ] [ <replaceable class="PARAMETER">#</replaceable> | ALL |LAST |NEXT | PRIOR ]
2626
{ IN | FROM } <replaceable class="PARAMETER">cursor</replaceable>
2727
</synopsis>
2828

@@ -40,7 +40,7 @@ FETCH [ FORWARD | BACKWARD | RELATIVE ] [ <replaceable class="PARAMETER">#</repl
4040
<term><replaceable class="PARAMETER">direction</replaceable></term>
4141
<listitem>
4242
<para>
43-
<replaceable class="PARAMETER">selector</replaceable>
43+
<replaceable class="PARAMETER">direction</replaceable>
4444
defines the fetch direction. It can be one of
4545
the following:
4646

@@ -50,7 +50,7 @@ FETCH [ FORWARD | BACKWARD | RELATIVE ] [ <replaceable class="PARAMETER">#</repl
5050
<listitem>
5151
<para>
5252
fetch next row(s). This is the default
53-
if <replaceable class="PARAMETER">selector</replaceable> is omitted.
53+
if <replaceable class="PARAMETER">direction</replaceable> is omitted.
5454
</para>
5555
</listitem>
5656
</varlistentry>
@@ -87,9 +87,9 @@ FETCH [ FORWARD | BACKWARD | RELATIVE ] [ <replaceable class="PARAMETER">#</repl
8787
<term><replaceable class="PARAMETER">#</replaceable></term>
8888
<listitem>
8989
<para>
90-
A signed integer that specifies how many rows to fetch.
90+
A signed integerconstantthat specifies how many rows to fetch.
9191
Note that a negative integer is equivalent to changing the sense of
92-
FORWARD and BACKWARD. Zero re-fetches the current row.
92+
FORWARD and BACKWARD. Zero re-fetches the current row, if any.
9393
</para>
9494
</listitem>
9595
</varlistentry>
@@ -105,6 +105,17 @@ FETCH [ FORWARD | BACKWARD | RELATIVE ] [ <replaceable class="PARAMETER">#</repl
105105
</listitem>
106106
</varlistentry>
107107

108+
<varlistentry>
109+
<term>
110+
LAST
111+
</term>
112+
<listitem>
113+
<para>
114+
Same as <literal>ALL</>, but conforms to SQL92 syntax.
115+
</para>
116+
</listitem>
117+
</varlistentry>
118+
108119
<varlistentry>
109120
<term>
110121
NEXT
@@ -151,7 +162,8 @@ FETCH [ FORWARD | BACKWARD | RELATIVE ] [ <replaceable class="PARAMETER">#</repl
151162
Outputs
152163
</title>
153164
<para>
154-
<command>FETCH</command> returns the results of the query defined by the specified cursor.
165+
<command>FETCH</command> returns rows from the result of the query defined
166+
by the specified cursor.
155167
The following messages will be returned if the query fails:
156168

157169
<variablelist>
@@ -200,10 +212,33 @@ WARNING: FETCH/ABSOLUTE not supported, using RELATIVE
200212
If the number of rows remaining in the cursor is less
201213
than <replaceable class="PARAMETER">#</replaceable>,
202214
then only those available are fetched.
203-
Substituting the keyword ALL in place of a number will
215+
Substituting the keyword ALLor LASTin place of a number will
204216
cause all remaining rows in the cursor to be retrieved.
205-
Instances may be fetched in both FORWARD and BACKWARD
217+
Rows may be fetched in both FORWARD and BACKWARD
206218
directions. The default direction is FORWARD.
219+
</para>
220+
221+
<para>
222+
The cursor position can be before the first row of the query result, or on
223+
any particular row of the result, or after the last row of the result.
224+
When created, a cursor is positioned before the first row. After fetching
225+
some rows, the cursor is positioned on the last row retrieved. A new
226+
<command>FETCH</command> always steps one row in the specified direction
227+
(if possible) before beginning to return rows. If the
228+
<command>FETCH</command> requests more rows than available, the cursor is
229+
left positioned after the last row of the query result (or before the first
230+
row, in the case of a backward fetch). This will always be the case after
231+
<command>FETCH ALL</>.
232+
</para>
233+
234+
<tip>
235+
<para>
236+
A zero row count requests fetching the current row without moving the
237+
cursor --- that is, re-fetching the most recently fetched row.
238+
This will succeed unless the cursor is positioned before the
239+
first row or after the last row; in which case, no row is returned.
240+
</para>
241+
</tip>
207242

208243
<tip>
209244
<para>
@@ -213,7 +248,6 @@ WARNING: FETCH/ABSOLUTE not supported, using RELATIVE
213248
<command>FORWARD -1</command> is the same as <command>BACKWARD 1</command>.
214249
</para>
215250
</tip>
216-
</para>
217251

218252
<refsect2 id="R2-SQL-FETCH-3">
219253
<refsect2info>
@@ -224,11 +258,9 @@ WARNING: FETCH/ABSOLUTE not supported, using RELATIVE
224258
</title>
225259

226260
<para>
227-
Note that the FORWARDandBACKWARD keywords are
261+
Note that the FORWARD, BACKWARD,andALL keywords are
228262
<productname>PostgreSQL</productname> extensions.
229-
The <acronym>SQL92</acronym> syntax is also supported, specified
230-
in the second form of the command. See below for details
231-
on compatibility issues.
263+
See below for details on compatibility issues.
232264
</para>
233265

234266
<para>
@@ -246,11 +278,11 @@ WARNING: FETCH/ABSOLUTE not supported, using RELATIVE
246278
</para>
247279

248280
<para>
281+
<xref linkend="sql-declare" endterm="sql-declare-title">
282+
is used to define a cursor.
249283
Use
250284
<xref linkend="sql-move" endterm="sql-move-title">
251-
to change cursor position.
252-
<xref linkend="sql-declare" endterm="sql-declare-title">
253-
will define a cursor.
285+
to change cursor position without retrieving data.
254286
Refer to
255287
<xref linkend="sql-begin" endterm="sql-begin-title">,
256288
<xref linkend="sql-commit" endterm="sql-commit-title">,

‎doc/src/sgml/ref/move.sgml

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/ref/move.sgml,v 1.15 2002/12/30 15:31:47 momjian Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/ref/move.sgml,v 1.16 2003/01/08 00:22:26 tgl Exp $
33
PostgreSQL documentation
44
-->
55

@@ -13,17 +13,15 @@ PostgreSQL documentation
1313
MOVE
1414
</refname>
1515
<refpurpose>
16-
position a cursor on a specified row of a table
16+
reposition a cursor
1717
</refpurpose>
1818
</refnamediv>
1919
<refsynopsisdiv>
2020
<refsynopsisdivinfo>
2121
<date>1999-07-20</date>
2222
</refsynopsisdivinfo>
2323
<synopsis>
24-
MOVE [ <replaceable class="PARAMETER">direction</replaceable> ]
25-
{<replaceable class="PARAMETER">count</replaceable> | LAST }
26-
{ IN | FROM } <replaceable class="PARAMETER">cursor</replaceable>
24+
MOVE [ <replaceable class="PARAMETER">direction</replaceable> ] [ <replaceable class="PARAMETER">count</replaceable> ] { IN | FROM } <replaceable class="PARAMETER">cursor</replaceable>
2725
</synopsis>
2826
</refsynopsisdiv>
2927

@@ -35,12 +33,10 @@ MOVE [ <replaceable class="PARAMETER">direction</replaceable> ]
3533
Description
3634
</title>
3735
<para>
38-
<command>MOVE</command> allows a user to move the cursor position a
39-
specified number of rows.
40-
<command>MOVE</command> works like the <command>FETCH</command> command,
41-
but only positions the cursor and does not return rows.
42-
<replaceable class="PARAMETER">LAST</replaceable> moves to the end
43-
of the cursor.
36+
<command>MOVE</command> allows the user to move the cursor position a
37+
specified number of rows, or all the way to the end or start of the query.
38+
<command>MOVE</command> works exactly like the <command>FETCH</command>
39+
command, except it only repositions the cursor and does not return rows.
4440
</para>
4541
<para>
4642
Refer to

‎doc/src/sgml/release.sgml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/release.sgml,v 1.175 2003/01/06 18:53:23 petere Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/release.sgml,v 1.176 2003/01/08 00:22:26 tgl Exp $
33
-->
44

55
<appendix id="release">
@@ -24,6 +24,7 @@ CDATA means the content is "SGML-free", so you can write without
2424
worries about funny characters.
2525
-->
2626
<literallayout><![CDATA[
27+
FETCH 0 now re-fetches cursor's current row, per SQL spec
2728
Revised executor state representation; plan trees are read-only to executor now
2829
Information schema
2930
Domains now support CHECK constraints

‎src/backend/commands/portalcmds.c

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/commands/portalcmds.c,v 1.7 2002/12/30 15:31:47 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/commands/portalcmds.c,v 1.8 2003/01/08 00:22:27 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -55,7 +55,7 @@ PortalCleanup(Portal portal)
5555
*
5656
*name: name of portal
5757
*forward: forward or backward fetch?
58-
*count: # of tuples to fetch
58+
*count: # of tuples to fetch (INT_MAX means "all"; 0 means "refetch")
5959
*dest: where to send results
6060
*completionTag: points to a buffer of size COMPLETION_TAG_BUFSIZE
6161
*in which to store a command completion status string.
@@ -100,18 +100,15 @@ PerformPortalFetch(char *name,
100100
return;
101101
}
102102

103-
/* If zero count, handle specially */
103+
/*
104+
* Zero count means to re-fetch the current row, if any (per SQL92)
105+
*/
104106
if (count==0)
105107
{
106-
boolon_row= false;
108+
boolon_row;
107109

108110
/* Are we sitting on a row? */
109-
oldcontext=MemoryContextSwitchTo(PortalGetHeapMemory(portal));
110-
queryDesc=PortalGetQueryDesc(portal);
111-
estate=queryDesc->estate;
112-
if (portal->atStart== false&&portal->atEnd== false)
113-
on_row= true;
114-
MemoryContextSwitchTo(oldcontext);
111+
on_row= (portal->atStart== false&&portal->atEnd== false);
115112

116113
if (dest==None)
117114
{
@@ -122,26 +119,25 @@ PerformPortalFetch(char *name,
122119
}
123120
else
124121
{
125-
/* If we are not on a row, FETCH 0 returns nothing */
126-
if (!on_row)
127-
return;
128-
129-
/* Since we are sitting on a row, return the row */
130-
/* Back up so we can reread the row */
131-
PerformPortalFetch(name, false/* backward */,1,
132-
None,/* throw away output */
133-
NULL/* do not modify the command tag */);
134-
135-
/* Set up to fetch one row */
136-
count=1;
137-
forward= true;
122+
/*
123+
* If we are sitting on a row, back up one so we can re-fetch it.
124+
* If we are not sitting on a row, we still have to start up and
125+
* shut down the executor so that the destination is initialized
126+
* and shut down correctly; so keep going. Further down in the
127+
* routine, count == 0 means we will retrieve no row.
128+
*/
129+
if (on_row)
130+
{
131+
PerformPortalFetch(name, false/* backward */,1L,
132+
None,/* throw away output */
133+
NULL/* do not modify the command tag */);
134+
/* Set up to fetch one row forward */
135+
count=1;
136+
forward= true;
137+
}
138138
}
139139
}
140140

141-
/* Internally, zero count processes all portal rows */
142-
if (count==LONG_MAX)
143-
count=0;
144-
145141
/*
146142
* switch into the portal context
147143
*/
@@ -185,31 +181,45 @@ PerformPortalFetch(char *name,
185181
*/
186182
if (forward)
187183
{
188-
if (portal->atEnd)
184+
if (portal->atEnd||count==0)
189185
direction=NoMovementScanDirection;
190186
else
191187
direction=ForwardScanDirection;
192188

193-
ExecutorRun(queryDesc,direction, (long)count);
189+
/* In the executor, zero count processes all portal rows */
190+
if (count==INT_MAX)
191+
count=0;
194192

195-
if (estate->es_processed>0)
196-
portal->atStart= false;/* OK to back up now */
197-
if (count <=0|| (int)estate->es_processed<count)
198-
portal->atEnd= true;/* we retrieved 'em all */
193+
ExecutorRun(queryDesc,direction,count);
194+
195+
if (direction!=NoMovementScanDirection)
196+
{
197+
if (estate->es_processed>0)
198+
portal->atStart= false;/* OK to back up now */
199+
if (count <=0|| (long)estate->es_processed<count)
200+
portal->atEnd= true;/* we retrieved 'em all */
201+
}
199202
}
200203
else
201204
{
202-
if (portal->atStart)
205+
if (portal->atStart||count==0)
203206
direction=NoMovementScanDirection;
204207
else
205208
direction=BackwardScanDirection;
206209

207-
ExecutorRun(queryDesc,direction, (long)count);
210+
/* In the executor, zero count processes all portal rows */
211+
if (count==INT_MAX)
212+
count=0;
213+
214+
ExecutorRun(queryDesc,direction,count);
208215

209-
if (estate->es_processed>0)
210-
portal->atEnd= false;/* OK to go forward now */
211-
if (count <=0|| (int)estate->es_processed<count)
212-
portal->atStart= true;/* we retrieved 'em all */
216+
if (direction!=NoMovementScanDirection)
217+
{
218+
if (estate->es_processed>0)
219+
portal->atEnd= false;/* OK to go forward now */
220+
if (count <=0|| (long)estate->es_processed<count)
221+
portal->atStart= true;/* we retrieved 'em all */
222+
}
213223
}
214224

215225
/* Return command status if wanted */

‎src/backend/parser/gram.y

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
*
1313
* IDENTIFICATION
14-
* $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.390 2003/01/06 00:31:44 tgl Exp $
14+
* $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.391 2003/01/08 00:22:27 tgl Exp $
1515
*
1616
* HISTORY
1717
* AUTHORDATEMAJOR EVENT
@@ -2594,7 +2594,7 @@ FetchStmt:FETCH direction fetch_how_many from_in name
25942594
if ($3 <0)
25952595
{
25962596
$3 = -$3;
2597-
$2 = (($2 == FORWARD)? BACKWARD: FORWARD);
2597+
$2 = (($2 == FORWARD)? BACKWARD: FORWARD);
25982598
}
25992599
n->direction =$2;
26002600
n->howMany =$3;
@@ -2652,7 +2652,7 @@ FetchStmt:FETCH direction fetch_how_many from_in name
26522652
if ($3 <0)
26532653
{
26542654
$3 = -$3;
2655-
$2 = (($2 == FORWARD) ? BACKWARD: FORWARD);
2655+
$2 = (($2 == FORWARD) ? BACKWARD: FORWARD);
26562656
}
26572657
n->direction =$2;
26582658
n->howMany =$3;
@@ -2720,8 +2720,8 @@ direction:FORWARD{ $$ = FORWARD; }
27202720
fetch_how_many:
27212721
Iconst{$$ =$1; }
27222722
|'-' Iconst{$$ = -$2; }
2723-
| ALL{$$ =LONG_MAX; }
2724-
| LAST{$$ =LONG_MAX; }
2723+
| ALL{$$ =INT_MAX; }
2724+
| LAST{$$ =INT_MAX; }
27252725
| NEXT{$$ =1; }
27262726
| PRIOR{$$ = -1; }
27272727
;
@@ -7115,8 +7115,8 @@ unreserved_keyword:
71157115
| INVOKER
71167116
| ISOLATION
71177117
| KEY
7118-
| LANGUAGE
71197118
| LANCOMPILER
7119+
| LANGUAGE
71207120
| LAST
71217121
| LEVEL
71227122
| LISTEN

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp