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
{
24 protected static $params = [];
32 * Used for categorizing functions in the UI
36 protected static $category;
38 const CATEGORY_AGGREGATE
= 'aggregate',
39 CATEGORY_COMPARISON
= 'comparison',
40 CATEGORY_DATE
= 'date',
41 CATEGORY_MATH
= 'math',
42 CATEGORY_STRING
= 'string';
45 * Parse the argument string into an array of function arguments
47 protected function initialize() {
48 $arg = trim(substr($this->expr
, strpos($this->expr
, '(') +
1, -1));
49 foreach ($this->getParams() as $idx => $param) {
50 $prefix = $this->captureKeyword($param['prefix'], $arg);
56 if ($param['max_expr'] && isset($prefix) ||
in_array('', $param['prefix']) ||
!$param['optional']) {
57 $exprs = $this->captureExpressions($arg, $param['must_be'], $param['cant_be']);
58 if (count($exprs) < $param['min_expr'] ||
count($exprs) > $param['max_expr']) {
59 throw new \
API_Exception('Incorrect number of arguments for SQL function ' . static::getName());
61 $this->args
[$idx]['expr'] = $exprs;
62 $this->args
[$idx]['suffix'] = $this->captureKeyword($param['suffix'], $arg);
68 * Shift a keyword off the beginning of the argument string and return it.
70 * @param array $keywords
71 * Whitelist of keywords
75 private function captureKeyword($keywords, &$arg) {
76 foreach (array_filter($keywords) as $key) {
77 if (strpos($arg, $key . ' ') === 0) {
78 $arg = ltrim(substr($arg, strlen($key)));
86 * Shifts 0 or more expressions off the argument string and returns them
89 * @param array $mustBe
90 * @param array $cantBe
92 * @throws \API_Exception
94 private function captureExpressions(&$arg, $mustBe, $cantBe) {
98 $item = $this->captureExpression($arg);
99 $arg = ltrim(substr($arg, strlen($item)));
100 $expr = SqlExpression
::convert($item, FALSE, $mustBe, $cantBe);
101 $this->fields
= array_merge($this->fields
, $expr->getFields());
103 // Keep going if we have a comma indicating another expression follows
104 if (substr($arg, 0, 1) === ',') {
105 $arg = ltrim(substr($arg, 1));
115 * Scans the beginning of a string for an expression; stops when it hits delimiter
120 private function captureExpression($arg) {
121 $chars = str_split($arg);
122 $isEscaped = $quote = NULL;
124 $quotes = ['"', "'"];
128 $enclosures = array_fill_keys($brackets, 0);
129 foreach ($chars as $index => $char) {
130 if (!$isEscaped && in_array($char, $quotes, TRUE)) {
131 // Open quotes - we'll ignore everything inside
136 elseif ($char === $quote) {
141 // Delineates end of expression
142 if (($char == ',' ||
$char == ' ') && !array_filter($enclosures)) {
145 // Open brackets - we'll ignore delineators inside
146 if (isset($enclosures[$char])) {
147 $enclosures[$char]++
;
150 if (isset($brackets[$char]) && $enclosures[$brackets[$char]]) {
151 $enclosures[$brackets[$char]]--;
155 // We are escaping the next char if this is a backslash not preceded by an odd number of backslashes
156 $isEscaped = $char === '\\' && ((strlen($item) - strlen(rtrim($item, '\\'))) %
2);
162 * Render the expression for insertion into the sql query
164 * @param array $fieldList
167 public function render(array $fieldList): string {
169 $params = $this->getParams();
170 foreach ($this->args
as $index => $arg) {
171 $rendered = $this->renderArg($arg, $params[$index], $fieldList);
172 if (strlen($rendered)) {
173 $output .= (strlen($output) ?
' ' : '') . $rendered;
176 return $this->getName() . '(' . $output . ')';
181 * @param array $param
182 * @param array $fieldList
185 private function renderArg($arg, $param, $fieldList): string {
186 // Supply api_default
187 if (!isset($arg['prefix']) && !isset($arg['suffix']) && empty($arg['expr']) && !empty($param['api_default'])) {
189 'prefix' => $param['api_default']['prefix'] ??
reset($param['prefix']),
190 'expr' => array_map([parent
::class, 'convert'], $param['api_default']['expr'] ??
[]),
191 'suffix' => $param['api_default']['suffix'] ??
reset($param['suffix']),
194 $rendered = $arg['prefix'] ??
'';
195 foreach ($arg['expr'] ??
[] as $idx => $expr) {
196 if (strlen($rendered) ||
$idx) {
197 $rendered .= $idx ?
', ' : ' ';
199 $rendered .= $expr->render($fieldList);
201 if (isset($arg['suffix'])) {
202 $rendered .= (strlen($rendered) ?
' ' : '') . $arg['suffix'];
210 public function getAlias(): string {
211 return $this->alias ??
$this->getName() . ':' . implode('_', $this->fields
);
215 * Get the name of this sql function.
218 public static function getName(): string {
219 $className = static::class;
220 return substr($className, strrpos($className, 'SqlFunction') +
11);
224 * Get the param metadata for this sql function.
227 public static function getParams(): array {
229 foreach (static::$params as $param) {
230 // Merge in defaults to ensure each param has these properties
231 $params[] = $param +
[
238 'cant_be' => ['SqlWild'],
239 'api_default' => NULL,
246 * Get the arguments passed to this sql function instance.
249 public function getArgs(): array {
256 public static function getCategory(): string {
257 return static::$category;
263 abstract public static function getTitle(): string;