@aaqu/fromcubes-db 0.0.0-stage → 0.1.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/LICENSE +201 -0
- package/NOTICE +24 -0
- package/README.md +442 -2
- package/SECURITY.md +75 -0
- package/THIRD-PARTY-NOTICES.md +151 -0
- package/examples/Quick Start/All Databases.json +1719 -0
- package/examples/Quick Start/ClickHouse.json +278 -0
- package/examples/Quick Start/CockroachDB.json +280 -0
- package/examples/Quick Start/Firebird.json +278 -0
- package/examples/Quick Start/MariaDB.json +281 -0
- package/examples/Quick Start/MySQL.json +281 -0
- package/examples/Quick Start/Oracle.json +280 -0
- package/examples/Quick Start/PostgreSQL.json +280 -0
- package/examples/Quick Start/SQL Server.json +282 -0
- package/examples/Quick Start/SQLite.json +278 -0
- package/examples/Quick Start/TiDB.json +281 -0
- package/examples/Quick Start/YugabyteDB.json +280 -0
- package/examples/SQLite Sensor Log With Portal.json +391 -0
- package/examples/Select With Parameters.json +130 -0
- package/index.js +32 -0
- package/lib/adapters/clickhouse.js +254 -0
- package/lib/adapters/firebird.js +416 -0
- package/lib/adapters/index.js +37 -0
- package/lib/adapters/mssql.js +178 -0
- package/lib/adapters/mysql.js +171 -0
- package/lib/adapters/oracle.js +272 -0
- package/lib/adapters/postgres.js +161 -0
- package/lib/adapters/read.js +57 -0
- package/lib/adapters/sqlite.js +193 -0
- package/lib/adapters/time.js +89 -0
- package/lib/bound.js +29 -0
- package/lib/editor-api.js +134 -0
- package/lib/editor-preview.js +191 -0
- package/lib/errors.js +66 -0
- package/lib/params.js +966 -0
- package/lib/pool.js +391 -0
- package/lib/settings.js +302 -0
- package/lib/sql/compile.js +160 -0
- package/lib/sql/dialect.js +194 -0
- package/lib/sql/parse.js +202 -0
- package/lib/sql/types.js +1071 -0
- package/lib/sqlite-files.js +137 -0
- package/nodes/connection.html +728 -0
- package/nodes/connection.js +179 -0
- package/nodes/editor/ark-pixel.LICENSE.txt +96 -0
- package/nodes/editor/ark-pixel.woff2 +0 -0
- package/nodes/editor/mark.svg +5 -0
- package/nodes/editor/sql-editor.css +604 -0
- package/nodes/editor/sql-editor.js +1490 -0
- package/nodes/editor/sql-model.js +203 -0
- package/nodes/icons/fromcubes-db.svg +24 -0
- package/nodes/query.html +662 -0
- package/nodes/query.js +195 -0
- package/package.json +81 -4
package/nodes/query.html
ADDED
|
@@ -0,0 +1,662 @@
|
|
|
1
|
+
<style>
|
|
2
|
+
/* The connection badge drawn above each query node. Node-RED's own theme
|
|
3
|
+
variables carry it through light and dark; the fallbacks keep it legible
|
|
4
|
+
on themes that do not define them. */
|
|
5
|
+
.fromcubes-db-conn-badge div {
|
|
6
|
+
font-size: 10px;
|
|
7
|
+
line-height: 16px;
|
|
8
|
+
text-align: left;
|
|
9
|
+
/* Lines the text up with the node's left edge rather than its icon. */
|
|
10
|
+
padding-left: 2px;
|
|
11
|
+
white-space: nowrap;
|
|
12
|
+
overflow: hidden;
|
|
13
|
+
text-overflow: ellipsis;
|
|
14
|
+
color: var(--red-ui-node-label-color, #666);
|
|
15
|
+
opacity: 0.75;
|
|
16
|
+
}
|
|
17
|
+
</style>
|
|
18
|
+
|
|
19
|
+
<script type="text/javascript">
|
|
20
|
+
// The SQL Editor (nodes/editor/) loads from this package's versioned asset
|
|
21
|
+
// path, which the runtime exports as a node setting: a cache in front of
|
|
22
|
+
// Node-RED can then never hand this template an older editor script.
|
|
23
|
+
(function () {
|
|
24
|
+
const base = (RED.settings && RED.settings.fromcubesDbQueryAssetBase) || "fromcubes-db/";
|
|
25
|
+
const link = document.createElement("link");
|
|
26
|
+
link.rel = "stylesheet";
|
|
27
|
+
link.href = base + "editor/sql-editor.css";
|
|
28
|
+
document.head.appendChild(link);
|
|
29
|
+
["sql-model.js", "sql-editor.js"].forEach(function (file) {
|
|
30
|
+
const s = document.createElement("script");
|
|
31
|
+
s.src = base + "editor/" + file;
|
|
32
|
+
s.async = false;
|
|
33
|
+
document.head.appendChild(s);
|
|
34
|
+
});
|
|
35
|
+
})();
|
|
36
|
+
</script>
|
|
37
|
+
|
|
38
|
+
<script type="text/javascript">
|
|
39
|
+
(function () {
|
|
40
|
+
// Kept in step with lib/params.js. These are the plain JS checks, and they
|
|
41
|
+
// stay in the picker for good: every deployed flow has one of these eight
|
|
42
|
+
// words saved in it, and dropping one would change what that flow means.
|
|
43
|
+
const PARAM_TYPES = ["auto", "string", "number", "integer", "boolean", "date", "json", "buffer"];
|
|
44
|
+
|
|
45
|
+
// Column types per product, fetched once each from the admin endpoint so
|
|
46
|
+
// that lib/sql/types.js stays the only list. Keyed by product id.
|
|
47
|
+
const columnTypes = {};
|
|
48
|
+
|
|
49
|
+
function loadColumnTypes(product) {
|
|
50
|
+
if (!product) return Promise.resolve([]);
|
|
51
|
+
if (columnTypes[product]) return Promise.resolve(columnTypes[product]);
|
|
52
|
+
return $.getJSON("fromcubes-db/types/" + encodeURIComponent(product))
|
|
53
|
+
.then(function (types) {
|
|
54
|
+
columnTypes[product] = types;
|
|
55
|
+
return types;
|
|
56
|
+
})
|
|
57
|
+
.catch(function () {
|
|
58
|
+
// An older runtime, or a product it does not know: the generic types
|
|
59
|
+
// still work, so the dialog carries on without the native ones.
|
|
60
|
+
columnTypes[product] = [];
|
|
61
|
+
return [];
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// ---------------------------------------------------------------------
|
|
66
|
+
// The database a query runs against is written above the node, so a node
|
|
67
|
+
// can carry both its own name and its connection type. The badge is drawn
|
|
68
|
+
// into the node's own SVG group, which means it follows the node through
|
|
69
|
+
// drags, zoom and panning for free.
|
|
70
|
+
//
|
|
71
|
+
// Everything below is decoration. It hangs off RED.hooks, the editor's
|
|
72
|
+
// documented extension point, and never touches Node-RED's own DOM or CSS
|
|
73
|
+
// classes. If a future release drops the hooks or changes their payload,
|
|
74
|
+
// the badge silently stops being drawn and the editor is unaffected.
|
|
75
|
+
// ---------------------------------------------------------------------
|
|
76
|
+
|
|
77
|
+
const BADGE_CLASS = "fromcubes-db-conn-badge";
|
|
78
|
+
const SVG_NS = "http://www.w3.org/2000/svg";
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The dialect names come from the connection node's own definition, so
|
|
82
|
+
* adding a dialect never means remembering to edit this file too. An
|
|
83
|
+
* unknown dialect falls back to its own id rather than vanishing.
|
|
84
|
+
*/
|
|
85
|
+
function dialectLabel(dialect) {
|
|
86
|
+
const def = typeof RED.nodes.getType === "function" ? RED.nodes.getType("fromcubes-db-connection") : null;
|
|
87
|
+
const labels = (def && def.dialectLabels) || {};
|
|
88
|
+
return labels[dialect] || dialect;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** The badge text, or '' when this node should carry no badge. */
|
|
92
|
+
function connLabel(node) {
|
|
93
|
+
const conn = node && node.connection ? RED.nodes.node(node.connection) : null;
|
|
94
|
+
// Connections saved before the checkbox existed have no showDialect,
|
|
95
|
+
// so only an explicit false opts out.
|
|
96
|
+
if (!conn || conn.showDialect === false || !conn.dialect) return "";
|
|
97
|
+
return dialectLabel(conn.dialect);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function paintBadge(evt) {
|
|
101
|
+
const node = evt && evt.node;
|
|
102
|
+
const el = evt && evt.el;
|
|
103
|
+
if (!node || node.type !== "fromcubes-db-query") return;
|
|
104
|
+
if (!el || typeof el.appendChild !== "function" || typeof el.querySelector !== "function") return;
|
|
105
|
+
|
|
106
|
+
// Scoped to our own child; nothing else in the group is ours to touch.
|
|
107
|
+
let holder = el.querySelector(":scope > ." + BADGE_CLASS);
|
|
108
|
+
const text = connLabel(node);
|
|
109
|
+
|
|
110
|
+
if (!text) {
|
|
111
|
+
if (holder) holder.remove();
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (!holder) {
|
|
116
|
+
holder = document.createElementNS(SVG_NS, "foreignObject");
|
|
117
|
+
holder.setAttribute("class", BADGE_CLASS);
|
|
118
|
+
holder.setAttribute("height", "16");
|
|
119
|
+
holder.setAttribute("y", "-18"); // above the node body
|
|
120
|
+
holder.setAttribute("x", "0");
|
|
121
|
+
holder.setAttribute("pointer-events", "none"); // clicks belong to the node
|
|
122
|
+
holder.appendChild(document.createElement("div"));
|
|
123
|
+
el.appendChild(holder);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// The redraw hook fires for every node on every redraw, so only write
|
|
127
|
+
// what actually changed.
|
|
128
|
+
const width = String(node.w || 140);
|
|
129
|
+
if (holder.getAttribute("width") !== width) holder.setAttribute("width", width);
|
|
130
|
+
const div = holder.firstChild;
|
|
131
|
+
if (div && div.textContent !== text) div.textContent = text;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function safePaint(evt) {
|
|
135
|
+
// A throw in a hook would break the redraw pass for the whole canvas.
|
|
136
|
+
try {
|
|
137
|
+
paintBadge(evt);
|
|
138
|
+
} catch (err) {
|
|
139
|
+
/* the badge is decoration; the editor is not */
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (RED.hooks && typeof RED.hooks.add === "function") {
|
|
144
|
+
RED.hooks.add("viewAddNode.fromcubes-db", safePaint);
|
|
145
|
+
RED.hooks.add("viewRedrawNode.fromcubes-db", safePaint);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Editing a connection does not mark the query nodes using it as dirty,
|
|
149
|
+
// so ask for a redraw after any dialog save.
|
|
150
|
+
if (RED.events && typeof RED.events.on === "function") {
|
|
151
|
+
RED.events.on("editor:save", function () {
|
|
152
|
+
try {
|
|
153
|
+
RED.nodes.eachNode(function (n) {
|
|
154
|
+
if (n.type === "fromcubes-db-query") n.dirty = true;
|
|
155
|
+
});
|
|
156
|
+
if (RED.view && typeof RED.view.redraw === "function") RED.view.redraw();
|
|
157
|
+
} catch (err) {
|
|
158
|
+
/* as above */
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** Pulls :name / :...name out of the SQL, skipping literals and comments. */
|
|
164
|
+
function placeholdersIn(sql) {
|
|
165
|
+
const found = [];
|
|
166
|
+
const stripped = String(sql || "")
|
|
167
|
+
.replace(/'(?:[^']|'')*'/g, "''")
|
|
168
|
+
.replace(/"(?:[^"]|"")*"/g, '""')
|
|
169
|
+
.replace(/`(?:[^`])*`/g, "``")
|
|
170
|
+
.replace(/--[^\n]*/g, "")
|
|
171
|
+
.replace(/\/\*[\s\S]*?\*\//g, "")
|
|
172
|
+
.replace(/::/g, " ");
|
|
173
|
+
const re = /:(\.\.\.)?([A-Za-z_][A-Za-z0-9_$]*)/g;
|
|
174
|
+
let m;
|
|
175
|
+
while ((m = re.exec(stripped)) !== null) {
|
|
176
|
+
if (found.indexOf(m[2]) === -1) found.push(m[2]);
|
|
177
|
+
}
|
|
178
|
+
return found;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
RED.nodes.registerType("fromcubes-db-query", {
|
|
182
|
+
category: "fromcubes",
|
|
183
|
+
color: "#5eead4",
|
|
184
|
+
defaults: {
|
|
185
|
+
name: { value: "" },
|
|
186
|
+
connection: { value: "", type: "fromcubes-db-connection", required: true },
|
|
187
|
+
sql: { value: "", required: true },
|
|
188
|
+
params: { value: [] },
|
|
189
|
+
output: { value: "rows" },
|
|
190
|
+
target: { value: "payload", required: true },
|
|
191
|
+
timeoutMs: { value: "", validate: RED.validators.number(true) },
|
|
192
|
+
},
|
|
193
|
+
inputs: 1,
|
|
194
|
+
outputs: 1,
|
|
195
|
+
icon: "fromcubes-db.svg",
|
|
196
|
+
paletteLabel: "db query",
|
|
197
|
+
label: function () {
|
|
198
|
+
// Which database this runs against is written above the node by the
|
|
199
|
+
// badge, so the label stays free for the user's own name.
|
|
200
|
+
return this.name || "db query";
|
|
201
|
+
},
|
|
202
|
+
labelStyle: function () {
|
|
203
|
+
return this.name ? "node_label_italic" : "";
|
|
204
|
+
},
|
|
205
|
+
oneditprepare: function () {
|
|
206
|
+
const node = this;
|
|
207
|
+
|
|
208
|
+
const tabs = RED.tabs.create({
|
|
209
|
+
id: "fromcubes-db-query-tabs",
|
|
210
|
+
onchange: function (tab) {
|
|
211
|
+
$("#fromcubes-db-query-tabs-content").children().hide();
|
|
212
|
+
$("#" + tab.id).show();
|
|
213
|
+
// The SQL editor is laid out from the tray height, which it can only
|
|
214
|
+
// measure while its tab is the visible one.
|
|
215
|
+
if (tab.id === "fromcubes-db-query-tab-sql") RED.tray.resize();
|
|
216
|
+
},
|
|
217
|
+
});
|
|
218
|
+
tabs.addTab({ id: "fromcubes-db-query-tab-sql", iconClass: "fa fa-file-code-o", label: "Query" });
|
|
219
|
+
tabs.addTab({
|
|
220
|
+
id: "fromcubes-db-query-tab-connection",
|
|
221
|
+
iconClass: "fa fa-database",
|
|
222
|
+
label: "Connection",
|
|
223
|
+
});
|
|
224
|
+
tabs.activateTab("fromcubes-db-query-tab-sql");
|
|
225
|
+
|
|
226
|
+
// Saves and closes this dialog (its oneditsave owns the same fields),
|
|
227
|
+
// then opens the SQL Editor on this node.
|
|
228
|
+
$("#fcdb-btn-sql-editor").on("click", function (ev) {
|
|
229
|
+
ev.preventDefault();
|
|
230
|
+
if (window.__fcdbSqlEditor) window.__fcdbSqlEditor.openNode(node.id);
|
|
231
|
+
else RED.notify("The SQL Editor did not load — reload the editor page.", "warning");
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
node.editor = RED.editor.createEditor({
|
|
235
|
+
id: "node-input-sql-editor",
|
|
236
|
+
mode: "ace/mode/sql",
|
|
237
|
+
value: node.sql || "",
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
// --- parameter type picker -------------------------------------
|
|
241
|
+
// The list of column types depends on the connection, so it is built
|
|
242
|
+
// from the product the connection node carries and rebuilt when that
|
|
243
|
+
// connection changes.
|
|
244
|
+
|
|
245
|
+
function selectedProduct() {
|
|
246
|
+
const id = $("#node-input-connection").val();
|
|
247
|
+
const conn = id ? RED.nodes.node(id) : null;
|
|
248
|
+
return conn && conn.dialect ? conn.dialect : "";
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function typeMeta(name) {
|
|
252
|
+
const types = columnTypes[selectedProduct()] || [];
|
|
253
|
+
for (let i = 0; i < types.length; i++) if (types[i].name === name) return types[i];
|
|
254
|
+
return null;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function showArgsFor(as, asArgs) {
|
|
258
|
+
const meta = typeMeta(as.val());
|
|
259
|
+
const takesArgs = !!meta && meta.args !== "none";
|
|
260
|
+
asArgs.toggle(takesArgs);
|
|
261
|
+
let hint = "n";
|
|
262
|
+
if (meta && meta.args === "precision") hint = "p,s";
|
|
263
|
+
else if (meta && meta.formats && meta.formats.length) hint = "n, " + meta.formats.join("|");
|
|
264
|
+
asArgs.attr("placeholder", hint);
|
|
265
|
+
asArgs.attr("title", hint);
|
|
266
|
+
if (!takesArgs) asArgs.val("");
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function fillTypePicker(as, asArgs, selected) {
|
|
270
|
+
as.empty();
|
|
271
|
+
const generic = $("<optgroup/>", { label: "generic" }).appendTo(as);
|
|
272
|
+
PARAM_TYPES.forEach(function (t) {
|
|
273
|
+
generic.append($("<option/>", { value: t }).text(t));
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
const product = selectedProduct();
|
|
277
|
+
return loadColumnTypes(product).then(function (types) {
|
|
278
|
+
const groups = {};
|
|
279
|
+
types.forEach(function (t) {
|
|
280
|
+
if (!groups[t.category]) {
|
|
281
|
+
groups[t.category] = $("<optgroup/>", { label: t.category }).appendTo(as);
|
|
282
|
+
}
|
|
283
|
+
let suffix = t.args === "none" ? "" : t.args === "precision" ? "(p,s)" : "(n)";
|
|
284
|
+
if (t.formats && t.formats.length) suffix = "(n, format)";
|
|
285
|
+
if (t.since) suffix += " — " + t.since + "+";
|
|
286
|
+
groups[t.category].append($("<option/>", { value: t.name }).text(t.name + suffix));
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
as.val(selected);
|
|
290
|
+
// A type this product does not have — an older flow, or the
|
|
291
|
+
// connection was just switched to another database. Keeping it
|
|
292
|
+
// selected beats turning it into 'auto' behind the user's back.
|
|
293
|
+
if (as.val() === null) {
|
|
294
|
+
$("<optgroup/>", { label: "not in " + (product || "this database") })
|
|
295
|
+
.append($("<option/>", { value: selected }).text(selected))
|
|
296
|
+
.appendTo(as);
|
|
297
|
+
as.val(selected);
|
|
298
|
+
}
|
|
299
|
+
showArgsFor(as, asArgs);
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
$("#node-input-connection").on("change", function () {
|
|
304
|
+
$("#node-input-params-container")
|
|
305
|
+
.editableList("items")
|
|
306
|
+
.each(function () {
|
|
307
|
+
const row = $(this);
|
|
308
|
+
const as = row.find(".node-input-param-as");
|
|
309
|
+
fillTypePicker(as, row.find(".node-input-param-as-args"), as.val() || "auto");
|
|
310
|
+
});
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
const list = $("#node-input-params-container");
|
|
314
|
+
list.css({ "min-height": "150px", "min-width": "450px" }).editableList({
|
|
315
|
+
removable: true,
|
|
316
|
+
sortable: true,
|
|
317
|
+
addItem: function (container, index, data) {
|
|
318
|
+
const row = $("<div/>", { style: "display:flex; gap:6px; align-items:center;" }).appendTo(container);
|
|
319
|
+
|
|
320
|
+
$("<span/>", { style: "color:#888;" }).text(":").appendTo(row);
|
|
321
|
+
|
|
322
|
+
const name = $("<input/>", {
|
|
323
|
+
class: "node-input-param-name",
|
|
324
|
+
type: "text",
|
|
325
|
+
placeholder: "name",
|
|
326
|
+
style: "width:120px;",
|
|
327
|
+
})
|
|
328
|
+
.appendTo(row)
|
|
329
|
+
.val(data.name || "");
|
|
330
|
+
|
|
331
|
+
const value = $("<input/>", { class: "node-input-param-value", type: "text" })
|
|
332
|
+
.appendTo(row)
|
|
333
|
+
.typedInput({
|
|
334
|
+
default: data.type || "msg",
|
|
335
|
+
types: ["msg", "flow", "global", "str", "num", "bool", "json", "bin", "date", "env", "jsonata"],
|
|
336
|
+
});
|
|
337
|
+
value.typedInput("type", data.type || "msg");
|
|
338
|
+
value.typedInput("value", data.value === undefined ? "" : data.value);
|
|
339
|
+
|
|
340
|
+
// Declared type. "auto" binds whatever arrives; anything else is
|
|
341
|
+
// checked before the value reaches the driver.
|
|
342
|
+
const as = $("<select/>", {
|
|
343
|
+
class: "node-input-param-as",
|
|
344
|
+
title: "expected type",
|
|
345
|
+
style: "width:130px; flex:none;",
|
|
346
|
+
}).appendTo(row);
|
|
347
|
+
|
|
348
|
+
// Length or precision for the types that take one: varchar -> 50,
|
|
349
|
+
// numeric -> 10,2. Kept apart from the name so the picker can stay
|
|
350
|
+
// a picker rather than a free-text field.
|
|
351
|
+
const asArgs = $("<input/>", {
|
|
352
|
+
class: "node-input-param-as-args",
|
|
353
|
+
type: "text",
|
|
354
|
+
placeholder: "n",
|
|
355
|
+
title: "length or precision",
|
|
356
|
+
style: "width:60px; flex:none;",
|
|
357
|
+
})
|
|
358
|
+
.appendTo(row)
|
|
359
|
+
.val(data.asArgs || "");
|
|
360
|
+
|
|
361
|
+
fillTypePicker(as, asArgs, data.as || "auto");
|
|
362
|
+
as.on("change", function () {
|
|
363
|
+
showArgsFor(as, asArgs);
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
name.on("change keyup", updateHint);
|
|
367
|
+
},
|
|
368
|
+
removeItem: updateHint,
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
(node.params || []).forEach(function (param) {
|
|
372
|
+
list.editableList("addItem", param);
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
node.editor.on("change", updateHint);
|
|
376
|
+
updateHint();
|
|
377
|
+
|
|
378
|
+
function updateHint() {
|
|
379
|
+
// Deferred so the value is read after editableList finishes its DOM work.
|
|
380
|
+
setTimeout(function () {
|
|
381
|
+
const declared = list
|
|
382
|
+
.editableList("items")
|
|
383
|
+
.map(function () {
|
|
384
|
+
return $(this).find(".node-input-param-name").val();
|
|
385
|
+
})
|
|
386
|
+
.get()
|
|
387
|
+
.filter(Boolean);
|
|
388
|
+
const used = placeholdersIn(node.editor ? node.editor.getValue() : node.sql);
|
|
389
|
+
|
|
390
|
+
const missing = used.filter(function (n) {
|
|
391
|
+
return declared.indexOf(n) === -1;
|
|
392
|
+
});
|
|
393
|
+
const unused = declared.filter(function (n) {
|
|
394
|
+
return used.indexOf(n) === -1;
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
const parts = [];
|
|
398
|
+
if (missing.length) parts.push("no value for :" + missing.join(", :"));
|
|
399
|
+
if (unused.length) parts.push("never used: :" + unused.join(", :"));
|
|
400
|
+
|
|
401
|
+
$("#node-input-params-hint")
|
|
402
|
+
.text(parts.join(" — "))
|
|
403
|
+
.css("color", missing.length ? "#a94442" : "#888");
|
|
404
|
+
}, 0);
|
|
405
|
+
}
|
|
406
|
+
},
|
|
407
|
+
oneditsave: function () {
|
|
408
|
+
const node = this;
|
|
409
|
+
node.sql = node.editor.getValue();
|
|
410
|
+
node.editor.destroy();
|
|
411
|
+
delete node.editor;
|
|
412
|
+
|
|
413
|
+
const params = [];
|
|
414
|
+
$("#node-input-params-container")
|
|
415
|
+
.editableList("items")
|
|
416
|
+
.each(function () {
|
|
417
|
+
const row = $(this);
|
|
418
|
+
const name = row.find(".node-input-param-name").val();
|
|
419
|
+
if (!name) return;
|
|
420
|
+
const value = row.find(".node-input-param-value");
|
|
421
|
+
params.push({
|
|
422
|
+
name: name.trim(),
|
|
423
|
+
type: value.typedInput("type"),
|
|
424
|
+
value: value.typedInput("value"),
|
|
425
|
+
as: row.find(".node-input-param-as").val() || "auto",
|
|
426
|
+
asArgs: (row.find(".node-input-param-as-args").val() || "").trim(),
|
|
427
|
+
});
|
|
428
|
+
});
|
|
429
|
+
node.params = params;
|
|
430
|
+
},
|
|
431
|
+
oneditcancel: function () {
|
|
432
|
+
if (this.editor) {
|
|
433
|
+
this.editor.destroy();
|
|
434
|
+
delete this.editor;
|
|
435
|
+
}
|
|
436
|
+
},
|
|
437
|
+
oneditresize: function (size) {
|
|
438
|
+
// Everything outside the tab content — the name row and the tab strip —
|
|
439
|
+
// plus every sibling of the editor inside the tab, is what the editor
|
|
440
|
+
// does not get.
|
|
441
|
+
let height = size.height;
|
|
442
|
+
$("#dialog-form")
|
|
443
|
+
.children()
|
|
444
|
+
.each(function () {
|
|
445
|
+
if (this.id !== "fromcubes-db-query-tabs-content") height -= $(this).outerHeight(true);
|
|
446
|
+
});
|
|
447
|
+
$("#fromcubes-db-query-tab-sql")
|
|
448
|
+
.children()
|
|
449
|
+
.each(function () {
|
|
450
|
+
if (!$(this).hasClass("node-text-editor-row")) height -= $(this).outerHeight(true);
|
|
451
|
+
});
|
|
452
|
+
|
|
453
|
+
const editorRow = $("#fromcubes-db-query-tab-sql > .node-text-editor-row");
|
|
454
|
+
height -= (parseInt(editorRow.css("marginTop"), 10) || 0) + (parseInt(editorRow.css("marginBottom"), 10) || 0);
|
|
455
|
+
|
|
456
|
+
$("#node-input-sql-editor").css("height", Math.max(120, height) + "px");
|
|
457
|
+
if (this.editor) this.editor.resize();
|
|
458
|
+
},
|
|
459
|
+
});
|
|
460
|
+
})();
|
|
461
|
+
</script>
|
|
462
|
+
|
|
463
|
+
<script type="text/html" data-template-name="fromcubes-db-query">
|
|
464
|
+
<div class="form-row">
|
|
465
|
+
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
|
466
|
+
<input type="text" id="node-input-name" placeholder="optional" />
|
|
467
|
+
</div>
|
|
468
|
+
|
|
469
|
+
<!-- The statement and its parameters are what this node is about; where it
|
|
470
|
+
runs and what it returns are set once and rarely touched again, so they
|
|
471
|
+
move off to their own tab instead of pushing the SQL editor down. -->
|
|
472
|
+
<div class="form-row fromcubes-db-tabs-row">
|
|
473
|
+
<ul style="min-width: 450px; margin-bottom: 20px" id="fromcubes-db-query-tabs"></ul>
|
|
474
|
+
</div>
|
|
475
|
+
|
|
476
|
+
<div id="fromcubes-db-query-tabs-content" style="min-height: calc(100% - 95px)">
|
|
477
|
+
<div id="fromcubes-db-query-tab-sql" style="display: none">
|
|
478
|
+
<div class="form-row" style="margin-bottom: 0">
|
|
479
|
+
<label for="node-input-sql-editor"><i class="fa fa-file-code-o"></i> SQL</label>
|
|
480
|
+
<button
|
|
481
|
+
type="button"
|
|
482
|
+
id="fcdb-btn-sql-editor"
|
|
483
|
+
class="red-ui-button red-ui-button-small"
|
|
484
|
+
style="float: right"
|
|
485
|
+
title="Edit the SQL and its parameters against a sample message"
|
|
486
|
+
>
|
|
487
|
+
<i class="fa fa-expand"></i> SQL Editor
|
|
488
|
+
</button>
|
|
489
|
+
</div>
|
|
490
|
+
<div class="form-row node-text-editor-row">
|
|
491
|
+
<div style="height: 200px; min-height: 120px" class="node-text-editor" id="node-input-sql-editor"></div>
|
|
492
|
+
</div>
|
|
493
|
+
<div class="form-tips" style="margin-bottom: 10px">
|
|
494
|
+
Write values as <code>:name</code>, lists as <code>:...name</code>. They are always bound as parameters — never
|
|
495
|
+
pasted into the statement.
|
|
496
|
+
</div>
|
|
497
|
+
|
|
498
|
+
<div class="form-row">
|
|
499
|
+
<label><i class="fa fa-list"></i> Parameters</label>
|
|
500
|
+
<ol id="node-input-params-container"></ol>
|
|
501
|
+
<div id="node-input-params-hint" style="font-size: 0.85em; color: #888; margin-top: 4px"></div>
|
|
502
|
+
</div>
|
|
503
|
+
</div>
|
|
504
|
+
|
|
505
|
+
<div id="fromcubes-db-query-tab-connection" style="display: none">
|
|
506
|
+
<div class="form-row">
|
|
507
|
+
<label for="node-input-connection"><i class="fa fa-database"></i> Connection</label>
|
|
508
|
+
<input type="text" id="node-input-connection" />
|
|
509
|
+
</div>
|
|
510
|
+
|
|
511
|
+
<div class="form-row">
|
|
512
|
+
<label for="node-input-output"><i class="fa fa-sign-out"></i> Result</label>
|
|
513
|
+
<select id="node-input-output">
|
|
514
|
+
<option value="rows">all rows (array)</option>
|
|
515
|
+
<option value="single">first row only</option>
|
|
516
|
+
<option value="count">affected row count</option>
|
|
517
|
+
<option value="msgPerRow">one message per row</option>
|
|
518
|
+
</select>
|
|
519
|
+
</div>
|
|
520
|
+
<div class="form-row">
|
|
521
|
+
<label for="node-input-target">to</label>
|
|
522
|
+
<!-- The result always lands on the message, so the dialog says so
|
|
523
|
+
instead of leaving the user to guess what "payload" is relative to. -->
|
|
524
|
+
<span style="color: var(--red-ui-form-text-color, #666); margin-right: 2px">msg.</span>
|
|
525
|
+
<input type="text" id="node-input-target" placeholder="payload" style="width: 160px" />
|
|
526
|
+
</div>
|
|
527
|
+
|
|
528
|
+
<div class="form-row">
|
|
529
|
+
<label for="node-input-timeoutMs">Timeout</label>
|
|
530
|
+
<input type="text" id="node-input-timeoutMs" placeholder="connection default" style="width: 120px" />
|
|
531
|
+
ms
|
|
532
|
+
</div>
|
|
533
|
+
<div class="form-tips">
|
|
534
|
+
Covers the whole wait, queueing included. Setting it above the connection's own Query timeout changes nothing on
|
|
535
|
+
PostgreSQL, Oracle and SQL Server — their drivers cancel at the connection's value.
|
|
536
|
+
</div>
|
|
537
|
+
</div>
|
|
538
|
+
</div>
|
|
539
|
+
</script>
|
|
540
|
+
|
|
541
|
+
<script type="text/html" data-help-name="fromcubes-db-query">
|
|
542
|
+
<p>Runs a SQL statement with values bound as parameters.</p>
|
|
543
|
+
<p>
|
|
544
|
+
The statement comes from this dialog and nothing else. Values arrive through the parameter list and are bound by the
|
|
545
|
+
driver, so message content cannot change what the statement does — SQL injection has no way in.
|
|
546
|
+
</p>
|
|
547
|
+
<p>
|
|
548
|
+
<b>Query</b> holds the statement and its parameters. <b>Connection</b> holds the things you set once: which database
|
|
549
|
+
this runs against, what the result looks like, which message property it lands on, and the timeout.
|
|
550
|
+
</p>
|
|
551
|
+
|
|
552
|
+
<h3>Placeholders</h3>
|
|
553
|
+
<dl class="message-properties">
|
|
554
|
+
<dt>:name</dt>
|
|
555
|
+
<dd>One value. Works for strings, numbers, booleans, dates, buffers and null.</dd>
|
|
556
|
+
<dt>:...name</dt>
|
|
557
|
+
<dd>An array expanded into a value list, for <code>IN (:...ids)</code>. An empty array matches nothing.</dd>
|
|
558
|
+
</dl>
|
|
559
|
+
<h3>Parameter types</h3>
|
|
560
|
+
<p>
|
|
561
|
+
The select next to each parameter declares what the value must be. With
|
|
562
|
+
<code>auto</code> it is bound as it arrives. Anything else is checked first, so a bad value fails with the
|
|
563
|
+
parameter's name instead of a driver error about a column you did not mention.
|
|
564
|
+
</p>
|
|
565
|
+
<dl class="message-properties">
|
|
566
|
+
<dt>string</dt>
|
|
567
|
+
<dd>Numbers, booleans and dates are converted; objects are rejected — use json.</dd>
|
|
568
|
+
<dt>number / integer</dt>
|
|
569
|
+
<dd>Numeric strings are accepted; <code>"abc"</code>, NaN and Infinity are not.</dd>
|
|
570
|
+
<dt>boolean</dt>
|
|
571
|
+
<dd><code>true</code>/<code>false</code>, <code>"yes"</code>/<code>"no"</code>, <code>1</code>/<code>0</code>.</dd>
|
|
572
|
+
<dt>date</dt>
|
|
573
|
+
<dd>A Date, an ISO string or an epoch number. An unparseable date is an error.</dd>
|
|
574
|
+
<dt>json</dt>
|
|
575
|
+
<dd>An object, an array, or a JSON string that is parsed first.</dd>
|
|
576
|
+
<dt>buffer</dt>
|
|
577
|
+
<dd>A Buffer, a Uint8Array, or a base64 string.</dd>
|
|
578
|
+
</dl>
|
|
579
|
+
<p>
|
|
580
|
+
For a spread parameter the check runs on every element, so
|
|
581
|
+
<code>IN (:...ids)</code> declared as <code>integer</code> rejects the whole message if one id is junk.
|
|
582
|
+
</p>
|
|
583
|
+
|
|
584
|
+
<h3>Column types</h3>
|
|
585
|
+
<p>
|
|
586
|
+
Below the generic types the select lists the column types of the database this node's connection points at, with a
|
|
587
|
+
field beside it for the length or precision. The value is then checked against that type as the product defines it.
|
|
588
|
+
The declaration is read before binding and never reaches the SQL text.
|
|
589
|
+
</p>
|
|
590
|
+
<p>
|
|
591
|
+
Each product's own rules apply, which is the reason to pick one:
|
|
592
|
+
<code>varchar(50)</code> is 50 characters on PostgreSQL, MySQL, MariaDB and CockroachDB, but 50 <b>bytes</b> on SQL
|
|
593
|
+
Server — <code>'ółć'</code> is six of them. Oracle counts bytes too, unless the length says
|
|
594
|
+
<code>varchar2(50 char)</code>. <code>tinyint</code> is 0…255 on SQL Server and −128…127 on MySQL.
|
|
595
|
+
</p>
|
|
596
|
+
<p>
|
|
597
|
+
Beyond length it checks integer ranges, decimal digit budgets, date ranges — <code>datetime</code> on SQL Server
|
|
598
|
+
starts in 1753 and MySQL's <code>TIMESTAMP</code> stops in 2038, a fraction the column rounds away checked as
|
|
599
|
+
rounded — and the shapes of <code>uuid</code>, <code>inet</code>, <code>macaddr</code>, <code>bit</code>, ranges and
|
|
600
|
+
SQL Server's <code>time</code>.
|
|
601
|
+
</p>
|
|
602
|
+
<p>
|
|
603
|
+
<code>vector</code> takes the number of dimensions, and on SQL Server and Oracle a storage format after it:
|
|
604
|
+
<code>3, float32</code>. The value is an array of numbers, a typed array or its JSON text.
|
|
605
|
+
</p>
|
|
606
|
+
<p>
|
|
607
|
+
The declared type also picks the driver's bind type — a Date for <code>datetime2</code> is not sent as the legacy
|
|
608
|
+
<code>DATETIME</code>, a JSON array is not turned into a PostgreSQL array. On Oracle any object is bound as JSON,
|
|
609
|
+
declared or not, so a payload can never be read as a bind definition.
|
|
610
|
+
</p>
|
|
611
|
+
<p>
|
|
612
|
+
<b>Dates are UTC.</b> A column without a time zone receives the Date's UTC wall clock and is read back the same way,
|
|
613
|
+
whatever zone Node-RED runs in; text without a zone means UTC. More digits than a Date holds, or an offset
|
|
614
|
+
(<code>timestamptz</code>, <code>datetimeoffset</code>), travel as text so nothing is cut.
|
|
615
|
+
</p>
|
|
616
|
+
<p>
|
|
617
|
+
Numbers too large for a JavaScript double are kept exactly: a BigInt stays a BigInt and a numeric string stays a
|
|
618
|
+
string, so a 19-digit id reaches the database as the id you sent rather than as the one
|
|
619
|
+
<code>Number()</code> rounds it to.
|
|
620
|
+
</p>
|
|
621
|
+
<p>
|
|
622
|
+
SQLite accepts <code>VARCHAR(5)</code> and then stores anything at all in it. The length check there is this node's,
|
|
623
|
+
not the database's.
|
|
624
|
+
</p>
|
|
625
|
+
|
|
626
|
+
<p>
|
|
627
|
+
Each dialect gets its own binding syntax automatically —
|
|
628
|
+
<code>$1</code> on Postgres, <code>@p1</code> on SQL Server, <code>:p1</code> on Oracle, <code>?</code> on MySQL.
|
|
629
|
+
</p>
|
|
630
|
+
|
|
631
|
+
<h3>Outputs</h3>
|
|
632
|
+
<dl class="message-properties">
|
|
633
|
+
<dt>payload <span class="property-type">array | object | number</span></dt>
|
|
634
|
+
<dd>Rows, the first row, or the affected row count, depending on Result.</dd>
|
|
635
|
+
<dt>rowCount <span class="property-type">number</span></dt>
|
|
636
|
+
<dd>Rows returned, or rows affected for INSERT/UPDATE/DELETE.</dd>
|
|
637
|
+
</dl>
|
|
638
|
+
<p>
|
|
639
|
+
Exact numbers are JS numbers only when that is exactly the value — a <code>BIGINT</code> past 2<sup>53</sup> arrives
|
|
640
|
+
as text, never rounded. Vectors are arrays, binary is a Buffer. SQL Server's <code>decimal</code> and
|
|
641
|
+
<code>money</code> arrive as doubles: cast them to <code>varchar</code> in the SQL when every digit counts.
|
|
642
|
+
</p>
|
|
643
|
+
|
|
644
|
+
<h3>On the canvas</h3>
|
|
645
|
+
<p>
|
|
646
|
+
The database this node runs against is written in small type above the node, leaving the node's own label for the
|
|
647
|
+
name you give it. The checkbox under the database picker on the connection turns that off.
|
|
648
|
+
</p>
|
|
649
|
+
<p>
|
|
650
|
+
The status marker says whose news it is. A <b>ring</b> is the shared connection — <code>connected</code> appears on
|
|
651
|
+
every node using that connection the moment any one of them opens it, because they all share the same pool, and red
|
|
652
|
+
appears on all of them if the server goes away. A <b>filled dot</b> is this node alone: <code>querying</code>, then
|
|
653
|
+
the row count or the error from its own statement.
|
|
654
|
+
</p>
|
|
655
|
+
|
|
656
|
+
<h3>Errors</h3>
|
|
657
|
+
<p>
|
|
658
|
+
Failures are reported through <code>done()</code>, so a Catch node picks them up. Constraint violations arrive as
|
|
659
|
+
recognisable errors (<code>UniqueViolationError</code>, <code>ForeignKeyViolationError</code>, ...) regardless of
|
|
660
|
+
which database raised them.
|
|
661
|
+
</p>
|
|
662
|
+
</script>
|