Completed
Push — master ( 4934e2...fe7b53 )
by Sébastien
01:56
created

MysqlAdapter::getCurrentSchema()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 9
ccs 0
cts 9
cp 0
rs 9.6667
cc 3
eloc 6
nc 2
nop 0
crap 12
1
<?php
2
3
namespace Soluble\DbWrapper\Adapter;
4
5
use Soluble\DbWrapper\Exception;
6
use ArrayObject;
7
8
class MysqlAdapter implements AdapterInterface
9
{
10
11
    const DRIVER_TYPE_PDO = 'pdo';
12
    const DRIVER_TYPE_MYSQLI = 'mysqli';
13
14
    /**
15
     * @var string
16
     */
17
    protected $driver;
18
19
    /**
20
     *
21
     * @var \mysqli|null
22
     */
23
    protected $mysqli;
24
25
    /**
26
     *
27
     * @var \PDO|null
28
     */
29
    protected $pdo;
30
31
    /**
32
     *
33
     * @var string
34
     */
35
    protected $type;
36
37
    /**
38
     * Constructor
39
     *
40
     * @throws Exception\InvalidArgumentException
41
     * @param \mysqli|\PDO $conn
42
     */
43
    public function __construct($conn)
44
    {
45
        if ($conn instanceof \mysqli) {
46
            $this->mysqli = $conn;
47
            $this->type = self::DRIVER_TYPE_MYSQLI;
48
        } elseif ($conn instanceof \PDO && $conn->getAttribute(\PDO::ATTR_DRIVER_NAME) == 'mysql') {
49
            $this->pdo = $conn;
50
            $this->type = self::DRIVER_TYPE_PDO;
51
        } else {
52
            $msg = "MysqlAdapter requires connection to be either 'pdo:mysql' or 'mysqli'";
53
            throw new Exception\InvalidArgumentException($msg);
54
        }
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function getCurrentSchema()
61
    {
62
        $query = 'SELECT DATABASE() as current_schema';
63
        $results = $this->query($query);
64
        if (count($results) == 0 || $results[0]['current_schema'] === null) {
65
            return false;
66
        }
67
        return $results[0]['current_schema'];
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function quoteValue($value)
74
    {
75
        if ($this->type == self::DRIVER_TYPE_MYSQLI) {
76
            $quoted = "'" . $this->mysqli->real_escape_string($value) . "'";
77
        } else {
78
            $quoted = $this->pdo->quote($value);
79
        }
80
        return $quoted;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function query($query)
87
    {
88
        if ($this->type == self::DRIVER_TYPE_MYSQLI) {
89
            $results = $this->queryMysqli($query);
90
        } else {
91
            $results = $this->queryPDO($query);
92
        }
93
        return $results;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function execute($query)
100
    {
101
        if ($this->type == self::DRIVER_TYPE_MYSQLI) {
102
            $this->queryMysqli($query);
103
        } else {
104
            $this->executePDO($query);
105
        }
106
    }
107
108
109
    /**
110
     * Execute on PDO
111
     *
112
     * @param string $query
113
     * @throws Exception\InvalidArgumentException
114
     */
115
    protected function executePDO($query)
116
    {
117
        try {
118
            $ret = $this->pdo->exec($query);
119
            if ($ret === false) {
120
                throw new Exception\InvalidArgumentException("Cannot execute [$query].");
121
            }
122
        } catch (Exception\InvalidArgumentException $e) {
123
            throw $e;
124
        } catch (\Exception $e) {
125
            $msg = "PDOException : {$e->getMessage()} [$query]";
126
            throw new Exception\InvalidArgumentException($msg);
127
        }
128
    }
129
130
131
    /**
132
     * Query on PDO
133
     * @param string $query
134
     * @throws Exception\InvalidArgumentException
135
     * @return ArrayObject
136
     */
137
    protected function queryPDO($query)
138
    {
139
        try {
140
            $stmt = $this->pdo->query($query, \PDO::FETCH_ASSOC);
141
            if ($stmt === false) {
142
                throw new Exception\InvalidArgumentException("Query cannot be executed [$query].");
143
            }
144
            $results = new ArrayObject();
145
            foreach ($stmt as $row) {
146
                $results->append($row);
147
            }
148
        } catch (Exception\InvalidArgumentException $e) {
149
            throw $e;
150
        } catch (\Exception $e) {
151
            $msg = "PDOException : {$e->getMessage()} [$query]";
152
            throw new Exception\InvalidArgumentException($msg);
153
        }
154
        return $results;
155
    }
156
157
    /**
158
     * Query on Mysqli
159
     *
160
     * @throws Exception\InvalidArgumentException
161
     * @param string $query
162
     * @return ArrayObject
163
     */
164
    protected function queryMysqli($query)
165
    {
166
        try {
167
            $r = $this->mysqli->query($query);
168
169
            $results = new ArrayObject();
170
171
            if ($r === false) {
172
                throw new Exception\InvalidArgumentException("Query cannot be executed [$query].");
173
            } elseif ($r !== true && !$r instanceof \mysqli_result) {
174
                throw new Exception\InvalidArgumentException("Query didn't return any result [$query].");
175
            } elseif ($r instanceof \mysqli_result) {
176
                while ($row = $r->fetch_assoc()) {
177
                    $results->append($row);
178
                }
179
            }
180
181
        } catch (Exception\InvalidArgumentException $e) {
182
            throw $e;
183
        } catch (\Exception $e) {
184
            $msg = "MysqliException: {$e->getMessage()} [$query]";
185
            throw new Exception\InvalidArgumentException($msg);
186
        }
187
        return $results;
188
    }
189
}
190