Open Data Catalog v2.0.0
DatabaseTable.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use Drupal\Core\Database\Connection;
6 use Dkan\Datastore\Resource;
9 
15 class DatabaseTable extends AbstractDatabaseTable implements \JsonSerializable {
16 
17  use LoggerTrait;
18 
24  private $resource;
25 
34  public function __construct(Connection $connection, Resource $resource) {
35  // Set resource before calling the parent constructor. The parent calls
36  // getTableName which we implement and needs the resource to operate.
37  $this->resource = $resource;
38  parent::__construct($connection);
39  }
40 
44  public function getSummary() {
45  $columns = $this->getSchema()['fields'];
46  $numOfColumns = count($columns);
47  $numOfRows = $this->count();
48  return new TableSummary($numOfColumns, $columns, $numOfRows);
49  }
50 
56  public function jsonSerialize() {
57  return (object) ['resource' => $this->resource];
58  }
59 
63  public static function hydrate(string $json) {
64  $data = json_decode($json);
65  $resource = Resource::hydrate(json_encode($data->resource));
66 
67  return new DatabaseTable(\Drupal::service('database'), $resource);
68  }
69 
76  public function getTableName() {
77  if ($this->resource) {
78  return "datastore_{$this->resource->getId()}";
79  }
80  return "datastore_does_not_exist";
81  }
82 
86  protected function prepareData(string $data, string $id = NULL): array {
87  $decoded = json_decode($data);
88  if ($decoded === NULL) {
89  $this->log(
90  'datastore_import',
91  "Error decoding id:@id, data: @data.",
92  ['@id' => $id, '@data' => $data]
93  );
94  throw new \Exception("Import for {$id} error when decoding {$data}");
95  }
96  elseif (!is_array($decoded)) {
97  $this->log(
98  'datastore_import',
99  "Array expected while decoding id:@id, data: @data.",
100  ['@id' => $id, '@data' => $data]
101  );
102  throw new \Exception("Import for {$id} returned an error when preparing table header: {$data}");
103  }
104  return $decoded;
105  }
106 
110  protected function primaryKey() {
111  return "record_number";
112  }
113 
117  protected function getNonSerialFields() {
118  $fields = parent::getNonSerialFields();
119  $index = array_search($this->primaryKey(), $fields);
120  if ($index !== FALSE) {
121  unset($fields[$index]);
122  }
123  return $fields;
124  }
125 
129  public function setSchema($schema) {
130  $fields = $schema['fields'];
131  $new_field = [
132  $this->primaryKey() =>
133  [
134  'type' => 'serial',
135  'unsigned' => TRUE,
136  'not null' => TRUE,
137  ],
138  ];
139  $fields = array_merge($new_field, $fields);
140 
141  $schema['fields'] = $fields;
142  $schema['primary key'] = [$this->primaryKey()];
143  parent::setSchema($schema);
144  }
145 
146 }
Drupal\common\LoggerTrait
trait LoggerTrait
Definition: LoggerTrait.php:11
Drupal\datastore\Storage\DatabaseTable\primaryKey
primaryKey()
Definition: DatabaseTable.php:110
Drupal\datastore\Storage\DatabaseTable\getTableName
getTableName()
Definition: DatabaseTable.php:76
Drupal\datastore\Storage\DatabaseTable\setSchema
setSchema($schema)
Definition: DatabaseTable.php:129
Drupal\datastore\Storage\DatabaseTable\prepareData
prepareData(string $data, string $id=NULL)
Definition: DatabaseTable.php:86
Drupal\datastore\Storage\DatabaseTable\jsonSerialize
jsonSerialize()
Definition: DatabaseTable.php:56
Drupal\datastore\Storage\TableSummary
Definition: TableSummary.php:10
Drupal\datastore\Storage\DatabaseTable\hydrate
static hydrate(string $json)
Definition: DatabaseTable.php:63
Drupal\datastore\Storage\DatabaseTable\__construct
__construct(Connection $connection, Resource $resource)
Definition: DatabaseTable.php:34
Drupal\datastore\Storage\DatabaseTable\getSummary
getSummary()
Definition: DatabaseTable.php:44
Drupal\datastore\Storage\DatabaseTable\getNonSerialFields
getNonSerialFields()
Definition: DatabaseTable.php:117
Drupal\common\Storage\AbstractDatabaseTable
Definition: AbstractDatabaseTable.php:12
Drupal\datastore\Storage
Definition: DatabaseTable.php:3
Drupal\datastore\Storage\DatabaseTable
Definition: DatabaseTable.php:15
Drupal\common\Storage\AbstractDatabaseTable\$connection
$connection
Definition: AbstractDatabaseTable.php:20
Drupal\common\Storage\AbstractDatabaseTable\count
count()
Definition: AbstractDatabaseTable.php:196