91 lines
3.0 KiB
PHP
91 lines
3.0 KiB
PHP
<?php
|
|
namespace DataBaseManager\DBManager;
|
|
use DataBaseManager\Entitie\Entitie;
|
|
use PDO;
|
|
use InvalidArgumentException;
|
|
|
|
class DBManager
|
|
{
|
|
private $pdo;
|
|
private $entitie;
|
|
public function __construct(Entitie $entitie, object $config)
|
|
{
|
|
$this->entitie = $entitie;
|
|
$dsn = "{$config->driver}:host={$config->host};port={$config->port};dbname={$config->dbname}";
|
|
$username = $config->username;
|
|
$password = $config->password;
|
|
$options = isset($config->options) ? (array)$config->options : [];
|
|
$this->pdo = new PDO($dsn, $username, $password, $options);
|
|
}
|
|
public function create_table()
|
|
{
|
|
return $this->pdo->query($this->entitie->get_create_table());
|
|
}
|
|
public function delete_table()
|
|
{
|
|
return $this->pdo->query($this->entitie->get_delete_table());
|
|
}
|
|
public function create(array $params)
|
|
{
|
|
$stmt = $this->pdo->prepare($this->entitie->get_create());
|
|
return $stmt->execute($params);
|
|
}
|
|
public function select(array $params)
|
|
{
|
|
$sql = str_replace("*", $params[0], $this->entitie->get_select());
|
|
array_shift($params);
|
|
$stmt = $this->pdo->prepare($sql);
|
|
$stmt->execute($params);
|
|
return $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
}
|
|
public function select_all(array $params = ["*"])
|
|
{
|
|
$sql = str_replace("*", $params[0], $this->entitie->get_select_all());
|
|
$stmt = $this->pdo->prepare($sql);
|
|
$stmt->execute([]);
|
|
return $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
}
|
|
public function select_by_condition(array $params)
|
|
{
|
|
$sql = str_replace("*", $params[0], $this->entitie->get_select_all());
|
|
array_shift($params);
|
|
$stmt = $this->pdo->prepare($sql . ' WHERE ' . $params[0]);
|
|
$stmt->execute([]);
|
|
return $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
}
|
|
public function remove(array $params)
|
|
{
|
|
$stmt = $this->pdo->prepare($this->entitie->get_remove());
|
|
return $stmt->execute($params);
|
|
}
|
|
public function clear(array $params)
|
|
{
|
|
$stmt = $this->pdo->prepare($this->entitie->get_clear());
|
|
return $stmt->execute($params);
|
|
}
|
|
public function create_all(array $params)
|
|
{
|
|
$create_all = $this->entitie->get_create_all();
|
|
$stmt = $this->pdo->prepare($create_all(sizeof($params)));
|
|
$params_merge = array_merge(...$params);
|
|
return $stmt->execute($params_merge);
|
|
}
|
|
public function update(array $params)
|
|
{
|
|
$update = $this->entitie->get_update();
|
|
$stmt = $this->pdo->prepare($update($params[0]));
|
|
array_shift($params);
|
|
$stmt->execute($params);
|
|
return $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
}
|
|
public function update_by_condition(array $params)
|
|
{
|
|
$update = $this->entitie->get_update_by_condition();
|
|
$stmt = $this->pdo->prepare($update($params[0]));
|
|
array_shift($params);
|
|
$stmt->execute($params);
|
|
return $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
}
|
|
|
|
}
|