pgsql-deparser 13.3.11 → 13.3.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,14 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [13.3.12](https://github.com/pyramation/pgsql-parser/compare/pgsql-deparser@13.3.11...pgsql-deparser@13.3.12) (2022-07-29)
7
+
8
+ **Note:** Version bump only for package pgsql-deparser
9
+
10
+
11
+
12
+
13
+
6
14
  ## [13.3.11](https://github.com/pyramation/pgsql-parser/compare/pgsql-deparser@13.3.10...pgsql-deparser@13.3.11) (2022-05-20)
7
15
 
8
16
  **Note:** Version bump only for package pgsql-deparser
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # pgsql-parser [![Build Status](https://travis-ci.org/pyramation/pgsql-parser.svg?branch=master)](https://travis-ci.org/pyramation/pgsql-parser)
1
+ # pgsql-parser [![tests](https://github.com/pyramation/pgsql-parser/actions/workflows/run-tests.yaml/badge.svg)](https://github.com/pyramation/pgsql-parser/actions/workflows/run-tests.yaml)
2
2
 
3
3
  The real PostgreSQL parser for nodejs. The primary objective of this module is to provide symmetric parsing and deparsing of SQL statements. With this module you can modify parts of a SQL query statement and serialize the query tree back into a formatted SQL statement. It uses the *real* [PostgreSQL parser](https://github.com/pganalyze/libpg_query).
4
4
 
package/main/deparser.js CHANGED
@@ -124,6 +124,15 @@ var Deparser = /*#__PURE__*/function () {
124
124
  return _lodash["default"].isArray(node) ? _this2.list(node, ', ', '', context) : _this2.deparse(node, context);
125
125
  });
126
126
  }
