@saltcorn/sql 0.4.0 → 0.4.2
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 +23 -2
- package/index.js +1 -1
- package/package.json +1 -1
- package/table-provider.js +35 -11
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
|
-
|
|
6
|
+
namespace: "Code",
|
|
7
|
+
configFields: ({ mode }) => [
|
|
7
8
|
{
|
|
8
9
|
name: "sql",
|
|
9
10
|
label: "SQL",
|
|
@@ -19,8 +20,25 @@ 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
|
+
...(mode === "workflow"
|
|
24
|
+
? [
|
|
25
|
+
{
|
|
26
|
+
label: "Variable",
|
|
27
|
+
name: "results_variable",
|
|
28
|
+
class: "validate-identifier",
|
|
29
|
+
sublabel: "Context variable to write to query results to",
|
|
30
|
+
type: "String",
|
|
31
|
+
required: true,
|
|
32
|
+
},
|
|
33
|
+
]
|
|
34
|
+
: []),
|
|
22
35
|
],
|
|
23
|
-
run: async ({
|
|
36
|
+
run: async ({
|
|
37
|
+
row,
|
|
38
|
+
configuration: { sql, row_parameters, results_variable },
|
|
39
|
+
user,
|
|
40
|
+
mode,
|
|
41
|
+
}) => {
|
|
24
42
|
const is_sqlite = db.isSQLite;
|
|
25
43
|
|
|
26
44
|
const phValues = [];
|
|
@@ -50,6 +68,9 @@ module.exports = {
|
|
|
50
68
|
await client.query(`COMMIT;`);
|
|
51
69
|
|
|
52
70
|
if (!is_sqlite) client.release(true);
|
|
71
|
+
if (mode === "workflow" && results_variable)
|
|
72
|
+
return { [results_variable]: qres.rows };
|
|
73
|
+
else return qres.rows;
|
|
53
74
|
},
|
|
54
75
|
},
|
|
55
76
|
};
|
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
|
|
151
|
+
const qres = await client.query(query, parameters || []);
|
|
152
152
|
|
|
153
153
|
await client.query(`ROLLBACK;`);
|
|
154
154
|
return qres;
|
package/package.json
CHANGED
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 (!
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
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) {
|
|
@@ -275,7 +295,11 @@ const runQuery = async (cfg, where, opts) => {
|
|
|
275
295
|
if (typeof orderBy === "string")
|
|
276
296
|
ast[0].orderby = [
|
|
277
297
|
{
|
|
278
|
-
expr: {
|
|
298
|
+
expr: {
|
|
299
|
+
type: "column_ref",
|
|
300
|
+
table: null,
|
|
301
|
+
column: db.sqlsanitize(orderBy),
|
|
302
|
+
},
|
|
279
303
|
type: orderDesc ? "DESC" : "ASC",
|
|
280
304
|
},
|
|
281
305
|
];
|
|
@@ -293,7 +317,7 @@ const runQuery = async (cfg, where, opts) => {
|
|
|
293
317
|
left: {
|
|
294
318
|
type: "column_ref",
|
|
295
319
|
table: null,
|
|
296
|
-
column: field,
|
|
320
|
+
column: db.sqlsanitize(field),
|
|
297
321
|
},
|
|
298
322
|
right: {
|
|
299
323
|
type: "number",
|