1 | <?php |
||
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 | * @var string |
||
39 | */ |
||
40 | protected $type; |
||
41 | |||
42 | /** |
||
43 | * |
||
44 | * @throws Exception\InvalidArgumentException |
||
45 | * @param \mysqli|\PDO $conn |
||
46 | */ |
||
47 | 33 | public function __construct($conn) |
|
48 | { |
||
49 | 33 | if ($conn instanceof \mysqli) { |
|
50 | 8 | $this->mysqli = $conn; |
|
51 | 8 | $this->type = self::DRIVER_TYPE_MYSQLI; |
|
52 | 33 | } elseif ($conn instanceof \PDO && $conn->getAttribute(\PDO::ATTR_DRIVER_NAME) == 'mysql') { |
|
53 | 28 | $this->pdo = $conn; |
|
54 | 28 | $this->type = self::DRIVER_TYPE_PDO; |
|
55 | 28 | } else { |
|
56 | 1 | $msg = "MysqlConnectionAdapter requires connection to be either 'pdo:mysql' or 'mysqli'"; |
|
57 | 1 | throw new Exception\InvalidArgumentException($msg); |
|
58 | } |
||
59 | 33 | } |
|
60 | |||
61 | /** |
||
62 | * Return current schema name |
||
63 | * @return string|false |
||
64 | */ |
||
65 | 26 | public function getCurrentSchema() |
|
66 | { |
||
67 | 26 | $query = 'SELECT DATABASE() as current_schema'; |
|
68 | 26 | $results = $this->query($query); |
|
69 | 26 | if (count($results) == 0 || $results[0]['current_schema'] === null) { |
|
70 | 2 | return false; |
|
71 | } |
||
72 | 26 | return $results[0]['current_schema']; |
|
73 | } |
||
74 | |||
75 | /** |
||
76 | * |
||
77 | * @param string $value |
||
78 | * @return string |
||
79 | */ |
||
80 | 8 | public function quoteValue($value) |
|
81 | { |
||
82 | 8 | if ($this->type == self::DRIVER_TYPE_MYSQLI) { |
|
83 | 1 | $quoted = "'" . $this->mysqli->real_escape_string($value) . "'"; |
|
84 | 1 | } else { |
|
85 | 7 | $quoted = $this->pdo->quote($value); |
|
86 | } |
||
87 | 8 | return $quoted; |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * Execute query and return query as an ArrayObject |
||
92 | * |
||
93 | * @param string $query |
||
94 | * @return ArrayObject |
||
95 | */ |
||
96 | 28 | public function query($query) |
|
105 | |||
106 | /** |
||
107 | * Execute special sql like set names... |
||
108 | * @param string $query |
||
109 | * @return void |
||
110 | */ |
||
111 | 10 | public function execute($query) |
|
119 | |||
120 | /** |
||
121 | * |
||
122 | * @param string $query |
||
123 | * @return void |
||
124 | */ |
||
125 | 8 | protected function executePDO($query) |
|
139 | |||
140 | |||
141 | /** |
||
142 | * |
||
143 | * @param string $query |
||
144 | * @return ArrayObject |
||
145 | */ |
||
146 | 26 | protected function queryPDO($query) |
|
165 | |||
166 | /** |
||
167 | * |
||
168 | * @param string $query |
||
169 | * @return ArrayObject |
||
170 | */ |
||
171 | 6 | protected function queryMysqli($query) |
|
196 | } |
||
197 |