Skip to content

Commit

Permalink
Fixing issue with datastore_mysql_import existing tables (#3972)
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-m committed May 18, 2023
1 parent bb8fadb commit 4bef8e5
Show file tree
Hide file tree
Showing 5 changed files with 616 additions and 2 deletions.
2 changes: 1 addition & 1 deletion modules/common/src/Storage/AbstractDatabaseTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public function query(Query $query, string $alias = 't', $fetch = TRUE) {
/**
* Create a minimal error message that does not leak database information.
*/
private function sanitizedErrorMessage(string $unsanitizedMessage) {
protected function sanitizedErrorMessage(string $unsanitizedMessage) {
// Insert portions of exception messages you want caught here.
$messages = [
// Portion of the message => User friendly message.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Drupal\datastore_mysql_import\Storage;

use Drupal\common\Storage\Query;
use Drupal\common\Storage\SelectFactory;
use Drupal\Core\Database\DatabaseExceptionWrapper;
use Drupal\datastore\Storage\DatabaseTable;

/**
Expand All @@ -23,8 +26,40 @@ protected function setTable() {
$this->tableCreate($this->getTableName(), $this->schema);
}
else {
throw new \Exception("Could not instantiate the table due to a lack of schema.");
throw new \Exception('Could not instantiate the table due to a lack of schema.');
}
}

/**
* 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) {
if (!$this->tableExist($this->getTableName())) {
throw new \Exception('Could not instantiate the table due to a lack of schema.');
}
$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;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"dataset": [
{
"accessLevel": "public",
"description": "Description #1",
"identifier": "1",
"modified": "2019-12-31",
"title": "One",
"keyword": ["keyword"]
},
{
"accessLevel": "public",
"description": "Description #2",
"identifier": "2",
"modified": "2019-12-31",
"title": "Two",
"keyword": ["keyword"]
},
{
"accessLevel": "public",
"description": "Description #3",
"identifier": "3",
"modified": "2019-12-31",
"title": "Three",
"keyword": ["keyword"]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"dataset": [
{
"accessLevel": "public",
"description": "Description #1",
"identifier": "1",
"modified": "2019-12-31",
"title": "One",
"keyword": ["keyword"]
},
{
"accessLevel": "public",
"description": "Description #2",
"identifier": "2",
"modified": "2019-12-31",
"title": "Two updated",
"keyword": ["keyword"]
},
{
"accessLevel": "public",
"description": "Description #4",
"identifier": "4",
"modified": "2019-12-31",
"title": "Four",
"keyword": ["keyword"]
}
]
}
Loading

0 comments on commit 4bef8e5

Please sign in to comment.