PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
crosstabview.c File Reference
#include "postgres_fe.h"
#include <string.h>
#include "common.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
#include "psqlscanslash.h"
#include "settings.h"
Include dependency graph for crosstabview.c:

Go to the source code of this file.

Data Structures

struct  _pivot_field
 
struct  _avl_node
 
struct  _avl_tree
 

Typedefs

typedef struct _pivot_field pivot_field
 
typedef struct _avl_node avl_node
 
typedef struct _avl_tree avl_tree
 

Functions

static bool printCrosstab (const PGresult *results, int num_columns, pivot_field *piv_columns, int field_for_columns, int num_rows, pivot_field *piv_rows, int field_for_rows, int field_for_data)
 
static void avlInit (avl_tree *tree)
 
static void avlMergeValue (avl_tree *tree, char *name, char *sort_value)
 
static int avlCollectFields (avl_tree *tree, avl_node *node, pivot_field *fields, int idx)
 
static void avlFree (avl_tree *tree, avl_node *node)
 
static void rankSort (int num_columns, pivot_field *piv_columns)
 
static int indexOfColumn (char *arg, const PGresult *res)
 
static int pivotFieldCompare (const void *a, const void *b)
 
static int rankCompare (const void *a, const void *b)
 
bool PrintResultsInCrosstab (const PGresult *res)
 
static void avlUpdateHeight (avl_node *n)
 
static avl_nodeavlRotate (avl_node **current, int dir)
 
static int avlBalance (avl_node *n)
 
static void avlAdjustBalance (avl_tree *tree, avl_node **node)
 
static void avlInsertNode (avl_tree *tree, avl_node **node, pivot_field field)
 

Typedef Documentation

Function Documentation

static void avlAdjustBalance ( avl_tree tree,
avl_node **  node 
)
static

Definition at line 507 of file crosstabview.c.

References avlBalance(), avlRotate(), avlUpdateHeight(), _avl_node::children, and _avl_tree::end.

Referenced by avlInsertNode().

508 {
509  avl_node *current = *node;
510  int b = avlBalance(current) / 2;
511 
512  if (b != 0)
513  {
514  int dir = (1 - b) / 2;
515 
516  if (avlBalance(current->children[dir]) == -b)
517  avlRotate(&current->children[dir], !dir);
518  current = avlRotate(node, dir);
519  }
520  if (current != tree->end)
521  avlUpdateHeight(current);
522 }
static avl_node * avlRotate(avl_node **current, int dir)
Definition: crosstabview.c:482
static void avlUpdateHeight(avl_node *n)
Definition: crosstabview.c:473
avl_node * end
Definition: crosstabview.c:78
struct _avl_node * children[2]
Definition: crosstabview.c:67
static int avlBalance(avl_node *n)
Definition: crosstabview.c:496
static int avlBalance ( avl_node n)
static

Definition at line 496 of file crosstabview.c.

References _avl_node::children, and _avl_node::height.

Referenced by avlAdjustBalance().

497 {
498  return n->children[0]->height - n->children[1]->height;
499 }
struct _avl_node * children[2]
Definition: crosstabview.c:67
static int avlCollectFields ( avl_tree tree,
avl_node node,
pivot_field fields,
int  idx 
)
static

Definition at line 578 of file crosstabview.c.

References _avl_node::children, _avl_tree::end, _avl_node::field, and idx().

Referenced by PrintResultsInCrosstab().

579 {
580  if (node == tree->end)
581  return idx;
582 
583  idx = avlCollectFields(tree, node->children[0], fields, idx);
584  fields[idx] = node->field;
585  return avlCollectFields(tree, node->children[1], fields, idx + 1);
586 }
avl_node * end
Definition: crosstabview.c:78
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:264
struct _avl_node * children[2]
Definition: crosstabview.c:67
static int avlCollectFields(avl_tree *tree, avl_node *node, pivot_field *fields, int idx)
Definition: crosstabview.c:578
pivot_field field
Definition: crosstabview.c:54
static void avlFree ( avl_tree tree,
avl_node node 
)
static

