Skip to content

Commit b09a135

Browse files
authored
Merge pull request #596 from utopia-php/abstract-execute-method
Abstract execution method
2 parents 82fd1ae + 83ee3b0 commit b09a135

5 files changed

Lines changed: 176 additions & 309 deletions

File tree

src/Database/Adapter.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,4 +1186,10 @@ abstract public function getSchemaAttributes(string $collection): array;
11861186
* @return string
11871187
*/
11881188
abstract public function getTenantQuery(string $collection, string $alias = ''): string;
1189+
1190+
/**
1191+
* @param mixed $stmt
1192+
* @return bool
1193+
*/
1194+
abstract protected function execute(mixed $stmt): bool;
11891195
}

src/Database/Adapter/MariaDB.php

Lines changed: 0 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -921,160 +921,6 @@ public function createDocument(string $collection, Document $document): Document
921921
return $document;
922922
}
923923

924-
/**
925-
* Create Documents in batches
926-
*
927-
* @param string $collection
928-
* @param array<Document> $documents
929-
*
930-
* @return array<Document>
931-
*
932-
* @throws DuplicateException
933-
* @throws \Throwable
934-
*/
935-
public function createDocuments(string $collection, array $documents): array
936-
{
937-
if (empty($documents)) {
938-
return $documents;
939-
}
940-
941-
try {
942-
$name = $this->filter($collection);
943-
944-
$attributeKeys = Database::INTERNAL_ATTRIBUTE_KEYS;
945-
946-
$hasSequence = null;
947-
foreach ($documents as $document) {
948-
$attributes = $document->getAttributes();
949-
$attributeKeys = [...$attributeKeys, ...\array_keys($attributes)];
950-
951-
if ($hasSequence === null) {
952-
$hasSequence = !empty($document->getSequence());
953-
} elseif ($hasSequence == empty($document->getSequence())) {
954-
throw new DatabaseException('All documents must have an sequence if one is set');
955-
}
956-
}
957-
958-
$attributeKeys = array_unique($attributeKeys);
959-
960-
if ($hasSequence) {
961-
$attributeKeys[] = '_id';
962-
}
963-
964-
if ($this->sharedTables) {
965-
$attributeKeys[] = '_tenant';
966-
}
967-
968-
$columns = [];
969-
foreach ($attributeKeys as $key => $attribute) {
970-
$columns[$key] = $this->quote($this->filter($attribute));
971-
}
972-
973-
$columns = '(' . \implode(', ', $columns) . ')';
974-
975-
$bindIndex = 0;
976-
$batchKeys = [];
977-
$bindValues = [];
978-
$permissions = [];
979-
$documentIds = [];
980-
$documentTenants = [];
981-
982-
foreach ($documents as $index => $document) {
983-
$attributes = $document->getAttributes();
984-
$attributes['_uid'] = $document->getId();
985-
$attributes['_createdAt'] = $document->getCreatedAt();
986-
$attributes['_updatedAt'] = $document->getUpdatedAt();
987-
$attributes['_permissions'] = \json_encode($document->getPermissions());
988-
989-
if (!empty($document->getSequence())) {
990-
$attributes['_id'] = $document->getSequence();
991-
} else {
992-
$documentIds[] = $document->getId();
993-
}
994-
995-
if ($this->sharedTables) {
996-
$attributes['_tenant'] = $document->getTenant();
997-
$documentTenants[] = $document->getTenant();
998-
}
999-
1000-
$bindKeys = [];
1001-
1002-
foreach ($attributeKeys as $key) {
1003-
$value = $attributes[$key] ?? null;
1004-
if (\is_array($value)) {
1005-
$value = \json_encode($value);
1006-
}
1007-
$value = (\is_bool($value)) ? (int)$value : $value;
1008-
$bindKey = 'key_' . $bindIndex;
1009-
$bindKeys[] = ':' . $bindKey;
1010-
$bindValues[$bindKey] = $value;
1011-
$bindIndex++;
1012-
}
1013-
1014-
$batchKeys[] = '(' . \implode(', ', $bindKeys) . ')';
1015-
1016-
foreach (Database::PERMISSIONS as $type) {
1017-
foreach ($document->getPermissionsByType($type) as $permission) {
1018-
$tenantBind = $this->sharedTables ? ", :_tenant_{$index}" : '';
1019-
$permission = \str_replace('"', '', $permission);
1020-
$permission = "('{$type}', '{$permission}', :_uid_{$index} {$tenantBind})";
1021-
$permissions[] = $permission;
1022-
}
1023-
}
1024-
}
1025-
1026-
$batchKeys = \implode(', ', $batchKeys);
1027-
1028-
$stmt = $this->getPDO()->prepare("
1029-
INSERT INTO {$this->getSQLTable($name)} {$columns}
1030-
VALUES {$batchKeys}
1031-
");
1032-
1033-
foreach ($bindValues as $key => $value) {
1034-
$stmt->bindValue($key, $value, $this->getPDOType($value));
1035-
}
1036-
1037-
$stmt->execute();
1038-
1039-
if (!empty($permissions)) {
1040-
$tenantColumn = $this->sharedTables ? ', _tenant' : '';
1041-
$permissions = \implode(', ', $permissions);
1042-
1043-
$sqlPermissions = "
1044-
INSERT INTO {$this->getSQLTable($name . '_perms')} (_type, _permission, _document {$tenantColumn})
1045-
VALUES {$permissions};
1046-
";
1047-
1048-
$stmtPermissions = $this->getPDO()->prepare($sqlPermissions);
1049-
1050-
foreach ($documents as $index => $document) {
1051-
$stmtPermissions->bindValue(":_uid_{$index}", $document->getId());
1052-
if ($this->sharedTables) {
1053-
$stmtPermissions->bindValue(":_tenant_{$index}", $document->getTenant());
1054-
}
1055-
}
1056-
1057-
$stmtPermissions?->execute();
1058-
}
1059-
1060-
$sequences = $this->getSequences(
1061-
$collection,
1062-
$documentIds,
1063-
$documentTenants
1064-
);
1065-
1066-
foreach ($documents as $document) {
1067-
if (isset($sequences[$document->getId()])) {
1068-
$document['$sequence'] = $sequences[$document->getId()];
1069-
}
1070-
}
1071-
} catch (PDOException $e) {
1072-
throw $this->processException($e);
1073-
}
1074-
1075-
return $documents;
1076-
}
1077-
1078924
/**
1079925
* Update Document
1080926
*

src/Database/Adapter/Pool.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,4 +484,9 @@ public function getTenantQuery(string $collection, string $alias = ''): string
484484
{
485485
return $this->delegate(__FUNCTION__, \func_get_args());
486486
}
487+
488+
protected function execute(mixed $stmt): bool
489+
{
490+
return $this->delegate(__FUNCTION__, \func_get_args());
491+
}
487492
}

src/Database/Adapter/Postgres.php

Lines changed: 1 addition & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function rollbackTransaction(): bool
8484
return $result;
8585
}
8686

87-
private function execute(mixed $stmt): bool
87+
protected function execute(mixed $stmt): bool
8888
{
8989
$pdo = $this->getPDO();
9090

@@ -1042,160 +1042,6 @@ public function createDocument(string $collection, Document $document): Document
10421042
return $document;
10431043
}
10441044

1045-
/**
1046-
* Create Documents in batches
1047-
*
1048-
* @param string $collection
1049-
* @param array<Document> $documents
1050-
*
1051-
* @return array<Document>
1052-
*
1053-
* @throws DuplicateException
1054-
* @throws \Throwable
1055-
*/
1056-
public function createDocuments(string $collection, array $documents): array
1057-
{
1058-
if (empty($documents)) {
1059-
return $documents;
1060-
}
1061-
1062-
try {
1063-
$name = $this->filter($collection);
1064-
1065-
$attributeKeys = Database::INTERNAL_ATTRIBUTE_KEYS;
1066-
1067-
$hasSequence = null;
1068-
foreach ($documents as $document) {
1069-
$attributes = $document->getAttributes();
1070-
$attributeKeys = [...$attributeKeys, ...\array_keys($attributes)];
1071-
1072-
if ($hasSequence === null) {
1073-
$hasSequence = !empty($document->getSequence());
1074-
} elseif ($hasSequence == empty($document->getSequence())) {
1075-
throw new DatabaseException('All documents must have an sequence if one is set');
1076-
}
1077-
}
1078-
1079-
$attributeKeys = array_unique($attributeKeys);
1080-
1081-
if ($hasSequence) {
1082-
$attributeKeys[] = '_id';
1083-
}
1084-
1085-
if ($this->sharedTables) {
1086-
$attributeKeys[] = '_tenant';
1087-
}
1088-
1089-
$columns = [];
1090-
foreach ($attributeKeys as $key => $attribute) {
1091-
$columns[$key] = $this->quote($this->filter($attribute));
1092-
}
1093-
1094-
$columns = '(' . \implode(', ', $columns) . ')';
1095-
1096-
$bindIndex = 0;
1097-
$batchKeys = [];
1098-
$bindValues = [];
1099-
$permissions = [];
1100-
$documentIds = [];
1101-
$documentTenants = [];
1102-
1103-
foreach ($documents as $index => $document) {
1104-
$attributes = $document->getAttributes();
1105-
$attributes['_uid'] = $document->getId();
1106-
$attributes['_createdAt'] = $document->getCreatedAt();
1107-
$attributes['_updatedAt'] = $document->getUpdatedAt();
1108-
$attributes['_permissions'] = \json_encode($document->getPermissions());
1109-
1110-
if (!empty($document->getSequence())) {
1111-
$attributes['_id'] = $document->getSequence();
1112-
} else {
1113-
$documentIds[] = $document->getId();
1114-
}
1115-
1116-
if ($this->sharedTables) {
1117-
$attributes['_tenant'] = $document->getTenant();
1118-
$documentTenants[] = $document->getTenant();
1119-
}
1120-
1121-
$bindKeys = [];
1122-
1123-
foreach ($attributeKeys as $key) {
1124-
$value = $attributes[$key] ?? null;
1125-
if (\is_array($value)) {
1126-
$value = \json_encode($value);
1127-
}
1128-
$value = (\is_bool($value)) ? (int)$value : $value;
1129-
$bindKey = 'key_' . $bindIndex;
1130-
$bindKeys[] = ':' . $bindKey;
1131-
$bindValues[$bindKey] = $value;
1132-
$bindIndex++;
1133-
}
1134-
1135-
$batchKeys[] = '(' . \implode(', ', $bindKeys) . ')';
1136-
1137-
foreach (Database::PERMISSIONS as $type) {
1138-
foreach ($document->getPermissionsByType($type) as $permission) {
1139-
$tenantBind = $this->sharedTables ? ", :_tenant_{$index}" : '';
1140-
$permission = \str_replace('"', '', $permission);
1141-
$permission = "('{$type}', '{$permission}', :_uid_{$index} {$tenantBind})";
1142-
$permissions[] = $permission;
1143-
}
1144-
}
1145-
}
1146-
1147-
$batchKeys = \implode(', ', $batchKeys);
1148-
1149-
$stmt = $this->getPDO()->prepare("
1150-
INSERT INTO {$this->getSQLTable($name)} {$columns}
1151-
VALUES {$batchKeys}
1152-
");
1153-
1154-
foreach ($bindValues as $key => $value) {
1155-
$stmt->bindValue($key, $value, $this->getPDOType($value));
1156-
}
1157-
1158-
$this->execute($stmt);
1159-
1160-
if (!empty($permissions)) {
1161-
$tenantColumn = $this->sharedTables ? ', _tenant' : '';
1162-
$permissions = \implode(', ', $permissions);
1163-
1164-
$sqlPermissions = "
1165-
INSERT INTO {$this->getSQLTable($name . '_perms')} (_type, _permission, _document {$tenantColumn})
1166-
VALUES {$permissions};
1167-
";
1168-
1169-
$stmtPermissions = $this->getPDO()->prepare($sqlPermissions);
1170-
1171-
foreach ($documents as $index => $document) {
1172-
$stmtPermissions->bindValue(":_uid_{$index}", $document->getId());
1173-
if ($this->sharedTables) {
1174-
$stmtPermissions->bindValue(":_tenant_{$index}", $document->getTenant());
1175-
}
1176-
}
1177-
1178-
$this->execute($stmtPermissions);
1179-
}
1180-
1181-
$sequences = $this->getSequences(
1182-
$collection,
1183-
$documentIds,
1184-
$documentTenants
1185-
);
1186-
1187-
foreach ($documents as $document) {
1188-
if (isset($sequences[$document->getId()])) {
1189-
$document['$sequence'] = $sequences[$document->getId()];
1190-
}
1191-
}
1192-
} catch (PDOException $e) {
1193-
throw $this->processException($e);
1194-
}
1195-
1196-
return $documents;
1197-
}
1198-
11991045
/**
12001046
* Update Document
12011047
*

0 commit comments

Comments
 (0)