Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 56 |
| App\Models\DatabaseMySql | |
0.00% |
0 / 1 |
|
0.00% |
0 / 9 |
380 | |
0.00% |
0 / 56 |
| __construct | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 4 |
|||
| getInstance | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 5 |
|||
| getDatabase | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 6 |
|||
| queryWithFactory | |
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 10 |
|||
| query | |
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 10 |
|||
| insert | |
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 9 |
|||
| update | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 5 |
|||
| delete | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 5 |
|||
| lastInsertedId | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
| <?php | |
| /* | |
| * To change this license header, choose License Headers in Project Properties. | |
| * To change this template file, choose Tools | Templates | |
| * and open the template in the editor. | |
| */ | |
| namespace App\Models; | |
| use PDO; | |
| use App\Models\ArticleFactory; | |
| /** | |
| * Description of DatabaseMySql | |
| * | |
| * @author lennartbinscheck | |
| */ | |
| class DatabaseMySql implements Interfaces\DatabaseInterface { | |
| private $pdo; | |
| private $connection; | |
| private $configuration; | |
| private $db; | |
| private static $instances = []; | |
| private function __construct($connectionName) { | |
| $this->configuration = new DatabaseConfiguration($connectionName); | |
| $this->connection = new DatabaseConnection($this->configuration); | |
| $this->db = $this->getDatabase(); | |
| } | |
| public static function getInstance($connectionName = 'mysql') { | |
| if (!isset(self::$instances[$connectionName])) { | |
| self::$instances[$connectionName] = new DatabaseMySql($connectionName); | |
| } | |
| return self::$instances[$connectionName]; | |
| } | |
| public function getDatabase() { | |
| if ($this->pdo === NULL) { | |
| $this->pdo = new PDO($this->connection->getDsn(), $this->configuration->getUsername(), $this->configuration->getPassword()); | |
| $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
| } | |
| return $this->pdo; | |
| } | |
| /** | |
| * @param string SQL Query | |
| * @param ArticleFactory $factory | |
| * @param array $placeholders | |
| * @return array | |
| */ | |
| public function queryWithFactory($sql, $factory, $placeholders) { | |
| $statement = $this->db->prepare($sql); | |
| $return = []; | |
| if ($statement) { | |
| $statement->execute($placeholders); | |
| while ($record = $statement->fetch(PDO::FETCH_ASSOC)) { | |
| $return[] = $factory->create($record); | |
| } | |
| } | |
| return $return; | |
| } | |
| /** | |
| * | |
| * @param string $sql | |
| * @param array $placeholders | |
| * @return array | |
| */ | |
| public function query($sql, $placeholders) { | |
| $statement = $this->db->prepare($sql); | |
| $return = []; | |
| if ($statement) { | |
| $statement->execute($placeholders); | |
| while ($record = $statement->fetch(PDO::FETCH_ASSOC)) { | |
| $return[] = $record; | |
| } | |
| } | |
| return $return; | |
| } | |
| /** | |
| * | |
| * @param string $sql | |
| * @param array $placeholders | |
| */ | |
| public function insert($sql, $placeholders) { | |
| $statement = $this->db->prepare($sql); | |
| if ($statement) { | |
| try { | |
| $statement->execute($placeholders); | |
| } catch (\PDOException $exc) { | |
| echo $exc->getTraceAsString(); | |
| } | |
| } | |
| } | |
| /** | |
| * | |
| * @param string $sql | |
| * @param array $placeholders | |
| */ | |
| public function update($sql, $placeholders) { | |
| $statement = $this->db->prepare($sql); | |
| if ($statement) { | |
| $statement->execute($placeholders); | |
| } | |
| } | |
| /** | |
| * | |
| * @param string $sql | |
| * @param array $placeholders | |
| */ | |
| public function delete($sql, $placeholders) { | |
| $statement = $this->db->prepare($sql); | |
| if ($statement) { | |
| $statement->execute($placeholders); | |
| } | |
| } | |
| /** | |
| * | |
| * @return string | |
| */ | |
| public function lastInsertedId() { | |
| return $this->db->lastInsertId(); | |
| } | |
| } |