11import {
2+ DatabaseTableName ,
23 IPersistenceLeftJoin ,
34 IPersistentOrderBy ,
45 SimpleWhere ,
@@ -13,7 +14,8 @@ export const sql = String.raw;
1314 * @returns Object containing the WHERE clause and values array
1415 */
1516export const buildWhereClause = < T > (
16- where : WhereCondition < T > | undefined
17+ where : WhereCondition < T > | undefined ,
18+ tableName : DatabaseTableName
1719) : { whereClause : string ; values : any [ ] } => {
1820 // If no where clause is provided, return empty
1921 if ( ! where ) {
@@ -23,10 +25,12 @@ export const buildWhereClause = <T>(
2325 const values : any [ ] = [ ] ;
2426
2527 // Process the where condition
26- const result = processWhereCondition ( where , values ) ;
28+ const whereClause = processWhereCondition ( where , values , tableName ) ;
29+
30+ console . log ( { whereClause, tableName } ) ;
2731
2832 return {
29- whereClause : result ,
33+ whereClause,
3034 values,
3135 } ;
3236} ;
@@ -36,13 +40,14 @@ export const buildWhereClause = <T>(
3640 */
3741const processWhereCondition = < T > (
3842 where : WhereCondition < T > ,
39- values : any [ ]
43+ values : any [ ] ,
44+ tableName : DatabaseTableName
4045) : string => {
4146 // Handle composite conditions (AND/OR)
4247 if ( typeof where === "object" ) {
4348 if ( "AND" in where && Array . isArray ( where . AND ) && where . AND . length > 0 ) {
4449 const conditions = where . AND . map ( ( condition ) =>
45- processWhereCondition ( condition , values )
50+ processWhereCondition ( condition , values , tableName )
4651 ) . filter ( Boolean ) ;
4752
4853 if ( conditions . length === 0 ) return "" ;
@@ -53,7 +58,7 @@ const processWhereCondition = <T>(
5358
5459 if ( "OR" in where && Array . isArray ( where . OR ) && where . OR . length > 0 ) {
5560 const conditions = where . OR . map ( ( condition ) =>
56- processWhereCondition ( condition , values )
61+ processWhereCondition ( condition , values , tableName )
5762 ) . filter ( Boolean ) ;
5863
5964 if ( conditions . length === 0 ) return "" ;
@@ -64,7 +69,7 @@ const processWhereCondition = <T>(
6469
6570 // Handle simple conditions
6671 if ( "key" in where && "operator" in where ) {
67- return processSimpleCondition ( where as SimpleWhere < T > , values ) ;
72+ return processSimpleCondition ( where as SimpleWhere < T > , values , tableName ) ;
6873 }
6974 }
7075
@@ -76,7 +81,8 @@ const processWhereCondition = <T>(
7681 */
7782const processSimpleCondition = < T > (
7883 condition : SimpleWhere < T > ,
79- values : any [ ]
84+ values : any [ ] ,
85+ tableName : DatabaseTableName
8086) : string => {
8187 const { key, operator, value } = condition ;
8288
@@ -96,21 +102,21 @@ const processSimpleCondition = <T>(
96102 } )
97103 . join ( ", " ) ;
98104
99- return `"${ key . toString ( ) } " ${ operator } (${ placeholders } )` ;
105+ return `"${ tableName } "." ${ key . toString ( ) } " ${ operator } (${ placeholders } )` ;
100106 }
101107
102108 // Handle NULL values
103109 if ( value === null ) {
104110 return operator === "="
105- ? `"${ key . toString ( ) } " IS NULL`
111+ ? `"${ tableName } "." ${ key . toString ( ) } " IS NULL`
106112 : operator === "<>"
107- ? `"${ key . toString ( ) } " IS NOT NULL`
108- : `"${ key . toString ( ) } " IS NULL` ;
113+ ? `"${ tableName } "." ${ key . toString ( ) } " IS NOT NULL`
114+ : `"${ tableName } "." ${ key . toString ( ) } " IS NULL` ;
109115 }
110116
111117 // Standard case with non-null value
112118 values . push ( value ) ;
113- return `"${ key . toString ( ) } " ${ operator } $${ values . length } ` ;
119+ return `"${ tableName } "." ${ key . toString ( ) } " ${ operator } $${ values . length } ` ;
114120} ;
115121
116122/**
0 commit comments