@saltcorn/sql 0.4.1 → 0.4.3

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/action.js CHANGED
@@ -3,7 +3,8 @@ const { eval_expression } = require("@saltcorn/data/models/expression");
3
3
 
4
4
  module.exports = {
5
5
  run_sql_code: {
6
- configFields: [
6
+ namespace: "Code",
7
+ configFields: ({ mode }) => [
7
8
  {
8
9
  name: "sql",
9
10
  label: "SQL",
@@ -19,8 +20,32 @@ module.exports = {
19
20
  "Comma separated list of row variables to use as SQL query parameters. User variables can be used as <code>user.id</code> etc",
20
21
  type: "String",
21
22
  },
23
+ {
24
+ name: "read_only",
25
+ label: "Read only",
26
+ sublabel: "Run the SQL in a read-only transactions",
27
+ type: "Bool",
28
+ },
29
+
30
+ ...(mode === "workflow"
31
+ ? [
32
+ {
33
+ label: "Variable",
34
+ name: "results_variable",
35
+ class: "validate-identifier",
36
+ sublabel: "Context variable to write to query results to",
37
+ type: "String",
38
+ required: true,
39
+ },
40
+ ]
41
+ : []),
22
42
  ],
23
- run: async ({ row, configuration: { sql, row_parameters }, user }) => {
43
+ run: async ({
44
+ row,
45
+ configuration: { sql, row_parameters, read_only, results_variable },
46
+ user,
47
+ mode,
48
+ }) => {
24
49
  const is_sqlite = db.isSQLite;
25
50
 
26
51
  const phValues = [];
@@ -41,15 +66,19 @@ module.exports = {
41
66
  await client.query(
42
67
  `SET LOCAL search_path TO "${db.getTenantSchema()}";`
43
68
  );
44
- await client.query(
45
- `SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY;`
46
- );
69
+ if (read_only)
70
+ await client.query(
71
+ `SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY;`
72
+ );
47
73
  }
48
74
  const qres = await client.query(sql, phValues);
49
75
 
50
76
  await client.query(`COMMIT;`);
51
77
 
52
78
  if (!is_sqlite) client.release(true);
79
+ if (mode === "workflow" && results_variable)
80
+ return { [results_variable]: qres.rows };
81
+ else return qres.rows;
53
82
  },
54
83
  },
55
84
  };
package/index.js CHANGED
@@ -148,7 +148,7 @@ module.exports = {
148
148
  `SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY;`
149
149
  );
150
150
  }
151
- const qres = await db.query(query, parameters || []);
151
+ const qres = await client.query(query, parameters || []);
152
152
 
153
153
  await client.query(`ROLLBACK;`);
154
154
  return qres;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saltcorn/sql",
3
- "version": "0.4.1",
3
+ "version": "0.4.3",
4
4
  "description": "Actions and views based on SQL",
5
5
  "main": "index.js",
6
6
  "dependencies": {
package/table-provider.js CHANGED
@@ -203,8 +203,16 @@ const runQuery = async (cfg, where, opts) => {
203
203
  as: null,
204
204
  }
205
205
  : (ast[0].columns || []).find((c) => c.expr?.as == k);
206
+ const sqlAggrCol = (ast[0].columns || []).find(
207
+ (c) =>
208
+ c.expr?.type === "aggr_func" &&
209
+ c.expr?.name?.toUpperCase() === k.toUpperCase()
210
+ );
211
+
206
212
  let left = sqlExprCol
207
213
  ? { ...sqlExprCol.expr, as: null }
214
+ : sqlAggrCol
215
+ ? { ...sqlAggrCol.expr }
208
216
  : {
209
217
  type: "column_ref",
210
218
  table: sqlCol?.expr?.table,
@@ -222,20 +230,32 @@ const runQuery = async (cfg, where, opts) => {
222
230
  }
223
231
  const newClause = {
224
232
  type: "binary_expr",
225
- operator: where[k]?.ilike ? "ILIKE" : "=",
233
+ operator: where[k]?.ilike && !sqlAggrCol ? "ILIKE" : "=",
226
234
  left,
227
235
  right: { type: "number", value: "$" + phIndex },
228
236
  };
229
237
  phIndex += 1;
230
238
  phValues.push(where[k]?.ilike ? where[k]?.ilike : where[k]);
231
- if (!ast[0].where) ast[0].where = newClause;
232
- else {
233
- ast[0].where = {
234
- type: "binary_expr",
235
- operator: "AND",
236
- left: ast[0].where,
237
- right: newClause,
238
- };
239
+ if (!sqlAggrCol) {
240
+ if (!ast[0].where) ast[0].where = newClause;
241
+ else {
242
+ ast[0].where = {
243
+ type: "binary_expr",
244
+ operator: "AND",
245
+ left: ast[0].where,
246
+ right: newClause,
247
+ };
248
+ }
249
+ } else {
250
+ if (!ast[0].having) ast[0].having = newClause;
251
+ else {
252
+ ast[0].having = {
253
+ type: "binary_expr",
254
+ operator: "AND",
255
+ left: ast[0].having,
256
+ right: newClause,
257
+ };
258
+ }
239
259
  }
240
260
  }
241
261
  if (where?.limit && where?.offset) {