From 47a64d4af03bf1514f8b6f93a11b61bf957e5dc2 Mon Sep 17 00:00:00 2001 From: Gregory Letellier Date: Mon, 30 Oct 2023 09:34:16 +0100 Subject: [PATCH] multi connections system --- src/Db.php | 49 +++++++++++++++++++++++++++++--------------- src/Model.php | 19 ++++++++++++++--- src/QueryBuilder.php | 19 ++++++++++++++--- tests/DbTest.php | 31 +++++++++++++++++++++++++++- 4 files changed, 94 insertions(+), 24 deletions(-) diff --git a/src/Db.php b/src/Db.php index 73e54d7..c6833b3 100644 --- a/src/Db.php +++ b/src/Db.php @@ -9,8 +9,9 @@ use PDOStatement; class Db { protected PDO $pdo; + protected string $name; protected string $connstring; - private static $instance = null; + private static array $instances = []; private string $type; private ?string $user; private ?string $password; @@ -19,10 +20,10 @@ class Db private ?string $host; private string $error; - - private function __construct($dir) + private function __construct($name, $dir) { $this->error = ""; + $this->name = $name; $this->loadEnv($dir); $this->initPdo(); } @@ -41,17 +42,23 @@ class Db private function parseEnv() { try { - $this->type = isset($_ENV["DB_TYPE"]) ? $_ENV["DB_TYPE"] : null; - $this->user = isset($_ENV["DB_USER"]) ? $_ENV["DB_USER"] : null; - $this->host = isset($_ENV["DB_HOST"]) ? $_ENV["DB_HOST"] : null; - $this->password = isset($_ENV["DB_PASSWORD"]) ? $_ENV["DB_PASSWORD"] : null; - $this->database = isset($_ENV["DB_DATABASE"]) ? $_ENV["DB_DATABASE"] : null; - $this->port = isset($_ENV["DB_PORT"]) ? $_ENV["DB_PORT"] : null; + $key = (strtoupper(trim($this->name))); + $this->type = isset($_ENV[$this->getKey("TYPE", $key)]) ? $_ENV[$this->getKey("TYPE", $key)] : null; + $this->user = isset($_ENV[$this->getKey("USER", $key)]) ? $_ENV[$this->getKey("USER", $key)] : null; + $this->host = isset($_ENV[$this->getKey("HOST", $key)]) ? $_ENV[$this->getKey("HOST", $key)] : null; + $this->password = isset($_ENV[$this->getKey("PASSWORD", $key)]) ? $_ENV[$this->getKey("PASSWORD", $key)] : null; + $this->database = isset($_ENV[$this->getKey("DATABASE", $key)]) ? $_ENV[$this->getKey("DATABASE", $key)] : null; + $this->port = isset($_ENV[$this->getKey("PORT", $key)]) ? $_ENV[$this->getKey("PORT", $key)] : null; } catch (\Throwable $th) { $this->error = $th->getMessage(); } } + private function getKey(string $type, string $name): string + { + return "DB_" . $name . "_" . $type; + } + private function initPdo() { $this->getConnString(); @@ -132,22 +139,30 @@ class Db } } - public static function init($dir) + public static function init($dir, array $connections = ["default"]) { - return self::retrieveInstance($dir); + self::fillInstances($dir, $connections); + return self::getInstance(); } - private static function retrieveInstance($dir = __DIR__) + private static function fillInstances($dir, array $connections) { - if (self::$instance == null) { - self::$instance = new Db($dir); + foreach ($connections as $connection) { + if (!isset(self::$instances[$connection])) { + self::$instances[$connection] = new Db($connection, $dir); + } } - return self::$instance; } - public static function getInstance() + public static function getInstance($connection = "default") { - return self::retrieveInstance(); + if (isset(self::$instances[$connection])) { + $instance = self::$instances[$connection]; + if ($instance instanceof Db) { + return self::$instances[$connection]; + } + } + throw new \Exception("Unknown connection"); } public function getError() diff --git a/src/Model.php b/src/Model.php index 7cabadf..fd6874c 100644 --- a/src/Model.php +++ b/src/Model.php @@ -7,13 +7,14 @@ class Model protected mixed $values; protected array $updated; protected string $table; + protected string $connection = "default"; protected string $pk; - private Db $db; + private ?Db $db; private bool $new; public function __construct() { - $this->db = Db::getInstance(); + $this->db = null; $this->new = true; $this->values = array(); $this->updated = array(); @@ -43,6 +44,17 @@ class Model return $this; } + public function getConnection() + { + return $this->connection; + } + + public function setConnection($connection) + { + $this->connection = $connection; + return $this; + } + public function fillData(mixed $data): self { $this->values = $data; @@ -53,6 +65,7 @@ class Model public function save(): bool { + $this->db = Db::getInstance($this->connection); $ret = false; $qb = $this->newQueryBuilder(); if ($this->new) { @@ -84,7 +97,7 @@ class Model public function newQueryBuilder(): QueryBuilder { - return new QueryBuilder($this->table, $this->pk); + return new QueryBuilder($this->table, $this->pk, $this->connection); } public function __set($name, $value): void diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index 76e3427..562dcae 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -4,22 +4,24 @@ namespace Kletellier\PdoWrapper; class QueryBuilder { - protected Db $db; + protected ?Db $db; protected array $columns; protected array $wheres; protected array $orders; protected array $groupby; protected string $table; + protected string $connection; protected string $pk; protected ?int $take; protected ?int $offset; protected string $raw_query; - public function __construct($table, $pk = "id") + public function __construct($table, $pk = "id", $connection = "default") { $this->table = $table; $this->pk = $pk; - $this->db = Db::getInstance(); + $this->connection = $connection; + $this->db = null; $this->reset(); } @@ -175,6 +177,7 @@ class QueryBuilder private function getData(): mixed { + $this->db = Db::getInstance($this->connection); return $this->db->getSelectQuery($this->getQuery()); } @@ -265,4 +268,14 @@ class QueryBuilder return $this->getConstructedQuery(); } } + + public function getConnection() + { + return $this->connection; + } + public function setConnection($connection) + { + $this->connection = $connection; + return $this; + } } diff --git a/tests/DbTest.php b/tests/DbTest.php index 193d1d9..981f446 100644 --- a/tests/DbTest.php +++ b/tests/DbTest.php @@ -7,9 +7,13 @@ use Kletellier\PdoWrapper\QueryBuilder; test("create db",function () { $dir = getcwd(); - $db = Db::init($dir); + $db = Db::init($dir,["default","contwo"]); $ret = $db->executeRawQuery("CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT, col1 VARCHAR, date1 DATETIME);"); expect($ret)->toBe(true); + + $db2 = Db::getInstance("contwo"); + $ret = $db2->executeRawQuery("CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT, col1 VARCHAR, date1 DATETIME);"); + expect($ret)->toBe(true); }); test("insert row",function () { @@ -137,4 +141,29 @@ test("where array", function() { expect(count($in))->toBe(2); expect($in[0]->id)->toBe(2); +}); + +test("second connection", function() { + $row = new Model(); + $row->setConnection("contwo"); + $row->setTable("test"); + $row->col1 = "test"; + $row->date1 = new \DateTime(); + $ret = $row->save(); + + $row2 = new Model(); + $row2->setConnection("contwo"); + $row2->setTable("test"); + $row2->col1 = "test2"; + $row2->date1 = new \DateTime(); + $ret2 = $row2->save(); + + expect($ret)->toBe(true); + expect($ret2)->toBe(true); + + $qb = new QueryBuilder("test","id","contwo"); + $rows = $qb->get(); + + expect($rows)->toBeArray(); + expect(count($rows))->toBe(2); }); \ No newline at end of file