Skip to content

Commit

Permalink
Using the new abstract database table storage class in dkan_datastore… (
Browse files Browse the repository at this point in the history
#243)

* Using the new abstract database table storage class in dkan_datastore database table storage class.
* Fixing complexity issues.
* Fix tests.
* Fix cypress tests.
  • Loading branch information
fmizzell authored and thierrydallacroce committed Oct 31, 2019
1 parent 88dcbb2 commit ab415d3
Show file tree
Hide file tree
Showing 8 changed files with 222 additions and 174 deletions.
1 change: 1 addition & 0 deletions cypress/fixtures/electionDistricts.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"uuid": "c9e2d352-e24c-4051-9158-f48127aa5692",
"properties" : [
"record_number",
"lon",
"lat",
"unit_type",
Expand Down
2 changes: 1 addition & 1 deletion cypress/integration/03_datastore.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ context('Datastore API', () => {
expect(response.status).eql(200);
expect(response.body.columns).eql(expected_columns);
expect(response.body.numOfRows).eql(2);
expect(response.body.numOfColumns).eql(5);
expect(response.body.numOfColumns).eql(6);
});

// Delete.
Expand Down
110 changes: 63 additions & 47 deletions modules/custom/dkan_common/src/Storage/AbstractDatabaseTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

namespace Drupal\dkan_common\Storage;

use Contracts\RemoverInterface;
use Contracts\RetrieverInterface;
use Dkan\Datastore\Storage\StorageInterface;
use Dkan\Datastore\Storage\Database\SqlStorageTrait;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Query\Select;
use Drupal\dkan_datastore\Storage\Query;

/**
* AbstractDatabaseTable class.
*/
abstract class AbstractDatabaseTable implements StorageInterface {
abstract class AbstractDatabaseTable implements StorageInterface, RetrieverInterface, RemoverInterface {
use SqlStorageTrait;
use QueryToQueryHelperTrait;

protected $connection;

Expand All @@ -30,7 +32,7 @@ abstract protected function getTableName();
* Transform the string data given into what should be use by the insert
* query.
*/
abstract protected function prepareData(string $data): array;
abstract protected function prepareData(string $data, string $id = NULL): array;

/**
* Get the primary key used in the table.
Expand All @@ -51,6 +53,23 @@ public function __construct(Connection $connection) {
}
}

/**
* Inherited.
*
* @inheritDoc
*/
public function retrieve(string $id) {
$this->setTable();

$result = $this->connection->select($this->getTableName(), 't')
->fields('t', array_keys($this->getSchema()['fields']))
->condition($this->primaryKey(), $id)
->execute()
->fetch();

return $result;
}

/**
* Inherited.
*
Expand Down Expand Up @@ -79,16 +98,52 @@ public function retrieveAll(): array {
*/
public function store($data, string $id = NULL): string {
$this->setTable();
$data = $this->prepareData($data);

$q = $this->connection->insert($this->getTableName());
$q->fields(array_keys($this->schema['fields']));
$q->values($data);
$id = $q->execute();
$existing = $this->retrieve($id);

$data = $this->prepareData($data, $id);

if (!$existing) {
$q = $this->connection->insert($this->getTableName());
$q->fields($this->getNonSerialFields());
$q->values($data);
$id = $q->execute();
}
else {
$q = $this->connection->update($this->getTableName());
$q->fields($data)
->condition($this->primaryKey(), $id)
->execute();
}

return "{$id}";
}

/**
* Private.
*/
private function getNonSerialFields() {
$fields = [];
foreach ($this->schema['fields'] as $field => $info) {
if ($info['type'] != 'serial') {
$fields[] = $field;
}
}
return $fields;
}

/**
* Inherited.
*
* @inheritDoc
*/
public function remove(string $id) {
$tableName = $this->getTableName();
$this->connection->delete($tableName)
->condition($this->primaryKey(), $id)
->execute();
}

/**
* Count rows in table.
*/
Expand Down Expand Up @@ -136,45 +191,6 @@ private function setTable() {
}
}

/**
* Private.
*/
private function setQueryConditions(Select $db_query, Query $query) {
foreach ($query->conditions as $property => $value) {
$db_query->condition($property, $value, "LIKE");
}
}

/**
* Private.
*/
private function setQueryOrderBy(Select $db_query, Query $query) {
foreach ($query->sort['ASC'] as $property) {
$db_query->orderBy($property);
}

foreach ($query->sort['DESC'] as $property) {
$db_query->orderBy($property, 'DESC');
}
}

/**
* Private.
*/
private function setQueryLimitAndOffset(Select $db_query, Query $query) {
if ($query->limit) {
if ($query->offset) {
$db_query->range($query->offset, $query->limit);
}
else {
$db_query->range(0, $query->limit);
}
}
elseif ($query->offset) {
$db_query->range($query->limit);
}
}

/**
* Destroy.
*
Expand Down
54 changes: 54 additions & 0 deletions modules/custom/dkan_common/src/Storage/QueryToQueryHelperTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Drupal\dkan_common\Storage;

use Drupal\Core\Database\Query\Select;
use Drupal\dkan_datastore\Storage\Query;

/**
* Class QueryToQueryHelperTrait.
*
* Given a Query object, setup a Drupal's select query.
*/
trait QueryToQueryHelperTrait {

/**
* Private.
*/
private function setQueryConditions(Select $db_query, Query $query) {
foreach ($query->conditions as $property => $value) {
$db_query->condition($property, $value, "LIKE");
}
}

/**
* Private.
*/
private function setQueryOrderBy(Select $db_query, Query $query) {
foreach ($query->sort['ASC'] as $property) {
$db_query->orderBy($property);
}

foreach ($query->sort['DESC'] as $property) {
$db_query->orderBy($property, 'DESC');
}
}

/**
* Private.
*/
private function setQueryLimitAndOffset(Select $db_query, Query $query) {
if ($query->limit) {
if ($query->offset) {
$db_query->range($query->offset, $query->limit);
}
else {
$db_query->range(0, $query->limit);
}
}
elseif ($query->offset) {
$db_query->range($query->limit);
}
}

}
22 changes: 21 additions & 1 deletion modules/custom/dkan_datastore/src/Storage/DatabaseTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ protected function getTableName() {
/**
* Protected.
*/
protected function prepareData(string $data): array {
protected function prepareData(string $data, string $id = NULL): array {
return json_decode($data);
}

Expand All @@ -91,4 +91,24 @@ protected function primaryKey() {
return "record_number";
}

/**
* Overriden.
*/
public function setSchema($schema) {
$fields = $schema['fields'];
$new_field = [
$this->primaryKey() =>
[
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
],
];
$fields = array_merge($new_field, $fields);

$schema['fields'] = $fields;
$schema['primary key'] = [$this->primaryKey()];
parent::setSchema($schema);
}

}
Loading

0 comments on commit ab415d3

Please sign in to comment.