From ebef426467fd07ae2cd3bc35975111e52cadff93 Mon Sep 17 00:00:00 2001 From: Dan Feder Date: Tue, 14 May 2024 12:03:28 -0400 Subject: [PATCH] Initial reorg of classes --- .../src/Storage/AbstractDatabaseTable.php | 33 +------- .../src/Storage/DatabaseTableInterface.php | 11 +-- .../Controller/AbstractQueryController.php | 6 +- .../src/Controller/QueryController.php | 2 +- .../Controller/QueryDownloadController.php | 2 +- modules/datastore/src/DatastoreService.php | 2 +- .../DatastoreQueryRequest.php} | 4 +- .../src/Query/NormalizedQuery.php} | 22 +++--- .../src/{Service => Query}/Query.php | 31 ++++---- .../QueryNormalizer.php} | 77 ++++++------------- .../src/Query/SelectBuilder.php} | 57 +++++++------- .../DatastoreSqlEndpointService.php | 28 +++---- .../datastore/src/Storage/DatabaseTable.php | 37 ++++++++- .../Unit/Controller/QueryControllerTest.php | 2 +- .../QueryDownloadControllerTest.php | 2 +- .../src/Unit/Service/DatastoreQueryTest.php | 6 +- 16 files changed, 149 insertions(+), 173 deletions(-) rename modules/datastore/src/{Service/DatastoreQuery.php => Query/DatastoreQueryRequest.php} (92%) rename modules/{common/src/Storage/Query.php => datastore/src/Query/NormalizedQuery.php} (91%) rename modules/datastore/src/{Service => Query}/Query.php (84%) rename modules/datastore/src/{Storage/QueryFactory.php => Query/QueryNormalizer.php} (81%) rename modules/{common/src/Storage/SelectFactory.php => datastore/src/Query/SelectBuilder.php} (88%) diff --git a/modules/common/src/Storage/AbstractDatabaseTable.php b/modules/common/src/Storage/AbstractDatabaseTable.php index 62e53a6715..f2d755449c 100644 --- a/modules/common/src/Storage/AbstractDatabaseTable.php +++ b/modules/common/src/Storage/AbstractDatabaseTable.php @@ -2,9 +2,8 @@ namespace Drupal\common\Storage; -use Drupal\Core\Database\Connection; -use Drupal\Core\Database\DatabaseExceptionWrapper; use Drupal\common\EventDispatcherTrait; +use Drupal\Core\Database\Connection; use Drupal\Core\Database\SchemaObjectExistsException; /** @@ -210,36 +209,6 @@ public function count(): int { return $query->countQuery()->execute()->fetchField(); } - /** - * Run a query on the database table. - * - * @param \Drupal\common\Storage\Query $query - * Query object. - * @param string $alias - * (Optional) alias for primary table. - * @param bool $fetch - * Fetch the rows if true, just return the result statement if not. - * - * @return array|\Drupal\Core\Database\StatementInterface - * Array of results if $fetch is true, otherwise result of - * Select::execute() (prepared Statement object or null). - */ - public function query(Query $query, string $alias = 't', $fetch = TRUE) { - $this->setTable(); - $query->collection = $this->getTableName(); - $selectFactory = new SelectFactory($this->connection, $alias); - $db_query = $selectFactory->create($query); - - try { - $result = $db_query->execute(); - } - catch (DatabaseExceptionWrapper $e) { - throw new \Exception($this->sanitizedErrorMessage($e->getMessage())); - } - - return $fetch ? $result->fetchAll() : $result; - } - /** * Create a minimal error message that does not leak database information. */ diff --git a/modules/common/src/Storage/DatabaseTableInterface.php b/modules/common/src/Storage/DatabaseTableInterface.php index a00741e2cd..60be7cdd73 100644 --- a/modules/common/src/Storage/DatabaseTableInterface.php +++ b/modules/common/src/Storage/DatabaseTableInterface.php @@ -2,12 +2,12 @@ namespace Drupal\common\Storage; -use Contracts\RemoverInterface; -use Contracts\RetrieverInterface; -use Contracts\StorerInterface; use Contracts\BulkRetrieverInterface; use Contracts\BulkStorerInterface; use Contracts\CountableInterface; +use Contracts\RemoverInterface; +use Contracts\RetrieverInterface; +use Contracts\StorerInterface; /** * Databaset table interface. @@ -19,11 +19,6 @@ interface DatabaseTableInterface extends StorerInterface, RetrieverInterface, Re */ public function destruct(); - /** - * Perform a SELECT query against the table. - */ - public function query(Query $query); - /** * Return the primary key for the table. * diff --git a/modules/datastore/src/Controller/AbstractQueryController.php b/modules/datastore/src/Controller/AbstractQueryController.php index 8a950fc75f..87bc74a55b 100644 --- a/modules/datastore/src/Controller/AbstractQueryController.php +++ b/modules/datastore/src/Controller/AbstractQueryController.php @@ -6,8 +6,8 @@ use Drupal\common\JsonResponseTrait; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; +use Drupal\datastore\Query\Query as QueryService; use Drupal\datastore\Service\DatastoreQuery; -use Drupal\datastore\Service\Query as QueryService; use Drupal\metastore\MetastoreApiResponse; use JsonSchema\Validator; use RootedData\RootedJsonData; @@ -65,7 +65,7 @@ public function __construct( QueryService $queryService, DatasetInfo $datasetInfo, MetastoreApiResponse $metastoreApiResponse, - ConfigFactoryInterface $configFactory + ConfigFactoryInterface $configFactory, ) { $this->queryService = $queryService; $this->datasetInfo = $datasetInfo; @@ -183,7 +183,7 @@ abstract public function formatResponse( DatastoreQuery $datastoreQuery, RootedJsonData $result, array $dependencies = [], - ?ParameterBag $params = NULL + ?ParameterBag $params = NULL, ); /** diff --git a/modules/datastore/src/Controller/QueryController.php b/modules/datastore/src/Controller/QueryController.php index 73c1185c5e..b3d900c57c 100644 --- a/modules/datastore/src/Controller/QueryController.php +++ b/modules/datastore/src/Controller/QueryController.php @@ -33,7 +33,7 @@ public function formatResponse( DatastoreQuery $datastoreQuery, RootedJsonData $result, array $dependencies = [], - ?ParameterBag $params = NULL + ?ParameterBag $params = NULL, ) { switch ($datastoreQuery->{"$.format"}) { case 'csv': diff --git a/modules/datastore/src/Controller/QueryDownloadController.php b/modules/datastore/src/Controller/QueryDownloadController.php index 49306a6acd..522843c296 100644 --- a/modules/datastore/src/Controller/QueryDownloadController.php +++ b/modules/datastore/src/Controller/QueryDownloadController.php @@ -21,7 +21,7 @@ public function formatResponse( DatastoreQuery $datastoreQuery, RootedJsonData $result, array $dependencies = [], - ?ParameterBag $params = NULL + ?ParameterBag $params = NULL, ) { switch ($datastoreQuery->{"$.format"}) { case 'csv': diff --git a/modules/datastore/src/DatastoreService.php b/modules/datastore/src/DatastoreService.php index 181cb7fef9..bf253d5d31 100644 --- a/modules/datastore/src/DatastoreService.php +++ b/modules/datastore/src/DatastoreService.php @@ -96,7 +96,7 @@ public function __construct( QueueFactory $queue, ImportJobStoreFactory $importJobStoreFactory, DictionaryEnforcer $dictionaryEnforcer, - ResourceMapper $resourceMapper + ResourceMapper $resourceMapper, ) { $this->resourceLocalizer = $resourceLocalizer; $this->importServiceFactory = $importServiceFactory; diff --git a/modules/datastore/src/Service/DatastoreQuery.php b/modules/datastore/src/Query/DatastoreQueryRequest.php similarity index 92% rename from modules/datastore/src/Service/DatastoreQuery.php rename to modules/datastore/src/Query/DatastoreQueryRequest.php index f94220d8da..49beeb57d0 100644 --- a/modules/datastore/src/Service/DatastoreQuery.php +++ b/modules/datastore/src/Query/DatastoreQueryRequest.php @@ -1,13 +1,13 @@ getProperties($datastoreQuery); @@ -72,13 +71,13 @@ public function runQuery(DatastoreQuery $datastoreQuery) { /** * Get an a schema for each resource. * - * @param \Drupal\datastore\Service\DatastoreQuery $datastoreQuery + * @param \Drupal\datastore\Query\DatastoreQueryRequest $datastoreQuery * DKAN Datastore Query API object. * * @return array * An assoc array containing a table schema for each resource. */ - private function getSchema(DatastoreQuery $datastoreQuery) { + private function getSchema(DatastoreQueryRequest $datastoreQuery) { $storageMap = $this->getQueryStorageMap($datastoreQuery); if (!$datastoreQuery->{"$.resources"}) { return []; @@ -98,13 +97,13 @@ private function getSchema(DatastoreQuery $datastoreQuery) { /** * Retrieve storage objects for all resources, and map to their aliases. * - * @param \Drupal\datastore\Service\DatastoreQuery $datastoreQuery + * @param \Drupal\datastore\Query\DatastoreQueryRequest $datastoreQuery * DatastoreQuery object. * * @return array * Array of storage objects, keyed to resource aliases. */ - public function getQueryStorageMap(DatastoreQuery $datastoreQuery) { + public function getQueryStorageMap(DatastoreQueryRequest $datastoreQuery) { $storageMap = []; foreach ($datastoreQuery->{"$.resources"} as $resource) { [$identifier, $version] = DataResource::getIdentifierAndVersion($resource["id"]); @@ -117,7 +116,7 @@ public function getQueryStorageMap(DatastoreQuery $datastoreQuery) { /** * Build query object for main "results query" for datastore. * - * @param \Drupal\datastore\Service\DatastoreQuery $datastoreQuery + * @param \Drupal\datastore\Query\DatastoreQueryRequest $datastoreQuery * DatastoreQuery object. * @param bool $fetch * Perform fetchAll and return array if true, else just statement (cursor). @@ -127,14 +126,14 @@ public function getQueryStorageMap(DatastoreQuery $datastoreQuery) { * @return array|\Drupal\Core\Database\StatementInterface * Array of result objects or result statement of $fetch is false. */ - public function runResultsQuery(DatastoreQuery $datastoreQuery, $fetch = TRUE, $csv = FALSE) { + public function runResultsQuery(DatastoreQueryRequest $datastoreQuery, $fetch = TRUE, $csv = FALSE) { $primaryAlias = $datastoreQuery->{"$.resources[0].alias"}; if (!$primaryAlias) { return []; } $storageMap = $this->getQueryStorageMap($datastoreQuery); - $query = QueryFactory::create($datastoreQuery, $storageMap); + $query = QueryNormalizer::create($datastoreQuery, $storageMap); // Get data dictionary fields. $meta_data = $csv != FALSE ? $this->getDatastoreService()->getDataDictionaryFields() : NULL; // Pass the data dictionary metadata to the query. @@ -199,17 +198,17 @@ private function stripRowKeys($row) { /** * Build count query object for datastore. * - * @param \Drupal\datastore\Service\DatastoreQuery $datastoreQuery + * @param \Drupal\datastore\Query\DatastoreQueryRequest $datastoreQuery * DatastoreQuery object. */ - private function runCountQuery(DatastoreQuery $datastoreQuery) { + private function runCountQuery(DatastoreQueryRequest $datastoreQuery) { $primaryAlias = $datastoreQuery->{"$.resources[0].alias"}; if (!$primaryAlias) { return 0; } $storageMap = $this->getQueryStorageMap($datastoreQuery); - $query = QueryFactory::create($datastoreQuery, $storageMap); + $query = QueryNormalizer::create($datastoreQuery, $storageMap); unset($query->limit, $query->offset); $query->count(); @@ -219,10 +218,10 @@ private function runCountQuery(DatastoreQuery $datastoreQuery) { /** * Under most circumstances, we want an explicit list of properties. * - * @param \Drupal\datastore\Service\DatastoreQuery $datastoreQuery + * @param \Drupal\datastore\Query\DatastoreQueryRequest $datastoreQuery * Datastore query object to be modified. */ - private function getProperties(DatastoreQuery $datastoreQuery) { + private function getProperties(DatastoreQueryRequest $datastoreQuery) { $primaryAlias = $datastoreQuery->{"$.resources[0].alias"}; if (!$primaryAlias) { return []; diff --git a/modules/datastore/src/Storage/QueryFactory.php b/modules/datastore/src/Query/QueryNormalizer.php similarity index 81% rename from modules/datastore/src/Storage/QueryFactory.php rename to modules/datastore/src/Query/QueryNormalizer.php index 9dc325e9ae..d8e23c6b67 100644 --- a/modules/datastore/src/Storage/QueryFactory.php +++ b/modules/datastore/src/Query/QueryNormalizer.php @@ -1,14 +1,11 @@ populateQuery(); } @@ -56,11 +53,11 @@ public static function create(DatastoreQuery $datastoreQuery, array $storageMap) /** * Create Query and populate with properties with DatastoreQuery object. * - * @return Drupal\common\Storage\Query + * @return \Drupal\datastore\Query\NormalizedQuery * Query object. */ - public function populateQuery(): Query { - $query = new Query(); + public function populateQuery(): NormalizedQuery { + $query = new NormalizedQuery(); $this->populateQueryProperties($query); $this->populateQueryConditions($query); @@ -79,13 +76,13 @@ public function populateQuery(): Query { /** * Helper function for adding group by clauses to the given query. * - * @param Drupal\common\Storage\Query $query + * @param \Drupal\datastore\Query\NormalizedQuery $query * DKAN query object we're building. * * @throws \Exception * When ungrouped properties are found in the datastore query. */ - private function populateQueryGroupBy(Query $query): void { + private function populateQueryGroupBy(NormalizedQuery $query): void { $groupings = $this->extractPropertyNames($this->datastoreQuery->{"$.groupings"} ?? []); if (empty($groupings)) { return; @@ -139,10 +136,10 @@ protected function extractPropertyNames(array $props): array { /** * Populate a query object with the queries from a datastore query payload. * - * @param Drupal\common\Storage\Query $query + * @param \Drupal\datastore\Query\NormalizedQuery $query * DKAN generalized query object. */ - private function populateQueryProperties(Query $query) { + private function populateQueryProperties(NormalizedQuery $query) { if (empty($this->datastoreQuery->{"$.properties"})) { return; } @@ -161,10 +158,7 @@ private function populateQueryProperties(Query $query) { * Standardized property object with "collection" instead of "resource." */ private function propertyConvert($property) { - if (is_array($property) && isset($property["resource"])) { - $property = (object) self::resourceRename($property); - } - elseif (is_array($property) && isset($property["expression"])) { + if (is_array($property) && isset($property["expression"])) { $property["expression"] = $this->expressionConvert($property["expression"]); $property = (object) $property; } @@ -181,7 +175,7 @@ private function propertyConvert($property) { * An expression from a datastore query, including "resources". * * @return object - * Standardized expression object with "collection" instead of "resource". + * Standardized expression object. */ private function expressionConvert(array $expression) { foreach ($expression["operands"] as $key => $operand) { @@ -214,25 +208,25 @@ private function operandConvert($operand) { /** * Process both potential sorting direction. * - * @param Drupal\common\Storage\Query $query + * @param \Drupal\datastore\Query\NormalizedQuery $query * DKAN query object we're building. */ - private function populateQuerySorts(Query $query) { + private function populateQuerySorts(NormalizedQuery $query) { if (!$this->datastoreQuery->{"$.sorts"}) { return; } foreach ($this->datastoreQuery->{"$.sorts"} as $sort) { - $query->sorts[] = (object) self::resourceRename($sort); + $query->sorts[] = (object) $sort; } } /** * Parse and normalize query conditions. * - * @param Drupal\common\Storage\Query $query + * @param \Drupal\datastore\Query\NormalizedQuery $query * DKAN query object we're building. */ - private function populateQueryConditions(Query $query) { + private function populateQueryConditions(NormalizedQuery $query) { if (empty($this->datastoreQuery->{"$.conditions"})) { return; } @@ -257,7 +251,7 @@ private function populateQueryCondition($condition) { $primaryAlias = $this->datastoreQuery->{"$.resources[0].alias"}; if (isset($condition["property"])) { $return = (object) [ - "collection" => $condition["resource"] ?? $primaryAlias, + "resource" => $condition["resource"] ?? $primaryAlias, "property" => $condition["property"], "value" => $this->propertyConvert($condition["value"]), ]; @@ -294,10 +288,10 @@ private function populateQueryGroup($conditionGroup) { /** * Helper function for converting joins to Query format. * - * @param Drupal\common\Storage\Query $query + * @param \Drupal\datastore\Query\NormalizedQuery $query * DKAN query object we're building. */ - private function populateQueryJoins(Query $query) { + private function populateQueryJoins(NormalizedQuery $query) { if (empty($this->datastoreQuery->{"$.joins"}) && count($this->datastoreQuery->{"$.resources"}) <= 1) { return; } @@ -319,33 +313,10 @@ private function populateQueryJoins(Query $query) { private function populateQueryJoin($join) { $storage = $this->storageMap[$join["resource"]]; $queryJoin = new \stdClass(); - $queryJoin->collection = $storage->getTableName(); + $queryJoin->resource = $storage->getTableName(); $queryJoin->alias = $join["resource"]; $queryJoin->condition = (object) $this->populateQueryCondition($join["condition"]); return $queryJoin; } - /** - * Rename any "resource" keys to "collection" in assoc. array. - * - * @param array $input - * Input array. - * - * @return array - * Array with renamed keys. - */ - private static function resourceRename(array $input) { - $return = []; - foreach ($input as $key => $value) { - if ($key == "resource") { - $key = "collection"; - } - if (is_array($value)) { - $value = self::resourceRename($value); - } - $return[$key] = $value; - } - return $return; - } - } diff --git a/modules/common/src/Storage/SelectFactory.php b/modules/datastore/src/Query/SelectBuilder.php similarity index 88% rename from modules/common/src/Storage/SelectFactory.php rename to modules/datastore/src/Query/SelectBuilder.php index b041c96d11..9222dd8055 100644 --- a/modules/common/src/Storage/SelectFactory.php +++ b/modules/datastore/src/Query/SelectBuilder.php @@ -1,14 +1,14 @@ dbQuery = $this->connection->select($query->collection, $this->alias); $this->setQueryProperties($query); @@ -81,10 +81,10 @@ public function create(Query $query): Select { /** * Specify fields on DB query. * - * @param Drupal\common\Storage\Query $query + * @param \Drupal\datastore\Query\NormalizedQuery $query * A DKAN query object. */ - private function setQueryProperties(Query $query) { + private function setQueryProperties(NormalizedQuery $query) { // If properties is empty, just get all from base collection. if (empty($query->properties)) { $this->dbQuery->fields($this->alias); @@ -97,11 +97,16 @@ private function setQueryProperties(Query $query) { } /** - * Reformatting date fields. - * - * {@inheritdoc} + * Reformat date fields. + * + * @param \Drupal\Core\Database\Query\Select $db_query + * DB Query object. + * @param array $fields + * Array of fields from db query. + * @param array $meta_data + * Data dictionary fields. */ - private function addDateExpressions($db_query, $fields, $meta_data) { + private function addDateExpressions(Select $db_query, array $fields, array $meta_data) { foreach ($meta_data as $definition) { // Confirm definition name is in the fields list. if ($fields[$definition['name']]['field'] && $definition['type'] == 'date') { @@ -113,10 +118,10 @@ private function addDateExpressions($db_query, $fields, $meta_data) { /** * Set a single property. * - * @param mixed $property + * @param object $property * One property from a query properties array. */ - private function setQueryProperty($property) { + private function setQueryProperty(object $property) { if (isset($property->expression)) { $expressionStr = $this->expressionToString($property->expression); @@ -124,7 +129,7 @@ private function setQueryProperty($property) { } else { $property = $this->normalizeProperty($property); - $this->dbQuery->addField($property->collection, $property->property, $property->alias); + $this->dbQuery->addField($property->resource, $property->property, $property->alias); } } @@ -145,7 +150,7 @@ private function normalizeProperty($property): object { "alias" => NULL, ]; } - if (!is_object($property) || !isset($property->property) || !isset($property->collection)) { + if (!is_object($property) || !isset($property->property) || !isset($property->resource)) { throw new \Exception("Bad query property: " . print_r($property, 1)); } self::safeProperty($property->property); @@ -256,10 +261,10 @@ private function propertyToString($property) { /** * Set filter conditions on DB query. * - * @param Drupal\common\Storage\Query $query + * @param \Drupal\datastore\Query\NormalizedQuery $query * A DKAN query object. */ - private function setQueryConditions(Query $query) { + private function setQueryConditions(NormalizedQuery $query) { foreach ($query->conditions as $c) { if (isset($c->groupOperator)) { $this->addConditionGroup($this->dbQuery, $c); @@ -360,20 +365,20 @@ private function addConditionGroup($statementObj, $conditionGroup) { /** * Set fields to group by on DB query. * - * @param Query $query + * @param \Drupal\datastore\Query\NormalizedQuery $query * A DKAN query object. */ - private function setQueryGroupBy(Query $query) { + private function setQueryGroupBy(NormalizedQuery $query) { array_map([$this->dbQuery, 'groupBy'], $query->groupby); } /** * Set sort order on DB query. * - * @param Query $query + * @param \Drupal\datastore\Query\NormalizedQuery $query * A DKAN query object. */ - private function setQueryOrderBy(Query $query) { + private function setQueryOrderBy(NormalizedQuery $query) { foreach ($query->sorts as $sort) { $this->setQueryDirectionOrderBy($sort, $this->dbQuery); } @@ -405,10 +410,10 @@ private function setQueryDirectionOrderBy($sort) { /** * Set limit and offset on DB query. * - * @param Query $query + * @param \Drupal\datastore\Query\NormalizedQuery $query * A DKAN query object. */ - private function setQueryLimitAndOffset(Query $query) { + private function setQueryLimitAndOffset(NormalizedQuery $query) { if (isset($query->limit) && $query->limit !== NULL) { $this->dbQuery->range(($query->offset ?? 0), ($query->limit)); } @@ -420,10 +425,10 @@ private function setQueryLimitAndOffset(Query $query) { /** * Add joins to the DB query. * - * @param Query $query + * @param Drupal\datastore\Query\NormalizedQuery $query * A DKAN query object. */ - private function setQueryJoins(Query $query) { + private function setQueryJoins(NormalizedQuery $query) { foreach ($query->joins as $join) { if (isset($join->condition)) { $this->dbQuery->join($join->collection, $join->alias, $this->conditionString($join->condition)); diff --git a/modules/datastore/src/SqlEndpoint/DatastoreSqlEndpointService.php b/modules/datastore/src/SqlEndpoint/DatastoreSqlEndpointService.php index 50797cd9bc..ec0862136c 100644 --- a/modules/datastore/src/SqlEndpoint/DatastoreSqlEndpointService.php +++ b/modules/datastore/src/SqlEndpoint/DatastoreSqlEndpointService.php @@ -5,8 +5,8 @@ use Drupal\common\DataResource; use Drupal\Core\Config\ConfigFactory; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; -use Drupal\common\Storage\Query; use Drupal\datastore\DatastoreService; +use Drupal\datastore\Query\NormalizedQuery; use Drupal\datastore\SqlEndpoint\Helper\GetStringsFromStateMachineExecution; use Drupal\datastore\SqlParser\SqlParser; use Drupal\datastore\Storage\DatabaseTable; @@ -146,10 +146,10 @@ protected function getTableNameFromSelect(MachineOfMachines $selectMachine): str * @param string $sqlString * A string with a sql statement. * - * @return \Drupal\datastore\Storage\Query + * @return \Drupal\datastore\Query\NormalizedQuery * A query object. */ - private function getQueryObject(string $sqlString): Query { + private function getQueryObject(string $sqlString): NormalizedQuery { return $this->getQueryObjectFromStateMachine($this->validate($sqlString)); } @@ -168,14 +168,14 @@ private function validate(string $sqlString): MachineOfMachines { /** * Take an instantiated state machine build a query object. * - * @param Maquina\StateMachine\MachineOfMachines $state_machine + * @param \Maquina\StateMachine\MachineOfMachines $state_machine * The state machine returned from the validate() function. * - * @return Drupal\common\Storage\Query + * @return \Drupal\datastore\Query\NormalizedQuery * A Drupal query object */ - private function getQueryObjectFromStateMachine(MachineOfMachines $state_machine): Query { - $object = new Query(); + private function getQueryObjectFromStateMachine(MachineOfMachines $state_machine): NormalizedQuery { + $object = new NormalizedQuery(); $this->setQueryObjectSelect($object, $state_machine->gsm('select')); $this->setQueryObjectWhere($object, $state_machine->gsm('where')); $this->setQueryObjectOrderBy($object, $state_machine->gsm('order_by')); @@ -187,12 +187,12 @@ private function getQueryObjectFromStateMachine(MachineOfMachines $state_machine /** * Set select statements on query object. * - * @param \Drupal\common\Storage\Query $object + * @param \Drupal\datastore\Query\NormalizedQuery $object * A drupal query object. * @param \Maquina\StateMachine\MachineOfMachines $state_machine * The state machine from validate(). */ - private function setQueryObjectSelect(Query $object, MachineOfMachines $state_machine) { + private function setQueryObjectSelect(NormalizedQuery $object, MachineOfMachines $state_machine) { $strings = $this->getStringsFromStringMachine($state_machine->gsm('select_count_all')); if (!empty($strings)) { $object->count(); @@ -213,12 +213,12 @@ private function setQueryObjectSelect(Query $object, MachineOfMachines $state_ma /** * Set where conditions on query object. * - * @param \Drupal\common\Storage\Query $object + * @param \Drupal\datastore\Query\NormalizedQuery $object * A drupal query object. * @param \Maquina\StateMachine\MachineOfMachines $state_machine * The state machine from validate(). */ - private function setQueryObjectWhere(Query $object, MachineOfMachines $state_machine) { + private function setQueryObjectWhere(NormalizedQuery $object, MachineOfMachines $state_machine) { $properties = $this->getStringsFromStringMachine($state_machine->gsm('where_column')); $quoted_string = $state_machine->gsm('quoted_string'); if (!($quoted_string instanceof MachineOfMachines)) { @@ -237,12 +237,12 @@ private function setQueryObjectWhere(Query $object, MachineOfMachines $state_mac /** * Set sorting on query object. * - * @param \Drupal\common\Storage\Query $object + * @param \Drupal\datastore\Query\NormalizedQuery $object * A drupal query object. * @param \Maquina\StateMachine\MachineOfMachines $state_machine * The state machine from validate(). */ - private function setQueryObjectOrderBy(Query $object, MachineOfMachines $state_machine) { + private function setQueryObjectOrderBy(NormalizedQuery $object, MachineOfMachines $state_machine) { $properties = $this->getStringsFromStringMachine($state_machine->gsm('order_var')); $direction = $this->getStringsFromStringMachine($state_machine->gsm('order_asc')); @@ -256,7 +256,7 @@ private function setQueryObjectOrderBy(Query $object, MachineOfMachines $state_m /** * Private. */ - private function setQueryObjectLimit(Query $object, MachineOfMachines $state_machine) { + private function setQueryObjectLimit(NormalizedQuery $object, MachineOfMachines $state_machine) { $limit = $this->getStringsFromStringMachine($state_machine->gsm('numeric1')); if (empty($limit)) { diff --git a/modules/datastore/src/Storage/DatabaseTable.php b/modules/datastore/src/Storage/DatabaseTable.php index bc6cfa4370..49c31e8bbb 100755 --- a/modules/datastore/src/Storage/DatabaseTable.php +++ b/modules/datastore/src/Storage/DatabaseTable.php @@ -2,9 +2,12 @@ namespace Drupal\datastore\Storage; -use Drupal\Core\Database\Connection; use Drupal\common\Storage\AbstractDatabaseTable; +use Drupal\Core\Database\Connection; +use Drupal\Core\Database\DatabaseExceptionWrapper; use Drupal\datastore\DatastoreResource; +use Drupal\datastore\Query\NormalizedQuery; +use Drupal\datastore\Query\SelectBuilder; use Psr\Log\LoggerInterface; /** @@ -41,7 +44,7 @@ class DatabaseTable extends AbstractDatabaseTable implements \JsonSerializable { public function __construct( Connection $connection, DatastoreResource $resource, - LoggerInterface $loggerChannel + LoggerInterface $loggerChannel, ) { // Set resource before calling the parent constructor. The parent calls // getTableName which we implement and needs the resource to operate. @@ -266,4 +269,34 @@ protected function translateType(string $type, $extra = NULL) { ]; } + /** + * Run a query on the database table. + * + * @param \Drupal\datastore\Query\NormalizedQuery $query + * Normalized Datastore query object. + * @param string $alias + * (Optional) alias for primary table. + * @param bool $fetch + * Fetch the rows if true, just return the result statement if not. + * + * @return array|\Drupal\Core\Database\StatementInterface + * Array of results if $fetch is true, otherwise result of + * Select::execute() (prepared Statement object or null). + */ + public function query(NormalizedQuery $query, string $alias = 't', $fetch = TRUE) { + $this->setTable(); + $query->collection = $this->getTableName(); + $selectFactory = new SelectBuilder($this->connection, $alias); + $db_query = $selectFactory->create($query); + + try { + $result = $db_query->execute(); + } + catch (DatabaseExceptionWrapper $e) { + throw new \Exception($this->sanitizedErrorMessage($e->getMessage())); + } + + return $fetch ? $result->fetchAll() : $result; + } + } diff --git a/modules/datastore/tests/src/Unit/Controller/QueryControllerTest.php b/modules/datastore/tests/src/Unit/Controller/QueryControllerTest.php index 42f80af896..1b92d77d6f 100644 --- a/modules/datastore/tests/src/Unit/Controller/QueryControllerTest.php +++ b/modules/datastore/tests/src/Unit/Controller/QueryControllerTest.php @@ -9,7 +9,7 @@ use Drupal\datastore\Controller\QueryController; use Drupal\datastore\DatastoreResource; use Drupal\datastore\DatastoreService; -use Drupal\datastore\Service\Query; +use Drupal\datastore\Query\Query; use Drupal\datastore\Storage\SqliteDatabaseTable; use Drupal\metastore\MetastoreApiResponse; use Drupal\metastore\NodeWrapper\Data; diff --git a/modules/datastore/tests/src/Unit/Controller/QueryDownloadControllerTest.php b/modules/datastore/tests/src/Unit/Controller/QueryDownloadControllerTest.php index 651e881b39..533e66983f 100644 --- a/modules/datastore/tests/src/Unit/Controller/QueryDownloadControllerTest.php +++ b/modules/datastore/tests/src/Unit/Controller/QueryDownloadControllerTest.php @@ -13,7 +13,7 @@ use Drupal\datastore\Controller\QueryDownloadController; use Drupal\datastore\DatastoreResource; use Drupal\datastore\DatastoreService; -use Drupal\datastore\Service\Query; +use Drupal\datastore\Query\Query; use Drupal\datastore\Storage\SqliteDatabaseTable; use Drupal\metastore\MetastoreApiResponse; use Drupal\metastore\NodeWrapper\Data; diff --git a/modules/datastore/tests/src/Unit/Service/DatastoreQueryTest.php b/modules/datastore/tests/src/Unit/Service/DatastoreQueryTest.php index 77cfbdc2aa..eba6732857 100644 --- a/modules/datastore/tests/src/Unit/Service/DatastoreQueryTest.php +++ b/modules/datastore/tests/src/Unit/Service/DatastoreQueryTest.php @@ -20,9 +20,9 @@ use Drupal\datastore\Service\DatastoreQuery; use Drupal\datastore\Service\ImportService; use Drupal\datastore\Service\Info\ImportInfoList; -use Drupal\datastore\Service\Query; +use Drupal\datastore\Query\Query; use Drupal\datastore\Storage\DatabaseTable; -use Drupal\datastore\Storage\QueryFactory; +use Drupal\datastore\Query\QueryNormalizer; use Drupal\metastore\Storage\Data; use Drupal\metastore\Storage\DataFactory; use Drupal\Tests\common\Unit\Storage\QueryDataProvider as QueryData; @@ -45,7 +45,7 @@ public function testQueryCompare($testName) { $queryService = new Query(DatastoreService::create($container->getMock())); $datastoreQuery = $this->getDatastoreQueryFromJson($testName); $storageMap = $queryService->getQueryStorageMap($datastoreQuery); - $dkanQuery = QueryFactory::create($datastoreQuery, $storageMap); + $dkanQuery = QueryNormalizer::create($datastoreQuery, $storageMap); $dkanQueryCompare = QueryData::$testName(QueryData::QUERY_OBJECT); $dkanQueryCompare->showDbColumns = TRUE; $this->assertEquals(json_encode($dkanQuery, JSON_PRETTY_PRINT), json_encode($dkanQueryCompare, JSON_PRETTY_PRINT));