19753d91acfa4f73e5b15b10d68a8e5eb073aeca
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
{
21 protected static $params = [];
26 * Parse the argument string into an array of function arguments
28 protected function initialize() {
29 $arg = trim(substr($this->expr
, strpos($this->expr
, '(') +
1, -1));
30 foreach ($this->getParams() as $param) {
31 $prefix = $this->captureKeyword($param['prefix'], $arg);
32 if ($param['expr'] && isset($prefix) ||
in_array('', $param['prefix']) ||
!$param['optional']) {
33 $this->captureExpressions($arg, $param['expr'], $param['must_be'], $param['cant_be']);
34 $this->captureKeyword($param['suffix'], $arg);
40 * Shift a keyword off the beginning of the argument string and into the argument array.
42 * @param array $keywords
43 * Whitelist of keywords
47 private function captureKeyword($keywords, &$arg) {
48 foreach (array_filter($keywords) as $key) {
49 if (strpos($arg, $key . ' ') === 0) {
51 $arg = ltrim(substr($arg, strlen($key)));
59 * Shifts 0 or more expressions off the argument string and into the argument array
63 * @param array $mustBe
64 * @param array $cantBe
65 * @throws \API_Exception
67 private function captureExpressions(&$arg, $limit, $mustBe, $cantBe) {
71 $item = $this->captureExpression($arg);
72 $arg = ltrim(substr($arg, strlen($item)));
73 $expr = SqlExpression
::convert($item, FALSE, $mustBe, $cantBe);
74 $this->fields
= array_merge($this->fields
, $expr->getFields());
78 $this->args
[] = $expr;
80 // Keep going if we have a comma indicating another expression follows
81 if ($captured < $limit && substr($arg, 0, 1) === ',') {
82 $arg = ltrim(substr($arg, 1));
91 * Scans the beginning of a string for an expression; stops when it hits delimiter
96 private function captureExpression($arg) {
97 $chars = str_split($arg);
98 $isEscaped = $quote = NULL;
100 $quotes = ['"', "'"];
104 $enclosures = array_fill_keys($brackets, 0);
105 foreach ($chars as $index => $char) {
106 if (!$isEscaped && in_array($char, $quotes, TRUE)) {
107 // Open quotes - we'll ignore everything inside
112 elseif ($char === $quote) {
117 // Delineates end of expression
118 if (($char == ',' ||
$char == ' ') && !array_filter($enclosures)) {
121 // Open brackets - we'll ignore delineators inside
122 if (isset($enclosures[$char])) {
123 $enclosures[$char]++
;
126 if (isset($brackets[$char]) && $enclosures[$brackets[$char]]) {
127 $enclosures[$brackets[$char]]--;
131 // We are escaping the next char if this is a backslash not preceded by an odd number of backslashes
132 $isEscaped = $char === '\\' && ((strlen($item) - strlen(rtrim($item, '\\'))) %
2);
137 public function render(array $fieldList): string {
138 $output = $this->getName() . '(';
139 foreach ($this->args
as $index => $arg) {
140 if ($index && $arg !== ',') {
143 if (is_object($arg)) {
144 $output .= $arg->render($fieldList);
150 return $output . ')';
156 public function getAlias(): string {
157 return $this->alias ??
$this->getName() . ':' . implode('_', $this->fields
);
161 * Get the name of this sql function.
164 public static function getName(): string {
165 $className = static::class;
166 return substr($className, strrpos($className, 'SqlFunction') +
11);
170 * Get the param metadata for this sql function.
173 public static function getParams(): array {
175 foreach (static::$params as $param) {
176 // Merge in defaults to ensure each param has these properties
177 $params[] = $param +
[
183 'cant_be' => ['SqlWild'],