multi connections system
This commit is contained in:
49
src/Db.php
49
src/Db.php
@@ -9,8 +9,9 @@ use PDOStatement;
|
|||||||
class Db
|
class Db
|
||||||
{
|
{
|
||||||
protected PDO $pdo;
|
protected PDO $pdo;
|
||||||
|
protected string $name;
|
||||||
protected string $connstring;
|
protected string $connstring;
|
||||||
private static $instance = null;
|
private static array $instances = [];
|
||||||
private string $type;
|
private string $type;
|
||||||
private ?string $user;
|
private ?string $user;
|
||||||
private ?string $password;
|
private ?string $password;
|
||||||
@@ -19,10 +20,10 @@ class Db
|
|||||||
private ?string $host;
|
private ?string $host;
|
||||||
private string $error;
|
private string $error;
|
||||||
|
|
||||||
|
private function __construct($name, $dir)
|
||||||
private function __construct($dir)
|
|
||||||
{
|
{
|
||||||
$this->error = "";
|
$this->error = "";
|
||||||
|
$this->name = $name;
|
||||||
$this->loadEnv($dir);
|
$this->loadEnv($dir);
|
||||||
$this->initPdo();
|
$this->initPdo();
|
||||||
}
|
}
|
||||||
@@ -41,17 +42,23 @@ class Db
|
|||||||
private function parseEnv()
|
private function parseEnv()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$this->type = isset($_ENV["DB_TYPE"]) ? $_ENV["DB_TYPE"] : null;
|
$key = (strtoupper(trim($this->name)));
|
||||||
$this->user = isset($_ENV["DB_USER"]) ? $_ENV["DB_USER"] : null;
|
$this->type = isset($_ENV[$this->getKey("TYPE", $key)]) ? $_ENV[$this->getKey("TYPE", $key)] : null;
|
||||||
$this->host = isset($_ENV["DB_HOST"]) ? $_ENV["DB_HOST"] : null;
|
$this->user = isset($_ENV[$this->getKey("USER", $key)]) ? $_ENV[$this->getKey("USER", $key)] : null;
|
||||||
$this->password = isset($_ENV["DB_PASSWORD"]) ? $_ENV["DB_PASSWORD"] : null;
|
$this->host = isset($_ENV[$this->getKey("HOST", $key)]) ? $_ENV[$this->getKey("HOST", $key)] : null;
|
||||||
$this->database = isset($_ENV["DB_DATABASE"]) ? $_ENV["DB_DATABASE"] : null;
|
$this->password = isset($_ENV[$this->getKey("PASSWORD", $key)]) ? $_ENV[$this->getKey("PASSWORD", $key)] : null;
|
||||||
$this->port = isset($_ENV["DB_PORT"]) ? $_ENV["DB_PORT"] : 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) {
|
} catch (\Throwable $th) {
|
||||||
$this->error = $th->getMessage();
|
$this->error = $th->getMessage();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function getKey(string $type, string $name): string
|
||||||
|
{
|
||||||
|
return "DB_" . $name . "_" . $type;
|
||||||
|
}
|
||||||
|
|
||||||
private function initPdo()
|
private function initPdo()
|
||||||
{
|
{
|
||||||
$this->getConnString();
|
$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) {
|
foreach ($connections as $connection) {
|
||||||
self::$instance = new Db($dir);
|
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()
|
public function getError()
|
||||||
|
|||||||
@@ -7,13 +7,14 @@ class Model
|
|||||||
protected mixed $values;
|
protected mixed $values;
|
||||||
protected array $updated;
|
protected array $updated;
|
||||||
protected string $table;
|
protected string $table;
|
||||||
|
protected string $connection = "default";
|
||||||
protected string $pk;
|
protected string $pk;
|
||||||
private Db $db;
|
private ?Db $db;
|
||||||
private bool $new;
|
private bool $new;
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->db = Db::getInstance();
|
$this->db = null;
|
||||||
$this->new = true;
|
$this->new = true;
|
||||||
$this->values = array();
|
$this->values = array();
|
||||||
$this->updated = array();
|
$this->updated = array();
|
||||||
@@ -43,6 +44,17 @@ class Model
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getConnection()
|
||||||
|
{
|
||||||
|
return $this->connection;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setConnection($connection)
|
||||||
|
{
|
||||||
|
$this->connection = $connection;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
public function fillData(mixed $data): self
|
public function fillData(mixed $data): self
|
||||||
{
|
{
|
||||||
$this->values = $data;
|
$this->values = $data;
|
||||||
@@ -53,6 +65,7 @@ class Model
|
|||||||
|
|
||||||
public function save(): bool
|
public function save(): bool
|
||||||
{
|
{
|
||||||
|
$this->db = Db::getInstance($this->connection);
|
||||||
$ret = false;
|
$ret = false;
|
||||||
$qb = $this->newQueryBuilder();
|
$qb = $this->newQueryBuilder();
|
||||||
if ($this->new) {
|
if ($this->new) {
|
||||||
@@ -84,7 +97,7 @@ class Model
|
|||||||
|
|
||||||
public function newQueryBuilder(): QueryBuilder
|
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
|
public function __set($name, $value): void
|
||||||
|
|||||||
@@ -4,22 +4,24 @@ namespace Kletellier\PdoWrapper;
|
|||||||
|
|
||||||
class QueryBuilder
|
class QueryBuilder
|
||||||
{
|
{
|
||||||
protected Db $db;
|
protected ?Db $db;
|
||||||
protected array $columns;
|
protected array $columns;
|
||||||
protected array $wheres;
|
protected array $wheres;
|
||||||
protected array $orders;
|
protected array $orders;
|
||||||
protected array $groupby;
|
protected array $groupby;
|
||||||
protected string $table;
|
protected string $table;
|
||||||
|
protected string $connection;
|
||||||
protected string $pk;
|
protected string $pk;
|
||||||
protected ?int $take;
|
protected ?int $take;
|
||||||
protected ?int $offset;
|
protected ?int $offset;
|
||||||
protected string $raw_query;
|
protected string $raw_query;
|
||||||
|
|
||||||
public function __construct($table, $pk = "id")
|
public function __construct($table, $pk = "id", $connection = "default")
|
||||||
{
|
{
|
||||||
$this->table = $table;
|
$this->table = $table;
|
||||||
$this->pk = $pk;
|
$this->pk = $pk;
|
||||||
$this->db = Db::getInstance();
|
$this->connection = $connection;
|
||||||
|
$this->db = null;
|
||||||
$this->reset();
|
$this->reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -175,6 +177,7 @@ class QueryBuilder
|
|||||||
|
|
||||||
private function getData(): mixed
|
private function getData(): mixed
|
||||||
{
|
{
|
||||||
|
$this->db = Db::getInstance($this->connection);
|
||||||
return $this->db->getSelectQuery($this->getQuery());
|
return $this->db->getSelectQuery($this->getQuery());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -265,4 +268,14 @@ class QueryBuilder
|
|||||||
return $this->getConstructedQuery();
|
return $this->getConstructedQuery();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getConnection()
|
||||||
|
{
|
||||||
|
return $this->connection;
|
||||||
|
}
|
||||||
|
public function setConnection($connection)
|
||||||
|
{
|
||||||
|
$this->connection = $connection;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,9 +7,13 @@ use Kletellier\PdoWrapper\QueryBuilder;
|
|||||||
|
|
||||||
test("create db",function () {
|
test("create db",function () {
|
||||||
$dir = getcwd();
|
$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);");
|
$ret = $db->executeRawQuery("CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT, col1 VARCHAR, date1 DATETIME);");
|
||||||
expect($ret)->toBe(true);
|
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 () {
|
test("insert row",function () {
|
||||||
@@ -138,3 +142,28 @@ test("where array", function() {
|
|||||||
expect($in[0]->id)->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);
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user