-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgdesc.go
3786 lines (3197 loc) · 102 KB
/
pgdesc.go
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
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Package pgdesc builds SQL introspection queries for PostgreSQL databases.
//
// Mix of generated and manually rewritten code from PostgreSQL's psql codebase.
//
// Written for use by xo and usql.
package pgdesc
// Code generated by gen.go. DO NOT EDIT.
//go:generate go run gen.go
import (
"fmt"
"io"
)
// Postgres RELKIND and other related constants.
const (
COERCION_CODE_ASSIGNMENT = 'a' // coercion in context of assignment
COERCION_CODE_EXPLICIT = 'e' // explicit cast operation
COERCION_CODE_IMPLICIT = 'i' // coercion in context of expression
COERCION_METHOD_BINARY = 'b' // types are binary-compatible
COERCION_METHOD_FUNCTION = 'f' // use a function
COERCION_METHOD_INOUT = 'i' // use input/output functions
DEFACLOBJ_FUNCTION = 'f' // function
DEFACLOBJ_NAMESPACE = 'n' // namespace
DEFACLOBJ_RELATION = 'r' // table, view
DEFACLOBJ_SEQUENCE = 'S' // sequence
DEFACLOBJ_TYPE = 'T' // type
RELKIND_COMPOSITE_TYPE = 'c' // composite type
RELKIND_FOREIGN_TABLE = 'f' // foreign table
RELKIND_INDEX = 'i' // secondary index
RELKIND_MATVIEW = 'm' // materialized view
RELKIND_PARTITIONED_INDEX = 'I' // partitioned index
RELKIND_PARTITIONED_TABLE = 'p' // partitioned table
RELKIND_RELATION = 'r' // ordinary table
RELKIND_SEQUENCE = 'S' // sequence object
RELKIND_TOASTVALUE = 't' // for out-of-line values
RELKIND_VIEW = 'v' // view
RELPERSISTENCE_PERMANENT = 'p' // regular table
RELPERSISTENCE_TEMP = 't' // temporary table
RELPERSISTENCE_UNLOGGED = 'u' // unlogged permanent table
)
// AccessMethods handles \dA.
//
// Generated from describeAccessMethods in psql's describe.c.
//
// \dA
// Takes an optional regexp to select particular access methods
func (d *PgDesc) AccessMethods(w io.Writer, pattern string, verbose bool) error {
// PQExpBufferData buf;
// PGresult *res;
// printQueryOpt myopt = pset.popt;
// static const bool translate_columns[] = {false, true, false, false};
if d.version < 90600 {
// char sverbuf[32];
return fmt.Errorf("The server (version %s) does not support access methods.\n",
d.sversion)
// return true;
}
// initPQExpBuffer(w);
fmt.Fprintf(w,
"SELECT amname AS \"%s\",\n"+
" CASE amtype"+
" WHEN 'i' THEN '%s'"+
" END AS \"%s\"",
GettextNoop("Name"),
GettextNoop("Index"),
GettextNoop("Type"))
if verbose {
fmt.Fprintf(w,
",\n amhandler AS \"%s\",\n"+
" pg_catalog.obj_description(oid, 'pg_am') AS \"%s\"",
GettextNoop("Handler"),
GettextNoop("Description"))
}
fmt.Fprint(w,
"\nFROM pg_catalog.pg_am\n")
processSQLNamePattern(w, pattern, false, false,
NULL, "amname", NULL,
NULL)
fmt.Fprint(w, "ORDER BY 1;")
// res = PSQLexec(buf.data);
// termPQExpBuffer(w);
// if (!res)
// return false;
// myopt.nullPrint = NULL;
// myopt.title = _("List of access methods");
// myopt.translate_header = true;
// myopt.translate_columns = translate_columns;
// myopt.n_translate_columns = lengthof(translate_columns);
// printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
// PQclear(res);
// return true;
return nil
}
// Aggregates handles \da.
//
// Generated from describeAggregates in psql's describe.c.
//
// \da
// Takes an optional regexp to select particular aggregates
func (d *PgDesc) Aggregates(w io.Writer, pattern string, verbose bool, showSystem bool) error {
// PQExpBufferData buf;
// PGresult *res;
// printQueryOpt myopt = pset.popt;
// initPQExpBuffer(w);
fmt.Fprintf(w,
"SELECT n.nspname as \"%s\",\n"+
" p.proname AS \"%s\",\n"+
" pg_catalog.format_type(p.prorettype, NULL) AS \"%s\",\n",
GettextNoop("Schema"),
GettextNoop("Name"),
GettextNoop("Result data type"))
if d.version >= 80400 {
fmt.Fprintf(w,
" CASE WHEN p.pronargs = 0\n"+
" THEN CAST('*' AS pg_catalog.text)\n"+
" ELSE pg_catalog.pg_get_function_arguments(p.oid)\n"+
" END AS \"%s\",\n",
GettextNoop("Argument data types"))
} else if d.version >= 80200 {
fmt.Fprintf(w,
" CASE WHEN p.pronargs = 0\n"+
" THEN CAST('*' AS pg_catalog.text)\n"+
" ELSE\n"+
" pg_catalog.array_to_string(ARRAY(\n"+
" SELECT\n"+
" pg_catalog.format_type(p.proargtypes[s.i], NULL)\n"+
" FROM\n"+
" pg_catalog.generate_series(0, pg_catalog.array_upper(p.proargtypes, 1)) AS s(i)\n"+
" ), ', ')\n"+
" END AS \"%s\",\n",
GettextNoop("Argument data types"))
} else {
fmt.Fprintf(w,
" pg_catalog.format_type(p.proargtypes[0], NULL) AS \"%s\",\n",
GettextNoop("Argument data types"))
}
if d.version >= 110000 {
fmt.Fprintf(w,
" pg_catalog.obj_description(p.oid, 'pg_proc') as \"%s\"\n"+
"FROM pg_catalog.pg_proc p\n"+
" LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n"+
"WHERE p.prokind = 'a'\n",
GettextNoop("Description"))
} else {
fmt.Fprintf(w,
" pg_catalog.obj_description(p.oid, 'pg_proc') as \"%s\"\n"+
"FROM pg_catalog.pg_proc p\n"+
" LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace\n"+
"WHERE p.proisagg\n",
GettextNoop("Description"))
}
if !showSystem && pattern != NULL {
fmt.Fprint(w, " AND n.nspname <> 'pg_catalog'\n"+
" AND n.nspname <> 'information_schema'\n")
}
processSQLNamePattern(w, pattern, true, false,
"n.nspname", "p.proname", NULL,
"pg_catalog.pg_function_is_visible(p.oid)")
fmt.Fprint(w, "ORDER BY 1, 2, 4;")
// res = PSQLexec(buf.data);
// termPQExpBuffer(w);
// if (!res)
// return false;
// myopt.nullPrint = NULL;
// myopt.title = _("List of aggregate functions");
// myopt.translate_header = true;
// printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
// PQclear(res);
// return true;
return nil
}
// Casts handles \dC.
//
// Generated from listCasts in psql's describe.c.
//
// \dC
//
// Describes casts.
func (d *PgDesc) Casts(w io.Writer, pattern string, verbose bool) error {
// PQExpBufferData buf;
// PGresult *res;
// printQueryOpt myopt = pset.popt;
// static const bool translate_columns[] = {false, false, false, true, false};
// initPQExpBuffer(w);
fmt.Fprintf(w,
"SELECT pg_catalog.format_type(castsource, NULL) AS \"%s\",\n"+
" pg_catalog.format_type(casttarget, NULL) AS \"%s\",\n",
GettextNoop("Source type"),
GettextNoop("Target type"))
/*
* We don't attempt to localize '(binary coercible)' or '(with inout)',
* because there's too much risk of gettext translating a function name
* that happens to match some string in the PO database.
*/
if d.version >= 80400 {
fmt.Fprintf(w,
" CASE WHEN c.castmethod = '%c' THEN '(binary coercible)'\n"+
" WHEN c.castmethod = '%c' THEN '(with inout)'\n"+
" ELSE p.proname\n"+
" END AS \"%s\",\n",
COERCION_METHOD_BINARY,
COERCION_METHOD_INOUT,
GettextNoop("Function"))
} else {
fmt.Fprintf(w,
" CASE WHEN c.castfunc = 0 THEN '(binary coercible)'\n"+
" ELSE p.proname\n"+
" END AS \"%s\",\n",
GettextNoop("Function"))
}
fmt.Fprintf(w,
" CASE WHEN c.castcontext = '%c' THEN '%s'\n"+
" WHEN c.castcontext = '%c' THEN '%s'\n"+
" ELSE '%s'\n"+
" END AS \"%s\"",
COERCION_CODE_EXPLICIT,
GettextNoop("no"),
COERCION_CODE_ASSIGNMENT,
GettextNoop("in assignment"),
GettextNoop("yes"),
GettextNoop("Implicit?"))
if verbose {
fmt.Fprintf(w,
",\n d.description AS \"%s\"",
GettextNoop("Description"))
}
/*
* We need a left join to pg_proc for binary casts; the others are just
* paranoia.
*/
fmt.Fprint(w,
"\nFROM pg_catalog.pg_cast c LEFT JOIN pg_catalog.pg_proc p\n"+
" ON c.castfunc = p.oid\n"+
" LEFT JOIN pg_catalog.pg_type ts\n"+
" ON c.castsource = ts.oid\n"+
" LEFT JOIN pg_catalog.pg_namespace ns\n"+
" ON ns.oid = ts.typnamespace\n"+
" LEFT JOIN pg_catalog.pg_type tt\n"+
" ON c.casttarget = tt.oid\n"+
" LEFT JOIN pg_catalog.pg_namespace nt\n"+
" ON nt.oid = tt.typnamespace\n")
if verbose {
fmt.Fprint(w,
" LEFT JOIN pg_catalog.pg_description d\n"+
" ON d.classoid = c.tableoid AND d.objoid = "+
"c.oid AND d.objsubid = 0\n")
}
fmt.Fprint(w, "WHERE ( (true")
/*
* Match name pattern against either internal or external name of either
* castsource or casttarget
*/
processSQLNamePattern(w, pattern, true, false,
"ns.nspname", "ts.typname",
"pg_catalog.format_type(ts.oid, NULL)",
"pg_catalog.pg_type_is_visible(ts.oid)")
fmt.Fprint(w, ") OR (true")
processSQLNamePattern(w, pattern, true, false,
"nt.nspname", "tt.typname",
"pg_catalog.format_type(tt.oid, NULL)",
"pg_catalog.pg_type_is_visible(tt.oid)")
fmt.Fprint(w, ") )\nORDER BY 1, 2;")
// res = PSQLexec(buf.data);
// termPQExpBuffer(w);
// if (!res)
// return false;
// myopt.nullPrint = NULL;
// myopt.title = _("List of casts");
// myopt.translate_header = true;
// myopt.translate_columns = translate_columns;
// myopt.n_translate_columns = lengthof(translate_columns);
// printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
// PQclear(res);
// return true;
return nil
}
// Collations handles \dO.
//
// Generated from listCollations in psql's describe.c.
//
// \dO
//
// Describes collations.
func (d *PgDesc) Collations(w io.Writer, pattern string, verbose bool, showSystem bool) error {
// PQExpBufferData buf;
// PGresult *res;
// printQueryOpt myopt = pset.popt;
// static const bool translate_columns[] = {false, false, false, false, false, false};
if d.version < 90100 {
// char sverbuf[32];
return fmt.Errorf("The server (version %s) does not support collations.\n",
d.sversion)
// return true;
}
// initPQExpBuffer(w);
fmt.Fprintf(w,
"SELECT n.nspname AS \"%s\",\n"+
" c.collname AS \"%s\",\n"+
" c.collcollate AS \"%s\",\n"+
" c.collctype AS \"%s\"",
GettextNoop("Schema"),
GettextNoop("Name"),
GettextNoop("Collate"),
GettextNoop("Ctype"))
if d.version >= 100000 {
fmt.Fprintf(w,
",\n CASE c.collprovider WHEN 'd' THEN 'default' WHEN 'c' THEN 'libc' WHEN 'i' THEN 'icu' END AS \"%s\"",
GettextNoop("Provider"))
}
if verbose {
fmt.Fprintf(w,
",\n pg_catalog.obj_description(c.oid, 'pg_collation') AS \"%s\"",
GettextNoop("Description"))
}
fmt.Fprint(w,
"\nFROM pg_catalog.pg_collation c, pg_catalog.pg_namespace n\n"+
"WHERE n.oid = c.collnamespace\n")
if !showSystem && pattern != NULL {
fmt.Fprint(w, " AND n.nspname <> 'pg_catalog'\n"+
" AND n.nspname <> 'information_schema'\n")
}
/*
* Hide collations that aren't usable in the current database's encoding.
* If you think to change this, note that pg_collation_is_visible rejects
* unusable collations, so you will need to hack name pattern processing
* somehow to avoid inconsistent behavior.
*/
fmt.Fprint(w, " AND c.collencoding IN (-1, pg_catalog.pg_char_to_encoding(pg_catalog.getdatabaseencoding()))\n")
processSQLNamePattern(w, pattern, true, false,
"n.nspname", "c.collname", NULL,
"pg_catalog.pg_collation_is_visible(c.oid)")
fmt.Fprint(w, "ORDER BY 1, 2;")
// res = PSQLexec(buf.data);
// termPQExpBuffer(w);
// if (!res)
// return false;
// myopt.nullPrint = NULL;
// myopt.title = _("List of collations");
// myopt.translate_header = true;
// myopt.translate_columns = translate_columns;
// myopt.n_translate_columns = lengthof(translate_columns);
// printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
// PQclear(res);
// return true;
return nil
}
// Conversions handles \dc.
//
// Generated from listConversions in psql's describe.c.
//
// \dc
//
// Describes conversions.
func (d *PgDesc) Conversions(w io.Writer, pattern string, verbose bool, showSystem bool) error {
// PQExpBufferData buf;
// PGresult *res;
// printQueryOpt myopt = pset.popt;
// static const bool translate_columns[] =
// {false, false, false, false, true, false};
// initPQExpBuffer(w);
fmt.Fprintf(w,
"SELECT n.nspname AS \"%s\",\n"+
" c.conname AS \"%s\",\n"+
" pg_catalog.pg_encoding_to_char(c.conforencoding) AS \"%s\",\n"+
" pg_catalog.pg_encoding_to_char(c.contoencoding) AS \"%s\",\n"+
" CASE WHEN c.condefault THEN '%s'\n"+
" ELSE '%s' END AS \"%s\"",
GettextNoop("Schema"),
GettextNoop("Name"),
GettextNoop("Source"),
GettextNoop("Destination"),
GettextNoop("yes"), GettextNoop("no"),
GettextNoop("Default?"))
if verbose {
fmt.Fprintf(w,
",\n d.description AS \"%s\"",
GettextNoop("Description"))
}
fmt.Fprint(w,
"\nFROM pg_catalog.pg_conversion c\n"+
" JOIN pg_catalog.pg_namespace n "+
"ON n.oid = c.connamespace\n")
if verbose {
fmt.Fprint(w,
"LEFT JOIN pg_catalog.pg_description d "+
"ON d.classoid = c.tableoid\n"+
" AND d.objoid = c.oid "+
"AND d.objsubid = 0\n")
}
fmt.Fprint(w, "WHERE true\n")
if !showSystem && pattern != NULL {
fmt.Fprint(w, " AND n.nspname <> 'pg_catalog'\n"+
" AND n.nspname <> 'information_schema'\n")
}
processSQLNamePattern(w, pattern, true, false,
"n.nspname", "c.conname", NULL,
"pg_catalog.pg_conversion_is_visible(c.oid)")
fmt.Fprint(w, "ORDER BY 1, 2;")
// res = PSQLexec(buf.data);
// termPQExpBuffer(w);
// if (!res)
// return false;
// myopt.nullPrint = NULL;
// myopt.title = _("List of conversions");
// myopt.translate_header = true;
// myopt.translate_columns = translate_columns;
// myopt.n_translate_columns = lengthof(translate_columns);
// printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
// PQclear(res);
// return true;
return nil
}
// DatabaseRoleSettings handles \drds.
//
// Generated from listDbRoleSettings in psql's describe.c.
//
// \drds
func (d *PgDesc) DatabaseRoleSettings(w io.Writer, pattern string, pattern2 string) error {
// PQExpBufferData buf;
// PGresult *res;
// printQueryOpt myopt = pset.popt;
var havewhere bool
if d.version < 90000 {
// char sverbuf[32];
return fmt.Errorf("The server (version %s) does not support per-database role settings.\n",
d.sversion)
// return true;
}
// initPQExpBuffer(w);
fmt.Fprintf(w, "SELECT rolname AS \"%s\", datname AS \"%s\",\n"+
"pg_catalog.array_to_string(setconfig, E'\\n') AS \"%s\"\n"+
"FROM pg_catalog.pg_db_role_setting s\n"+
"LEFT JOIN pg_catalog.pg_database d ON d.oid = setdatabase\n"+
"LEFT JOIN pg_catalog.pg_roles r ON r.oid = setrole\n",
GettextNoop("Role"),
GettextNoop("Database"),
GettextNoop("Settings"))
havewhere = processSQLNamePattern(w, pattern, false, false,
NULL, "r.rolname", NULL, NULL)
processSQLNamePattern(w, pattern2, havewhere, false,
NULL, "d.datname", NULL, NULL)
fmt.Fprint(w, "ORDER BY 1, 2;")
// res = PSQLexec(buf.data);
// termPQExpBuffer(w);
// if (!res)
// return false;
// /*
// * Most functions in this file are content to print an empty table when
// * there are no matching objects. We intentionally deviate from that
// * here, but only in !quiet mode, because of the possibility that the user
// * is confused about what the two pattern arguments mean.
// */
// if (PQntuples(res) == 0 && !pset.quiet) {
// if (pattern != NULL && pattern2 != NULL)
// return fmt.Errorf("Did not find any settings for role \"%s\" and database \"%s\".\n",
// pattern, pattern2);
// else if (pattern != NULL)
// return fmt.Errorf("Did not find any settings for role \"%s\".\n",
// pattern);
// else
// return fmt.Errorf("Did not find any settings.\n");
// }
// else
// {
// myopt.nullPrint = NULL;
// myopt.title = _("List of settings");
// myopt.translate_header = true;
// printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
// }
// PQclear(res);
// return true;
return nil
}
// Databases handles \l.
//
// Generated from listAllDbs in psql's describe.c.
//
// listAllDbs
//
// for \l, \list, and -l switch
func (d *PgDesc) Databases(w io.Writer, pattern string, verbose bool) error {
// PGresult *res;
// PQExpBufferData buf;
// printQueryOpt myopt = pset.popt;
// initPQExpBuffer(w);
fmt.Fprintf(w,
"SELECT d.datname as \"%s\",\n"+
" pg_catalog.pg_get_userbyid(d.datdba) as \"%s\",\n"+
" pg_catalog.pg_encoding_to_char(d.encoding) as \"%s\",\n",
GettextNoop("Name"),
GettextNoop("Owner"),
GettextNoop("Encoding"))
if d.version >= 80400 {
fmt.Fprintf(w,
" d.datcollate as \"%s\",\n"+
" d.datctype as \"%s\",\n",
GettextNoop("Collate"),
GettextNoop("Ctype"))
}
fmt.Fprint(w, " ")
d.printACLColumn(w, "d.datacl")
if verbose && d.version >= 80200 {
fmt.Fprintf(w,
",\n CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT')\n"+
" THEN pg_catalog.pg_size_pretty(pg_catalog.pg_database_size(d.datname))\n"+
" ELSE 'No Access'\n"+
" END as \"%s\"",
GettextNoop("Size"))
}
if verbose && d.version >= 80000 {
fmt.Fprintf(w,
",\n t.spcname as \"%s\"",
GettextNoop("Tablespace"))
}
if verbose && d.version >= 80200 {
fmt.Fprintf(w,
",\n pg_catalog.shobj_description(d.oid, 'pg_database') as \"%s\"",
GettextNoop("Description"))
}
fmt.Fprint(w,
"\nFROM pg_catalog.pg_database d\n")
if verbose && d.version >= 80000 {
fmt.Fprint(w,
" JOIN pg_catalog.pg_tablespace t on d.dattablespace = t.oid\n")
}
if pattern != NULL {
processSQLNamePattern(w, pattern, false, false,
NULL, "d.datname", NULL, NULL)
}
fmt.Fprint(w, "ORDER BY 1;")
// res = PSQLexec(buf.data);
// termPQExpBuffer(w);
// if (!res)
// return false;
// myopt.nullPrint = NULL;
// myopt.title = _("List of databases");
// myopt.translate_header = true;
// printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
// PQclear(res);
// return true;
return nil
}
// DefaultACLS handles \ddp.
//
// Generated from listDefaultACLs in psql's describe.c.
//
// \ddp
//
// List Default ACLs. The pattern can match either schema or role name.
func (d *PgDesc) DefaultACLS(w io.Writer, pattern string) error {
// PQExpBufferData buf;
// PGresult *res;
// printQueryOpt myopt = pset.popt;
// static const bool translate_columns[] = {false, false, true, false};
if d.version < 90000 {
// char sverbuf[32];
return fmt.Errorf("The server (version %s) does not support altering default privileges.\n",
d.sversion)
// return true;
}
// initPQExpBuffer(w);
fmt.Fprintf(w,
"SELECT pg_catalog.pg_get_userbyid(d.defaclrole) AS \"%s\",\n"+
" n.nspname AS \"%s\",\n"+
" CASE d.defaclobjtype WHEN '%c' THEN '%s' WHEN '%c' THEN '%s' WHEN '%c' THEN '%s' WHEN '%c' THEN '%s' WHEN '%c' THEN '%s' END AS \"%s\",\n"+
" ",
GettextNoop("Owner"),
GettextNoop("Schema"),
DEFACLOBJ_RELATION,
GettextNoop("table"),
DEFACLOBJ_SEQUENCE,
GettextNoop("sequence"),
DEFACLOBJ_FUNCTION,
GettextNoop("function"),
DEFACLOBJ_TYPE,
GettextNoop("type"),
DEFACLOBJ_NAMESPACE,
GettextNoop("schema"),
GettextNoop("Type"))
d.printACLColumn(w, "d.defaclacl")
fmt.Fprint(w, "\nFROM pg_catalog.pg_default_acl d\n"+
" LEFT JOIN pg_catalog.pg_namespace n ON n.oid = d.defaclnamespace\n")
processSQLNamePattern(w, pattern, false, false,
NULL,
"n.nspname",
"pg_catalog.pg_get_userbyid(d.defaclrole)",
NULL)
fmt.Fprint(w, "ORDER BY 1, 2, 3;")
// res = PSQLexec(buf.data);
// if (!res) {
// termPQExpBuffer(w);
// return false;
// }
// myopt.nullPrint = NULL;
// fmt.Fprintf(w, _("Default access privileges"));
// myopt.title = buf.data;
// myopt.translate_header = true;
// myopt.translate_columns = translate_columns;
// myopt.n_translate_columns = lengthof(translate_columns);
// printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
// termPQExpBuffer(w);
// PQclear(res);
// return true;
return nil
}
// Domains handles \dD.
//
// Generated from listDomains in psql's describe.c.
//
// \dD
//
// Describes domains.
func (d *PgDesc) Domains(w io.Writer, pattern string, verbose bool, showSystem bool) error {
// PQExpBufferData buf;
// PGresult *res;
// printQueryOpt myopt = pset.popt;
// initPQExpBuffer(w);
fmt.Fprintf(w,
"SELECT n.nspname as \"%s\",\n"+
" t.typname as \"%s\",\n"+
" pg_catalog.format_type(t.typbasetype, t.typtypmod) as \"%s\",\n",
GettextNoop("Schema"),
GettextNoop("Name"),
GettextNoop("Type"))
if d.version >= 90100 {
fmt.Fprintf(w,
" (SELECT c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type bt\n"+
" WHERE c.oid = t.typcollation AND bt.oid = t.typbasetype AND t.typcollation <> bt.typcollation) as \"%s\",\n",
GettextNoop("Collation"))
}
fmt.Fprintf(w,
" CASE WHEN t.typnotnull THEN 'not null' END as \"%s\",\n"+
" t.typdefault as \"%s\",\n"+
" pg_catalog.array_to_string(ARRAY(\n"+
" SELECT pg_catalog.pg_get_constraintdef(r.oid, true) FROM pg_catalog.pg_constraint r WHERE t.oid = r.contypid\n"+
" ), ' ') as \"%s\"",
GettextNoop("Nullable"),
GettextNoop("Default"),
GettextNoop("Check"))
if verbose {
if d.version >= 90200 {
fmt.Fprint(w, ",\n ")
d.printACLColumn(w, "t.typacl")
}
fmt.Fprintf(w,
",\n d.description as \"%s\"",
GettextNoop("Description"))
}
fmt.Fprint(w,
"\nFROM pg_catalog.pg_type t\n"+
" LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace\n")
if verbose {
fmt.Fprint(w,
" LEFT JOIN pg_catalog.pg_description d "+
"ON d.classoid = t.tableoid AND d.objoid = t.oid "+
"AND d.objsubid = 0\n")
}
fmt.Fprint(w, "WHERE t.typtype = 'd'\n")
if !showSystem && pattern != NULL {
fmt.Fprint(w, " AND n.nspname <> 'pg_catalog'\n"+
" AND n.nspname <> 'information_schema'\n")
}
processSQLNamePattern(w, pattern, true, false,
"n.nspname", "t.typname", NULL,
"pg_catalog.pg_type_is_visible(t.oid)")
fmt.Fprint(w, "ORDER BY 1, 2;")
// res = PSQLexec(buf.data);
// termPQExpBuffer(w);
// if (!res)
// return false;
// myopt.nullPrint = NULL;
// myopt.title = _("List of domains");
// myopt.translate_header = true;
// printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
// PQclear(res);
// return true;
return nil
}
// EventTriggers handles \dy.
//
// Generated from listEventTriggers in psql's describe.c.
//
// \dy
//
// Describes Event Triggers.
func (d *PgDesc) EventTriggers(w io.Writer, pattern string, verbose bool) error {
// PQExpBufferData buf;
// PGresult *res;
// printQueryOpt myopt = pset.popt;
// static const bool translate_columns[] =
// {false, false, false, true, false, false, false};
// initPQExpBuffer(w);
fmt.Fprintf(w,
"SELECT evtname as \"%s\", "+
"evtevent as \"%s\", "+
"pg_catalog.pg_get_userbyid(e.evtowner) as \"%s\",\n"+
" case evtenabled when 'O' then '%s'"+
" when 'R' then '%s'"+
" when 'A' then '%s'"+
" when 'D' then '%s' end as \"%s\",\n"+
" e.evtfoid::pg_catalog.regproc as \"%s\", "+
"pg_catalog.array_to_string(array(select x"+
" from pg_catalog.unnest(evttags) as t(x)), ', ') as \"%s\"",
GettextNoop("Name"),
GettextNoop("Event"),
GettextNoop("Owner"),
GettextNoop("enabled"),
GettextNoop("replica"),
GettextNoop("always"),
GettextNoop("disabled"),
GettextNoop("Enabled"),
GettextNoop("Function"),
GettextNoop("Tags"))
if verbose {
fmt.Fprintf(w,
",\npg_catalog.obj_description(e.oid, 'pg_event_trigger') as \"%s\"",
GettextNoop("Description"))
}
fmt.Fprint(w,
"\nFROM pg_catalog.pg_event_trigger e ")
processSQLNamePattern(w, pattern, false, false,
NULL, "evtname", NULL, NULL)
fmt.Fprint(w, "ORDER BY 1")
// res = PSQLexec(buf.data);
// termPQExpBuffer(w);
// if (!res)
// return false;
// myopt.nullPrint = NULL;
// myopt.title = _("List of event triggers");
// myopt.translate_header = true;
// myopt.translate_columns = translate_columns;
// myopt.n_translate_columns = lengthof(translate_columns);
// printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
// PQclear(res);
// return true;
return nil
}
// ExtensionContents handles \dx+.
//
// Generated from listExtensionContents in psql's describe.c.
//
// \dx+
//
// List contents of installed extensions.
func (d *PgDesc) ExtensionContents(w io.Writer, pattern string) error {
// PQExpBufferData buf;
// PGresult *res;
// int i;
if d.version < 90100 {
// char sverbuf[32];
return fmt.Errorf("The server (version %s) does not support extensions.\n",
d.sversion)
// return true;
}
// initPQExpBuffer(w);
fmt.Fprintf(w,
"SELECT e.extname, e.oid\n"+
"FROM pg_catalog.pg_extension e\n")
processSQLNamePattern(w, pattern,
false, false,
NULL, "e.extname", NULL,
NULL)
fmt.Fprint(w, "ORDER BY 1;")
// res = PSQLexec(buf.data);
// termPQExpBuffer(w);
// if (!res)
// return false;
// if (PQntuples(res) == 0) {
// if (!pset.quiet) {
// if (pattern != NULL)
// return fmt.Errorf("Did not find any extension named \"%s\".\n",
// pattern);
// else
// return fmt.Errorf("Did not find any extensions.\n");
// }
// PQclear(res);
// return false;
// }
// for i := 0; i < PQntuples(res); i++ {
// const char *extname;
// const char *oid;
// extname = PQgetvalue(res, i, 0);
// oid = PQgetvalue(res, i, 1);
// if (!d.OneExtensionContents(w, extname, oid)) {
// PQclear(res);
// return false;
// }
// if (cancel_pressed) {
// PQclear(res);
// return false;
// }
// }
// PQclear(res);
// return true;
return nil
}
// Extensions handles \dx.
//
// Generated from listExtensions in psql's describe.c.
//
// \dx
//
// Briefly describes installed extensions.
func (d *PgDesc) Extensions(w io.Writer, pattern string) error {
// PQExpBufferData buf;
// PGresult *res;
// printQueryOpt myopt = pset.popt;
if d.version < 90100 {
// char sverbuf[32];
return fmt.Errorf("The server (version %s) does not support extensions.\n",
d.sversion)
// return true;
}
// initPQExpBuffer(w);
fmt.Fprintf(w,
"SELECT e.extname AS \"%s\", "+
"e.extversion AS \"%s\", n.nspname AS \"%s\", c.description AS \"%s\"\n"+
"FROM pg_catalog.pg_extension e "+
"LEFT JOIN pg_catalog.pg_namespace n ON n.oid = e.extnamespace "+
"LEFT JOIN pg_catalog.pg_description c ON c.objoid = e.oid "+
"AND c.classoid = 'pg_catalog.pg_extension'::pg_catalog.regclass\n",
GettextNoop("Name"),
GettextNoop("Version"),
GettextNoop("Schema"),
GettextNoop("Description"))
processSQLNamePattern(w, pattern,
false, false,
NULL, "e.extname", NULL,
NULL)
fmt.Fprint(w, "ORDER BY 1;")
// res = PSQLexec(buf.data);
// termPQExpBuffer(w);
// if (!res)
// return false;
// myopt.nullPrint = NULL;
// myopt.title = _("List of installed extensions");
// myopt.translate_header = true;
// printQuery(res, &myopt, pset.queryFout, false, pset.logfile);