127
+ }, {
128
+ key: "deparseReturningList",
129
+ value: function deparseReturningList(list, context) {
130
+ var _this3 = this;
131
+
132
+ return list.map(function (returning) {
133
+ return _this3.deparse(returning.ResTarget.val, context) + (returning.ResTarget.name ? ' AS ' + _this3.quote(returning.ResTarget.name) : '');
134
+ }).join(',');
135
+ }
127
136
  }, {
128
137
  key: "list",
129
138
  value: function list(nodes) {
@@ -142,17 +151,17 @@ var Deparser = /*#__PURE__*/function () {
142
151
  }, {
143
152
  key: "listQuotes",
144
153
  value: function listQuotes(nodes) {
145
- var _this3 = this;
154
+ var _this4 = this;
146
155
 
147
156
  var separator = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ', ';
148
157
  return this.list(nodes, separator).split(separator).map(function (a) {
149
- return _this3.quote(a.trim());
158
+ return _this4.quote(a.trim());
150
159
  }).join(separator);
151
160
  }
152
161
  }, {
153
162
  key: "quote",
154
163
  value: function quote(value) {
155
- var _this4 = this;
164
+ var _this5 = this;
156
165
 
157
166
  if (value == null) {
158
167
  return null;
@@ -160,7 +169,7 @@ var Deparser = /*#__PURE__*/function () {
160
169
 
161
170
  if (_lodash["default"].isArray(value)) {
162
171
  return value.map(function (o) {
163
- return _this4.quote(o);
172
+ return _this5.quote(o);
164
173
  });
165
174
  }
166
175
 
@@ -229,10 +238,10 @@ var Deparser = /*#__PURE__*/function () {
229
238
  }, {
230
239
  key: "type",
231
240
  value: function type(names, args) {
232
- var _this5 = this;
241
+ var _this6 = this;
233
242
 
234
243
  var _names$map = names.map(function (name) {
235
- return _this5.deparse(name);
244
+ return _this6.deparse(name);
236
245
  }),
237
246
  _names$map2 = (0, _slicedToArray2["default"])(_names$map, 2),
238
247
  catalog = _names$map2[0],
@@ -336,15 +345,15 @@ var Deparser = /*#__PURE__*/function () {
336
345
  output.push('TO');
337
346
  output.push(this.RangeVar(node.relation, context));
338
347
 
339
- if (node.instead) {
340
- output.push('DO');
341
- output.push('INSTEAD');
342
- }
343
-
344
348
  if (node.whereClause) {
345
349
  output.push('WHERE');
346
350
  output.push(this.deparse(node.whereClause, context));
347
- output.push('DO');
351
+ }
352
+
353
+ output.push('DO');
354
+
355
+ if (node.instead) {
356
+ output.push('INSTEAD');
348
357
  }
349
358
 
350
359
  if (!node.actions || !node.actions.length) {
@@ -920,22 +929,22 @@ var Deparser = /*#__PURE__*/function () {
920
929
  }, {
921
930
  key: 'ColumnRef',
922
931
  value: function ColumnRef(node) {
923
- var _this6 = this;
932
+ var _this7 = this;
924
933
 
925
934
  var context = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
926
935
  var KEYWORDS = ['old', 'new'];
927
936
  var fields = node.fields.map(function (field) {
928
937
  if (field.String) {
929
- var value = _this6.deparse(field, context);
938
+ var value = _this7.deparse(field, context);
930
939
 
931
940
  if (context === 'trigger' && KEYWORDS.includes(value.toLowerCase())) {
932
941
  return value.toUpperCase();
933
942
  }
934
943
 
935
- return _this6.quote(value);
944
+ return _this7.quote(value);
936
945
  }
937
946
 
938
- return _this6.deparse(field, context);
947
+ return _this7.deparse(field, context);
939
948
  });
940
949
  return fields.join('.');
941
950
  }
@@ -1038,7 +1047,8 @@ var Deparser = /*#__PURE__*/function () {
1038
1047
  output.push(node.ctename);
1039
1048
 
1040
1049
  if (node.aliascolnames) {
1041
- output.push((0, _util.format)('(%s)', this.quote(this.deparseNodes(node.aliascolnames, context))));
1050
+ var colnames = this.quote(this.deparseNodes(node.aliascolnames, context));
1051
+ output.push("(".concat(colnames.join(', '), ")"));
1042
1052
  }
1043
1053
 
1044
1054
  output.push((0, _util.format)('AS (%s)', this.deparse(node.ctequery)));
@@ -1151,7 +1161,7 @@ var Deparser = /*#__PURE__*/function () {
1151
1161
  }, {
1152
1162
  key: 'DefElem',
1153
1163
  value: function DefElem(node) {
1154
- var _this7 = this;
1164
+ var _this8 = this;
1155
1165
 
1156
1166
  var context = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1157
1167
 
@@ -1226,7 +1236,7 @@ var Deparser = /*#__PURE__*/function () {
1226
1236
  {
1227
1237
  var output = [];
1228
1238
  node.arg.forEach(function (opt) {
1229
- output.push(_this7.quote(_this7.deparse(opt, context)));
1239
+ output.push(_this8.quote(_this8.deparse(opt, context)));
1230
1240
  });
1231
1241
  return "OWNED BY ".concat(output.join('.'));
1232
1242
  }
@@ -1248,6 +1258,10 @@ var Deparser = /*#__PURE__*/function () {
1248
1258
  }
1249
1259
 
1250
1260
  }
1261
+ } else if (context === 'explain') {
1262
+ if (node.arg) {
1263
+ return "".concat(name, " ").concat(this.deparse(node.arg));
1264
+ }
1251
1265
  } else if (node.arg) {
1252
1266
  return "".concat(name, " = ").concat(this.deparse(node.arg, context));
1253
1267
  }
@@ -1272,7 +1286,7 @@ var Deparser = /*#__PURE__*/function () {
1272
1286
  }, {
1273
1287
  key: 'FuncCall',
1274
1288
  value: function FuncCall(node) {
1275
- var _this8 = this;
1289
+ var _this9 = this;
1276
1290
 
1277
1291
  var context = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1278
1292
  var output = [];
@@ -1280,7 +1294,7 @@ var Deparser = /*#__PURE__*/function () {
1280
1294
 
1281
1295
  if (node.args) {
1282
1296
  params = node.args.map(function (item) {
1283
- return _this8.deparse(item, context);
1297
+ return _this9.deparse(item, context);
1284
1298
  });
1285
1299
  } // COUNT(*)
1286
1300
 
@@ -1446,10 +1460,13 @@ var Deparser = /*#__PURE__*/function () {
1446
1460
  }, {
1447
1461
  key: 'InsertStmt',
1448
1462
  value: function InsertStmt(node) {
1449
- var _this9 = this;
1450
-
1451
1463
  var context = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1452
1464
  var output = [];
1465
+
1466
+ if (node.withClause) {
1467
+ output.push(this.WithClause(node.withClause, context));
1468
+ }
1469
+
1453
1470
  output.push('INSERT INTO');
1454
1471
  output.push(this.RangeVar(node.relation, context));
1455
1472
 
@@ -1495,9 +1512,7 @@ var Deparser = /*#__PURE__*/function () {
1495
1512
 
1496
1513
  if (node.returningList) {
1497
1514
  output.push('RETURNING');
1498
- output.push(node.returningList.map(function (returning) {
1499
- return _this9.deparse(returning.ResTarget.val, context) + (returning.ResTarget.name ? ' AS ' + _this9.quote(returning.ResTarget.name) : '');
1500
- }).join(','));
1515
+ output.push(this.deparseReturningList(node.returningList, context));
1501
1516
  }
1502
1517
 
1503
1518
  return output.join(' ');
@@ -1520,15 +1535,30 @@ var Deparser = /*#__PURE__*/function () {
1520
1535
  value: function DeleteStmt(node) {
1521
1536
  var context = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1522
1537
  var output = [''];
1538
+
1539
+ if (node.withClause) {
1540
+ output.push(this.WithClause(node.withClause, context));
1541
+ }
1542
+
1523
1543
  output.push('DELETE');
1524
1544
  output.push('FROM');
1525
1545
  output.push(this.RangeVar(node.relation, context));
1526
1546
 
1547
+ if (node.usingClause) {
1548
+ output.push('USING');
1549
+ output.push(this.list(node.usingClause, ', ', '', context));
1550
+ }
1551
+
1527
1552
  if (node.whereClause) {
1528
1553
  output.push('WHERE');
1529
1554
  output.push(this.deparse(node.whereClause, context));
1530
1555
  }
1531
1556
 
1557
+ if (node.returningList) {
1558
+ output.push('RETURNING');
1559
+ output.push(this.deparseReturningList(node.returningList, context));
1560
+ }
1561
+
1532
1562
  return output.join(' ');
1533
1563
  }
1534
1564
  }, {
@@ -1538,6 +1568,11 @@ var Deparser = /*#__PURE__*/function () {
1538
1568
 
1539
1569
  var context = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1540
1570
  var output = [];
1571
+
1572
+ if (node.withClause) {
1573
+ output.push(this.WithClause(node.withClause, context));
1574
+ }
1575
+
1541
1576
  output.push('UPDATE');
1542
1577
 
1543
1578
  if (node.relation) {
@@ -1575,9 +1610,7 @@ var Deparser = /*#__PURE__*/function () {
1575
1610
 
1576
1611
  if (node.returningList) {
1577
1612
  output.push('RETURNING');
1578
- output.push(node.returningList.map(function (returning) {
1579
- return _this10.deparse(returning.ResTarget.val, context) + (returning.ResTarget.name ? ' AS ' + _this10.quote(returning.ResTarget.name) : '');
1580
- }).join(','));
1613
+ output.push(this.deparseReturningList(node.returningList, context));
1581
1614
  }
1582
1615
 
1583
1616
  return output.join(' ');
@@ -1712,15 +1745,11 @@ var Deparser = /*#__PURE__*/function () {
1712
1745
  }, {
1713
1746
  key: 'LockStmt',
1714
1747
  value: function LockStmt(node) {
1715
- var _this11 = this;
1716
-
1717
1748
  var context = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1718
1749
  var output = ['LOCK'];
1719
- output.push(node.relations.map(function (e) {
1720
- return _this11.deparse(e, {
1721
- lock: true
1722
- });
1723
- }).join(', '));
1750
+ output.push(this.list(node.relations, ', ', '', {
1751
+ lock: true
1752
+ }));
1724
1753
  output.push('IN');
1725
1754
  output.push(LOCK_MODES[node.mode]);
1726
1755
  output.push('MODE');
@@ -1882,7 +1911,7 @@ var Deparser = /*#__PURE__*/function () {
1882
1911
  output.push('ONLY');
1883
1912
  }
1884
1913
 
1885
- if (!node.inh && context.lock) {
1914
+ if (!node.inh && (context.lock || context === 'truncate')) {
1886
1915
  output.push('ONLY');
1887
1916
  }
1888
1917
 
@@ -1890,7 +1919,7 @@ var Deparser = /*#__PURE__*/function () {
1890
1919
  output.push('UNLOGGED');
1891
1920
  }
1892
1921
 
1893
- if (node.relpersistence === 't') {
1922
+ if (node.relpersistence === 't' && context !== 'view') {
1894
1923
  output.push('TEMPORARY TABLE');
1895
1924
  }
1896
1925
 
@@ -1938,13 +1967,20 @@ var Deparser = /*#__PURE__*/function () {
1938
1967
  var context = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1939
1968
  var output = [];
1940
1969
  output.push('EXPLAIN');
1970
+
1971
+ if (node.options) {
1972
+ output.push('(');
1973
+ output.push(this.list(node.options, ', ', '', 'explain'));
1974
+ output.push(')');
1975
+ }
1976
+
1941
1977
  output.push(this.deparse(node.query, context));
1942
1978
  return output.join(' ');
1943
1979
  }
1944
1980
  }, {
1945
1981
  key: 'SelectStmt',
1946
1982
  value: function SelectStmt(node) {
1947
- var _this12 = this;
1983
+ var _this11 = this;
1948
1984
 
1949
1985
  var context = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1950
1986
  var output = [];
@@ -1994,7 +2030,7 @@ var Deparser = /*#__PURE__*/function () {
1994
2030
  ) {
1995
2031
  output.push('DISTINCT ON');
1996
2032
  var clause = node.distinctClause.map(function (e) {
1997
- return _this12.deparse(e, 'select');
2033
+ return _this11.deparse(e, 'select');
1998
2034
  }).join(",".concat(NEWLINE_CHAR));
1999
2035
  output.push("(".concat(clause, ")"));
2000
2036
  } else {
@@ -2004,7 +2040,7 @@ var Deparser = /*#__PURE__*/function () {
2004
2040
 
2005
2041
  if (node.targetList) {
2006
2042
  output.push(indent(node.targetList.map(function (e) {
2007
- return _this12.deparse(e, 'select');
2043
+ return _this11.deparse(e, 'select');
2008
2044
  }).join(",".concat(NEWLINE_CHAR))));
2009
2045
  }
2010
2046
 
@@ -2016,7 +2052,7 @@ var Deparser = /*#__PURE__*/function () {
2016
2052
  if (node.fromClause) {
2017
2053
  output.push('FROM');
2018
2054
  output.push(indent(node.fromClause.map(function (e) {
2019
- return _this12.deparse(e, 'from');
2055
+ return _this11.deparse(e, 'from');
2020
2056
  }).join(",".concat(NEWLINE_CHAR))));
2021
2057
  }
2022
2058
 
@@ -2028,7 +2064,7 @@ var Deparser = /*#__PURE__*/function () {
2028
2064
  if (node.valuesLists) {
2029
2065
  output.push('VALUES');
2030
2066
  var lists = node.valuesLists.map(function (list) {
2031
- return "(".concat(_this12.list(list, ', ', '', context), ")");
2067
+ return "(".concat(_this11.list(list, ', ', '', context), ")");
2032
2068
  });
2033
2069
  output.push(lists.join(', '));
2034
2070
  }
@@ -2036,7 +2072,7 @@ var Deparser = /*#__PURE__*/function () {
2036
2072
  if (node.groupClause) {
2037
2073
  output.push('GROUP BY');
2038
2074
  output.push(indent(node.groupClause.map(function (e) {
2039
- return _this12.deparse(e, 'group');
2075
+ return _this11.deparse(e, 'group');
2040
2076
  }).join(",".concat(NEWLINE_CHAR))));
2041
2077
  }
2042
2078
 
@@ -2067,7 +2103,7 @@ var Deparser = /*#__PURE__*/function () {
2067
2103
  if (node.sortClause) {
2068
2104
  output.push('ORDER BY');
2069
2105
  output.push(indent(node.sortClause.map(function (e) {
2070
- return _this12.deparse(e, 'sort');
2106
+ return _this11.deparse(e, 'sort');
2071
2107
  }).join(",".concat(NEWLINE_CHAR))));
2072
2108
  }
2073
2109
 
@@ -2083,13 +2119,30 @@ var Deparser = /*#__PURE__*/function () {
2083
2119
 
2084
2120
  if (node.lockingClause) {
2085
2121
  node.lockingClause.forEach(function (item) {
2086
- return output.push(_this12.deparse(item, context));
2122
+ return output.push(_this11.deparse(item, context));
2087
2123
  });
2088
2124
  }
2089
2125
 
2090
2126
  return output.join(' ');
2091
2127
  }
2092
2128
  }, {
2129
+ key: 'TruncateStmt',
2130
+ value: function TruncateStmt(node) {
2131
+ var context = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
2132
+ var output = ['TRUNCATE TABLE'];
2133
+ output.push(this.list(node.relations, ', ', '', 'truncate'));
2134
+
2135
+ if (node.restart_seqs) {
2136
+ output.push('RESTART IDENTITY');
2137
+ }
2138
+
2139
+ if (node.behavior === 'DROP_CASCADE') {
2140
+ output.push('CASCADE');
2141
+ }
2142
+
2143
+ return output.join(' ');
2144
+ }
2145
+ }, {
2093
2146
  key: 'AlterDefaultPrivilegesStmt',
2094
2147
  value: function AlterDefaultPrivilegesStmt(node) {
2095
2148
  var context = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
@@ -2393,7 +2446,7 @@ var Deparser = /*#__PURE__*/function () {
2393
2446
  }, {
2394
2447
  key: 'CreateExtensionStmt',
2395
2448
  value: function CreateExtensionStmt(node) {
2396
- var _this13 = this;
2449
+ var _this12 = this;
2397
2450
 
2398
2451
  var output = [];
2399
2452
  output.push('CREATE EXTENSION');
@@ -2412,7 +2465,7 @@ var Deparser = /*#__PURE__*/function () {
2412
2465
 
2413
2466
  if (opt.DefElem.defname === 'schema') {
2414
2467
  output.push('WITH SCHEMA');
2415
- output.push(_this13.quote(_this13.deparse(opt.DefElem.arg)));
2468
+ output.push(_this12.quote(_this12.deparse(opt.DefElem.arg)));
2416
2469
  }
2417
2470
  });
2418
2471
  }
@@ -2612,16 +2665,35 @@ var Deparser = /*#__PURE__*/function () {
2612
2665
  var output = [];
2613
2666
  output.push('CREATE');
2614
2667
  if (node.replace) output.push('OR REPLACE');
2668
+
2669
+ if (node.view.relpersistence === 't') {
2670
+ output.push('TEMPORARY');
2671
+ }
2672
+
2615
2673
  output.push('VIEW');
2616
- output.push(this.RangeVar(node.view, context));
2674
+ output.push(this.RangeVar(node.view, 'view'));
2675
+
2676
+ if (node.aliases) {
2677
+ output.push('(');
2678
+ output.push(this.list(node.aliases, ', ', '', context));
2679
+ output.push(')');
2680
+ }
2681
+
2617
2682
  output.push('AS');
2618
2683
  output.push(this.deparse(node.query, context));
2684
+
2685
+ if (node.withCheckOption === 'LOCAL_CHECK_OPTION') {
2686
+ output.push('WITH LOCAL CHECK OPTION');
2687
+ } else if (node.withCheckOption === 'CASCADED_CHECK_OPTION') {
2688
+ output.push('WITH CASCADED CHECK OPTION');
2689
+ }
2690
+
2619
2691
  return output.join(' ');
2620
2692
  }
2621
2693
  }, {
2622
2694
  key: 'CreateSeqStmt',
2623
2695
  value: function CreateSeqStmt(node) {
2624
- var _this14 = this;
2696
+ var _this13 = this;
2625
2697
 
2626
2698
  var context = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
2627
2699
  var output = [];
@@ -2630,7 +2702,7 @@ var Deparser = /*#__PURE__*/function () {
2630
2702
 
2631
2703
  if (node.options && node.options.length) {
2632
2704
  node.options.forEach(function (opt) {
2633
- output.push(_this14.deparse(opt, 'sequence'));
2705
+ output.push(_this13.deparse(opt, 'sequence'));
2634
2706
  });
2635
2707
  }
2636
2708
 
@@ -2639,7 +2711,7 @@ var Deparser = /*#__PURE__*/function () {
2639
2711
  }, {
2640
2712
  key: 'AlterSeqStmt',
2641
2713
  value: function AlterSeqStmt(node) {
2642
- var _this15 = this;
2714
+ var _this14 = this;
2643
2715
 
2644
2716
  var context = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
2645
2717
  var output = [];
@@ -2648,7 +2720,7 @@ var Deparser = /*#__PURE__*/function () {
2648
2720
 
2649
2721
  if (node.options && node.options.length) {
2650
2722
  node.options.forEach(function (opt) {
2651
- output.push(_this15.deparse(opt, 'sequence'));
2723
+ output.push(_this14.deparse(opt, 'sequence'));
2652
2724
  });
2653
2725
  }
2654
2726
 
@@ -2658,8 +2730,19 @@ var Deparser = /*#__PURE__*/function () {
2658
2730
  key: 'CreateTableAsStmt',
2659
2731
  value: function CreateTableAsStmt(node) {
2660
2732
  var context = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
2661
- var output = [];
2662
- output.push('CREATE MATERIALIZED VIEW');
2733
+ var output = ['CREATE'];
2734
+ var relpersistence = dotty.get(node, 'into.rel.relpersistence');
2735
+
2736
+ if (node.relkind === 'OBJECT_MATVIEW') {
2737
+ output.push('MATERIALIZED VIEW');
2738
+ } else if (relpersistence !== 't') {
2739
+ output.push('TABLE');
2740
+
2741
+ if (node.if_not_exists) {
2742
+ output.push('IF NOT EXISTS');
2743
+ }
2744
+ }
2745
+
2663
2746
  output.push(this.IntoClause(node.into, context));
2664
2747
  output.push('AS');
2665
2748
  output.push(this.deparse(node.query, context));
@@ -2668,7 +2751,7 @@ var Deparser = /*#__PURE__*/function () {
2668
2751
  }, {
2669
2752
  key: 'CreateTrigStmt',
2670
2753
  value: function CreateTrigStmt(node) {
2671
- var _this16 = this;
2754
+ var _this15 = this;
2672
2755
 
2673
2756
  var context = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
2674
2757
  var output = [];
@@ -2787,7 +2870,7 @@ var Deparser = /*#__PURE__*/function () {
2787
2870
  return "'".concat(dotty.get(arg, 'String.str'), "'");
2788
2871
  }
2789
2872
 
2790
- return _this16.deparse(arg, context);
2873
+ return _this15.deparse(arg, context);
2791
2874
  }).filter(function (a) {
2792
2875
  return a;
2793
2876
  });
@@ -2833,7 +2916,7 @@ var Deparser = /*#__PURE__*/function () {
2833
2916
  output.push(this.list(node.tableElts, ",".concat(NEWLINE_CHAR), TAB_CHAR, context));
2834
2917
  output.push("".concat(NEWLINE_CHAR, ")"));
2835
2918
 
2836
- if (relpersistence === 'p' && node.hasOwnProperty('inhRelations')) {
2919
+ if (node.hasOwnProperty('inhRelations')) {
2837
2920
  output.push('INHERITS');
2838
2921
  output.push('(');
2839
2922
  output.push(this.list(node.inhRelations, ', ', '', context));
@@ -2950,17 +3033,17 @@ var Deparser = /*#__PURE__*/function () {
2950
3033
  var output = [];
2951
3034
 
2952
3035
  function getExclusionGroup(nde) {
2953
- var _this17 = this;
3036
+ var _this16 = this;
2954
3037
 
2955
3038
  var a = nde.exclusions.map(function (excl) {
2956
3039
  if (excl[0].IndexElem.name) {
2957
3040
  return excl[0].IndexElem.name;
2958
3041
  }
2959
3042
 
2960
- return excl[0].IndexElem.expr ? _this17.deparse(excl[0].IndexElem.expr, context) : null;
3043
+ return excl[0].IndexElem.expr ? _this16.deparse(excl[0].IndexElem.expr, context) : null;
2961
3044
  });
2962
3045
  var b = nde.exclusions.map(function (excl) {
2963
- return _this17.deparse(excl[1][0], context);
3046
+ return _this16.deparse(excl[1][0], context);
2964
3047
  });
2965
3048
  var stmts = a.map(function (_v, i) {
2966
3049
  return "".concat(a[i], " WITH ").concat(b[i]);
@@ -3178,7 +3261,7 @@ var Deparser = /*#__PURE__*/function () {
3178
3261
  }, {
3179
3262
  key: 'CreateFunctionStmt',
3180
3263
  value: function CreateFunctionStmt(node) {
3181
- var _this18 = this;
3264
+ var _this17 = this;
3182
3265
 
3183
3266
  var context = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
3184
3267
  var output = [];
@@ -3190,7 +3273,7 @@ var Deparser = /*#__PURE__*/function () {
3190
3273
 
3191
3274
  output.push('FUNCTION');
3192
3275
  output.push(node.funcname.map(function (name) {
3193
- return _this18.deparse(name, context);
3276
+ return _this17.deparse(name, context);
3194
3277
  }).join('.'));
3195
3278
  output.push('(');
3196
3279
  var parameters = [];
@@ -3227,12 +3310,12 @@ var Deparser = /*#__PURE__*/function () {
3227
3310
 
3228
3311
  switch (option.DefElem.defname) {
3229
3312
  case 'as':
3230
- value = _this18.deparse(option.DefElem.arg[0], context);
3313
+ value = _this17.deparse(option.DefElem.arg[0], context);
3231
3314
  output.push("AS $EOFCODE$".concat(value, "$EOFCODE$"));
3232
3315
  break;
3233
3316
 
3234
3317
  case 'language':
3235
- value = _this18.deparse(option.DefElem.arg, context);
3318
+ value = _this17.deparse(option.DefElem.arg, context);
3236
3319
  output.push('LANGUAGE');
3237
3320
  output.push(value);
3238
3321
  break;
@@ -3279,11 +3362,11 @@ var Deparser = /*#__PURE__*/function () {
3279
3362
  break;
3280
3363
 
3281
3364
  case 'set':
3282
- output.push(_this18.deparse(option, context));
3365
+ output.push(_this17.deparse(option, context));
3283
3366
  break;
3284
3367
 
3285
3368
  case 'volatility':
3286
- value = _this18.deparse(option.DefElem.arg, context);
3369
+ value = _this17.deparse(option.DefElem.arg, context);
3287
3370
  output.push(value.toUpperCase());
3288
3371
  break;
3289
3372
 
@@ -3480,7 +3563,7 @@ var Deparser = /*#__PURE__*/function () {
3480
3563
  }, {
3481
3564
  key: 'CreateRoleStmt',
3482
3565
  value: function CreateRoleStmt(node) {
3483
- var _this19 = this;
3566
+ var _this18 = this;
3484
3567
 
3485
3568
  var context = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
3486
3569
  var output = [];
@@ -3541,12 +3624,12 @@ var Deparser = /*#__PURE__*/function () {
3541
3624
 
3542
3625
  case 'adminmembers':
3543
3626
  output.push('ADMIN');
3544
- output.push(_this19.list(node.options[i].DefElem.arg, ', ', '', context));
3627
+ output.push(_this18.list(node.options[i].DefElem.arg, ', ', '', context));
3545
3628
  break;
3546
3629
 
3547
3630
  case 'rolemembers':
3548
3631
  output.push('USER');
3549
- output.push(_this19.list(node.options[i].DefElem.arg, ', ', '', context));
3632
+ output.push(_this18.list(node.options[i].DefElem.arg, ', ', '', context));
3550
3633
  break;
3551
3634
 
3552
3635
  case 'createdb':
@@ -3589,7 +3672,7 @@ var Deparser = /*#__PURE__*/function () {
3589
3672
  }, {
3590
3673
  key: 'TransactionStmt',
3591
3674
  value: function TransactionStmt(node) {
3592
- var _this20 = this;
3675
+ var _this19 = this;
3593
3676
 
3594
3677
  var context = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
3595
3678
  var output = [];
@@ -3601,7 +3684,7 @@ var Deparser = /*#__PURE__*/function () {
3601
3684
  var index = opts.indexOf('transaction_read_only');
3602
3685
  var obj = nodeOpts.options[index];
3603
3686
  var set = false;
3604
- var flag = Number(_this20.deparse(dotty.get(obj, 'DefElem.arg'), context));
3687
+ var flag = Number(_this19.deparse(dotty.get(obj, 'DefElem.arg'), context));
3605
3688
 
3606
3689
  if (flag > 0) {
3607
3690
  set = true;
@@ -3619,7 +3702,7 @@ var Deparser = /*#__PURE__*/function () {
3619
3702
 
3620
3703
  var _obj = nodeOpts.options[_index];
3621
3704
 
3622
- var lopts = _this20.deparse(dotty.get(_obj, 'DefElem.arg'), context).replace(/['"]+/g, '');
3705
+ var lopts = _this19.deparse(dotty.get(_obj, 'DefElem.arg'), context).replace(/['"]+/g, '');
3623
3706
 
3624
3707
  return "BEGIN TRANSACTION ISOLATION LEVEL ".concat(lopts.toUpperCase());
3625
3708
  }
@@ -3634,7 +3717,7 @@ var Deparser = /*#__PURE__*/function () {
3634
3717
  var index = opts.indexOf('transaction_read_only');
3635
3718
  var obj = nodeOpts.options[index];
3636
3719
  var set = false;
3637
- var flag = Number(_this20.deparse(dotty.get(obj, 'DefElem.arg'), context));
3720
+ var flag = Number(_this19.deparse(dotty.get(obj, 'DefElem.arg'), context));
3638
3721
 
3639
3722
  if (flag > 0) {
3640
3723
  set = true;
@@ -3738,7 +3821,7 @@ var Deparser = /*#__PURE__*/function () {
3738
3821
  }, {
3739
3822
  key: 'ObjectWithArgs',
3740
3823
  value: function ObjectWithArgs(node) {
3741
- var _this21 = this;
3824
+ var _this20 = this;
3742
3825
 
3743
3826
  var context = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
3744
3827
  var output = [];
@@ -3756,7 +3839,7 @@ var Deparser = /*#__PURE__*/function () {
3756
3839
  return 'NONE';
3757
3840
  }
3758
3841
 
3759
- return _this21.deparse(arg, context);
3842
+ return _this20.deparse(arg, context);
3760
3843
  }).join(','));
3761
3844
  output.push(')');
3762
3845
  } else if (!node.args_unspecified) {
@@ -3835,7 +3918,7 @@ var Deparser = /*#__PURE__*/function () {
3835
3918
  }, {
3836
3919
  key: 'TypeName',
3837
3920
  value: function TypeName(node) {
3838
- var _this22 = this;
3921
+ var _this21 = this;
3839
3922
 
3840
3923
  var context = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
3841
3924
 
@@ -3853,7 +3936,7 @@ var Deparser = /*#__PURE__*/function () {
3853
3936
 
3854
3937
  if (node.typmods != null) {
3855
3938
  args = node.typmods.map(function (item) {
3856
- return _this22.deparse(item, context);
3939
+ return _this21.deparse(item, context);
3857
3940
  });
3858
3941
  }
3859
3942
 
@@ -3880,7 +3963,7 @@ var Deparser = /*#__PURE__*/function () {
3880
3963
  }, {
3881
3964
  key: 'WindowDef',
3882
3965
  value: function WindowDef(node) {
3883
- var _this23 = this;
3966
+ var _this22 = this;
3884
3967
 
3885
3968
  var context = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
3886
3969
  var output = [];
@@ -3904,7 +3987,7 @@ var Deparser = /*#__PURE__*/function () {
3904
3987
  if (node.partitionClause) {
3905
3988
  var partition = ['PARTITION BY'];
3906
3989
  var clause = node.partitionClause.map(function (item) {
3907
- return _this23.deparse(item, context);
3990
+ return _this22.deparse(item, context);
3908
3991
  });
3909
3992
  partition.push(clause.join(', '));
3910
3993
  windowParts.push(partition.join(' '));
@@ -3914,7 +3997,7 @@ var Deparser = /*#__PURE__*/function () {
3914
3997
  if (node.orderClause) {
3915
3998
  windowParts.push('ORDER BY');
3916
3999
  var orders = node.orderClause.map(function (item) {
3917
- return _this23.deparse(item);
4000
+ return _this22.deparse(item);
3918
4001
  });
3919
4002
  windowParts.push(orders.join(', '));
3920
4003
  useParens = true;
@@ -4121,7 +4204,7 @@ var Deparser = /*#__PURE__*/function () {
4121
4204
  }, {
4122
4205
  key: "deparseInterval",
4123
4206
  value: function deparseInterval(node) {
4124
- var _this24 = this;
4207
+ var _this23 = this;
4125
4208
 
4126
4209
  var type = ['interval'];
4127
4210
 
@@ -4131,7 +4214,7 @@ var Deparser = /*#__PURE__*/function () {
4131
4214
 
4132
4215
  if (node.typmods) {
4133
4216
  var typmods = node.typmods.map(function (item) {
4134
- return _this24.deparse(item);
4217
+ return _this23.deparse(item);
4135
4218
  });
4136
4219
  var intervals = this.interval(typmods[0]); // SELECT interval(0) '1 day 01:23:45.6789'
4137
4220
 
@@ -90,6 +90,10 @@ export default class Deparser {
90
90
  });
91
91
  }
92
92
 
93
+ deparseReturningList(list, context) {
94
+ return list.map(returning => this.deparse(returning.ResTarget.val, context) + (returning.ResTarget.name ? ' AS ' + this.quote(returning.ResTarget.name) : '')).join(',');
95
+ }
96
+
93
97
  list(nodes, separator = ', ', prefix = '', context) {
94
98
  if (!nodes) {
95
99
  return '';
@@ -267,15 +271,15 @@ export default class Deparser {
267
271
  output.push('TO');
268
272
  output.push(this.RangeVar(node.relation, context));
269
273
 
270
- if (node.instead) {
271
- output.push('DO');
272
- output.push('INSTEAD');
273
- }
274
-
275
274
  if (node.whereClause) {
276
275
  output.push('WHERE');
277
276
  output.push(this.deparse(node.whereClause, context));
278
- output.push('DO');
277
+ }
278
+
279
+ output.push('DO');
280
+
281
+ if (node.instead) {
282
+ output.push('INSTEAD');
279
283
  }
280
284
 
281
285
  if (!node.actions || !node.actions.length) {
@@ -924,7 +928,8 @@ export default class Deparser {
924
928
  output.push(node.ctename);
925
929
 
926
930
  if (node.aliascolnames) {
927
- output.push(format('(%s)', this.quote(this.deparseNodes(node.aliascolnames, context))));
931
+ const colnames = this.quote(this.deparseNodes(node.aliascolnames, context));
932
+ output.push(`(${colnames.join(', ')})`);
928
933
  }
929
934
 
930
935
  output.push(format('AS (%s)', this.deparse(node.ctequery)));
@@ -1126,6 +1131,10 @@ export default class Deparser {
1126
1131
  }
1127
1132
 
1128
1133
  }
1134
+ } else if (context === 'explain') {
1135
+ if (node.arg) {
1136
+ return `${name} ${this.deparse(node.arg)}`;
1137
+ }
1129
1138
  } else if (node.arg) {
1130
1139
  return `${name} = ${this.deparse(node.arg, context)}`;
1131
1140
  }
@@ -1309,6 +1318,11 @@ export default class Deparser {
1309
1318
 
1310
1319
  ['InsertStmt'](node, context = {}) {
1311
1320
  const output = [];
1321
+
1322
+ if (node.withClause) {
1323
+ output.push(this.WithClause(node.withClause, context));
1324
+ }
1325
+
1312
1326
  output.push('INSERT INTO');
1313
1327
  output.push(this.RangeVar(node.relation, context));
1314
1328
 
@@ -1354,7 +1368,7 @@ export default class Deparser {
1354
1368
 
1355
1369
  if (node.returningList) {
1356
1370
  output.push('RETURNING');
1357
- output.push(node.returningList.map(returning => this.deparse(returning.ResTarget.val, context) + (returning.ResTarget.name ? ' AS ' + this.quote(returning.ResTarget.name) : '')).join(','));
1371
+ output.push(this.deparseReturningList(node.returningList, context));
1358
1372
  }
1359
1373
 
1360
1374
  return output.join(' ');
@@ -1372,20 +1386,40 @@ export default class Deparser {
1372
1386
 
1373
1387
  ['DeleteStmt'](node, context = {}) {
1374
1388
  const output = [''];
1389
+
1390
+ if (node.withClause) {
1391
+ output.push(this.WithClause(node.withClause, context));
1392
+ }
1393
+
1375
1394
  output.push('DELETE');
1376
1395
  output.push('FROM');
1377
1396
  output.push(this.RangeVar(node.relation, context));
1378
1397
 
1398
+ if (node.usingClause) {
1399
+ output.push('USING');
1400
+ output.push(this.list(node.usingClause, ', ', '', context));
1401
+ }
1402
+
1379
1403
  if (node.whereClause) {
1380
1404
  output.push('WHERE');
1381
1405
  output.push(this.deparse(node.whereClause, context));
1382
1406
  }
1383
1407
 
1408
+ if (node.returningList) {
1409
+ output.push('RETURNING');
1410
+ output.push(this.deparseReturningList(node.returningList, context));
1411
+ }
1412
+
1384
1413
  return output.join(' ');
1385
1414
  }
1386
1415
 
1387
1416
  ['UpdateStmt'](node, context = {}) {
1388
1417
  const output = [];
1418
+
1419
+ if (node.withClause) {
1420
+ output.push(this.WithClause(node.withClause, context));
1421
+ }
1422
+
1389
1423
  output.push('UPDATE');
1390
1424
 
1391
1425
  if (node.relation) {
@@ -1419,7 +1453,7 @@ export default class Deparser {
1419
1453
 
1420
1454
  if (node.returningList) {
1421
1455
  output.push('RETURNING');
1422
- output.push(node.returningList.map(returning => this.deparse(returning.ResTarget.val, context) + (returning.ResTarget.name ? ' AS ' + this.quote(returning.ResTarget.name) : '')).join(','));
1456
+ output.push(this.deparseReturningList(node.returningList, context));
1423
1457
  }
1424
1458
 
1425
1459
  return output.join(' ');
@@ -1545,9 +1579,9 @@ export default class Deparser {
1545
1579
 
1546
1580
  ['LockStmt'](node, context = {}) {
1547
1581
  const output = ['LOCK'];
1548
- output.push(node.relations.map(e => this.deparse(e, {
1582
+ output.push(this.list(node.relations, ', ', '', {
1549
1583
  lock: true
1550
- })).join(', '));
1584
+ }));
1551
1585
  output.push('IN');
1552
1586
  output.push(LOCK_MODES[node.mode]);
1553
1587
  output.push('MODE');
@@ -1693,7 +1727,7 @@ export default class Deparser {
1693
1727
  output.push('ONLY');
1694
1728
  }
1695
1729
 
1696
- if (!node.inh && context.lock) {
1730
+ if (!node.inh && (context.lock || context === 'truncate')) {
1697
1731
  output.push('ONLY');
1698
1732
  }
1699
1733
 
@@ -1701,7 +1735,7 @@ export default class Deparser {
1701
1735
  output.push('UNLOGGED');
1702
1736
  }
1703
1737
 
1704
- if (node.relpersistence === 't') {
1738
+ if (node.relpersistence === 't' && context !== 'view') {
1705
1739
  output.push('TEMPORARY TABLE');
1706
1740
  }
1707
1741
 
@@ -1741,6 +1775,13 @@ export default class Deparser {
1741
1775
  ['ExplainStmt'](node, context = {}) {
1742
1776
  const output = [];
1743
1777
  output.push('EXPLAIN');
1778
+
1779
+ if (node.options) {
1780
+ output.push('(');
1781
+ output.push(this.list(node.options, ', ', '', 'explain'));
1782
+ output.push(')');
1783
+ }
1784
+
1744
1785
  output.push(this.deparse(node.query, context));
1745
1786
  return output.join(' ');
1746
1787
  }
@@ -1879,6 +1920,21 @@ export default class Deparser {
1879
1920
  return output.join(' ');
1880
1921
  }
1881
1922
 
1923
+ ['TruncateStmt'](node, context = {}) {
1924
+ const output = ['TRUNCATE TABLE'];
1925
+ output.push(this.list(node.relations, ', ', '', 'truncate'));
1926
+
1927
+ if (node.restart_seqs) {
1928
+ output.push('RESTART IDENTITY');
1929
+ }
1930
+
1931
+ if (node.behavior === 'DROP_CASCADE') {
1932
+ output.push('CASCADE');
1933
+ }
1934
+
1935
+ return output.join(' ');
1936
+ }
1937
+
1882
1938
  ['AlterDefaultPrivilegesStmt'](node, context = {}) {
1883
1939
  const output = [];
1884
1940
  output.push('ALTER DEFAULT PRIVILEGES');
@@ -2375,10 +2431,29 @@ export default class Deparser {
2375
2431
  const output = [];
2376
2432
  output.push('CREATE');
2377
2433
  if (node.replace) output.push('OR REPLACE');
2434
+
2435
+ if (node.view.relpersistence === 't') {
2436
+ output.push('TEMPORARY');
2437
+ }
2438
+
2378
2439
  output.push('VIEW');
2379
- output.push(this.RangeVar(node.view, context));
2440
+ output.push(this.RangeVar(node.view, 'view'));
2441
+
2442
+ if (node.aliases) {
2443
+ output.push('(');
2444
+ output.push(this.list(node.aliases, ', ', '', context));
2445
+ output.push(')');
2446
+ }
2447
+
2380
2448
  output.push('AS');
2381
2449
  output.push(this.deparse(node.query, context));
2450
+
2451
+ if (node.withCheckOption === 'LOCAL_CHECK_OPTION') {
2452
+ output.push('WITH LOCAL CHECK OPTION');
2453
+ } else if (node.withCheckOption === 'CASCADED_CHECK_OPTION') {
2454
+ output.push('WITH CASCADED CHECK OPTION');
2455
+ }
2456
+
2382
2457
  return output.join(' ');
2383
2458
  }
2384
2459
 
@@ -2411,8 +2486,19 @@ export default class Deparser {
2411
2486
  }
2412
2487
 
2413
2488
  ['CreateTableAsStmt'](node, context = {}) {
2414
- const output = [];
2415
- output.push('CREATE MATERIALIZED VIEW');
2489
+ const output = ['CREATE'];
2490
+ const relpersistence = dotty.get(node, 'into.rel.relpersistence');
2491
+
2492
+ if (node.relkind === 'OBJECT_MATVIEW') {
2493
+ output.push('MATERIALIZED VIEW');
2494
+ } else if (relpersistence !== 't') {
2495
+ output.push('TABLE');
2496
+
2497
+ if (node.if_not_exists) {
2498
+ output.push('IF NOT EXISTS');
2499
+ }
2500
+ }
2501
+
2416
2502
  output.push(this.IntoClause(node.into, context));
2417
2503
  output.push('AS');
2418
2504
  output.push(this.deparse(node.query, context));
@@ -2576,7 +2662,7 @@ export default class Deparser {
2576
2662
  output.push(this.list(node.tableElts, `,${NEWLINE_CHAR}`, TAB_CHAR, context));
2577
2663
  output.push(`${NEWLINE_CHAR})`);
2578
2664
 
2579
- if (relpersistence === 'p' && node.hasOwnProperty('inhRelations')) {
2665
+ if (node.hasOwnProperty('inhRelations')) {
2580
2666
  output.push('INHERITS');
2581
2667
  output.push('(');
2582
2668
  output.push(this.list(node.inhRelations, ', ', '', context));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pgsql-deparser",
3
- "version": "13.3.11",
3
+ "version": "13.3.12",
4
4
  "description": "PostgreSQL AST Deparser",
5
5
  "author": "Dan Lynch <pyramation@gmail.com>",
6
6
  "homepage": "https://github.com/pyramation/pgsql-parser",
@@ -65,7 +65,7 @@
65
65
  "eslint-plugin-prettier": "^3.1.2",
66
66
  "glob": "7.1.6",
67
67
  "jest": "^25.1.0",
68
- "pgsql-parser": "^13.3.11",
68
+ "pgsql-parser": "^13.3.12",
69
69
  "prettier": "^2.1.2",
70
70
  "regenerator-runtime": "^0.13.2"
71
71
  },
@@ -73,7 +73,7 @@
73
73
  "@babel/runtime": "^7.11.2",
74
74
  "dotty": "^0.1.0",
75
75
  "lodash": "^4.17.20",
76
- "pgsql-enums": "^13.1.2"
76
+ "pgsql-enums": "^13.1.3"
77
77
  },
78
- "gitHead": "3000794fa5f3a0e3e0d3a05cb1cee76f99124557"
78
+ "gitHead": "b6691acf4c2c12f1285ea04f60603cf3aceee95e"
79
79
  }