Passed
Branch main (5746ae)
by Sammy
02:23
created

Connection::useDatabase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 2
b 0
f 0
1
<?php
2
3
/**
4
 * Simple PDO connection wrapper for Crudites
5
 *
6
 * Sets defaults: ERRMODE_EXCEPTION, CASE_NATURAL, FETCH_ASSOC, required by Crudites
7
 * Sets prefered fetch mode: associative array
8
 *
9
 * Throws \PDOException when DSN is wrong
10
 */
11
12
namespace HexMakina\Crudites;
13
14
use HexMakina\BlackBox\Database\ConnectionInterface;
15
16
class Connection implements ConnectionInterface
17
{
18
    private $database_name = null;
19
    private $pdo;
20
    private $dsn;
21
22
    private static $driver_default_options = [
23
    \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
24
    \PDO::ATTR_CASE => \PDO::CASE_NATURAL,
25
    \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC
26
    ];
27
28
29
    public function __construct($dsn, $username = '', $password = '', $driver_options = [])
30
    {
31
        $this->validateDSN($dsn); //throws \PDOException
32
        $this->dsn = $dsn;
33
34
        if (isset($driver_options[\PDO::ATTR_ERRMODE])) {
35
            unset($driver_options[\PDO::ATTR_ERRMODE]); // mandatory for CRUDITES error handler
36
        }
37
38
        $driver_options = array_merge(self::$driver_default_options, $driver_options);
39
        $this->pdo = new \PDO($dsn, $username, $password, $driver_options);
40
    }
41
42
    public function driverName()
43
    {
44
        return $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME);
45
    }
46
47
    public function databaseName(): string
48
    {
49
        return $this->database_name;
50
    }
51
52
    public function prepare($sql_statement, $options = [])
53
    {
54
        return $this->pdo->prepare($sql_statement, $options);
55
    }
56
57
    public function transact(): bool
58
    {
59
        return $this->pdo->beginTransaction();
60
    }
61
62
    public function commit(): bool
63
    {
64
        return $this->pdo->commit();
65
    }
66
67
    public function rollback(): bool
68
    {
69
        return $this->pdo->rollback();
70
    }
71
72
    public function errorInfo(): array
73
    {
74
        return $this->pdo->errorInfo();
75
    }
76
77
    public function lastInsertId($name = null)
78
    {
79
        return $this->pdo->lastInsertId($name);
80
    }
81
82
    public function errorCode(): ?string
83
    {
84
        return $this->pdo->errorCode();
85
    }
86
87
    private function validateDSN($dsn)
88
    {
89
        $matches = [];
90
        if (preg_match('/^([a-z]+)\:/', $dsn, $matches) !== 1) {
91
            throw new \PDOException('DSN_NO_DRIVER');
92
        }
93
94
        if (!in_array($matches[1], \PDO::getAvailableDrivers(), true)) {
95
            throw new \PDOException('DSN_UNAVAILABLE_DRIVER');
96
        }
97
98
        if (preg_match('/dbname=(.+);/', $dsn, $matches) !== 1) {
99
            throw new \PDOException('DSN_NO_DBNAME');
100
        }
101
102
        $this->database_name = $matches[1];
103
104
        return true;
105
    }
106
107
    public function query($sql_statement, $fetch_mode = null, $fetch_col_num = null)
108
    {
109
        if (is_null($fetch_mode)) {
110
            return $this->pdo->query($sql_statement);
111
        }
112
113
        return $this->pdo->query($sql_statement, $fetch_mode, $fetch_col_num);
114
    }
115
116
    public function alter($sql_statement)
117
    {
118
        return $this->pdo->exec($sql_statement);
119
    }
120
121
    public function useDatabase($name)
122
    {
123
        $this->database_name = $name;
124
        $this->pdo->query(sprintf('USE `%s`;', $name));
125
    }
126
}
127