@saltcorn/sql 0.1.1 → 0.2.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/action.js +55 -0
- package/index.js +4 -1
- package/package.json +1 -1
package/action.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
const db = require("@saltcorn/data/db");
|
|
2
|
+
const { eval_expression } = require("@saltcorn/data/models/expression");
|
|
3
|
+
|
|
4
|
+
module.exports = {
|
|
5
|
+
run_sql_code: {
|
|
6
|
+
configFields: [
|
|
7
|
+
{
|
|
8
|
+
name: "sql",
|
|
9
|
+
label: "SQL",
|
|
10
|
+
input_type: "code",
|
|
11
|
+
attributes: { mode: "text/x-sql" },
|
|
12
|
+
sublabel:
|
|
13
|
+
"Refer to row parameters in the order below with <code>$1</code>, <code>$2</code> etc",
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
name: "row_parameters",
|
|
17
|
+
label: "Row parameters",
|
|
18
|
+
sublabel:
|
|
19
|
+
"Comma separated list of row variables to use as SQL query parameters. User variables can be used as <code>user.id</code> etc",
|
|
20
|
+
type: "String",
|
|
21
|
+
},
|
|
22
|
+
],
|
|
23
|
+
run: async ({ row, configuration: { sql, row_parameters }, user }) => {
|
|
24
|
+
const is_sqlite = db.isSQLite;
|
|
25
|
+
|
|
26
|
+
const phValues = [];
|
|
27
|
+
(row_parameters || "")
|
|
28
|
+
.split(",")
|
|
29
|
+
.filter((s) => s)
|
|
30
|
+
.forEach((sp0) => {
|
|
31
|
+
const sp = sp0.trim();
|
|
32
|
+
if (sp.startsWith("user.")) {
|
|
33
|
+
phValues.push(eval_expression(sp, {}, user));
|
|
34
|
+
} else if (typeof row[sp] === "undefined") phValues.push(null);
|
|
35
|
+
else phValues.push(row[sp]);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
const client = is_sqlite ? db : await db.getClient();
|
|
39
|
+
await client.query(`BEGIN;`);
|
|
40
|
+
if (!is_sqlite) {
|
|
41
|
+
await client.query(
|
|
42
|
+
`SET LOCAL search_path TO "${db.getTenantSchema()}";`
|
|
43
|
+
);
|
|
44
|
+
await client.query(
|
|
45
|
+
`SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY;`
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
const qres = await client.query(sql, phValues);
|
|
49
|
+
|
|
50
|
+
await client.query(`COMMIT;`);
|
|
51
|
+
|
|
52
|
+
if (!is_sqlite) client.release(true);
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
};
|
package/index.js
CHANGED
|
@@ -35,6 +35,8 @@ const configuration_workflow = () =>
|
|
|
35
35
|
label: "SQL",
|
|
36
36
|
input_type: "code",
|
|
37
37
|
attributes: { mode: "text/x-sql" },
|
|
38
|
+
sublabel:
|
|
39
|
+
"Refer to state parameters in the order below with <code>$1</code>, <code>$2</code> etc",
|
|
38
40
|
},
|
|
39
41
|
{
|
|
40
42
|
name: "state_parameters",
|
|
@@ -103,7 +105,7 @@ const run = async (
|
|
|
103
105
|
}
|
|
104
106
|
const qres = await client.query(sql, phValues);
|
|
105
107
|
|
|
106
|
-
await client.query(`ROLLBACK
|
|
108
|
+
await client.query(`ROLLBACK;`);
|
|
107
109
|
|
|
108
110
|
if (!is_sqlite) client.release(true);
|
|
109
111
|
switch (output_type) {
|
|
@@ -129,6 +131,7 @@ const run = async (
|
|
|
129
131
|
module.exports = {
|
|
130
132
|
sc_plugin_api_version: 1,
|
|
131
133
|
plugin_name: "sql",
|
|
134
|
+
actions: require("./action.js"),
|
|
132
135
|
viewtemplates: [
|
|
133
136
|
{
|
|
134
137
|
name: "SQLView",
|