88 DeleteCommand
99} from './interfaces' ;
1010import { From } from 'node-sql-parser' ;
11+ import debug from 'debug' ;
12+
13+ const log = debug ( 'queryleaf:compiler' ) ;
1114
1215/**
1316 * SQL to MongoDB compiler implementation
@@ -21,7 +24,7 @@ export class SqlCompilerImpl implements SqlCompiler {
2124 compile ( statement : SqlStatement ) : Command [ ] {
2225 const ast = statement . ast ;
2326
24- console . log ( 'Compiling SQL AST:' , JSON . stringify ( ast , null , 2 ) ) ;
27+ log ( 'Compiling SQL AST:' , JSON . stringify ( ast , null , 2 ) ) ;
2528
2629 // Pre-process the AST to handle nested fields that might be parsed as table references
2730 this . handleNestedFieldReferences ( ast ) ;
@@ -45,7 +48,7 @@ export class SqlCompilerImpl implements SqlCompiler {
4548 throw new Error ( `Unsupported SQL statement type: ${ ast . type } ` ) ;
4649 }
4750
48- console . log ( 'Compiled to MongoDB command:' , JSON . stringify ( result , null , 2 ) ) ;
51+ log ( 'Compiled to MongoDB command:' , JSON . stringify ( result , null , 2 ) ) ;
4952
5053 return result ;
5154 }
@@ -97,7 +100,7 @@ export class SqlCompilerImpl implements SqlCompiler {
97100 }
98101
99102 if ( ast . limit ) {
100- console . log ( 'Limit found in AST:' , JSON . stringify ( ast . limit , null , 2 ) ) ;
103+ log ( 'Limit found in AST:' , JSON . stringify ( ast . limit , null , 2 ) ) ;
101104 if ( typeof ast . limit === 'object' && 'value' in ast . limit && ast . limit . value ) {
102105 // Standard PostgreSQL LIMIT format
103106 command . limit = Number ( ast . limit . value ) ;
@@ -131,8 +134,8 @@ export class SqlCompilerImpl implements SqlCompiler {
131134 throw new Error ( 'VALUES are required for INSERT statements' ) ;
132135 }
133136
134- console . log ( 'INSERT values:' , JSON . stringify ( ast . values , null , 2 ) ) ;
135- console . log ( 'INSERT columns:' , JSON . stringify ( ast . columns , null , 2 ) ) ;
137+ log ( 'INSERT values:' , JSON . stringify ( ast . values , null , 2 ) ) ;
138+ log ( 'INSERT columns:' , JSON . stringify ( ast . columns , null , 2 ) ) ;
136139
137140 const documents = ast . values . map ( ( valueList : any ) => {
138141 const document : Record < string , any > = { } ;
@@ -152,7 +155,7 @@ export class SqlCompilerImpl implements SqlCompiler {
152155 values = [ valueList ] ;
153156 }
154157
155- console . log ( 'Processed values:' , JSON . stringify ( values , null , 2 ) ) ;
158+ log ( 'Processed values:' , JSON . stringify ( values , null , 2 ) ) ;
156159
157160 ast . columns . forEach ( ( column : any , index : number ) => {
158161 let columnName : string ;
@@ -170,7 +173,7 @@ export class SqlCompilerImpl implements SqlCompiler {
170173 }
171174 } ) ;
172175
173- console . log ( 'Constructed document:' , JSON . stringify ( document , null , 2 ) ) ;
176+ log ( 'Constructed document:' , JSON . stringify ( document , null , 2 ) ) ;
174177 return document ;
175178 } ) ;
176179
@@ -359,13 +362,13 @@ export class SqlCompilerImpl implements SqlCompiler {
359362 private convertColumns ( columns : any [ ] ) : Record < string , any > {
360363 const projection : Record < string , any > = { } ;
361364
362- console . log ( 'Converting columns to projection:' , JSON . stringify ( columns , null , 2 ) ) ;
365+ log ( 'Converting columns to projection:' , JSON . stringify ( columns , null , 2 ) ) ;
363366
364367 // If * is used, return empty projection (which means all fields)
365368 if ( columns . some ( col => col === '*' ||
366369 ( typeof col === 'object' && col . expr && col . expr . type === 'star' ) ||
367370 ( typeof col === 'object' && col . expr && col . expr . column === '*' ) ) ) {
368- console . log ( 'Star (*) detected, returning empty projection' ) ;
371+ log ( 'Star (*) detected, returning empty projection' ) ;
369372 return { } ;
370373 }
371374
@@ -449,7 +452,7 @@ export class SqlCompilerImpl implements SqlCompiler {
449452 }
450453 } ) ;
451454
452- console . log ( 'Final projection:' , JSON . stringify ( projection , null , 2 ) ) ;
455+ log ( 'Final projection:' , JSON . stringify ( projection , null , 2 ) ) ;
453456
454457 return projection ;
455458 }
@@ -465,7 +468,7 @@ export class SqlCompilerImpl implements SqlCompiler {
465468 private processFieldName ( fieldName : string ) : string {
466469 if ( ! fieldName ) return fieldName ;
467470
468- console . log ( `Processing field name: "${ fieldName } "` ) ;
471+ log ( `Processing field name: "${ fieldName } "` ) ;
469472
470473 // First convert our placeholder format back to MongoDB dot notation
471474 // This transforms items__ARRAY_0__name => items.0.name
@@ -485,7 +488,7 @@ export class SqlCompilerImpl implements SqlCompiler {
485488 // Handle nested field access directly
486489 // MongoDB already uses dot notation for nested fields, so we can use it as is
487490 if ( processed . includes ( '.' ) ) {
488- console . log ( `Using nested field in MongoDB filter: "${ processed } "` ) ;
491+ log ( `Using nested field in MongoDB filter: "${ processed } "` ) ;
489492 }
490493
491494 return processed ;
@@ -497,7 +500,7 @@ export class SqlCompilerImpl implements SqlCompiler {
497500 * address.zip might be parsed as table "address", column "zip"
498501 */
499502 private handleNestedFieldReferences ( ast : any ) : void {
500- console . log ( 'Handling nested field references in AST' ) ;
503+ log ( 'Handling nested field references in AST' ) ;
501504
502505 // Handle column references in SELECT clause
503506 if ( ast . columns && Array . isArray ( ast . columns ) ) {
@@ -507,7 +510,7 @@ export class SqlCompilerImpl implements SqlCompiler {
507510 // This could be a nested field - convert table.column to a single column path
508511 column . expr . column = `${ column . expr . table } .${ column . expr . column } ` ;
509512 column . expr . table = null ;
510- console . log ( `Converted SELECT column to nested field: ${ column . expr . column } ` ) ;
513+ log ( `Converted SELECT column to nested field: ${ column . expr . column } ` ) ;
511514 }
512515 } ) ;
513516 }
@@ -516,7 +519,7 @@ export class SqlCompilerImpl implements SqlCompiler {
516519 this . processWhereClauseForNestedFields ( ast . where ) ;
517520
518521 // For debugging - show the resulting AST after transformation
519- console . log ( 'AST after nested field handling:' , JSON . stringify ( ast ?. where , null , 2 ) ) ;
522+ log ( 'AST after nested field handling:' , JSON . stringify ( ast ?. where , null , 2 ) ) ;
520523 }
521524
522525 /**
@@ -525,7 +528,7 @@ export class SqlCompilerImpl implements SqlCompiler {
525528 private processWhereClauseForNestedFields ( where : any ) : void {
526529 if ( ! where ) return ;
527530
528- console . log ( 'Processing WHERE clause for nested fields:' , JSON . stringify ( where , null , 2 ) ) ;
531+ log ( 'Processing WHERE clause for nested fields:' , JSON . stringify ( where , null , 2 ) ) ;
529532
530533 if ( where . type === 'binary_expr' ) {
531534 // Process left and right sides recursively
@@ -534,15 +537,15 @@ export class SqlCompilerImpl implements SqlCompiler {
534537
535538 // Handle column references in comparison expressions
536539 if ( where . left && where . left . type === 'column_ref' ) {
537- console . log ( 'Processing column reference:' , JSON . stringify ( where . left , null , 2 ) ) ;
540+ log ( 'Processing column reference:' , JSON . stringify ( where . left , null , 2 ) ) ;
538541
539542 // Handle both direct dot notation in column name and table.column format
540543 if ( where . left . column && where . left . column . includes ( '.' ) ) {
541544 // Already has dot notation, just keep it
542- console . log ( 'Column already has dot notation:' , where . left . column ) ;
545+ log ( 'Column already has dot notation:' , where . left . column ) ;
543546 } else if ( where . left . table && where . left . column ) {
544547 // Convert table.column format to a nested field path
545- console . log ( 'Converting table.column to nested path:' ,
548+ log ( 'Converting table.column to nested path:' ,
546549 `${ where . left . table } .${ where . left . column } ` ) ;
547550 where . left . column = `${ where . left . table } .${ where . left . column } ` ;
548551 where . left . table = null ;
@@ -580,8 +583,8 @@ export class SqlCompilerImpl implements SqlCompiler {
580583 return undefined ;
581584 }
582585
583- console . log ( 'Converting GROUP BY:' , JSON . stringify ( groupby , null , 2 ) ) ;
584- console . log ( 'With columns:' , JSON . stringify ( columns , null , 2 ) ) ;
586+ log ( 'Converting GROUP BY:' , JSON . stringify ( groupby , null , 2 ) ) ;
587+ log ( 'With columns:' , JSON . stringify ( columns , null , 2 ) ) ;
585588
586589 // Create the group stage
587590 let group : { _id : any ; [ key : string ] : any } ;
@@ -715,7 +718,7 @@ export class SqlCompilerImpl implements SqlCompiler {
715718 } ) ;
716719 }
717720
718- console . log ( 'Generated group stage:' , JSON . stringify ( group , null , 2 ) ) ;
721+ log ( 'Generated group stage:' , JSON . stringify ( group , null , 2 ) ) ;
719722 return group ;
720723 }
721724
@@ -780,7 +783,7 @@ export class SqlCompilerImpl implements SqlCompiler {
780783 pipeline . push ( { $project : projectionFormat } ) ;
781784 }
782785
783- console . log ( 'Generated aggregate pipeline:' , JSON . stringify ( pipeline , null , 2 ) ) ;
786+ log ( 'Generated aggregate pipeline:' , JSON . stringify ( pipeline , null , 2 ) ) ;
784787 return pipeline ;
785788 }
786789
@@ -822,8 +825,8 @@ export class SqlCompilerImpl implements SqlCompiler {
822825 return [ ] ;
823826 }
824827
825- console . log ( 'Converting JOINs:' , JSON . stringify ( from , null , 2 ) ) ;
826- console . log ( 'With WHERE:' , JSON . stringify ( where , null , 2 ) ) ;
828+ log ( 'Converting JOINs:' , JSON . stringify ( from , null , 2 ) ) ;
829+ log ( 'With WHERE:' , JSON . stringify ( where , null , 2 ) ) ;
827830
828831 const lookups : { from : string ; localField : string ; foreignField : string ; as : string } [ ] = [ ] ;
829832 const mainTable = this . extractTableName ( from [ 0 ] ) ;
@@ -868,7 +871,7 @@ export class SqlCompilerImpl implements SqlCompiler {
868871 }
869872 }
870873
871- console . log ( 'Generated lookups:' , JSON . stringify ( lookups , null , 2 ) ) ;
874+ log ( 'Generated lookups:' , JSON . stringify ( lookups , null , 2 ) ) ;
872875 return lookups ;
873876 }
874877
0 commit comments