114 int field_for_columns;
116 int sort_field_for_columns;
124 pg_log_error(
"\\crosstabview: statement did not return a result set");
130 pg_log_error(
"\\crosstabview: query must return at least three columns");
140 if (field_for_rows < 0)
146 field_for_columns = 1;
150 if (field_for_columns < 0)
155 if (field_for_columns == field_for_rows)
157 pg_log_error(
"\\crosstabview: vertical and horizontal headers must be different columns");
173 pg_log_error(
"\\crosstabview: data column must be specified when query returns more than three columns");
180 if (
i != field_for_rows &&
i != field_for_columns)
186 Assert(field_for_data >= 0);
191 if (field_for_data < 0)
197 sort_field_for_columns = -1;
201 if (sort_field_for_columns < 0)
221 if (sort_field_for_columns >= 0 &&
229 pg_log_error(
"\\crosstabview: maximum number of columns (%d) exceeded",
245 num_columns = piv_columns.
count;
246 num_rows = piv_rows.
count;
261 if (sort_field_for_columns >= 0)
262 rankSort(num_columns, array_columns);
268 num_columns, array_columns, field_for_columns,
269 num_rows, array_rows, field_for_rows,
#define Assert(condition)
static int avlCollectFields(avl_tree *tree, avl_node *node, pivot_field *fields, int idx)
static void avlFree(avl_tree *tree, avl_node *node)
static bool printCrosstab(const PGresult *result, 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 rankSort(int num_columns, pivot_field *piv_columns)
static int indexOfColumn(char *arg, const PGresult *res)
static void avlInit(avl_tree *tree)
static void avlMergeValue(avl_tree *tree, char *name, char *sort_value)
#define CROSSTABVIEW_MAX_COLUMNS
static void PGresult * res
ExecStatusType PQresultStatus(const PGresult *res)
int PQntuples(const PGresult *res)
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
int PQnfields(const PGresult *res)
void * pg_malloc(size_t size)
#define pg_log_error(...)