PostgreSQL Source Code git master
Loading...
Searching...
No Matches
crosstabview.h File Reference
#include "libpq-fe.h"
Include dependency graph for crosstabview.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define CROSSTABVIEW_MAX_COLUMNS   1600
 

Functions

bool PrintResultInCrosstab (const PGresult *res)
 

Macro Definition Documentation

◆ CROSSTABVIEW_MAX_COLUMNS

#define CROSSTABVIEW_MAX_COLUMNS   1600

Definition at line 24 of file crosstabview.h.

Function Documentation

◆ PrintResultInCrosstab()

bool PrintResultInCrosstab ( const PGresult res)
extern

Definition at line 104 of file crosstabview.c.

105{
106 bool retval = false;
111 int num_columns = 0;
112 int num_rows = 0;
113 int field_for_rows;
115 int field_for_data;
117 int rn;
118
121
123 {
124 pg_log_error("\\crosstabview: statement did not return a result set");
125 goto error_return;
126 }
127
128 if (PQnfields(res) < 3)
129 {
130 pg_log_error("\\crosstabview: query must return at least three columns");
131 goto error_return;
132 }
133
134 /* Process first optional arg (vertical header column) */
135 if (pset.ctv_args[0] == NULL)
136 field_for_rows = 0;
137 else
138 {
140 if (field_for_rows < 0)
141 goto error_return;
142 }
143
144 /* Process second optional arg (horizontal header column) */
145 if (pset.ctv_args[1] == NULL)
147 else
148 {
150 if (field_for_columns < 0)
151 goto error_return;
152 }
153
154 /* Insist that header columns be distinct */
156 {
157 pg_log_error("\\crosstabview: vertical and horizontal headers must be different columns");
158 goto error_return;
159 }
160
161 /* Process third optional arg (data column) */
162 if (pset.ctv_args[2] == NULL)
163 {
164 int i;
165
166 /*
167 * If the data column was not specified, we search for the one not
168 * used as either vertical or horizontal headers. Must be exactly
169 * three columns, or this won't be unique.
170 */
171 if (PQnfields(res) != 3)
172 {
173 pg_log_error("\\crosstabview: data column must be specified when query returns more than three columns");
174 goto error_return;
175 }
176
177 field_for_data = -1;
178 for (i = 0; i < PQnfields(res); i++)
179 {
180 if (i != field_for_rows && i != field_for_columns)
181 {
183 break;
184 }
185 }
187 }
188 else
189 {
191 if (field_for_data < 0)
192 goto error_return;
193 }
194
195 /* Process fourth optional arg (horizontal header sort column) */
196 if (pset.ctv_args[3] == NULL)
197 sort_field_for_columns = -1; /* no sort column */
198 else
199 {
202 goto error_return;
203 }
204
205 /*
206 * First part: accumulate the names that go into the vertical and
207 * horizontal headers, each into an AVL binary tree to build the set of
208 * DISTINCT values.
209 */
210
211 for (rn = 0; rn < PQntuples(res); rn++)
212 {
213 char *val;
214 char *val1;
215
216 /* horizontal */
219 val1 = NULL;
220
221 if (sort_field_for_columns >= 0 &&
224
226
228 {
229 pg_log_error("\\crosstabview: maximum number of columns (%d) exceeded",
231 goto error_return;
232 }
233
234 /* vertical */
237
239 }
240
241 /*
242 * Second part: Generate sorted arrays from the AVL trees.
243 */
244
245 num_columns = piv_columns.count;
246 num_rows = piv_rows.count;
247
250
252 pg_malloc(sizeof(pivot_field) * num_rows);
253
256
257 /*
258 * Third part: optionally, process the ranking data for the horizontal
259 * header
260 */
261 if (sort_field_for_columns >= 0)
263
264 /*
265 * Fourth part: print the crosstab'ed result.
266 */
267 retval = printCrosstab(res,
271
274 avlFree(&piv_rows, piv_rows.root);
277
278 return retval;
279}
#define Assert(condition)
Definition c.h:873
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
void * pg_malloc(size_t size)
Definition fe_memutils.c:47
void pg_free(void *ptr)
long val
Definition informix.c:689
int i
Definition isn.c:77
#define PQgetvalue
#define PQnfields
#define PQresultStatus
#define PQgetisnull
#define PQntuples
@ PGRES_TUPLES_OK
Definition libpq-fe.h:128
#define pg_log_error(...)
Definition logging.h:106
static int fb(int x)
PsqlSettings pset
Definition startup.c:32
char * ctv_args[4]
Definition settings.h:133

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

Referenced by PrintQueryResult().