APIv4 - Add more SQL functions
authorColeman Watts <coleman@civicrm.org>
Mon, 20 Apr 2020 13:18:04 +0000 (09:18 -0400)
committerColeman Watts <coleman@civicrm.org>
Wed, 24 Jun 2020 22:17:00 +0000 (18:17 -0400)
20 files changed:
CRM/Api4/Page/Api4Explorer.php
Civi/Api4/Query/SqlFunction.php
Civi/Api4/Query/SqlFunctionABS.php [new file with mode: 0644]
Civi/Api4/Query/SqlFunctionAVG.php
Civi/Api4/Query/SqlFunctionCOALESCE.php [new file with mode: 0644]
Civi/Api4/Query/SqlFunctionCONCAT.php [new file with mode: 0644]
Civi/Api4/Query/SqlFunctionCOUNT.php
Civi/Api4/Query/SqlFunctionCURDATE.php [new file with mode: 0644]
Civi/Api4/Query/SqlFunctionGREATEST.php [new file with mode: 0644]
Civi/Api4/Query/SqlFunctionGROUP_CONCAT.php [new file with mode: 0644]
Civi/Api4/Query/SqlFunctionISNULL.php [new file with mode: 0644]
Civi/Api4/Query/SqlFunctionLEAST.php [new file with mode: 0644]
Civi/Api4/Query/SqlFunctionLOWER.php [new file with mode: 0644]
Civi/Api4/Query/SqlFunctionMAX.php
Civi/Api4/Query/SqlFunctionMIN.php
Civi/Api4/Query/SqlFunctionNULLIF.php [new file with mode: 0644]
Civi/Api4/Query/SqlFunctionREPLACE.php [new file with mode: 0644]
Civi/Api4/Query/SqlFunctionROUND.php [new file with mode: 0644]
Civi/Api4/Query/SqlFunctionSUM.php
Civi/Api4/Query/SqlFunctionUPPER.php [new file with mode: 0644]

index c5280bb2573ebd292bb90ca704d780d17922006c..35897d051d4d67de3c4beae2d8b5ee705dc80340 100644 (file)
@@ -71,6 +71,7 @@ class CRM_Api4_Page_Api4Explorer extends CRM_Core_Page {
           $fns[] = [
             'name' => $className::getName(),
             'params' => $className::getParams(),
+            'category' => $className::getCategory(),
           ];
         }
       }
index 19753d91acfa4f73e5b15b10d68a8e5eb073aeca..dacf04c5338d2d6c68e188c51acad82100e5e33b 100644 (file)
@@ -22,6 +22,19 @@ abstract class SqlFunction extends SqlExpression {
 
   protected $args = [];
 
+  /**
+   * Used for categorizing functions in the UI
+   *
+   * @var string
+   */
+  protected static $category;
+
+  const CATEGORY_AGGREGATE = 'aggregate',
+    CATEGORY_COMPARISON = 'comparison',
+    CATEGORY_DATE = 'date',
+    CATEGORY_MATH = 'math',
+    CATEGORY_STRING = 'string';
+
   /**
    * Parse the argument string into an array of function arguments
    */
@@ -186,4 +199,11 @@ abstract class SqlFunction extends SqlExpression {
     return $params;
   }
 
+  /**
+   * @return string
+   */
+  public static function getCategory(): string {
+    return static::$category;
+  }
+
 }
diff --git a/Civi/Api4/Query/SqlFunctionABS.php b/Civi/Api4/Query/SqlFunctionABS.php
new file mode 100644 (file)
index 0000000..f626e41
--- /dev/null
@@ -0,0 +1,29 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+namespace Civi\Api4\Query;
+
+/**
+ * Sql function
+ */
+class SqlFunctionABS extends SqlFunction {
+
+  protected static $category = self::CATEGORY_MATH;
+
+  protected static $params = [
+    [
+      'expr' => 1,
+      'optional' => FALSE,
+      'must_be' => ['SqlField', 'SqlNumber'],
+    ],
+  ];
+
+}
index 9a064135e9170a8fdbbe243178f5b4083e82d433..9d9fcdd539ea45c0bbf23a3be16a244a7abadecf 100644 (file)
@@ -16,6 +16,8 @@ namespace Civi\Api4\Query;
  */
 class SqlFunctionAVG extends SqlFunction {
 
+  protected static $category = self::CATEGORY_AGGREGATE;
+
   protected static $params = [
     [
       'prefix' => ['', 'DISTINCT', 'ALL'],
diff --git a/Civi/Api4/Query/SqlFunctionCOALESCE.php b/Civi/Api4/Query/SqlFunctionCOALESCE.php
new file mode 100644 (file)
index 0000000..938eb02
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+namespace Civi\Api4\Query;
+
+/**
+ * Sql function
+ */
+class SqlFunctionCOALESCE extends SqlFunction {
+
+  protected static $category = self::CATEGORY_COMPARISON;
+
+  protected static $params = [
+    [
+      'expr' => 99,
+      'optional' => FALSE,
+    ],
+  ];
+
+}
diff --git a/Civi/Api4/Query/SqlFunctionCONCAT.php b/Civi/Api4/Query/SqlFunctionCONCAT.php
new file mode 100644 (file)
index 0000000..ff6b7b9
--- /dev/null
@@ -0,0 +1,29 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+namespace Civi\Api4\Query;
+
+/**
+ * Sql function
+ */
+class SqlFunctionCONCAT extends SqlFunction {
+
+  protected static $category = self::CATEGORY_STRING;
+
+  protected static $params = [
+    [
+      'expr' => 99,
+      'optional' => FALSE,
+      'must_be' => ['SqlField', 'SqlString'],
+    ],
+  ];
+
+}
index d444675350b398abf816a1d322f7de1562b392f0..84bb2105fb772798f287d5d6cfb3c863f485d898 100644 (file)
@@ -16,6 +16,8 @@ namespace Civi\Api4\Query;
  */
 class SqlFunctionCOUNT extends SqlFunction {
 
+  protected static $category = self::CATEGORY_AGGREGATE;
+
   protected static $params = [
     [
       'prefix' => ['', 'DISTINCT', 'ALL'],
diff --git a/Civi/Api4/Query/SqlFunctionCURDATE.php b/Civi/Api4/Query/SqlFunctionCURDATE.php
new file mode 100644 (file)
index 0000000..db3cb84
--- /dev/null
@@ -0,0 +1,21 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+namespace Civi\Api4\Query;
+
+/**
+ * Sql function
+ */
+class SqlFunctionCURDATE extends SqlFunction {
+
+  protected static $category = self::CATEGORY_DATE;
+
+}
diff --git a/Civi/Api4/Query/SqlFunctionGREATEST.php b/Civi/Api4/Query/SqlFunctionGREATEST.php
new file mode 100644 (file)
index 0000000..8b2dc40
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+namespace Civi\Api4\Query;
+
+/**
+ * Sql function
+ */
+class SqlFunctionGREATEST extends SqlFunction {
+
+  protected static $category = self::CATEGORY_COMPARISON;
+
+  protected static $params = [
+    [
+      'expr' => 99,
+      'optional' => FALSE,
+    ],
+  ];
+
+}
diff --git a/Civi/Api4/Query/SqlFunctionGROUP_CONCAT.php b/Civi/Api4/Query/SqlFunctionGROUP_CONCAT.php
new file mode 100644 (file)
index 0000000..43d548a
--- /dev/null
@@ -0,0 +1,43 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+namespace Civi\Api4\Query;
+
+/**
+ * Sql function
+ */
+class SqlFunctionGROUP_CONCAT extends SqlFunction {
+
+  protected static $category = self::CATEGORY_AGGREGATE;
+
+  protected static $params = [
+    [
+      'prefix' => ['', 'DISTINCT', 'ALL'],
+      'expr' => 1,
+      'must_be' => ['SqlField'],
+      'optional' => FALSE,
+    ],
+    [
+      'prefix' => ['ORDER BY'],
+      'expr' => 1,
+      'suffix' => ['', 'ASC', 'DESC'],
+      'must_be' => ['SqlField'],
+      'optional' => TRUE,
+    ],
+    [
+      'prefix' => ['SEPARATOR'],
+      'expr' => 1,
+      'must_be' => ['SqlString'],
+      'optional' => TRUE,
+    ],
+  ];
+
+}
diff --git a/Civi/Api4/Query/SqlFunctionISNULL.php b/Civi/Api4/Query/SqlFunctionISNULL.php
new file mode 100644 (file)
index 0000000..30ab3dd
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+namespace Civi\Api4\Query;
+
+/**
+ * Sql function
+ */
+class SqlFunctionISNULL extends SqlFunction {
+
+  protected static $category = self::CATEGORY_COMPARISON;
+
+  protected static $params = [
+    [
+      'expr' => 1,
+      'optional' => FALSE,
+    ],
+  ];
+
+}
diff --git a/Civi/Api4/Query/SqlFunctionLEAST.php b/Civi/Api4/Query/SqlFunctionLEAST.php
new file mode 100644 (file)
index 0000000..4c22415
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+namespace Civi\Api4\Query;
+
+/**
+ * Sql function
+ */
+class SqlFunctionLEAST extends SqlFunction {
+
+  protected static $category = self::CATEGORY_COMPARISON;
+
+  protected static $params = [
+    [
+      'expr' => 99,
+      'optional' => FALSE,
+    ],
+  ];
+
+}
diff --git a/Civi/Api4/Query/SqlFunctionLOWER.php b/Civi/Api4/Query/SqlFunctionLOWER.php
new file mode 100644 (file)
index 0000000..eefd0f9
--- /dev/null
@@ -0,0 +1,29 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+namespace Civi\Api4\Query;
+
+/**
+ * Sql function
+ */
+class SqlFunctionLOWER extends SqlFunction {
+
+  protected static $category = self::CATEGORY_STRING;
+
+  protected static $params = [
+    [
+      'expr' => 1,
+      'optional' => FALSE,
+      'must_be' => ['SqlField', 'SqlString'],
+    ],
+  ];
+
+}
index f80ebeea15abfb7fb804642d675ea920f6950400..c40d5876aa6093bbc8a1d3981667b444f873df98 100644 (file)
@@ -16,6 +16,8 @@ namespace Civi\Api4\Query;
  */
 class SqlFunctionMAX extends SqlFunction {
 
+  protected static $category = self::CATEGORY_AGGREGATE;
+
   protected static $params = [
     [
       'prefix' => ['', 'DISTINCT', 'ALL'],
index 993a5b18eb876e2447b916c6ae4a6e99dc0e0d46..29c313452fc6c94abcbf4ff1cd20ed66542065a1 100644 (file)
@@ -16,6 +16,8 @@ namespace Civi\Api4\Query;
  */
 class SqlFunctionMIN extends SqlFunction {
 
+  protected static $category = self::CATEGORY_AGGREGATE;
+
   protected static $params = [
     [
       'prefix' => ['', 'DISTINCT', 'ALL'],
diff --git a/Civi/Api4/Query/SqlFunctionNULLIF.php b/Civi/Api4/Query/SqlFunctionNULLIF.php
new file mode 100644 (file)
index 0000000..53e7499
--- /dev/null
@@ -0,0 +1,32 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+namespace Civi\Api4\Query;
+
+/**
+ * Sql function
+ */
+class SqlFunctionNULLIF extends SqlFunction {
+
+  protected static $category = self::CATEGORY_COMPARISON;
+
+  protected static $params = [
+    [
+      'expr' => 1,
+      'optional' => FALSE,
+    ],
+    [
+      'expr' => 1,
+      'optional' => FALSE,
+    ],
+  ];
+
+}
diff --git a/Civi/Api4/Query/SqlFunctionREPLACE.php b/Civi/Api4/Query/SqlFunctionREPLACE.php
new file mode 100644 (file)
index 0000000..36d5bcd
--- /dev/null
@@ -0,0 +1,39 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+namespace Civi\Api4\Query;
+
+/**
+ * Sql function
+ */
+class SqlFunctionREPLACE extends SqlFunction {
+
+  protected static $category = self::CATEGORY_STRING;
+
+  protected static $params = [
+    [
+      'expr' => 1,
+      'optional' => FALSE,
+      'must_be' => ['SqlField', 'SqlString'],
+    ],
+    [
+      'expr' => 1,
+      'optional' => FALSE,
+      'must_be' => ['SqlField', 'SqlString'],
+    ],
+    [
+      'expr' => 1,
+      'optional' => FALSE,
+      'must_be' => ['SqlField', 'SqlString'],
+    ],
+  ];
+
+}
diff --git a/Civi/Api4/Query/SqlFunctionROUND.php b/Civi/Api4/Query/SqlFunctionROUND.php
new file mode 100644 (file)
index 0000000..29eb06c
--- /dev/null
@@ -0,0 +1,34 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+namespace Civi\Api4\Query;
+
+/**
+ * Sql function
+ */
+class SqlFunctionROUND extends SqlFunction {
+
+  protected static $category = self::CATEGORY_MATH;
+
+  protected static $params = [
+    [
+      'expr' => 1,
+      'optional' => FALSE,
+      'must_be' => ['SqlField', 'SqlNumber'],
+    ],
+    [
+      'expr' => 1,
+      'optional' => TRUE,
+      'must_be' => ['SqlNumber'],
+    ],
+  ];
+
+}
index 36f4ebb3cc3c6e7c886944b60e8633ededdb8d2a..44520c3f32836fe1af10a43b3f4b970e07271684 100644 (file)
@@ -16,6 +16,8 @@ namespace Civi\Api4\Query;
  */
 class SqlFunctionSUM extends SqlFunction {
 
+  protected static $category = self::CATEGORY_AGGREGATE;
+
   protected static $params = [
     [
       'prefix' => ['', 'DISTINCT', 'ALL'],
diff --git a/Civi/Api4/Query/SqlFunctionUPPER.php b/Civi/Api4/Query/SqlFunctionUPPER.php
new file mode 100644 (file)
index 0000000..c234303
--- /dev/null
@@ -0,0 +1,29 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+namespace Civi\Api4\Query;
+
+/**
+ * Sql function
+ */
+class SqlFunctionUPPER extends SqlFunction {
+
+  protected static $category = self::CATEGORY_STRING;
+
+  protected static $params = [
+    [
+      'expr' => 1,
+      'optional' => FALSE,
+      'must_be' => ['SqlField', 'SqlString'],
+    ],
+  ];
+
+}