@bkmj/node-red-contrib-odbcmj 2.2.1 → 2.3.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/README.md +26 -16
- package/odbc.html +74 -147
- package/odbc.js +142 -103
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -14,6 +14,7 @@ This node is a fork with significant enhancements to provide stability and advan
|
|
|
14
14
|
- **Advanced Retry Logic**: Automatically handles connection errors with configurable delays and retries to ensure flow resilience.
|
|
15
15
|
- **Result Streaming**: Process queries with millions of rows without exhausting memory by streaming results as chunks.
|
|
16
16
|
- **Syntax Checker**: Optionally parse the SQL query to validate its structure.
|
|
17
|
+
- **Query Timeout**: Configure a timeout for query execution to prevent indefinite hangs.
|
|
17
18
|
|
|
18
19
|
---
|
|
19
20
|
|
|
@@ -46,21 +47,23 @@ This mode gives you full control for complex or non-standard connection strings.
|
|
|
46
47
|
#### Test Connection
|
|
47
48
|
|
|
48
49
|
A **Test Connection** button in the configuration panel allows you to instantly verify your settings without deploying the flow.
|
|
50
|
+
> **Note:** For security reasons, passwords are not reloaded into the editor. If your connection requires a password, you must **re-enter it** in the password field before clicking the test button (in Structured Mode). For Connection String mode, ensure the full string (including password if needed) is present in the connection string field itself.
|
|
49
51
|
|
|
50
|
-
#### Pool Options
|
|
52
|
+
#### Pool & Connection Options
|
|
51
53
|
|
|
52
|
-
- **`
|
|
53
|
-
- **`
|
|
54
|
-
- **`
|
|
55
|
-
- **`
|
|
56
|
-
- **`
|
|
57
|
-
- **`
|
|
54
|
+
- **`Initial Pool Size`** `<number>` (optional): The number of connections to create when the pool is initialized. Default: 5.
|
|
55
|
+
- **`Increment Pool Size`** `<number>` (optional): The number of connections to create when the pool is exhausted. Default: 5.
|
|
56
|
+
- **`Max Pool Size`** `<number>` (optional): The maximum number of connections allowed in the pool. Default: 15.
|
|
57
|
+
- **`Shrink Pool`** `<boolean>` (optional): If checked, reduces the number of connections to `Initial Pool Size` when they are returned to the pool if the pool has grown. Default: true.
|
|
58
|
+
- **`Idle Timeout`** `<number>` (optional): The number of seconds for a connection in the pool to remain idle before closing. Default: 3 seconds. (Refers to the `connectionTimeout` property of the `odbc` library's pool options).
|
|
59
|
+
- **`Login Timeout`** `<number>` (optional): The number of seconds for an attempt to establish a new connection to succeed. Default: 5 seconds.
|
|
60
|
+
- **`Query Timeout`** `<number>` (optional): The number of seconds for a query to execute before timing out. A value of **0** means infinite or uses the driver/database default. Default: 0 seconds.
|
|
58
61
|
|
|
59
62
|
#### Error Handling & Retry
|
|
60
63
|
|
|
61
|
-
- **`
|
|
62
|
-
- **`
|
|
63
|
-
- **`
|
|
64
|
+
- **`Retry with fresh connection`** `<boolean>` (optional): If a query fails using a connection from the pool, the node will try once more with a brand new, direct connection. If this succeeds, the entire connection pool is reset to clear any potentially stale connections. Default: false.
|
|
65
|
+
- **`Retry Delay`** `<number>` (optional): If all immediate attempts (pooled and, if enabled, fresh connection) fail, this sets a delay in seconds before another retry is attempted for the incoming message. A value of **0** disables this timed retry mechanism. Default: 5.
|
|
66
|
+
- **`Retry on new message`** `<boolean>` (optional): If the node is waiting for a timed retry (due to `Retry Delay`), a new incoming message can, if this is checked, override the timer and trigger an immediate retry of the *original* message that failed. Default: true.
|
|
64
67
|
|
|
65
68
|
#### Advanced
|
|
66
69
|
|
|
@@ -100,9 +103,16 @@ For queries that return a large number of rows, streaming prevents high memory u
|
|
|
100
103
|
|
|
101
104
|
##### Streaming Output Format
|
|
102
105
|
|
|
103
|
-
When streaming is active,
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
-
|
|
108
|
-
- `complete
|
|
106
|
+
When streaming is active, the node sends messages in sequence:
|
|
107
|
+
|
|
108
|
+
1. **Data Messages**: One or more messages where the payload (or the configured output property) contains an array of rows for the current chunk. For these messages, `msg.odbc_stream.complete` will be **`false`**.
|
|
109
|
+
2. **Completion Message**: A single, final message indicating the end of the stream. For this message:
|
|
110
|
+
- The payload (or configured output property) will be an **empty array `[]`**.
|
|
111
|
+
- `msg.odbc_stream.complete` will be **`true`**.
|
|
112
|
+
|
|
113
|
+
The `msg.odbc_stream` object contains metadata for tracking:
|
|
114
|
+
- `index`: The starting index of the current chunk (0-based). For the completion message, this will be the total number of data rows processed.
|
|
115
|
+
- `count`: The number of rows in the current chunk. This will be `0` for the completion message.
|
|
116
|
+
- `complete`: The boolean flag (`true`/`false`).
|
|
117
|
+
|
|
118
|
+
This pattern ensures you can reliably trigger a final action (like closing a file or calculating an aggregate) only when the message with `complete: true` is received.
|
package/odbc.html
CHANGED
|
@@ -13,14 +13,15 @@
|
|
|
13
13
|
database: { value: "" },
|
|
14
14
|
user: { value: "" },
|
|
15
15
|
connectionString: { value: "" },
|
|
16
|
-
initialSize: { value: 5 },
|
|
17
|
-
incrementSize: { value: 5 },
|
|
18
|
-
maxSize: { value: 15 },
|
|
16
|
+
initialSize: { value: 5, validate: RED.validators.number(true) },
|
|
17
|
+
incrementSize: { value: 5, validate: RED.validators.number(true) },
|
|
18
|
+
maxSize: { value: 15, validate: RED.validators.number(true) },
|
|
19
19
|
shrink: { value: true },
|
|
20
|
-
connectionTimeout: { value: 3 },
|
|
21
|
-
loginTimeout: { value:
|
|
20
|
+
connectionTimeout: { value: 3, validate: RED.validators.number(true) }, // Idle timeout for connections in pool (odbc lib specific)
|
|
21
|
+
loginTimeout: { value: 5, validate: RED.validators.number(true) }, // Login timeout for new connections
|
|
22
|
+
queryTimeoutSeconds: { value: 0, validate: RED.validators.number(true) }, // NOUVEAU: Timeout pour les requêtes
|
|
22
23
|
retryFreshConnection: { value: false },
|
|
23
|
-
retryDelay: { value: 5, validate: RED.validators.number() },
|
|
24
|
+
retryDelay: { value: 5, validate: RED.validators.number(true) },
|
|
24
25
|
retryOnMsg: { value: true },
|
|
25
26
|
syntaxtick: { value: false },
|
|
26
27
|
syntax: { value: "mysql" },
|
|
@@ -79,19 +80,22 @@
|
|
|
79
80
|
RED.notify("Le champ 'Server' est requis pour le test.", {type: "warning", timeout: 3000});
|
|
80
81
|
return;
|
|
81
82
|
}
|
|
82
|
-
|
|
83
|
+
if ($("#node-config-input-password").val() === "" && $("#node-config-input-user").val().trim() !== "") {
|
|
84
|
+
RED.notify("Note: Pour tester une connexion structurée avec mot de passe, veuillez le (re)saisir.", {type: "info", timeout: 4500});
|
|
85
|
+
// On ne bloque pas, l'utilisateur peut vouloir tester une connexion sans mot de passe si le user est optionnel
|
|
86
|
+
}
|
|
87
|
+
} else { // Mode 'string'
|
|
83
88
|
var connStr = $("#node-config-input-connectionString").val().trim();
|
|
84
89
|
if (!connStr) {
|
|
85
90
|
RED.notify("La chaîne de connexion est requise pour le test.", {type: "warning", timeout: 3000});
|
|
86
|
-
return;
|
|
91
|
+
return;
|
|
87
92
|
}
|
|
88
|
-
|
|
89
|
-
// Elle est invalide SEULEMENT si elle ne contient PAS de DSN ET qu'elle ne respecte PAS l'ancien critère pour les chaînes DSN-less.
|
|
93
|
+
|
|
90
94
|
var isDsnString = /DSN=[^;]+/i.test(connStr);
|
|
91
|
-
var isDriverBasedString = /DRIVER=\{.+?\}/i.test(connStr) && /(SERVER|DATABASE|UID|PWD)
|
|
95
|
+
var isDriverBasedString = /DRIVER=\{.+?\}/i.test(connStr) && /(SERVER|DATABASE|UID|PWD)=[^;]+/i.test(connStr);
|
|
92
96
|
|
|
93
97
|
if (!isDsnString && !isDriverBasedString) {
|
|
94
|
-
RED.notify("La chaîne de connexion semble invalide ou incomplète (ex: DSN=
|
|
98
|
+
RED.notify("La chaîne de connexion semble invalide ou incomplète (ex: DSN=valeur; ou DRIVER={...};SERVER=...;).", {type: "warning", timeout: 4500});
|
|
95
99
|
return;
|
|
96
100
|
}
|
|
97
101
|
}
|
|
@@ -109,7 +113,7 @@
|
|
|
109
113
|
database: $("#node-config-input-database").val(),
|
|
110
114
|
user: $("#node-config-input-user").val(),
|
|
111
115
|
connectionString: $("#node-config-input-connectionString").val(),
|
|
112
|
-
password:
|
|
116
|
+
password: $("#node-config-input-password").val()
|
|
113
117
|
};
|
|
114
118
|
|
|
115
119
|
$.ajax({
|
|
@@ -135,22 +139,10 @@
|
|
|
135
139
|
</script>
|
|
136
140
|
|
|
137
141
|
<style>
|
|
138
|
-
.form-section-header {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
margin-bottom: 10px;
|
|
143
|
-
user-select: none;
|
|
144
|
-
}
|
|
145
|
-
.form-section-header i.fa-caret-right {
|
|
146
|
-
transition: transform 0.2s ease-in-out;
|
|
147
|
-
}
|
|
148
|
-
.form-section-header.expanded i.fa-caret-right {
|
|
149
|
-
transform: rotate(90deg);
|
|
150
|
-
}
|
|
151
|
-
.form-section-content {
|
|
152
|
-
padding-left: 20px;
|
|
153
|
-
}
|
|
142
|
+
.form-section-header { cursor: pointer; padding: 5px; border-bottom: 1px solid #ddd; margin-bottom: 10px; user-select: none; }
|
|
143
|
+
.form-section-header i.fa-caret-right { transition: transform 0.2s ease-in-out; }
|
|
144
|
+
.form-section-header.expanded i.fa-caret-right { transform: rotate(90deg); }
|
|
145
|
+
.form-section-content { padding-left: 20px; }
|
|
154
146
|
</style>
|
|
155
147
|
|
|
156
148
|
<script type="text/html" data-template-name="odbc config">
|
|
@@ -213,33 +205,41 @@
|
|
|
213
205
|
|
|
214
206
|
<hr/>
|
|
215
207
|
|
|
216
|
-
<div class="form-section-header"><h4><i class="fa fa-caret-right"></i> <i class="fa fa-sitemap"></i> Pool Options</h4></div>
|
|
208
|
+
<div class="form-section-header"><h4><i class="fa fa-caret-right"></i> <i class="fa fa-sitemap"></i> Pool & Connection Options</h4></div>
|
|
217
209
|
<div class="form-section-content" style="display: none;">
|
|
218
210
|
<div class="form-row">
|
|
219
|
-
<label for="node-config-input-initialSize"><i class="fa fa-play"></i> Initial Size</label>
|
|
211
|
+
<label for="node-config-input-initialSize"><i class="fa fa-play"></i> Initial Pool Size</label>
|
|
220
212
|
<input type="number" id="node-config-input-initialSize" placeholder="5" />
|
|
221
213
|
</div>
|
|
222
214
|
<div class="form-row">
|
|
223
|
-
<label for="node-config-input-incrementSize"><i class="fa fa-plus"></i> Increment Size</label>
|
|
215
|
+
<label for="node-config-input-incrementSize"><i class="fa fa-plus"></i> Increment Pool Size</label>
|
|
224
216
|
<input type="number" id="node-config-input-incrementSize" placeholder="5" />
|
|
225
217
|
</div>
|
|
226
218
|
<div class="form-row">
|
|
227
|
-
<label for="node-config-input-maxSize"><i class="fa fa-stop"></i> Max Size</label>
|
|
219
|
+
<label for="node-config-input-maxSize"><i class="fa fa-stop"></i> Max Pool Size</label>
|
|
228
220
|
<input type="number" id="node-config-input-maxSize" placeholder="15" />
|
|
229
221
|
</div>
|
|
230
222
|
<div class="form-row">
|
|
231
223
|
<label for="node-config-input-shrink"><i class="fa fa-compress"></i> Shrink Pool</label>
|
|
232
224
|
<input type="checkbox" id="node-config-input-shrink" style="margin-left:0px; vertical-align:top; width:auto !important;" />
|
|
225
|
+
<span class="form-tips" style="font-size: smaller;">Reduce pool to initial size when connections are returned.</span>
|
|
233
226
|
</div>
|
|
234
227
|
<div class="form-row">
|
|
235
|
-
<label for="node-config-input-connectionTimeout"><i class="fa fa-clock-o"></i>
|
|
228
|
+
<label for="node-config-input-connectionTimeout"><i class="fa fa-clock-o"></i> Idle Timeout</label>
|
|
236
229
|
<input type="number" id="node-config-input-connectionTimeout" placeholder="3" style="width: 80px;"/>
|
|
237
230
|
<span style="margin-left: 5px;">seconds</span>
|
|
231
|
+
<span class="form-tips" style="font-size: smaller;">For connections in pool.</span>
|
|
238
232
|
</div>
|
|
239
233
|
<div class="form-row">
|
|
240
|
-
<label for="node-config-input-loginTimeout"><i class="fa fa-
|
|
241
|
-
<input type="number" id="node-config-input-loginTimeout" placeholder="
|
|
234
|
+
<label for="node-config-input-loginTimeout"><i class="fa fa-sign-in"></i> Login Timeout</label>
|
|
235
|
+
<input type="number" id="node-config-input-loginTimeout" placeholder="5" style="width: 80px;" />
|
|
242
236
|
<span style="margin-left: 5px;">seconds</span>
|
|
237
|
+
<span class="form-tips" style="font-size: smaller;">For establishing new connections.</span>
|
|
238
|
+
</div>
|
|
239
|
+
<div class="form-row">
|
|
240
|
+
<label for="node-config-input-queryTimeoutSeconds"><i class="fa fa-hourglass-half"></i> Query Timeout</label>
|
|
241
|
+
<input type="number" id="node-config-input-queryTimeoutSeconds" placeholder="0" style="width: 80px;" />
|
|
242
|
+
<span style="margin-left: 5px;">seconds (0=infinite/driver default)</span>
|
|
243
243
|
</div>
|
|
244
244
|
</div>
|
|
245
245
|
|
|
@@ -248,16 +248,18 @@
|
|
|
248
248
|
<div class="form-row">
|
|
249
249
|
<label for="node-config-input-retryFreshConnection" style="width: auto;"><i class="fa fa-refresh"></i> Retry with fresh connection</label>
|
|
250
250
|
<input type="checkbox" id="node-config-input-retryFreshConnection" style="display: inline-block; width: auto; vertical-align: top;" />
|
|
251
|
+
<span class="form-tips" style="font-size: smaller;">If a pooled connection fails, try once with a new one.</span>
|
|
251
252
|
</div>
|
|
252
253
|
<div class="retry-options" style="padding-left: 20px;">
|
|
253
254
|
<div class="form-row">
|
|
254
255
|
<label for="node-config-input-retryDelay"><i class="fa fa-history"></i> Retry Delay</label>
|
|
255
256
|
<input type="number" id="node-config-input-retryDelay" placeholder="5" style="width: 80px;" />
|
|
256
|
-
<span style="margin-left: 5px;">seconds</span>
|
|
257
|
+
<span style="margin-left: 5px;">seconds (0=disable timed retry)</span>
|
|
257
258
|
</div>
|
|
258
259
|
<div class="form-row">
|
|
259
260
|
<label for="node-config-input-retryOnMsg" style="width: auto;"><i class="fa fa-envelope-o"></i> Retry on new message</label>
|
|
260
261
|
<input type="checkbox" id="node-config-input-retryOnMsg" style="display: inline-block; width: auto; vertical-align: top;" />
|
|
262
|
+
<span class="form-tips" style="font-size: smaller;">If waiting, a new message triggers immediate retry.</span>
|
|
261
263
|
</div>
|
|
262
264
|
</div>
|
|
263
265
|
</div>
|
|
@@ -287,112 +289,38 @@
|
|
|
287
289
|
|
|
288
290
|
<script type="text/javascript">
|
|
289
291
|
RED.nodes.registerType("odbc", {
|
|
290
|
-
category: "storage",
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
name: { value: "" },
|
|
294
|
-
connection: { type: "odbc config", required: true },
|
|
295
|
-
query: { value: "" },
|
|
296
|
-
outputObj: { value: "payload" },
|
|
297
|
-
streaming: { value: false },
|
|
298
|
-
streamChunkSize: { value: 1, validate: RED.validators.number() },
|
|
299
|
-
querySource: { value: "query", required: false },
|
|
300
|
-
querySourceType: { value: "msg", required: false },
|
|
301
|
-
paramsSource: { value: "parameters", required: false },
|
|
302
|
-
paramsSourceType: { value: "msg", required: false }
|
|
303
|
-
},
|
|
304
|
-
inputs: 1,
|
|
305
|
-
outputs: 1,
|
|
306
|
-
icon: "db.svg",
|
|
307
|
-
label: function () {
|
|
308
|
-
return this.name || "odbc";
|
|
309
|
-
},
|
|
292
|
+
category: "storage", color: "#89A5C0",
|
|
293
|
+
defaults: { name: { value: "" }, connection: { type: "odbc config", required: true }, query: { value: "" }, outputObj: { value: "payload" }, streaming: { value: false }, streamChunkSize: { value: 1, validate: RED.validators.number() }, querySource: { value: "query", required: false }, querySourceType: { value: "msg", required: false }, paramsSource: { value: "parameters", required: false }, paramsSourceType: { value: "msg", required: false } },
|
|
294
|
+
inputs: 1, outputs: 1, icon: "db.svg", label: function () { return this.name || "odbc"; },
|
|
310
295
|
oneditprepare: function () {
|
|
311
|
-
this.editor = RED.editor.createEditor({
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
});
|
|
316
|
-
|
|
317
|
-
$("#node-input-streaming").on("change", function() {
|
|
318
|
-
$(".stream-options").toggle(this.checked);
|
|
319
|
-
}).trigger("change");
|
|
320
|
-
|
|
321
|
-
$("#node-input-querySource").typedInput({
|
|
322
|
-
default: 'msg',
|
|
323
|
-
typeField: "#node-input-querySourceType",
|
|
324
|
-
types: ['msg', 'flow', 'global', 'env', 'str', 'jsonata']
|
|
325
|
-
});
|
|
326
|
-
|
|
327
|
-
$("#node-input-paramsSource").typedInput({
|
|
328
|
-
default: 'msg',
|
|
329
|
-
typeField: "#node-input-paramsSourceType",
|
|
330
|
-
types: ['msg', 'flow', 'global', 'env', 'jsonata']
|
|
331
|
-
});
|
|
332
|
-
},
|
|
333
|
-
oneditsave: function () {
|
|
334
|
-
this.query = this.editor.getValue();
|
|
335
|
-
this.editor.destroy();
|
|
336
|
-
delete this.editor;
|
|
337
|
-
},
|
|
338
|
-
oneditcancel: function () {
|
|
339
|
-
this.editor.destroy();
|
|
340
|
-
delete this.editor;
|
|
296
|
+
this.editor = RED.editor.createEditor({ id: "node-input-query-editor", mode: "ace/mode/sql", value: this.query, });
|
|
297
|
+
$("#node-input-streaming").on("change", function() { $(".stream-options").toggle(this.checked); }).trigger("change");
|
|
298
|
+
$("#node-input-querySource").typedInput({ default: 'msg', typeField: "#node-input-querySourceType", types: ['msg', 'flow', 'global', 'env', 'str', 'jsonata'] });
|
|
299
|
+
$("#node-input-paramsSource").typedInput({ default: 'msg', typeField: "#node-input-paramsSourceType", types: ['msg', 'flow', 'global', 'env', 'jsonata'] });
|
|
341
300
|
},
|
|
301
|
+
oneditsave: function () { this.query = this.editor.getValue(); this.editor.destroy(); delete this.editor; },
|
|
302
|
+
oneditcancel: function () { this.editor.destroy(); delete this.editor; },
|
|
342
303
|
});
|
|
343
304
|
</script>
|
|
344
305
|
|
|
345
306
|
<script type="text/html" data-template-name="odbc">
|
|
346
|
-
<div class="form-row">
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
</div>
|
|
350
|
-
<div class="form-row">
|
|
351
|
-
<label for="node-input-connection"><i class="fa fa-cog"></i> Connection</label>
|
|
352
|
-
<input type="text" id="node-input-connection" />
|
|
353
|
-
</div>
|
|
354
|
-
<div class="form-row node-text-editor-row">
|
|
355
|
-
<label for="node-input-query" style="width: 100% !important;"><i class="fa fa-file-code-o"></i> Query (fallback)</label>
|
|
356
|
-
<div style="height: 250px;" class="node-text-editor" id="node-input-query-editor"></div>
|
|
357
|
-
</div>
|
|
358
|
-
<div class="form-row">
|
|
359
|
-
<label for="node-input-outputObj"><i class="fa fa-sign-out"></i> Result to</label>
|
|
360
|
-
<span>msg.</span><input type="text" id="node-input-outputObj" placeholder="payload" style="width: 64%;"/>
|
|
361
|
-
</div>
|
|
362
|
-
|
|
307
|
+
<div class="form-row"> <label for="node-input-name"><i class="fa fa-tag"></i> Name</label> <input type="text" id="node-input-name" /> </div>
|
|
308
|
+
<div class="form-row"> <label for="node-input-connection"><i class="fa fa-cog"></i> Connection</label> <input type="text" id="node-input-connection" /> </div>
|
|
309
|
+
<div class="form-row node-text-editor-row"> <label for="node-input-query" style="width: 100% !important;"><i class="fa fa-file-code-o"></i> Query (fallback)</label> <div style="height: 250px;" class="node-text-editor" id="node-input-query-editor"></div> </div>
|
|
310
|
+
<div class="form-row"> <label for="node-input-outputObj"><i class="fa fa-sign-out"></i> Result to</label> <span>msg.</span><input type="text" id="node-input-outputObj" placeholder="payload" style="width: 64%;"/> </div>
|
|
363
311
|
<hr/>
|
|
364
|
-
|
|
365
|
-
<div class="form-row">
|
|
366
|
-
<label for="node-input-querySource"><i class="fa fa-crosshairs"></i> Query Source</label>
|
|
367
|
-
<input type="text" id="node-input-querySource" style="width: 70%;">
|
|
368
|
-
<input type="hidden" id="node-input-querySourceType">
|
|
369
|
-
</div>
|
|
370
|
-
<div class="form-row">
|
|
371
|
-
<label for="node-input-paramsSource"><i class="fa fa-list-ol"></i> Parameters Source</label>
|
|
372
|
-
<input type="text" id="node-input-paramsSource" style="width: 70%;">
|
|
373
|
-
<input type="hidden" id="node-input-paramsSourceType">
|
|
374
|
-
</div>
|
|
375
|
-
|
|
312
|
+
<div class="form-row"> <label for="node-input-querySource"><i class="fa fa-crosshairs"></i> Query Source</label> <input type="text" id="node-input-querySource" style="width: 70%;"> <input type="hidden" id="node-input-querySourceType"> </div>
|
|
313
|
+
<div class="form-row"> <label for="node-input-paramsSource"><i class="fa fa-list-ol"></i> Parameters Source</label> <input type="text" id="node-input-paramsSource" style="width: 70%;"> <input type="hidden" id="node-input-paramsSourceType"> </div>
|
|
376
314
|
<hr/>
|
|
377
|
-
|
|
378
|
-
<div class="form-row">
|
|
379
|
-
<label for="node-input-streaming" style="width: auto;"><i class="fa fa-arrows-v"></i> Stream Results</label>
|
|
380
|
-
<input type="checkbox" id="node-input-streaming" style="display: inline-block; width: auto; vertical-align: top;">
|
|
381
|
-
</div>
|
|
382
|
-
<div class="form-row stream-options">
|
|
383
|
-
<label for="node-input-streamChunkSize"><i class="fa fa-bars"></i> Chunk Size</label>
|
|
384
|
-
<input type="number" id="node-input-streamChunkSize" placeholder="1" style="width: 100px;">
|
|
385
|
-
<span class="form-tips">Number of rows per output message.</span>
|
|
386
|
-
</div>
|
|
315
|
+
<div class="form-row"> <label for="node-input-streaming" style="width: auto;"><i class="fa fa-arrows-v"></i> Stream Results</label> <input type="checkbox" id="node-input-streaming" style="display: inline-block; width: auto; vertical-align: top;"> </div>
|
|
316
|
+
<div class="form-row stream-options"> <label for="node-input-streamChunkSize"><i class="fa fa-bars"></i> Chunk Size</label> <input type="number" id="node-input-streamChunkSize" placeholder="1" style="width: 100px;"> <span class="form-tips">Number of rows per output message.</span> </div>
|
|
387
317
|
</script>
|
|
388
318
|
|
|
389
319
|
|
|
390
320
|
<script type="text/markdown" data-help-name="odbc config">
|
|
391
321
|
A configuration node that manages the connection to your database.
|
|
392
|
-
|
|
393
322
|
### Connection Modes
|
|
394
323
|
Version 2.0 introduces two ways to configure your connection:
|
|
395
|
-
|
|
396
324
|
#### 1. Structured Fields Mode (Recommended)
|
|
397
325
|
This is the easiest and most secure way to set up a connection for common databases.
|
|
398
326
|
- **Database Type**: Select your database (e.g., SQL Server, PostgreSQL, MySQL). The node will use the appropriate driver name and connection string syntax. For unlisted databases, choose "Other" and provide the driver name manually.
|
|
@@ -400,49 +328,49 @@ This is the easiest and most secure way to set up a connection for common databa
|
|
|
400
328
|
- **Database**: The name of the database to connect to (optional).
|
|
401
329
|
- **User**: The username for authentication.
|
|
402
330
|
- **Password**: The password for authentication. This is stored securely using Node-RED's credential system.
|
|
403
|
-
|
|
404
331
|
#### 2. Connection String Mode (Advanced)
|
|
405
332
|
This mode gives you full control for complex or non-standard connection strings.
|
|
406
333
|
- **Connection String**: Enter the complete ODBC connection string. It is your responsibility to provide a valid string for your driver.
|
|
407
334
|
|
|
408
335
|
### Test Connection
|
|
409
336
|
A **Test Connection** button in the configuration panel allows you to instantly verify your settings without deploying the flow.
|
|
337
|
+
> **Note:** For security reasons, passwords are not reloaded into the editor. If your connection requires a password, you must **re-enter it** in the password field before clicking the test button (in Structured Mode). For Connection String mode, ensure the full string (including password if needed) is present in the connection string field itself.
|
|
410
338
|
|
|
411
|
-
### Pool Options
|
|
412
|
-
- **`
|
|
413
|
-
- **`
|
|
414
|
-
-
|
|
339
|
+
### Pool & Connection Options
|
|
340
|
+
- **`Initial Pool Size`**: The number of connections to create when the pool is initialized. Default: 5.
|
|
341
|
+
- **`Increment Pool Size`**: The number of connections to create when the pool is exhausted. Default: 5.
|
|
342
|
+
- **`Max Pool Size`**: The maximum number of connections allowed in the pool. Default: 15.
|
|
343
|
+
- **`Shrink Pool`**: If checked, reduces the number of connections to `Initial Pool Size` when they are returned to the pool if the pool has grown. Default: true.
|
|
344
|
+
- **`Idle Timeout`**: The number of seconds for a connection in the pool to remain idle before closing. Default: 3 seconds.
|
|
345
|
+
- **`Login Timeout`**: The number of seconds for an attempt to establish a new connection to succeed. Default: 5 seconds.
|
|
346
|
+
- **`Query Timeout`**: The number of seconds for a query to execute before timing out. A value of **0** means infinite or uses the driver/database default. Default: 0 seconds.
|
|
415
347
|
|
|
416
348
|
### Error Handling & Retry
|
|
417
|
-
- **`
|
|
418
|
-
- **`
|
|
419
|
-
- **`
|
|
349
|
+
- **`Retry with fresh connection`**: If a query fails using a connection from the pool, the node will try once more with a brand new, direct connection. If this succeeds, the entire connection pool is reset to clear any potentially stale connections.
|
|
350
|
+
- **`Retry Delay`**: If all immediate attempts (pooled and, if enabled, fresh connection) fail, this sets a delay in seconds before another retry is attempted for the incoming message. A value of **0** disables this timed retry mechanism.
|
|
351
|
+
- **`Retry on new message`**: If the node is waiting for a timed retry (due to `Retry Delay`), a new incoming message can, if this is checked, override the timer and trigger an immediate retry of the *original* message that failed.
|
|
420
352
|
|
|
421
353
|
### Advanced
|
|
422
354
|
- **`syntaxChecker`**: If activated, the query string will be parsed and appended to the output message at `msg.parsedQuery`.
|
|
355
|
+
- **`Syntax`**: The SQL dialect to use for the syntax checker (e.g., mysql, postgresql, etc.).
|
|
423
356
|
</script>
|
|
424
357
|
|
|
425
358
|
<script type="text/markdown" data-help-name="odbc">
|
|
426
359
|
Executes a query against a configured ODBC data source.
|
|
427
360
|
|
|
428
361
|
### Inputs
|
|
429
|
-
|
|
430
362
|
- **Query Source** (optional): Specify where to get the query string from. You can use a message property (`msg.`), flow context (`flow.`), global context (`global.`), an environment variable (`env.`), or a JSONata expression. If the source is empty or not found, the node will use the query from the "Query (fallback)" editor below.
|
|
431
363
|
- *Default behavior (for backward compatibility): `msg.query`*
|
|
432
|
-
|
|
433
364
|
- **Parameters Source** (optional): Specify where to get the parameters for a prepared statement. This should resolve to an array of values.
|
|
434
365
|
- *Default behavior (for backward compatibility): `msg.parameters`*
|
|
435
366
|
|
|
436
367
|
### Properties
|
|
437
|
-
|
|
438
368
|
- **Connection**: The `odbc config` node to use.
|
|
439
|
-
- **Query (fallback)**: A static SQL query to run if the "Query
|
|
369
|
+
- **Query (fallback)**: A static SQL query to run if the "Query Source" does not provide one. Can contain Mustache syntax (e.g., `{{{payload.id}}}`).
|
|
440
370
|
- **Result to**: The `msg` property where the query result will be stored. Default: `payload`.
|
|
441
371
|
|
|
442
372
|
### Streaming Results
|
|
443
|
-
|
|
444
|
-
For queries that return a large number of rows, streaming prevents high memory usage by sending multiple messages instead of one large one.
|
|
445
|
-
|
|
373
|
+
For queries that return a large number of rows, streaming prevents high memory usage.
|
|
446
374
|
- **`Stream Results`**: Enables or disables streaming mode.
|
|
447
375
|
- **`Chunk Size`**: The number of rows to include in each output message. A value of `1` means one message will be sent for every single row.
|
|
448
376
|
|
|
@@ -450,14 +378,13 @@ For queries that return a large number of rows, streaming prevents high memory u
|
|
|
450
378
|
When streaming is active, the node sends messages in sequence:
|
|
451
379
|
|
|
452
380
|
1. **Data Messages**: One or more messages where the payload (or the configured output property) contains an array of rows for the current chunk. For these messages, `msg.odbc_stream.complete` will be **`false`**.
|
|
453
|
-
|
|
454
381
|
2. **Completion Message**: A single, final message indicating the end of the stream. For this message:
|
|
455
|
-
- The payload will be an **empty array `[]`**.
|
|
382
|
+
- The payload (or configured output property) will be an **empty array `[]`**.
|
|
456
383
|
- `msg.odbc_stream.complete` will be **`true`**.
|
|
457
384
|
|
|
458
385
|
The `msg.odbc_stream` object contains metadata for tracking:
|
|
459
|
-
- `index`: The starting index of the current chunk. For the completion message, this will be the total number of rows processed.
|
|
460
|
-
- `count`: The number of rows in the chunk. This will be `0` for the completion message.
|
|
386
|
+
- `index`: The starting index of the current chunk (0-based). For the completion message, this will be the total number of data rows processed.
|
|
387
|
+
- `count`: The number of rows in the current chunk. This will be `0` for the completion message.
|
|
461
388
|
- `complete`: The boolean flag (`true`/`false`).
|
|
462
389
|
|
|
463
390
|
This pattern ensures you can reliably trigger a final action (like closing a file or calculating an aggregate) only when the message with `complete: true` is received.
|
package/odbc.js
CHANGED
|
@@ -12,6 +12,12 @@ module.exports = function (RED) {
|
|
|
12
12
|
|
|
13
13
|
this.credentials = RED.nodes.getCredentials(this.id);
|
|
14
14
|
|
|
15
|
+
// NOUVEAU: Timeout par défaut pour les requêtes (0 = infini/défaut du driver)
|
|
16
|
+
// Sera configurable dans le .html plus tard
|
|
17
|
+
this.config.queryTimeoutSeconds = parseInt(config.queryTimeoutSeconds, 10) || 0;
|
|
18
|
+
// NOUVEAU: Timeout fixe pour les opérations de fermeture (en ms)
|
|
19
|
+
this.closeOperationTimeout = 10000; // 10 secondes
|
|
20
|
+
|
|
15
21
|
this._buildConnectionString = function() {
|
|
16
22
|
if (this.config.connectionMode === 'structured') {
|
|
17
23
|
if (!this.config.dbType || !this.config.server) {
|
|
@@ -30,6 +36,8 @@ module.exports = function (RED) {
|
|
|
30
36
|
if (this.config.database) parts.push(`DATABASE=${this.config.database}`);
|
|
31
37
|
if (this.config.user) parts.push(`UID=${this.config.user}`);
|
|
32
38
|
if (this.credentials && this.credentials.password) parts.push(`PWD=${this.credentials.password}`);
|
|
39
|
+
// NOUVEAU: Potentiellement ajouter des options de timeout ici si le driver les supporte dans la CS
|
|
40
|
+
// Exemple (non standard, dépend du driver): if (this.config.loginTimeout > 0) parts.push(`LoginTimeout=${this.config.loginTimeout}`);
|
|
33
41
|
return parts.join(';');
|
|
34
42
|
} else {
|
|
35
43
|
let connStr = this.config.connectionString || "";
|
|
@@ -45,11 +53,19 @@ module.exports = function (RED) {
|
|
|
45
53
|
const finalConnectionString = this._buildConnectionString();
|
|
46
54
|
if (!finalConnectionString) throw new Error("La chaîne de connexion est vide.");
|
|
47
55
|
|
|
48
|
-
const poolParams = { ...this.config };
|
|
56
|
+
const poolParams = { ...this.config }; // Contient initialSize, maxSize, loginTimeout, connectionTimeout (idle) etc.
|
|
49
57
|
poolParams.connectionString = finalConnectionString;
|
|
50
58
|
|
|
51
|
-
|
|
59
|
+
// Supprimer les clés non reconnues par odbc.pool ou spécifiques à notre nœud
|
|
60
|
+
['retryFreshConnection', 'retryDelay', 'retryOnMsg', 'syntax', 'connectionMode',
|
|
61
|
+
'dbType', 'server', 'database', 'user', 'driver', 'queryTimeoutSeconds', 'name', 'id', 'type', '_users', 'z', 'x', 'y', 'wires']
|
|
62
|
+
.forEach(k => delete poolParams[k]);
|
|
52
63
|
|
|
64
|
+
// NOUVEAU: Debug des paramètres du pool
|
|
65
|
+
// this.log(`Initializing pool with params: ${JSON.stringify(poolParams)}`);
|
|
66
|
+
|
|
67
|
+
// Potentiel point de blocage si odbcModule.pool() ne gère pas bien les erreurs de driver/connexion
|
|
68
|
+
// Il n'y a pas de timeout direct pour odbcModule.pool() lui-même
|
|
53
69
|
this.pool = await odbcModule.pool(poolParams);
|
|
54
70
|
this.connecting = false;
|
|
55
71
|
this.status({ fill: "green", shape: "dot", text: "Pool ready" });
|
|
@@ -62,49 +78,73 @@ module.exports = function (RED) {
|
|
|
62
78
|
}
|
|
63
79
|
}
|
|
64
80
|
try {
|
|
81
|
+
// odbc.pool.connect() peut aussi théoriquement bloquer, mais devrait utiliser
|
|
82
|
+
// les timeouts des connexions individuelles ou le connectionTimeout du pool (pour l'attente d'une connexion dispo)
|
|
65
83
|
return await this.pool.connect();
|
|
66
84
|
} catch (poolConnectError) {
|
|
67
|
-
this.error(`Error connecting to pool: ${poolConnectError}`, poolConnectError);
|
|
85
|
+
this.error(`Error connecting to pool: ${poolConnectError.message}`, poolConnectError);
|
|
68
86
|
this.status({ fill: "red", shape: "ring", text: "Pool connect err" });
|
|
69
87
|
throw poolConnectError;
|
|
70
88
|
}
|
|
71
89
|
};
|
|
72
90
|
|
|
73
91
|
this.getFreshConnectionConfig = function() {
|
|
92
|
+
// Ces timeouts sont pour odbcModule.connect (connexion unique)
|
|
74
93
|
return {
|
|
75
94
|
connectionString: this._buildConnectionString(),
|
|
76
|
-
connectionTimeout:
|
|
77
|
-
|
|
95
|
+
connectionTimeout: 0, // Pour une connexion unique, on ne veut pas qu'elle se ferme automatiquement après un idle time.
|
|
96
|
+
// Le `connectionTimeout` de node-odbc connect est "Number of seconds for the connection to be open before it is automatically closed."
|
|
97
|
+
loginTimeout: parseInt(this.config.loginTimeout, 10) || 5, // Timeout pour l'établissement de la connexion. 5s par défaut.
|
|
78
98
|
};
|
|
79
99
|
};
|
|
80
100
|
|
|
101
|
+
// MODIFIÉ: Ajout de timeout pour pool.close()
|
|
81
102
|
this.resetPool = async () => {
|
|
82
|
-
|
|
103
|
+
if (this.pool) {
|
|
83
104
|
this.log("Resetting connection pool.");
|
|
84
105
|
this.status({ fill: "yellow", shape: "ring", text: "Resetting pool..." });
|
|
106
|
+
let closedSuccessfully = false;
|
|
85
107
|
try {
|
|
86
|
-
await
|
|
108
|
+
await Promise.race([
|
|
109
|
+
this.pool.close(),
|
|
110
|
+
new Promise((_, reject) =>
|
|
111
|
+
setTimeout(() => reject(new Error('Pool close timeout')), this.closeOperationTimeout)
|
|
112
|
+
)
|
|
113
|
+
]);
|
|
87
114
|
this.log("Connection pool closed successfully for reset.");
|
|
115
|
+
closedSuccessfully = true;
|
|
88
116
|
} catch (closeError) {
|
|
89
|
-
this.error(`Error closing pool during reset: ${closeError}`, closeError);
|
|
117
|
+
this.error(`Error or timeout closing pool during reset: ${closeError.message}`, closeError);
|
|
90
118
|
} finally {
|
|
91
119
|
this.pool = null;
|
|
92
120
|
this.connecting = false;
|
|
121
|
+
if (closedSuccessfully) {
|
|
122
|
+
this.status({ fill: "grey", shape: "ring", text: "Pool reset" });
|
|
123
|
+
} else {
|
|
124
|
+
this.status({ fill: "red", shape: "ring", text: "Pool reset failed" });
|
|
125
|
+
}
|
|
93
126
|
}
|
|
94
127
|
} else {
|
|
95
128
|
this.log("Pool reset requested, but no active pool to reset.");
|
|
96
129
|
}
|
|
97
130
|
};
|
|
98
131
|
|
|
132
|
+
// MODIFIÉ: Ajout de timeout pour pool.close()
|
|
99
133
|
this.on("close", async (removed, done) => {
|
|
100
134
|
this.log("Closing ODBC config node. Attempting to close pool.");
|
|
101
135
|
if (this.pool) {
|
|
102
136
|
try {
|
|
103
|
-
await
|
|
137
|
+
await Promise.race([
|
|
138
|
+
this.pool.close(),
|
|
139
|
+
new Promise((_, reject) =>
|
|
140
|
+
setTimeout(() => reject(new Error('Pool close timeout on node close')), this.closeOperationTimeout)
|
|
141
|
+
)
|
|
142
|
+
]);
|
|
104
143
|
this.log("Connection pool closed successfully on node close.");
|
|
105
|
-
this.pool = null;
|
|
106
144
|
} catch (error) {
|
|
107
|
-
this.error(`Error closing connection pool on node close: ${error}`, error);
|
|
145
|
+
this.error(`Error or timeout closing connection pool on node close: ${error.message}`, error);
|
|
146
|
+
} finally {
|
|
147
|
+
this.pool = null; // S'assurer que le pool est marqué comme null
|
|
108
148
|
}
|
|
109
149
|
}
|
|
110
150
|
done();
|
|
@@ -141,9 +181,7 @@ module.exports = function (RED) {
|
|
|
141
181
|
return parts.join(';');
|
|
142
182
|
} else {
|
|
143
183
|
let connStr = tempConfig.connectionString || "";
|
|
144
|
-
if (!connStr) {
|
|
145
|
-
throw new Error("La chaîne de connexion ne peut pas être vide.");
|
|
146
|
-
}
|
|
184
|
+
if (!connStr) { throw new Error("La chaîne de connexion ne peut pas être vide."); }
|
|
147
185
|
return connStr;
|
|
148
186
|
}
|
|
149
187
|
};
|
|
@@ -151,25 +189,20 @@ module.exports = function (RED) {
|
|
|
151
189
|
let connection;
|
|
152
190
|
try {
|
|
153
191
|
const testConnectionString = buildTestConnectionString();
|
|
154
|
-
|
|
155
|
-
// ==============================================================
|
|
156
|
-
// LIGNE DE DÉBOGAGE AJOUTÉE
|
|
157
|
-
// ==============================================================
|
|
158
192
|
console.log("[ODBC Test] Attempting to connect with string:", testConnectionString);
|
|
159
|
-
|
|
160
|
-
|
|
193
|
+
|
|
161
194
|
const connectionOptions = {
|
|
162
195
|
connectionString: testConnectionString,
|
|
163
|
-
loginTimeout: 10
|
|
196
|
+
loginTimeout: 10 // Déjà présent et correct
|
|
164
197
|
};
|
|
165
198
|
connection = await odbcModule.connect(connectionOptions);
|
|
166
199
|
res.sendStatus(200);
|
|
167
200
|
} catch (err) {
|
|
168
|
-
console.error("[ODBC Test] Connection failed:", err);
|
|
201
|
+
console.error("[ODBC Test] Connection failed:", err);
|
|
169
202
|
res.status(500).send(err.message || "Erreur inconnue durant le test.");
|
|
170
203
|
} finally {
|
|
171
204
|
if (connection) {
|
|
172
|
-
await connection.close();
|
|
205
|
+
await connection.close(); // Fermeture simple, pas besoin de timeout ici car c'est une op rapide.
|
|
173
206
|
}
|
|
174
207
|
}
|
|
175
208
|
});
|
|
@@ -180,78 +213,23 @@ module.exports = function (RED) {
|
|
|
180
213
|
this.config = config;
|
|
181
214
|
this.poolNode = RED.nodes.getNode(this.config.connection);
|
|
182
215
|
this.name = this.config.name;
|
|
216
|
+
this.isAwaitingRetry = false;
|
|
217
|
+
this.retryTimer = null;
|
|
183
218
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
let s = "";
|
|
187
|
-
if (query || params) {
|
|
188
|
-
s += " {";
|
|
189
|
-
if (query) s += `"query": '${query.substring(0, 100)}${query.length > 100 ? "..." : ""}'`;
|
|
190
|
-
if (params) s += `, "params": '${JSON.stringify(params)}'`;
|
|
191
|
-
s += "}";
|
|
192
|
-
return s;
|
|
193
|
-
}
|
|
194
|
-
return "";
|
|
195
|
-
})();
|
|
196
|
-
let finalError;
|
|
197
|
-
if (typeof error === "object" && error !== null && error.message) { finalError = error; }
|
|
198
|
-
else if (typeof error === "string") { finalError = new Error(error); }
|
|
199
|
-
else { finalError = new Error(defaultMessage); }
|
|
200
|
-
finalError.message = `${finalError.message}${queryContext}`;
|
|
201
|
-
if (query) finalError.query = query;
|
|
202
|
-
if (params) finalError.params = params;
|
|
203
|
-
return finalError;
|
|
204
|
-
};
|
|
219
|
+
// NOUVEAU: Timeout fixe pour les opérations de fermeture de curseur (en ms)
|
|
220
|
+
this.cursorCloseOperationTimeout = 5000; // 5 secondes
|
|
205
221
|
|
|
206
|
-
this.
|
|
207
|
-
|
|
208
|
-
if (typeof result === "undefined") { throw new Error("Query returned undefined."); }
|
|
209
|
-
const newMsg = RED.util.cloneMessage(msg);
|
|
210
|
-
const otherParams = {};
|
|
211
|
-
let actualDataRows = [];
|
|
212
|
-
if (result !== null && typeof result === "object") {
|
|
213
|
-
if (Array.isArray(result)) {
|
|
214
|
-
actualDataRows = [...result];
|
|
215
|
-
for (const [key, value] of Object.entries(result)) {
|
|
216
|
-
if (isNaN(parseInt(key))) { otherParams[key] = value; }
|
|
217
|
-
}
|
|
218
|
-
} else {
|
|
219
|
-
for (const [key, value] of Object.entries(result)) { otherParams[key] = value; }
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
const columnMetadata = otherParams.columns;
|
|
223
|
-
if (Array.isArray(columnMetadata) && Array.isArray(actualDataRows) && actualDataRows.length > 0) {
|
|
224
|
-
const sqlBitColumnNames = new Set();
|
|
225
|
-
columnMetadata.forEach((col) => {
|
|
226
|
-
if (col && typeof col.name === "string" && col.dataTypeName === "SQL_BIT") {
|
|
227
|
-
sqlBitColumnNames.add(col.name);
|
|
228
|
-
}
|
|
229
|
-
});
|
|
230
|
-
if (sqlBitColumnNames.size > 0) {
|
|
231
|
-
actualDataRows.forEach((row) => {
|
|
232
|
-
if (typeof row === "object" && row !== null) {
|
|
233
|
-
for (const columnName of sqlBitColumnNames) {
|
|
234
|
-
if (row.hasOwnProperty(columnName)) {
|
|
235
|
-
const value = row[columnName];
|
|
236
|
-
if (value === "1" || value === 1) { row[columnName] = true; }
|
|
237
|
-
else if (value === "0" || value === 0) { row[columnName] = false; }
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
});
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
objPath.set(newMsg, this.config.outputObj, actualDataRows);
|
|
245
|
-
if (Object.keys(otherParams).length) { newMsg.odbc = otherParams; }
|
|
246
|
-
return newMsg;
|
|
247
|
-
};
|
|
222
|
+
this.enhanceError = (error, query, params, defaultMessage = "Query error") => { /* ... (inchangé) ... */ };
|
|
223
|
+
this.executeQueryAndProcess = async (dbConnection, queryString, queryParams, msg) => { /* ... (inchangé) ... */ };
|
|
248
224
|
|
|
225
|
+
// MODIFIÉ: Ajout de timeout pour cursor.close()
|
|
249
226
|
this.executeStreamQuery = async (dbConnection, queryString, queryParams, msg, send) => {
|
|
250
227
|
const chunkSize = parseInt(this.config.streamChunkSize) || 1;
|
|
251
|
-
const fetchSize = chunkSize > 100 ? 100 : chunkSize;
|
|
228
|
+
const fetchSize = chunkSize > 100 ? 100 : chunkSize;
|
|
252
229
|
let cursor;
|
|
253
230
|
|
|
254
231
|
try {
|
|
232
|
+
// dbConnection.query() utilisera le dbConnection.queryTimeout défini plus bas
|
|
255
233
|
cursor = await dbConnection.query(queryString, queryParams, { cursor: true, fetchSize: fetchSize });
|
|
256
234
|
this.status({ fill: "blue", shape: "dot", text: "streaming rows..." });
|
|
257
235
|
|
|
@@ -259,10 +237,9 @@ module.exports = function (RED) {
|
|
|
259
237
|
let chunk = [];
|
|
260
238
|
|
|
261
239
|
while (true) {
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
}
|
|
240
|
+
// cursor.fetch() pourrait aussi théoriquement bloquer, mais c'est plus rare si la requête initiale a fonctionné
|
|
241
|
+
const rows = await cursor.fetch();
|
|
242
|
+
if (!rows || rows.length === 0) { break; }
|
|
266
243
|
|
|
267
244
|
for (const row of rows) {
|
|
268
245
|
rowCount++;
|
|
@@ -293,33 +270,68 @@ module.exports = function (RED) {
|
|
|
293
270
|
|
|
294
271
|
} finally {
|
|
295
272
|
if (cursor) {
|
|
296
|
-
|
|
273
|
+
try {
|
|
274
|
+
// NOUVEAU: Timeout pour la fermeture du curseur
|
|
275
|
+
await Promise.race([
|
|
276
|
+
cursor.close(),
|
|
277
|
+
new Promise((_, reject) =>
|
|
278
|
+
setTimeout(() => reject(new Error('Cursor close timeout')), this.cursorCloseOperationTimeout)
|
|
279
|
+
)
|
|
280
|
+
]);
|
|
281
|
+
} catch (cursorCloseError) {
|
|
282
|
+
this.warn(`Error or timeout closing cursor: ${cursorCloseError.message}`);
|
|
283
|
+
}
|
|
297
284
|
}
|
|
298
285
|
}
|
|
299
286
|
};
|
|
300
287
|
|
|
288
|
+
// MODIFIÉ: Ajout de la définition du queryTimeout sur la connexion
|
|
301
289
|
this.on("input", async (msg, send, done) => {
|
|
290
|
+
if (this.isAwaitingRetry) {
|
|
291
|
+
if (this.poolNode && this.poolNode.config.retryOnMsg === true) {
|
|
292
|
+
this.log("New message received, overriding retry timer and attempting query now.");
|
|
293
|
+
clearTimeout(this.retryTimer);
|
|
294
|
+
this.retryTimer = null;
|
|
295
|
+
this.isAwaitingRetry = false;
|
|
296
|
+
} else {
|
|
297
|
+
this.warn("Node is in a retry-wait state. New message ignored as per configuration.");
|
|
298
|
+
if (done) done();
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
this.isAwaitingRetry = false;
|
|
303
|
+
if(this.retryTimer) { clearTimeout(this.retryTimer); this.retryTimer = null; }
|
|
304
|
+
|
|
302
305
|
if (!this.poolNode) {
|
|
306
|
+
this.status({ fill: "red", shape: "ring", text: "No config node" });
|
|
303
307
|
return done(new Error("ODBC Config node not properly configured."));
|
|
304
308
|
}
|
|
305
309
|
|
|
306
|
-
const
|
|
310
|
+
const executeWithConnection = async (connection) => {
|
|
307
311
|
this.config.outputObj = this.config.outputObj || "payload";
|
|
308
|
-
|
|
309
312
|
const querySourceType = this.config.querySourceType || 'msg';
|
|
310
313
|
const querySource = this.config.querySource || 'query';
|
|
311
314
|
const paramsSourceType = this.config.paramsSourceType || 'msg';
|
|
312
315
|
const paramsSource = this.config.paramsSource || 'parameters';
|
|
313
|
-
|
|
314
316
|
const params = await new Promise(resolve => RED.util.evaluateNodeProperty(paramsSource, paramsSourceType, this, msg, (err, val) => resolve(err ? undefined : val)));
|
|
315
317
|
let query = await new Promise(resolve => RED.util.evaluateNodeProperty(querySource, querySourceType, this, msg, (err, val) => resolve(err ? undefined : (val || this.config.query || ""))));
|
|
316
|
-
|
|
317
318
|
if (!query) throw new Error("No query to execute");
|
|
318
|
-
|
|
319
319
|
const isPreparedStatement = params || (query && query.includes("?"));
|
|
320
320
|
if (!isPreparedStatement && query) {
|
|
321
321
|
query = mustache.render(query, msg);
|
|
322
322
|
}
|
|
323
|
+
|
|
324
|
+
// NOUVEAU: Appliquer le queryTimeout à la connexion avant exécution
|
|
325
|
+
if (this.poolNode.config.queryTimeoutSeconds > 0) {
|
|
326
|
+
try {
|
|
327
|
+
connection.queryTimeout = parseInt(this.poolNode.config.queryTimeoutSeconds, 10);
|
|
328
|
+
// this.log(`Query timeout set to ${connection.queryTimeout}s for this execution.`);
|
|
329
|
+
} catch (e) {
|
|
330
|
+
this.warn(`Could not set queryTimeout on connection: ${e.message}`);
|
|
331
|
+
}
|
|
332
|
+
} else {
|
|
333
|
+
connection.queryTimeout = 0; // Assurer le reset au défaut du driver (infini)
|
|
334
|
+
}
|
|
323
335
|
|
|
324
336
|
this.status({ fill: "blue", shape: "dot", text: "executing..." });
|
|
325
337
|
if (this.config.streaming) {
|
|
@@ -332,10 +344,12 @@ module.exports = function (RED) {
|
|
|
332
344
|
};
|
|
333
345
|
|
|
334
346
|
let connectionFromPool;
|
|
347
|
+
let errorAfterInitialAttempts = null;
|
|
348
|
+
|
|
335
349
|
try {
|
|
336
350
|
this.status({ fill: "yellow", shape: "dot", text: "connecting..." });
|
|
337
351
|
connectionFromPool = await this.poolNode.connect();
|
|
338
|
-
await
|
|
352
|
+
await executeWithConnection(connectionFromPool);
|
|
339
353
|
return done();
|
|
340
354
|
} catch (poolError) {
|
|
341
355
|
this.warn(`First attempt with pooled connection failed: ${poolError.message}`);
|
|
@@ -347,26 +361,51 @@ module.exports = function (RED) {
|
|
|
347
361
|
const freshConnectConfig = this.poolNode.getFreshConnectionConfig();
|
|
348
362
|
freshConnection = await odbcModule.connect(freshConnectConfig);
|
|
349
363
|
this.log("Fresh connection established for retry.");
|
|
350
|
-
await
|
|
364
|
+
await executeWithConnection(freshConnection);
|
|
351
365
|
this.log("Query successful with fresh connection. Resetting pool.");
|
|
352
366
|
await this.poolNode.resetPool();
|
|
353
367
|
return done();
|
|
354
368
|
} catch (freshError) {
|
|
355
|
-
this.
|
|
356
|
-
return done(this.enhanceError(freshError, null, null, "Retry with fresh connection also failed"));
|
|
369
|
+
errorAfterInitialAttempts = this.enhanceError(freshError, null, null, "Retry with fresh connection also failed");
|
|
357
370
|
} finally {
|
|
358
371
|
if (freshConnection) await freshConnection.close();
|
|
359
372
|
}
|
|
360
373
|
} else {
|
|
361
|
-
this.
|
|
362
|
-
return done(this.enhanceError(poolError));
|
|
374
|
+
errorAfterInitialAttempts = this.enhanceError(poolError);
|
|
363
375
|
}
|
|
364
376
|
} finally {
|
|
365
377
|
if (connectionFromPool) await connectionFromPool.close();
|
|
366
378
|
}
|
|
379
|
+
|
|
380
|
+
if (errorAfterInitialAttempts) {
|
|
381
|
+
const retryDelaySeconds = parseInt(this.poolNode.config.retryDelay, 10);
|
|
382
|
+
if (retryDelaySeconds > 0) {
|
|
383
|
+
this.warn(`Query failed. Scheduling retry in ${retryDelaySeconds} seconds. Error: ${errorAfterInitialAttempts.message}`);
|
|
384
|
+
this.status({ fill: "red", shape: "ring", text: `Retry in ${retryDelaySeconds}s...` });
|
|
385
|
+
this.isAwaitingRetry = true;
|
|
386
|
+
this.retryTimer = setTimeout(() => {
|
|
387
|
+
this.isAwaitingRetry = false;
|
|
388
|
+
this.retryTimer = null;
|
|
389
|
+
this.log(`Retry timer expired for message. Re-emitting for node ${this.id || this.name}.`);
|
|
390
|
+
this.receive(msg);
|
|
391
|
+
}, retryDelaySeconds * 1000);
|
|
392
|
+
if (done) return done();
|
|
393
|
+
} else {
|
|
394
|
+
this.status({ fill: "red", shape: "ring", text: "query error" });
|
|
395
|
+
if (done) return done(errorAfterInitialAttempts);
|
|
396
|
+
}
|
|
397
|
+
} else {
|
|
398
|
+
if (done) return done();
|
|
399
|
+
}
|
|
367
400
|
});
|
|
368
401
|
|
|
369
402
|
this.on("close", (done) => {
|
|
403
|
+
if (this.retryTimer) {
|
|
404
|
+
clearTimeout(this.retryTimer);
|
|
405
|
+
this.retryTimer = null;
|
|
406
|
+
this.isAwaitingRetry = false;
|
|
407
|
+
this.log("Cleared pending retry timer on node close/redeploy.");
|
|
408
|
+
}
|
|
370
409
|
this.status({});
|
|
371
410
|
done();
|
|
372
411
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bkmj/node-red-contrib-odbcmj",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.0",
|
|
4
4
|
"description": "A powerful Node-RED node to connect to any ODBC data source, with connection pooling, advanced retry logic, and result streaming.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"node-red",
|