@sqb/mssql 6.0.1 → 6.0.3

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.
@@ -17,4 +17,5 @@ export declare class MssqlConnection implements Adapter.Connection {
17
17
  execute(query: QueryRequest): Promise<Adapter.Response>;
18
18
  private _bindParams;
19
19
  private _convertFields;
20
+ _normalizeNamedParams(query: QueryRequest): void;
20
21
  }
@@ -1,3 +1,4 @@
1
+ import { tokenize } from 'fast-tokenizer';
1
2
  import sql, {} from 'mssql';
2
3
  import { MssqlCursor } from './mssql-cursor.js';
3
4
  const typeNameMap = {
@@ -30,6 +31,7 @@ const typeNameMap = {
30
31
  VarBinary: { dataType: 'VARBINARY', jsType: 'Buffer' },
31
32
  Image: { dataType: 'IMAGE', jsType: 'Buffer' },
32
33
  };
34
+ const NAMED_PARAM_PATTERN = /^( *):([a-zA-Z_]\w*)$/;
33
35
  export class MssqlConnection {
34
36
  intlcon;
35
37
  _transaction;
@@ -103,6 +105,8 @@ export class MssqlConnection {
103
105
  if (!query.autoCommit && !this._transaction)
104
106
  await this.startTransaction();
105
107
  const out = {};
108
+ if (query.normalizeNamedParams)
109
+ this._normalizeNamedParams(query);
106
110
  const m = query.sql.match(/\b(insert into|update|delete from)\b ("?\w+"?)/i);
107
111
  if (m) {
108
112
  const stmtType = m[1].toLowerCase();
@@ -189,6 +193,46 @@ export class MssqlConnection {
189
193
  }
190
194
  return result;
191
195
  }
196
+ _normalizeNamedParams(query) {
197
+ const tokenizer = tokenize(query.sql, {
198
+ brackets: false,
199
+ delimiters: undefined,
200
+ quotes: true,
201
+ keepBrackets: true,
202
+ keepQuotes: true,
203
+ keepDelimiters: true,
204
+ emptyTokens: true,
205
+ });
206
+ let token;
207
+ let out = '';
208
+ // quotes:true hands back an entire string/double-quoted-identifier
209
+ // literal as one token, so a ":name" occurring inside one never
210
+ // matches the anchored NAMED_PARAM_PATTERN at all. T-SQL's [bracket]
211
+ // identifier quoting isn't a "quote" character the tokenizer knows
212
+ // about though (brackets:false), so "[col:name]" comes back as three
213
+ // separate tokens ("[col", ":name", "]") - track bracket state
214
+ // explicitly so a ":name" inside one isn't mistaken for a param.
215
+ let inBracket = false;
216
+ while ((token = tokenizer.next())) {
217
+ const trimmed = token.trim();
218
+ if (inBracket) {
219
+ if (trimmed.includes(']'))
220
+ inBracket = false;
221
+ }
222
+ else if (trimmed.startsWith('[')) {
223
+ inBracket = true;
224
+ }
225
+ else {
226
+ const m = NAMED_PARAM_PATTERN.exec(token);
227
+ if (m) {
228
+ const [, leading, name] = m;
229
+ token = leading + '@' + name;
230
+ }
231
+ }
232
+ out += token;
233
+ }
234
+ query.sql = out;
235
+ }
192
236
  }
193
237
  function assertDefined(d) {
194
238
  if (d == null)
package/package.json CHANGED
@@ -1,16 +1,17 @@
1
1
  {
2
2
  "name": "@sqb/mssql",
3
3
  "description": "SQB adapter for the mssql (tedious) driver",
4
- "version": "6.0.1",
4
+ "version": "6.0.3",
5
5
  "author": "Panates",
6
6
  "license": "Apache-2.0",
7
7
  "dependencies": {
8
+ "fast-tokenizer": "^1.9.0",
8
9
  "tslib": "^2.8.1"
9
10
  },
10
11
  "peerDependencies": {
11
- "@sqb/builder": "^6.0.1",
12
- "@sqb/connect": "^6.0.1",
13
- "@sqb/mssql-dialect": "^6.0.1",
12
+ "@sqb/builder": "^6.0.3",
13
+ "@sqb/connect": "^6.0.3",
14
+ "@sqb/mssql-dialect": "^6.0.3",
14
15
  "mssql": "^11.0.0 || ^12.0.0"
15
16
  },
16
17
  "type": "module",