Skip to content

Commit 94e19d9

Browse files
committed
fix type errors
1 parent 4f4973d commit 94e19d9

1 file changed

Lines changed: 18 additions & 8 deletions

File tree

packages/lib/src/compiler.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
DeleteCommand,
99
AggregateCommand,
1010
} from './interfaces';
11-
import { From } from 'node-sql-parser';
11+
import { From, Dual } from 'node-sql-parser';
1212
import debug from 'debug';
1313

1414
const log = debug('queryleaf:compiler');
@@ -28,6 +28,13 @@ export class SqlCompilerImpl implements SqlCompiler {
2828
// Store table aliases from FROM clause
2929
private currentTableAliases: Map<string, string> = new Map();
3030

31+
/**
32+
* Type guard to check if an object is a From type and not a Dual type
33+
*/
34+
private isFromType(obj: From | Dual): obj is From {
35+
return (obj as From).table !== undefined;
36+
}
37+
3138
compile(statement: SqlStatement): Command[] {
3239
const ast = statement.ast;
3340

@@ -44,23 +51,26 @@ export class SqlCompilerImpl implements SqlCompiler {
4451
if (ast.type === 'select' && ast.from) {
4552
// Extract aliases from SELECT FROM clause
4653
for (const fromItem of ast.from) {
47-
if (fromItem.as) {
54+
// Type guard to check if this is a From type and not a Dual type
55+
if (this.isFromType(fromItem) && fromItem.as) {
4856
this.currentTableAliases.set(fromItem.as, fromItem.table);
4957
log(`Found table alias in SELECT: ${fromItem.as} -> ${fromItem.table}`);
5058
}
5159
}
5260
} else if (ast.type === 'update' && ast.table) {
5361
// Extract aliases from UPDATE table clause
5462
for (const tableItem of ast.table) {
55-
if (tableItem.as) {
63+
// Type guard to check if this is a From type and not a Dual type
64+
if (this.isFromType(tableItem) && tableItem.as) {
5665
this.currentTableAliases.set(tableItem.as, tableItem.table);
5766
log(`Found table alias in UPDATE: ${tableItem.as} -> ${tableItem.table}`);
5867
}
5968
}
6069
} else if (ast.type === 'delete' && ast.from) {
6170
// Extract aliases from DELETE FROM clause
6271
for (const fromItem of ast.from) {
63-
if (fromItem.as) {
72+
// Type guard to check if this is a From type and not a Dual type
73+
if (this.isFromType(fromItem) && fromItem.as) {
6474
this.currentTableAliases.set(fromItem.as, fromItem.table);
6575
log(`Found table alias in DELETE: ${fromItem.as} -> ${fromItem.table}`);
6676
}
@@ -349,7 +359,7 @@ export class SqlCompilerImpl implements SqlCompiler {
349359
const hasStar =
350360
ast.columns &&
351361
ast.columns.some(
352-
(col) =>
362+
(col: any) =>
353363
col === '*' || (typeof col === 'object' && col.expr && col.expr.type === 'star')
354364
);
355365

@@ -641,7 +651,7 @@ export class SqlCompilerImpl implements SqlCompiler {
641651
const addFieldsStage: Record<string, any> = {};
642652

643653
// Detailed output of each column being processed
644-
ast.columns.forEach((column, idx) => {
654+
ast.columns.forEach((column: any, idx: number) => {
645655
log(`Column ${idx} details:`, JSON.stringify(column, null, 2));
646656
});
647657

@@ -1039,10 +1049,10 @@ export class SqlCompilerImpl implements SqlCompiler {
10391049
/**
10401050
* Extract table name from FROM clause
10411051
*/
1042-
private extractTableName(from: From): string {
1052+
private extractTableName(from: From | Dual): string {
10431053
if (typeof from === 'string') {
10441054
return from;
1045-
} else if (from.table) {
1055+
} else if (this.isFromType(from) && from.table) {
10461056
return from.table;
10471057
}
10481058
throw new Error('Invalid FROM clause');

0 commit comments

Comments
 (0)