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) {
46 $name = $param['name'] ?
: ($idx +
1);
47 // If this isn't the first param it needs to start with something;
48 // either the name (e.g. "ORDER BY") if it has one, or a comma separating it from the previous param.
49 $start = $param['name'] ?
: ($idx ?
',' : NULL);
51 $prefix = $this->captureKeyword([$start], $arg);
53 if (!$prefix && isset($param['api_default'])) {
56 'expr' => array_map([parent
::class, 'convert'], $param['api_default']['expr']),
61 if (!$prefix && !$param['optional']) {
62 throw new \
API_Exception("Missing param $name for SQL function " . static::getName());
65 elseif ($param['flag_before']) {
66 $prefix = $this->captureKeyword(array_keys($param['flag_before']), $arg);
69 'prefix' => (array) $prefix,
73 if ($param['max_expr'] && (!$param['name'] ||
$param['name'] === $prefix)) {
74 $exprs = $this->captureExpressions($arg, $param['must_be'], $param['max_expr']);
76 count($exprs) < $param['min_expr'] &&
77 !(!$exprs && $param['optional'])
79 throw new \
API_Exception("Too few arguments to param $name for SQL function " . static::getName());
81 $this->args
[$idx]['expr'] = $exprs;
83 $this->args
[$idx]['suffix'] = (array) $this->captureKeyword(array_keys($param['flag_after']), $arg);
87 throw new \
API_Exception("Too many arguments given for SQL function " . static::getName());
92 * Change $dataType according to output of function
94 * @see \Civi\Api4\Utils\FormattingUtil::formatOutputValues
95 * @param string $value
96 * @param string $dataType
99 public function formatOutputValue($value, &$dataType) {
100 if (static::$dataType) {
101 $dataType = static::$dataType;
107 * Render the expression for insertion into the sql query
109 * @param array $fieldList
112 public function render(array $fieldList): string {
114 foreach ($this->args
as $arg) {
115 $rendered = $this->renderArg($arg, $fieldList);
116 if (strlen($rendered)) {
117 $output .= (strlen($output) ?
' ' : '') . $rendered;
120 return $this->getName() . '(' . $output . ')';
125 * @param array $fieldList
128 private function renderArg($arg, $fieldList): string {
129 $rendered = implode(' ', $arg['prefix']);
130 foreach ($arg['expr'] ??
[] as $idx => $expr) {
131 if (strlen($rendered) ||
$idx) {
132 $rendered .= $idx ?
', ' : ' ';
134 $rendered .= $expr->render($fieldList);
136 if ($arg['suffix']) {
137 $rendered .= (strlen($rendered) ?
' ' : '') . implode(' ', $arg['suffix']);
145 public function getAlias(): string {
146 return $this->alias ??
$this->getName() . ':' . implode('_', $this->fields
);
150 * Get the name of this sql function.
153 public static function getName(): string {
154 $className = static::class;
155 return substr($className, strrpos($className, 'SqlFunction') +
11);
159 * Get the param metadata for this sql function.
162 final public static function getParams(): array {
164 foreach (static::params() as $param) {
165 // Merge in defaults to ensure each param has these properties
166 $params[] = $param +
[
168 'label' => ts('Select'),
174 'must_be' => ['SqlField', 'SqlFunction', 'SqlString', 'SqlNumber', 'SqlNull'],
175 'api_default' => NULL,
181 abstract protected static function params(): array;
184 * Get the arguments passed to this sql function instance.
185 * @return array{prefix: array, suffix: array, expr: SqlExpression}[]
187 public function getArgs(): array {
194 public static function getCategory(): string {
195 return static::$category;
199 * All functions return 'SqlFunction' as their type.
201 * To get the function name @see SqlFunction::getName()
204 public function getType(): string {
205 return 'SqlFunction';
211 abstract public static function getDescription(): string;