Commit 255363f8 authored by Bryan Henderson's avatar Bryan Henderson

Clean up. Get rid of tabs and overly long lines.

parent ea8b5196
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.19 1996/11/20 22:35:19 momjian Exp $ * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.20 1996/12/13 09:25:08 bryanh Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -32,7 +32,7 @@ struct winsize { ...@@ -32,7 +32,7 @@ struct winsize {
} screen_size; } screen_size;
#endif #endif
/* the tuples array in a PGresGroup has to grow to accommodate the tuples */ /* the rows array in a PGresGroup has to grow to accommodate the rows */
/* returned. Each time, we grow by this much: */ /* returned. Each time, we grow by this much: */
#define TUPARR_GROW_BY 100 #define TUPARR_GROW_BY 100
...@@ -66,7 +66,7 @@ PQclear(PGresult* res) ...@@ -66,7 +66,7 @@ PQclear(PGresult* res)
if (!res) if (!res)
return; return;
/* free all the tuples */ /* free all the rows */
for (i=0;i<res->ntups;i++) { for (i=0;i<res->ntups;i++) {
for (j=0;j<res->numAttributes;j++) { for (j=0;j<res->numAttributes;j++) {
if (res->tuples[i][j].value) if (res->tuples[i][j].value)
...@@ -114,7 +114,7 @@ makeEmptyPGresult(PGconn *conn, ExecStatusType status) ...@@ -114,7 +114,7 @@ makeEmptyPGresult(PGconn *conn, ExecStatusType status)
/* /*
* getTuple - * getTuple -
* get the next tuple from the stream * get the next row from the stream
* *
* the CALLER is responsible from freeing the PGresAttValue returned * the CALLER is responsible from freeing the PGresAttValue returned
*/ */
...@@ -147,7 +147,7 @@ getTuple(PGconn *conn, PGresult* result, int binary) ...@@ -147,7 +147,7 @@ getTuple(PGconn *conn, PGresult* result, int binary)
if (pqGetnchar(bitmap, nbytes, pfin, pfdebug) == 1){ if (pqGetnchar(bitmap, nbytes, pfin, pfdebug) == 1){
sprintf(conn->errorMessage, sprintf(conn->errorMessage,
"Error reading null-values bitmap from tuple data stream\n"); "Error reading null-values bitmap from row data stream\n");
return NULL; return NULL;
} }
...@@ -191,7 +191,7 @@ getTuple(PGconn *conn, PGresult* result, int binary) ...@@ -191,7 +191,7 @@ getTuple(PGconn *conn, PGresult* result, int binary)
/* /*
* addTuple * addTuple
* add a tuple to the PGresult structure, growing it if necessary * add a row to the PGresult structure, growing it if necessary
* to accommodate * to accommodate
* *
*/ */
...@@ -217,11 +217,11 @@ addTuple(PGresult* res, PGresAttValue* tup) ...@@ -217,11 +217,11 @@ addTuple(PGresult* res, PGresAttValue* tup)
/* /*
* PGresult * PGresult
* fill out the PGresult structure with result tuples from the backend * fill out the PGresult structure with result rows from the backend
* this is called after query has been successfully run and we have * this is called after query has been successfully run and we have
* a portal name * a portal name
* *
* ASSUMPTION: we assume only *1* tuple group is returned from the backend * ASSUMPTION: we assume only *1* row group is returned from the backend
* *
* the CALLER is reponsible for free'ing the new PGresult allocated here * the CALLER is reponsible for free'ing the new PGresult allocated here
* *
...@@ -284,34 +284,30 @@ makePGresult(PGconn* conn, char* pname) ...@@ -284,34 +284,30 @@ makePGresult(PGconn* conn, char* pname)
/* process the data stream until we're finished */ /* process the data stream until we're finished */
while(!done) { while(!done) {
switch (id) { switch (id) {
case 'T': /* a new tuple group */ case 'T': /* a new row group */
sprintf(conn->errorMessage, sprintf(conn->errorMessage,
"makePGresult() -- is not equipped to handle multiple tuple groups.\n"); "makePGresult() -- "
"is not equipped to handle multiple row groups.\n");
goto makePGresult_badResponse_return; goto makePGresult_badResponse_return;
case 'B': /* a tuple in binary format */ case 'B': /* a row in binary format */
case 'D': /* a tuple in ASCII format */ case 'D': /* a row in ASCII format */
newTup = getTuple(conn, result, (id == 'B')); newTup = getTuple(conn, result, (id == 'B'));
if (newTup == NULL) if (newTup == NULL)
goto makePGresult_badResponse_return; goto makePGresult_badResponse_return;
addTuple(result,newTup); addTuple(result,newTup);
break; break;
/* case 'A': case 'C': /* end of portal row stream */
sprintf(conn->errorMessage, "Asynchronous portals not supported");
result->resultStatus = PGRES_NONFATAL_ERROR;
return result;
break;
*/
case 'C': /* end of portal tuple stream */
{ {
char command[MAX_MESSAGE_LEN]; char command[MAX_MESSAGE_LEN];
pqGets(command,MAX_MESSAGE_LEN, pfin, pfdebug); /* read the command tag */ pqGets(command,MAX_MESSAGE_LEN, pfin, pfdebug); /* read command tag */
done = 1; done = 1;
} }
break; break;
case 'E': /* errors */ case 'E': /* errors */
if (pqGets(conn->errorMessage, ERROR_MSG_LENGTH, pfin, pfdebug) == 1) { if (pqGets(conn->errorMessage, ERROR_MSG_LENGTH, pfin, pfdebug) == 1) {
sprintf(conn->errorMessage, sprintf(conn->errorMessage,
"Error return detected from backend, but error message cannot be read"); "Error return detected from backend, "
"but error message cannot be read");
} }
result->resultStatus = PGRES_FATAL_ERROR; result->resultStatus = PGRES_FATAL_ERROR;
return result; return result;
...@@ -319,7 +315,8 @@ makePGresult(PGconn* conn, char* pname) ...@@ -319,7 +315,8 @@ makePGresult(PGconn* conn, char* pname)
case 'N': /* notices from the backend */ case 'N': /* notices from the backend */
if (pqGets(conn->errorMessage, ERROR_MSG_LENGTH, pfin, pfdebug) == 1) { if (pqGets(conn->errorMessage, ERROR_MSG_LENGTH, pfin, pfdebug) == 1) {
sprintf(conn->errorMessage, sprintf(conn->errorMessage,
"Notice return detected from backend, but error message cannot be read"); "Notice return detected from backend, "
"but error message cannot be read");
} else } else
/* XXXX send Notices to stderr for now */ /* XXXX send Notices to stderr for now */
fprintf(stderr, "%s\n", conn->errorMessage); fprintf(stderr, "%s\n", conn->errorMessage);
...@@ -327,7 +324,9 @@ makePGresult(PGconn* conn, char* pname) ...@@ -327,7 +324,9 @@ makePGresult(PGconn* conn, char* pname)
default: /* uh-oh default: /* uh-oh
this should never happen but frequently does when the this should never happen but frequently does when the
backend dumps core */ backend dumps core */
sprintf(conn->errorMessage,"FATAL: unexpected results from the backend, it probably dumped core."); sprintf(conn->errorMessage,
"FATAL: unrecognized data from the backend. "
"It probably dumped core.");
fprintf(stderr, conn->errorMessage); fprintf(stderr, conn->errorMessage);
result->resultStatus = PGRES_FATAL_ERROR; result->resultStatus = PGRES_FATAL_ERROR;
return result; return result;
...@@ -445,10 +444,11 @@ PQexec(PGconn* conn, const char* query) ...@@ -445,10 +444,11 @@ PQexec(PGconn* conn, const char* query)
/* async messages are piggy'ed back on other messages, /* async messages are piggy'ed back on other messages,
so we stay in the while loop for other messages */ so we stay in the while loop for other messages */
break; break;
case 'C': /* portal query command, no tuples returned */ case 'C': /* portal query command, no rows returned */
if (pqGets(cmdStatus, MAX_MESSAGE_LEN, pfin, pfdebug) == 1) { if (pqGets(cmdStatus, MAX_MESSAGE_LEN, pfin, pfdebug) == 1) {
sprintf(conn->errorMessage, sprintf(conn->errorMessage,
"PQexec() -- query command completed, but return message from backend cannot be read"); "PQexec() -- query command completed, "
"but return message from backend cannot be read.");
return (PGresult*)NULL; return (PGresult*)NULL;
} }
else { else {
...@@ -495,7 +495,8 @@ PQexec(PGconn* conn, const char* query) ...@@ -495,7 +495,8 @@ PQexec(PGconn* conn, const char* query)
case 'E': /* error return */ case 'E': /* error return */
if (pqGets(conn->errorMessage, ERROR_MSG_LENGTH, pfin, pfdebug) == 1) { if (pqGets(conn->errorMessage, ERROR_MSG_LENGTH, pfin, pfdebug) == 1) {
(void) sprintf(conn->errorMessage, (void) sprintf(conn->errorMessage,
"PQexec() -- error return detected from backend, but error message cannot be read"); "PQexec() -- error return detected from backend, "
"but attempt to read the error message failed.");
} }
return (PGresult*)NULL; return (PGresult*)NULL;
break; break;
...@@ -506,7 +507,6 @@ PQexec(PGconn* conn, const char* query) ...@@ -506,7 +507,6 @@ PQexec(PGconn* conn, const char* query)
if ((c = pqGetc(pfin,pfdebug)) != '\0') { if ((c = pqGetc(pfin,pfdebug)) != '\0') {
fprintf(stderr,"error!, unexpected character %c following 'I'\n", c); fprintf(stderr,"error!, unexpected character %c following 'I'\n", c);
} }
#ifdef PQ_NOTIFY_PATCH
if (isCommand) { if (isCommand) {
/* /*
* If this is the result of a portal query command set the * If this is the result of a portal query command set the
...@@ -516,7 +516,6 @@ PQexec(PGconn* conn, const char* query) ...@@ -516,7 +516,6 @@ PQexec(PGconn* conn, const char* query)
strncpy(result->cmdStatus,cmdStatus, CMDSTATUS_LEN-1); strncpy(result->cmdStatus,cmdStatus, CMDSTATUS_LEN-1);
return result; return result;
} }
#endif
result = makeEmptyPGresult(conn, PGRES_EMPTY_QUERY); result = makeEmptyPGresult(conn, PGRES_EMPTY_QUERY);
return result; return result;
} }
...@@ -524,16 +523,17 @@ PQexec(PGconn* conn, const char* query) ...@@ -524,16 +523,17 @@ PQexec(PGconn* conn, const char* query)
case 'N': /* notices from the backend */ case 'N': /* notices from the backend */
if (pqGets(conn->errorMessage, ERROR_MSG_LENGTH, pfin, pfdebug) == 1) { if (pqGets(conn->errorMessage, ERROR_MSG_LENGTH, pfin, pfdebug) == 1) {
sprintf(conn->errorMessage, sprintf(conn->errorMessage,
"PQexec() -- error return detected from backend, but error message cannot be read"); "PQexec() -- Notice detected from backend, "
"but attempt to read the notice failed.");
return (PGresult*)NULL; return (PGresult*)NULL;
} }
else else
fprintf(stderr,"%s", conn->errorMessage); fprintf(stderr,"%s", conn->errorMessage);
break; break;
case 'P': /* synchronous (normal) portal */ case 'P': /* synchronous (normal) portal */
pqGets(pname,MAX_MESSAGE_LEN,pfin, pfdebug); /* read in the portal name*/ pqGets(pname,MAX_MESSAGE_LEN,pfin, pfdebug); /* read in portal name*/
break; break;
case 'T': /* actual tuple results: */ case 'T': /* actual row results: */
return makePGresult(conn, pname); return makePGresult(conn, pname);
break; break;
case 'D': /* copy command began successfully */ case 'D': /* copy command began successfully */
...@@ -668,7 +668,8 @@ PQendcopy(PGconn *conn) ...@@ -668,7 +668,8 @@ PQendcopy(PGconn *conn)
case 'N': case 'N':
if (pqGets(conn->errorMessage, ERROR_MSG_LENGTH, pfin, pfdebug) == 1) { if (pqGets(conn->errorMessage, ERROR_MSG_LENGTH, pfin, pfdebug) == 1) {
sprintf(conn->errorMessage, sprintf(conn->errorMessage,
"Error return detected from backend, but error message cannot be read"); "Error return detected from backend, "
"but attempt to read the message failed.");
} }
return(0); return(0);
break; break;
...@@ -813,7 +814,7 @@ PQprintTuples(PGresult *res, ...@@ -813,7 +814,7 @@ PQprintTuples(PGresult *res,
} else } else
sprintf(formatString,"%%s %%s"); sprintf(formatString,"%%s %%s");
if ( nFields > 0 ) { /* only print tuples with at least 1 field. */ if ( nFields > 0 ) { /* only print rows with at least 1 field. */
if (!TerseOutput) if (!TerseOutput)
{ {
...@@ -856,13 +857,206 @@ PQprintTuples(PGresult *res, ...@@ -856,13 +857,206 @@ PQprintTuples(PGresult *res,
} }
} }
static void
do_field(PQprintOpt *po, PGresult *res,
const int i, const int j, char *buf, const int fs_len,
char *fields[],
const int nFields, char *fieldNames[],
unsigned char fieldNotNum[], int fieldMax[],
const int fieldMaxLen, FILE *fout
) {
char *pval, *p, *o;
int plen;
bool skipit;
plen=PQgetlength(res,i,j);
pval=PQgetvalue(res,i,j);
if (plen < 1 || !pval || !*pval) {
if (po->align || po->expanded) skipit = true;
else {
skipit = false;
goto efield;
}
} else skipit = false;
if (!skipit) {
for (p=pval, o=buf; *p; *(o++)=*(p++)) {
if ((fs_len==1 && (*p==*(po->fieldSep))) || *p=='\\')
*(o++)='\\';
if (po->align && (*pval=='E' || *pval=='e' ||
!((*p>='0' && *p<='9') ||
*p=='.' ||
*p=='E' ||
*p=='e' ||
*p==' ' ||
*p=='-')))
fieldNotNum[j]=1;
}
*o='\0';
if (!po->expanded && (po->align || po->html3)) {
int n=strlen(buf);
if (n>fieldMax[j])
fieldMax[j]=n;
if (!(fields[i*nFields+j]=(char *)malloc(n+1))) {
perror("malloc");
exit(1);
}
strcpy(fields[i*nFields+j], buf);
} else {
if (po->expanded) {
if (po->html3)
fprintf(fout,
"<tr><td align=left><b>%s</b></td>"
"<td align=%s>%s</td></tr>\n",
fieldNames[j],
fieldNotNum[j] ? "left": "right",
buf);
else {
if (po->align)
fprintf(fout,
"%-*s%s %s\n",
fieldMaxLen-fs_len, fieldNames[j], po->fieldSep,
buf);
else
fprintf(fout, "%s%s%s\n", fieldNames[j], po->fieldSep, buf);
}
} else {
if (!po->html3) {
fputs(buf, fout);
efield:
if ((j+1)<nFields)
fputs(po->fieldSep, fout);
else
fputc('\n', fout);
}
}
}
}
}
static void
do_header(FILE *fout, PQprintOpt *po, const int nFields, int fieldMax[],
char *fieldNames[], unsigned char fieldNotNum[],
const int fs_len, char *border, PGresult *res) {
int j; /* for loop index */
if (po->html3)
fputs("<tr>", fout);
else {
int j; /* for loop index */
int tot=0;
int n=0;
char *p;
for (; n < nFields; n++)
tot+=fieldMax[n]+fs_len+(po->standard? 2: 0);
if (po->standard)
tot+=fs_len*2+2;
if (!(p=border=malloc(tot+1))) {
perror("malloc");
exit(1);
}
if (po->standard) {
char *fs=po->fieldSep;
while (*fs++)
*p++='+';
}
for (j=0; j < nFields; j++) {
int len;
for (len=fieldMax[j] + (po->standard? 2:0) ; len--; *p++='-');
if (po->standard || (j+1)<nFields) {
char *fs=po->fieldSep;
while (*fs++)
*p++='+';
}
}
*p='\0';
if (po->standard)
fprintf(fout, "%s\n", border);
}
if (po->standard)
fputs(po->fieldSep, fout);
for (j=0; j < nFields; j++) {
char *s=PQfname(res, j);
if (po->html3) {
fprintf(fout, "<th align=%s>%s</th>",
fieldNotNum[j]? "left": "right", fieldNames[j]);
} else {
int n=strlen(s);
if (n>fieldMax[j])
fieldMax[j]=n;
if (po->standard)
fprintf(fout,
fieldNotNum[j] ? " %-*s ": " %*s ",
fieldMax[j], s);
else
fprintf(fout, fieldNotNum[j] ? "%-*s": "%*s", fieldMax[j], s);
if (po->standard || (j+1)<nFields)
fputs(po->fieldSep, fout);
}
}
if (po->html3)
fputs("</tr>\n", fout);
else
fprintf(fout, "\n%s\n", border);
}
static void
output_row(FILE *fout, PQprintOpt *po, const int nFields, char *fields[],
unsigned char fieldNotNum[], int fieldMax[], char *border,
const int row_index) {
int field_index; /* for loop index */
if (po->html3)
fputs("<tr>", fout);
else if (po->standard)
fputs(po->fieldSep, fout);
for (field_index = 0; field_index < nFields; field_index++) {
char *p=fields[row_index*nFields+field_index];
if (po->html3)
fprintf(fout, "<td align=%s>%s</td>",
fieldNotNum[field_index]? "left": "right", p? p: "");
else {
fprintf(fout,
fieldNotNum[field_index] ?
(po->standard ? " %-*s ": "%-*s") :
(po->standard ? " %*s ": "%*s"),
fieldMax[field_index],
p ? p: "");
if (po->standard || field_index+1 < nFields)
fputs(po->fieldSep, fout);
}
if (p)
free(p);
}
if (po->html3)
fputs("</tr>", fout);
else
if (po->standard)
fprintf(fout, "\n%s", border);
fputc('\n', fout);
}
/* /*
* PQprint() * PQprint()
* *
* new PQprintTuples routine (proff@suburbia.net) * Format results of a query for printing.
*
* PQprintOpt is a typedef (structure) that containes * PQprintOpt is a typedef (structure) that containes
* various flags and options. consult libpq-fe.h for * various flags and options. consult libpq-fe.h for
* details * details
*
* Obsoletes PQprintTuples.
*/ */
void void
...@@ -875,7 +1069,7 @@ PQprint(FILE *fout, ...@@ -875,7 +1069,7 @@ PQprint(FILE *fout,
nFields = PQnfields(res); nFields = PQnfields(res);
if ( nFields > 0 ) { /* only print tuples with at least 1 field. */ if ( nFields > 0 ) { /* only print rows with at least 1 field. */
int i,j; int i,j;
int nTups; int nTups;
int *fieldMax=NULL; /* in case we don't use them */ int *fieldMax=NULL; /* in case we don't use them */
...@@ -892,34 +1086,30 @@ PQprint(FILE *fout, ...@@ -892,34 +1086,30 @@ PQprint(FILE *fout,
char buf[8192*2+1]; char buf[8192*2+1];
nTups = PQntuples(res); nTups = PQntuples(res);
if (!(fieldNames=(char **)calloc(nFields, sizeof (char *)))) if (!(fieldNames=(char **)calloc(nFields, sizeof (char *)))) {
{
perror("calloc"); perror("calloc");
exit(1); exit(1);
} }
if (!(fieldNotNum=(unsigned char *)calloc(nFields, 1))) if (!(fieldNotNum=(unsigned char *)calloc(nFields, 1))) {
{
perror("calloc"); perror("calloc");
exit(1); exit(1);
} }
if (!(fieldMax=(int *)calloc(nFields, sizeof(int)))) if (!(fieldMax=(int *)calloc(nFields, sizeof(int)))) {
{
perror("calloc"); perror("calloc");
exit(1); exit(1);
} }
for (numFieldName=0; po->fieldName && po->fieldName[numFieldName]; numFieldName++) for (numFieldName=0;
po->fieldName && po->fieldName[numFieldName];
numFieldName++)
; ;
for (j=0; j < nFields; j++) for (j=0; j < nFields; j++) {
{
int len; int len;
char *s=(j<numFieldName && po->fieldName[j][0])? po->fieldName[j]: PQfname(res, j); char *s =
(j<numFieldName && po->fieldName[j][0])?
po->fieldName[j]: PQfname(res, j);
fieldNames[j]=s; fieldNames[j]=s;
len=s? strlen(s): 0; len=s ? strlen(s): 0;
fieldMax[j] = len; fieldMax[j] = len;
/*
if (po->header && len<5)
len=5;
*/
len+=fs_len; len+=fs_len;
if (len>fieldMaxLen) if (len>fieldMaxLen)
fieldMaxLen=len; fieldMaxLen=len;
...@@ -932,14 +1122,12 @@ PQprint(FILE *fout, ...@@ -932,14 +1122,12 @@ PQprint(FILE *fout,
fout = stdout; fout = stdout;
if (po->pager && fout == stdout && if (po->pager && fout == stdout &&
isatty(fileno(stdin)) && isatty(fileno(stdin)) &&
isatty(fileno(stdout))) isatty(fileno(stdout))) {
{
/* try to pipe to the pager program if possible */ /* try to pipe to the pager program if possible */
#ifdef TIOCGWINSZ #ifdef TIOCGWINSZ
if (ioctl(fileno(stdout),TIOCGWINSZ,&screen_size) == -1 || if (ioctl(fileno(stdout),TIOCGWINSZ,&screen_size) == -1 ||
screen_size.ws_col == 0 || screen_size.ws_col == 0 ||
screen_size.ws_row == 0) screen_size.ws_row == 0) {
{
#endif #endif
screen_size.ws_row = 24; screen_size.ws_row = 24;
screen_size.ws_col = 80; screen_size.ws_col = 80;
...@@ -959,8 +1147,7 @@ PQprint(FILE *fout, ...@@ -959,8 +1147,7 @@ PQprint(FILE *fout,
(po->header != 0) * (po->header != 0) *
(total_line_length / screen_size.ws_col + 1) * 2 (total_line_length / screen_size.ws_col + 1) * 2
- (po->header != 0) *2 /* row count and newline */ - (po->header != 0) *2 /* row count and newline */
))) ))) {
{
fout = popen(pagerenv, "w"); fout = popen(pagerenv, "w");
if (fout) { if (fout) {
usePipe = 1; usePipe = 1;
...@@ -970,29 +1157,22 @@ PQprint(FILE *fout, ...@@ -970,29 +1157,22 @@ PQprint(FILE *fout,
} }
} }
if (!po->expanded && (po->align || po->html3)) if (!po->expanded && (po->align || po->html3)) {
{ if (!(fields=(char **)calloc(nFields*(nTups+1), sizeof(char *)))) {
if (!(fields=(char **)calloc(nFields*(nTups+1), sizeof(char *))))
{
perror("calloc"); perror("calloc");
exit(1); exit(1);
} }
} } else
else if (po->header && !po->html3) {
if (po->header && !po->html3) if (po->expanded) {
{
if (po->expanded)
{
if (po->align) if (po->align)
fprintf(fout, "%-*s%s Value\n", fieldMaxLen-fs_len, "Field", po->fieldSep); fprintf(fout, "%-*s%s Value\n",
fieldMaxLen-fs_len, "Field", po->fieldSep);
else else
fprintf(fout, "%s%sValue\n", "Field", po->fieldSep); fprintf(fout, "%s%sValue\n", "Field", po->fieldSep);
} } else {
else
{
int len=0; int len=0;
for (j=0; j < nFields; j++) for (j=0; j < nFields; j++) {
{
char *s=fieldNames[j]; char *s=fieldNames[j];
fputs(s, fout); fputs(s, fout);
len+=strlen(s)+fs_len; len+=strlen(s)+fs_len;
...@@ -1004,195 +1184,55 @@ PQprint(FILE *fout, ...@@ -1004,195 +1184,55 @@ PQprint(FILE *fout,
fputc('\n', fout); fputc('\n', fout);
} }
} }
if (po->expanded && po->html3) if (po->expanded && po->html3) {
{
if (po->caption) if (po->caption)
fprintf(fout, "<centre><h2>%s</h2></centre>\n", po->caption); fprintf(fout, "<centre><h2>%s</h2></centre>\n", po->caption);
else else
fprintf(fout, "<centre><h2>Query retrieved %d tuples * %d fields</h2></centre>\n", nTups, nFields); fprintf(fout,
"<centre><h2>"
"Query retrieved %d rows * %d fields"
"</h2></centre>\n",
nTups, nFields);
} }
for (i = 0; i < nTups; i++) for (i = 0; i < nTups; i++) {
{ if (po->expanded) {
if (po->expanded)
{
if (po->html3) if (po->html3)
fprintf(fout, "<table %s><caption align=high>%d</caption>\n", po->tableOpt? po->tableOpt: "", i); fprintf(fout,
"<table %s><caption align=high>%d</caption>\n",
po->tableOpt? po->tableOpt: "", i);
else else
fprintf(fout, "-- RECORD %d --\n", i); fprintf(fout, "-- RECORD %d --\n", i);
} }
for (j = 0; j < nFields; j++) for (j = 0; j < nFields; j++)
{ do_field(po, res, i, j, buf, fs_len, fields, nFields,
char *pval, *p, *o; fieldNames, fieldNotNum,
int plen; fieldMax, fieldMaxLen, fout);
if ((plen=PQgetlength(res,i,j))<1 || !(pval=PQgetvalue(res,i,j)) || !*pval)
{
if (po->align || po->expanded)
continue;
goto efield;
}
for (p=pval, o=buf; *p; *(o++)=*(p++))
{
if ((fs_len==1 && (*p==*(po->fieldSep))) || *p=='\\')
*(o++)='\\';
if (po->align && (*pval=='E' || *pval=='e' ||
!((*p>='0' && *p<='9') || *p=='.' || *p=='E' || *p=='e' || *p==' ' || *p=='-')))
fieldNotNum[j]=1;
}
*o='\0';
if (!po->expanded && (po->align || po->html3))
{
int n=strlen(buf);
if (n>fieldMax[j])
fieldMax[j]=n;
if (!(fields[i*nFields+j]=(char *)malloc(n+1)))
{
perror("malloc");
exit(1);
}
strcpy(fields[i*nFields+j], buf);
}
else
{
if (po->expanded)
{
if (po->html3)
fprintf(fout, "<tr><td align=left><b>%s</b></td><td align=%s>%s</td></tr>\n",
fieldNames[j], fieldNotNum[j]? "left": "right", buf);
else
{
if (po->align)
fprintf(fout, "%-*s%s %s\n", fieldMaxLen-fs_len, fieldNames[j], po->fieldSep, buf);
else
fprintf(fout, "%s%s%s\n", fieldNames[j], po->fieldSep, buf);
}
}
else
{
if (!po->html3)
{
fputs(buf, fout);
efield:
if ((j+1)<nFields)
fputs(po->fieldSep, fout);
else
fputc('\n', fout);
}
}
}
}
if (po->html3 && po->expanded) if (po->html3 && po->expanded)
fputs("</table>\n", fout); fputs("</table>\n", fout);
} }
if (!po->expanded && (po->align || po->html3)) if (!po->expanded && (po->align || po->html3)) {
{ if (po->html3) {
if (po->html3) if (po->header) {
{
if (po->header)
{
if (po->caption) if (po->caption)
fprintf(fout, "<table %s><caption align=high>%s</caption>\n", po->tableOpt? po->tableOpt: "", po->caption); fprintf(fout,
"<table %s><caption align=high>%s</caption>\n",
po->tableOpt? po->tableOpt: "",
po->caption);
else else
fprintf(fout, "<table %s><caption align=high>Retrieved %d tuples * %d fields</caption>\n", po->tableOpt? po->tableOpt: "", nTups, nFields); fprintf(fout,
"<table %s><caption align=high>"
"Retrieved %d rows * %d fields"
"</caption>\n",
po->tableOpt? po->tableOpt: "", nTups, nFields);
} else } else
fprintf(fout, "<table %s>", po->tableOpt? po->tableOpt: ""); fprintf(fout, "<table %s>", po->tableOpt? po->tableOpt: "");
} }
if (po->header) if (po->header)
{ do_header(fout, po, nFields, fieldMax, fieldNames, fieldNotNum,
if (po->html3) fs_len, border, res);
fputs("<tr>", fout);
else
{
int tot=0;
int n=0;
char *p;
for (; n<nFields; n++)
tot+=fieldMax[n]+fs_len+(po->standard? 2: 0);
if (po->standard)
tot+=fs_len*2+2;
if (!(p=border=malloc(tot+1)))
{
perror("malloc");
exit(1);
}
if (po->standard)
{
char *fs=po->fieldSep;
while (*fs++)
*p++='+';
}
for (j=0; j <nFields; j++)
{
int len;
for (len=fieldMax[j] + (po->standard? 2:0) ; len--; *p++='-');
if (po->standard || (j+1)<nFields)
{
char *fs=po->fieldSep;
while (*fs++)
*p++='+';
}
}
*p='\0';
if (po->standard)
fprintf(fout, "%s\n", border);
}
if (po->standard)
fputs(po->fieldSep, fout);
for (j=0; j < nFields; j++)
{
char *s=PQfname(res, j);
if (po->html3)
{
fprintf(fout, "<th align=%s>%s</th>", fieldNotNum[j]? "left": "right",
fieldNames[j]);
}
else
{
int n=strlen(s);
if (n>fieldMax[j])
fieldMax[j]=n;
if (po->standard)
fprintf(fout, fieldNotNum[j]? " %-*s ": " %*s ", fieldMax[j], s);
else
fprintf(fout, fieldNotNum[j]? "%-*s": "%*s", fieldMax[j], s);
if (po->standard || (j+1)<nFields)
fputs(po->fieldSep, fout);
}
}
if (po->html3)
fputs("</tr>\n", fout);
else
fprintf(fout, "\n%s\n", border);
}
for (i = 0; i < nTups; i++) for (i = 0; i < nTups; i++)
{ output_row(fout, po, nFields, fields,
if (po->html3) fieldNotNum, fieldMax, border, i);
fputs("<tr>", fout);
else
if (po->standard)
fputs(po->fieldSep, fout);
for (j = 0; j < nFields; j++)
{
char *p=fields[i*nFields+j];
if (po->html3)
fprintf(fout, "<td align=%s>%s</td>", fieldNotNum[j]? "left": "right", p? p: "");
else
{
fprintf(fout, fieldNotNum[j]? (po->standard? " %-*s ": "%-*s"): (po->standard? " %*s ": "%*s"), fieldMax[j], p? p: "");
if (po->standard || (j+1)<nFields)
fputs(po->fieldSep, fout);
}
if (p)
free(p);
}
if (po->html3)
fputs("</tr>", fout);
else
if (po->standard)
fprintf(fout, "\n%s", border);
fputc('\n', fout);
}
free(fields); free(fields);
} }
if (po->header && !po->html3) if (po->header && !po->html3)
...@@ -1201,8 +1241,7 @@ efield: ...@@ -1201,8 +1241,7 @@ efield:
free(fieldMax); free(fieldMax);
free(fieldNotNum); free(fieldNotNum);
free(fieldNames); free(fieldNames);
if (usePipe) if (usePipe) {
{
pclose(fout); pclose(fout);
signal(SIGPIPE, SIG_DFL); signal(SIGPIPE, SIG_DFL);
} }
...@@ -1303,9 +1342,11 @@ PQfn(PGconn *conn, ...@@ -1303,9 +1342,11 @@ PQfn(PGconn *conn,
return makeEmptyPGresult(conn,PGRES_FATAL_ERROR); return makeEmptyPGresult(conn,PGRES_FATAL_ERROR);
case 'N': case 'N':
/* print notice and go back to processing return values */ /* print notice and go back to processing return values */
if (pqGets(conn->errorMessage, ERROR_MSG_LENGTH, pfin, pfdebug) == 1) { if (pqGets(conn->errorMessage, ERROR_MSG_LENGTH, pfin, pfdebug)
== 1) {
sprintf(conn->errorMessage, sprintf(conn->errorMessage,
"Notice return detected from backend, but error message cannot be read"); "Notice return detected from backend, but message "
"cannot be read");
} else } else
fprintf(stderr, "%s\n", conn->errorMessage); fprintf(stderr, "%s\n", conn->errorMessage);
/* keep iterating */ /* keep iterating */
...@@ -1472,7 +1513,7 @@ const char* PQoidStatus(PGresult *res) { ...@@ -1472,7 +1513,7 @@ const char* PQoidStatus(PGresult *res) {
/* /*
PQgetvalue: PQgetvalue:
return the attribute value of field 'field_num' of return the attribute value of field 'field_num' of
tuple 'tup_num' row 'tup_num'
If res is binary, then the value returned is NOT a null-terminated If res is binary, then the value returned is NOT a null-terminated
ASCII string, but the binary representation in the server's native ASCII string, but the binary representation in the server's native
...@@ -1491,7 +1532,8 @@ PQgetvalue(PGresult *res, int tup_num, int field_num) ...@@ -1491,7 +1532,8 @@ PQgetvalue(PGresult *res, int tup_num, int field_num)
if (tup_num > (res->ntups - 1) || if (tup_num > (res->ntups - 1) ||
field_num > (res->numAttributes - 1)) { field_num > (res->numAttributes - 1)) {
fprintf(stderr, fprintf(stderr,
"PQgetvalue: ERROR! field %d(of %d) of tuple %d(of %d) is not available", "PQgetvalue: ERROR! field %d(of %d) of row %d(of %d) "
"is not available",
field_num, res->numAttributes - 1, tup_num, res->ntups); field_num, res->numAttributes - 1, tup_num, res->ntups);
} }
...@@ -1514,7 +1556,8 @@ PQgetlength(PGresult *res, int tup_num, int field_num) ...@@ -1514,7 +1556,8 @@ PQgetlength(PGresult *res, int tup_num, int field_num)
if (tup_num > (res->ntups - 1 )|| if (tup_num > (res->ntups - 1 )||
field_num > (res->numAttributes - 1)) { field_num > (res->numAttributes - 1)) {
fprintf(stderr, fprintf(stderr,
"PQgetlength: ERROR! field %d(of %d) of tuple %d(of %d) is not available", "PQgetlength: ERROR! field %d(of %d) of row %d(of %d) "
"is not available",
field_num, res->numAttributes - 1, tup_num, res->ntups); field_num, res->numAttributes - 1, tup_num, res->ntups);
} }
...@@ -1538,7 +1581,8 @@ PQgetisnull(PGresult *res, int tup_num, int field_num) ...@@ -1538,7 +1581,8 @@ PQgetisnull(PGresult *res, int tup_num, int field_num)
if (tup_num > (res->ntups - 1 )|| if (tup_num > (res->ntups - 1 )||
field_num > (res->numAttributes - 1)) { field_num > (res->numAttributes - 1)) {
fprintf(stderr, fprintf(stderr,
"PQgetisnull: ERROR! field %d(of %d) of tuple %d(of %d) is not available", "PQgetisnull: ERROR! field %d(of %d) of row %d(of %d) "
"is not available",
field_num, res->numAttributes - 1, tup_num, res->ntups); field_num, res->numAttributes - 1, tup_num, res->ntups);
} }
...@@ -1547,5 +1591,3 @@ PQgetisnull(PGresult *res, int tup_num, int field_num) ...@@ -1547,5 +1591,3 @@ PQgetisnull(PGresult *res, int tup_num, int field_num)
else else
return 0; return 0;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment