Compare commits
2 Commits
d67916e2ff
...
4d7f0d6d83
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4d7f0d6d83 | ||
|
|
a6c51f5f17 |
@@ -6,7 +6,7 @@ use Dotenv\Dotenv;
|
|||||||
use PDO;
|
use PDO;
|
||||||
use PDOStatement;
|
use PDOStatement;
|
||||||
|
|
||||||
class Db
|
class Connection
|
||||||
{
|
{
|
||||||
protected PDO $pdo;
|
protected PDO $pdo;
|
||||||
protected string $name;
|
protected string $name;
|
||||||
@@ -170,7 +170,7 @@ class Db
|
|||||||
{
|
{
|
||||||
foreach ($connections as $connection) {
|
foreach ($connections as $connection) {
|
||||||
if (!isset(self::$instances[$connection])) {
|
if (!isset(self::$instances[$connection])) {
|
||||||
self::$instances[$connection] = new Db($connection, $config);
|
self::$instances[$connection] = new Connection($connection, $config);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -183,7 +183,7 @@ class Db
|
|||||||
}
|
}
|
||||||
if (isset(self::$instances[$connection])) {
|
if (isset(self::$instances[$connection])) {
|
||||||
$instance = self::$instances[$connection];
|
$instance = self::$instances[$connection];
|
||||||
if ($instance instanceof Db) {
|
if ($instance instanceof Connection) {
|
||||||
return self::$instances[$connection];
|
return self::$instances[$connection];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -9,12 +9,10 @@ class Model
|
|||||||
protected string $table;
|
protected string $table;
|
||||||
protected string $connection = "";
|
protected string $connection = "";
|
||||||
protected string $pk;
|
protected string $pk;
|
||||||
private ?Db $db;
|
|
||||||
private bool $new;
|
private bool $new;
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->db = null;
|
|
||||||
$this->new = true;
|
$this->new = true;
|
||||||
$this->values = array();
|
$this->values = array();
|
||||||
$this->updated = array();
|
$this->updated = array();
|
||||||
@@ -65,12 +63,11 @@ 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) {
|
||||||
$query = $qb->getPreparedQuery($this->values);
|
$query = $qb->getPreparedQuery($this->values);
|
||||||
$id = $this->db->insertQuery($query);
|
$id = Connection::getInstance($this->connection)->insertQuery($query);
|
||||||
if ($id !== false) {
|
if ($id !== false) {
|
||||||
$this->values[$this->pk] = $id;
|
$this->values[$this->pk] = $id;
|
||||||
$this->new = false;
|
$this->new = false;
|
||||||
@@ -85,7 +82,7 @@ class Model
|
|||||||
$data[$updatecol] = $this->values[$updatecol];
|
$data[$updatecol] = $this->values[$updatecol];
|
||||||
}
|
}
|
||||||
$query = $qb->getPreparedQuery($data, false);
|
$query = $qb->getPreparedQuery($data, false);
|
||||||
$ret = $this->db->updateQuery($query);
|
$ret = Connection::getInstance($this->connection)->updateQuery($query);
|
||||||
if ($ret) {
|
if ($ret) {
|
||||||
$this->updated = [];
|
$this->updated = [];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ namespace Kletellier\PdoWrapper;
|
|||||||
|
|
||||||
class QueryBuilder
|
class QueryBuilder
|
||||||
{
|
{
|
||||||
protected ?Db $db;
|
|
||||||
protected array $columns;
|
protected array $columns;
|
||||||
protected array $wheres;
|
protected array $wheres;
|
||||||
protected array $orders;
|
protected array $orders;
|
||||||
@@ -22,7 +21,6 @@ class QueryBuilder
|
|||||||
$this->table = $table;
|
$this->table = $table;
|
||||||
$this->pk = $pk;
|
$this->pk = $pk;
|
||||||
$this->connection = $connection;
|
$this->connection = $connection;
|
||||||
$this->db = null;
|
|
||||||
$this->reset();
|
$this->reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -197,8 +195,7 @@ class QueryBuilder
|
|||||||
|
|
||||||
private function getData(): mixed
|
private function getData(): mixed
|
||||||
{
|
{
|
||||||
$this->db = Db::getInstance($this->connection);
|
return Connection::getInstance($this->connection)->getSelectQuery($this->getQuery());
|
||||||
return $this->db->getSelectQuery($this->getQuery());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getConstructedQuery(): Query
|
private function getConstructedQuery(): Query
|
||||||
|
|||||||
@@ -1,18 +1,18 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Kletellier\PdoWrapper\Collection;
|
use Kletellier\PdoWrapper\Collection;
|
||||||
use Kletellier\PdoWrapper\Db;
|
use Kletellier\PdoWrapper\Connection;
|
||||||
use Kletellier\PdoWrapper\EventDispatcher;
|
use Kletellier\PdoWrapper\EventDispatcher;
|
||||||
use Kletellier\PdoWrapper\Model;
|
use Kletellier\PdoWrapper\Model;
|
||||||
use Kletellier\PdoWrapper\QueryBuilder;
|
use Kletellier\PdoWrapper\QueryBuilder;
|
||||||
|
|
||||||
test("create db",function () {
|
test("create db",function () {
|
||||||
$config = ["conone" => ["TYPE"=>"sqlite", "DATABASE" => ":memory:" ], "contwo"=>["TYPE"=>"sqlite", "DATABASE" => ":memory:" ]];
|
$config = ["conone" => ["TYPE"=>"sqlite", "DATABASE" => ":memory:" ], "contwo"=>["TYPE"=>"sqlite", "DATABASE" => ":memory:" ]];
|
||||||
$db = Db::init($config,["conone","contwo"]);
|
$db = Connection::init($config,["conone","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");
|
$db2 = Connection::getInstance("contwo");
|
||||||
$ret = $db2->executeRawQuery("CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT, col1 VARCHAR, date1 DATETIME);");
|
$ret = $db2->executeRawQuery("CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT, col1 VARCHAR, date1 DATETIME);");
|
||||||
expect($ret)->toBe(true);
|
expect($ret)->toBe(true);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user