|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Soluble\Schema\Source\Mysql; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Wrapper for basic mysqli/pdo usage |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
use Soluble\Schema\Exception; |
|
10
|
|
|
use ArrayObject; |
|
11
|
|
|
|
|
12
|
|
|
class MysqlConnectionAdapter |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
const DRIVER_TYPE_PDO = 'pdo'; |
|
16
|
|
|
const DRIVER_TYPE_MYSQLI = 'mysqli'; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $driver; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* |
|
26
|
|
|
* @var \mysqli|null |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $mysqli; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* |
|
32
|
|
|
* @var \PDO|null |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $pdo; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* |
|
38
|
|
|
* @throws Exception\InvalidArgumentException |
|
39
|
|
|
* @param \mysqli|\PDO $conn |
|
40
|
|
|
*/ |
|
41
|
26 |
|
public function __construct($conn) |
|
42
|
|
|
{ |
|
43
|
26 |
|
if ($conn instanceof \mysqli) { |
|
44
|
4 |
|
$this->mysqli = $conn; |
|
45
|
4 |
|
$this->type = self::DRIVER_TYPE_MYSQLI; |
|
|
|
|
|
|
46
|
26 |
|
} elseif ($conn instanceof \PDO && $conn->getAttribute(\PDO::ATTR_DRIVER_NAME) == 'mysql') { |
|
47
|
25 |
|
$this->pdo = $conn; |
|
48
|
25 |
|
$this->type = self::DRIVER_TYPE_PDO; |
|
49
|
25 |
|
} else { |
|
50
|
|
|
$msg = "MysqlConnectionAdapter requires connection to be either 'pdo:mysql' or 'mysqli'"; |
|
51
|
|
|
throw new Exception\InvalidArgumentException($msg); |
|
52
|
|
|
} |
|
53
|
26 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Return current schema name |
|
57
|
|
|
* @return string|false |
|
58
|
|
|
*/ |
|
59
|
26 |
|
public function getCurrentSchema() |
|
60
|
|
|
{ |
|
61
|
26 |
|
$query = 'SELECT DATABASE() as current_schema'; |
|
62
|
26 |
|
$results = $this->query($query); |
|
63
|
26 |
|
if (count($results) == 0) { |
|
64
|
|
|
return false; |
|
65
|
|
|
} |
|
66
|
26 |
|
return $results[0]['current_schema']; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $value |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
6 |
|
public function quoteValue($value) |
|
75
|
|
|
{ |
|
76
|
6 |
|
if ($this->type == self::DRIVER_TYPE_MYSQLI) { |
|
77
|
|
|
$quoted = "'" . $this->mysqli->real_escape_string($value) . "'"; |
|
78
|
|
|
} else { |
|
79
|
6 |
|
$quoted = $this->pdo->quote($value); |
|
80
|
|
|
} |
|
81
|
6 |
|
return $quoted; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Execute query and return query as an ArrayObject |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $query |
|
88
|
|
|
* @return ArrayObject |
|
89
|
|
|
*/ |
|
90
|
26 |
|
public function query($query) |
|
91
|
|
|
{ |
|
92
|
26 |
|
if ($this->type == self::DRIVER_TYPE_MYSQLI) { |
|
93
|
4 |
|
$results = $this->executeMysqli($query); |
|
94
|
4 |
|
} else { |
|
95
|
25 |
|
$results = $this->executePDO($query); |
|
96
|
|
|
} |
|
97
|
26 |
|
return $results; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* |
|
102
|
|
|
* @param string $query |
|
103
|
|
|
* @return ArrayObject |
|
104
|
|
|
*/ |
|
105
|
25 |
|
protected function executePDO($query) |
|
106
|
|
|
{ |
|
107
|
|
|
|
|
108
|
|
|
try { |
|
109
|
25 |
|
$stmt = $this->pdo->query($query, \PDO::FETCH_ASSOC); |
|
110
|
25 |
|
if (!$stmt) { |
|
111
|
|
|
throw new Exception\InvalidArgumentException("Query cannot be executed [$query]."); |
|
112
|
|
|
} |
|
113
|
25 |
|
$results = new ArrayObject(); |
|
114
|
25 |
|
foreach ($stmt as $row) { |
|
115
|
25 |
|
$results->append($row); |
|
116
|
25 |
|
} |
|
117
|
25 |
|
} catch (Exception\InvalidArgumentException $e) { |
|
118
|
|
|
throw $e; |
|
119
|
|
|
} catch (\Exception $e) { |
|
120
|
|
|
$msg = "PDOException : {$e->getMessage()} [$query]"; |
|
121
|
|
|
throw new Exception\InvalidArgumentException($msg); |
|
122
|
|
|
} |
|
123
|
25 |
|
return $results; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* |
|
128
|
|
|
* @param string $query |
|
129
|
|
|
* @return ArrayObject |
|
130
|
|
|
*/ |
|
131
|
4 |
|
protected function executeMysqli($query) |
|
132
|
|
|
{ |
|
133
|
|
|
try { |
|
134
|
4 |
|
$r = $this->mysqli->query($query); |
|
135
|
4 |
|
if (!$r) { |
|
136
|
|
|
throw new Exception\InvalidArgumentException("Query cannot be executed [$query]."); |
|
137
|
4 |
|
} elseif (!$r instanceof \mysqli_result) { |
|
138
|
|
|
throw new Exception\InvalidArgumentException("Query didn't return any result [$query]."); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
4 |
|
$results = new ArrayObject(); |
|
142
|
|
|
|
|
143
|
4 |
|
while ($row = $r->fetch_assoc()) { |
|
144
|
4 |
|
$results->append($row); |
|
145
|
4 |
|
} |
|
146
|
|
|
|
|
147
|
4 |
|
} catch (Exception\InvalidArgumentException $e) { |
|
148
|
|
|
throw $e; |
|
149
|
|
|
} catch (\Exception $e) { |
|
150
|
|
|
$msg = "MysqliException: {$e->getMessage()} [$query]"; |
|
151
|
|
|
throw new Exception\InvalidArgumentException($msg); |
|
152
|
|
|
} |
|
153
|
4 |
|
return $results; |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: