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

Commit2a24ec6

Browse files
committed
In the spirit of TODO item
* Add use of 'const' for varibles in source tree(which is misspelled, btw.)I went through the front-end libpq code and did so. This affects inparticular the various accessor functions (such as PQdb() andPQgetvalue()) as well as, by necessity, the internal helpers they use.I have been really thorough in that regard, perhaps some people will findit annoying that things likechar * foo = PQgetvalue(res, 0, 0)will generate a warning. On the other hand it _should_ generate one. Thisis no real compatibility break, although a few clients will have to befixed to suppress warnings. (Which again would be in the spirit of theabove TODO.)In addition I replaced some int's by size_t's and removed some warnings(and generated some new ones -- grmpf!). Also I rewrote PQoidStatus (so itactually honors the const!) and supplied a new function PQoidValue thatreturns a proper Oid type. This is only front-end stuff, none of thecommunicaton stuff was touched.The psql patch also adds some new consts to honor the new libpq situation,as well as fixes a fatal condition that resulted when using the -V(--version) option and there is no database listening.So, to summarize, the psql you should definitely put in (with or withoutthe libpq). If you think I went too far with the const-mania in libpq, letme know and I'll make adjustments. If you approve it, I will also updatethe docs. -Peter--Peter Eisentraut Sernanders vaeg 10:115
1 parentc6c6030 commit2a24ec6

File tree

12 files changed

+267
-261
lines changed

12 files changed

+267
-261
lines changed

‎src/bin/psql/command.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ do_connect(const char *new_dbname, const char *new_user, PsqlSettings *pset)
828828
PGconn*oldconn=pset->db;
829829
constchar*dbparam=NULL;
830830
constchar*userparam=NULL;
831-
char*pwparam=NULL;
831+
constchar*pwparam=NULL;
832832
char*prompted_password=NULL;
833833
char*prompted_user=NULL;
834834
boolneed_pass;

‎src/bin/psql/describe.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -519,8 +519,8 @@ describeTableDetails(const char *name, PsqlSettings *pset)
519519
printTableOptmyopt=pset->popt.topt;
520520
booldescription=GetVariableBool(pset->vars,"description");
521521
inti;
522-
char*view_def=NULL;
523-
char*headers[5];
522+
constchar*view_def=NULL;
523+
constchar*headers[5];
524524
char**cells=NULL;
525525
char*title=NULL;
526526
char**footers=NULL;
@@ -587,11 +587,10 @@ describeTableDetails(const char *name, PsqlSettings *pset)
587587
for (i=0;i<PQntuples(res);i++)
588588
{
589589
int4attypmod=atoi(PQgetvalue(res,i,3));
590-
char*attype=PQgetvalue(res,i,1);
590+
constchar*attype=PQgetvalue(res,i,1);
591591

592592
/* Name */
593-
cells[i*cols+0]=PQgetvalue(res,i,0);/* don't free this
594-
* afterwards */
593+
cells[i*cols+0]= (char*)PQgetvalue(res,i,0);/* don't free this afterwards */
595594

596595
/* Type */
597596
cells[i*cols+1]=xmalloc(NAMEDATALEN+16);
@@ -609,7 +608,7 @@ describeTableDetails(const char *name, PsqlSettings *pset)
609608

610609
/* Info */
611610
cells[i*cols+2]=xmalloc(128+128);/* I'm cutting off the
612-
* default string at 128 */
611+
*'default' string at 128 */
613612
cells[i*cols+2][0]='\0';
614613
if (strcmp(PQgetvalue(res,i,4),"t")==0)
615614
strcat(cells[i*cols+2],"not null");
@@ -633,7 +632,7 @@ describeTableDetails(const char *name, PsqlSettings *pset)
633632

634633
/* Description */
635634
if (description)
636-
cells[i*cols+3]=PQgetvalue(res,i,7);
635+
cells[i*cols+3]=(char*)PQgetvalue(res,i,7);
637636
}
638637

639638
/* Make title */
@@ -685,7 +684,7 @@ describeTableDetails(const char *name, PsqlSettings *pset)
685684

686685

687686
myopt.tuples_only= false;
688-
printTable(title,headers,cells,footers,"llll",&myopt,pset->queryFout);
687+
printTable(title,headers,(constchar**)cells,(constchar**)footers,"llll",&myopt,pset->queryFout);
689688

690689
/* clean up */
691690
free(title);

‎src/bin/psql/print.c

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@
2828

2929

3030
staticvoid
31-
print_unaligned_text(constchar*title,char**headers,char**cells,char**footers,
31+
print_unaligned_text(constchar*title,constchar*const*headers,
32+
constchar*const*cells,constchar*const*footers,
3233
constchar*opt_fieldsep,boolopt_barebones,
3334
FILE*fout)
3435
{
3536
unsignedintcol_count=0;
3637
unsignedinti;
37-
char**ptr;
38+
constchar*const*ptr;
3839

3940
if (!opt_fieldsep)
4041
opt_fieldsep="";
@@ -80,14 +81,15 @@ print_unaligned_text(const char *title, char **headers, char **cells, char **foo
8081

8182

8283
staticvoid
83-
print_unaligned_vertical(constchar*title,char**headers,char**cells,char**footers,
84+
print_unaligned_vertical(constchar*title,constchar*const*headers,
85+
constchar*const*cells,constchar*const*footers,
8486
constchar*opt_fieldsep,boolopt_barebones,
8587
FILE*fout)
8688
{
8789
unsignedintcol_count=0;
8890
unsignedinti;
8991
unsignedintrecord=1;
90-
char**ptr;
92+
constchar*const*ptr;
9193

9294
if (!opt_fieldsep)
9395
opt_fieldsep="";
@@ -167,16 +169,17 @@ _print_horizontal_line(const unsigned int col_count, const unsigned int *widths,
167169

168170

169171
staticvoid
170-
print_aligned_text(constchar*title,char**headers,char**cells,char**footers,
171-
constchar*opt_align,boolopt_barebones,unsigned shortintopt_border,
172+
print_aligned_text(constchar*title,constchar*const*headers,
173+
constchar*const*cells,constchar*const*footers,
174+
constchar*opt_align,boolopt_barebones,unsigned shortintopt_border,
172175
FILE*fout)
173176
{
174177
unsignedintcol_count=0;
175178
unsignedinti,
176179
tmp;
177180
unsignedint*widths,
178181
total_w;
179-
char**ptr;
182+
constchar*const*ptr;
180183

181184
/* count columns */
182185
for (ptr=headers;*ptr;ptr++)
@@ -308,13 +311,14 @@ const char *opt_align, bool opt_barebones, unsigned short int opt_border,
308311

309312

310313
staticvoid
311-
print_aligned_vertical(constchar*title,char**headers,char**cells,char**footers,
314+
print_aligned_vertical(constchar*title,constchar*const*headers,
315+
constchar*const*cells,constchar*const*footers,
312316
boolopt_barebones,unsigned shortintopt_border,
313317
FILE*fout)
314318
{
315319
unsignedintcol_count=0;
316320
unsignedintrecord=1;
317-
char**ptr;
321+
constchar*const*ptr;
318322
unsignedinti,
319323
tmp,
320324
hwidth=0,
@@ -471,14 +475,15 @@ html_escaped_print(const char *in, FILE *fout)
471475

472476

473477
staticvoid
474-
print_html_text(constchar*title,char**headers,char**cells,char**footers,
475-
constchar*opt_align,boolopt_barebones,unsigned shortintopt_border,
476-
char*opt_table_attr,
478+
print_html_text(constchar*title,constchar*const*headers,
479+
constchar*const*cells,constchar*const*footers,
480+
constchar*opt_align,boolopt_barebones,unsigned shortintopt_border,
481+
constchar*opt_table_attr,
477482
FILE*fout)
478483
{
479484
unsignedintcol_count=0;
480485
unsignedinti;
481-
char**ptr;
486+
constchar*const*ptr;
482487

483488
fprintf(fout,"<table border=%d",opt_border);
484489
if (opt_table_attr)
@@ -544,15 +549,16 @@ const char *opt_align, bool opt_barebones, unsigned short int opt_border,
544549

545550

546551
staticvoid
547-
print_html_vertical(constchar*title,char**headers,char**cells,char**footers,
548-
constchar*opt_align,boolopt_barebones,unsigned shortintopt_border,
549-
char*opt_table_attr,
552+
print_html_vertical(constchar*title,constchar*const*headers,
553+
constchar*const*cells,constchar*const*footers,
554+
constchar*opt_align,boolopt_barebones,unsigned shortintopt_border,
555+
constchar*opt_table_attr,
550556
FILE*fout)
551557
{
552558
unsignedintcol_count=0;
553559
unsignedinti;
554560
unsignedintrecord=1;
555-
char**ptr;
561+
constchar*const*ptr;
556562

557563
fprintf(fout,"<table border=%d",opt_border);
558564
if (opt_table_attr)
@@ -652,14 +658,15 @@ latex_escaped_print(const char *in, FILE *fout)
652658

653659

654660
staticvoid
655-
print_latex_text(constchar*title,char**headers,char**cells,char**footers,
656-
constchar*opt_align,boolopt_barebones,unsigned shortintopt_border,
661+
print_latex_text(constchar*title,constchar*const*headers,
662+
constchar*const*cells,constchar*const*footers,
663+
constchar*opt_align,boolopt_barebones,unsigned shortintopt_border,
657664
FILE*fout)
658665
{
659666
unsignedintcol_count=0;
660667
unsignedinti;
661668
constchar*cp;
662-
char**ptr;
669+
constchar*const*ptr;
663670

664671

665672
/* print title */
@@ -747,13 +754,14 @@ const char *opt_align, bool opt_barebones, unsigned short int opt_border,
747754

748755

749756
staticvoid
750-
print_latex_vertical(constchar*title,char**headers,char**cells,char**footers,
751-
constchar*opt_align,boolopt_barebones,unsigned shortintopt_border,
757+
print_latex_vertical(constchar*title,constchar*const*headers,
758+
constchar*const*cells,constchar*const*footers,
759+
constchar*opt_align,boolopt_barebones,unsigned shortintopt_border,
752760
FILE*fout)
753761
{
754762
unsignedintcol_count=0;
755763
unsignedinti;
756-
char**ptr;
764+
constchar*const*ptr;
757765
unsignedintrecord=1;
758766

759767
(void)opt_align;/* currently unused parameter */
@@ -833,11 +841,14 @@ const char *opt_align, bool opt_barebones, unsigned short int opt_border,
833841

834842

835843
void
836-
printTable(constchar*title,char**headers,char**cells,char**footers,
844+
printTable(constchar*title,
845+
constchar*const*headers,
846+
constchar*const*cells,
847+
constchar*const*footers,
837848
constchar*align,
838849
constprintTableOpt*opt,FILE*fout)
839850
{
840-
char*default_footer[]= {NULL};
851+
constchar*default_footer[]= {NULL};
841852
unsigned shortintborder=opt->border;
842853
FILE*pager=NULL,
843854
*output;
@@ -868,7 +879,7 @@ printTable(const char *title, char **headers, char **cells, char **footers,
868879
unsignedintcol_count=0,
869880
row_count=0,
870881
lines;
871-
char**ptr;
882+
constchar*const*ptr;
872883
intresult;
873884
structwinsizescreen_size;
874885

@@ -952,11 +963,11 @@ printTable(const char *title, char **headers, char **cells, char **footers,
952963

953964

954965
void
955-
printQuery(PGresult*result,constprintQueryOpt*opt,FILE*fout)
966+
printQuery(constPGresult*result,constprintQueryOpt*opt,FILE*fout)
956967
{
957968
intnfields;
958-
char**headers;
959-
char**cells;
969+
constchar**headers;
970+
constchar**cells;
960971
char**footers;
961972
char*align;
962973
inti;
@@ -1043,8 +1054,9 @@ printQuery(PGresult *result, const printQueryOpt * opt, FILE *fout)
10431054

10441055
/* call table printer */
10451056

1046-
printTable(opt->title,headers,cells,footers ?footers :opt->footers,align,
1047-
&opt->topt,fout);
1057+
printTable(opt->title,headers,cells,
1058+
footers ? (constchar*const*)footers : (constchar*const*)(opt->footers),
1059+
align,&opt->topt,fout);
10481060

10491061
free(headers);
10501062
free(cells);

‎src/bin/psql/print.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ typedef struct _printTableOpt
4444
* - align is an 'l' or an 'r' for every column, if the output format needs it.
4545
* (You must specify this long enough. Otherwise anything could happen.)
4646
*/
47-
voidprintTable(constchar*title,char**headers,char**cells,char**footers,
47+
voidprintTable(constchar*title,constchar*const*headers,
48+
constchar*const*cells,constchar*const*footers,
4849
constchar*align,
4950
constprintTableOpt*opt,FILE*fout);
5051

@@ -66,7 +67,7 @@ typedef struct _printQueryOpt
6667
* It calls the printTable above with all the things set straight.
6768
*/
6869
void
69-
printQuery(PGresult*result,constprintQueryOpt*opt,FILE*fout);
70+
printQuery(constPGresult*result,constprintQueryOpt*opt,FILE*fout);
7071

7172

7273
#endif/* PRINT_H */

‎src/bin/psql/startup.c

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@
3737

3838

3939
staticvoid
40-
process_psqlrc(PsqlSettings*pset);
40+
process_psqlrc(PsqlSettings*pset);
4141

4242
staticvoid
43-
showVersion(PsqlSettings*pset,boolverbose);
43+
showVersion(PsqlSettings*pset);
4444

4545

4646
/* Structures to pass information between the option parsing routine
@@ -68,7 +68,7 @@ struct adhoc_opts
6868
};
6969

7070
staticvoid
71-
parse_options(intargc,char*argv[],PsqlSettings*pset,structadhoc_opts*options);
71+
parse_options(intargc,char*argv[],PsqlSettings*pset,structadhoc_opts*options);
7272

7373

7474

@@ -152,7 +152,7 @@ main(int argc, char **argv)
152152
free(username);
153153
free(password);
154154

155-
if (PQstatus(settings.db)==CONNECTION_BAD)
155+
if (PQstatus(settings.db)==CONNECTION_BAD&&options.action!=ACT_SHOW_VER)
156156
{
157157
fprintf(stderr,"Connection to database '%s' failed.\n%s\n",PQdb(settings.db),PQerrorMessage(settings.db));
158158
PQfinish(settings.db);
@@ -169,19 +169,16 @@ main(int argc, char **argv)
169169

170170
if (options.action==ACT_SHOW_VER)
171171
{
172-
showVersion(&settings, true);
172+
showVersion(&settings);
173173
PQfinish(settings.db);
174174
exit(EXIT_SUCCESS);
175175
}
176176

177177

178178
if (!GetVariable(settings.vars,"quiet")&& !settings.notty&& !options.action)
179179
{
180-
puts("Welcome to psql, the PostgreSQL interactive terminal.\n");
181-
182-
//showVersion(&settings, false);
183-
184-
puts("Type: \\copyright for distribution terms\n"
180+
puts("Welcome to psql, the PostgreSQL interactive terminal.\n\n"
181+
"Type: \\copyright for distribution terms\n"
185182
" \\h for help with SQL commands\n"
186183
" \\? for help on internal slash commands\n"
187184
" \\g or terminate with semicolon to execute query\n"
@@ -509,28 +506,22 @@ process_psqlrc(PsqlSettings *pset)
509506
* or a mismatch was detected.
510507
*/
511508
staticvoid
512-
showVersion(PsqlSettings*pset,boolverbose)
509+
showVersion(PsqlSettings*pset)
513510
{
514-
PGresult*res;
515-
char*versionstr=NULL;
511+
PGresult*res=NULL;
512+
constchar*versionstr=NULL;
516513
longintrelease=0,
517514
version=0,
518515
subversion=0;
519516

520517
/* get backend version */
518+
if (pset->db&&PQstatus(pset->db)==CONNECTION_OK) {
521519
res=PSQLexec(pset,"SELECT version()");
522520
if (PQresultStatus(res)==PGRES_TUPLES_OK)
523521
versionstr=PQgetvalue(res,0,0);
524-
525-
if (!verbose)
526-
{
527-
if (versionstr)
528-
puts(versionstr);
529-
PQclear(res);
530-
return;
531522
}
532523

533-
if (strncmp(versionstr,"PostgreSQL ",11)==0)
524+
if (versionstr&&strncmp(versionstr,"PostgreSQL ",11)==0)
534525
{
535526
char*tmp;
536527

@@ -539,9 +530,9 @@ showVersion(PsqlSettings *pset, bool verbose)
539530
subversion=strtol(tmp+1,&tmp,10);
540531
}
541532

542-
printf("Server: %s\npsql",versionstr ?versionstr :"(could notconnected)");
533+
printf("Server: %s\npsql",versionstr ?versionstr :"(could notconnect)");
543534

544-
if (strcmp(versionstr,PG_VERSION_STR)!=0)
535+
if (!versionstr||strcmp(versionstr,PG_VERSION_STR)!=0)
545536
printf(&PG_VERSION_STR[strcspn(PG_VERSION_STR," ")]);
546537
printf(" ("__DATE__" "__TIME__")");
547538

@@ -569,10 +560,11 @@ showVersion(PsqlSettings *pset, bool verbose)
569560

570561
puts("");
571562

572-
if (release<6|| (release==6&&version<5))
563+
if (versionstr&& (release<6|| (release==6&&version<5)))
573564
puts("\nWarning: The server you are connected to is potentially too old for this client\n"
574565
"version. You should ideally be using clients and servers from the same\n"
575566
"distribution.");
576567

568+
if (res)
577569
PQclear(res);
578570
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp