Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
975f60e
v1.6.26 ~ ability to add options via array for computed columns
roncodes Nov 20, 2025
6e64481
Fix: Support computed columns in aggregate functions
Nov 20, 2025
91966c4
Merge pull request #173 from fleetbase/fix/computed-column-aggregates
roncodes Nov 20, 2025
cba8186
Fix: Extract computed columns from groupBy aggregates
Nov 20, 2025
6ab6c15
Merge pull request #174 from fleetbase/fix/computed-column-aggregates-v2
roncodes Nov 20, 2025
e7e3582
Add comprehensive SQL function support to ComputedColumnValidator
Nov 20, 2025
7bb3162
Merge pull request #175 from fleetbase/fix/add-missing-sql-functions
roncodes Nov 20, 2025
f3b047b
Allow computed columns to reference other computed columns
Nov 20, 2025
c70c4cc
Fix resolveComputedColumnReferences to handle SQL functions and compu…
Nov 20, 2025
e6aaf9a
Fix order of operations in resolveComputedColumnReferences
Nov 20, 2025
375bc7a
CRITICAL FIX: Expand computed columns BEFORE protecting string literals
Nov 20, 2025
970d4cb
Fix: Protect SQL function calls from being prefixed with table aliases
Nov 20, 2025
0329831
Fix: Resolve multi-level nested relationship paths in computed columns
Nov 20, 2025
98f3123
Add automatic join detection for computed column expressions
Nov 20, 2025
b467ef9
hotfix: fix syntax errors in ReportQueryConverter
roncodes Nov 20, 2025
12f2400
Fix resolveAliasAndColumn to use longest matching relationship path
Nov 20, 2025
b3b7362
Fix join creation timing for computed columns in aggregates
Nov 20, 2025
d35b0b0
Fix ONLY_FULL_GROUP_BY violation with computed columns
Nov 20, 2025
f65c9a8
Remove module-specific references from core reporting framework
Nov 20, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fleetbase/core-api",
"version": "1.6.25",
"version": "1.6.26",
"description": "Core Framework and Resources for Fleetbase API",
"keywords": [
"fleetbase",
Expand Down
132 changes: 111 additions & 21 deletions src/Support/Reporting/ComputedColumnValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,10 @@ class ComputedColumnValidator
* Allowed SQL functions.
*/
protected array $allowedFunctions = [
// Date/Time Functions
'DATEDIFF',
'DATE_ADD',
'DATE_SUB',
'CONCAT',
'COALESCE',
'IFNULL',
'CASE',
'WHEN',
'THEN',
'ELSE',
'END',
'LEAST',
'GREATEST',
'ROUND',
'ABS',
'UPPER',
'LOWER',
'TRIM',
'LENGTH',
'NULLIF',
'NOW',
'CURDATE',
'CURTIME',
Expand All @@ -40,9 +24,103 @@ class ComputedColumnValidator
'MINUTE',
'SECOND',
'DATE_FORMAT',
'LAST_DAY', // Get last day of month
'DAYOFWEEK', // Get day of week (1=Sunday, 7=Saturday)
'DAYOFMONTH', // Get day of month (1-31)
'DAYOFYEAR', // Get day of year (1-366)
'WEEK', // Get week number
'WEEKDAY', // Get weekday index (0=Monday, 6=Sunday)
'QUARTER', // Get quarter (1-4)
'TIMESTAMPDIFF', // Difference between timestamps
'TIMESTAMPADD', // Add interval to timestamp
'FROM_UNIXTIME', // Convert Unix timestamp to datetime
'UNIX_TIMESTAMP', // Convert datetime to Unix timestamp
'STR_TO_DATE', // Parse string to date
'MAKEDATE', // Create date from year and day of year
'ADDDATE', // Add days to date
'SUBDATE', // Subtract days from date

// String Functions
'CONCAT',
'CONCAT_WS', // Concat with separator
'SUBSTRING',
'SUBSTR', // Alias for SUBSTRING
'REPLACE',
'INTERVAL',
'UPPER',
'LOWER',
'TRIM',
'LTRIM', // Left trim
'RTRIM', // Right trim
'LENGTH',
'CHAR_LENGTH', // Character length
'LEFT', // Get leftmost characters
'RIGHT', // Get rightmost characters
'LPAD', // Left pad string
'RPAD', // Right pad string
'REVERSE', // Reverse string
'LOCATE', // Find substring position
'POSITION', // Find substring position
'INSTR', // Find substring position
'STRCMP', // Compare strings

// Numeric Functions
'ROUND',
'ABS',
'CEIL', // Round up
'CEILING', // Round up (alias)
'FLOOR', // Round down
'TRUNCATE', // Truncate to decimal places
'MOD', // Modulo
'POW', // Power
'POWER', // Power (alias)
'SQRT', // Square root
'SIGN', // Sign of number (-1, 0, 1)
'RAND', // Random number
'PI', // Pi constant
'EXP', // Exponential
'LN', // Natural logarithm
'LOG', // Logarithm
'LOG10', // Base-10 logarithm
'LOG2', // Base-2 logarithm
'DEGREES', // Radians to degrees
'RADIANS', // Degrees to radians
'SIN', // Sine
'COS', // Cosine
'TAN', // Tangent
'ASIN', // Arc sine
'ACOS', // Arc cosine
'ATAN', // Arc tangent
'ATAN2', // Arc tangent of two variables

// Conditional/Logic Functions
'CASE',
'WHEN',
'THEN',
'ELSE',
'END',
'IF', // IF(condition, true_value, false_value)
'IFNULL',
'NULLIF',
'COALESCE',

// Comparison Functions
'LEAST',
'GREATEST',

// Aggregate Functions (for reference, though typically used in GROUP BY)
'COUNT',
'SUM',
'AVG',
'MIN',
'MAX',
'GROUP_CONCAT',

// Type Conversion
'CAST',
'CONVERT',

// Other Utility Functions
'INTERVAL', // For date arithmetic
];

/**
Expand Down Expand Up @@ -78,10 +156,11 @@ public function __construct(ReportSchemaRegistry $registry)
*
* @param string $expression the SQL expression
* @param string $tableName the base table name for column validation
* @param array $computedColumns optional array of other computed columns that can be referenced
*
* @return array validation result with 'valid' boolean and optional 'errors' array
*/
public function validate(string $expression, string $tableName): array
public function validate(string $expression, string $tableName, array $computedColumns = []): array
{
$errors = [];

Expand All @@ -104,7 +183,7 @@ public function validate(string $expression, string $tableName): array
}

// Validate column references
$columnCheck = $this->validateColumnReferences($expression, $tableName);
$columnCheck = $this->validateColumnReferences($expression, $tableName, $computedColumns);
if (!$columnCheck['valid']) {
$errors = array_merge($errors, $columnCheck['errors']);
}
Expand Down Expand Up @@ -188,7 +267,7 @@ protected function validateOperators(string $expression): array
/**
* Validate that all column references exist in the schema.
*/
protected function validateColumnReferences(string $expression, string $tableName): array
protected function validateColumnReferences(string $expression, string $tableName, array $computedColumns = []): array
{
$errors = [];

Expand All @@ -201,6 +280,12 @@ protected function validateColumnReferences(string $expression, string $tableNam
];
}

// Build a list of computed column names for quick lookup
$computedColumnNames = [];
foreach ($computedColumns as $col) {
$computedColumnNames[] = $col['name'] ?? '';
}

// Remove string literals (both single and double quoted) to avoid treating them as column references
$cleanExpression = $this->removeStringLiterals($expression);

Expand All @@ -217,6 +302,11 @@ protected function validateColumnReferences(string $expression, string $tableNam
continue;
}

// Check if it's a reference to another computed column
if (in_array($columnRef, $computedColumnNames)) {
continue; // Valid reference to another computed column
}

// Check if it's a valid column reference
if (!$this->isValidColumnReference($columnRef, $table)) {
$errors[] = "Column reference '{$columnRef}' does not exist in table '{$tableName}' or its relationships";
Expand Down
Loading