3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
12 namespace Civi\Api4\Query
;
15 * Base class for all Sql functions.
17 * @package Civi\Api4\Query
19 abstract class SqlFunction
extends SqlExpression
{
27 * Used for categorizing functions in the UI
31 protected static $category;
33 const CATEGORY_AGGREGATE
= 'aggregate',
34 CATEGORY_COMPARISON
= 'comparison',
35 CATEGORY_DATE
= 'date',
36 CATEGORY_MATH
= 'math',
37 CATEGORY_STRING
= 'string';
40 * Parse the argument string into an array of function arguments
42 protected function initialize() {
43 $arg = trim(substr($this->expr
, strpos($this->expr
, '(') +
1, -1));
44 foreach ($this->getParams() as $idx => $param) {
47 $prefix = $this->captureKeyword([$param['name']], $arg);
49 if (!$prefix && isset($param['api_default'])) {
51 'prefix' => [$param['name']],
52 'expr' => array_map([parent
::class, 'convert'], $param['api_default']['expr']),
57 if (!$prefix && !$param['optional']) {
58 throw new \
API_Exception("Missing {$param['name']} for SQL function " . static::getName());
61 elseif ($param['flag_before']) {
62 $prefix = $this->captureKeyword(array_keys($param['flag_before']), $arg);
65 'prefix' => (array) $prefix,
69 if ($param['max_expr'] && (!$param['name'] ||
$param['name'] === $prefix)) {
70 $exprs = $this->captureExpressions($arg, $param['must_be'], TRUE);
71 if (count($exprs) < $param['min_expr'] ||
count($exprs) > $param['max_expr']) {
72 throw new \
API_Exception('Incorrect number of arguments for SQL function ' . static::getName());
74 $this->args
[$idx]['expr'] = $exprs;
76 $this->args
[$idx]['suffix'] = (array) $this->captureKeyword(array_keys($param['flag_after']), $arg);
82 * Change $dataType according to output of function
84 * @see \Civi\Api4\Utils\FormattingUtil::formatOutputValues
85 * @param string $value
86 * @param string $dataType
89 public function formatOutputValue($value, &$dataType) {
90 if (static::$dataType) {
91 $dataType = static::$dataType;
97 * Render the expression for insertion into the sql query
99 * @param array $fieldList
102 public function render(array $fieldList): string {
104 foreach ($this->args
as $arg) {
105 $rendered = $this->renderArg($arg, $fieldList);
106 if (strlen($rendered)) {
107 $output .= (strlen($output) ?
' ' : '') . $rendered;
110 return $this->getName() . '(' . $output . ')';
115 * @param array $fieldList
118 private function renderArg($arg, $fieldList): string {
119 $rendered = implode(' ', $arg['prefix']);
120 foreach ($arg['expr'] ??
[] as $idx => $expr) {
121 if (strlen($rendered) ||
$idx) {
122 $rendered .= $idx ?
', ' : ' ';
124 $rendered .= $expr->render($fieldList);
126 if ($arg['suffix']) {
127 $rendered .= (strlen($rendered) ?
' ' : '') . implode(' ', $arg['suffix']);
135 public function getAlias(): string {
136 return $this->alias ??
$this->getName() . ':' . implode('_', $this->fields
);
140 * Get the name of this sql function.
143 public static function getName(): string {
144 $className = static::class;
145 return substr($className, strrpos($className, 'SqlFunction') +
11);
149 * Get the param metadata for this sql function.
152 final public static function getParams(): array {
154 foreach (static::params() as $param) {
155 // Merge in defaults to ensure each param has these properties
156 $params[] = $param +
[
163 'must_be' => ['SqlField', 'SqlFunction', 'SqlString', 'SqlNumber', 'SqlNull'],
164 'api_default' => NULL,
170 abstract protected static function params(): array;
173 * Get the arguments passed to this sql function instance.
176 public function getArgs(): array {
183 public static function getCategory(): string {
184 return static::$category;
190 abstract public static function getTitle(): string;
195 abstract public static function getDescription(): string;