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

Commite85a5ff

Browse files
committed
Fix tracking of psql script line numbers during \copy from another place.
Commit0814677 changed do_copy() totemporarily scribble on pset.cur_cmd_source. That was a mighty ugly bit ofcode in any case, but in particular it broke handleCopyIn's ability to tellwhether it was reading from the current script source file (in which casepset.lineno should be incremented for each line of COPY data), or fromsomeplace else (in which case it shouldn't). The former case still worked,the latter not so much. The visible effect was that line numbers reportedfor errors in a script file would be wrong if there were an earlier \copythat was reading anything other than inline-in-the-script-file data.To fix, introduce another pset field that holds the file do_copy wants theCOPY code to use. This is a little bit ugly, but less so than passing thefile down explicitly through several layers that aren't COPY-specific.Extracted from a larger patch by Kumar Rajeev Rastogi; that patch alsochanges printing of COPY command tags, which is not a bug fix and shouldn'tget back-patched. This particular idea was from a suggestion by AmitKhandekar, if I'm reading the thread correctly.Back-patch to 9.2 where the faulty code was introduced.
1 parent8722017 commite85a5ff

File tree

4 files changed

+25
-14
lines changed

4 files changed

+25
-14
lines changed

‎src/bin/psql/common.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ StoreQueryTuple(const PGresult *result)
628628
* command. In that event, we'll marshal data for the COPY and then cycle
629629
* through any subsequent PGresult objects.
630630
*
631-
* When the command string contained noaffected COPY command, this function
631+
* When the command string contained nosuch COPY command, this function
632632
* degenerates to an AcceptResult() call.
633633
*
634634
* Changes its argument to point to the last PGresult of the command string,
@@ -688,13 +688,28 @@ ProcessResult(PGresult **results)
688688
* Marshal the COPY data. Either subroutine will get the
689689
* connection out of its COPY state, then call PQresultStatus()
690690
* once and report any error.
691+
*
692+
* If pset.copyStream is set, use that as data source/sink,
693+
* otherwise use queryFout or cur_cmd_source as appropriate.
691694
*/
695+
FILE*copystream=pset.copyStream;
696+
692697
SetCancelConn();
693698
if (result_status==PGRES_COPY_OUT)
694-
success=handleCopyOut(pset.db,pset.queryFout)&&success;
699+
{
700+
if (!copystream)
701+
copystream=pset.queryFout;
702+
success=handleCopyOut(pset.db,
703+
copystream)&&success;
704+
}
695705
else
696-
success=handleCopyIn(pset.db,pset.cur_cmd_source,
706+
{
707+
if (!copystream)
708+
copystream=pset.cur_cmd_source;
709+
success=handleCopyIn(pset.db,
710+
copystream,
697711
PQbinaryTuples(*results))&&success;
712+
}
698713
ResetCancelConn();
699714

700715
/*

‎src/bin/psql/copy.c

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,8 @@ do_copy(const char *args)
269269
{
270270
PQExpBufferDataquery;
271271
FILE*copystream;
272-
FILE*save_file;
273-
FILE**override_file;
274272
structcopy_options*options;
275273
boolsuccess;
276-
structstatst;
277274

278275
/* parse options */
279276
options=parse_slash_copy(args);
@@ -287,8 +284,6 @@ do_copy(const char *args)
287284

288285
if (options->from)
289286
{
290-
override_file=&pset.cur_cmd_source;
291-
292287
if (options->file)
293288
{
294289
if (options->program)
@@ -308,8 +303,6 @@ do_copy(const char *args)
308303
}
309304
else
310305
{
311-
override_file=&pset.queryFout;
312-
313306
if (options->file)
314307
{
315308
if (options->program)
@@ -345,6 +338,7 @@ do_copy(const char *args)
345338

346339
if (!options->program)
347340
{
341+
structstatst;
348342
intresult;
349343

350344
/* make sure the specified file is not a directory */
@@ -375,11 +369,10 @@ do_copy(const char *args)
375369
if (options->after_tofrom)
376370
appendPQExpBufferStr(&query,options->after_tofrom);
377371

378-
/* Run it like a user command, interposing the data source or sink. */
379-
save_file=*override_file;
380-
*override_file=copystream;
372+
/* run it like a user command, but with copystream as data source/sink */
373+
pset.copyStream=copystream;
381374
success=SendQuery(query.data);
382-
*override_file=save_file;
375+
pset.copyStream=NULL;
383376
termPQExpBuffer(&query);
384377

385378
if (options->file!=NULL)

‎src/bin/psql/settings.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ typedef struct _psqlSettings
7070
FILE*queryFout;/* where to send the query results */
7171
boolqueryFoutPipe;/* queryFout is from a popen() */
7272

73+
FILE*copyStream;/* Stream to read/write for \copy command */
74+
7375
printQueryOptpopt;
7476

7577
char*gfname;/* one-shot file output argument for \g */

‎src/bin/psql/startup.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ main(int argc, char *argv[])
118118
pset.encoding=PQenv2encoding();
119119
pset.queryFout=stdout;
120120
pset.queryFoutPipe= false;
121+
pset.copyStream=NULL;
121122
pset.cur_cmd_source=stdin;
122123
pset.cur_cmd_interactive= false;
123124

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp