Skip to content

Commit bf4219b

Browse files
committed
Merge branch 'main' of github.com:utopia-php/database into joins2
# Conflicts: # composer.lock # src/Database/Database.php # tests/e2e/Adapter/Base.php
2 parents 31c25ab + 72b2e2c commit bf4219b

22 files changed

Lines changed: 18742 additions & 18674 deletions

composer.lock

Lines changed: 13 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Database/Adapter.php

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
abstract class Adapter
1212
{
1313
protected string $database = '';
14+
protected string $hostname = '';
1415

1516
protected string $namespace = '';
1617

@@ -79,15 +80,15 @@ public function resetDebug(): static
7980
*
8081
* @param string $namespace
8182
*
82-
* @return bool
83+
* @return $this
8384
* @throws DatabaseException
8485
*
8586
*/
86-
public function setNamespace(string $namespace): bool
87+
public function setNamespace(string $namespace): static
8788
{
8889
$this->namespace = $this->filter($namespace);
8990

90-
return true;
91+
return $this;
9192
}
9293

9394
/**
@@ -103,6 +104,29 @@ public function getNamespace(): string
103104
return $this->namespace;
104105
}
105106

107+
/**
108+
* Set Hostname.
109+
*
110+
* @param string $hostname
111+
* @return $this
112+
*/
113+
public function setHostname(string $hostname): static
114+
{
115+
$this->hostname = $hostname;
116+
117+
return $this;
118+
}
119+
120+
/**
121+
* Get Hostname.
122+
*
123+
* @return string
124+
*/
125+
public function getHostname(): string
126+
{
127+
return $this->hostname;
128+
}
129+
106130
/**
107131
* Set Database.
108132
*
@@ -982,8 +1006,20 @@ abstract public function getSupportForUpserts(): bool;
9821006
*/
9831007
abstract public function getSupportForCacheSkipOnFailure(): bool;
9841008

1009+
/**
1010+
* Is reconnection supported?
1011+
*
1012+
* @return bool
1013+
*/
9851014
abstract public function getSupportForReconnection(): bool;
9861015

1016+
/**
1017+
* Is hostname supported?
1018+
*
1019+
* @return bool
1020+
*/
1021+
abstract public function getSupportForHostname(): bool;
1022+
9871023
/**
9881024
* Get current attribute count from collection document
9891025
*

src/Database/Adapter/MariaDB.php

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -332,9 +332,13 @@ public function deleteCollection(string $id): bool
332332

333333
$sql = $this->trigger(Database::EVENT_COLLECTION_DELETE, $sql);
334334

335-
return $this->getPDO()
336-
->prepare($sql)
337-
->execute();
335+
try {
336+
return $this->getPDO()
337+
->prepare($sql)
338+
->execute();
339+
} catch (PDOException $e) {
340+
throw $this->processException($e);
341+
}
338342
}
339343

340344
/**
@@ -763,26 +767,31 @@ public function createIndex(string $collection, string $id, string $type, array
763767
throw new NotFoundException('Collection not found');
764768
}
765769

770+
/**
771+
* We do not have internalId's added to list, since we check only for array field
772+
*/
766773
$collectionAttributes = \json_decode($collection->getAttribute('attributes', []), true);
767774

768775
$id = $this->filter($id);
769776

770777
foreach ($attributes as $i => $attr) {
771-
$collectionAttribute = \array_filter($collectionAttributes, fn ($collectionAttribute) => array_key_exists('key', $collectionAttribute) && $collectionAttribute['key'] === $attr);
772-
$collectionAttribute = end($collectionAttribute);
778+
$attribute = null;
779+
foreach ($collectionAttributes as $collectionAttribute) {
780+
if (\strtolower($collectionAttribute['$id']) === \strtolower($attr)) {
781+
$attribute = $collectionAttribute;
782+
break;
783+
}
784+
}
785+
773786
$order = empty($orders[$i]) || Database::INDEX_FULLTEXT === $type ? '' : $orders[$i];
774787
$length = empty($lengths[$i]) ? '' : '(' . (int)$lengths[$i] . ')';
775788

776-
$attr = match ($attr) {
777-
'$id' => '_uid',
778-
'$createdAt' => '_createdAt',
779-
'$updatedAt' => '_updatedAt',
780-
default => $this->filter($attr),
781-
};
789+
$attr = $this->getInternalKeyForAttribute($attr);
790+
$attr = $this->filter($attr);
782791

783792
$attributes[$i] = "`{$attr}`{$length} {$order}";
784793

785-
if (!empty($collectionAttribute['array']) && $this->getSupportForCastIndexArray()) {
794+
if ($this->getSupportForCastIndexArray() && !empty($attribute['array'])) {
786795
$attributes[$i] = '(CAST(`' . $attr . '` AS char(' . Database::ARRAY_INDEX_LENGTH . ') ARRAY))';
787796
}
788797
}
@@ -1130,10 +1139,6 @@ public function updateDocument(string $collection, string $id, Document $documen
11301139
$attributes['_updatedAt'] = $document->getUpdatedAt();
11311140
$attributes['_permissions'] = json_encode($document->getPermissions());
11321141

1133-
if ($this->sharedTables) {
1134-
$attributes['_tenant'] = $this->tenant;
1135-
}
1136-
11371142
$name = $this->filter($collection);
11381143
$columns = '';
11391144

@@ -1298,15 +1303,15 @@ public function updateDocument(string $collection, string $id, Document $documen
12981303
$sql = "
12991304
UPDATE {$this->getSQLTable($name)}
13001305
SET {$columns} _uid = :_newUid
1301-
WHERE _uid = :_existingUid
1306+
WHERE _id=:_internalId
13021307
{$this->getTenantQuery($collection)}
13031308
";
13041309

13051310
$sql = $this->trigger(Database::EVENT_DOCUMENT_UPDATE, $sql);
13061311

13071312
$stmt = $this->getPDO()->prepare($sql);
13081313

1309-
$stmt->bindValue(':_existingUid', $id);
1314+
$stmt->bindValue(':_internalId', $document->getInternalId());
13101315
$stmt->bindValue(':_newUid', $document->getId());
13111316

13121317
if ($this->sharedTables) {
@@ -2332,6 +2337,13 @@ protected function processException(PDOException $e): \Exception
23322337
return new NotFoundException('Collection not found', $e->getCode(), $e);
23332338
}
23342339

2340+
// Unknown collection
2341+
// We have two of same, because docs point to 1051.
2342+
// Keeping previous 1049 (above) just in case it's for older versions
2343+
if ($e->getCode() === '42S02' && isset($e->errorInfo[1]) && $e->errorInfo[1] === 1051) {
2344+
return new NotFoundException('Collection not found', $e->getCode(), $e);
2345+
}
2346+
23352347
return $e;
23362348
}
23372349

src/Database/Adapter/Pool.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,11 @@ public function getSupportForReconnection(): bool
417417
return $this->delegate(__FUNCTION__, \func_get_args());
418418
}
419419

420+
public function getSupportForHostname(): bool
421+
{
422+
return $this->delegate(__FUNCTION__, \func_get_args());
423+
}
424+
420425
public function getCountOfAttributes(Document $collection): int
421426
{
422427
return $this->delegate(__FUNCTION__, \func_get_args());

src/Database/Adapter/Postgres.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,10 +1190,6 @@ public function updateDocument(string $collection, string $id, Document $documen
11901190
$attributes['_updatedAt'] = $document->getUpdatedAt();
11911191
$attributes['_permissions'] = json_encode($document->getPermissions());
11921192

1193-
if ($this->sharedTables) {
1194-
$attributes['_tenant'] = $this->tenant;
1195-
}
1196-
11971193
$name = $this->filter($collection);
11981194
$columns = '';
11991195

@@ -1344,15 +1340,15 @@ public function updateDocument(string $collection, string $id, Document $documen
13441340
$sql = "
13451341
UPDATE {$this->getSQLTable($name)}
13461342
SET {$columns} _uid = :_newUid
1347-
WHERE _uid = :_existingUid
1343+
WHERE _id=:_internalId
13481344
{$this->getTenantQuery($collection)}
13491345
";
13501346

13511347
$sql = $this->trigger(Database::EVENT_DOCUMENT_UPDATE, $sql);
13521348

13531349
$stmt = $this->getPDO()->prepare($sql);
13541350

1355-
$stmt->bindValue(':_existingUid', $id);
1351+
$stmt->bindValue(':_internalId', $document->getInternalId());
13561352
$stmt->bindValue(':_newUid', $document->getId());
13571353

13581354
if ($this->sharedTables) {

src/Database/Adapter/SQLite.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,11 +928,26 @@ public function getSupportForSchemaAttributes(): bool
928928
return false;
929929
}
930930

931+
/**
932+
* Is upsert supported?
933+
*
934+
* @return bool
935+
*/
931936
public function getSupportForUpserts(): bool
932937
{
933938
return false;
934939
}
935940

941+
/**
942+
* Is hostname supported?
943+
*
944+
* @return bool
945+
*/
946+
public function getSupportForHostname(): bool
947+
{
948+
return false;
949+
}
950+
936951
/**
937952
* Get SQL Index Type
938953
*

0 commit comments

Comments
 (0)