Skip to content
Merged
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
6 changes: 6 additions & 0 deletions src/Database/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -1186,4 +1186,10 @@ abstract public function getSchemaAttributes(string $collection): array;
* @return string
*/
abstract public function getTenantQuery(string $collection, string $alias = ''): string;

/**
* @param mixed $stmt
* @return bool
*/
abstract protected function execute(mixed $stmt): bool;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the name be executeStatement?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fine, make enough sense with the $stmt param

}
154 changes: 0 additions & 154 deletions src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -921,160 +921,6 @@ public function createDocument(string $collection, Document $document): Document
return $document;
}

/**
* Create Documents in batches
*
* @param string $collection
* @param array<Document> $documents
*
* @return array<Document>
*
* @throws DuplicateException
* @throws \Throwable
*/
public function createDocuments(string $collection, array $documents): array
{
if (empty($documents)) {
return $documents;
}

try {
$name = $this->filter($collection);

$attributeKeys = Database::INTERNAL_ATTRIBUTE_KEYS;

$hasSequence = null;
foreach ($documents as $document) {
$attributes = $document->getAttributes();
$attributeKeys = [...$attributeKeys, ...\array_keys($attributes)];

if ($hasSequence === null) {
$hasSequence = !empty($document->getSequence());
} elseif ($hasSequence == empty($document->getSequence())) {
throw new DatabaseException('All documents must have an sequence if one is set');
}
}

$attributeKeys = array_unique($attributeKeys);

if ($hasSequence) {
$attributeKeys[] = '_id';
}

if ($this->sharedTables) {
$attributeKeys[] = '_tenant';
}

$columns = [];
foreach ($attributeKeys as $key => $attribute) {
$columns[$key] = $this->quote($this->filter($attribute));
}

$columns = '(' . \implode(', ', $columns) . ')';

$bindIndex = 0;
$batchKeys = [];
$bindValues = [];
$permissions = [];
$documentIds = [];
$documentTenants = [];

foreach ($documents as $index => $document) {
$attributes = $document->getAttributes();
$attributes['_uid'] = $document->getId();
$attributes['_createdAt'] = $document->getCreatedAt();
$attributes['_updatedAt'] = $document->getUpdatedAt();
$attributes['_permissions'] = \json_encode($document->getPermissions());

if (!empty($document->getSequence())) {
$attributes['_id'] = $document->getSequence();
} else {
$documentIds[] = $document->getId();
}

if ($this->sharedTables) {
$attributes['_tenant'] = $document->getTenant();
$documentTenants[] = $document->getTenant();
}

$bindKeys = [];

foreach ($attributeKeys as $key) {
$value = $attributes[$key] ?? null;
if (\is_array($value)) {
$value = \json_encode($value);
}
$value = (\is_bool($value)) ? (int)$value : $value;
$bindKey = 'key_' . $bindIndex;
$bindKeys[] = ':' . $bindKey;
$bindValues[$bindKey] = $value;
$bindIndex++;
}

$batchKeys[] = '(' . \implode(', ', $bindKeys) . ')';

foreach (Database::PERMISSIONS as $type) {
foreach ($document->getPermissionsByType($type) as $permission) {
$tenantBind = $this->sharedTables ? ", :_tenant_{$index}" : '';
$permission = \str_replace('"', '', $permission);
$permission = "('{$type}', '{$permission}', :_uid_{$index} {$tenantBind})";
$permissions[] = $permission;
}
}
}

$batchKeys = \implode(', ', $batchKeys);

$stmt = $this->getPDO()->prepare("
INSERT INTO {$this->getSQLTable($name)} {$columns}
VALUES {$batchKeys}
");

foreach ($bindValues as $key => $value) {
$stmt->bindValue($key, $value, $this->getPDOType($value));
}

$stmt->execute();

if (!empty($permissions)) {
$tenantColumn = $this->sharedTables ? ', _tenant' : '';
$permissions = \implode(', ', $permissions);

$sqlPermissions = "
INSERT INTO {$this->getSQLTable($name . '_perms')} (_type, _permission, _document {$tenantColumn})
VALUES {$permissions};
";

$stmtPermissions = $this->getPDO()->prepare($sqlPermissions);

foreach ($documents as $index => $document) {
$stmtPermissions->bindValue(":_uid_{$index}", $document->getId());
if ($this->sharedTables) {
$stmtPermissions->bindValue(":_tenant_{$index}", $document->getTenant());
}
}

$stmtPermissions?->execute();
}

$sequences = $this->getSequences(
$collection,
$documentIds,
$documentTenants
);

foreach ($documents as $document) {
if (isset($sequences[$document->getId()])) {
$document['$sequence'] = $sequences[$document->getId()];
}
}
} catch (PDOException $e) {
throw $this->processException($e);
}

return $documents;
}

/**
* Update Document
*
Expand Down
5 changes: 5 additions & 0 deletions src/Database/Adapter/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -484,4 +484,9 @@ public function getTenantQuery(string $collection, string $alias = ''): string
{
return $this->delegate(__FUNCTION__, \func_get_args());
}

protected function execute(mixed $stmt): bool
{
return $this->delegate(__FUNCTION__, \func_get_args());
}
}
156 changes: 1 addition & 155 deletions src/Database/Adapter/Postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function rollbackTransaction(): bool
return $result;
}

private function execute(mixed $stmt): bool
protected function execute(mixed $stmt): bool
{
$pdo = $this->getPDO();

Expand Down Expand Up @@ -1042,160 +1042,6 @@ public function createDocument(string $collection, Document $document): Document
return $document;
}

/**
* Create Documents in batches
*
* @param string $collection
* @param array<Document> $documents
*
* @return array<Document>
*
* @throws DuplicateException
* @throws \Throwable
*/
public function createDocuments(string $collection, array $documents): array
{
if (empty($documents)) {
return $documents;
}

try {
$name = $this->filter($collection);

$attributeKeys = Database::INTERNAL_ATTRIBUTE_KEYS;

$hasSequence = null;
foreach ($documents as $document) {
$attributes = $document->getAttributes();
$attributeKeys = [...$attributeKeys, ...\array_keys($attributes)];

if ($hasSequence === null) {
$hasSequence = !empty($document->getSequence());
} elseif ($hasSequence == empty($document->getSequence())) {
throw new DatabaseException('All documents must have an sequence if one is set');
}
}

$attributeKeys = array_unique($attributeKeys);

if ($hasSequence) {
$attributeKeys[] = '_id';
}

if ($this->sharedTables) {
$attributeKeys[] = '_tenant';
}

$columns = [];
foreach ($attributeKeys as $key => $attribute) {
$columns[$key] = $this->quote($this->filter($attribute));
}

$columns = '(' . \implode(', ', $columns) . ')';

$bindIndex = 0;
$batchKeys = [];
$bindValues = [];
$permissions = [];
$documentIds = [];
$documentTenants = [];

foreach ($documents as $index => $document) {
$attributes = $document->getAttributes();
$attributes['_uid'] = $document->getId();
$attributes['_createdAt'] = $document->getCreatedAt();
$attributes['_updatedAt'] = $document->getUpdatedAt();
$attributes['_permissions'] = \json_encode($document->getPermissions());

if (!empty($document->getSequence())) {
$attributes['_id'] = $document->getSequence();
} else {
$documentIds[] = $document->getId();
}

if ($this->sharedTables) {
$attributes['_tenant'] = $document->getTenant();
$documentTenants[] = $document->getTenant();
}

$bindKeys = [];

foreach ($attributeKeys as $key) {
$value = $attributes[$key] ?? null;
if (\is_array($value)) {
$value = \json_encode($value);
}
$value = (\is_bool($value)) ? (int)$value : $value;
$bindKey = 'key_' . $bindIndex;
$bindKeys[] = ':' . $bindKey;
$bindValues[$bindKey] = $value;
$bindIndex++;
}

$batchKeys[] = '(' . \implode(', ', $bindKeys) . ')';

foreach (Database::PERMISSIONS as $type) {
foreach ($document->getPermissionsByType($type) as $permission) {
$tenantBind = $this->sharedTables ? ", :_tenant_{$index}" : '';
$permission = \str_replace('"', '', $permission);
$permission = "('{$type}', '{$permission}', :_uid_{$index} {$tenantBind})";
$permissions[] = $permission;
}
}
}

$batchKeys = \implode(', ', $batchKeys);

$stmt = $this->getPDO()->prepare("
INSERT INTO {$this->getSQLTable($name)} {$columns}
VALUES {$batchKeys}
");

foreach ($bindValues as $key => $value) {
$stmt->bindValue($key, $value, $this->getPDOType($value));
}

$this->execute($stmt);

if (!empty($permissions)) {
$tenantColumn = $this->sharedTables ? ', _tenant' : '';
$permissions = \implode(', ', $permissions);

$sqlPermissions = "
INSERT INTO {$this->getSQLTable($name . '_perms')} (_type, _permission, _document {$tenantColumn})
VALUES {$permissions};
";

$stmtPermissions = $this->getPDO()->prepare($sqlPermissions);

foreach ($documents as $index => $document) {
$stmtPermissions->bindValue(":_uid_{$index}", $document->getId());
if ($this->sharedTables) {
$stmtPermissions->bindValue(":_tenant_{$index}", $document->getTenant());
}
}

$this->execute($stmtPermissions);
}

$sequences = $this->getSequences(
$collection,
$documentIds,
$documentTenants
);

foreach ($documents as $document) {
if (isset($sequences[$document->getId()])) {
$document['$sequence'] = $sequences[$document->getId()];
}
}
} catch (PDOException $e) {
throw $this->processException($e);
}

return $documents;
}

/**
* Update Document
*
Expand Down
Loading