orange-orm 4.7.4 → 4.7.5-beta.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/dist/index.mjs CHANGED
@@ -17435,6 +17435,7 @@ function requireWrapQuery$2 () {
17435
17435
  if (hasRequiredWrapQuery$2) return wrapQuery_1$2;
17436
17436
  hasRequiredWrapQuery$2 = 1;
17437
17437
  var log = requireLog();
17438
+ var getSessionSingleton = requireGetSessionSingleton();
17438
17439
 
17439
17440
  function wrapQuery(_context, connection) {
17440
17441
  var runOriginalQuery = connection.query;
@@ -17444,13 +17445,59 @@ function requireWrapQuery$2 () {
17444
17445
  var params = query.parameters;
17445
17446
  var sql = query.sql();
17446
17447
  log.emitQuery({ sql, parameters: params });
17447
- const sap = connection.msnodesqlv8;
17448
- for (let i = 0; i < params.length; i++) {
17449
- const parameter = params[i];
17450
- if (typeof parameter === 'string')
17451
- params[i] = sap.VarChar(parameter);
17448
+
17449
+ // Helper function to check for non-ASCII UTF-8 characters
17450
+ function hasNonAsciiCharacters(str) {
17451
+ // Check if string contains any character with code point > 127 (non-ASCII)
17452
+ return /[\u0080-\uFFFF]/.test(str);
17453
+ }
17454
+
17455
+ function stringToHex(str) {
17456
+ return Buffer.from(str, 'utf8').toString('hex');
17457
+ }
17458
+
17459
+ const replacements = [];
17460
+ const parametersToRemove = [];
17461
+ const engine = getSessionSingleton(_context, 'engine');
17462
+
17463
+ if (engine === 'sap') {
17464
+ //non-ASCII UTF-8 characters workaround
17465
+ for (let i = 0; i < params.length; i++) {
17466
+ const parameter = params[i];
17467
+
17468
+ if (typeof parameter === 'string' && hasNonAsciiCharacters(parameter)) {
17469
+ const hexValue = stringToHex(parameter);
17470
+ const convertClause = `CONVERT(VARCHAR(255), CONVERT(VARBINARY(127), 0x${hexValue}))`;
17471
+
17472
+ replacements.push({
17473
+ index: i,
17474
+ replacement: convertClause
17475
+ });
17476
+
17477
+ parametersToRemove.push(i);
17478
+ }
17479
+ }
17480
+ }
17481
+
17482
+ // Second pass: replace the ? placeholders at specific positions
17483
+ if (replacements.length > 0) {
17484
+ let questionMarkIndex = 0;
17485
+ sql = sql.replace(/\?/g, (match) => {
17486
+ const replacement = replacements.find(r => r.index === questionMarkIndex);
17487
+ questionMarkIndex++;
17488
+
17489
+ if (replacement) {
17490
+ return replacement.replacement;
17491
+ }
17492
+ return match;
17493
+ });
17452
17494
  }
17453
17495
 
17496
+ // Remove parameters in reverse order to maintain correct indices
17497
+ parametersToRemove.reverse().forEach(index => {
17498
+ params.splice(index, 1);
17499
+ });
17500
+
17454
17501
  runOriginalQuery.call(connection, sql, params, onInnerCompleted);
17455
17502
  let result = [];
17456
17503
 
package/docs/changelog.md CHANGED
@@ -1,4 +1,6 @@
1
1
  ## Changelog
2
+ __4.7.5__
3
+ Implemented automatic hex conversion for non-ASCII UTF-8 characters in database parameters to resolve SAP ASE encoding issues.
2
4
  __4.7.4__
3
5
  Bugfix: SAP ASE: Do not throw errors on warnings. See [#129](https://github.com/alfateam/issues/129)
4
6
  __4.7.3__
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "orange-orm",
3
- "version": "4.7.4",
3
+ "version": "4.7.5-beta.0",
4
4
  "main": "./src/index.js",
5
5
  "module": "./dist/index.mjs",
6
6
  "browser": "./dist/index.browser.mjs",
@@ -1,4 +1,5 @@
1
1
  var log = require('../table/log');
2
+ var getSessionSingleton = require('../table/getSessionSingleton');
2
3
 
3
4
  function wrapQuery(_context, connection) {
4
5
  var runOriginalQuery = connection.query;
@@ -8,13 +9,59 @@ function wrapQuery(_context, connection) {
8
9
  var params = query.parameters;
9
10
  var sql = query.sql();
10
11
  log.emitQuery({ sql, parameters: params });
11
- const sap = connection.msnodesqlv8;
12
- for (let i = 0; i < params.length; i++) {
13
- const parameter = params[i];
14
- if (typeof parameter === 'string')
15
- params[i] = sap.VarChar(parameter);
12
+
13
+ // Helper function to check for non-ASCII UTF-8 characters
14
+ function hasNonAsciiCharacters(str) {
15
+ // Check if string contains any character with code point > 127 (non-ASCII)
16
+ return /[\u0080-\uFFFF]/.test(str);
17
+ }
18
+
19
+ function stringToHex(str) {
20
+ return Buffer.from(str, 'utf8').toString('hex');
21
+ }
22
+
23
+ const replacements = [];
24
+ const parametersToRemove = [];
25
+ const engine = getSessionSingleton(_context, 'engine');
26
+
27
+ if (engine === 'sap') {
28
+ //non-ASCII UTF-8 characters workaround
29
+ for (let i = 0; i < params.length; i++) {
30
+ const parameter = params[i];
31
+
32
+ if (typeof parameter === 'string' && hasNonAsciiCharacters(parameter)) {
33
+ const hexValue = stringToHex(parameter);
34
+ const convertClause = `CONVERT(VARCHAR(255), CONVERT(VARBINARY(127), 0x${hexValue}))`;
35
+
36
+ replacements.push({
37
+ index: i,
38
+ replacement: convertClause
39
+ });
40
+
41
+ parametersToRemove.push(i);
42
+ }
43
+ }
16
44
  }
17
45
 
46
+ // Second pass: replace the ? placeholders at specific positions
47
+ if (replacements.length > 0) {
48
+ let questionMarkIndex = 0;
49
+ sql = sql.replace(/\?/g, (match) => {
50
+ const replacement = replacements.find(r => r.index === questionMarkIndex);
51
+ questionMarkIndex++;
52
+
53
+ if (replacement) {
54
+ return replacement.replacement;
55
+ }
56
+ return match;
57
+ });
58
+ }
59
+
60
+ // Remove parameters in reverse order to maintain correct indices
61
+ parametersToRemove.reverse().forEach(index => {
62
+ params.splice(index, 1);
63
+ });
64
+
18
65
  runOriginalQuery.call(connection, sql, params, onInnerCompleted);
19
66
  let result = [];
20
67