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

Commitadd932e

Browse files
committed
I'm continuing to work on cleaning up code in psql. As things appear
now, my changes seem to work. Some possible minor bugs got squishedon the way but I can't be sure without more feedback from people whoreally put the code to the test.The new patch mostly simplifies variable handling and reduces codeduplication. Changes in the command parser eliminate some redundantvariables (boolean state + depth counter), replaces some"else if" constructs with switches, and so on. It is meant to beapplied together with my previous patch, although I hope they don'tconflict; I went back to the CVS version for this one.One more thing I thought should perhaps be changed: an IGNOREEOFvalue of n will ignore only n-1 EOFs. I didn't want to touch thisfor fear of breaking existing applications, but it does seem a tadillogical.Jeroen T. Vermeulen
1 parent1b3d4ce commitadd932e

File tree

10 files changed

+233
-201
lines changed

10 files changed

+233
-201
lines changed

‎src/bin/psql/command.c

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000-2002 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/command.c,v 1.93 2003/03/19 22:49:43 momjian Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/command.c,v 1.94 2003/03/20 06:43:35 momjian Exp $
77
*/
88
#include"postgres_fe.h"
99
#include"command.h"
@@ -669,15 +669,7 @@ exec_command(const char *cmd,
669669
if (!opt0)
670670
{
671671
/* list all variables */
672-
673-
/*
674-
* XXX This is in utter violation of the GetVariable
675-
* abstraction, but I have not bothered to do it better.
676-
*/
677-
struct_variable*ptr;
678-
679-
for (ptr=pset.vars;ptr->next;ptr=ptr->next)
680-
fprintf(stdout,"%s = '%s'\n",ptr->next->name,ptr->next->value);
672+
PrintVariables(pset.vars);
681673
success= true;
682674
}
683675
else
@@ -1073,9 +1065,7 @@ scan_option(char **string, enum option_type type, char *quote, bool semicolon)
10731065
save_char=options_string[pos+token_end+1];
10741066
options_string[pos+token_end+1]='\0';
10751067
value=GetVariable(pset.vars,options_string+pos+1);
1076-
if (!value)
1077-
value="";
1078-
return_val=xstrdup(value);
1068+
return_val=xstrdup(value ?value :"");
10791069
options_string[pos+token_end+1]=save_char;
10801070
*string=&options_string[pos+token_end+1];
10811071
/* XXX should we set *quote to ':' here? */
@@ -1287,15 +1277,9 @@ unescape(const unsigned char *source, size_t len)
12871277
case'7':
12881278
case'8':
12891279
case'9':
1290-
{
1291-
longintl;
1292-
char*end;
1293-
1294-
l=strtol(p,&end,0);
1295-
c= (char)l;
1296-
p=end-1;
1280+
c=parse_char((char**)&p);
12971281
break;
1298-
}
1282+
12991283
default:
13001284
c=*p;
13011285
}

‎src/bin/psql/common.c

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/common.c,v 1.59 2003/03/20 06:00:12 momjian Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/common.c,v 1.60 2003/03/20 06:43:35 momjian Exp $
77
*/
88
#include"postgres_fe.h"
99
#include"common.h"
@@ -368,7 +368,7 @@ PSQLexec(const char *query, bool ignore_command_ok)
368368
{
369369
PGresult*res=NULL;
370370
PGresult*newres;
371-
constchar*var;
371+
intecho_hidden;
372372
ExecStatusTyperstatus;
373373

374374
if (!pset.db)
@@ -377,17 +377,17 @@ PSQLexec(const char *query, bool ignore_command_ok)
377377
returnNULL;
378378
}
379379

380-
var=GetVariable(pset.vars,"ECHO_HIDDEN");
381-
if (var)
380+
echo_hidden=SwitchVariable(pset.vars,"ECHO_HIDDEN","noexec",NULL);
381+
if (echo_hidden!=var_notset)
382382
{
383383
printf("********* QUERY **********\n"
384384
"%s\n"
385385
"**************************\n\n",query);
386386
fflush(stdout);
387-
}
388387

389-
if (var&&strcmp(var,"noexec")==0)
388+
if (echo_hidden==1)
390389
returnNULL;
390+
}
391391

392392
/* discard any uneaten results of past queries */
393393
while ((newres=PQgetResult(pset.db))!=NULL)
@@ -579,13 +579,13 @@ SendQuery(const char *query)
579579
boolOK;
580580

581581
if (!pset.db)
582-
{
582+
{
583583
psql_error("You are currently not connected to a database.\n");
584584
return false;
585-
}
585+
}
586586

