@aaqu/fromcubes-db 0.0.0-stage → 0.1.1
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
|
@@ -0,0 +1,1490 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SQL Editor — a full-screen editor for every db query node, built the way the
|
|
3
|
+
* React Editor in @aaqu/fromcubes-react is (one Monaco, one model per node,
|
|
4
|
+
* edits written onto the node with an undo entry, deploy left to the user),
|
|
5
|
+
* without depending on it.
|
|
6
|
+
*
|
|
7
|
+
* Under the SQL sits the Input panel: a sample message (typed, or captured
|
|
8
|
+
* from the node as it runs), its fields, and the parameters — each checked
|
|
9
|
+
* against the sample with the same functions a deployed node runs
|
|
10
|
+
* (POST fromcubes-db/sql/preview), so what it says is what the deploy does.
|
|
11
|
+
*
|
|
12
|
+
* Writes go to the node, never to the server. Close does not discard.
|
|
13
|
+
*/
|
|
14
|
+
(function () {
|
|
15
|
+
"use strict";
|
|
16
|
+
|
|
17
|
+
const PREFIX = "[fcdb-sql-editor]";
|
|
18
|
+
const EDITOR_NAME = "SQL Editor";
|
|
19
|
+
const FLUSH_DEBOUNCE_MS = 400;
|
|
20
|
+
const PREVIEW_DEBOUNCE_MS = 250;
|
|
21
|
+
const CAPTURE_POLL_MS = 1000;
|
|
22
|
+
const SAMPLES_KEY = "fcdbSqlEditorSamples";
|
|
23
|
+
const SETTINGS_KEY = "fcdbSqlEditorSettings";
|
|
24
|
+
const DEFAULT_SETTINGS = { treeWidth: 250, panelHeight: 300, panelTab: "input" };
|
|
25
|
+
const SOURCES = ["msg", "flow", "global", "str", "num", "bool", "json", "bin", "date", "env", "jsonata"];
|
|
26
|
+
const PARAM_TYPES = ["auto", "string", "number", "integer", "boolean", "date", "json", "buffer"];
|
|
27
|
+
const DEFAULT_SAMPLE = '{\n "payload": {}\n}\n';
|
|
28
|
+
|
|
29
|
+
const ASSET_BASE = (RED.settings && RED.settings.fromcubesDbQueryAssetBase) || "fromcubes-db/";
|
|
30
|
+
|
|
31
|
+
let M = null; // window.__fcdbSqlModel
|
|
32
|
+
|
|
33
|
+
// DOM
|
|
34
|
+
let overlay = null;
|
|
35
|
+
let treeEl = null;
|
|
36
|
+
let tabsEl = null;
|
|
37
|
+
let monacoHost = null;
|
|
38
|
+
let fallbackEl = null;
|
|
39
|
+
let placeholderEl = null;
|
|
40
|
+
let panelEl = null;
|
|
41
|
+
let statusEl = null;
|
|
42
|
+
let deployBtn = null;
|
|
43
|
+
let filterEl = null;
|
|
44
|
+
let sampleHost = null;
|
|
45
|
+
let sampleFallback = null;
|
|
46
|
+
let sampleStatus = null;
|
|
47
|
+
let captureBtn = null;
|
|
48
|
+
let fieldsEl = null;
|
|
49
|
+
let paramsEl = null;
|
|
50
|
+
let connSelect = null;
|
|
51
|
+
let receivesEl = null;
|
|
52
|
+
|
|
53
|
+
// Monaco
|
|
54
|
+
let editor = null;
|
|
55
|
+
let sampleEditor = null;
|
|
56
|
+
let monacoReady = false;
|
|
57
|
+
let monacoFailed = false;
|
|
58
|
+
let completionRegistered = false;
|
|
59
|
+
|
|
60
|
+
/** nodeId → working state of an open node */
|
|
61
|
+
const entries = {};
|
|
62
|
+
const openIds = [];
|
|
63
|
+
let activeId = null;
|
|
64
|
+
let queries = [];
|
|
65
|
+
let filterText = "";
|
|
66
|
+
let settings = Object.assign({}, DEFAULT_SETTINGS);
|
|
67
|
+
let samples = {};
|
|
68
|
+
const flushTimers = {};
|
|
69
|
+
let previewTimer = null;
|
|
70
|
+
let previewSeq = 0;
|
|
71
|
+
let resyncTimer = null;
|
|
72
|
+
let selfEmitting = false;
|
|
73
|
+
let captureTimer = null;
|
|
74
|
+
const columnTypes = {};
|
|
75
|
+
|
|
76
|
+
// ── Settings and samples (localStorage, never the flow) ────
|
|
77
|
+
|
|
78
|
+
function loadJson(key, fallback) {
|
|
79
|
+
try {
|
|
80
|
+
const v = JSON.parse(localStorage.getItem(key) || "null");
|
|
81
|
+
return v && typeof v === "object" ? v : fallback;
|
|
82
|
+
} catch (err) {
|
|
83
|
+
return fallback;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function saveJson(key, value) {
|
|
88
|
+
try {
|
|
89
|
+
localStorage.setItem(key, JSON.stringify(value));
|
|
90
|
+
} catch (err) {
|
|
91
|
+
/* private mode, full storage: only persistence is lost */
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function loadSettings() {
|
|
96
|
+
const s = loadJson(SETTINGS_KEY, {});
|
|
97
|
+
settings = Object.assign({}, DEFAULT_SETTINGS);
|
|
98
|
+
if (Number(s.treeWidth) >= 150) settings.treeWidth = Number(s.treeWidth);
|
|
99
|
+
if (Number(s.panelHeight) >= 120) settings.panelHeight = Number(s.panelHeight);
|
|
100
|
+
if (s.panelTab === "input" || s.panelTab === "receives") settings.panelTab = s.panelTab;
|
|
101
|
+
// A sample is the user's test data, kept in this browser only: it may hold
|
|
102
|
+
// things that do not belong in flows.json.
|
|
103
|
+
samples = loadJson(SAMPLES_KEY, {});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// ── Nodes ──────────────────────────────────────────────────
|
|
107
|
+
|
|
108
|
+
function queryNodes() {
|
|
109
|
+
const out = [];
|
|
110
|
+
RED.nodes.eachNode(function (n) {
|
|
111
|
+
if (n.type === "fromcubes-db-query") out.push(n);
|
|
112
|
+
});
|
|
113
|
+
return out;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function flowOf(id) {
|
|
117
|
+
return RED.nodes.workspace(id) || RED.nodes.subflow(id) || null;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function connections() {
|
|
121
|
+
const out = [];
|
|
122
|
+
RED.nodes.eachConfig(function (c) {
|
|
123
|
+
if (c.type === "fromcubes-db-connection") out.push(c);
|
|
124
|
+
});
|
|
125
|
+
out.sort(function (a, b) {
|
|
126
|
+
return label(a).localeCompare(label(b));
|
|
127
|
+
});
|
|
128
|
+
return out;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function label(conn) {
|
|
132
|
+
const def = RED.nodes.getType("fromcubes-db-connection");
|
|
133
|
+
try {
|
|
134
|
+
return def && def.label ? def.label.call(conn) : conn.name || conn.id;
|
|
135
|
+
} catch (err) {
|
|
136
|
+
return conn.name || conn.id;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function collect() {
|
|
141
|
+
queries = M.collectQueries(queryNodes(), flowOf, function (id) {
|
|
142
|
+
return RED.nodes.node(id);
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function productOf(entry) {
|
|
147
|
+
const conn = entry && entry.connection ? RED.nodes.node(entry.connection) : null;
|
|
148
|
+
return conn && conn.dialect ? conn.dialect : "";
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// ── Entries: what is being edited, per node ────────────────
|
|
152
|
+
|
|
153
|
+
function modelGone(entry) {
|
|
154
|
+
return !entry.model || (entry.model.isDisposed && entry.model.isDisposed());
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function ensureModel(entry) {
|
|
158
|
+
if (!monacoReady) return;
|
|
159
|
+
if (modelGone(entry)) {
|
|
160
|
+
// Node-RED disposes every Monaco model when any of its own dialogs
|
|
161
|
+
// closes; the text lives in the entry too, so nothing is lost.
|
|
162
|
+
const uri = monaco.Uri.parse("file:///fcdb-sql-" + entry.id + ".sql");
|
|
163
|
+
const stale = monaco.editor.getModel(uri);
|
|
164
|
+
if (stale) stale.dispose();
|
|
165
|
+
entry.model = monaco.editor.createModel(entry.sql, "sql", uri);
|
|
166
|
+
entry.model.onDidChangeContent(function () {
|
|
167
|
+
entry.sql = entry.model.getValue();
|
|
168
|
+
scheduleFlush(entry.id);
|
|
169
|
+
schedulePreview();
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
if (!entry.sampleModel || (entry.sampleModel.isDisposed && entry.sampleModel.isDisposed())) {
|
|
173
|
+
const uri = monaco.Uri.parse("file:///fcdb-sample-" + entry.id + ".json");
|
|
174
|
+
const stale = monaco.editor.getModel(uri);
|
|
175
|
+
if (stale) stale.dispose();
|
|
176
|
+
entry.sampleModel = monaco.editor.createModel(entry.sample, "json", uri);
|
|
177
|
+
entry.sampleModel.onDidChangeContent(function () {
|
|
178
|
+
setSample(entry, entry.sampleModel.getValue(), true);
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function ensureEntry(id) {
|
|
184
|
+
const node = RED.nodes.node(id);
|
|
185
|
+
if (!node) return null;
|
|
186
|
+
let entry = entries[id];
|
|
187
|
+
if (!entry) {
|
|
188
|
+
entry = entries[id] = {
|
|
189
|
+
id: id,
|
|
190
|
+
sql: node.sql || "",
|
|
191
|
+
params: M.normalizeParams(node.params),
|
|
192
|
+
connection: node.connection || "",
|
|
193
|
+
sample: typeof samples[id] === "string" ? samples[id] : DEFAULT_SAMPLE,
|
|
194
|
+
sampleMsg: {},
|
|
195
|
+
sampleError: "",
|
|
196
|
+
synced: null,
|
|
197
|
+
preview: null,
|
|
198
|
+
viewState: null,
|
|
199
|
+
};
|
|
200
|
+
entry.synced = { sql: entry.sql, params: M.normalizeParams(entry.params), connection: entry.connection };
|
|
201
|
+
parseSample(entry);
|
|
202
|
+
}
|
|
203
|
+
ensureModel(entry);
|
|
204
|
+
return entry;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function parseSample(entry) {
|
|
208
|
+
try {
|
|
209
|
+
const v = entry.sample.trim() === "" ? {} : JSON.parse(entry.sample);
|
|
210
|
+
if (!v || typeof v !== "object" || Array.isArray(v)) throw new Error('a message is an object: { "payload": … }');
|
|
211
|
+
entry.sampleMsg = v;
|
|
212
|
+
entry.sampleError = "";
|
|
213
|
+
} catch (err) {
|
|
214
|
+
entry.sampleError = err.message;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function setSample(entry, text, fromModel) {
|
|
219
|
+
entry.sample = text;
|
|
220
|
+
samples[entry.id] = text;
|
|
221
|
+
saveJson(SAMPLES_KEY, samples);
|
|
222
|
+
parseSample(entry);
|
|
223
|
+
if (!fromModel && entry.sampleModel && !entry.sampleModel.isDisposed() && entry.sampleModel.getValue() !== text) {
|
|
224
|
+
entry.sampleModel.setValue(text);
|
|
225
|
+
}
|
|
226
|
+
if (entry.id === activeId) {
|
|
227
|
+
renderSampleStatus();
|
|
228
|
+
renderFields();
|
|
229
|
+
schedulePreview();
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function isDirty(entry) {
|
|
234
|
+
const node = RED.nodes.node(entry.id);
|
|
235
|
+
if (!node) return false;
|
|
236
|
+
return (
|
|
237
|
+
(node.sql || "") !== entry.sql ||
|
|
238
|
+
(node.connection || "") !== entry.connection ||
|
|
239
|
+
!M.sameParams(node.params, entry.params)
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// ── Write-back: onto the node, with an undo entry ──────────
|
|
244
|
+
|
|
245
|
+
function flush(id) {
|
|
246
|
+
const entry = entries[id];
|
|
247
|
+
clearTimeout(flushTimers[id]);
|
|
248
|
+
delete flushTimers[id];
|
|
249
|
+
if (!entry) return false;
|
|
250
|
+
const node = RED.nodes.node(id);
|
|
251
|
+
if (!node) return false;
|
|
252
|
+
|
|
253
|
+
const changes = {};
|
|
254
|
+
if ((node.sql || "") !== entry.sql) changes.sql = node.sql;
|
|
255
|
+
if (!M.sameParams(node.params, entry.params)) changes.params = node.params;
|
|
256
|
+
if ((node.connection || "") !== entry.connection) changes.connection = node.connection;
|
|
257
|
+
entry.synced = { sql: entry.sql, params: M.normalizeParams(entry.params), connection: entry.connection };
|
|
258
|
+
if (!Object.keys(changes).length) return false;
|
|
259
|
+
|
|
260
|
+
const wasDirty = RED.nodes.dirty();
|
|
261
|
+
const wasChanged = node.changed;
|
|
262
|
+
if ("sql" in changes) node.sql = entry.sql;
|
|
263
|
+
if ("params" in changes) node.params = M.normalizeParams(entry.params);
|
|
264
|
+
if ("connection" in changes) moveUser(node, changes.connection, entry.connection);
|
|
265
|
+
node.changed = true;
|
|
266
|
+
node.dirty = true;
|
|
267
|
+
RED.nodes.dirty(true);
|
|
268
|
+
try {
|
|
269
|
+
RED.history.push({ t: "edit", node: node, changes: changes, dirty: wasDirty, changed: wasChanged });
|
|
270
|
+
} catch (err) {
|
|
271
|
+
console.warn(PREFIX, "history push failed", err);
|
|
272
|
+
}
|
|
273
|
+
try {
|
|
274
|
+
if (RED.editor && RED.editor.validateNode) RED.editor.validateNode(node);
|
|
275
|
+
} catch (err) {
|
|
276
|
+
/* validation is decoration here */
|
|
277
|
+
}
|
|
278
|
+
selfEmitting = true;
|
|
279
|
+
try {
|
|
280
|
+
RED.events.emit("nodes:change", node);
|
|
281
|
+
if (RED.view && RED.view.redraw) RED.view.redraw();
|
|
282
|
+
} finally {
|
|
283
|
+
selfEmitting = false;
|
|
284
|
+
}
|
|
285
|
+
return true;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/** A node's config reference, as the dialog would move it: the users lists follow. */
|
|
289
|
+
function moveUser(node, fromId, toId) {
|
|
290
|
+
node.connection = toId;
|
|
291
|
+
const from = fromId ? RED.nodes.node(fromId) : null;
|
|
292
|
+
const to = toId ? RED.nodes.node(toId) : null;
|
|
293
|
+
if (from && Array.isArray(from.users)) {
|
|
294
|
+
const i = from.users.indexOf(node);
|
|
295
|
+
if (i !== -1) from.users.splice(i, 1);
|
|
296
|
+
}
|
|
297
|
+
if (to && Array.isArray(to.users) && to.users.indexOf(node) === -1) to.users.push(node);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
function flushAll() {
|
|
301
|
+
openIds.slice().forEach(flush);
|
|
302
|
+
renderAll();
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function scheduleFlush(id) {
|
|
306
|
+
clearTimeout(flushTimers[id]);
|
|
307
|
+
flushTimers[id] = setTimeout(function () {
|
|
308
|
+
flush(id);
|
|
309
|
+
renderTree();
|
|
310
|
+
renderTabs();
|
|
311
|
+
updateStatus();
|
|
312
|
+
}, FLUSH_DEBOUNCE_MS);
|
|
313
|
+
renderTabs();
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// ── Preview: the server's answer for the active node ───────
|
|
317
|
+
|
|
318
|
+
function schedulePreview() {
|
|
319
|
+
clearTimeout(previewTimer);
|
|
320
|
+
previewTimer = setTimeout(runPreview, PREVIEW_DEBOUNCE_MS);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
function runPreview() {
|
|
324
|
+
const entry = entries[activeId];
|
|
325
|
+
if (!entry) return;
|
|
326
|
+
const product = productOf(entry);
|
|
327
|
+
if (!product) {
|
|
328
|
+
entry.preview = { error: { kind: "connection", message: "Pick a connection" } };
|
|
329
|
+
renderPreview();
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
const seq = ++previewSeq;
|
|
333
|
+
$.ajax({
|
|
334
|
+
url: "fromcubes-db/sql/preview",
|
|
335
|
+
method: "POST",
|
|
336
|
+
contentType: "application/json",
|
|
337
|
+
data: JSON.stringify({
|
|
338
|
+
product: product,
|
|
339
|
+
sql: entry.sql,
|
|
340
|
+
params: entry.params,
|
|
341
|
+
msg: entry.sampleMsg,
|
|
342
|
+
}),
|
|
343
|
+
})
|
|
344
|
+
.done(function (answer) {
|
|
345
|
+
if (seq !== previewSeq) return; // an older answer, overtaken
|
|
346
|
+
entry.preview = answer;
|
|
347
|
+
if (entry.id === activeId) renderPreview();
|
|
348
|
+
})
|
|
349
|
+
.fail(function (xhr) {
|
|
350
|
+
if (seq !== previewSeq) return;
|
|
351
|
+
entry.preview = { error: { kind: "server", message: "Preview unavailable (" + xhr.status + ")" } };
|
|
352
|
+
if (entry.id === activeId) renderPreview();
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// ── Monaco ─────────────────────────────────────────────────
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* Monaco from this package (fromcubes-db/<version>/vs), unless the page
|
|
360
|
+
* already has one — Node-RED's own, or the React Editor's: one Monaco per
|
|
361
|
+
* page, whoever loaded it.
|
|
362
|
+
*/
|
|
363
|
+
function loadMonaco(cb) {
|
|
364
|
+
if (window.monaco && window.monaco.editor) {
|
|
365
|
+
cb();
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
// Another loader is already on its way (the React Editor's): wait for it
|
|
369
|
+
// rather than start a second AMD loader beside it.
|
|
370
|
+
if (window.require && window.require.config && document.querySelector('script[src$="vs/loader.js"]')) {
|
|
371
|
+
let waited = 0;
|
|
372
|
+
const tick = function () {
|
|
373
|
+
if (window.monaco && window.monaco.editor) return cb();
|
|
374
|
+
waited += 100;
|
|
375
|
+
if (waited > 15000) return cb(true);
|
|
376
|
+
setTimeout(tick, 100);
|
|
377
|
+
};
|
|
378
|
+
tick();
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
const s = document.createElement("script");
|
|
382
|
+
s.src = ASSET_BASE + "vs/loader.js";
|
|
383
|
+
s.onload = function () {
|
|
384
|
+
window.require.config({ paths: { vs: ASSET_BASE + "vs" } });
|
|
385
|
+
window.require(
|
|
386
|
+
["vs/editor/editor.main"],
|
|
387
|
+
function () {
|
|
388
|
+
cb();
|
|
389
|
+
},
|
|
390
|
+
function () {
|
|
391
|
+
cb(true);
|
|
392
|
+
},
|
|
393
|
+
);
|
|
394
|
+
};
|
|
395
|
+
s.onerror = function () {
|
|
396
|
+
cb(true);
|
|
397
|
+
};
|
|
398
|
+
document.head.appendChild(s);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
const EDITOR_OPTS = {
|
|
402
|
+
theme: "vs-dark",
|
|
403
|
+
minimap: { enabled: false },
|
|
404
|
+
fontSize: 13,
|
|
405
|
+
fontFamily: "'Cascadia Code','Fira Code',Consolas,monospace",
|
|
406
|
+
scrollBeyondLastLine: false,
|
|
407
|
+
automaticLayout: true,
|
|
408
|
+
tabSize: 2,
|
|
409
|
+
padding: { top: 8, bottom: 8 },
|
|
410
|
+
};
|
|
411
|
+
|
|
412
|
+
function ensureMonaco(done) {
|
|
413
|
+
if (monacoFailed) return done();
|
|
414
|
+
if (monacoReady && editor && editor.getDomNode() && sampleEditor && sampleEditor.getDomNode()) return done();
|
|
415
|
+
monacoReady = false;
|
|
416
|
+
editor = null;
|
|
417
|
+
sampleEditor = null;
|
|
418
|
+
loadMonaco(function (failed) {
|
|
419
|
+
if (failed || !window.monaco) {
|
|
420
|
+
monacoFailed = true;
|
|
421
|
+
console.warn(PREFIX, "Monaco unavailable — falling back to text areas");
|
|
422
|
+
return done();
|
|
423
|
+
}
|
|
424
|
+
monacoHost.empty();
|
|
425
|
+
sampleHost.empty();
|
|
426
|
+
editor = monaco.editor.create(monacoHost[0], Object.assign({ model: null }, EDITOR_OPTS));
|
|
427
|
+
sampleEditor = monaco.editor.create(
|
|
428
|
+
sampleHost[0],
|
|
429
|
+
Object.assign({ model: null }, EDITOR_OPTS, { lineNumbers: "off", folding: true, fontSize: 12 }),
|
|
430
|
+
);
|
|
431
|
+
editor.addAction({
|
|
432
|
+
id: "fcdb-deploy",
|
|
433
|
+
label: "Deploy",
|
|
434
|
+
keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyS],
|
|
435
|
+
run: deploy,
|
|
436
|
+
});
|
|
437
|
+
editor.addAction({
|
|
438
|
+
id: "fcdb-filter",
|
|
439
|
+
label: "Filter queries",
|
|
440
|
+
keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyP],
|
|
441
|
+
run: function () {
|
|
442
|
+
filterEl.trigger("focus").trigger("select");
|
|
443
|
+
},
|
|
444
|
+
});
|
|
445
|
+
registerCompletion();
|
|
446
|
+
monacoReady = true;
|
|
447
|
+
Object.keys(entries).forEach(function (id) {
|
|
448
|
+
ensureModel(entries[id]);
|
|
449
|
+
});
|
|
450
|
+
done();
|
|
451
|
+
});
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/**
|
|
455
|
+
* `:` offers the node's parameters, then every field of the sample message
|
|
456
|
+
* as a new one — picking a field writes `:name` and binds it to that field.
|
|
457
|
+
*/
|
|
458
|
+
function registerCompletion() {
|
|
459
|
+
if (completionRegistered) return;
|
|
460
|
+
completionRegistered = true;
|
|
461
|
+
const bindCommand = editor.addCommand(0, function (_ctx, name, path) {
|
|
462
|
+
const entry = entries[activeId];
|
|
463
|
+
if (entry) addParam(entry, { name: name, type: "msg", value: path, as: "auto", asArgs: "" });
|
|
464
|
+
});
|
|
465
|
+
monaco.languages.registerCompletionItemProvider("sql", {
|
|
466
|
+
triggerCharacters: [":"],
|
|
467
|
+
provideCompletionItems: function (model, position) {
|
|
468
|
+
const entry = Object.keys(entries)
|
|
469
|
+
.map(function (id) {
|
|
470
|
+
return entries[id];
|
|
471
|
+
})
|
|
472
|
+
.filter(function (e) {
|
|
473
|
+
return e.model === model;
|
|
474
|
+
})[0];
|
|
475
|
+
if (!entry) return { suggestions: [] };
|
|
476
|
+
const line = model.getLineContent(position.lineNumber).slice(0, position.column - 1);
|
|
477
|
+
const m = /(?:^|[^:\w])(:(?:\.\.\.)?)([A-Za-z_]\w*)?$/.exec(line);
|
|
478
|
+
if (!m) return { suggestions: [] };
|
|
479
|
+
const typed = (m[2] || "").length;
|
|
480
|
+
const range = new monaco.Range(
|
|
481
|
+
position.lineNumber,
|
|
482
|
+
position.column - typed,
|
|
483
|
+
position.lineNumber,
|
|
484
|
+
position.column,
|
|
485
|
+
);
|
|
486
|
+
const bound = {};
|
|
487
|
+
const suggestions = entry.params
|
|
488
|
+
.filter(function (p) {
|
|
489
|
+
return p.name;
|
|
490
|
+
})
|
|
491
|
+
.map(function (p) {
|
|
492
|
+
bound[p.name] = true;
|
|
493
|
+
return {
|
|
494
|
+
label: p.name,
|
|
495
|
+
kind: monaco.languages.CompletionItemKind.Variable,
|
|
496
|
+
detail: p.type + (p.value === "" ? "" : " " + p.value) + (p.as && p.as !== "auto" ? " · " + p.as : ""),
|
|
497
|
+
insertText: p.name,
|
|
498
|
+
range: range,
|
|
499
|
+
sortText: "0" + p.name,
|
|
500
|
+
};
|
|
501
|
+
});
|
|
502
|
+
M.messageFields(entry.sampleMsg).forEach(function (f) {
|
|
503
|
+
if (f.kind === "object") return;
|
|
504
|
+
const name = M.nameFor(f);
|
|
505
|
+
if (bound[name]) return;
|
|
506
|
+
bound[name] = true;
|
|
507
|
+
suggestions.push({
|
|
508
|
+
label: name,
|
|
509
|
+
kind: monaco.languages.CompletionItemKind.Field,
|
|
510
|
+
detail: "bind msg." + f.path + " = " + f.preview,
|
|
511
|
+
insertText: name,
|
|
512
|
+
range: range,
|
|
513
|
+
sortText: "1" + name,
|
|
514
|
+
command: { id: bindCommand, title: "bind", arguments: [name, f.path] },
|
|
515
|
+
});
|
|
516
|
+
});
|
|
517
|
+
return { suggestions: suggestions };
|
|
518
|
+
},
|
|
519
|
+
});
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
function applyMarkers(entry) {
|
|
523
|
+
if (!monacoReady || modelGone(entry)) return;
|
|
524
|
+
const p = entry.preview;
|
|
525
|
+
const markers = [];
|
|
526
|
+
if (p && p.placeholders) {
|
|
527
|
+
const byName = {};
|
|
528
|
+
(p.params || []).forEach(function (r) {
|
|
529
|
+
if (r.name) byName[r.name] = r;
|
|
530
|
+
});
|
|
531
|
+
p.placeholders.forEach(function (ph) {
|
|
532
|
+
const start = entry.model.getPositionAt(ph.start);
|
|
533
|
+
const end = entry.model.getPositionAt(ph.end);
|
|
534
|
+
const r = byName[ph.name];
|
|
535
|
+
let severity = null;
|
|
536
|
+
let message = "";
|
|
537
|
+
if (!r) {
|
|
538
|
+
severity = monaco.MarkerSeverity.Warning;
|
|
539
|
+
message = "No value bound to :" + ph.name + " — add it under Input";
|
|
540
|
+
} else if (r.status === "error") {
|
|
541
|
+
severity = monaco.MarkerSeverity.Error;
|
|
542
|
+
message = r.message;
|
|
543
|
+
} else if (r.status === "missing") {
|
|
544
|
+
severity = monaco.MarkerSeverity.Info;
|
|
545
|
+
message = r.message;
|
|
546
|
+
}
|
|
547
|
+
if (severity === null) return;
|
|
548
|
+
markers.push({
|
|
549
|
+
severity: severity,
|
|
550
|
+
message: message,
|
|
551
|
+
startLineNumber: start.lineNumber,
|
|
552
|
+
startColumn: start.column,
|
|
553
|
+
endLineNumber: end.lineNumber,
|
|
554
|
+
endColumn: end.column,
|
|
555
|
+
source: "fromcubes-db",
|
|
556
|
+
});
|
|
557
|
+
});
|
|
558
|
+
}
|
|
559
|
+
monaco.editor.setModelMarkers(entry.model, "fromcubes-db", markers);
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
// ── Parameters ─────────────────────────────────────────────
|
|
563
|
+
|
|
564
|
+
function uniqueName(entry, base) {
|
|
565
|
+
const taken = {};
|
|
566
|
+
entry.params.forEach(function (p) {
|
|
567
|
+
taken[p.name] = true;
|
|
568
|
+
});
|
|
569
|
+
if (!taken[base]) return base;
|
|
570
|
+
for (let i = 2; ; i++) if (!taken[base + "_" + i]) return base + "_" + i;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
function addParam(entry, param) {
|
|
574
|
+
entry.params.push(param);
|
|
575
|
+
paramsChanged(entry, true);
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
function paramsChanged(entry, structural) {
|
|
579
|
+
scheduleFlush(entry.id);
|
|
580
|
+
if (structural) renderParams();
|
|
581
|
+
schedulePreview();
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
/** A field clicked in the list: `:name` at the cursor, bound to that field. */
|
|
585
|
+
function bindField(field) {
|
|
586
|
+
const entry = entries[activeId];
|
|
587
|
+
if (!entry) return;
|
|
588
|
+
let name = M.nameFor(field);
|
|
589
|
+
const existing = entry.params.filter(function (p) {
|
|
590
|
+
return p.name === name;
|
|
591
|
+
})[0];
|
|
592
|
+
if (existing && !(existing.type === "msg" && existing.value === field.path)) name = uniqueName(entry, name);
|
|
593
|
+
if (!existing || existing.name !== name) {
|
|
594
|
+
entry.params.push({ name: name, type: "msg", value: field.path, as: "auto", asArgs: "" });
|
|
595
|
+
paramsChanged(entry, true);
|
|
596
|
+
}
|
|
597
|
+
insertAtCursor(":" + name);
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
function insertAtCursor(text) {
|
|
601
|
+
if (editor && monacoReady) {
|
|
602
|
+
const sel = editor.getSelection();
|
|
603
|
+
editor.executeEdits("fcdb", [{ range: sel, text: text, forceMoveMarkers: true }]);
|
|
604
|
+
editor.focus();
|
|
605
|
+
} else if (fallbackEl) {
|
|
606
|
+
const el = fallbackEl[0];
|
|
607
|
+
const at = el.selectionStart || 0;
|
|
608
|
+
el.value = el.value.slice(0, at) + text + el.value.slice(el.selectionEnd || at);
|
|
609
|
+
fallbackEl.trigger("input");
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
function loadColumnTypes(product) {
|
|
614
|
+
if (!product) return $.Deferred().resolve([]).promise();
|
|
615
|
+
if (columnTypes[product]) return $.Deferred().resolve(columnTypes[product]).promise();
|
|
616
|
+
return $.getJSON("fromcubes-db/types/" + encodeURIComponent(product)).then(
|
|
617
|
+
function (types) {
|
|
618
|
+
columnTypes[product] = types;
|
|
619
|
+
return types;
|
|
620
|
+
},
|
|
621
|
+
function () {
|
|
622
|
+
columnTypes[product] = [];
|
|
623
|
+
return $.Deferred().resolve([]).promise();
|
|
624
|
+
},
|
|
625
|
+
);
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
function fillTypes(select, args, product, selected) {
|
|
629
|
+
select.empty();
|
|
630
|
+
const generic = $("<optgroup/>", { label: "generic" }).appendTo(select);
|
|
631
|
+
PARAM_TYPES.forEach(function (t) {
|
|
632
|
+
generic.append($("<option/>", { value: t }).text(t));
|
|
633
|
+
});
|
|
634
|
+
loadColumnTypes(product).then(function (types) {
|
|
635
|
+
const groups = {};
|
|
636
|
+
types.forEach(function (t) {
|
|
637
|
+
if (!groups[t.category]) groups[t.category] = $("<optgroup/>", { label: t.category }).appendTo(select);
|
|
638
|
+
let suffix = t.args === "none" ? "" : t.args === "precision" ? "(p,s)" : t.args === "type" ? "(…)" : "(n)";
|
|
639
|
+
if (t.since) suffix += " — " + t.since + "+";
|
|
640
|
+
groups[t.category].append($("<option/>", { value: t.name }).text(t.name + suffix));
|
|
641
|
+
});
|
|
642
|
+
select.val(selected);
|
|
643
|
+
if (select.val() === null) {
|
|
644
|
+
$("<optgroup/>", { label: "not in " + (product || "this database") })
|
|
645
|
+
.append($("<option/>", { value: selected }).text(selected))
|
|
646
|
+
.appendTo(select);
|
|
647
|
+
select.val(selected);
|
|
648
|
+
}
|
|
649
|
+
const meta = types.filter(function (t) {
|
|
650
|
+
return t.name === selected;
|
|
651
|
+
})[0];
|
|
652
|
+
args.toggle(!!meta && meta.args !== "none");
|
|
653
|
+
args.attr(
|
|
654
|
+
"placeholder",
|
|
655
|
+
meta && meta.args === "precision" ? "p,s" : meta && meta.args === "type" ? "Int32" : "n",
|
|
656
|
+
);
|
|
657
|
+
});
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
function renderParams() {
|
|
661
|
+
const entry = entries[activeId];
|
|
662
|
+
paramsEl.empty();
|
|
663
|
+
if (!entry) return;
|
|
664
|
+
const product = productOf(entry);
|
|
665
|
+
const rows = M.rowsFor(entry.params, entry.preview);
|
|
666
|
+
const table = $('<div class="fcdb-params"></div>').appendTo(paramsEl);
|
|
667
|
+
rows.forEach(function (row) {
|
|
668
|
+
const el = $('<div class="fcdb-param"></div>').attr(
|
|
669
|
+
"data-row",
|
|
670
|
+
row.kind + ":" + (row.kind === "param" ? row.index : row.name),
|
|
671
|
+
);
|
|
672
|
+
if (row.kind === "missing") {
|
|
673
|
+
el.addClass("fcdb-param--missing");
|
|
674
|
+
$('<span class="fcdb-param-name"></span>')
|
|
675
|
+
.text(":" + row.name)
|
|
676
|
+
.appendTo(el);
|
|
677
|
+
$('<span class="fcdb-param-hint"></span>').text("in the SQL, no value bound").appendTo(el);
|
|
678
|
+
$('<button type="button" class="red-ui-button red-ui-button-small"><i class="fa fa-link"></i> bind</button>')
|
|
679
|
+
.attr("title", "Bind :" + row.name + " to msg." + M.suggestPath(row.name, M.messageFields(entry.sampleMsg)))
|
|
680
|
+
.on("click", function () {
|
|
681
|
+
addParam(entry, M.newParam(row.name, M.messageFields(entry.sampleMsg)));
|
|
682
|
+
})
|
|
683
|
+
.appendTo(el);
|
|
684
|
+
el.appendTo(table);
|
|
685
|
+
return;
|
|
686
|
+
}
|
|
687
|
+
const p = entry.params[row.index];
|
|
688
|
+
$('<span class="fcdb-param-colon">:</span>').appendTo(el);
|
|
689
|
+
const name = $('<input type="text" class="fcdb-param-name-input" placeholder="name">')
|
|
690
|
+
.val(p.name)
|
|
691
|
+
.on("input", function () {
|
|
692
|
+
p.name = this.value.trim();
|
|
693
|
+
paramsChanged(entry, false);
|
|
694
|
+
})
|
|
695
|
+
.appendTo(el);
|
|
696
|
+
const valueWrap = $('<span class="fcdb-param-value"></span>').appendTo(el);
|
|
697
|
+
const value = $('<input type="text">').appendTo(valueWrap);
|
|
698
|
+
value.typedInput({ default: p.type || "msg", types: SOURCES });
|
|
699
|
+
value.typedInput("type", p.type || "msg");
|
|
700
|
+
value.typedInput("value", p.value === undefined ? "" : p.value);
|
|
701
|
+
value.on("change", function () {
|
|
702
|
+
p.type = value.typedInput("type");
|
|
703
|
+
p.value = value.typedInput("value");
|
|
704
|
+
paramsChanged(entry, false);
|
|
705
|
+
});
|
|
706
|
+
const as = $('<select class="fcdb-param-as" title="declared type"></select>').appendTo(el);
|
|
707
|
+
const args = $('<input type="text" class="fcdb-param-args" title="length, precision or inner type">')
|
|
708
|
+
.val(p.asArgs || "")
|
|
709
|
+
.on("input", function () {
|
|
710
|
+
p.asArgs = this.value.trim();
|
|
711
|
+
paramsChanged(entry, false);
|
|
712
|
+
})
|
|
713
|
+
.appendTo(el);
|
|
714
|
+
fillTypes(as, args, product, p.as || "auto");
|
|
715
|
+
as.on("change", function () {
|
|
716
|
+
p.as = as.val() || "auto";
|
|
717
|
+
if (p.as === "auto") p.asArgs = "";
|
|
718
|
+
fillTypes(as, args, product, p.as);
|
|
719
|
+
paramsChanged(entry, false);
|
|
720
|
+
});
|
|
721
|
+
$('<span class="fcdb-param-result"></span>').appendTo(el);
|
|
722
|
+
$(
|
|
723
|
+
'<button type="button" class="red-ui-button red-ui-button-small fcdb-param-remove" title="Remove"><i class="fa fa-times"></i></button>',
|
|
724
|
+
)
|
|
725
|
+
.on("click", function () {
|
|
726
|
+
entry.params.splice(row.index, 1);
|
|
727
|
+
paramsChanged(entry, true);
|
|
728
|
+
})
|
|
729
|
+
.appendTo(el);
|
|
730
|
+
void name;
|
|
731
|
+
el.appendTo(table);
|
|
732
|
+
});
|
|
733
|
+
$(
|
|
734
|
+
'<button type="button" class="red-ui-button red-ui-button-small fcdb-param-add"><i class="fa fa-plus"></i> parameter</button>',
|
|
735
|
+
)
|
|
736
|
+
.on("click", function () {
|
|
737
|
+
addParam(entry, { name: uniqueName(entry, "param"), type: "msg", value: "payload", as: "auto", asArgs: "" });
|
|
738
|
+
})
|
|
739
|
+
.appendTo(paramsEl);
|
|
740
|
+
renderParamResults();
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
/** Status cells only — the inputs keep their focus while the preview updates. */
|
|
744
|
+
function renderParamResults() {
|
|
745
|
+
const entry = entries[activeId];
|
|
746
|
+
if (!entry) return;
|
|
747
|
+
const rows = M.rowsFor(entry.params, entry.preview);
|
|
748
|
+
const missing = rows
|
|
749
|
+
.filter(function (r) {
|
|
750
|
+
return r.kind === "missing";
|
|
751
|
+
})
|
|
752
|
+
.map(function (r) {
|
|
753
|
+
return r.name;
|
|
754
|
+
});
|
|
755
|
+
const shownMissing = paramsEl
|
|
756
|
+
.find(".fcdb-param--missing")
|
|
757
|
+
.map(function () {
|
|
758
|
+
return $(this).attr("data-row").slice("missing:".length);
|
|
759
|
+
})
|
|
760
|
+
.get();
|
|
761
|
+
if (missing.join("\u0000") !== shownMissing.join("\u0000")) {
|
|
762
|
+
// The SQL gained or lost a placeholder: the row set itself changed.
|
|
763
|
+
renderParams();
|
|
764
|
+
return;
|
|
765
|
+
}
|
|
766
|
+
rows.forEach(function (row) {
|
|
767
|
+
if (row.kind !== "param") return;
|
|
768
|
+
const el = paramsEl.find('[data-row="param:' + row.index + '"]');
|
|
769
|
+
const cell = el.find(".fcdb-param-result").empty().removeClass("ok error warn runtime");
|
|
770
|
+
el.toggleClass("fcdb-param--unused", !!row.name && !row.used && !!entry.preview && !entry.preview.error);
|
|
771
|
+
if (row.name && !row.used && entry.preview && !entry.preview.error) {
|
|
772
|
+
cell.addClass("warn").text("not in the SQL");
|
|
773
|
+
return;
|
|
774
|
+
}
|
|
775
|
+
if (row.status === "ok") {
|
|
776
|
+
cell.addClass("ok").attr("title", row.value && row.value.text);
|
|
777
|
+
$('<i class="fa fa-check"></i>').appendTo(cell);
|
|
778
|
+
$("<span>")
|
|
779
|
+
.text(" " + describeValue(row.value))
|
|
780
|
+
.appendTo(cell);
|
|
781
|
+
} else if (row.status === "error") {
|
|
782
|
+
cell.addClass("error").attr("title", row.message);
|
|
783
|
+
$('<i class="fa fa-times"></i>').appendTo(cell);
|
|
784
|
+
$("<span>")
|
|
785
|
+
.text(" " + row.message.replace(/^Parameter :\S+ — /, ""))
|
|
786
|
+
.appendTo(cell);
|
|
787
|
+
} else if (row.status === "missing" || row.status === "runtime") {
|
|
788
|
+
cell.addClass(row.status === "runtime" ? "runtime" : "warn").attr("title", row.message);
|
|
789
|
+
$('<i class="fa ' + (row.status === "runtime" ? "fa-clock-o" : "fa-question") + '"></i>').appendTo(cell);
|
|
790
|
+
$("<span>")
|
|
791
|
+
.text(" " + row.message)
|
|
792
|
+
.appendTo(cell);
|
|
793
|
+
}
|
|
794
|
+
});
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
function describeValue(v) {
|
|
798
|
+
if (!v) return "";
|
|
799
|
+
if (v.kind === "null") return "NULL";
|
|
800
|
+
if (v.kind === "undefined") return "undefined";
|
|
801
|
+
const text = v.kind === "string" ? JSON.stringify(v.text) : v.text;
|
|
802
|
+
return (text.length > 60 ? text.slice(0, 60) + "…" : text) + (v.as ? " · " + v.as : "");
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
// ── Rendering ──────────────────────────────────────────────
|
|
806
|
+
|
|
807
|
+
function renderTree() {
|
|
808
|
+
if (!treeEl) return;
|
|
809
|
+
treeEl.empty();
|
|
810
|
+
const groups = M.groupByFlow(queries, filterText);
|
|
811
|
+
if (!groups.length) {
|
|
812
|
+
$('<div class="fcdb-empty"></div>')
|
|
813
|
+
.text(queries.length ? "No query matches the filter." : "No db query nodes yet — drop one on the canvas.")
|
|
814
|
+
.appendTo(treeEl);
|
|
815
|
+
return;
|
|
816
|
+
}
|
|
817
|
+
groups.forEach(function (g) {
|
|
818
|
+
const head = $('<div class="fcdb-row fcdb-row--flow"></div>').appendTo(treeEl);
|
|
819
|
+
$('<i class="fa fa-sitemap fcdb-icon"></i>').appendTo(head);
|
|
820
|
+
$('<span class="fcdb-name"></span>').text(g.flowLabel).appendTo(head);
|
|
821
|
+
if (g.disabled) head.addClass("fcdb-off").attr("title", "This flow is disabled");
|
|
822
|
+
g.items.forEach(function (q) {
|
|
823
|
+
const row = $('<div class="fcdb-row fcdb-row--query"></div>').attr("data-id", q.id).appendTo(treeEl);
|
|
824
|
+
if (q.id === activeId) row.addClass("fcdb-active");
|
|
825
|
+
$('<i class="fa fa-database fcdb-icon"></i>').appendTo(row);
|
|
826
|
+
$('<span class="fcdb-name"></span>').text(q.label).appendTo(row);
|
|
827
|
+
$('<span class="fcdb-conn"></span>')
|
|
828
|
+
.text(q.connectionLabel ? q.product || q.connectionLabel : "no connection")
|
|
829
|
+
.appendTo(row);
|
|
830
|
+
const node = RED.nodes.node(q.id);
|
|
831
|
+
const entry = entries[q.id];
|
|
832
|
+
if ((entry && isDirty(entry)) || (node && node.changed)) {
|
|
833
|
+
$('<span class="fcdb-mark fcdb-mark--dirty" title="Changed, not deployed">●</span>').appendTo(row);
|
|
834
|
+
}
|
|
835
|
+
if (entry && entry.preview && previewHasProblem(entry.preview)) {
|
|
836
|
+
$('<span class="fcdb-mark fcdb-mark--error" title="A parameter or the SQL has a problem">●</span>').appendTo(
|
|
837
|
+
row,
|
|
838
|
+
);
|
|
839
|
+
}
|
|
840
|
+
if (q.disabled) {
|
|
841
|
+
row
|
|
842
|
+
.addClass("fcdb-off")
|
|
843
|
+
.attr("title", q.disabled === "node" ? "This node is disabled" : "Its flow is disabled");
|
|
844
|
+
}
|
|
845
|
+
row.on("click", function () {
|
|
846
|
+
openQuery(q.id);
|
|
847
|
+
});
|
|
848
|
+
row.on("contextmenu", function (ev) {
|
|
849
|
+
ev.preventDefault();
|
|
850
|
+
close();
|
|
851
|
+
if (RED.view && RED.view.reveal) RED.view.reveal(q.id);
|
|
852
|
+
});
|
|
853
|
+
});
|
|
854
|
+
});
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
function previewHasProblem(p) {
|
|
858
|
+
if (p.error) return p.error.kind === "sql";
|
|
859
|
+
return (
|
|
860
|
+
(p.missing && p.missing.length > 0) ||
|
|
861
|
+
(p.params || []).some(function (r) {
|
|
862
|
+
return r.status === "error" && r.used;
|
|
863
|
+
})
|
|
864
|
+
);
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
function renderTabs() {
|
|
868
|
+
if (!tabsEl) return;
|
|
869
|
+
tabsEl.empty();
|
|
870
|
+
openIds.forEach(function (id) {
|
|
871
|
+
const node = RED.nodes.node(id);
|
|
872
|
+
const entry = entries[id];
|
|
873
|
+
const tab = $('<div class="fcdb-tab"></div>')
|
|
874
|
+
.toggleClass("fcdb-active", id === activeId)
|
|
875
|
+
.appendTo(tabsEl);
|
|
876
|
+
$('<i class="fa fa-database"></i>').appendTo(tab);
|
|
877
|
+
$("<span>")
|
|
878
|
+
.text(node ? node.name || "db query" : "(deleted)")
|
|
879
|
+
.appendTo(tab);
|
|
880
|
+
if (entry && isDirty(entry)) $('<span class="fcdb-mark fcdb-mark--dirty">●</span>').appendTo(tab);
|
|
881
|
+
$('<span class="fcdb-tab-close" title="Close tab">×</span>')
|
|
882
|
+
.on("click", function (ev) {
|
|
883
|
+
ev.stopPropagation();
|
|
884
|
+
closeTab(id);
|
|
885
|
+
})
|
|
886
|
+
.appendTo(tab);
|
|
887
|
+
tab.on("click", function () {
|
|
888
|
+
setActive(id);
|
|
889
|
+
});
|
|
890
|
+
});
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
function renderConnection() {
|
|
894
|
+
const entry = entries[activeId];
|
|
895
|
+
connSelect.empty();
|
|
896
|
+
$("<option/>", { value: "" }).text("— pick a connection —").appendTo(connSelect);
|
|
897
|
+
connections().forEach(function (c) {
|
|
898
|
+
$("<option/>", { value: c.id }).text(label(c)).appendTo(connSelect);
|
|
899
|
+
});
|
|
900
|
+
connSelect.val(entry ? entry.connection : "");
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
function renderSampleStatus() {
|
|
904
|
+
const entry = entries[activeId];
|
|
905
|
+
sampleStatus.removeClass("error").text("");
|
|
906
|
+
if (!entry) return;
|
|
907
|
+
if (entry.sampleError) sampleStatus.addClass("error").text("Not JSON: " + entry.sampleError);
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
function renderFields() {
|
|
911
|
+
const entry = entries[activeId];
|
|
912
|
+
fieldsEl.empty();
|
|
913
|
+
if (!entry) return;
|
|
914
|
+
const fields = M.messageFields(entry.sampleMsg);
|
|
915
|
+
if (!fields.length) {
|
|
916
|
+
$('<div class="fcdb-empty"></div>').text("Type a message on the left, or capture one.").appendTo(fieldsEl);
|
|
917
|
+
return;
|
|
918
|
+
}
|
|
919
|
+
fields.forEach(function (f) {
|
|
920
|
+
const row = $('<div class="fcdb-field"></div>')
|
|
921
|
+
.css("padding-left", 6 + f.depth * 12 + "px")
|
|
922
|
+
.attr(
|
|
923
|
+
"title",
|
|
924
|
+
f.kind === "object"
|
|
925
|
+
? "msg." + f.path
|
|
926
|
+
: "Click: write :" + M.nameFor(f) + " at the cursor, bound to msg." + f.path,
|
|
927
|
+
)
|
|
928
|
+
.appendTo(fieldsEl);
|
|
929
|
+
$('<span class="fcdb-field-key"></span>').text(f.key).appendTo(row);
|
|
930
|
+
$('<span class="fcdb-field-preview"></span>').text(f.preview).appendTo(row);
|
|
931
|
+
if (f.kind === "object") row.addClass("fcdb-field--branch");
|
|
932
|
+
else
|
|
933
|
+
row.on("click", function () {
|
|
934
|
+
bindField(f);
|
|
935
|
+
});
|
|
936
|
+
});
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
function renderReceives() {
|
|
940
|
+
const entry = entries[activeId];
|
|
941
|
+
receivesEl.empty();
|
|
942
|
+
if (!entry || !entry.preview) return;
|
|
943
|
+
const p = entry.preview;
|
|
944
|
+
if (p.error) {
|
|
945
|
+
$('<div class="fcdb-problem"></div>').text(p.error.message).appendTo(receivesEl);
|
|
946
|
+
return;
|
|
947
|
+
}
|
|
948
|
+
const c = p.compiled || {};
|
|
949
|
+
if (c.error) {
|
|
950
|
+
$('<div class="fcdb-problem"></div>').text(c.error).appendTo(receivesEl);
|
|
951
|
+
return;
|
|
952
|
+
}
|
|
953
|
+
$('<div class="fcdb-cap"></div>').text("Statement — the same text whatever the values are").appendTo(receivesEl);
|
|
954
|
+
$('<pre class="fcdb-sql"></pre>').text(c.text).appendTo(receivesEl);
|
|
955
|
+
$('<div class="fcdb-cap"></div>')
|
|
956
|
+
.text("Values — sent apart from it, " + (c.binding === "named" ? "by name" : "in order"))
|
|
957
|
+
.appendTo(receivesEl);
|
|
958
|
+
const list = $('<div class="fcdb-values"></div>').appendTo(receivesEl);
|
|
959
|
+
const vals = c.values || {};
|
|
960
|
+
const keys = Array.isArray(vals)
|
|
961
|
+
? vals.map(function (_v, i) {
|
|
962
|
+
return i;
|
|
963
|
+
})
|
|
964
|
+
: Object.keys(vals);
|
|
965
|
+
if (!keys.length)
|
|
966
|
+
$('<div class="fcdb-empty"></div>').text("No values: the statement has no placeholders.").appendTo(list);
|
|
967
|
+
keys.forEach(function (k) {
|
|
968
|
+
const row = $('<div class="fcdb-value"></div>').appendTo(list);
|
|
969
|
+
$('<span class="fcdb-value-key"></span>')
|
|
970
|
+
.text(Array.isArray(vals) ? "#" + (k + 1) : k)
|
|
971
|
+
.appendTo(row);
|
|
972
|
+
$('<span class="fcdb-value-text"></span>').text(describeValue(vals[k])).appendTo(row);
|
|
973
|
+
});
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
function renderPreview() {
|
|
977
|
+
const entry = entries[activeId];
|
|
978
|
+
if (!entry) return;
|
|
979
|
+
renderParamResults();
|
|
980
|
+
renderReceives();
|
|
981
|
+
applyMarkers(entry);
|
|
982
|
+
renderTree();
|
|
983
|
+
updateStatus();
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
function updateStatus() {
|
|
987
|
+
if (!statusEl) return;
|
|
988
|
+
const entry = entries[activeId];
|
|
989
|
+
const node = entry ? RED.nodes.node(entry.id) : null;
|
|
990
|
+
const flow = node ? flowOf(node.z) : null;
|
|
991
|
+
statusEl.text(node ? (flow ? (flow.label || flow.name) + " / " : "") + (node.name || "db query") : "");
|
|
992
|
+
if (deployBtn) deployBtn.toggleClass("disabled", !RED.nodes.dirty());
|
|
993
|
+
}
|
|
994
|
+
|
|
995
|
+
function renderPanelTab() {
|
|
996
|
+
panelEl.find(".fcdb-panel-tab").each(function () {
|
|
997
|
+
$(this).toggleClass("fcdb-active", $(this).attr("data-tab") === settings.panelTab);
|
|
998
|
+
});
|
|
999
|
+
panelEl.find(".fcdb-panel-input").toggle(settings.panelTab === "input");
|
|
1000
|
+
panelEl.find(".fcdb-panel-receives").toggle(settings.panelTab === "receives");
|
|
1001
|
+
if (sampleEditor) sampleEditor.layout();
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
function renderAll() {
|
|
1005
|
+
renderTree();
|
|
1006
|
+
renderTabs();
|
|
1007
|
+
updateStatus();
|
|
1008
|
+
const has = !!entries[activeId];
|
|
1009
|
+
placeholderEl.toggle(!has);
|
|
1010
|
+
panelEl.toggle(has);
|
|
1011
|
+
if (monacoFailed) {
|
|
1012
|
+
monacoHost.hide();
|
|
1013
|
+
fallbackEl.toggle(has);
|
|
1014
|
+
sampleHost.hide();
|
|
1015
|
+
sampleFallback.show();
|
|
1016
|
+
} else {
|
|
1017
|
+
monacoHost.toggle(has);
|
|
1018
|
+
fallbackEl.hide();
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
1021
|
+
|
|
1022
|
+
// ── Opening, switching, closing ────────────────────────────
|
|
1023
|
+
|
|
1024
|
+
function openQuery(id) {
|
|
1025
|
+
if (!ensureEntry(id)) return;
|
|
1026
|
+
if (openIds.indexOf(id) === -1) openIds.push(id);
|
|
1027
|
+
setActive(id);
|
|
1028
|
+
}
|
|
1029
|
+
|
|
1030
|
+
function setActive(id) {
|
|
1031
|
+
const prev = entries[activeId];
|
|
1032
|
+
if (prev && editor && !modelGone(prev)) prev.viewState = editor.saveViewState();
|
|
1033
|
+
activeId = id;
|
|
1034
|
+
const entry = ensureEntry(id);
|
|
1035
|
+
if (!entry) return;
|
|
1036
|
+
stopCapture();
|
|
1037
|
+
if (monacoReady) {
|
|
1038
|
+
editor.setModel(entry.model);
|
|
1039
|
+
if (entry.viewState) editor.restoreViewState(entry.viewState);
|
|
1040
|
+
sampleEditor.setModel(entry.sampleModel);
|
|
1041
|
+
editor.focus();
|
|
1042
|
+
} else {
|
|
1043
|
+
fallbackEl.val(entry.sql);
|
|
1044
|
+
sampleFallback.val(entry.sample);
|
|
1045
|
+
}
|
|
1046
|
+
renderConnection();
|
|
1047
|
+
renderSampleStatus();
|
|
1048
|
+
renderFields();
|
|
1049
|
+
renderParams();
|
|
1050
|
+
renderReceives();
|
|
1051
|
+
renderAll();
|
|
1052
|
+
runPreview();
|
|
1053
|
+
}
|
|
1054
|
+
|
|
1055
|
+
function closeTab(id) {
|
|
1056
|
+
flush(id);
|
|
1057
|
+
const i = openIds.indexOf(id);
|
|
1058
|
+
if (i !== -1) openIds.splice(i, 1);
|
|
1059
|
+
if (activeId === id) {
|
|
1060
|
+
activeId = null;
|
|
1061
|
+
const next = openIds[Math.min(i, openIds.length - 1)];
|
|
1062
|
+
if (next) setActive(next);
|
|
1063
|
+
else {
|
|
1064
|
+
if (editor) editor.setModel(null);
|
|
1065
|
+
if (sampleEditor) sampleEditor.setModel(null);
|
|
1066
|
+
}
|
|
1067
|
+
}
|
|
1068
|
+
renderAll();
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
function trayOpen() {
|
|
1072
|
+
return $("#red-ui-tray").length > 0 || $(".red-ui-tray").length > 0;
|
|
1073
|
+
}
|
|
1074
|
+
|
|
1075
|
+
function deploy() {
|
|
1076
|
+
flushAll();
|
|
1077
|
+
if (!RED.nodes.dirty()) {
|
|
1078
|
+
RED.notify("Nothing to deploy", "info");
|
|
1079
|
+
return;
|
|
1080
|
+
}
|
|
1081
|
+
try {
|
|
1082
|
+
RED.actions.invoke("core:deploy-flows");
|
|
1083
|
+
} catch (err) {
|
|
1084
|
+
$("#red-ui-header-button-deploy").trigger("click");
|
|
1085
|
+
}
|
|
1086
|
+
}
|
|
1087
|
+
|
|
1088
|
+
function open(id) {
|
|
1089
|
+
if (trayOpen()) {
|
|
1090
|
+
RED.notify("Close the node edit dialog first", "warning");
|
|
1091
|
+
return;
|
|
1092
|
+
}
|
|
1093
|
+
buildOverlay();
|
|
1094
|
+
collect();
|
|
1095
|
+
overlay.removeAttr("hidden");
|
|
1096
|
+
$(document.body).addClass("fcdb-sql-open");
|
|
1097
|
+
$(document).on("keydown.fcdb-sql", onKeyDown);
|
|
1098
|
+
ensureMonaco(function () {
|
|
1099
|
+
if (id) openQuery(id);
|
|
1100
|
+
else openDefault();
|
|
1101
|
+
renderAll();
|
|
1102
|
+
if (editor) editor.layout();
|
|
1103
|
+
if (sampleEditor) sampleEditor.layout();
|
|
1104
|
+
});
|
|
1105
|
+
}
|
|
1106
|
+
|
|
1107
|
+
/** The canvas tab you are on is the best guess; keep your place if it is there. */
|
|
1108
|
+
function openDefault() {
|
|
1109
|
+
if (activeId && entries[activeId] && RED.nodes.node(activeId)) {
|
|
1110
|
+
setActive(activeId);
|
|
1111
|
+
return;
|
|
1112
|
+
}
|
|
1113
|
+
const flowId = RED.workspaces && RED.workspaces.active ? RED.workspaces.active() : null;
|
|
1114
|
+
const pick =
|
|
1115
|
+
queries.filter(function (q) {
|
|
1116
|
+
return q.flowId === flowId;
|
|
1117
|
+
})[0] ||
|
|
1118
|
+
queries.filter(function (q) {
|
|
1119
|
+
return !q.disabled;
|
|
1120
|
+
})[0] ||
|
|
1121
|
+
queries[0];
|
|
1122
|
+
if (pick) openQuery(pick.id);
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
/** From a node's edit dialog: save and close it (its oneditsave owns the same fields), then open. */
|
|
1126
|
+
function openNode(id) {
|
|
1127
|
+
if (trayOpen()) $("#node-dialog-ok").trigger("click");
|
|
1128
|
+
let waited = 0;
|
|
1129
|
+
const tick = function () {
|
|
1130
|
+
if (!trayOpen()) return open(id);
|
|
1131
|
+
waited += 100;
|
|
1132
|
+
if (waited > 3000) return RED.notify("Close the node edit dialog first", "warning");
|
|
1133
|
+
setTimeout(tick, 100);
|
|
1134
|
+
};
|
|
1135
|
+
setTimeout(tick, 100);
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
function close() {
|
|
1139
|
+
if (!overlay) return;
|
|
1140
|
+
const entry = entries[activeId];
|
|
1141
|
+
if (entry && editor && !modelGone(entry)) entry.viewState = editor.saveViewState();
|
|
1142
|
+
stopCapture();
|
|
1143
|
+
flushAll();
|
|
1144
|
+
overlay.attr("hidden", true);
|
|
1145
|
+
$(document.body).removeClass("fcdb-sql-open");
|
|
1146
|
+
$(document).off("keydown.fcdb-sql");
|
|
1147
|
+
}
|
|
1148
|
+
|
|
1149
|
+
function onKeyDown(ev) {
|
|
1150
|
+
if (ev.key === "Escape") {
|
|
1151
|
+
// Esc inside a typedInput menu or Monaco's suggest list closes that, not the editor.
|
|
1152
|
+
if ($(".red-ui-typedInput-options:visible").length) return;
|
|
1153
|
+
if ($(ev.target).closest(".monaco-editor").length && $(".suggest-widget.visible").length) return;
|
|
1154
|
+
ev.preventDefault();
|
|
1155
|
+
ev.stopPropagation();
|
|
1156
|
+
close();
|
|
1157
|
+
return;
|
|
1158
|
+
}
|
|
1159
|
+
if ((ev.ctrlKey || ev.metaKey) && (ev.key === "s" || ev.key === "S")) {
|
|
1160
|
+
ev.preventDefault();
|
|
1161
|
+
if (monacoHost && $.contains(monacoHost[0], ev.target)) return; // Monaco's own action
|
|
1162
|
+
deploy();
|
|
1163
|
+
}
|
|
1164
|
+
}
|
|
1165
|
+
|
|
1166
|
+
// ── Capture: the next message the deployed node receives ───
|
|
1167
|
+
|
|
1168
|
+
function startCapture() {
|
|
1169
|
+
const entry = entries[activeId];
|
|
1170
|
+
if (!entry) return;
|
|
1171
|
+
const id = entry.id;
|
|
1172
|
+
$.ajax({ url: "fromcubes-db/sql/capture/" + encodeURIComponent(id), method: "POST" })
|
|
1173
|
+
.done(function () {
|
|
1174
|
+
captureBtn.addClass("fcdb-waiting").html('<i class="fa fa-circle-o-notch fa-spin"></i> waiting for a message…');
|
|
1175
|
+
sampleStatus.removeClass("error").text("Send a message into this node — the next one is copied here.");
|
|
1176
|
+
const poll = function () {
|
|
1177
|
+
$.getJSON("fromcubes-db/sql/capture/" + encodeURIComponent(id)).done(function (got) {
|
|
1178
|
+
if (activeId !== id) return;
|
|
1179
|
+
if (got.msg) {
|
|
1180
|
+
stopCapture(true);
|
|
1181
|
+
setSample(entries[id], JSON.stringify(got.msg, null, 2) + "\n");
|
|
1182
|
+
sampleStatus.text("Captured " + new Date(got.at).toLocaleTimeString());
|
|
1183
|
+
return;
|
|
1184
|
+
}
|
|
1185
|
+
if (got.error) {
|
|
1186
|
+
stopCapture(true);
|
|
1187
|
+
sampleStatus.addClass("error").text("The message " + got.error);
|
|
1188
|
+
return;
|
|
1189
|
+
}
|
|
1190
|
+
if (!got.waiting) {
|
|
1191
|
+
stopCapture(true);
|
|
1192
|
+
sampleStatus.text("No message arrived in five minutes.");
|
|
1193
|
+
return;
|
|
1194
|
+
}
|
|
1195
|
+
captureTimer = setTimeout(poll, CAPTURE_POLL_MS);
|
|
1196
|
+
});
|
|
1197
|
+
};
|
|
1198
|
+
captureTimer = setTimeout(poll, CAPTURE_POLL_MS);
|
|
1199
|
+
})
|
|
1200
|
+
.fail(function (xhr) {
|
|
1201
|
+
RED.notify(
|
|
1202
|
+
xhr.status === 404
|
|
1203
|
+
? "Deploy this node first — only a running node can capture."
|
|
1204
|
+
: "Capture failed (" + xhr.status + ")",
|
|
1205
|
+
"warning",
|
|
1206
|
+
);
|
|
1207
|
+
});
|
|
1208
|
+
}
|
|
1209
|
+
|
|
1210
|
+
function stopCapture(arrived) {
|
|
1211
|
+
if (captureTimer === null && !(captureBtn && captureBtn.hasClass("fcdb-waiting"))) return;
|
|
1212
|
+
clearTimeout(captureTimer);
|
|
1213
|
+
captureTimer = null;
|
|
1214
|
+
if (captureBtn) captureBtn.removeClass("fcdb-waiting").html('<i class="fa fa-download"></i> Capture next message');
|
|
1215
|
+
if (!arrived) {
|
|
1216
|
+
openIds.forEach(function (id) {
|
|
1217
|
+
$.ajax({ url: "fromcubes-db/sql/capture/" + encodeURIComponent(id), method: "DELETE" });
|
|
1218
|
+
});
|
|
1219
|
+
}
|
|
1220
|
+
}
|
|
1221
|
+
|
|
1222
|
+
// ── Overlay ────────────────────────────────────────────────
|
|
1223
|
+
|
|
1224
|
+
function buildOverlay() {
|
|
1225
|
+
if (overlay) return;
|
|
1226
|
+
overlay = $('<div id="fcdb-sql-overlay" hidden></div>');
|
|
1227
|
+
|
|
1228
|
+
const header = $('<div class="fcdb-header"></div>').appendTo(overlay);
|
|
1229
|
+
const brand = $('<div class="fcdb-brand"></div>').appendTo(header);
|
|
1230
|
+
$('<img class="fcdb-mark-logo" alt="fromcubes">')
|
|
1231
|
+
.attr("src", ASSET_BASE + "editor/mark.svg")
|
|
1232
|
+
.appendTo(brand);
|
|
1233
|
+
const brandText = $('<span class="fcdb-brand-text"></span>').appendTo(brand);
|
|
1234
|
+
$('<span class="fcdb-brand-name">fromcubes-</span>').appendTo(brandText);
|
|
1235
|
+
$('<span class="fcdb-brand-accent">db</span>').appendTo(brandText);
|
|
1236
|
+
statusEl = $('<div class="fcdb-status"></div>').appendTo(header);
|
|
1237
|
+
$('<div class="fcdb-spacer"></div>').appendTo(header);
|
|
1238
|
+
deployBtn = $(
|
|
1239
|
+
'<button type="button" class="red-ui-button red-ui-button-small fcdb-deploy">' +
|
|
1240
|
+
'<i class="fa fa-cloud-upload"></i> Deploy <span class="fcdb-kbd">Ctrl+S</span></button>',
|
|
1241
|
+
)
|
|
1242
|
+
.on("click", function (ev) {
|
|
1243
|
+
ev.preventDefault();
|
|
1244
|
+
deploy();
|
|
1245
|
+
})
|
|
1246
|
+
.appendTo(header);
|
|
1247
|
+
$(
|
|
1248
|
+
'<button type="button" class="red-ui-button red-ui-button-small fcdb-close"><i class="fa fa-times"></i> Close <span class="fcdb-kbd">Esc</span></button>',
|
|
1249
|
+
)
|
|
1250
|
+
.on("click", close)
|
|
1251
|
+
.appendTo(header);
|
|
1252
|
+
|
|
1253
|
+
const body = $('<div class="fcdb-body"></div>').appendTo(overlay);
|
|
1254
|
+
const treePane = $('<div class="fcdb-tree-pane"></div>')
|
|
1255
|
+
.css("width", settings.treeWidth + "px")
|
|
1256
|
+
.appendTo(body);
|
|
1257
|
+
filterEl = $('<input type="text" class="fcdb-filter" placeholder="Filter queries…">')
|
|
1258
|
+
.on("input", function () {
|
|
1259
|
+
filterText = this.value;
|
|
1260
|
+
renderTree();
|
|
1261
|
+
})
|
|
1262
|
+
.appendTo($('<div class="fcdb-filter-row"></div>').appendTo(treePane));
|
|
1263
|
+
treeEl = $('<div class="fcdb-tree"></div>').appendTo(treePane);
|
|
1264
|
+
|
|
1265
|
+
dragSplitter(
|
|
1266
|
+
$('<div class="fcdb-vsplit"></div>').appendTo(body),
|
|
1267
|
+
"x",
|
|
1268
|
+
function (start, delta) {
|
|
1269
|
+
settings.treeWidth = Math.max(150, start.tree + delta);
|
|
1270
|
+
treePane.css("width", settings.treeWidth + "px");
|
|
1271
|
+
},
|
|
1272
|
+
function () {
|
|
1273
|
+
return { tree: treePane.width() };
|
|
1274
|
+
},
|
|
1275
|
+
);
|
|
1276
|
+
|
|
1277
|
+
const main = $('<div class="fcdb-main"></div>').appendTo(body);
|
|
1278
|
+
tabsEl = $('<div class="fcdb-tabs"></div>').appendTo(main);
|
|
1279
|
+
placeholderEl = $('<div class="fcdb-placeholder"></div>')
|
|
1280
|
+
.html(
|
|
1281
|
+
"Pick a query on the left.<br /><kbd>Esc</kbd> closes · <kbd>Ctrl/Cmd+S</kbd> deploys · <kbd>:</kbd> offers parameters",
|
|
1282
|
+
)
|
|
1283
|
+
.appendTo(main);
|
|
1284
|
+
monacoHost = $('<div class="fcdb-monaco"></div>').hide().appendTo(main);
|
|
1285
|
+
fallbackEl = $('<textarea class="fcdb-fallback" spellcheck="false"></textarea>')
|
|
1286
|
+
.hide()
|
|
1287
|
+
.on("input", function () {
|
|
1288
|
+
const entry = entries[activeId];
|
|
1289
|
+
if (!entry) return;
|
|
1290
|
+
entry.sql = this.value;
|
|
1291
|
+
scheduleFlush(entry.id);
|
|
1292
|
+
schedulePreview();
|
|
1293
|
+
})
|
|
1294
|
+
.appendTo(main);
|
|
1295
|
+
|
|
1296
|
+
const hsplit = $('<div class="fcdb-hsplit"></div>').appendTo(main);
|
|
1297
|
+
panelEl = $('<div class="fcdb-panel"></div>')
|
|
1298
|
+
.css("height", settings.panelHeight + "px")
|
|
1299
|
+
.hide()
|
|
1300
|
+
.appendTo(main);
|
|
1301
|
+
dragSplitter(
|
|
1302
|
+
hsplit,
|
|
1303
|
+
"y",
|
|
1304
|
+
function (start, delta) {
|
|
1305
|
+
settings.panelHeight = Math.max(120, Math.min(main.height() - 120, start.panel - delta));
|
|
1306
|
+
panelEl.css("height", settings.panelHeight + "px");
|
|
1307
|
+
},
|
|
1308
|
+
function () {
|
|
1309
|
+
return { panel: panelEl.height() };
|
|
1310
|
+
},
|
|
1311
|
+
);
|
|
1312
|
+
|
|
1313
|
+
const panelTabs = $('<div class="fcdb-panel-tabs"></div>').appendTo(panelEl);
|
|
1314
|
+
[
|
|
1315
|
+
["input", "fa-sign-in", "Input"],
|
|
1316
|
+
["receives", "fa-database", "What the database receives"],
|
|
1317
|
+
].forEach(function (t) {
|
|
1318
|
+
$('<div class="fcdb-panel-tab"></div>')
|
|
1319
|
+
.attr("data-tab", t[0])
|
|
1320
|
+
.html('<i class="fa ' + t[1] + '"></i> ')
|
|
1321
|
+
.append($("<span>").text(t[2]))
|
|
1322
|
+
.on("click", function () {
|
|
1323
|
+
settings.panelTab = t[0];
|
|
1324
|
+
saveJson(SETTINGS_KEY, settings);
|
|
1325
|
+
renderPanelTab();
|
|
1326
|
+
})
|
|
1327
|
+
.appendTo(panelTabs);
|
|
1328
|
+
});
|
|
1329
|
+
|
|
1330
|
+
const input = $('<div class="fcdb-panel-input"></div>').appendTo(panelEl);
|
|
1331
|
+
const msgCol = $('<div class="fcdb-col fcdb-col--msg"></div>').appendTo(input);
|
|
1332
|
+
const msgHead = $('<div class="fcdb-col-head"></div>').appendTo(msgCol);
|
|
1333
|
+
$("<span>").text("Sample message").appendTo(msgHead);
|
|
1334
|
+
captureBtn = $(
|
|
1335
|
+
'<button type="button" class="red-ui-button red-ui-button-small"><i class="fa fa-download"></i> Capture next message</button>',
|
|
1336
|
+
)
|
|
1337
|
+
.attr("title", "Copy the next message this deployed node receives")
|
|
1338
|
+
.on("click", function () {
|
|
1339
|
+
if (captureBtn.hasClass("fcdb-waiting")) stopCapture();
|
|
1340
|
+
else startCapture();
|
|
1341
|
+
})
|
|
1342
|
+
.appendTo(msgHead);
|
|
1343
|
+
sampleHost = $('<div class="fcdb-sample"></div>').appendTo(msgCol);
|
|
1344
|
+
sampleFallback = $('<textarea class="fcdb-sample-fallback" spellcheck="false"></textarea>')
|
|
1345
|
+
.hide()
|
|
1346
|
+
.on("input", function () {
|
|
1347
|
+
const entry = entries[activeId];
|
|
1348
|
+
if (entry) setSample(entry, this.value, true);
|
|
1349
|
+
})
|
|
1350
|
+
.appendTo(msgCol);
|
|
1351
|
+
sampleStatus = $('<div class="fcdb-sample-status"></div>').appendTo(msgCol);
|
|
1352
|
+
|
|
1353
|
+
const fieldCol = $('<div class="fcdb-col fcdb-col--fields"></div>').appendTo(input);
|
|
1354
|
+
$('<div class="fcdb-col-head"></div>').append($("<span>").text("Fields — click to bind")).appendTo(fieldCol);
|
|
1355
|
+
fieldsEl = $('<div class="fcdb-fields"></div>').appendTo(fieldCol);
|
|
1356
|
+
|
|
1357
|
+
const paramCol = $('<div class="fcdb-col fcdb-col--params"></div>').appendTo(input);
|
|
1358
|
+
const paramHead = $('<div class="fcdb-col-head"></div>').appendTo(paramCol);
|
|
1359
|
+
$("<span>").text("Connection").appendTo(paramHead);
|
|
1360
|
+
connSelect = $('<select class="fcdb-conn-select"></select>')
|
|
1361
|
+
.on("change", function () {
|
|
1362
|
+
const entry = entries[activeId];
|
|
1363
|
+
if (!entry) return;
|
|
1364
|
+
entry.connection = this.value;
|
|
1365
|
+
scheduleFlush(entry.id);
|
|
1366
|
+
renderParams();
|
|
1367
|
+
schedulePreview();
|
|
1368
|
+
})
|
|
1369
|
+
.appendTo(paramHead);
|
|
1370
|
+
paramsEl = $('<div class="fcdb-params-wrap"></div>').appendTo(paramCol);
|
|
1371
|
+
|
|
1372
|
+
receivesEl = $('<div class="fcdb-panel-receives"></div>').appendTo(panelEl);
|
|
1373
|
+
|
|
1374
|
+
$(document.body).append(overlay);
|
|
1375
|
+
renderPanelTab();
|
|
1376
|
+
}
|
|
1377
|
+
|
|
1378
|
+
function dragSplitter(el, axis, onMove, measure) {
|
|
1379
|
+
el.on("mousedown", function (ev) {
|
|
1380
|
+
ev.preventDefault();
|
|
1381
|
+
const origin = axis === "x" ? ev.pageX : ev.pageY;
|
|
1382
|
+
const start = measure();
|
|
1383
|
+
const move = function (e) {
|
|
1384
|
+
onMove(start, (axis === "x" ? e.pageX : e.pageY) - origin);
|
|
1385
|
+
if (editor) editor.layout();
|
|
1386
|
+
if (sampleEditor) sampleEditor.layout();
|
|
1387
|
+
};
|
|
1388
|
+
const up = function () {
|
|
1389
|
+
$(document).off("mousemove", move).off("mouseup", up);
|
|
1390
|
+
saveJson(SETTINGS_KEY, settings);
|
|
1391
|
+
};
|
|
1392
|
+
$(document).on("mousemove", move).on("mouseup", up);
|
|
1393
|
+
});
|
|
1394
|
+
}
|
|
1395
|
+
|
|
1396
|
+
// ── External changes: dialog save, undo, deploy, delete ────
|
|
1397
|
+
|
|
1398
|
+
function onFlowsChanged() {
|
|
1399
|
+
if (selfEmitting) return;
|
|
1400
|
+
clearTimeout(resyncTimer);
|
|
1401
|
+
resyncTimer = setTimeout(resync, 100);
|
|
1402
|
+
}
|
|
1403
|
+
|
|
1404
|
+
function resync() {
|
|
1405
|
+
collect();
|
|
1406
|
+
openIds.slice().forEach(function (id) {
|
|
1407
|
+
const entry = entries[id];
|
|
1408
|
+
const node = RED.nodes.node(id);
|
|
1409
|
+
if (!node) {
|
|
1410
|
+
closeTab(id);
|
|
1411
|
+
delete entries[id];
|
|
1412
|
+
return;
|
|
1413
|
+
}
|
|
1414
|
+
const nodeState = {
|
|
1415
|
+
sql: node.sql || "",
|
|
1416
|
+
params: M.normalizeParams(node.params),
|
|
1417
|
+
connection: node.connection || "",
|
|
1418
|
+
};
|
|
1419
|
+
const mine = { sql: entry.sql, params: M.normalizeParams(entry.params), connection: entry.connection };
|
|
1420
|
+
if (JSON.stringify(nodeState) === JSON.stringify(mine)) return;
|
|
1421
|
+
if (JSON.stringify(mine) === JSON.stringify(entry.synced)) {
|
|
1422
|
+
// Nothing unflushed here: the node is newer, take it.
|
|
1423
|
+
entry.synced = nodeState;
|
|
1424
|
+
entry.params = nodeState.params;
|
|
1425
|
+
entry.connection = nodeState.connection;
|
|
1426
|
+
if (entry.sql !== nodeState.sql) {
|
|
1427
|
+
entry.sql = nodeState.sql;
|
|
1428
|
+
if (!modelGone(entry)) entry.model.setValue(nodeState.sql);
|
|
1429
|
+
else if (id === activeId) fallbackEl.val(nodeState.sql);
|
|
1430
|
+
}
|
|
1431
|
+
if (id === activeId) {
|
|
1432
|
+
renderConnection();
|
|
1433
|
+
renderParams();
|
|
1434
|
+
schedulePreview();
|
|
1435
|
+
}
|
|
1436
|
+
} else {
|
|
1437
|
+
RED.notify((node.name || "db query") + " changed outside the SQL Editor — keeping your version.", {
|
|
1438
|
+
type: "warning",
|
|
1439
|
+
timeout: 6000,
|
|
1440
|
+
});
|
|
1441
|
+
}
|
|
1442
|
+
});
|
|
1443
|
+
if (overlay && !overlay.attr("hidden")) renderAll();
|
|
1444
|
+
}
|
|
1445
|
+
|
|
1446
|
+
// ── Entry points ───────────────────────────────────────────
|
|
1447
|
+
|
|
1448
|
+
function registerStatusBar() {
|
|
1449
|
+
if (!RED.statusBar || !RED.statusBar.add) return;
|
|
1450
|
+
const el = $('<button type="button" class="red-ui-footer-button fcdb-statusbar-btn"></button>')
|
|
1451
|
+
.attr("title", "fromcubes-db — open the " + EDITOR_NAME)
|
|
1452
|
+
.on("click", function (ev) {
|
|
1453
|
+
ev.preventDefault();
|
|
1454
|
+
open();
|
|
1455
|
+
});
|
|
1456
|
+
// The node icon as a mask filled with the button's own text colour: the
|
|
1457
|
+
// icon's dark ink would vanish on a dark Node-RED theme otherwise. Inlined
|
|
1458
|
+
// as a data URL — Chrome fetches a mask-image by URL in CORS mode and
|
|
1459
|
+
// draws nothing when that fails, where a data URL always lands.
|
|
1460
|
+
const icon = $('<span class="fcdb-statusbar-icon" aria-hidden="true"></span>').appendTo(el);
|
|
1461
|
+
$.ajax({ url: ASSET_BASE + "icons/fromcubes-db.svg", dataType: "text" }).done(function (svg) {
|
|
1462
|
+
const url = 'url("data:image/svg+xml,' + encodeURIComponent(svg) + '")';
|
|
1463
|
+
icon.css({ "-webkit-mask-image": url, "mask-image": url });
|
|
1464
|
+
});
|
|
1465
|
+
$("<span>").text(EDITOR_NAME).appendTo(el);
|
|
1466
|
+
RED.statusBar.add({ id: "fromcubes-db-sql-editor", align: "right", element: el });
|
|
1467
|
+
RED.actions.add("fromcubes:open-sql-editor", function () {
|
|
1468
|
+
open();
|
|
1469
|
+
});
|
|
1470
|
+
}
|
|
1471
|
+
|
|
1472
|
+
function init() {
|
|
1473
|
+
if (typeof RED === "undefined" || !RED.events || !window.__fcdbSqlModel) {
|
|
1474
|
+
setTimeout(init, 200);
|
|
1475
|
+
return;
|
|
1476
|
+
}
|
|
1477
|
+
M = window.__fcdbSqlModel;
|
|
1478
|
+
loadSettings();
|
|
1479
|
+
registerStatusBar();
|
|
1480
|
+
["nodes:add", "nodes:remove", "nodes:change", "flows:change", "workspace:change"].forEach(function (evt) {
|
|
1481
|
+
RED.events.on(evt, onFlowsChanged);
|
|
1482
|
+
});
|
|
1483
|
+
RED.events.on("deploy", function () {
|
|
1484
|
+
if (overlay && !overlay.attr("hidden")) renderAll();
|
|
1485
|
+
});
|
|
1486
|
+
window.__fcdbSqlEditor = { open: open, openNode: openNode, close: close, refresh: resync };
|
|
1487
|
+
}
|
|
1488
|
+
|
|
1489
|
+
init();
|
|
1490
|
+
})();
|