From 0df2e95e7fb4a694455920972fa1c83845af37b0 Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Thu, 8 Sep 2022 20:02:05 -0400 Subject: [PATCH] Implementations --- src/Database/Connection.php | 75 ++++++++++++++++++++++++++++++++++++ src/Database/MySQL.php | 28 ++++++++++++++ src/Database/ResultSet.php | 44 +++++++++++++++++++++ src/Database/Transaction.php | 59 ++++++++++++++++++++++++++++ 4 files changed, 206 insertions(+) create mode 100644 src/Database/Connection.php create mode 100644 src/Database/MySQL.php create mode 100644 src/Database/ResultSet.php create mode 100644 src/Database/Transaction.php diff --git a/src/Database/Connection.php b/src/Database/Connection.php new file mode 100644 index 0000000..0e5c8f3 --- /dev/null +++ b/src/Database/Connection.php @@ -0,0 +1,75 @@ +setDatabase($database); + $this->connect(); + } + + protected Database $database; + public function setDatabase(Database $database): ConnectionInterface + { + $this->database = $database; + return $this; + } + public function getDatabase(): Database + { + return $this->database; + } + public function connect(): ConnectionInterface + { + if (!isset($this->pdo)) { + if ($this->getDatabase()->needsUser()) { + $pdo = new PDO($this->getDatabase()->getDSN(), $this->getDatabase()->getUsername(), $this->getDatabase()->getPassword()); + } else { + $pdo = new PDO($this->getDatabase()->getDSN()); + } + $this->setPDO($pdo); + } + return $this; + } + protected PDO $pdo; + public function setPDO(PDO $pdo): ConnectionInterface + { + $this->pdo = $pdo; + return $this; + } + public function getPDO(): PDO + { + return $this->pdo; + } + public function query(string $query): ResultSetInterface + { + $st = $this->getPDO()->query($query); + if (!$st) { + throw new PDOException("Could not run query {$query}"); + } + return new ResultSet($st); + } + public function prepare(string $query): ResultSetInterface + { + $st = $this->getPDO()->prepare($query); + if (!$st) { + throw new PDOException("Could not prepare query {$query}"); + } + return new ResultSet($st); + } + public function execute(string $query, array $values): ResultSetInterface + { + return $this->prepare($query)->execute($values); + } + public function transaction(): TransactionInterface + { + return new Transaction($this->getPDO()); + } +} diff --git a/src/Database/MySQL.php b/src/Database/MySQL.php new file mode 100644 index 0000000..9836747 --- /dev/null +++ b/src/Database/MySQL.php @@ -0,0 +1,28 @@ +getHost()}" + ]; + if (isset($this->port)) { + $arr []= "port={$this->getPort()}"; + } + $arr []= "dbname={$this->getName()}"; + + return implode(':', [ + 'mysql', + implode(';', $arr) + ]); + } +} diff --git a/src/Database/ResultSet.php b/src/Database/ResultSet.php new file mode 100644 index 0000000..c234bd5 --- /dev/null +++ b/src/Database/ResultSet.php @@ -0,0 +1,44 @@ +setStatement($statement); + } + + protected PDOStatement $statement; + public function setStatement(PDOStatement $statement): RSInterface + { + $this->statement = $statement; + return $this; + } + public function getStatement(): PDOStatement + { + return $this->statement; + } + + public function execute(array $values): RSInterface + { + $this->getStatement()->execute($values); + return $this; + } + + public function getAsArray(): array + { + return $this->getStatement()->fetchAll(PDO::FETCH_ASSOC); + } + public function getAsObject(): array + { + return $this->getStatement()->fetchAll(PDO::FETCH_OBJ); + } + public function getFirst(): mixed + { + return $this->getStatement()->fetch(PDO::FETCH_OBJ); + } +} diff --git a/src/Database/Transaction.php b/src/Database/Transaction.php new file mode 100644 index 0000000..7901640 --- /dev/null +++ b/src/Database/Transaction.php @@ -0,0 +1,59 @@ +setPDO($pdo); + } + + protected PDO $pdo; + public function setPDO(PDO $pdo): TransactionInterface + { + $this->pdo = $pdo; + return $this; + } + public function getPDO(): PDO + { + return $this->pdo; + } + public function begin(): TransactionInterface + { + $this->getPDO()->beginTransaction(); + return $this; + } + public function query(string $query): ResultSetInterface + { + $st = $this->getPDO()->query($query); + if (!$st) { + throw new PDOException("Could not run query {$query}"); + } + return new ResultSet($st); + } + public function prepare(string $query): ResultSetInterface + { + $st = $this->getPDO()->prepare($query); + if (!$st) { + throw new PDOException("Could not prepare query {$query}"); + } + return new ResultSet($st); + } + public function execute(string $query, array $values): ResultSetInterface + { + return $this->prepare($query)->execute($values); + } + public function commit(): void + { + $this->getPDO()->commit(); + } + public function rollBack(): void + { + $this->getPDO()->rollBack(); + } +}