@polyglot-sql/sdk 0.5.2 → 0.5.4

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/dist/manual.js CHANGED
@@ -1824,6 +1824,30 @@ function ast_get_table_names(ast_json) {
1824
1824
  }
1825
1825
  }
1826
1826
 
1827
+ /**
1828
+ * Get all table nodes from an AST (as JSON string).
1829
+ * @param {string} ast_json
1830
+ * @returns {string}
1831
+ */
1832
+ function ast_get_tables(ast_json) {
1833
+ let deferred2_0;
1834
+ let deferred2_1;
1835
+ try {
1836
+ const retptr = wasm$2.__wbindgen_add_to_stack_pointer(-16);
1837
+ const ptr0 = passStringToWasm0(ast_json, wasm$2.__wbindgen_export, wasm$2.__wbindgen_export2);
1838
+ const len0 = WASM_VECTOR_LEN;
1839
+ wasm$2.ast_get_tables(retptr, ptr0, len0);
1840
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
1841
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
1842
+ deferred2_0 = r0;
1843
+ deferred2_1 = r1;
1844
+ return getStringFromWasm0(r0, r1);
1845
+ } finally {
1846
+ wasm$2.__wbindgen_add_to_stack_pointer(16);
1847
+ wasm$2.__wbindgen_export4(deferred2_0, deferred2_1, 1);
1848
+ }
1849
+ }
1850
+
1827
1851
  /**
1828
1852
  * Get all window functions from an AST (as JSON string).
1829
1853
  * @param {string} ast_json
@@ -2084,6 +2108,58 @@ function ast_set_limit(ast_json, limit) {
2084
2108
  }
2085
2109
  }
2086
2110
 
2111
+ /**
2112
+ * Set the OFFSET on a SELECT or set-operation AST.
2113
+ * @param {string} ast_json
2114
+ * @param {number} offset
2115
+ * @returns {string}
2116
+ */
2117
+ function ast_set_offset(ast_json, offset) {
2118
+ let deferred2_0;
2119
+ let deferred2_1;
2120
+ try {
2121
+ const retptr = wasm$2.__wbindgen_add_to_stack_pointer(-16);
2122
+ const ptr0 = passStringToWasm0(ast_json, wasm$2.__wbindgen_export, wasm$2.__wbindgen_export2);
2123
+ const len0 = WASM_VECTOR_LEN;
2124
+ wasm$2.ast_set_offset(retptr, ptr0, len0, offset);
2125
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
2126
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
2127
+ deferred2_0 = r0;
2128
+ deferred2_1 = r1;
2129
+ return getStringFromWasm0(r0, r1);
2130
+ } finally {
2131
+ wasm$2.__wbindgen_add_to_stack_pointer(16);
2132
+ wasm$2.__wbindgen_export4(deferred2_0, deferred2_1, 1);
2133
+ }
2134
+ }
2135
+
2136
+ /**
2137
+ * Set the ORDER BY clause on a SELECT or set-operation AST.
2138
+ * @param {string} ast_json
2139
+ * @param {string} order_by_json
2140
+ * @returns {string}
2141
+ */
2142
+ function ast_set_order_by(ast_json, order_by_json) {
2143
+ let deferred3_0;
2144
+ let deferred3_1;
2145
+ try {
2146
+ const retptr = wasm$2.__wbindgen_add_to_stack_pointer(-16);
2147
+ const ptr0 = passStringToWasm0(ast_json, wasm$2.__wbindgen_export, wasm$2.__wbindgen_export2);
2148
+ const len0 = WASM_VECTOR_LEN;
2149
+ const ptr1 = passStringToWasm0(order_by_json, wasm$2.__wbindgen_export, wasm$2.__wbindgen_export2);
2150
+ const len1 = WASM_VECTOR_LEN;
2151
+ wasm$2.ast_set_order_by(retptr, ptr0, len0, ptr1, len1);
2152
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
2153
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
2154
+ deferred3_0 = r0;
2155
+ deferred3_1 = r1;
2156
+ return getStringFromWasm0(r0, r1);
2157
+ } finally {
2158
+ wasm$2.__wbindgen_add_to_stack_pointer(16);
2159
+ wasm$2.__wbindgen_export4(deferred3_0, deferred3_1, 1);
2160
+ }
2161
+ }
2162
+
2087
2163
  /**
2088
2164
  * Diff two SQL statements and return edit operations.
2089
2165
  *
@@ -3271,10 +3347,6 @@ function __wbg_get_imports() {
3271
3347
  const ret = getObject(arg0).call(getObject(arg1));
3272
3348
  return addHeapObject(ret);
3273
3349
  }, arguments); },
3274
- __wbg_codePointAt_bf59dbf74d8db275: function(arg0, arg1) {
3275
- const ret = getObject(arg0).codePointAt(arg1 >>> 0);
3276
- return addHeapObject(ret);
3277
- },
3278
3350
  __wbg_done_57b39ecd9addfe81: function(arg0) {
3279
3351
  const ret = getObject(arg0).done;
3280
3352
  return ret;
@@ -3360,10 +3432,6 @@ function __wbg_get_imports() {
3360
3432
  const ret = getObject(arg0).length;
3361
3433
  return ret;
3362
3434
  },
3363
- __wbg_length_68dc7c5cf1b6d349: function(arg0) {
3364
- const ret = getObject(arg0).length;
3365
- return ret;
3366
- },
3367
3435
  __wbg_new_361308b2356cecd0: function() {
3368
3436
  const ret = new Object();
3369
3437
  return addHeapObject(ret);
@@ -3827,6 +3895,7 @@ const wasmModule = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.defineProperty
3827
3895
  ast_get_literals,
3828
3896
  ast_get_subqueries,
3829
3897
  ast_get_table_names,
3898
+ ast_get_tables,
3830
3899
  ast_get_window_functions,
3831
3900
  ast_node_count,
3832
3901
  ast_qualify_columns,
@@ -3837,6 +3906,8 @@ const wasmModule = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.defineProperty
3837
3906
  ast_rename_tables_with_options,
3838
3907
  ast_set_distinct,
3839
3908
  ast_set_limit,
3909
+ ast_set_offset,
3910
+ ast_set_order_by,
3840
3911
  diff_sql,
3841
3912
  format_sql,
3842
3913
  format_sql_value,
@@ -4026,6 +4097,69 @@ function parseAstResult(json) {
4026
4097
  const result = JSON.parse(json);
4027
4098
  return result.success ? JSON.parse(result.ast) : null;
4028
4099
  }
4100
+ function isQueryWithOuterClauses(node) {
4101
+ return ["select", "union", "intersect", "except"].includes(getExprType(node));
4102
+ }
4103
+ function applyLimitExpr(node, limit) {
4104
+ const type = getExprType(node);
4105
+ if (!isQueryWithOuterClauses(node)) {
4106
+ return node;
4107
+ }
4108
+ const data = getExprData(node);
4109
+ if (type === "select") {
4110
+ return makeExpr("select", {
4111
+ ...data,
4112
+ limit: { this: limit, percent: false, comments: [] }
4113
+ });
4114
+ }
4115
+ return makeExpr(type, {
4116
+ ...data,
4117
+ limit
4118
+ });
4119
+ }
4120
+ function applyOffsetExpr(node, offset) {
4121
+ const type = getExprType(node);
4122
+ if (!isQueryWithOuterClauses(node)) {
4123
+ return node;
4124
+ }
4125
+ const data = getExprData(node);
4126
+ if (type === "select") {
4127
+ return makeExpr("select", {
4128
+ ...data,
4129
+ offset: { this: offset, rows: null }
4130
+ });
4131
+ }
4132
+ return makeExpr(type, {
4133
+ ...data,
4134
+ offset
4135
+ });
4136
+ }
4137
+ function applyOrderBy(node, orderBy) {
4138
+ const type = getExprType(node);
4139
+ if (!isQueryWithOuterClauses(node)) {
4140
+ return node;
4141
+ }
4142
+ const expressions = orderBy.map((expression) => {
4143
+ if (getExprType(expression) === "ordered") {
4144
+ return getExprData(expression);
4145
+ }
4146
+ return {
4147
+ this: expression,
4148
+ desc: false,
4149
+ nulls_first: null,
4150
+ explicit_asc: false,
4151
+ with_fill: null
4152
+ };
4153
+ });
4154
+ return makeExpr(type, {
4155
+ ...getExprData(node),
4156
+ order_by: {
4157
+ expressions,
4158
+ siblings: false,
4159
+ comments: []
4160
+ }
4161
+ });
4162
+ }
4029
4163
  function transformValue(value, config, parent) {
4030
4164
  if (value === null || value === void 0) return value;
4031
4165
  if (Array.isArray(value)) {
@@ -4195,25 +4329,21 @@ function setLimit(node, limit) {
4195
4329
  const result = parseAstResult(ast_set_limit(exprToJson$1(node), limit));
4196
4330
  return result ?? node;
4197
4331
  }
4198
- if (getExprType(node) !== "select") {
4199
- return node;
4200
- }
4201
- const selectData = getExprData(node);
4202
- return makeExpr("select", {
4203
- ...selectData,
4204
- limit: { this: limit }
4205
- });
4332
+ return applyLimitExpr(node, limit);
4206
4333
  }
4207
4334
  function setOffset(node, offset) {
4208
- if (getExprType(node) !== "select") {
4209
- return node;
4335
+ if (typeof offset === "number") {
4336
+ const result = parseAstResult(ast_set_offset(exprToJson$1(node), offset));
4337
+ return result ?? node;
4210
4338
  }
4211
- const selectData = getExprData(node);
4212
- const offsetExpr = typeof offset === "number" ? makeExpr("literal", { literal_type: "number", value: String(offset) }) : offset;
4213
- return makeExpr("select", {
4214
- ...selectData,
4215
- offset: { this: offsetExpr }
4216
- });
4339
+ return applyOffsetExpr(node, offset);
4340
+ }
4341
+ function setOrderBy(node, orderBy) {
4342
+ const orderByExpressions = Array.isArray(orderBy) ? orderBy : [orderBy];
4343
+ const result = parseAstResult(
4344
+ ast_set_order_by(exprToJson$1(node), JSON.stringify(orderByExpressions))
4345
+ );
4346
+ return result ?? applyOrderBy(node, orderByExpressions);
4217
4347
  }
4218
4348
  function removeLimitOffset(node) {
4219
4349
  if (getExprType(node) !== "select") {
@@ -4381,7 +4511,8 @@ function getColumns(node) {
4381
4511
  return findByType(node, "column");
4382
4512
  }
4383
4513
  function getTables(node) {
4384
- return findByType(node, "table");
4514
+ const result = JSON.parse(ast_get_tables(exprToJson(node)));
4515
+ return result.success ? JSON.parse(result.ast) : [];
4385
4516
  }
4386
4517
  function getIdentifiers(node) {
4387
4518
  return findByType(node, "identifier");
@@ -4615,6 +4746,7 @@ const index = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.defineProperty({
4615
4746
  setDistinct,
4616
4747
  setLimit,
4617
4748
  setOffset,
4749
+ setOrderBy,
4618
4750
  some,
4619
4751
  transform,
4620
4752
  walk
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polyglot-sql/sdk",
3
- "version": "0.5.2",
3
+ "version": "0.5.4",
4
4
  "description": "SQL dialect translator powered by WebAssembly",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",