📄 pqprint.c
字号:
ch == '-')) { fieldNotNum[j] = 1; break; } } /* * Above loop will believe E in first column is numeric; also, * we insist on a digit in the last column for a numeric. This * test is still not bulletproof but it handles most cases. */ if (*pval == 'E' || *pval == 'e' || !(ch >= '0' && ch <= '9')) fieldNotNum[j] = 1; } if (!po->expanded && (po->align || po->html3)) { if (plen > fieldMax[j]) fieldMax[j] = plen; if (!(fields[i * nFields + j] = (char *) malloc(plen + 1))) { perror("malloc"); exit(1); } charstring::copy(fields[i * nFields + j], pval); } 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", pval); else { if (po->align) fprintf(fout, "%-*s%s %s\n", fieldMaxLen - fs_len, fieldNames[j], po->fieldSep, pval); else fprintf(fout, "%s%s%s\n", fieldNames[j], po->fieldSep, pval); } } else { if (!po->html3) { fputs(pval, fout); efield: if ((j + 1) < nFields) fputs(po->fieldSep, fout); else fputc('\n', fout); } } } }}static char *do_header(FILE *fout, const PQprintOpt *po, const int nFields, int *fieldMax, const char **fieldNames, unsigned char *fieldNotNum, const int fs_len, const PGresult *res){ int j; /* for loop index */ char *border = NULL; if (po->html3) fputs("<tr>", fout); else { int tot = 0; int n = 0; char *p = NULL; for (; n < nFields; n++) tot += fieldMax[n] + fs_len + (po->standard ? 2 : 0); if (po->standard) tot += fs_len * 2 + 2; border = (char *)malloc(tot + 1); if (!border) { perror("malloc"); exit(1); } p = border; 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++) { const char *s = PQfname(res, j); if (po->html3) { fprintf(fout, "<th align=%s>%s</th>", fieldNotNum[j] ? "left" : "right", fieldNames[j]); } else { int n = charstring::length(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); return border;}static voidoutput_row(FILE *fout, const 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);}/* * really old printing routines */voidPQdisplayTuples(const PGresult *res, FILE *fp, /* where to send the output */ int fillAlign, /* pad the fields with spaces */ const char *fieldSep, /* field separator */ int printHeader, /* display headers? */ int quiet){#define DEFAULT_FIELD_SEP " " int i, j; int nFields; int nTuples; int *fLength = NULL; if (fieldSep == NULL) fieldSep = DEFAULT_FIELD_SEP; /* Get some useful info about the results */ nFields = PQnfields(res); nTuples = PQntuples(res); if (fp == NULL) fp = stdout; /* Figure the field lengths to align to */ /* will be somewhat time consuming for very large results */ if (fillAlign) { fLength = (int *) malloc(nFields * sizeof(int)); for (j = 0; j < nFields; j++) { fLength[j] = charstring::length(PQfname(res, j)); for (i = 0; i < nTuples; i++) { int flen = PQgetlength(res, i, j); if (flen > fLength[j]) fLength[j] = flen; } } } if (printHeader) { /* first, print out the attribute names */ for (i = 0; i < nFields; i++) { fputs(PQfname(res, i), fp); if (fillAlign) fill(charstring::length(PQfname(res, i)), fLength[i], ' ', fp); fputs(fieldSep, fp); } fprintf(fp, "\n"); /* Underline the attribute names */ for (i = 0; i < nFields; i++) { if (fillAlign) fill(0, fLength[i], '-', fp); fputs(fieldSep, fp); } fprintf(fp, "\n"); } /* next, print out the instances */ for (i = 0; i < nTuples; i++) { for (j = 0; j < nFields; j++) { fprintf(fp, "%s", PQgetvalue(res, i, j)); if (fillAlign) fill(charstring::length(PQgetvalue(res, i, j)), fLength[j], ' ', fp); fputs(fieldSep, fp); } fprintf(fp, "\n"); } if (!quiet) fprintf(fp, "\nQuery returned %d row%s.\n", PQntuples(res), (PQntuples(res) == 1) ? "" : "s"); fflush(fp); if (fLength) free(fLength);}voidPQprintTuples(const PGresult *res, FILE *fout, /* output stream */ int PrintAttNames, /* print attribute names or not */ int TerseOutput, /* delimiter bars or not? */ int colWidth /* width of column, if 0, use variable * width */){ int nFields; int nTups; int i, j; char formatString[80]; char *tborder = NULL; nFields = PQnfields(res); nTups = PQntuples(res); if (colWidth > 0) sprintf(formatString, "%%s %%-%ds", colWidth); else sprintf(formatString, "%%s %%s"); if (nFields > 0) { /* only print rows with at least 1 field. */ if (!TerseOutput) { int width; width = nFields * 14; tborder = (char *)malloc(width + 1); for (i = 0; i <= width; i++) tborder[i] = '-'; tborder[i] = '\0'; fprintf(fout, "%s\n", tborder); } for (i = 0; i < nFields; i++) { if (PrintAttNames) { fprintf(fout, formatString, TerseOutput ? "" : "|", PQfname(res, i)); } } if (PrintAttNames) { if (TerseOutput) fprintf(fout, "\n"); else fprintf(fout, "|\n%s\n", tborder); } for (i = 0; i < nTups; i++) { for (j = 0; j < nFields; j++) { const char *pval = PQgetvalue(res, i, j); fprintf(fout, formatString, TerseOutput ? "" : "|", pval ? pval : ""); } if (TerseOutput) fprintf(fout, "\n"); else fprintf(fout, "|\n%s\n", tborder); } }}/* simply send out max-length number of filler characters to fp */static voidfill(int length, int max, char filler, FILE *fp){ int count; count = max - length; while (count-- >= 0) putc(filler, fp);}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -