Current Path : /data/web/virtuals/51568/virtual/www/subdom/srps/src/db/ |
Current File : /data/web/virtuals/51568/virtual/www/subdom/srps/src/db/Database.php |
<?php namespace app\db; use \PDO; use app\exceptions\DBPDOException; class Database { private $options = [ PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ, PDO::ATTR_EMULATE_PREPARES => false, ]; private $pdo; public function __construct(array $config) { $dsn = $config['dsn'] ?? ''; $user = $config['user'] ?? ''; $password = $config['password'] ?? ''; if ($this->pdo) { return; } try { $this->pdo = new PDO($dsn, $user, $password, $this->options); $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (\PDOException $e) { throw new DBPDOException($e->getMessage()); } } public function prepare($sql){ return $this->pdo->prepare($sql); } public function query($sql, ...$params) { try { $navrat = $this->pdo->prepare($sql); $navrat->execute($params); return $navrat; } catch (\PDOException $e) { throw new DBPDOException($e->getMessage()); } } public function commit(){ $this->pdo->commit(); } public function fetchOne($sql, ...$params) { return $this->query($sql, ...$params)->fetch(); } public function fetchOneCommit($sql, ...$params) { $this->pdo->beginTransaction(); $navrat = $this->pdo->prepare($sql); $navrat->execute($params); $navrat = $this->query($sql, ...$params)->fetch(); $this->pdo->commit(); return $navrat; } public function fetchOneAsoc($sql, ...$params) { return $this->query($sql, ...$params)->fetch(PDO::FETCH_ASSOC); } public function fetchAll($sql, ...$params) { return $this->query($sql, ...$params)->fetchAll(); } public function lastInsertId($sequence = null) { return $this->pdo->lastInsertId($sequence); } }