-
-
Notifications
You must be signed in to change notification settings - Fork 824
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
APIv4 - Add EntitySet api for set-ops such as UNION
- Loading branch information
Showing
21 changed files
with
1,050 additions
and
589 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?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\Action\EntitySet; | ||
|
||
use Civi\Api4\Generic\DAOGetAction; | ||
use Civi\Api4\Generic\Result; | ||
use Civi\Api4\Generic\Traits\GroupAndHavingParamTrait; | ||
use Civi\Api4\Generic\Traits\SelectParamTrait; | ||
use Civi\Api4\Query\Api4EntitySetQuery; | ||
|
||
/** | ||
* @method array getSets() | ||
* @method setSets(array $sets) | ||
*/ | ||
class Get extends \Civi\Api4\Generic\AbstractQueryAction { | ||
|
||
use SelectParamTrait; | ||
use GroupAndHavingParamTrait; | ||
|
||
/** | ||
* Api queries to combine using UNION DISTINCT or UNION ALL | ||
* | ||
* The SQL rules of unions apply: each query must SELECT the same number of fields | ||
* with matching types (in order). Field names do not have to match; (returned fields | ||
* will use the name from the first query). | ||
* | ||
* @var array | ||
*/ | ||
protected $sets = []; | ||
|
||
/** | ||
* @param string $type | ||
* 'UNION DISTINCT' or 'UNION ALL' | ||
* @param \Civi\Api4\Generic\DAOGetAction $apiRequest | ||
* @return $this | ||
*/ | ||
public function addSet(string $type, DAOGetAction $apiRequest) { | ||
$this->sets[] = [$type, $apiRequest->getEntityName(), $apiRequest->getActionName(), $apiRequest->getParams()]; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @throws \CRM_Core_Exception | ||
*/ | ||
public function _run(Result $result) { | ||
$query = new Api4EntitySetQuery($this); | ||
$rows = $query->run(); | ||
\CRM_Utils_API_HTMLInputCoder::singleton()->decodeRows($rows); | ||
$result->exchangeArray($rows); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?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; | ||
|
||
use Civi\Api4\Generic\BasicGetFieldsAction; | ||
|
||
/** | ||
* API to query multiple entities with a UNION. | ||
* | ||
* @searchable none | ||
* @since 5.64 | ||
* @package Civi\Api4 | ||
*/ | ||
class EntitySet extends Generic\AbstractEntity { | ||
|
||
/** | ||
* @return \Civi\Api4\Action\EntitySet\Get | ||
*/ | ||
public static function get($checkPermissions = TRUE) { | ||
return (new Action\EntitySet\Get('EntitySet', __FUNCTION__)) | ||
->setCheckPermissions($checkPermissions); | ||
} | ||
|
||
/** | ||
* @return \Civi\Api4\Generic\BasicGetFieldsAction | ||
*/ | ||
public static function getFields($checkPermissions = TRUE) { | ||
return (new BasicGetFieldsAction('EntitySet', __FUNCTION__, function() { | ||
return []; | ||
}))->setCheckPermissions($checkPermissions); | ||
} | ||
|
||
public static function permissions() { | ||
return []; | ||
} | ||
|
||
/** | ||
* @param bool $plural | ||
* @return string | ||
*/ | ||
protected static function getEntityTitle($plural = FALSE) { | ||
return $plural ? ts('Entity Sets') : ts('Entity Set'); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?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\Generic\Traits; | ||
|
||
use Civi\Api4\Utils\CoreUtil; | ||
|
||
/** | ||
* @method $this setHaving(array $clauses) | ||
* @method array getHaving() | ||
* @method $this setGroupBy(array $clauses) | ||
* @method array getGroupBy() | ||
* @package Civi\Api4\Generic | ||
*/ | ||
trait GroupAndHavingParamTrait { | ||
|
||
/** | ||
* Field(s) by which to group the results. | ||
* | ||
* @var array | ||
*/ | ||
protected $groupBy = []; | ||
|
||
/** | ||
* Clause for filtering results after grouping and filters are applied. | ||
* | ||
* Each expression should correspond to an item from the SELECT array. | ||
* | ||
* @var array | ||
*/ | ||
protected $having = []; | ||
|
||
/** | ||
* @param string $field | ||
* @return $this | ||
*/ | ||
public function addGroupBy(string $field) { | ||
$this->groupBy[] = $field; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param string $expr | ||
* @param string $op | ||
* @param mixed $value | ||
* @return $this | ||
* @throws \CRM_Core_Exception | ||
*/ | ||
public function addHaving(string $expr, string $op, $value = NULL) { | ||
if (!in_array($op, CoreUtil::getOperators())) { | ||
throw new \CRM_Core_Exception('Unsupported operator'); | ||
} | ||
$this->having[] = [$expr, $op, $value]; | ||
return $this; | ||
} | ||
|
||
} |
Oops, something went wrong.