587587
if (GetVariableBool(pset.vars,"SINGLESTEP"))
588-
{
588+
{
589589
charbuf[3];
590590

591591
printf(gettext("***(Single step mode: Verify query)*********************************************\n"
@@ -596,14 +596,9 @@ SendQuery(const char *query)
596596
if (fgets(buf,sizeof(buf),stdin)!=NULL)
597597
if (buf[0]=='x')
598598
return false;
599-
}
600-
else
601-
{
602-
constchar*var=GetVariable(pset.vars,"ECHO");
603-
604-
if (var&&strncmp(var,"queries",strlen(var))==0)
605-
puts(query);
606599
}
600+
elseif (VariableEquals(pset.vars,"ECHO","queries"))
601+
puts(query);
607602

608603
SetCancelConn();
609604

@@ -619,3 +614,15 @@ SendQuery(const char *query)
619614
PrintNotifications();
620615
returnOK;
621616
}
617+
618+
619+
charparse_char(char**buf)
620+
{
621+
longl;
622+
623+
l=strtol(*buf,buf,0);
624+
(*buf)--;
625+
return (char)l;
626+
}
627+
628+

‎src/bin/psql/common.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/common.h,v 1.23 2003/03/20 06:00:12 momjian Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/common.h,v 1.24 2003/03/20 06:43:35 momjian Exp $
77
*/
88
#ifndefCOMMON_H
99
#defineCOMMON_H
@@ -42,6 +42,12 @@ extern bool SendQuery(const char *query);
4242
/* sprompt.h */
4343
externchar*simple_prompt(constchar*prompt,intmaxlen,boolecho);
4444

45+
/* Parse a numeric character code from the string pointed at by *buf, e.g.
46+
* one written as 0x0c (hexadecimal) or 015 (octal); advance *buf to the last
47+
* character of the numeric character code.
48+
*/
49+
externcharparse_char(char**buf);
50+
4551
/* Used for all Win32 popen/pclose calls */
4652
#ifdefWIN32
4753
#definepopen(x,y) _popen(x,y)

‎src/bin/psql/input.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/input.c,v 1.22 2003/03/20 06:00:12 momjian Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/input.c,v 1.23 2003/03/20 06:43:35 momjian Exp $
77
*/
88
#include"postgres_fe.h"
99
#include"input.h"
@@ -233,10 +233,12 @@ finishInput(int exitstatus, void *arg)
233233
strlen(PSQLHISTORY)+1);
234234
if (psql_history)
235235
{
236-
constchar*var=GetVariable(pset.vars,"HISTSIZE");
236+
inthist_size;
237+
hist_size=GetVariableNum(pset.vars,"HISTSIZE",-1,-1,true);
238+
239+
if (hist_size >=0)
240+
stifle_history(hist_size);
237241

238-
if (var)
239-
stifle_history(atoi(var));
240242
sprintf(psql_history,"%s/%s",home,PSQLHISTORY);
241243
write_history(psql_history);
242244
free(psql_history);

‎src/bin/psql/large_obj.c

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000-2002 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/large_obj.c,v 1.23 2002/10/15 02:24:16 tgl Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/large_obj.c,v 1.24 2003/03/20 06:43:35 momjian Exp $
77
*/
88
#include"postgres_fe.h"
99
#include"large_obj.h"
@@ -40,15 +40,20 @@ _my_notice_handler(void *arg, const char *message)
4040
staticbool
4141
handle_transaction(void)
4242
{
43-
constchar*var=GetVariable(pset.vars,"LO_TRANSACTION");
4443
PGresult*res;
45-
boolcommit;
44+
boolcommit= false;
4645
PQnoticeProcessorold_notice_hook;
4746

48-
if (var&&strcmp(var,"nothing")==0)
47+
switch (SwitchVariable(pset.vars,"LO_TRANSACTION",
48+
"nothing",
49+
"commit",
50+
NULL))
51+
{
52+
case1:
4953
return true;
50-
51-
commit= (var&&strcmp(var,"commit")==0);
54+
case2:
55+
commit= true;
56+
}
5257

5358
notice[0]='\0';
5459
old_notice_hook=PQsetNoticeProcessor(pset.db,_my_notice_handler,NULL);
@@ -87,11 +92,9 @@ do_lo_export(const char *loid_arg, const char *filename_arg)
8792
{
8893
PGresult*res;
8994
intstatus;
90-
boolown_transaction= true;
91-
constchar*var=GetVariable(pset.vars,"LO_TRANSACTION");
95+
boolown_transaction;
9296

93-
if (var&&strcmp(var,"nothing")==0)
94-
own_transaction= false;
97+
own_transaction=VariableEquals(pset.vars,"LO_TRANSACTION","nothing");
9598

9699
if (!pset.db)
97100
{
@@ -154,11 +157,9 @@ do_lo_import(const char *filename_arg, const char *comment_arg)
154157
Oidloid;
155158
charoidbuf[32];
156159
unsignedinti;
157-
boolown_transaction= true;
158-
constchar*var=GetVariable(pset.vars,"LO_TRANSACTION");
160+
boolown_transaction;
159161

160-
if (var&&strcmp(var,"nothing")==0)
161-
own_transaction= false;
162+
own_transaction=VariableEquals(pset.vars,"LO_TRANSACTION","nothing");
162163

163164
if (!pset.db)
164165
{
@@ -271,11 +272,10 @@ do_lo_unlink(const char *loid_arg)
271272
intstatus;
272273
Oidloid=atooid(loid_arg);
273274
charbuf[256];
274-
boolown_transaction= true;
275-
constchar*var=GetVariable(pset.vars,"LO_TRANSACTION");
276275

277-
if (var&&strcmp(var,"nothing")==0)
278-
own_transaction= false;
276+
boolown_transaction;
277+
278+
own_transaction=VariableEquals(pset.vars,"LO_TRANSACTION","nothing");
279279

280280
if (!pset.db)
281281
{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp