Skip to content

Commit 2f2527b

Browse files
authored
Merge pull request #102 from utopia-php/feat-check-if-collection-exists
Feat: check if collection exists in database
2 parents bf92279 + a40e4be commit 2f2527b

5 files changed

Lines changed: 73 additions & 19 deletions

File tree

src/Database/Adapter.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,14 @@ abstract public function create(string $name): bool;
142142

143143
/**
144144
* Check if database exists
145+
* Optionally check if collection exists in database
145146
*
146-
* @param string $name
147+
* @param string $database database name
148+
* @param string $collection (optional) collection name
147149
*
148150
* @return bool
149151
*/
150-
abstract public function exists(string $name): bool;
152+
abstract public function exists(string $database, string $collection = null): bool;
151153

152154
/**
153155
* List Databases

src/Database/Adapter/MariaDB.php

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,27 +49,47 @@ public function create(string $name): bool
4949

5050
/**
5151
* Check if database exists
52+
* Optionally check if collection exists in database
5253
*
53-
* @param string $name
54+
* @param string $database database name
55+
* @param string $collection (optional) collection name
5456
*
5557
* @return bool
5658
*/
57-
public function exists(string $name): bool
59+
public function exists(string $database, string $collection = null): bool
5860
{
59-
$name = $this->filter($name);
61+
$database = $this->filter($database);
62+
63+
if (!\is_null($collection)) {
64+
$collection = $this->filter($collection);
65+
66+
$select = 'TABLE_NAME';
67+
$from = 'INFORMATION_SCHEMA.TABLES' ;
68+
$where = 'TABLE_SCHEMA = :schema AND TABLE_NAME = :table';
69+
$match = "{$this->getNamespace()}_{$collection}";
70+
} else {
71+
$select = 'SCHEMA_NAME';
72+
$from = 'INFORMATION_SCHEMA.SCHEMATA' ;
73+
$where = 'SCHEMA_NAME = :schema';
74+
$match = $database;
75+
}
6076

6177
$stmt = $this->getPDO()
62-
->prepare("SELECT SCHEMA_NAME
63-
FROM INFORMATION_SCHEMA.SCHEMATA
64-
WHERE SCHEMA_NAME = :schema;");
65-
66-
$stmt->bindValue(':schema', $name, PDO::PARAM_STR);
78+
->prepare("SELECT {$select}
79+
FROM {$from}
80+
WHERE {$where};");
81+
82+
$stmt->bindValue(':schema', $database, PDO::PARAM_STR);
83+
84+
if (!\is_null($collection)) {
85+
$stmt->bindValue(':table', "{$this->getNamespace()}_{$collection}", PDO::PARAM_STR);
86+
}
6787

6888
$stmt->execute();
6989

7090
$document = $stmt->fetch(PDO::FETCH_ASSOC);
7191

72-
return (($document['SCHEMA_NAME'] ?? '') == $name);
92+
return (($document[$select] ?? '') === $match);
7393
}
7494

7595
/**

src/Database/Adapter/MongoDB.php

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,41 @@ public function create(string $name): bool
4747

4848
/**
4949
* Check if database exists
50+
* Optionally check if collection exists in database
5051
*
51-
* @param string $name
52+
* @param string $database database name
53+
* @param string $collection (optional) collection name
5254
*
5355
* @return bool
5456
*/
55-
public function exists(string $name): bool
57+
public function exists(string $database, string $collection = null): bool
5658
{
57-
$name = $this->filter($name);
58-
forEach ($this->getClient()->listDatabaseNames() as $key => $value) {
59-
if ($name === $value) {
59+
$database = $this->filter($database);
60+
61+
if (!\is_null($collection)) {
62+
$collection = $this->filter($collection);
63+
64+
$match = "{$this->getNamespace()}_{$collection}";
65+
$names = $this
66+
->getClient()
67+
->selectDatabase($database)
68+
->listCollectionNames([
69+
'filter' => [
70+
'name' => $match
71+
]
72+
]);
73+
} else {
74+
$match = $database;
75+
$names = $this->getClient()
76+
->listDatabaseNames([
77+
'filter' => [
78+
'name' => $match
79+
]
80+
]);
81+
}
82+
83+
foreach ($names as $name) {
84+
if ($name === $match) {
6085
return true;
6186
}
6287
}

src/Database/Database.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,12 +280,16 @@ public function createMetadata(): bool
280280

281281
/**
282282
* Check if database exists
283+
* Optionally check if collection exists in database
284+
*
285+
* @param string $database database name
286+
* @param string $collection (optional) collection name
283287
*
284288
* @return bool
285289
*/
286-
public function exists(string $name): bool
290+
public function exists(string $database, string $collection = null): bool
287291
{
288-
return $this->adapter->exists($name);
292+
return $this->adapter->exists($database, $collection);
289293
}
290294

291295
/**

tests/Database/Base.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,17 @@ public function testCreateExistsDelete()
5757
/**
5858
* @depends testCreateExistsDelete
5959
*/
60-
public function testCreateListDeleteCollection()
60+
public function testCreateListExistsDeleteCollection()
6161
{
6262
$this->assertInstanceOf('Utopia\Database\Document', static::getDatabase()->createCollection('actors'));
6363

6464
$this->assertCount(1, static::getDatabase()->listCollections());
65+
$this->assertEquals(true, static::getDatabase()->exists($this->testDatabase, 'actors'));
6566

6667
// Collection names should not be unique
6768
$this->assertInstanceOf('Utopia\Database\Document', static::getDatabase()->createCollection('actors2'));
6869
$this->assertCount(2, static::getDatabase()->listCollections());
70+
$this->assertEquals(true, static::getDatabase()->exists($this->testDatabase, 'actors2'));
6971
$collection = static::getDatabase()->getCollection('actors2');
7072
$collection->setAttribute('name', 'actors'); // change name to one that exists
7173
$this->assertInstanceOf('Utopia\Database\Document', static::getDatabase()->updateDocument($collection->getCollection(), $collection->getId(), $collection));
@@ -75,6 +77,7 @@ public function testCreateListDeleteCollection()
7577
$this->assertEquals(false, static::getDatabase()->getCollection('actors')->isEmpty());
7678
$this->assertEquals(true, static::getDatabase()->deleteCollection('actors'));
7779
$this->assertEquals(true, static::getDatabase()->getCollection('actors')->isEmpty());
80+
$this->assertEquals(false, static::getDatabase()->exists($this->testDatabase, 'actors'));
7881
}
7982

8083
public function testCreateDeleteAttribute()

0 commit comments

Comments
 (0)