@leonardovida-md/drizzle-neo-duckdb 1.1.2 → 1.1.4
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/duckdb-introspect.mjs +611 -2
- package/dist/helpers.mjs +10 -0
- package/dist/index.mjs +614 -2
- package/dist/sql/query-rewriters.d.ts +13 -0
- package/package.json +5 -2
- package/src/columns.ts +6 -1
- package/src/session.ts +22 -3
- package/src/sql/query-rewriters.ts +948 -0
|
@@ -183,6 +183,604 @@ function adaptArrayOperators(query) {
|
|
|
183
183
|
}
|
|
184
184
|
return rewritten;
|
|
185
185
|
}
|
|
186
|
+
function extractQuotedIdentifier(original, start) {
|
|
187
|
+
if (original[start] !== '"') {
|
|
188
|
+
return null;
|
|
189
|
+
}
|
|
190
|
+
let pos = start + 1;
|
|
191
|
+
while (pos < original.length) {
|
|
192
|
+
if (original[pos] === '"') {
|
|
193
|
+
if (original[pos + 1] === '"') {
|
|
194
|
+
pos += 2;
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
break;
|
|
198
|
+
}
|
|
199
|
+
pos++;
|
|
200
|
+
}
|
|
201
|
+
if (pos >= original.length) {
|
|
202
|
+
return null;
|
|
203
|
+
}
|
|
204
|
+
return {
|
|
205
|
+
name: original.slice(start + 1, pos),
|
|
206
|
+
end: pos + 1
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
function findMainFromClause(scrubbed) {
|
|
210
|
+
const lowerScrubbed = scrubbed.toLowerCase();
|
|
211
|
+
let searchStart = 0;
|
|
212
|
+
const withMatch = /\bwith\s+/i.exec(lowerScrubbed);
|
|
213
|
+
if (withMatch) {
|
|
214
|
+
let depth = 0;
|
|
215
|
+
let pos = withMatch.index + withMatch[0].length;
|
|
216
|
+
while (pos < scrubbed.length) {
|
|
217
|
+
const char = scrubbed[pos];
|
|
218
|
+
if (char === "(") {
|
|
219
|
+
depth++;
|
|
220
|
+
} else if (char === ")") {
|
|
221
|
+
depth--;
|
|
222
|
+
} else if (depth === 0) {
|
|
223
|
+
const remaining = lowerScrubbed.slice(pos);
|
|
224
|
+
if (/^\s*select\s+/i.test(remaining)) {
|
|
225
|
+
searchStart = pos;
|
|
226
|
+
break;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
pos++;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
const fromPattern = /\bfrom\s+/gi;
|
|
233
|
+
fromPattern.lastIndex = searchStart;
|
|
234
|
+
const fromMatch = fromPattern.exec(lowerScrubbed);
|
|
235
|
+
return fromMatch ? fromMatch.index + fromMatch[0].length : -1;
|
|
236
|
+
}
|
|
237
|
+
function parseTableSources(original, scrubbed) {
|
|
238
|
+
const sources = [];
|
|
239
|
+
const lowerScrubbed = scrubbed.toLowerCase();
|
|
240
|
+
const fromPos = findMainFromClause(scrubbed);
|
|
241
|
+
if (fromPos < 0) {
|
|
242
|
+
return sources;
|
|
243
|
+
}
|
|
244
|
+
const fromTable = parseTableRef(original, scrubbed, fromPos);
|
|
245
|
+
if (fromTable) {
|
|
246
|
+
sources.push(fromTable);
|
|
247
|
+
}
|
|
248
|
+
const joinPattern = /\b(left\s+|right\s+|inner\s+|full\s+|cross\s+)?join\s+/gi;
|
|
249
|
+
joinPattern.lastIndex = fromPos;
|
|
250
|
+
let joinMatch;
|
|
251
|
+
while ((joinMatch = joinPattern.exec(lowerScrubbed)) !== null) {
|
|
252
|
+
const tableStart = joinMatch.index + joinMatch[0].length;
|
|
253
|
+
const joinTable = parseTableRef(original, scrubbed, tableStart);
|
|
254
|
+
if (joinTable) {
|
|
255
|
+
sources.push(joinTable);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
return sources;
|
|
259
|
+
}
|
|
260
|
+
function parseTableRef(original, scrubbed, start) {
|
|
261
|
+
let pos = start;
|
|
262
|
+
while (pos < scrubbed.length && isWhitespace(scrubbed[pos])) {
|
|
263
|
+
pos++;
|
|
264
|
+
}
|
|
265
|
+
if (scrubbed[pos] === "(") {
|
|
266
|
+
const nameStart2 = pos;
|
|
267
|
+
let depth = 1;
|
|
268
|
+
pos++;
|
|
269
|
+
while (pos < scrubbed.length && depth > 0) {
|
|
270
|
+
if (scrubbed[pos] === "(")
|
|
271
|
+
depth++;
|
|
272
|
+
else if (scrubbed[pos] === ")")
|
|
273
|
+
depth--;
|
|
274
|
+
pos++;
|
|
275
|
+
}
|
|
276
|
+
while (pos < scrubbed.length && isWhitespace(scrubbed[pos])) {
|
|
277
|
+
pos++;
|
|
278
|
+
}
|
|
279
|
+
const afterSubquery = scrubbed.slice(pos).toLowerCase();
|
|
280
|
+
if (afterSubquery.startsWith("as ")) {
|
|
281
|
+
pos += 3;
|
|
282
|
+
while (pos < scrubbed.length && isWhitespace(scrubbed[pos])) {
|
|
283
|
+
pos++;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
if (original[pos] === '"') {
|
|
287
|
+
const aliasIdent = extractQuotedIdentifier(original, pos);
|
|
288
|
+
if (aliasIdent) {
|
|
289
|
+
return {
|
|
290
|
+
name: aliasIdent.name,
|
|
291
|
+
alias: aliasIdent.name,
|
|
292
|
+
position: nameStart2
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
return null;
|
|
297
|
+
}
|
|
298
|
+
if (original[pos] !== '"') {
|
|
299
|
+
return null;
|
|
300
|
+
}
|
|
301
|
+
const nameStart = pos;
|
|
302
|
+
const firstIdent = extractQuotedIdentifier(original, pos);
|
|
303
|
+
if (!firstIdent) {
|
|
304
|
+
return null;
|
|
305
|
+
}
|
|
306
|
+
let name = firstIdent.name;
|
|
307
|
+
pos = firstIdent.end;
|
|
308
|
+
let afterName = pos;
|
|
309
|
+
while (afterName < scrubbed.length && isWhitespace(scrubbed[afterName])) {
|
|
310
|
+
afterName++;
|
|
311
|
+
}
|
|
312
|
+
if (scrubbed[afterName] === ".") {
|
|
313
|
+
afterName++;
|
|
314
|
+
while (afterName < scrubbed.length && isWhitespace(scrubbed[afterName])) {
|
|
315
|
+
afterName++;
|
|
316
|
+
}
|
|
317
|
+
if (original[afterName] === '"') {
|
|
318
|
+
const tableIdent = extractQuotedIdentifier(original, afterName);
|
|
319
|
+
if (tableIdent) {
|
|
320
|
+
name = tableIdent.name;
|
|
321
|
+
pos = tableIdent.end;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
let alias;
|
|
326
|
+
let aliasPos = pos;
|
|
327
|
+
while (aliasPos < scrubbed.length && isWhitespace(scrubbed[aliasPos])) {
|
|
328
|
+
aliasPos++;
|
|
329
|
+
}
|
|
330
|
+
const afterTable = scrubbed.slice(aliasPos).toLowerCase();
|
|
331
|
+
if (afterTable.startsWith("as ")) {
|
|
332
|
+
aliasPos += 3;
|
|
333
|
+
while (aliasPos < scrubbed.length && isWhitespace(scrubbed[aliasPos])) {
|
|
334
|
+
aliasPos++;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
const afterAlias = scrubbed.slice(aliasPos).toLowerCase();
|
|
338
|
+
if (original[aliasPos] === '"' && !afterAlias.startsWith("on ") && !afterAlias.startsWith("left ") && !afterAlias.startsWith("right ") && !afterAlias.startsWith("inner ") && !afterAlias.startsWith("full ") && !afterAlias.startsWith("cross ") && !afterAlias.startsWith("join ") && !afterAlias.startsWith("where ") && !afterAlias.startsWith("group ") && !afterAlias.startsWith("order ") && !afterAlias.startsWith("limit ")) {
|
|
339
|
+
const aliasIdent = extractQuotedIdentifier(original, aliasPos);
|
|
340
|
+
if (aliasIdent) {
|
|
341
|
+
alias = aliasIdent.name;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
return {
|
|
345
|
+
name,
|
|
346
|
+
alias,
|
|
347
|
+
position: nameStart
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
function findJoinClauses(original, scrubbed, sources, fromPos) {
|
|
351
|
+
const clauses = [];
|
|
352
|
+
const lowerScrubbed = scrubbed.toLowerCase();
|
|
353
|
+
const joinPattern = /\b(left\s+|right\s+|inner\s+|full\s+|cross\s+)?join\s+"[^"]*"(\s*\.\s*"[^"]*")?(\s+as)?(\s+"[^"]*")?\s+on\s+/gi;
|
|
354
|
+
joinPattern.lastIndex = fromPos;
|
|
355
|
+
let match;
|
|
356
|
+
let sourceIndex = 1;
|
|
357
|
+
while ((match = joinPattern.exec(lowerScrubbed)) !== null) {
|
|
358
|
+
const joinType = (match[1] || "").trim().toLowerCase();
|
|
359
|
+
const joinKeywordEnd = match.index + (match[1] || "").length + "join".length;
|
|
360
|
+
let tableStart = joinKeywordEnd;
|
|
361
|
+
while (tableStart < original.length && isWhitespace(original[tableStart])) {
|
|
362
|
+
tableStart++;
|
|
363
|
+
}
|
|
364
|
+
const tableIdent = extractQuotedIdentifier(original, tableStart);
|
|
365
|
+
if (!tableIdent)
|
|
366
|
+
continue;
|
|
367
|
+
let tableName = tableIdent.name;
|
|
368
|
+
let afterTable = tableIdent.end;
|
|
369
|
+
let checkPos = afterTable;
|
|
370
|
+
while (checkPos < scrubbed.length && isWhitespace(scrubbed[checkPos])) {
|
|
371
|
+
checkPos++;
|
|
372
|
+
}
|
|
373
|
+
if (scrubbed[checkPos] === ".") {
|
|
374
|
+
checkPos++;
|
|
375
|
+
while (checkPos < scrubbed.length && isWhitespace(scrubbed[checkPos])) {
|
|
376
|
+
checkPos++;
|
|
377
|
+
}
|
|
378
|
+
const realTableIdent = extractQuotedIdentifier(original, checkPos);
|
|
379
|
+
if (realTableIdent) {
|
|
380
|
+
tableName = realTableIdent.name;
|
|
381
|
+
afterTable = realTableIdent.end;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
let tableAlias;
|
|
385
|
+
let aliasPos = afterTable;
|
|
386
|
+
while (aliasPos < scrubbed.length && isWhitespace(scrubbed[aliasPos])) {
|
|
387
|
+
aliasPos++;
|
|
388
|
+
}
|
|
389
|
+
const afterTableStr = scrubbed.slice(aliasPos).toLowerCase();
|
|
390
|
+
if (afterTableStr.startsWith("as ")) {
|
|
391
|
+
aliasPos += 3;
|
|
392
|
+
while (aliasPos < scrubbed.length && isWhitespace(scrubbed[aliasPos])) {
|
|
393
|
+
aliasPos++;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
if (original[aliasPos] === '"' && !afterTableStr.startsWith("on ")) {
|
|
397
|
+
const aliasIdent = extractQuotedIdentifier(original, aliasPos);
|
|
398
|
+
if (aliasIdent) {
|
|
399
|
+
tableAlias = aliasIdent.name;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
const onStart = match.index + match[0].length;
|
|
403
|
+
const endPattern = /\b(left\s+join|right\s+join|inner\s+join|full\s+join|cross\s+join|join|where|group\s+by|order\s+by|limit|$)/i;
|
|
404
|
+
const remaining = lowerScrubbed.slice(onStart);
|
|
405
|
+
const endMatch = endPattern.exec(remaining);
|
|
406
|
+
const onEnd = endMatch ? onStart + endMatch.index : scrubbed.length;
|
|
407
|
+
let leftSource = "";
|
|
408
|
+
if (sourceIndex > 0 && sourceIndex <= sources.length) {
|
|
409
|
+
const prev = sources[sourceIndex - 1];
|
|
410
|
+
leftSource = prev?.alias || prev?.name || "";
|
|
411
|
+
}
|
|
412
|
+
const rightSource = tableAlias || tableName;
|
|
413
|
+
clauses.push({
|
|
414
|
+
joinType,
|
|
415
|
+
tableName,
|
|
416
|
+
tableAlias,
|
|
417
|
+
onStart,
|
|
418
|
+
onEnd,
|
|
419
|
+
leftSource,
|
|
420
|
+
rightSource
|
|
421
|
+
});
|
|
422
|
+
sourceIndex++;
|
|
423
|
+
}
|
|
424
|
+
return clauses;
|
|
425
|
+
}
|
|
426
|
+
function findMainSelectClause(scrubbed) {
|
|
427
|
+
const lowerScrubbed = scrubbed.toLowerCase();
|
|
428
|
+
let searchStart = 0;
|
|
429
|
+
const withMatch = /\bwith\s+/i.exec(lowerScrubbed);
|
|
430
|
+
if (withMatch) {
|
|
431
|
+
let depth2 = 0;
|
|
432
|
+
let pos2 = withMatch.index + withMatch[0].length;
|
|
433
|
+
while (pos2 < scrubbed.length) {
|
|
434
|
+
const char = scrubbed[pos2];
|
|
435
|
+
if (char === "(") {
|
|
436
|
+
depth2++;
|
|
437
|
+
} else if (char === ")") {
|
|
438
|
+
depth2--;
|
|
439
|
+
} else if (depth2 === 0) {
|
|
440
|
+
const remaining = lowerScrubbed.slice(pos2);
|
|
441
|
+
if (/^\s*select\s+/i.test(remaining)) {
|
|
442
|
+
searchStart = pos2;
|
|
443
|
+
break;
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
pos2++;
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
const selectPattern = /\bselect\s+/gi;
|
|
450
|
+
selectPattern.lastIndex = searchStart;
|
|
451
|
+
const selectMatch = selectPattern.exec(lowerScrubbed);
|
|
452
|
+
if (!selectMatch)
|
|
453
|
+
return null;
|
|
454
|
+
const selectStart = selectMatch.index + selectMatch[0].length;
|
|
455
|
+
let depth = 0;
|
|
456
|
+
let pos = selectStart;
|
|
457
|
+
while (pos < scrubbed.length) {
|
|
458
|
+
const char = scrubbed[pos];
|
|
459
|
+
if (char === "(") {
|
|
460
|
+
depth++;
|
|
461
|
+
} else if (char === ")") {
|
|
462
|
+
depth--;
|
|
463
|
+
} else if (depth === 0) {
|
|
464
|
+
const remaining = lowerScrubbed.slice(pos);
|
|
465
|
+
if (/^\s*from\s+/i.test(remaining)) {
|
|
466
|
+
return { start: selectStart, end: pos };
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
pos++;
|
|
470
|
+
}
|
|
471
|
+
return null;
|
|
472
|
+
}
|
|
473
|
+
function findWhereClause(scrubbed, fromPos) {
|
|
474
|
+
const lowerScrubbed = scrubbed.toLowerCase();
|
|
475
|
+
let depth = 0;
|
|
476
|
+
let pos = fromPos;
|
|
477
|
+
let whereStart = -1;
|
|
478
|
+
while (pos < scrubbed.length) {
|
|
479
|
+
const char = scrubbed[pos];
|
|
480
|
+
if (char === "(") {
|
|
481
|
+
depth++;
|
|
482
|
+
} else if (char === ")") {
|
|
483
|
+
depth--;
|
|
484
|
+
} else if (depth === 0) {
|
|
485
|
+
const remaining = lowerScrubbed.slice(pos);
|
|
486
|
+
if (whereStart === -1 && /^\s*where\s+/i.test(remaining)) {
|
|
487
|
+
const match = /^\s*where\s+/i.exec(remaining);
|
|
488
|
+
if (match) {
|
|
489
|
+
whereStart = pos + match[0].length;
|
|
490
|
+
}
|
|
491
|
+
} else if (whereStart !== -1 && /^\s*(group\s+by|order\s+by|limit|having|union|intersect|except|$)/i.test(remaining)) {
|
|
492
|
+
return { start: whereStart, end: pos };
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
pos++;
|
|
496
|
+
}
|
|
497
|
+
if (whereStart !== -1) {
|
|
498
|
+
return { start: whereStart, end: scrubbed.length };
|
|
499
|
+
}
|
|
500
|
+
return null;
|
|
501
|
+
}
|
|
502
|
+
function findOrderByClause(scrubbed, fromPos) {
|
|
503
|
+
const lowerScrubbed = scrubbed.toLowerCase();
|
|
504
|
+
let depth = 0;
|
|
505
|
+
let pos = fromPos;
|
|
506
|
+
let orderStart = -1;
|
|
507
|
+
while (pos < scrubbed.length) {
|
|
508
|
+
const char = scrubbed[pos];
|
|
509
|
+
if (char === "(") {
|
|
510
|
+
depth++;
|
|
511
|
+
} else if (char === ")") {
|
|
512
|
+
depth--;
|
|
513
|
+
} else if (depth === 0) {
|
|
514
|
+
const remaining = lowerScrubbed.slice(pos);
|
|
515
|
+
if (orderStart === -1 && /^\s*order\s+by\s+/i.test(remaining)) {
|
|
516
|
+
const match = /^\s*order\s+by\s+/i.exec(remaining);
|
|
517
|
+
if (match) {
|
|
518
|
+
orderStart = pos + match[0].length;
|
|
519
|
+
}
|
|
520
|
+
} else if (orderStart !== -1 && /^\s*(limit|offset|fetch|for\s+update|$)/i.test(remaining)) {
|
|
521
|
+
return { start: orderStart, end: pos };
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
pos++;
|
|
525
|
+
}
|
|
526
|
+
if (orderStart !== -1) {
|
|
527
|
+
return { start: orderStart, end: scrubbed.length };
|
|
528
|
+
}
|
|
529
|
+
return null;
|
|
530
|
+
}
|
|
531
|
+
function qualifyClauseColumnsSelective(original, scrubbed, clauseStart, clauseEnd, defaultSource, ambiguousColumns) {
|
|
532
|
+
const clauseOriginal = original.slice(clauseStart, clauseEnd);
|
|
533
|
+
const clauseScrubbed = scrubbed.slice(clauseStart, clauseEnd);
|
|
534
|
+
let result = clauseOriginal;
|
|
535
|
+
let offset = 0;
|
|
536
|
+
let pos = 0;
|
|
537
|
+
while (pos < clauseScrubbed.length) {
|
|
538
|
+
const quotePos = clauseScrubbed.indexOf('"', pos);
|
|
539
|
+
if (quotePos === -1)
|
|
540
|
+
break;
|
|
541
|
+
if (quotePos > 0 && clauseScrubbed[quotePos - 1] === ".") {
|
|
542
|
+
const ident2 = extractQuotedIdentifier(clauseOriginal, quotePos);
|
|
543
|
+
pos = ident2 ? ident2.end : quotePos + 1;
|
|
544
|
+
continue;
|
|
545
|
+
}
|
|
546
|
+
const ident = extractQuotedIdentifier(clauseOriginal, quotePos);
|
|
547
|
+
if (!ident) {
|
|
548
|
+
pos = quotePos + 1;
|
|
549
|
+
continue;
|
|
550
|
+
}
|
|
551
|
+
if (ident.end < clauseScrubbed.length && clauseScrubbed[ident.end] === ".") {
|
|
552
|
+
pos = ident.end + 1;
|
|
553
|
+
continue;
|
|
554
|
+
}
|
|
555
|
+
if (!ambiguousColumns.has(ident.name)) {
|
|
556
|
+
pos = ident.end;
|
|
557
|
+
continue;
|
|
558
|
+
}
|
|
559
|
+
let afterIdent = ident.end;
|
|
560
|
+
while (afterIdent < clauseScrubbed.length && isWhitespace(clauseScrubbed[afterIdent])) {
|
|
561
|
+
afterIdent++;
|
|
562
|
+
}
|
|
563
|
+
if (clauseScrubbed[afterIdent] === "(") {
|
|
564
|
+
pos = ident.end;
|
|
565
|
+
continue;
|
|
566
|
+
}
|
|
567
|
+
const beforeQuote = clauseScrubbed.slice(0, quotePos).toLowerCase();
|
|
568
|
+
if (/\bas\s*$/i.test(beforeQuote)) {
|
|
569
|
+
pos = ident.end;
|
|
570
|
+
continue;
|
|
571
|
+
}
|
|
572
|
+
const qualified = `"${defaultSource}"."${ident.name}"`;
|
|
573
|
+
const oldLength = ident.end - quotePos;
|
|
574
|
+
result = result.slice(0, quotePos + offset) + qualified + result.slice(quotePos + oldLength + offset);
|
|
575
|
+
offset += qualified.length - oldLength;
|
|
576
|
+
pos = ident.end;
|
|
577
|
+
}
|
|
578
|
+
return { result, offset };
|
|
579
|
+
}
|
|
580
|
+
function qualifyJoinColumns(query) {
|
|
581
|
+
const lowerQuery = query.toLowerCase();
|
|
582
|
+
if (!lowerQuery.includes("join")) {
|
|
583
|
+
return query;
|
|
584
|
+
}
|
|
585
|
+
const scrubbed = scrubForRewrite(query);
|
|
586
|
+
const fromPos = findMainFromClause(scrubbed);
|
|
587
|
+
if (fromPos < 0) {
|
|
588
|
+
return query;
|
|
589
|
+
}
|
|
590
|
+
const sources = parseTableSources(query, scrubbed);
|
|
591
|
+
if (sources.length < 2) {
|
|
592
|
+
return query;
|
|
593
|
+
}
|
|
594
|
+
const joinClauses = findJoinClauses(query, scrubbed, sources, fromPos);
|
|
595
|
+
if (joinClauses.length === 0) {
|
|
596
|
+
return query;
|
|
597
|
+
}
|
|
598
|
+
const firstSource = sources[0];
|
|
599
|
+
const defaultQualifier = firstSource.alias || firstSource.name;
|
|
600
|
+
let result = query;
|
|
601
|
+
let totalOffset = 0;
|
|
602
|
+
for (const join of joinClauses) {
|
|
603
|
+
const scrubbedOnClause = scrubbed.slice(join.onStart, join.onEnd);
|
|
604
|
+
const originalOnClause = query.slice(join.onStart, join.onEnd);
|
|
605
|
+
let clauseResult = originalOnClause;
|
|
606
|
+
let clauseOffset = 0;
|
|
607
|
+
let eqPos = -1;
|
|
608
|
+
while ((eqPos = scrubbedOnClause.indexOf("=", eqPos + 1)) !== -1) {
|
|
609
|
+
let lhsEnd = eqPos - 1;
|
|
610
|
+
while (lhsEnd >= 0 && isWhitespace(scrubbedOnClause[lhsEnd])) {
|
|
611
|
+
lhsEnd--;
|
|
612
|
+
}
|
|
613
|
+
if (scrubbedOnClause[lhsEnd] !== '"')
|
|
614
|
+
continue;
|
|
615
|
+
let lhsStartPos = lhsEnd - 1;
|
|
616
|
+
while (lhsStartPos >= 0) {
|
|
617
|
+
if (scrubbedOnClause[lhsStartPos] === '"') {
|
|
618
|
+
if (lhsStartPos > 0 && scrubbedOnClause[lhsStartPos - 1] === '"') {
|
|
619
|
+
lhsStartPos -= 2;
|
|
620
|
+
continue;
|
|
621
|
+
}
|
|
622
|
+
break;
|
|
623
|
+
}
|
|
624
|
+
lhsStartPos--;
|
|
625
|
+
}
|
|
626
|
+
if (lhsStartPos < 0)
|
|
627
|
+
continue;
|
|
628
|
+
const lhsIsQualified = lhsStartPos > 0 && scrubbedOnClause[lhsStartPos - 1] === ".";
|
|
629
|
+
let rhsStartPos = eqPos + 1;
|
|
630
|
+
while (rhsStartPos < scrubbedOnClause.length && isWhitespace(scrubbedOnClause[rhsStartPos])) {
|
|
631
|
+
rhsStartPos++;
|
|
632
|
+
}
|
|
633
|
+
const rhsChar = originalOnClause[rhsStartPos];
|
|
634
|
+
const rhsIsParam = rhsChar === "$";
|
|
635
|
+
const rhsIsStringLiteral = rhsChar === "'";
|
|
636
|
+
const rhsIsColumn = rhsChar === '"';
|
|
637
|
+
if (!rhsIsParam && !rhsIsStringLiteral && !rhsIsColumn)
|
|
638
|
+
continue;
|
|
639
|
+
const rhsIsQualified = !rhsIsColumn || rhsStartPos > 0 && scrubbedOnClause[rhsStartPos - 1] === ".";
|
|
640
|
+
if (lhsIsQualified || rhsIsQualified)
|
|
641
|
+
continue;
|
|
642
|
+
const lhsIdent = extractQuotedIdentifier(originalOnClause, lhsStartPos);
|
|
643
|
+
if (!lhsIdent)
|
|
644
|
+
continue;
|
|
645
|
+
let rhsIdent = null;
|
|
646
|
+
let rhsValue = "";
|
|
647
|
+
let rhsEnd = rhsStartPos;
|
|
648
|
+
if (rhsIsParam) {
|
|
649
|
+
let paramEnd = rhsStartPos + 1;
|
|
650
|
+
while (paramEnd < originalOnClause.length && /\d/.test(originalOnClause[paramEnd])) {
|
|
651
|
+
paramEnd++;
|
|
652
|
+
}
|
|
653
|
+
rhsValue = originalOnClause.slice(rhsStartPos, paramEnd);
|
|
654
|
+
rhsEnd = paramEnd;
|
|
655
|
+
} else if (rhsIsStringLiteral) {
|
|
656
|
+
let literalEnd = rhsStartPos + 1;
|
|
657
|
+
while (literalEnd < originalOnClause.length) {
|
|
658
|
+
if (originalOnClause[literalEnd] === "'") {
|
|
659
|
+
if (originalOnClause[literalEnd + 1] === "'") {
|
|
660
|
+
literalEnd += 2;
|
|
661
|
+
continue;
|
|
662
|
+
}
|
|
663
|
+
break;
|
|
664
|
+
}
|
|
665
|
+
literalEnd++;
|
|
666
|
+
}
|
|
667
|
+
rhsValue = originalOnClause.slice(rhsStartPos, literalEnd + 1);
|
|
668
|
+
rhsEnd = literalEnd + 1;
|
|
669
|
+
} else if (rhsIsColumn) {
|
|
670
|
+
rhsIdent = extractQuotedIdentifier(originalOnClause, rhsStartPos);
|
|
671
|
+
if (rhsIdent) {
|
|
672
|
+
if (rhsIdent.end < scrubbedOnClause.length && scrubbedOnClause[rhsIdent.end] === ".") {
|
|
673
|
+
continue;
|
|
674
|
+
}
|
|
675
|
+
rhsValue = `"${rhsIdent.name}"`;
|
|
676
|
+
rhsEnd = rhsIdent.end;
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
if (!rhsValue)
|
|
680
|
+
continue;
|
|
681
|
+
if (!rhsIsColumn || !rhsIdent || lhsIdent.name !== rhsIdent.name) {
|
|
682
|
+
continue;
|
|
683
|
+
}
|
|
684
|
+
const lhsOriginal = `"${lhsIdent.name}"`;
|
|
685
|
+
let newLhs = lhsOriginal;
|
|
686
|
+
let newRhs = rhsValue;
|
|
687
|
+
if (!lhsIsQualified && join.leftSource) {
|
|
688
|
+
newLhs = `"${join.leftSource}"."${lhsIdent.name}"`;
|
|
689
|
+
}
|
|
690
|
+
if (!rhsIsQualified && rhsIsColumn && rhsIdent && join.rightSource) {
|
|
691
|
+
newRhs = `"${join.rightSource}"."${rhsIdent.name}"`;
|
|
692
|
+
}
|
|
693
|
+
if (newLhs !== lhsOriginal || newRhs !== rhsValue) {
|
|
694
|
+
const opStart = lhsIdent.end;
|
|
695
|
+
let opEnd = opStart;
|
|
696
|
+
while (opEnd < rhsEnd && (isWhitespace(originalOnClause[opEnd]) || originalOnClause[opEnd] === "=")) {
|
|
697
|
+
opEnd++;
|
|
698
|
+
}
|
|
699
|
+
const operator = originalOnClause.slice(opStart, opEnd);
|
|
700
|
+
const newExpr = `${newLhs}${operator}${newRhs}`;
|
|
701
|
+
const oldExprLength = rhsEnd - lhsStartPos;
|
|
702
|
+
clauseResult = clauseResult.slice(0, lhsStartPos + clauseOffset) + newExpr + clauseResult.slice(lhsStartPos + oldExprLength + clauseOffset);
|
|
703
|
+
clauseOffset += newExpr.length - oldExprLength;
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
if (clauseResult !== originalOnClause) {
|
|
707
|
+
result = result.slice(0, join.onStart + totalOffset) + clauseResult + result.slice(join.onEnd + totalOffset);
|
|
708
|
+
totalOffset += clauseResult.length - originalOnClause.length;
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
const ambiguousColumns = new Set;
|
|
712
|
+
for (const join of joinClauses) {
|
|
713
|
+
const scrubbedOnClause = scrubbed.slice(join.onStart, join.onEnd);
|
|
714
|
+
const originalOnClause = query.slice(join.onStart, join.onEnd);
|
|
715
|
+
let eqPos = -1;
|
|
716
|
+
while ((eqPos = scrubbedOnClause.indexOf("=", eqPos + 1)) !== -1) {
|
|
717
|
+
let lhsEnd = eqPos - 1;
|
|
718
|
+
while (lhsEnd >= 0 && isWhitespace(scrubbedOnClause[lhsEnd])) {
|
|
719
|
+
lhsEnd--;
|
|
720
|
+
}
|
|
721
|
+
if (scrubbedOnClause[lhsEnd] !== '"')
|
|
722
|
+
continue;
|
|
723
|
+
let lhsStartPos = lhsEnd - 1;
|
|
724
|
+
while (lhsStartPos >= 0) {
|
|
725
|
+
if (scrubbedOnClause[lhsStartPos] === '"') {
|
|
726
|
+
if (lhsStartPos > 0 && scrubbedOnClause[lhsStartPos - 1] === '"') {
|
|
727
|
+
lhsStartPos -= 2;
|
|
728
|
+
continue;
|
|
729
|
+
}
|
|
730
|
+
break;
|
|
731
|
+
}
|
|
732
|
+
lhsStartPos--;
|
|
733
|
+
}
|
|
734
|
+
if (lhsStartPos < 0)
|
|
735
|
+
continue;
|
|
736
|
+
let rhsStartPos = eqPos + 1;
|
|
737
|
+
while (rhsStartPos < scrubbedOnClause.length && isWhitespace(scrubbedOnClause[rhsStartPos])) {
|
|
738
|
+
rhsStartPos++;
|
|
739
|
+
}
|
|
740
|
+
if (originalOnClause[rhsStartPos] !== '"')
|
|
741
|
+
continue;
|
|
742
|
+
const lhsIdent = extractQuotedIdentifier(originalOnClause, lhsStartPos);
|
|
743
|
+
const rhsIdent = extractQuotedIdentifier(originalOnClause, rhsStartPos);
|
|
744
|
+
if (lhsIdent && rhsIdent && lhsIdent.name === rhsIdent.name) {
|
|
745
|
+
ambiguousColumns.add(lhsIdent.name);
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
if (ambiguousColumns.size === 0) {
|
|
750
|
+
return result;
|
|
751
|
+
}
|
|
752
|
+
const updatedScrubbed = scrubForRewrite(result);
|
|
753
|
+
const selectClause = findMainSelectClause(updatedScrubbed);
|
|
754
|
+
if (selectClause) {
|
|
755
|
+
const { result: selectResult, offset: selectOffset } = qualifyClauseColumnsSelective(result, updatedScrubbed, selectClause.start, selectClause.end, defaultQualifier, ambiguousColumns);
|
|
756
|
+
if (selectOffset !== 0) {
|
|
757
|
+
result = result.slice(0, selectClause.start) + selectResult + result.slice(selectClause.end);
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
const scrubbed2 = scrubForRewrite(result);
|
|
761
|
+
const fromPos2 = findMainFromClause(scrubbed2);
|
|
762
|
+
if (fromPos2 >= 0) {
|
|
763
|
+
const whereClause = findWhereClause(scrubbed2, fromPos2);
|
|
764
|
+
if (whereClause) {
|
|
765
|
+
const { result: whereResult, offset: whereOffset } = qualifyClauseColumnsSelective(result, scrubbed2, whereClause.start, whereClause.end, defaultQualifier, ambiguousColumns);
|
|
766
|
+
if (whereOffset !== 0) {
|
|
767
|
+
result = result.slice(0, whereClause.start) + whereResult + result.slice(whereClause.end);
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
const scrubbed3 = scrubForRewrite(result);
|
|
772
|
+
const fromPos3 = findMainFromClause(scrubbed3);
|
|
773
|
+
if (fromPos3 >= 0) {
|
|
774
|
+
const orderByClause = findOrderByClause(scrubbed3, fromPos3);
|
|
775
|
+
if (orderByClause) {
|
|
776
|
+
const { result: orderResult, offset: orderOffset } = qualifyClauseColumnsSelective(result, scrubbed3, orderByClause.start, orderByClause.end, defaultQualifier, ambiguousColumns);
|
|
777
|
+
if (orderOffset !== 0) {
|
|
778
|
+
result = result.slice(0, orderByClause.start) + orderResult + result.slice(orderByClause.end);
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
return result;
|
|
783
|
+
}
|
|
186
784
|
|
|
187
785
|
// src/sql/result-mapper.ts
|
|
188
786
|
import {
|
|
@@ -764,8 +1362,19 @@ function rewriteQuery(mode, query) {
|
|
|
764
1362
|
if (mode === "never") {
|
|
765
1363
|
return { sql: query, rewritten: false };
|
|
766
1364
|
}
|
|
767
|
-
|
|
768
|
-
|
|
1365
|
+
let result = query;
|
|
1366
|
+
let wasRewritten = false;
|
|
1367
|
+
const arrayRewritten = adaptArrayOperators(result);
|
|
1368
|
+
if (arrayRewritten !== result) {
|
|
1369
|
+
result = arrayRewritten;
|
|
1370
|
+
wasRewritten = true;
|
|
1371
|
+
}
|
|
1372
|
+
const joinQualified = qualifyJoinColumns(result);
|
|
1373
|
+
if (joinQualified !== result) {
|
|
1374
|
+
result = joinQualified;
|
|
1375
|
+
wasRewritten = true;
|
|
1376
|
+
}
|
|
1377
|
+
return { sql: result, rewritten: wasRewritten };
|
|
769
1378
|
}
|
|
770
1379
|
|
|
771
1380
|
class DuckDBPreparedQuery extends PgPreparedQuery {
|
package/dist/helpers.mjs
CHANGED
|
@@ -115,6 +115,13 @@ function buildStructLiteral(value, schema) {
|
|
|
115
115
|
});
|
|
116
116
|
return sql`struct_pack(${sql.join(parts, sql.raw(", "))})`;
|
|
117
117
|
}
|
|
118
|
+
function buildMapLiteral(value, valueType) {
|
|
119
|
+
const keys = Object.keys(value);
|
|
120
|
+
const vals = Object.values(value);
|
|
121
|
+
const keyList = buildListLiteral(keys, "TEXT");
|
|
122
|
+
const valList = buildListLiteral(vals, valueType?.endsWith("[]") ? valueType.slice(0, -2) : valueType);
|
|
123
|
+
return sql`map(${keyList}, ${valList})`;
|
|
124
|
+
}
|
|
118
125
|
var duckDbList = (name, elementType) => customType({
|
|
119
126
|
dataType() {
|
|
120
127
|
return `${elementType}[]`;
|
|
@@ -160,6 +167,9 @@ var duckDbMap = (name, valueType) => customType({
|
|
|
160
167
|
return `MAP (STRING, ${valueType})`;
|
|
161
168
|
},
|
|
162
169
|
toDriver(value) {
|
|
170
|
+
if (Object.keys(value).length === 0) {
|
|
171
|
+
return buildMapLiteral(value, valueType);
|
|
172
|
+
}
|
|
163
173
|
return wrapMap(value, valueType);
|
|
164
174
|
},
|
|
165
175
|
fromDriver(value) {
|