From: Coleman Watts Date: Thu, 16 Sep 2021 14:42:45 +0000 (-0400) Subject: APIv4 - Reword SqlFunction param name for clarity X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=0dc0821cd018dacd9e0711dee08cee351ddfbdf7;p=civicrm-core.git APIv4 - Reword SqlFunction param name for clarity Yes it's a prefix, but it's really the name of the argument. --- diff --git a/Civi/Api4/Query/SqlFunction.php b/Civi/Api4/Query/SqlFunction.php index 457186c2e8..43adbff9f0 100644 --- a/Civi/Api4/Query/SqlFunction.php +++ b/Civi/Api4/Query/SqlFunction.php @@ -43,19 +43,19 @@ abstract class SqlFunction extends SqlExpression { $arg = trim(substr($this->expr, strpos($this->expr, '(') + 1, -1)); foreach ($this->getParams() as $idx => $param) { $prefix = NULL; - if ($param['prefix']) { - $prefix = $this->captureKeyword([$param['prefix']], $arg); + if ($param['name']) { + $prefix = $this->captureKeyword([$param['name']], $arg); // Supply api_default if (!$prefix && isset($param['api_default'])) { $this->args[$idx] = [ - 'prefix' => $param['api_default']['prefix'] ?? [$param['prefix']], + 'prefix' => [$param['name']], 'expr' => array_map([parent::class, 'convert'], $param['api_default']['expr']), - 'suffix' => $param['api_default']['suffix'] ?? [], + 'suffix' => [], ]; continue; } if (!$prefix && !$param['optional']) { - throw new \API_Exception("Missing {$param['prefix']} for SQL function " . static::getName()); + throw new \API_Exception("Missing {$param['name']} for SQL function " . static::getName()); } } elseif ($param['flag_before']) { @@ -66,7 +66,7 @@ abstract class SqlFunction extends SqlExpression { 'expr' => [], 'suffix' => [], ]; - if ($param['max_expr'] && (!$param['prefix'] || $param['prefix'] === $prefix)) { + if ($param['max_expr'] && (!$param['name'] || $param['name'] === $prefix)) { $exprs = $this->captureExpressions($arg, $param['must_be'], $param['cant_be']); if (count($exprs) < $param['min_expr'] || count($exprs) > $param['max_expr']) { throw new \API_Exception('Incorrect number of arguments for SQL function ' . static::getName()); @@ -195,9 +195,8 @@ abstract class SqlFunction extends SqlExpression { */ public function render(array $fieldList): string { $output = ''; - $params = $this->getParams(); - foreach ($this->args as $index => $arg) { - $rendered = $this->renderArg($arg, $params[$index], $fieldList); + foreach ($this->args as $arg) { + $rendered = $this->renderArg($arg, $fieldList); if (strlen($rendered)) { $output .= (strlen($output) ? ' ' : '') . $rendered; } @@ -207,11 +206,10 @@ abstract class SqlFunction extends SqlExpression { /** * @param array $arg - * @param array $param * @param array $fieldList * @return string */ - private function renderArg($arg, $param, $fieldList): string { + private function renderArg($arg, $fieldList): string { $rendered = implode(' ', $arg['prefix']); foreach ($arg['expr'] ?? [] as $idx => $expr) { if (strlen($rendered) || $idx) { @@ -250,7 +248,7 @@ abstract class SqlFunction extends SqlExpression { foreach (static::params() as $param) { // Merge in defaults to ensure each param has these properties $params[] = $param + [ - 'prefix' => NULL, + 'name' => NULL, 'min_expr' => 1, 'max_expr' => 1, 'flag_before' => [], diff --git a/Civi/Api4/Query/SqlFunctionGROUP_CONCAT.php b/Civi/Api4/Query/SqlFunctionGROUP_CONCAT.php index 939c2c7553..4745cbabea 100644 --- a/Civi/Api4/Query/SqlFunctionGROUP_CONCAT.php +++ b/Civi/Api4/Query/SqlFunctionGROUP_CONCAT.php @@ -29,14 +29,14 @@ class SqlFunctionGROUP_CONCAT extends SqlFunction { 'optional' => FALSE, ], [ - 'prefix' => 'ORDER BY', + 'name' => 'ORDER BY', 'max_expr' => 1, 'flag_after' => ['ASC' => ts('Ascending'), 'DESC' => ts('Descending')], 'must_be' => ['SqlField'], 'optional' => TRUE, ], [ - 'prefix' => 'SEPARATOR', + 'name' => 'SEPARATOR', 'max_expr' => 1, 'must_be' => ['SqlString'], 'optional' => TRUE,