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 * Numeric sql expression
17 class SqlEquation
extends SqlExpression
{
27 public static $arithmeticOperators = [
37 public static $comparisonOperators = [
51 protected function initialize() {
52 $arg = trim(substr($this->expr
, strpos($this->expr
, '(') +
1, -1));
53 $permitted = ['SqlField', 'SqlString', 'SqlNumber', 'SqlNull'];
54 $operators = array_merge(self
::$arithmeticOperators, self
::$comparisonOperators);
55 while (strlen($arg)) {
56 $this->args
= array_merge($this->args
, $this->captureExpressions($arg, $permitted, FALSE));
57 $op = $this->captureKeyword($operators, $arg);
65 * Render the expression for insertion into the sql query
67 * @param array $fieldList
70 public function render(array $fieldList): string {
72 foreach ($this->args
as $arg) {
73 $output[] = is_string($arg) ?
$arg : $arg->render($fieldList);
75 return '(' . implode(' ', $output) . ')';
79 * Returns the alias to use for SELECT AS.
83 public function getAlias(): string {
84 return $this->alias ?? \CRM_Utils_String
::munge(trim($this->expr
, ' ()'), '_', 256);
88 * Change $dataType according to operator used in equation
90 * @see \Civi\Api4\Utils\FormattingUtil::formatOutputValues
91 * @param string $value
92 * @param string $dataType
95 public function formatOutputValue($value, &$dataType) {
96 foreach (self
::$comparisonOperators as $op) {
97 if (strpos($this->expr
, " $op ")) {
98 $dataType = 'Boolean';
101 foreach (self
::$arithmeticOperators as $op) {
102 if (strpos($this->expr
, " $op ")) {