diff --git a/composer.json b/composer.json index e415bf2..af00d84 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ }, "autoload": { "psr-4": { - "ProVM\\": "src/" + "Database\\": "src/" } } } diff --git a/src/Concept/Database/Connection.php b/src/Concept/Database/Connection.php deleted file mode 100644 index 705400d..0000000 --- a/src/Concept/Database/Connection.php +++ /dev/null @@ -1,15 +0,0 @@ -setDatabase($database); - } - - protected Database $database; - - protected function getDatabase(): Database - { - return $this->database; - } - - protected function setDatabase(Database $database): Database\Connection - { - $this->database = $database; - return $this; - } - - protected PDO $pdo; - public function connect(): PDO - { - if (!isset($this->pdo)) { - $dsn = $this->getDatabase()->getDsn(); - if ($this->getDatabase()->needsUser()) { - $this->pdo = new PDO($dsn, $this->getDatabase()->getUser(), $this->getDatabase()->getPassword()); - } else { - $this->pdo = new PDO($dsn); - } - } - return $this->pdo; - } - - protected Database\Transaction $transaction; - - public function transaction(): Database\Transaction - { - if (!isset($this->transaction)) { - $this->transaction = new Transaction($this); - } - return $this->transaction; - } - - public function query(string $query): Database\ResultSet - { - $statement = $this->connect()->query($query); - if ($statement === false) { - throw new InvalidQuery($query); - } - return new ResultSet($statement); - } - public function prepare(string $query): Database\ResultSet - { - $statement = $this->connect()->prepare($query); - if ($statement === false) { - throw new InvalidQuery($query); - } - return new ResultSet($statement); - } - public function execute(string $query, ?array $data = null): Database\ResultSet - { - if ($data !== null) { - $rs = $this->prepare($query); - $rs->execute($data); - return $rs; - } - return $this->query($query); - } - - public function fetchOne(string $query, ?array $data = null): array - { - return $this->execute($query, $data)->fetchFirst(); - } - public function fetchMany(string $query, ?array $data = null): array - { - return $this->execute($query, $data)->fetchAll(); - } -} diff --git a/src/Database/Transaction.php b/src/Database/Transaction.php deleted file mode 100644 index 3181903..0000000 --- a/src/Database/Transaction.php +++ /dev/null @@ -1,40 +0,0 @@ -setConnection($connection); - } - - protected Concept\Database\Connection $connection; - - public function getConnection(): Concept\Database\Connection - { - return $this->connection; - } - - public function setConnection(Concept\Database\Connection $connection): Concept\Database\Transaction - { - $this->connection = $connection; - return $this; - } - - public function begin(): Concept\Database\Transaction - { - $this->getConnection()->connect()->beginTransaction(); - return $this; - } - public function commit(): void - { - $this->getConnection()->connect()->commit(); - } - public function rollBack(): void - { - $this->getConnection()->connect()->rollBack(); - } -} diff --git a/src/Concept/Database.php b/src/Define/Database.php similarity index 50% rename from src/Concept/Database.php rename to src/Define/Database.php index bc9f2a9..75f9804 100644 --- a/src/Concept/Database.php +++ b/src/Define/Database.php @@ -1,5 +1,5 @@ password; } - public function setHost(string $host): Concept\Database + public function setHost(string $host): Define\Database { $this->host = $host; return $this; } - public function setPort(int $port): Concept\Database + public function setPort(int $port): Define\Database { $this->port = $port; return $this; } - public function setName(string $name): Concept\Database + public function setName(string $name): Define\Database { $this->name = $name; return $this; } - public function setUser(string $username): Concept\Database + public function setUser(string $username): Define\Database { $this->user = $username; return $this; } - public function setPassword(string $password): Concept\Database + public function setPassword(string $password): Define\Database { $this->password = $password; return $this; diff --git a/src/Implement/Connection.php b/src/Implement/Connection.php new file mode 100644 index 0000000..b599d7a --- /dev/null +++ b/src/Implement/Connection.php @@ -0,0 +1,111 @@ +setDatabase($database); + } + + protected Define\Database $database; + + protected function getDatabase(): Define\Database + { + return $this->database; + } + + protected function setDatabase(Define\Database $database): self + { + $this->database = $database; + return $this; + } + + protected PDO $pdo; + public function connect(): self + { + if (!isset($this->pdo)) { + $dsn = $this->getDatabase()->getDsn(); + if ($this->getDatabase()->needsUser()) { + $this->pdo = new PDO($dsn, $this->getDatabase()->getUser(), $this->getDatabase()->getPassword()); + } else { + $this->pdo = new PDO($dsn); + } + } + return $this; + } + public function getPDO(): PDO + { + return $this->connect()->pdo; + } + + protected Define\Database\Transaction $transaction; + + public function transaction(): Define\Database\Transaction + { + if (!isset($this->transaction)) { + $this->transaction = new Transaction($this); + } + return $this->transaction; + } + + public function query(string $query): Define\Database\ResultSet + { + try { + $statement = $this->getPDO()->query($query); + } catch (PDOException $exception) { + throw new InvalidQuery($query, $exception); + } + if ($statement === false) { + throw new InvalidQuery($query); + } + return new ResultSet($statement); + } + public function prepare(string $query): Define\Database\ResultSet + { + try { + $statement = $this->getPDO()->prepare($query); + } catch (PDOException $exception) { + throw new InvalidQuery($query, $exception); + } + if ($statement === false) { + throw new InvalidQuery($query); + } + return new ResultSet($statement); + } + public function execute(string $query, ?array $data = null): Define\Database\ResultSet + { + if ($data !== null) { + return $this->prepare($query) + ->execute($data); + } + return $this->query($query); + } + + /** + * @param string $query + * @param array|null $data + * @return array + * @throws InvalidQuery + */ + public function fetchOne(string $query, ?array $data = null): array + { + return $this->execute($query, $data)->fetchFirst(); + } + + /** + * @param string $query + * @param array|null $data + * @return array + * @throws InvalidQuery + */ + public function fetchMany(string $query, ?array $data = null): array + { + return $this->execute($query, $data)->fetchAll(); + } +} diff --git a/src/Database/MySQL.php b/src/Implement/MySQL.php similarity index 86% rename from src/Database/MySQL.php rename to src/Implement/MySQL.php index a4e009f..546366c 100644 --- a/src/Database/MySQL.php +++ b/src/Implement/MySQL.php @@ -1,7 +1,7 @@ statement; } - protected function setStatement(PDOStatement $statement): ResultSet + protected function setStatement(PDOStatement $statement): self { $this->statement = $statement; return $this; } - public function execute(array $data): Database\ResultSet + public function execute(array $data): self { $this->statement->execute($data); return $this; } + /** + * @return PDOStatement + * @throws BlankResult + */ protected function checkResults(): PDOStatement { if ($this->getStatement()->rowCount() === 0) { @@ -38,18 +42,38 @@ class ResultSet implements Database\ResultSet } return $this->getStatement(); } + + /** + * @return array + * @throws BlankResult + */ public function fetchFirst(): array { return $this->checkResults()->fetch(PDO::FETCH_ASSOC); } + + /** + * @return array + * @throws BlankResult + */ public function fetchAll(): array { return $this->checkResults()->fetchAll(PDO::FETCH_ASSOC); } + + /** + * @return object + * @throws BlankResult + */ public function fetchFirstAsObject(): object { return $this->checkResults()->fetch(PDO::FETCH_OBJ); } + + /** + * @return array + * @throws BlankResult + */ public function fetchAllAsObjects(): array { return $this->checkResults()->fetchAll(PDO::FETCH_OBJ); diff --git a/src/Database/SQLite.php b/src/Implement/SQLite.php similarity index 69% rename from src/Database/SQLite.php rename to src/Implement/SQLite.php index f8ab299..e43481c 100644 --- a/src/Database/SQLite.php +++ b/src/Implement/SQLite.php @@ -1,7 +1,7 @@ setConnection($connection); + } + + protected Define\Database\Connection $connection; + + public function getConnection(): Define\Database\Connection + { + return $this->connection; + } + + public function setConnection(Define\Database\Connection $connection): self + { + $this->connection = $connection; + return $this; + } + + public function begin(): self + { + $this->getConnection()->getPDO()->beginTransaction(); + return $this; + } + public function commit(): void + { + $this->getConnection()->getPDO()->commit(); + } + public function rollBack(): void + { + $this->getConnection()->getPDO()->rollBack(); + } +} diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index fe4919f..9286f22 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -1,65 +1,61 @@ pdo = new PDO('sqlite::memory:'); - $query = "CREATE TABLE IF NOT EXISTS test_table (id INTEGER PRIMARY KEY, test TEXT)"; + $this->pdo = new PDO("sqlite:{$this->host}"); + $query = "CREATE TABLE IF NOT EXISTS {$this->tableName} (id INTEGER PRIMARY KEY, test TEXT)"; + $this->pdo->query($query); + $query = "INSERT INTO {$this->tableName} (test) VALUES ('test')"; $this->pdo->query($query); } protected function tearDown(): void { unset($this->pdo); + unlink($this->host); + } + + protected function getConnection(): Connection + { + $host = $this->host; + + $database = $this->getMockBuilder(Define\Database::class)->getMock(); + $database->method('getHost')->willReturn($host); + $database->method('getDsn')->willReturn("sqlite:{$host}"); + $database->method('needsUser')->willReturn(false); + + return new Connection($database); } public function testConnection() { - $host = "memory"; - - $database = $this->getMockBuilder(Concept\Database::class)->getMock(); - $database->method('getHost')->willReturn($host); - $database->method('getDsn')->willReturn("sqlite::{$host}"); - $database->method('needsUser')->willReturn(false); - - $connection = new Connection($database); - $this->assertEquals($this->pdo, $connection->connect()); + $connection = $this->getConnection(); + $this->assertEquals($this->pdo, $connection->getPDO()); } public function testQuery() { - $host = "memory"; - - $database = $this->getMockBuilder(Concept\Database::class)->getMock(); - $database->method('getHost')->willReturn($host); - $database->method('getDsn')->willReturn("sqlite::{$host}"); - $database->method('needsUser')->willReturn(false); - - $connection = new Connection($database); + $connection = $this->getConnection(); $query = "CREATE TABLE IF NOT EXISTS test_table (id INTEGER PRIMARY KEY, test TEXT)"; $connection->query($query); $query = "SELECT * FROM test_table"; - $result = $connection->query($query); - $this->assertInstanceOf(Concept\Database\ResultSet::class, $result); + $connection->query($query); + $this->assertTrue(true); } public function testPrepare() { - $host = "memory"; - - $database = $this->getMockBuilder(Concept\Database::class)->getMock(); - $database->method('getHost')->willReturn($host); - $database->method('getDsn')->willReturn("sqlite::{$host}"); - $database->method('needsUser')->willReturn(false); - - $connection = new Connection($database); + $connection = $this->getConnection(); $query = "CREATE TABLE IF NOT EXISTS test_table (id INTEGER PRIMARY KEY, test TEXT)"; $connection->query($query); $query = "SELECT * FROM test_table"; - $result = $connection->prepare($query); - $this->assertInstanceOf(Concept\Database\ResultSet::class, $result); + $connection->prepare($query); + $this->assertTrue(true); } } diff --git a/tests/MySQLTest.php b/tests/MySQLTest.php index c02ede0..cce4c82 100644 --- a/tests/MySQLTest.php +++ b/tests/MySQLTest.php @@ -1,6 +1,6 @@ createMock(ProVM\Concept\Database\Connection::class); + $connection = $this->createMock(Database\Define\Database\Connection::class); $transaction = new Transaction($connection); $transaction->begin(); $this->assertTrue(true);