1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
|
#include <config.h>
#include <c.h>
#include "describe.h"
#include <string.h>
#include <postgres.h> /* for VARHDRSZ, int4 type */
#include <postgres_ext.h>
#include <libpq-fe.h>
#include "common.h"
#include "settings.h"
#include "print.h"
#include "variables.h"
/*----------------
* Handlers for various slash commands displaying some sort of list
* of things in the database.
*
* If you add something here, consider this:
* - If (and only if) the variable "description" is set, the description/
* comment for the object should be displayed.
* - Try to format the query to look nice in -E output.
*----------------
*/
/* the maximal size of regular expression we'll accept here */
/* (it is save to just change this here) */
#define REGEXP_CUTOFF 10 * NAMEDATALEN
/* \da
* takes an optional regexp to match specific aggregates by name
*/
bool
describeAggregates(const char *name, PsqlSettings *pset)
{
char descbuf[384 + 2 * REGEXP_CUTOFF]; /* observe/adjust this
* if you change the
* query */
PGresult *res;
bool description = GetVariableBool(pset->vars, "description");
printQueryOpt myopt = pset->popt;
descbuf[0] = '\0';
/*
* There are two kinds of aggregates: ones that work on particular
* types ones that work on all
*/
strcat(descbuf,
"SELECT a.aggname AS \"Name\", t.typname AS \"Type\"");
if (description)
strcat(descbuf,
",\n obj_description(a.oid) as \"Description\"");
strcat(descbuf,
"\nFROM pg_aggregate a, pg_type t\n"
"WHERE a.aggbasetype = t.oid\n");
if (name)
{
strcat(descbuf, " AND a.aggname ~* '^");
strncat(descbuf, name, REGEXP_CUTOFF);
strcat(descbuf, "'\n");
}
strcat(descbuf,
"UNION\n"
"SELECT a.aggname AS \"Name\", '(all types)' as \"Type\"");
if (description)
strcat(descbuf,
",\n obj_description(a.oid) as \"Description\"");
strcat(descbuf,
"\nFROM pg_aggregate a\n"
"WHERE a.aggbasetype = 0\n");
if (name)
{
strcat(descbuf, " AND a.aggname ~* '^");
strncat(descbuf, name, REGEXP_CUTOFF);
strcat(descbuf, "'\n");
}
strcat(descbuf, "ORDER BY \"Name\", \"Type\"");
res = PSQLexec(pset, descbuf);
if (!res)
return false;
myopt.topt.tuples_only = false;
myopt.nullPrint = NULL;
myopt.title = "List of aggregates";
printQuery(res, &myopt, pset->queryFout);
PQclear(res);
return true;
}
/* \df
* takes an optional regexp to narrow down the function name
*/
bool
describeFunctions(const char *name, PsqlSettings *pset)
{
char descbuf[384 + REGEXP_CUTOFF];
PGresult *res;
printQueryOpt myopt = pset->popt;
/*
* we skip in/out funcs by excluding functions that take some
* arguments, but have no types defined for those arguments
*/
descbuf[0] = '\0';
strcat(descbuf, "SELECT t.typname as \"Result\", p.proname as \"Function\",\n"
" oid8types(p.proargtypes) as \"Arguments\"");
if (GetVariableBool(pset->vars, "description"))
strcat(descbuf, "\n, obj_description(p.oid) as \"Description\"");
strcat(descbuf, "\nFROM pg_proc p, pg_type t\n"
"WHERE p.prorettype = t.oid and (pronargs = 0 or oid8types(p.proargtypes) != '')\n");
if (name)
{
strcat(descbuf, " AND p.proname ~* '^");
strncat(descbuf, name, REGEXP_CUTOFF);
strcat(descbuf, "'\n");
}
strcat(descbuf, "ORDER BY \"Function\", \"Result\", \"Arguments\"");
res = PSQLexec(pset, descbuf);
if (!res)
return false;
myopt.topt.tuples_only = false;
myopt.nullPrint = NULL;
myopt.title = "List of functions";
printQuery(res, &myopt, pset->queryFout);
PQclear(res);
return true;
}
/*
* describeTypes
*
* for \dT
*/
bool
describeTypes(const char *name, PsqlSettings *pset)
{
char descbuf[256 + REGEXP_CUTOFF];
PGresult *res;
printQueryOpt myopt = pset->popt;
descbuf[0] = '\0';
strcat(descbuf, "SELECT typname AS \"Type\"");
if (GetVariableBool(pset->vars, "description"))
strcat(descbuf, ", obj_description(p.oid) as \"Description\"");
strcat(descbuf, "\nFROM pg_type\n"
"WHERE typrelid = 0 AND typname !~ '^_.*'\n");
if (name)
{
strcat(descbuf, " AND typname ~* '^");
strncat(descbuf, name, REGEXP_CUTOFF);
strcat(descbuf, "' ");
}
strcat(descbuf, "ORDER BY typname;");
res = PSQLexec(pset, descbuf);
if (!res)
return false;
myopt.topt.tuples_only = false;
myopt.nullPrint = NULL;
myopt.title = "List of types";
printQuery(res, &myopt, pset->queryFout);
PQclear(res);
return true;
}
/* \do
* NOTE: The (optional) argument here is _not_ a regexp since with all the
* funny chars floating around that would probably confuse people. It's an
* exact match string.
*/
bool
describeOperators(const char *name, PsqlSettings *pset)
{
char descbuf[1536 + 3 * 32]; /* 32 is max length for operator
* name */
PGresult *res;
bool description = GetVariableBool(pset->vars, "description");
printQueryOpt myopt = pset->popt;
descbuf[0] = '\0';
strcat(descbuf, "SELECT o.oprname AS \"Op\",\n"
" t1.typname AS \"Left arg\",\n"
" t2.typname AS \"Right arg\",\n"
" t0.typname AS \"Result\"");
if (description)
strcat(descbuf, ",\n obj_description(p.oid) as \"Description\"");
strcat(descbuf, "\nFROM pg_proc p, pg_type t0,\n"
" pg_type t1, pg_type t2,\n"
" pg_operator o\n"
"WHERE p.prorettype = t0.oid AND\n"
" RegprocToOid(o.oprcode) = p.oid AND\n"
" p.pronargs = 2 AND\n"
" o.oprleft = t1.oid AND\n"
" o.oprright = t2.oid\n");
if (name)
{
strcat(descbuf, " AND o.oprname = '");
strncat(descbuf, name, 32);
strcat(descbuf, "'\n");
}
strcat(descbuf, "\nUNION\n\n"
"SELECT o.oprname as \"Op\",\n"
" ''::name AS \"Left arg\",\n"
" t1.typname AS \"Right arg\",\n"
" t0.typname AS \"Result\"");
if (description)
strcat(descbuf, ",\n obj_description(p.oid) as \"Description\"");
strcat(descbuf, "\nFROM pg_operator o, pg_proc p, pg_type t0, pg_type t1\n"
"WHERE RegprocToOid(o.oprcode) = p.oid AND\n"
" o.oprresult = t0.oid AND\n"
" o.oprkind = 'l' AND\n"
" o.oprright = t1.oid\n");
if (name)
{
strcat(descbuf, "AND o.oprname = '");
strncat(descbuf, name, 32);
strcat(descbuf, "'\n");
}
strcat(descbuf, "\nUNION\n\n"
"SELECT o.oprname as \"Op\",\n"
" t1.typname AS \"Left arg\",\n"
" ''::name AS \"Right arg\",\n"
" t0.typname AS \"Result\"");
if (description)
strcat(descbuf, ",\n obj_description(p.oid) as \"Description\"");
strcat(descbuf, "\nFROM pg_operator o, pg_proc p, pg_type t0, pg_type t1\n"
"WHERE RegprocToOid(o.oprcode) = p.oid AND\n"
" o.oprresult = t0.oid AND\n"
" o.oprkind = 'r' AND\n"
" o.oprleft = t1.oid\n");
if (name)
{
strcat(descbuf, "AND o.oprname = '");
strncat(descbuf, name, 32);
strcat(descbuf, "'\n");
}
strcat(descbuf, "\nORDER BY \"Op\", \"Left arg\", \"Right arg\", \"Result\"");
res = PSQLexec(pset, descbuf);
if (!res)
return false;
myopt.topt.tuples_only = false;
myopt.nullPrint = NULL;
myopt.title = "List of operators";
printQuery(res, &myopt, pset->queryFout);
PQclear(res);
return true;
}
/*
* listAllDbs
*
* for \l, \list, and -l switch
*/
bool
listAllDbs(PsqlSettings *pset)
{
PGresult *res;
char descbuf[256];
printQueryOpt myopt = pset->popt;
descbuf[0] = '\0';
strcat(descbuf, "SELECT pg_database.datname as \"Database\",\n"
" pg_user.usename as \"Owner\""
#ifdef MULTIBYTE
",\n pg_database.encoding as \"Encoding\""
#endif
);
if (GetVariableBool(pset->vars, "description"))
strcat(descbuf, ",\n obj_description(pg_database.oid) as \"Description\"\n");
strcat(descbuf, "FROM pg_database, pg_user\n"
"WHERE pg_database.datdba = pg_user.usesysid\n"
"ORDER BY \"Database\"");
res = PSQLexec(pset, descbuf);
if (!res)
return false;
myopt.topt.tuples_only = false;
myopt.nullPrint = NULL;
myopt.title = "List of databases";
printQuery(res, &myopt, pset->queryFout);
PQclear(res);
return true;
}
/* List Tables Grant/Revoke Permissions
* \z (now also \dp -- perhaps more mnemonic)
*
*/
bool
permissionsList(const char *name, PsqlSettings *pset)
{
char descbuf[256 + REGEXP_CUTOFF];
PGresult *res;
printQueryOpt myopt = pset->popt;
descbuf[0] = '\0';
/* Currently, we ignore indexes since they have no meaningful rights */
strcat(descbuf, "SELECT relname as \"Relation\",\n"
" relacl as \"Access permissions\"\n"
"FROM pg_class\n"
"WHERE ( relkind = 'r' OR relkind = 'S') AND\n"
" relname !~ '^pg_'\n");
if (name)
{
strcat(descbuf, " AND rename ~ '^");
strncat(descbuf, name, REGEXP_CUTOFF);
strcat(descbuf, "'\n");
}
strcat(descbuf, "ORDER BY relname");
res = PSQLexec(pset, descbuf);
if (!res)
return false;
if (PQntuples(res) == 0)
fputs("Couldn't find any tables.\n", pset->queryFout);
else
{
myopt.topt.tuples_only = false;
myopt.nullPrint = NULL;
sprintf(descbuf, "Access permissions for database \"%s\"", PQdb(pset->db));
myopt.title = descbuf;
printQuery(res, &myopt, pset->queryFout);
}
PQclear(res);
return true;
}
/*
* Get object comments
*
* \dd [foo]
*
* Note: This only lists things that actually have a description. For complete
* lists of things, there are other \d? commands.
*/
bool
objectDescription(const char *object, PsqlSettings *pset)
{
char descbuf[2048 + 7 * REGEXP_CUTOFF];
PGresult *res;
printQueryOpt myopt = pset->popt;
descbuf[0] = '\0';
/* Aggregate descriptions */
strcat(descbuf, "SELECT DISTINCT a.aggname as \"Name\", 'aggregate'::text as \"What\", d.description as \"Description\"\n"
"FROM pg_aggregate a, pg_description d\n"
"WHERE a.oid = d.objoid\n");
if (object)
{
strcat(descbuf, " AND a.aggname ~* '^");
strncat(descbuf, object, REGEXP_CUTOFF);
strcat(descbuf, "'\n");
}
/* Function descriptions (except in/outs for datatypes) */
strcat(descbuf, "\nUNION ALL\n\n");
strcat(descbuf, "SELECT DISTINCT p.proname as \"Name\", 'function'::text as \"What\", d.description as \"Description\"\n"
"FROM pg_proc p, pg_description d\n"
"WHERE p.oid = d.objoid AND (p.pronargs = 0 or oid8types(p.proargtypes) != '')\n");
if (object)
{
strcat(descbuf, " AND p.proname ~* '^");
strncat(descbuf, object, REGEXP_CUTOFF);
strcat(descbuf, "'\n");
}
/* Operator descriptions */
strcat(descbuf, "\nUNION ALL\n\n");
strcat(descbuf, "SELECT DISTINCT o.oprname as \"Name\", 'operator'::text as \"What\", d.description as \"Description\"\n"
"FROM pg_operator o, pg_description d\n"
// must get comment via associated function
"WHERE RegprocToOid(o.oprcode) = d.objoid\n");
if (object)
{
strcat(descbuf, " AND o.oprname = '");
strncat(descbuf, object, REGEXP_CUTOFF);
strcat(descbuf, "'\n");
}
/* Type description */
strcat(descbuf, "\nUNION ALL\n\n");
strcat(descbuf, "SELECT DISTINCT t.typname as \"Name\", 'type'::text as \"What\", d.description as \"Description\"\n"
"FROM pg_type t, pg_description d\n"
"WHERE t.oid = d.objoid\n");
if (object)
{
strcat(descbuf, " AND t.typname ~* '^");
strncat(descbuf, object, REGEXP_CUTOFF);
strcat(descbuf, "'\n");
}
/* Relation (tables, views, indices, sequences) descriptions */
strcat(descbuf, "\nUNION ALL\n\n");
strcat(descbuf, "SELECT DISTINCT c.relname as \"Name\", 'relation'::text||'('||c.relkind||')' as \"What\", d.description as \"Description\"\n"
"FROM pg_class c, pg_description d\n"
"WHERE c.oid = d.objoid\n");
if (object)
{
strcat(descbuf, " AND c.relname ~* '^");
strncat(descbuf, object, REGEXP_CUTOFF);
strcat(descbuf, "'\n");
}
/* Rule description (ignore rules for views) */
strcat(descbuf, "\nUNION ALL\n\n");
strcat(descbuf, "SELECT DISTINCT r.rulename as \"Name\", 'rule'::text as \"What\", d.description as \"Description\"\n"
"FROM pg_rewrite r, pg_description d\n"
"WHERE r.oid = d.objoid AND r.rulename !~ '^_RET'\n");
if (object)
{
strcat(descbuf, " AND r.rulename ~* '^");
strncat(descbuf, object, REGEXP_CUTOFF);
strcat(descbuf, "'\n");
}
/* Trigger description */
strcat(descbuf, "\nUNION ALL\n\n");
strcat(descbuf, "SELECT DISTINCT t.tgname as \"Name\", 'trigger'::text as \"What\", d.description as \"Description\"\n"
"FROM pg_trigger t, pg_description d\n"
"WHERE t.oid = d.objoid\n");
if (object)
{
strcat(descbuf, " AND t.tgname ~* '^");
strncat(descbuf, object, REGEXP_CUTOFF);
strcat(descbuf, "'\n");
}
strcat(descbuf, "\nORDER BY \"Name\"");
res = PSQLexec(pset, descbuf);
if (!res)
return false;
myopt.topt.tuples_only = false;
myopt.nullPrint = NULL;
myopt.title = "Object descriptions";
printQuery(res, &myopt, pset->queryFout);
PQclear(res);
return true;
}
/*
* describeTableDetails (for \d)
*
* Unfortunately, the information presented here is so complicated that it
* be done in a single query. So we have to assemble the printed table by hand
* and pass it to the underlying printTable() function.
*
*/
static void *
xmalloc(size_t size)
{
void *tmp;
tmp = malloc(size);
if (!tmp)
{
perror("malloc");
exit(EXIT_FAILURE);
}
return tmp;
}
bool
describeTableDetails(const char *name, PsqlSettings *pset)
{
char descbuf[512 + NAMEDATALEN];
PGresult *res = NULL,
*res2 = NULL,
*res3 = NULL;
printTableOpt myopt = pset->popt.topt;
bool description = GetVariableBool(pset->vars, "description");
int i;
char *view_def = NULL;
char *headers[5];
char **cells = NULL;
char *title = NULL;
char **footers = NULL;
char **ptr;
unsigned int cols;
cols = 3 + (description ? 1 : 0);
headers[0] = "Attribute";
headers[1] = "Type";
headers[2] = "Info";
if (description)
{
headers[3] = "Description";
headers[4] = NULL;
}
else
headers[3] = NULL;
/* Get general table info */
strcpy(descbuf, "SELECT a.attname, t.typname, a.attlen, a.atttypmod, a.attnotnull, a.atthasdef, a.attnum");
if (description)
strcat(descbuf, ", obj_description(a.oid)");
strcat(descbuf, "\nFROM pg_class c, pg_attribute a, pg_type t\n"
"WHERE c.relname = '");
strncat(descbuf, name, NAMEDATALEN);
strcat(descbuf, "'\n AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid\n"
"ORDER BY a.attnum");
res = PSQLexec(pset, descbuf);
if (!res)
return false;
/* Did we get anything? */
if (PQntuples(res) == 0)
{
if (!GetVariableBool(pset->vars, "quiet"))
fprintf(stdout, "Did not find any class named \"%s\".\n", name);
PQclear(res);
return false;
}
/* Check if table is a view */
strcpy(descbuf, "SELECT definition FROM pg_views WHERE viewname = '");
strncat(descbuf, name, NAMEDATALEN);
strcat(descbuf, "'");
res2 = PSQLexec(pset, descbuf);
if (!res2)
return false;
if (PQntuples(res2) > 0)
view_def = PQgetvalue(res2, 0, 0);
/* Generate table cells to be printed */
cells = calloc(PQntuples(res) * cols + 1, sizeof(*cells));
if (!cells)
{
perror("calloc");
exit(EXIT_FAILURE);
}
for (i = 0; i < PQntuples(res); i++)
{
int4 attypmod = atoi(PQgetvalue(res, i, 3));
char *attype = PQgetvalue(res, i, 1);
/* Name */
cells[i * cols + 0] = PQgetvalue(res, i, 0); /* don't free this
* afterwards */
/* Type */
cells[i * cols + 1] = xmalloc(NAMEDATALEN + 16);
if (strcmp(attype, "bpchar") == 0)
sprintf(cells[i * cols + 1], "char(%d)", attypmod != -1 ? attypmod - VARHDRSZ : 0);
else if (strcmp(attype, "varchar") == 0)
sprintf(cells[i * cols + 1], "varchar(%d)", attypmod != -1 ? attypmod - VARHDRSZ : 0);
else if (strcmp(attype, "numeric") == 0)
sprintf(cells[i * cols + 1], "numeric(%d,%d)", ((attypmod - VARHDRSZ) >> 16) & 0xffff,
(attypmod - VARHDRSZ) & 0xffff);
else if (attype[0] == '_')
sprintf(cells[i * cols + 1], "%s[]", attype + 1);
else
strcpy(cells[i * cols + 1], attype);
/* Info */
cells[i * cols + 2] = xmalloc(128 + 128); /* I'm cutting off the
* default string at 128 */
cells[i * cols + 2][0] = '\0';
if (strcmp(PQgetvalue(res, i, 4), "t") == 0)
strcat(cells[i * cols + 2], "not null");
if (strcmp(PQgetvalue(res, i, 5), "t") == 0)
{
/* handle "default" here */
strcpy(descbuf, "SELECT substring(d.adsrc for 128) FROM pg_attrdef d, pg_class c\n"
"WHERE c.relname = '");
strncat(descbuf, name, NAMEDATALEN);
strcat(descbuf, "' AND c.oid = d.adrelid AND d.adnum = ");
strcat(descbuf, PQgetvalue(res, i, 6));
res3 = PSQLexec(pset, descbuf);
if (!res)
return false;
if (cells[i * cols + 2][0])
strcat(cells[i * cols + 2], " ");
strcat(cells[i * cols + 2], "default ");
strcat(cells[i * cols + 2], PQgetvalue(res3, 0, 0));
}
/* Description */
if (description)
cells[i * cols + 3] = PQgetvalue(res, i, 7);
}
/* Make title */
title = xmalloc(10 + strlen(name));
if (view_def)
sprintf(title, "View \"%s\"", name);
else
sprintf(title, "Table \"%s\"", name);
/* Make footers */
if (view_def)
{
footers = xmalloc(2 * sizeof(*footers));
footers[0] = xmalloc(20 + strlen(view_def));
sprintf(footers[0], "View definition: %s", view_def);
footers[1] = NULL;
}
else
{
/* display indices */
strcpy(descbuf, "SELECT c2.relname\n"
"FROM pg_class c, pg_class c2, pg_index i\n"
"WHERE c.relname = '");
strncat(descbuf, name, NAMEDATALEN);
strcat(descbuf, "' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n"
"ORDER BY c2.relname");
res3 = PSQLexec(pset, descbuf);
if (!res3)
return false;
if (PQntuples(res3) > 0)
{
footers = xmalloc((PQntuples(res3) + 1) * sizeof(*footers));
for (i = 0; i < PQntuples(res3); i++)
{
footers[i] = xmalloc(10 + NAMEDATALEN);
if (PQntuples(res3) == 1)
sprintf(footers[i], "Index: %s", PQgetvalue(res3, i, 0));
else if (i == 0)
sprintf(footers[i], "Indices: %s", PQgetvalue(res3, i, 0));
else
sprintf(footers[i], " %s", PQgetvalue(res3, i, 0));
}
footers[i] = NULL;
}
}
myopt.tuples_only = false;
printTable(title, headers, cells, footers, "llll", &myopt, pset->queryFout);
/* clean up */
free(title);
for (i = 0; i < PQntuples(res); i++)
{
free(cells[i * cols + 1]);
free(cells[i * cols + 2]);
}
free(cells);
for (ptr = footers; footers && *ptr; ptr++)
free(*ptr);
free(footers);
PQclear(res);
PQclear(res2);
PQclear(res3);
return true;
}
/*
* listTables()
*
* handler for \d, \dt, etc.
*
* The infotype is an array of characters, specifying what info is desired:
* t - tables
* i - indices
* v - views
* s - sequences
* S - systems tables (~'^pg_')
* (any order of the above is fine)
*/
bool
listTables(const char *infotype, const char *name, PsqlSettings *pset)
{
bool showTables = strchr(infotype, 't') != NULL;
bool showIndices = strchr(infotype, 'i') != NULL;
bool showViews = strchr(infotype, 'v') != NULL;
bool showSeq = strchr(infotype, 's') != NULL;
bool showSystem = strchr(infotype, 'S') != NULL;
bool description = GetVariableBool(pset->vars, "description");
char descbuf[1536 + 4 * REGEXP_CUTOFF];
PGresult *res;
printQueryOpt myopt = pset->popt;
descbuf[0] = '\0';
/* tables */
if (showTables)
{
strcat(descbuf, "SELECT u.usename as \"Owner\", c.relname as \"Name\", 'table'::text as \"Type\"");
if (description)
strcat(descbuf, ", obj_description(c.oid) as \"Description\"");
strcat(descbuf, "\nFROM pg_class c, pg_user u\n"
"WHERE c.relowner = u.usesysid AND c.relkind = 'r'\n"
" AND not exists (select 1 from pg_views where viewname = c.relname)\n");
strcat(descbuf, showSystem ? " AND c.relname ~ '^pg_'\n" : " AND c.relname !~ '^pg_'\n");
if (name)
{
strcat(descbuf, " AND c.relname ~ '^");
strncat(descbuf, name, REGEXP_CUTOFF);
strcat(descbuf, "'\n");
}
}
/* views */
if (showViews)
{
if (descbuf[0])
strcat(descbuf, "\nUNION\n\n");
strcat(descbuf, "SELECT u.usename as \"Owner\", c.relname as \"Name\", 'view'::text as \"Type\"");
if (description)
strcat(descbuf, ", obj_description(c.oid) as \"Description\"");
strcat(descbuf, "\nFROM pg_class c, pg_user u\n"
"WHERE c.relowner = u.usesysid AND c.relkind = 'r'\n"
" AND exists (select 1 from pg_views where viewname = c.relname)\n");
strcat(descbuf, showSystem ? " AND c.relname ~ '^pg_'\n" : " AND c.relname !~ '^pg_'\n");
if (name)
{
strcat(descbuf, " AND c.relname ~ '^");
strncat(descbuf, name, REGEXP_CUTOFF);
strcat(descbuf, "'\n");
}
}
/* indices, sequences */
if (showIndices || showSeq)
{
if (descbuf[0])
strcat(descbuf, "\nUNION\n\n");
strcat(descbuf, "SELECT u.usename as \"Owner\", c.relname as \"Name\",\n"
" (CASE WHEN relkind = 'S' THEN 'sequence'::text ELSE 'index'::text END) as \"Type\"");
if (description)
strcat(descbuf, ", obj_description(c.oid) as \"Description\"");
strcat(descbuf, "\nFROM pg_class c, pg_user u\n"
"WHERE c.relowner = u.usesysid AND relkind in (");
if (showIndices && showSeq)
strcat(descbuf, "'i', 'S'");
else if (showIndices)
strcat(descbuf, "'i'");
else
strcat(descbuf, "'S'");
strcat(descbuf, ")\n");
/* ignore large-obj indices */
if (showIndices)
strcat(descbuf, " AND (c.relkind != 'i' OR c.relname !~ '^xinx')\n");
strcat(descbuf, showSystem ? " AND c.relname ~ '^pg_'\n" : " AND c.relname !~ '^pg_'\n");
if (name)
{
strcat(descbuf, " AND c.relname ~ '^");
strncat(descbuf, name, REGEXP_CUTOFF);
strcat(descbuf, "'\n");
}
}
/* real system catalogue tables */
if (showSystem && showTables)
{
if (descbuf[0])
strcat(descbuf, "\nUNION\n\n");
strcat(descbuf, "SELECT u.usename as \"Owner\", c.relname as \"Name\", 'system'::text as \"Type\"");
if (description)
strcat(descbuf, ", obj_description(c.oid) as \"Description\"");
strcat(descbuf, "\nFROM pg_class c, pg_user u\n"
"WHERE c.relowner = u.usesysid AND c.relkind = 's'\n");
if (name)
{
strcat(descbuf, " AND c.relname ~ '^");
strncat(descbuf, name, REGEXP_CUTOFF);
strcat(descbuf, "'\n");
}
}
strcat(descbuf, "\nORDER BY \"Name\"");
res = PSQLexec(pset, descbuf);
if (!res)
return false;
if (PQntuples(res) == 0)
fprintf(pset->queryFout, "No matching classes found.\n");
else
{
myopt.topt.tuples_only = false;
myopt.nullPrint = NULL;
myopt.title = "List of classes";
printQuery(res, &myopt, pset->queryFout);
}
PQclear(res);
return true;
}
/* the end */
|