@saltcorn/sql 0.3.8 → 0.4.0

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/index.js CHANGED
@@ -144,10 +144,13 @@ module.exports = {
144
144
  await client.query(
145
145
  `SET LOCAL search_path TO "${db.getTenantSchema()}";`
146
146
  );
147
+ await client.query(
148
+ `SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY;`
149
+ );
147
150
  }
148
151
  const qres = await db.query(query, parameters || []);
149
152
 
150
- await client.query(`COMMIT;`);
153
+ await client.query(`ROLLBACK;`);
151
154
  return qres;
152
155
  },
153
156
  isAsync: true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saltcorn/sql",
3
- "version": "0.3.8",
3
+ "version": "0.4.0",
4
4
  "description": "Actions and views based on SQL",
5
5
  "main": "index.js",
6
6
  "dependencies": {
package/table-provider.js CHANGED
@@ -185,10 +185,24 @@ const runQuery = async (cfg, where, opts) => {
185
185
  //console.log(ast[0].columns);
186
186
  for (const k of Object.keys(where)) {
187
187
  if (!colNames.has(k)) continue;
188
- const sqlCol = (ast[0].columns || []).find(
189
- (c) => k === c.as || (!c.as && k === c.expr?.column)
190
- );
191
- const sqlExprCol = (ast[0].columns || []).find((c) => c.expr?.as == k);
188
+ const sqlCol =
189
+ ast[0].columns == "*"
190
+ ? {
191
+ type: "expr",
192
+ expr: { type: "column_ref", table: null, column: k },
193
+ as: null,
194
+ }
195
+ : (ast[0].columns || []).find(
196
+ (c) => k === c.as || (!c.as && k === c.expr?.column)
197
+ );
198
+ const sqlExprCol =
199
+ ast[0].columns == "*"
200
+ ? {
201
+ type: "expr",
202
+ expr: { type: "column_ref", table: null, column: k },
203
+ as: null,
204
+ }
205
+ : (ast[0].columns || []).find((c) => c.expr?.as == k);
192
206
  let left = sqlExprCol
193
207
  ? { ...sqlExprCol.expr, as: null }
194
208
  : {
@@ -232,13 +246,68 @@ const runQuery = async (cfg, where, opts) => {
232
246
  { type: "number", value: where.offset },
233
247
  ],
234
248
  };
249
+ } else if (opts?.limit && opts?.offset) {
250
+ ast[0].limit = {
251
+ seperator: "offset",
252
+ value: [
253
+ { type: "number", value: opts.limit },
254
+ { type: "number", value: opts.offset },
255
+ ],
256
+ };
235
257
  } else if (where?.limit) {
236
258
  ast[0].limit = {
237
259
  seperator: "",
238
260
  value: [{ type: "number", value: where.limit }],
239
261
  };
262
+ } else if (opts?.limit) {
263
+ ast[0].limit = {
264
+ seperator: "",
265
+ value: [{ type: "number", value: opts.limit }],
266
+ };
240
267
  }
268
+ //console.log(ast[0]);
269
+ //console.log(ast[0].orderby[[0]]);
241
270
 
271
+ const orderBy = where?.orderBy || opts?.orderBy;
272
+ const orderDesc = where?.orderDesc || opts?.orderDesc;
273
+
274
+ if (orderBy) {
275
+ if (typeof orderBy === "string")
276
+ ast[0].orderby = [
277
+ {
278
+ expr: { type: "column_ref", table: null, column: orderBy },
279
+ type: orderDesc ? "DESC" : "ASC",
280
+ },
281
+ ];
282
+ else if (orderBy.operator) {
283
+ const { operator, field, target } = orderBy;
284
+ const fieldCol = (cfg.columns || []).find((c) => c.name === field);
285
+ const type = getState().types[fieldCol?.type];
286
+ const op = type?.distance_operators[operator];
287
+ if (op?.type === "SqlBinOp") {
288
+ ast[0].orderby = [
289
+ {
290
+ expr: {
291
+ type: "binary_expr",
292
+ operator: op.name,
293
+ left: {
294
+ type: "column_ref",
295
+ table: null,
296
+ column: field,
297
+ },
298
+ right: {
299
+ type: "number",
300
+ value: "$" + phIndex,
301
+ },
302
+ },
303
+ type: orderDesc ? "DESC" : "ASC",
304
+ },
305
+ ];
306
+ phIndex += 1;
307
+ phValues.push(target);
308
+ }
309
+ }
310
+ }
242
311
  const client = is_sqlite ? db : await db.getClient();
243
312
  await client.query(`BEGIN;`);
244
313
  if (!is_sqlite) {
@@ -247,7 +316,7 @@ const runQuery = async (cfg, where, opts) => {
247
316
  }
248
317
 
249
318
  const sqlQ = parser.sqlify(ast, opt);
250
- //console.log(sqlQ, phValues);
319
+ console.log({ sqlQ, phValues, opts });
251
320
  const qres = await client.query(sqlQ, phValues);
252
321
  qres.query = sqlQ;
253
322
  await client.query(`ROLLBACK;`);
package/terminal.js CHANGED
@@ -53,14 +53,13 @@ const runPost = async (
53
53
  await client.query(`BEGIN;`);
54
54
  if (!is_sqlite) {
55
55
  await client.query(`SET LOCAL search_path TO "${db.getTenantSchema()}";`);
56
- await client.query(`SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY;`);
57
56
  }
58
57
 
59
58
  let sqlResult;
60
59
  try {
61
60
  const qres = await client.query(form.values.sql, []);
62
61
 
63
- await client.query(`ROLLBACK;`);
62
+ await client.query(`COMMIT;`);
64
63
 
65
64
  if (!is_sqlite) client.release(true);
66
65
  sqlResult = mkTable(