OSDN Git Service

Add new columns to pg_store_plans view
[pgstoreplans/pg_store_plans.git] / sjson2testsql.pl
1 #! /usr/bin/perl
2
3 print <<'EOS';
4 \pset pager
5 SET client_min_messages = 'error';
6 CREATE EXTENSION IF NOT EXISTS pg_store_plans;
7 DROP TABLE IF EXISTS plans;
8 CREATE TABLE PLANS (id int, title text, plan text, inflated text);
9 SET client_min_messages = 'notice';
10 INSERT INTO PLANS (VALUES
11 EOS
12
13 $state = 0;
14 $first = 1;
15 while(<>) {
16         chomp;
17         if ($state == 0) {
18                 next if (!/^###### (Plan ([0-9]+):.*$)/);
19                 $title = $1; $plan_no = $2;
20                 if ($first) {
21                         $first = 0;
22                 } else {
23                         print ",\n\n";
24                 }
25                 print "-- ###### $title\n";
26                 $state = 1;
27         } elsif ($state == 1) {
28                 if (/^######/) {
29                         die("??? : $_");
30                 }
31                 next if (!/^ *({[^ ].*})$/);
32                 $plan = $1;
33                 $escape = "";
34                 if ($plan =~ /'/ || $plan =~ /\\\"/) {
35                         $escape = "E";
36                 }
37
38                 # Add escape char for '''
39                 $plan =~ s/'/\\'/g;
40                 # Add escape char for '\"'
41                 $plan =~ s/\\\"/\\\\\"/g;
42
43                 print "($plan_no, \'$title\',\n";
44                 print " $escape'$plan')";
45                 $state = 0;
46         }
47 }
48
49 print <<'EOS';
50 );
51 UPDATE plans SET inflated = pg_store_plans_jsonplan(plan);
52
53 \echo  ###### format conversion tests
54 SELECT '### '||'inflate-short    '||title||E'\n'||
55   inflated
56   FROM plans WHERE id BETWEEN 1 AND 3 ORDER BY id;
57 SELECT '### '||'yaml-short       '||title||E'\n'||
58   pg_store_plans_yamlplan(plan)
59   FROM plans WHERE id BETWEEN 4 AND 6 or id = 1 ORDER BY id;
60 SELECT '### '||'xml-short        '||title||E'\n'||
61   pg_store_plans_xmlplan(plan)
62   FROM plans WHERE id BETWEEN 7 AND 9 or id = 1 ORDER BY id;
63
64 \echo  ###### text format output test
65 SELECT '### '||'TEXT-short       '||title||E'\n'||
66   pg_store_plans_textplan(plan)
67   FROM plans ORDER BY id;
68
69 \echo  ###### long-json-as-a-source test
70 SELECT '### '||'inflate-long JSON'||title||E'\n'||
71   pg_store_plans_jsonplan(inflated)
72   FROM plans WHERE id BETWEEN 1 AND 3 ORDER BY id;
73 SELECT '### '||'yaml-long JSON   '||title||E'\n'||
74   pg_store_plans_yamlplan(inflated)
75   FROM plans WHERE id BETWEEN 4 AND 6 ORDER BY id;
76 SELECT '### '||'xml-long JSON    '||title||E'\n'||
77   pg_store_plans_xmlplan(inflated)
78   FROM plans WHERE id BETWEEN 7 AND 9 ORDER BY id;
79 SELECT '### '||'text-long JSON   '||title||E'\n'||
80   pg_store_plans_xmlplan(inflated)
81   FROM plans WHERE id BETWEEN 10 AND 12 ORDER BY id;
82
83 \echo  ###### chopped-source test
84 SELECT '### '||'inflate-chopped  '||title||E'\n'||
85   pg_store_plans_jsonplan(substring(plan from 1 for char_length(plan) / 3))
86   FROM plans WHERE id BETWEEN 13 AND 15 ORDER BY id;
87 SELECT '### '||'yaml-chopped     '||title||E'\n'||
88   pg_store_plans_yamlplan(substring(plan from 1 for char_length(plan) / 3))
89   FROM plans WHERE id BETWEEN 16 AND 18 ORDER BY id;
90 SELECT '### '||'xml-chopped      '||title||E'\n'||
91   pg_store_plans_xmlplan(substring(plan from 1 for char_length(plan) / 3))
92   FROM plans WHERE id BETWEEN 19 AND 21 ORDER BY id;
93 SELECT '### '||'text-chopped     '||title||E'\n'||
94   pg_store_plans_textplan(substring(plan from 1 for char_length(plan) / 3))
95   FROM plans WHERE id BETWEEN 22 AND 24 ORDER BY id;
96
97 \echo ###### shorten, normalize test
98 SELECT '### '||'shorten          '||title||E'\n'||
99   pg_store_plans_shorten(inflated)
100   FROM plans WHERE id BETWEEN 1 AND 3 ORDER BY id;
101 SELECT '### '||'normalize        '||title||E'\n'||
102   pg_store_plans_shorten(inflated)
103   FROM plans WHERE id BETWEEN 1 AND 3 ORDER BY id;
104
105 \echo ###### round-trip test
106 SELECT COUNT(*), SUM(success)
107  FROM (SELECT CASE
108                   WHEN pg_store_plans_shorten(inflated) = plan THEN 1 ELSE 0
109               END as success
110            FROM plans) t;
111 EOS