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 107 of file crosstabview.c.

108{
109 bool retval = false;
114 int num_columns = 0;
115 int num_rows = 0;
116 int field_for_rows;
118 int field_for_data;
120 int rn;
121
124
126 {
127 pg_log_error("\\crosstabview: statement did not return a result set");
128 goto error_return;
129 }
130
131 if (PQnfields(res) < 3)
132 {
133 pg_log_error("\\crosstabview: query must return at least three columns");
134 goto error_return;
135 }
136
137 /* Process first optional arg (vertical header column) */
138 if (pset.ctv_args[0] == NULL)
139 field_for_rows = 0;
140 else
141 {
143 if (field_for_rows < 0)
144 goto error_return;
145 }
146
147 /* Process second optional arg (horizontal header column) */
148 if (pset.ctv_args[1] == NULL)
150 else
151 {
153 if (field_for_columns < 0)
154 goto error_return;
155 }
156
157 /* Insist that header columns be distinct */
159 {
160 pg_log_error("\\crosstabview: vertical and horizontal headers must be different columns");
161 goto error_return;
162 }
163
164 /* Process third optional arg (data column) */
165 if (pset.ctv_args[2] == NULL)
166 {
167 int i;
168
169 /*
170 * If the data column was not specified, we search for the one not
171 * used as either vertical or horizontal headers. Must be exactly
172 * three columns, or this won't be unique.
173 */
174 if (PQnfields(res) != 3)
175 {
176 pg_log_error("\\crosstabview: data column must be specified when query returns more than three columns");
177 goto error_return;
178 }
179
180 field_for_data = -1;
181 for (i = 0; i < PQnfields(res); i++)
182 {
183 if (i != field_for_rows && i != field_for_columns)
184 {
186 break;
187 }
188 }
190 }
191 else
192 {
194 if (field_for_data < 0)
195 goto error_return;
196 }
197
198 /* Process fourth optional arg (horizontal header sort column) */
199 if (pset.ctv_args[3] == NULL)
200 sort_field_for_columns = -1; /* no sort column */
201 else
202 {
205 goto error_return;
206 }
207
208 /*
209 * First part: accumulate the names that go into the vertical and
210 * horizontal headers, each into an AVL binary tree to build the set of
211 * DISTINCT values.
212 */
213
214 for (rn = 0; rn < PQntuples(res); rn++)
215 {
216 char *val;
217 char *val1;
218
219 /* horizontal */
222 val1 = NULL;
223
224 if (sort_field_for_columns >= 0 &&
227
229
231 {
232 pg_log_error("\\crosstabview: maximum number of columns (%d) exceeded",
234 goto error_return;
235 }
236
237 /* vertical */
240
242 }
243
244 /*
245 * Second part: Generate sorted arrays from the AVL trees.
246 */
247
248 num_columns = piv_columns.count;
249 num_rows = piv_rows.count;
250
252
254
257
258 /*
259 * Third part: optionally, process the ranking data for the horizontal
260 * header
261 */
262 if (sort_field_for_columns >= 0)
264
265 /*
266 * Fourth part: print the crosstab'ed result.
267 */
268 retval = printCrosstab(res,
272
275 avlFree(&piv_rows, piv_rows.root);
278
279 return retval;
280}
#define Assert(condition)
Definition c.h:999
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_free(void *ptr)
#define pg_malloc_array(type, count)
Definition fe_memutils.h:66
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:134
#define pg_log_error(...)
Definition logging.h:108
static int fb(int x)
PsqlSettings pset
Definition startup.c:33
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_array, PGRES_TUPLES_OK, PQgetisnull, PQgetvalue, PQnfields, PQntuples, PQresultStatus, printCrosstab(), pset, rankSort(), and val.

Referenced by PrintQueryResult().