Definition at line 449 of file crosstabview.c.

References _avl_node::children, _avl_tree::end, pg_free(), and _avl_tree::root.

Referenced by PrintResultsInCrosstab().

450 {
451  if (node->children[0] != tree->end)
452  {
453  avlFree(tree, node->children[0]);
454  pg_free(node->children[0]);
455  }
456  if (node->children[1] != tree->end)
457  {
458  avlFree(tree, node->children[1]);
459  pg_free(node->children[1]);
460  }
461  if (node == tree->root)
462  {
463  /* free the root separately as it's not child of anything */
464  if (node != tree->end)
465  pg_free(node);
466  /* free the tree->end struct only once and when all else is freed */
467  pg_free(tree->end);
468  }
469 }
static void avlFree(avl_tree *tree, avl_node *node)
Definition: crosstabview.c:449
avl_node * end
Definition: crosstabview.c:78
struct _avl_node * children[2]
Definition: crosstabview.c:67
avl_node * root
Definition: crosstabview.c:77
void pg_free(void *ptr)
Definition: fe_memutils.c:105
static void avlInit ( avl_tree tree)
static

Definition at line 439 of file crosstabview.c.

References _avl_node::children, _avl_tree::count, _avl_tree::end, pg_malloc0(), and _avl_tree::root.

Referenced by PrintResultsInCrosstab().

440 {
441  tree->end = (avl_node *) pg_malloc0(sizeof(avl_node));
442  tree->end->children[0] = tree->end->children[1] = tree->end;
443  tree->count = 0;
444  tree->root = tree->end;
445 }
avl_node * end
Definition: crosstabview.c:78
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
struct _avl_node * children[2]
Definition: crosstabview.c:67
avl_node * root
Definition: crosstabview.c:77
static void avlInsertNode ( avl_tree tree,
avl_node **  node,
pivot_field  field 
)
static

Definition at line 530 of file crosstabview.c.

References avlAdjustBalance(), _avl_node::children, cmp(), _avl_tree::count, _avl_tree::end, _avl_node::field, _avl_node::height, pg_malloc(), and pivotFieldCompare().

Referenced by avlMergeValue().

531 {
532  avl_node *current = *node;
533 
534  if (current == tree->end)
535  {
536  avl_node *new_node = (avl_node *)
537  pg_malloc(sizeof(avl_node));
538 
539  new_node->height = 1;
540  new_node->field = field;
541  new_node->children[0] = new_node->children[1] = tree->end;
542  tree->count++;
543  *node = new_node;
544  }
545  else
546  {
547  int cmp = pivotFieldCompare(&field, &current->field);
548 
549  if (cmp != 0)
550  {
551  avlInsertNode(tree,
552  cmp > 0 ? &current->children[1] : &current->children[0],
553  field);
554  avlAdjustBalance(tree, node);
555  }
556  }
557 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
avl_node * end
Definition: crosstabview.c:78
struct _avl_node * children[2]
Definition: crosstabview.c:67
static void avlInsertNode(avl_tree *tree, avl_node **node, pivot_field field)
Definition: crosstabview.c:530
pivot_field field
Definition: crosstabview.c:54
static int pivotFieldCompare(const void *a, const void *b)
Definition: crosstabview.c:696
static void avlAdjustBalance(avl_tree *tree, avl_node **node)
Definition: crosstabview.c:507
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742
static void avlMergeValue ( avl_tree tree,
char *  name,
char *  sort_value 
)
static

Definition at line 561 of file crosstabview.c.

References avlInsertNode(), _avl_tree::count, _pivot_field::name, name, _pivot_field::rank, _avl_tree::root, and _pivot_field::sort_value.

Referenced by PrintResultsInCrosstab().

562 {
563  pivot_field field;
564 
565  field.name = name;
566  field.rank = tree->count;
567  field.sort_value = sort_value;
568  avlInsertNode(tree, &tree->root, field);
569 }
char * sort_value
Definition: crosstabview.c:38
static void avlInsertNode(avl_tree *tree, avl_node **node, pivot_field field)
Definition: crosstabview.c:530
avl_node * root
Definition: crosstabview.c:77
const char * name
Definition: encode.c:521
static avl_node* avlRotate ( avl_node **  current,
int  dir 
)
static

Definition at line 482 of file crosstabview.c.

References avlUpdateHeight(), before(), and _avl_node::children.

Referenced by avlAdjustBalance().

483 {
484  avl_node *before = *current;
485  avl_node *after = (*current)->children[dir];
486 
487  *current = after;
488  before->children[dir] = after->children[!dir];
489  avlUpdateHeight(before);
490  after->children[!dir] = before;
491 
492  return after;
493 }
static void avlUpdateHeight(avl_node *n)
Definition: crosstabview.c:473
static int before(chr x, chr y)
Definition: regc_locale.c:496
struct _avl_node * children[2]
Definition: crosstabview.c:67
static void avlUpdateHeight ( avl_node n)
static

Definition at line 473 of file crosstabview.c.

References _avl_node::children, and _avl_node::height.

Referenced by avlAdjustBalance(), and avlRotate().

474 {
475  n->height = 1 + (n->children[0]->height > n->children[1]->height ?
476  n->children[0]->height :
477  n->children[1]->height);
478 }
struct _avl_node * children[2]
Definition: crosstabview.c:67
static int indexOfColumn ( char *  arg,
const PGresult res 
)
static

Definition at line 637 of file crosstabview.c.

References dequote_downcase_identifier(), _psqlSettings::encoding, i, idx(), PQfname(), PQnfields(), pset, and psql_error().

Referenced by PrintResultsInCrosstab().

638 {
639  int idx;
640 
641  if (arg[0] && strspn(arg, "0123456789") == strlen(arg))
642  {
643  /* if arg contains only digits, it's a column number */
644  idx = atoi(arg) - 1;
645  if (idx < 0 || idx >= PQnfields(res))
646  {
647  psql_error("\\crosstabview: column number %d is out of range 1..%d\n",
648  idx + 1, PQnfields(res));
649  return -1;
650  }
651  }
652  else
653  {
654  int i;
655 
656  /*
657  * Dequote and downcase the column name. By checking for all-digits
658  * before doing this, we can ensure that a quoted name is treated as a
659  * name even if it's all digits.
660  */
662 
663  /* Now look for match(es) among res' column names */
664  idx = -1;
665  for (i = 0; i < PQnfields(res); i++)
666  {
667  if (strcmp(arg, PQfname(res, i)) == 0)
668  {
669  if (idx >= 0)
670  {
671  /* another idx was already found for the same name */
672  psql_error("\\crosstabview: ambiguous column name: \"%s\"\n", arg);
673  return -1;
674  }
675  idx = i;
676  }
677  }
678  if (idx == -1)
679  {
680  psql_error("\\crosstabview: column name not found: \"%s\"\n", arg);
681  return -1;
682  }
683  }
684 
685  return idx;
686 }
int PQnfields(const PGresult *res)
Definition: fe-exec.c:2681
PsqlSettings pset
Definition: startup.c:37
void dequote_downcase_identifier(char *str, bool downcase, int encoding)
char * PQfname(const PGresult *res, int field_num)
Definition: fe-exec.c:2759
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:264
void psql_error(const char *fmt,...)
Definition: common.c:177
int i
void * arg
int encoding
Definition: settings.h:83
static int pivotFieldCompare ( const void *  a,
const void *  b 
)
static

Definition at line 696 of file crosstabview.c.

References _pivot_field::name.

Referenced by avlInsertNode(), and printCrosstab().

697 {
698  const pivot_field *pa = (const pivot_field *) a;
699  const pivot_field *pb = (const pivot_field *) b;
700 
701  /* test null values */
702  if (!pb->name)
703  return pa->name ? -1 : 0;
704  else if (!pa->name)
705  return 1;
706 
707  /* non-null values */
708  return strcmp(pa->name, pb->name);
709 }
static bool printCrosstab ( const PGresult results,
int  num_columns,
pivot_field piv_columns,
int  field_for_columns,
int  num_rows,
pivot_field piv_rows,
int  field_for_rows,
int  field_for_data 
)
static

Definition at line 287 of file crosstabview.c.

References Assert, printTableContent::cells, printTableContent::cellsadded, column_type_alignment(), error(), i, idx(), _psqlSettings::logfile, _pivot_field::name, name, NULL, printQueryOpt::nullPrint, pg_free(), pg_malloc(), pivotFieldCompare(), _psqlSettings::popt, PQfname(), PQftype(), PQgetisnull(), PQgetvalue(), PQntuples(), printTable(), printTableAddHeader(), printTableCleanup(), printTableInit(), pset, psql_error(), _psqlSettings::queryFout, _pivot_field::rank, printQueryOpt::title, and printQueryOpt::topt.

Referenced by PrintResultsInCrosstab().

291 {
292  printQueryOpt popt = pset.popt;
293  printTableContent cont;
294  int i,
295  rn;
296  char col_align;
297  int *horiz_map;
298  bool retval = false;
299 
300  printTableInit(&cont, &popt.topt, popt.title, num_columns + 1, num_rows);
301 
302  /* Step 1: set target column names (horizontal header) */
303 
304  /* The name of the first column is kept unchanged by the pivoting */
305  printTableAddHeader(&cont,
306  PQfname(results, field_for_rows),
307  false,
309  field_for_rows)));
310 
311  /*
312  * To iterate over piv_columns[] by piv_columns[].rank, create a reverse
313  * map associating each piv_columns[].rank to its index in piv_columns.
314  * This avoids an O(N^2) loop later.
315  */
316  horiz_map = (int *) pg_malloc(sizeof(int) * num_columns);
317  for (i = 0; i < num_columns; i++)
318  horiz_map[piv_columns[i].rank] = i;
319 
320  /*
321  * The display alignment depends on its PQftype().
322  */
323  col_align = column_type_alignment(PQftype(results, field_for_data));
324 
325  for (i = 0; i < num_columns; i++)
326  {
327  char *colname;
328 
329  colname = piv_columns[horiz_map[i]].name ?
330  piv_columns[horiz_map[i]].name :
331  (popt.nullPrint ? popt.nullPrint : "");
332 
333  printTableAddHeader(&cont, colname, false, col_align);
334  }
335  pg_free(horiz_map);
336 
337  /* Step 2: set row names in the first output column (vertical header) */
338  for (i = 0; i < num_rows; i++)
339  {
340  int k = piv_rows[i].rank;
341 
342  cont.cells[k * (num_columns + 1)] = piv_rows[i].name ?
343  piv_rows[i].name :
344  (popt.nullPrint ? popt.nullPrint : "");
345  }
346  cont.cellsadded = num_rows * (num_columns + 1);
347 
348  /*
349  * Step 3: fill in the content cells.
350  */
351  for (rn = 0; rn < PQntuples(results); rn++)
352  {
353  int row_number;
354  int col_number;
355  pivot_field *rp,
356  *cp;
357  pivot_field elt;
358 
359  /* Find target row */
360  if (!PQgetisnull(results, rn, field_for_rows))
361  elt.name = PQgetvalue(results, rn, field_for_rows);
362  else
363  elt.name = NULL;
364  rp = (pivot_field *) bsearch(&elt,
365  piv_rows,
366  num_rows,
367  sizeof(pivot_field),
369  Assert(rp != NULL);
370  row_number = rp->rank;
371 
372  /* Find target column */
373  if (!PQgetisnull(results, rn, field_for_columns))
374  elt.name = PQgetvalue(results, rn, field_for_columns);
375  else
376  elt.name = NULL;
377 
378  cp = (pivot_field *) bsearch(&elt,
379  piv_columns,
380  num_columns,
381  sizeof(pivot_field),
383  Assert(cp != NULL);
384  col_number = cp->rank;
385 
386  /* Place value into cell */
387  if (col_number >= 0 && row_number >= 0)
388  {
389  int idx;
390 
391  /* index into the cont.cells array */
392  idx = 1 + col_number + row_number * (num_columns + 1);
393 
394  /*
395  * If the cell already contains a value, raise an error.
396  */
397  if (cont.cells[idx] != NULL)
398  {
399  psql_error("\\crosstabview: query result contains multiple data values for row \"%s\", column \"%s\"\n",
400  rp->name ? rp->name :
401  (popt.nullPrint ? popt.nullPrint : "(null)"),
402  cp->name ? cp->name :
403  (popt.nullPrint ? popt.nullPrint : "(null)"));
404  goto error;
405  }
406 
407  cont.cells[idx] = !PQgetisnull(results, rn, field_for_data) ?
408  PQgetvalue(results, rn, field_for_data) :
409  (popt.nullPrint ? popt.nullPrint : "");
410  }
411  }
412 
413  /*
414  * The non-initialized cells must be set to an empty string for the print
415  * functions
416  */
417  for (i = 0; i < cont.cellsadded; i++)
418  {
419  if (cont.cells[i] == NULL)
420  cont.cells[i] = "";
421  }
422 
423  printTable(&cont, pset.queryFout, false, pset.logfile);
424  retval = true;
425 
426 error:
427  printTableCleanup(&cont);
428 
429  return retval;
430 }
char * nullPrint
Definition: print.h:166
PsqlSettings pset
Definition: startup.c:37
void printTableCleanup(printTableContent *const content)
Definition: print.c:3100
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3067
static void error(void)
Definition: sql-dyntest.c:147
char * PQfname(const PGresult *res, int field_num)
Definition: fe-exec.c:2759
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
printTableOpt topt
Definition: print.h:165
FILE * queryFout
Definition: settings.h:84
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:264
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2673
void printTableAddHeader(printTableContent *const content, char *header, const bool translate, const char align)
Definition: print.c:2969
Oid PQftype(const PGresult *res, int field_num)
Definition: fe-exec.c:2911
const char ** cells
Definition: print.h:151
long cellsadded
Definition: print.h:154
void printTable(const printTableContent *cont, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3189
char column_type_alignment(Oid ftype)
Definition: print.c:3351
void psql_error(const char *fmt,...)
Definition: common.c:177
FILE * logfile
Definition: settings.h:113
char * title
Definition: print.h:167
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:670
static int pivotFieldCompare(const void *a, const void *b)
Definition: crosstabview.c:696
printQueryOpt popt
Definition: settings.h:91
void printTableInit(printTableContent *const content, const printTableOpt *opt, const char *title, const int ncolumns, const int nrows)
Definition: print.c:2932
void pg_free(void *ptr)
Definition: fe_memutils.c:105
const char * name
Definition: encode.c:521
int i
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3092
bool PrintResultsInCrosstab ( const PGresult res)

Definition at line 105 of file crosstabview.c.

References Assert, avlCollectFields(), avlFree(), avlInit(), avlMergeValue(), _avl_tree::count, CROSSTABVIEW_MAX_COLUMNS, _psqlSettings::ctv_args, i, indexOfColumn(), NULL, pg_free(), pg_malloc(), PGRES_TUPLES_OK, PQgetisnull(), PQgetvalue(), PQnfields(), PQntuples(), PQresultStatus(), printCrosstab(), pset, psql_error(), rankSort(), _avl_tree::root, and val.

Referenced by PrintQueryResults().

106 {
107  bool retval = false;
108  avl_tree piv_columns;
109  avl_tree piv_rows;
110  pivot_field *array_columns = NULL;
111  pivot_field *array_rows = NULL;
112  int num_columns = 0;
113  int num_rows = 0;
114  int field_for_rows;
115  int field_for_columns;
116  int field_for_data;
117  int sort_field_for_columns;
118  int rn;
119 
120  avlInit(&piv_rows);
121  avlInit(&piv_columns);
122 
123  if (PQresultStatus(res) != PGRES_TUPLES_OK)
124  {
125  psql_error("\\crosstabview: statement did not return a result set\n");
126  goto error_return;
127  }
128 
129  if (PQnfields(res) < 3)
130  {
131  psql_error("\\crosstabview: query must return at least three columns\n");
132  goto error_return;
133  }
134 
135  /* Process first optional arg (vertical header column) */
136  if (pset.ctv_args[0] == NULL)
137  field_for_rows = 0;
138  else
139  {
140  field_for_rows = indexOfColumn(pset.ctv_args[0], res);
141  if (field_for_rows < 0)
142  goto error_return;
143  }
144 
145  /* Process second optional arg (horizontal header column) */
146  if (pset.ctv_args[1] == NULL)
147  field_for_columns = 1;
148  else
149  {
150  field_for_columns = indexOfColumn(pset.ctv_args[1], res);
151  if (field_for_columns < 0)
152  goto error_return;
153  }
154 
155  /* Insist that header columns be distinct */
156  if (field_for_columns == field_for_rows)
157  {
158  psql_error("\\crosstabview: vertical and horizontal headers must be different columns\n");
159  goto error_return;
160  }
161 
162  /* Process third optional arg (data column) */
163  if (pset.ctv_args[2] == NULL)
164  {
165  int i;
166 
167  /*
168  * If the data column was not specified, we search for the one not
169  * used as either vertical or horizontal headers. Must be exactly
170  * three columns, or this won't be unique.
171  */
172  if (PQnfields(res) != 3)
173  {
174  psql_error("\\crosstabview: data column must be specified when query returns more than three columns\n");
175  goto error_return;
176  }
177 
178  field_for_data = -1;
179  for (i = 0; i < PQnfields(res); i++)
180  {
181  if (i != field_for_rows && i != field_for_columns)
182  {
183  field_for_data = i;
184  break;
185  }
186  }
187  Assert(field_for_data >= 0);
188  }
189  else
190  {
191  field_for_data = indexOfColumn(pset.ctv_args[2], res);
192  if (field_for_data < 0)
193  goto error_return;
194  }
195 
196  /* Process fourth optional arg (horizontal header sort column) */
197  if (pset.ctv_args[3] == NULL)
198  sort_field_for_columns = -1; /* no sort column */
199  else
200  {
201  sort_field_for_columns = indexOfColumn(pset.ctv_args[3], res);
202  if (sort_field_for_columns < 0)
203  goto error_return;
204  }
205 
206  /*
207  * First part: accumulate the names that go into the vertical and
208  * horizontal headers, each into an AVL binary tree to build the set of
209  * DISTINCT values.
210  */
211 
212  for (rn = 0; rn < PQntuples(res); rn++)
213  {
214  char *val;
215  char *val1;
216 
217  /* horizontal */
218  val = PQgetisnull(res, rn, field_for_columns) ? NULL :
219  PQgetvalue(res, rn, field_for_columns);
220  val1 = NULL;
221 
222  if (sort_field_for_columns >= 0 &&
223  !PQgetisnull(res, rn, sort_field_for_columns))
224  val1 = PQgetvalue(res, rn, sort_field_for_columns);
225 
226  avlMergeValue(&piv_columns, val, val1);
227 
228  if (piv_columns.count > CROSSTABVIEW_MAX_COLUMNS)
229  {
230  psql_error("\\crosstabview: maximum number of columns (%d) exceeded\n",
232  goto error_return;
233  }
234 
235  /* vertical */
236  val = PQgetisnull(res, rn, field_for_rows) ? NULL :
237  PQgetvalue(res, rn, field_for_rows);
238 
239  avlMergeValue(&piv_rows, val, NULL);
240  }
241 
242  /*
243  * Second part: Generate sorted arrays from the AVL trees.
244  */
245 
246  num_columns = piv_columns.count;
247  num_rows = piv_rows.count;
248 
249  array_columns = (pivot_field *)
250  pg_malloc(sizeof(pivot_field) * num_columns);
251 
252  array_rows = (pivot_field *)
253  pg_malloc(sizeof(pivot_field) * num_rows);
254 
255  avlCollectFields(&piv_columns, piv_columns.root, array_columns, 0);
256  avlCollectFields(&piv_rows, piv_rows.root, array_rows, 0);
257 
258  /*
259  * Third part: optionally, process the ranking data for the horizontal
260  * header
261  */
262  if (sort_field_for_columns >= 0)
263  rankSort(num_columns, array_columns);
264 
265  /*
266  * Fourth part: print the crosstab'ed results.
267  */
268  retval = printCrosstab(res,
269  num_columns, array_columns, field_for_columns,
270  num_rows, array_rows, field_for_rows,
271  field_for_data);
272 
273 error_return:
274  avlFree(&piv_columns, piv_columns.root);
275  avlFree(&piv_rows, piv_rows.root);
276  pg_free(array_columns);
277  pg_free(array_rows);
278 
279  return retval;
280 }
int PQnfields(const PGresult *res)
Definition: fe-exec.c:2681
static void avlFree(avl_tree *tree, avl_node *node)
Definition: crosstabview.c:449
PsqlSettings pset
Definition: startup.c:37
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3067
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2673
static void avlInit(avl_tree *tree)
Definition: crosstabview.c:439
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2596
char * ctv_args[4]
Definition: settings.h:97
static int indexOfColumn(char *arg, const PGresult *res)
Definition: crosstabview.c:637
#define CROSSTABVIEW_MAX_COLUMNS
Definition: crosstabview.h:22
static void rankSort(int num_columns, pivot_field *piv_columns)
Definition: crosstabview.c:589
static bool printCrosstab(const PGresult *results, int num_columns, pivot_field *piv_columns, int field_for_columns, int num_rows, pivot_field *piv_rows, int field_for_rows, int field_for_data)
Definition: crosstabview.c:287
void psql_error(const char *fmt,...)
Definition: common.c:177
avl_node * root
Definition: crosstabview.c:77
static int avlCollectFields(avl_tree *tree, avl_node *node, pivot_field *fields, int idx)
Definition: crosstabview.c:578
static void avlMergeValue(avl_tree *tree, char *name, char *sort_value)
Definition: crosstabview.c:561
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:670
void pg_free(void *ptr)
Definition: fe_memutils.c:105
int i
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3092
long val
Definition: informix.c:689
static int rankCompare ( const void *  a,
const void *  b 
)
static

Definition at line 712 of file crosstabview.c.

Referenced by rankSort().

713 {
714  return *((const int *) a) - *((const int *) b);
715 }
static void rankSort ( int  num_columns,
pivot_field piv_columns 
)
static

Definition at line 589 of file crosstabview.c.

References i, pg_free(), pg_malloc(), qsort, _pivot_field::rank, rankCompare(), _pivot_field::sort_value, and val.

Referenced by PrintResultsInCrosstab().

590 {
591  int *hmap; /* [[offset in piv_columns, rank], ...for
592  * every header entry] */
593  int i;
594 
595  hmap = (int *) pg_malloc(sizeof(int) * num_columns * 2);
596  for (i = 0; i < num_columns; i++)
597  {
598  char *val = piv_columns[i].sort_value;
599 
600  /* ranking information is valid if non null and matches /^-?\d+$/ */
601  if (val &&
602  ((*val == '-' &&
603  strspn(val + 1, "0123456789") == strlen(val + 1)) ||
604  strspn(val, "0123456789") == strlen(val)))
605  {
606  hmap[i * 2] = atoi(val);
607  hmap[i * 2 + 1] = i;
608  }
609  else
610  {
611  /* invalid rank information ignored (equivalent to rank 0) */
612  hmap[i * 2] = 0;
613  hmap[i * 2 + 1] = i;
614  }
615  }
616 
617  qsort(hmap, num_columns, sizeof(int) * 2, rankCompare);
618 
619  for (i = 0; i < num_columns; i++)
620  {
621  piv_columns[hmap[i * 2 + 1]].rank = i;
622  }
623 
624  pg_free(hmap);
625 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
char * sort_value
Definition: crosstabview.c:38
void pg_free(void *ptr)
Definition: fe_memutils.c:105
int i
#define qsort(a, b, c, d)
Definition: port.h:440
static int rankCompare(const void *a, const void *b)
Definition: crosstabview.c:712
long val
Definition: informix.c:689