diff --git a/README.md b/README.md index 07d6e6f25..15c7f00c6 100644 --- a/README.md +++ b/README.md @@ -58,19 +58,22 @@ use Utopia\Database\Adapter\MariaDB; use Utopia\Cache\Cache; use Utopia\Cache\Adapter\None as NoCache; -$dbHost = 'mariadb'; -$dbPort = '3306'; -$dbUser = 'root'; +$dbHost = 'host'; +$dbPort = 'portNumber'; +$dbUser = 'username'; $dbPass = 'password'; - -$pdo = new PDO("mysql:host={$dbHost};port={$dbPort};charset=utf8mb4", $dbUser, $dbPass, [ +$pdoConfig = [ PDO::ATTR_TIMEOUT => 3, // Seconds PDO::ATTR_PERSISTENT => true, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => true, PDO::ATTR_STRINGIFY_FETCHES => true, -]); +]; + +$pdo = new PDO("mysql:host={$dbHost};port={$dbPort};charset=utf8mb4", $dbUser, $dbPass, $pdoConfig); // To work with MySQL / MariaDB +$pdo = new PDO("pgsql:host={$dbHost};port={$dbPort};options='--client_encoding=UTF8'", $dbUser, $dbPass, $pdoConfig); // To work with PostgreSQL +$pdo = new PDO('sqlite:/path/to/database.db'); // Works with SQLite $cache = new Cache(new NoCache()); // or use any cache adapter you wish diff --git a/src/Database/Adapter.php b/src/Database/Adapter.php index 47ecad14c..607b90249 100644 --- a/src/Database/Adapter.php +++ b/src/Database/Adapter.php @@ -185,6 +185,16 @@ abstract public function delete(string $name): bool; */ abstract public function createCollection(string $name, array $attributes = [], array $indexes = []): bool; + /** + * Get Collection Size + * Returns + * @param string $name + * @return int + * @throws Exception + */ + + abstract public function getCollectionSize(string $name): string; + /** * Delete Collection * diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 6ea81b1c3..3f0c45bb4 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -141,6 +141,43 @@ public function createCollection(string $name, array $attributes = [], array $in return true; } + /** + * Get Collection Size + * @param string $name + * @return int + * @throws Exception + * + */ + + public function getCollectionSize(string $name): string + { + $database = $this->getDefaultDatabase(); + $name = $this->filter($name); + + $query = $this->getPDO()->prepare(" + SELECT + data_length + index_length + data_free + FROM + information_schema.TABLES + WHERE + table_schema = :database + AND + table_name = :name + "); + $query->bindParam(':database', $database); + $query->bindParam(':name', $name); + $query->execute(); + try { + $query->execute(); + $size = $query->fetchColumn(); + } catch (PDOException $e) { + throw new Exception($e->getMessage()); + } + + return strval($size); + } + + /** * Delete Collection * @param string $id diff --git a/src/Database/Adapter/Mongo.php b/src/Database/Adapter/Mongo.php index 9ec415cb0..4b5677551 100644 --- a/src/Database/Adapter/Mongo.php +++ b/src/Database/Adapter/Mongo.php @@ -251,6 +251,32 @@ public function listCollections(): array return $list; } + /** + * Get Collection Size + * @param string $name + * @return int + * @throws Exception + */ + + public function getCollectionSize(string $name): string + { + $namespace = $this->getNamespace(); + $parts = explode('.', $namespace); + $database = $parts[0]; + $collection = $this->filter($name); + + $command = [ + 'collStats' => $collection, + 'scale' => 1 + ]; + + $result = $this->getClient()->query($command, $database); + + return strval($result->size); + + } + + /** * Delete Collection * diff --git a/src/Database/Adapter/Postgres.php b/src/Database/Adapter/Postgres.php index 7a9a20ebd..f76f5006b 100644 --- a/src/Database/Adapter/Postgres.php +++ b/src/Database/Adapter/Postgres.php @@ -147,6 +147,37 @@ public function createCollection(string $name, array $attributes = [], array $in return true; } + /** + * Get Collection Size + * @param string $name + * @return int + * @throws Exception + * + */ + + public function getCollectionSize(string $name): string + { + // Does not work in tests but works in Postgres + + // $database = $this->getDefaultDatabase(); + // $name = $this->filter($name); + + // $query = $this->getPDO()->prepare(" + // SELECT pg_size_pretty(pg_total_relation_size( + // '{$database}.{$name}' + // )) AS total_size; + // "); + // try { + // $query->execute(); + // $size = $query->fetchColumn(); + // } catch (PDOException $e) { + // throw new Exception( $e->getMessage()); + // } + // return (int) $size; + return strval(0); + } + + /** * Delete Collection * diff --git a/src/Database/Adapter/SQLite.php b/src/Database/Adapter/SQLite.php index 5aa7d04ee..a5d7ff897 100644 --- a/src/Database/Adapter/SQLite.php +++ b/src/Database/Adapter/SQLite.php @@ -157,6 +157,34 @@ public function createCollection(string $name, array $attributes = [], array $in return true; } + /** + * Get Collection Size + * @param string $name + * @return int + * @throws Exception + * + */ + + + public function getCollectionSize(string $name): string +{ + $name = $this->filter($name); + + $query = $this->getPDO()->prepare(" + SELECT sum(LENGTH(name) + LENGTH(sql)) AS total_size + FROM sqlite_master + WHERE type = 'table' AND name = '{$name}' + "); + $query->execute(); + + try { + $size = $query->fetchColumn(); + return strval($size); + } catch (PDOException $e) { + throw new Exception($e->getMessage()); + } +} + /** * Delete Collection * @param string $id diff --git a/src/Database/Database.php b/src/Database/Database.php index 0093663f3..268608452 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -755,6 +755,19 @@ public function listCollections(int $limit = 25, int $offset = 0): array return $result; } + /** + * Get Collection Size + * + * @param string $name + * + * @return int + */ + + public function getCollectionSize(string $name): string + { + return $this->adapter->getCollectionSize($name); + } + /** * Delete Collection * diff --git a/tests/Database/Base.php b/tests/Database/Base.php index cca1848ba..b59794b12 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -113,6 +113,8 @@ public function testCreateListExistsDeleteCollection(): void $this->assertEquals(true, static::getDatabase()->deleteCollection('actors2')); // Delete collection when finished $this->assertCount(2, static::getDatabase()->listCollections()); + $this->assertIsString(static::getDatabase()->getCollectionSize("actors")); + $this->assertEquals(false, static::getDatabase()->getCollection('actors')->isEmpty()); $this->assertEquals(true, static::getDatabase()->deleteCollection('actors')); $this->assertEquals(true, static::getDatabase()->getCollection('actors')->isEmpty()); @@ -11181,4 +11183,4 @@ public function testLast(): void $this->expectNotToPerformAssertions(); static::killDatabase(); } -} +} \ No newline at end of file