Skip to content

Commit b155102

Browse files
committed
fix: Prevent table prefix from being added to string literals in computed columns
- Extract and protect string literals before resolving column references - Use placeholder system to preserve quoted strings - Restore original string literals after column resolution - Fixes issue where 'High' became 'fliit_orders.High' in CASE statements
1 parent b275e9c commit b155102

1 file changed

Lines changed: 38 additions & 3 deletions

File tree

src/Support/Reporting/ReportQueryConverter.php

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -871,8 +871,31 @@ protected function buildComputedColumns(Builder $query, array &$selects): void
871871
*/
872872
protected function resolveComputedColumnReferences(string $expression, string $rootTable): string
873873
{
874-
// Find all potential column references (words that could be column names)
875-
return preg_replace_callback(
874+
// First, extract and protect string literals
875+
$stringLiterals = [];
876+
$protectedExpression = preg_replace_callback(
877+
"/'([^']*)'/",
878+
function ($matches) use (&$stringLiterals) {
879+
$placeholder = '___STRING_LITERAL_' . count($stringLiterals) . '___';
880+
$stringLiterals[$placeholder] = $matches[0]; // Keep the quotes
881+
return $placeholder;
882+
},
883+
$expression
884+
);
885+
886+
// Also protect double-quoted strings
887+
$protectedExpression = preg_replace_callback(
888+
'/"([^"]*)"/',
889+
function ($matches) use (&$stringLiterals) {
890+
$placeholder = '___STRING_LITERAL_' . count($stringLiterals) . '___';
891+
$stringLiterals[$placeholder] = $matches[0]; // Keep the quotes
892+
return $placeholder;
893+
},
894+
$protectedExpression
895+
);
896+
897+
// Now resolve column references in the protected expression
898+
$resolvedExpression = preg_replace_callback(
876899
'/\b([a-z_][a-z0-9_]*(?:\.[a-z_][a-z0-9_]*)*)\b/i',
877900
function ($matches) use ($rootTable) {
878901
$columnRef = $matches[1];
@@ -889,6 +912,11 @@ function ($matches) use ($rootTable) {
889912
return $columnRef;
890913
}
891914

915+
// Skip string literal placeholders
916+
if (strpos($columnRef, '___STRING_LITERAL_') === 0) {
917+
return $columnRef;
918+
}
919+
892920
// Try to resolve the column reference
893921
try {
894922
[$tblAlias, $col] = $this->resolveAliasAndColumn($rootTable, $columnRef);
@@ -899,8 +927,15 @@ function ($matches) use ($rootTable) {
899927
return $columnRef;
900928
}
901929
},
902-
$expression
930+
$protectedExpression
903931
);
932+
933+
// Restore string literals
934+
foreach ($stringLiterals as $placeholder => $original) {
935+
$resolvedExpression = str_replace($placeholder, $original, $resolvedExpression);
936+
}
937+
938+
return $resolvedExpression;
904939
}
905940

906941
/**

0 commit comments

Comments
 (0)