Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reorganize datastore classes #4187

Draft
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 1 addition & 32 deletions modules/common/src/Storage/AbstractDatabaseTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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.
*/
Expand Down
11 changes: 3 additions & 8 deletions modules/common/src/Storage/DatabaseTableInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
*
Expand Down
6 changes: 3 additions & 3 deletions modules/datastore/src/Controller/AbstractQueryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -65,7 +65,7 @@ public function __construct(
QueryService $queryService,
DatasetInfo $datasetInfo,
MetastoreApiResponse $metastoreApiResponse,
ConfigFactoryInterface $configFactory
ConfigFactoryInterface $configFactory,
) {
$this->queryService = $queryService;
$this->datasetInfo = $datasetInfo;
Expand Down Expand Up @@ -183,7 +183,7 @@ abstract public function formatResponse(
DatastoreQuery $datastoreQuery,
RootedJsonData $result,
array $dependencies = [],
?ParameterBag $params = NULL
?ParameterBag $params = NULL,
);

/**
Expand Down
2 changes: 1 addition & 1 deletion modules/datastore/src/Controller/QueryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function formatResponse(
DatastoreQuery $datastoreQuery,
RootedJsonData $result,
array $dependencies = [],
?ParameterBag $params = NULL
?ParameterBag $params = NULL,
) {
switch ($datastoreQuery->{"$.format"}) {
case 'csv':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function formatResponse(
DatastoreQuery $datastoreQuery,
RootedJsonData $result,
array $dependencies = [],
?ParameterBag $params = NULL
?ParameterBag $params = NULL,
) {
switch ($datastoreQuery->{"$.format"}) {
case 'csv':
Expand Down
2 changes: 1 addition & 1 deletion modules/datastore/src/DatastoreService.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function __construct(
QueueFactory $queue,
ImportJobStoreFactory $importJobStoreFactory,
DictionaryEnforcer $dictionaryEnforcer,
ResourceMapper $resourceMapper
ResourceMapper $resourceMapper,
) {
$this->resourceLocalizer = $resourceLocalizer;
$this->importServiceFactory = $importServiceFactory;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

namespace Drupal\datastore\Service;
namespace Drupal\datastore\Query;

use RootedData\RootedJsonData;

/**
* Datastore query data object.
*/
class DatastoreQuery extends RootedJsonData {
class DatastoreQueryRequest extends RootedJsonData {

/**
* Constructor.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
<?php

namespace Drupal\common\Storage;
namespace Drupal\datastore\Query;

use Contracts\SorterInterface;
use Contracts\ConditionerInterface;
use Contracts\OffsetterInterface;
use Contracts\LimiterInterface;
use Contracts\OffsetterInterface;
use Contracts\SorterInterface;

/**
* DKAN API Query data object.
* Normalized datastore query object.
*
* This provides a more predictable structure for datastore queries,
* allowing SelectBuilder (or other backends in the future) to use more
* straightforward logic to build database API queries.
*/
class Query implements
SorterInterface,
ConditionerInterface,
OffsetterInterface,
LimiterInterface {
class NormalizedQuery implements
SorterInterface,
ConditionerInterface,
OffsetterInterface,
LimiterInterface {

/**
* The collection of records (usually, a database table) to query against.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<?php

namespace Drupal\datastore\Service;
namespace Drupal\datastore\Query;

use Drupal\common\DataResource;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\datastore\DatastoreService;
use Drupal\datastore\Storage\QueryFactory;
use RootedData\RootedJsonData;
use Symfony\Component\DependencyInjection\ContainerInterface;

Expand Down Expand Up @@ -43,13 +42,13 @@ public function __construct(DatastoreService $datastore) {
/**
* Run query.
*
* @param \Drupal\datastore\Service\DatastoreQuery $datastoreQuery
* @param \Drupal\datastore\Query\DatastoreQueryRequest $datastoreQuery
* DKAN Datastore Query API object.
*
* @return \RootedData\RootedJsonData
* Array of row/record objects.
*/
public function runQuery(DatastoreQuery $datastoreQuery) {
public function runQuery(DatastoreQueryRequest $datastoreQuery) {
$return = (object) [];

$this->getProperties($datastoreQuery);
Expand All @@ -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 [];
Expand All @@ -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"]);
Expand All @@ -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).
Expand All @@ -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.
Expand Down Expand Up @@ -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();
Expand All @@ -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 [];
Expand Down
Loading