1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package CleverStyle Framework |
4
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
5
|
|
|
* @copyright Copyright (c) 2011-2016, Nazar Mokrynskyi |
6
|
|
|
* @license MIT License, see license.txt |
7
|
|
|
*/ |
8
|
|
|
namespace cs\DB; |
9
|
|
|
use |
10
|
|
|
mysqli_result; |
11
|
|
|
|
12
|
|
|
class MySQLi extends _Abstract { |
13
|
|
|
/** |
14
|
|
|
* @var \MySQLi Instance of DB connection |
15
|
|
|
*/ |
16
|
|
|
protected $instance; |
17
|
|
|
/** |
18
|
|
|
* @inheritdoc |
19
|
|
|
*/ |
20
|
18 |
|
function __construct ($database, $user = '', $password = '', $host = 'localhost', $prefix = '') { |
21
|
18 |
|
$start = microtime(true); |
22
|
|
|
/** |
23
|
|
|
* Parsing of $host variable, detecting port and persistent connection |
24
|
|
|
*/ |
25
|
18 |
|
list($host, $port) = $this->get_host_and_port($host); |
26
|
18 |
|
$this->instance = new \MySQLi($host, $user, $password, $database, $port); |
27
|
18 |
|
if (!is_object($this->instance) || $this->instance->connect_errno) { |
28
|
1 |
|
return; |
29
|
|
|
} |
30
|
18 |
|
$this->database = $database; |
31
|
|
|
/** |
32
|
|
|
* Changing DB charset if necessary |
33
|
|
|
*/ |
34
|
18 |
|
if ($this->instance->character_set_name() != 'utf8mb4') { |
35
|
18 |
|
$this->instance->set_charset('utf8mb4'); |
36
|
|
|
} |
37
|
18 |
|
$this->connected = true; |
38
|
18 |
|
$this->connecting_time = microtime(true) - $start; |
39
|
18 |
|
$this->db_type = 'mysql'; |
40
|
18 |
|
$this->prefix = $prefix; |
41
|
18 |
|
} |
42
|
|
|
/** |
43
|
|
|
* Parse host string into host and port separately |
44
|
|
|
* |
45
|
|
|
* Understands `p:` prefix for persistent connections |
46
|
|
|
* |
47
|
|
|
* @param string $host_string |
48
|
|
|
* |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
18 |
|
protected function get_host_and_port ($host_string) { |
52
|
18 |
|
$host = explode(':', $host_string); |
53
|
18 |
|
$port = ini_get('mysqli.default_port') ?: 3306; |
54
|
18 |
|
switch (count($host)) { |
55
|
18 |
|
case 1: |
56
|
18 |
|
$host = $host[0]; |
57
|
18 |
|
break; |
58
|
1 |
|
case 2: |
59
|
1 |
|
if ($host[0] == 'p') { |
60
|
1 |
|
$host = "$host[0]:$host[1]"; |
61
|
|
|
} else { |
62
|
1 |
|
list($host, $port) = $host; |
63
|
|
|
} |
64
|
1 |
|
break; |
65
|
1 |
|
case 3: |
66
|
1 |
|
$port = $host[2]; |
67
|
1 |
|
$host = "$host[0]:$host[1]"; |
68
|
|
|
} |
69
|
18 |
|
return [$host, $port]; |
70
|
|
|
} |
71
|
|
|
/** |
72
|
|
|
* @inheritdoc |
73
|
|
|
* |
74
|
|
|
* @return false|mysqli_result |
75
|
|
|
*/ |
76
|
18 |
|
protected function q_internal ($query) { |
77
|
18 |
|
if (!$query) { |
78
|
1 |
|
return false; |
79
|
|
|
} |
80
|
18 |
|
$result = $this->instance->query($query); |
81
|
|
|
// In case of MySQL Client error - try to fix everything, but only once |
82
|
|
|
if ( |
83
|
18 |
|
!$result && |
84
|
18 |
|
$this->instance->errno >= 2000 && |
85
|
18 |
|
$this->instance->ping() |
86
|
|
|
) { |
87
|
|
|
$result = $this->instance->query($query); |
88
|
|
|
} |
89
|
18 |
|
return $result; |
90
|
|
|
} |
91
|
|
|
/** |
92
|
|
|
* @inheritdoc |
93
|
|
|
*/ |
94
|
5 |
|
protected function q_multi_internal ($query) { |
95
|
5 |
|
$result = true; |
96
|
5 |
|
foreach ($query as $q) { |
97
|
5 |
|
$result = $result && $this->q_internal($q); |
98
|
|
|
} |
99
|
5 |
|
return $result; |
100
|
|
|
} |
101
|
|
|
/** |
102
|
|
|
* @inheritdoc |
103
|
|
|
* |
104
|
|
|
* @param false|mysqli_result $query_result |
105
|
|
|
*/ |
106
|
17 |
|
function f ($query_result, $single_column = false, $array = false, $indexed = false) { |
107
|
17 |
|
if (!($query_result instanceof mysqli_result)) { |
108
|
1 |
|
return false; |
109
|
|
|
} |
110
|
17 |
|
$result_type = $single_column || $indexed ? MYSQLI_NUM : MYSQLI_ASSOC; |
111
|
17 |
|
if ($array) { |
112
|
16 |
|
$result = []; |
113
|
16 |
|
while ($current = $query_result->fetch_array($result_type)) { |
114
|
16 |
|
$result[] = $single_column ? $current[0] : $current; |
115
|
|
|
} |
116
|
16 |
|
$this->free($query_result); |
117
|
16 |
|
return $result; |
118
|
|
|
} |
119
|
16 |
|
$result = $query_result->fetch_array($result_type); |
120
|
16 |
|
if ($result === null) { |
121
|
13 |
|
$result = false; |
122
|
|
|
} |
123
|
16 |
|
return $single_column && $result ? $result[0] : $result; |
124
|
|
|
} |
125
|
|
|
/** |
126
|
|
|
* @inheritdoc |
127
|
|
|
*/ |
128
|
13 |
|
function id () { |
129
|
13 |
|
return $this->instance->insert_id; |
130
|
|
|
} |
131
|
|
|
/** |
132
|
|
|
* @inheritdoc |
133
|
|
|
*/ |
134
|
1 |
|
function affected () { |
135
|
1 |
|
return $this->instance->affected_rows; |
136
|
|
|
} |
137
|
|
|
/** |
138
|
|
|
* @inheritdoc |
139
|
|
|
* |
140
|
|
|
* @param false|mysqli_result $query_result |
141
|
|
|
*/ |
142
|
16 |
|
function free ($query_result) { |
143
|
16 |
|
if ($query_result instanceof mysqli_result) { |
144
|
16 |
|
$query_result->free(); |
145
|
|
|
} |
146
|
16 |
|
return true; |
147
|
|
|
} |
148
|
|
|
/** |
149
|
|
|
* @inheritdoc |
150
|
|
|
*/ |
151
|
2 |
|
function columns ($table, $like = false) { |
152
|
2 |
|
if (!$table) { |
153
|
1 |
|
return false; |
154
|
|
|
} |
155
|
2 |
|
if ($like) { |
156
|
1 |
|
$like = $this->s($like); |
157
|
1 |
|
$columns = $this->qfas("SHOW COLUMNS FROM `$table` LIKE $like") ?: []; |
158
|
|
|
} else { |
159
|
2 |
|
$columns = $this->qfas("SHOW COLUMNS FROM `$table`") ?: []; |
160
|
|
|
} |
161
|
2 |
|
return $columns; |
162
|
|
|
} |
163
|
|
|
/** |
164
|
|
|
* @inheritdoc |
165
|
|
|
*/ |
166
|
1 |
|
function tables ($like = false) { |
167
|
1 |
|
if ($like) { |
168
|
1 |
|
$like = $this->s($like); |
169
|
1 |
|
return $this->qfas("SHOW TABLES FROM `$this->database` LIKE $like") ?: []; |
170
|
|
|
} else { |
171
|
1 |
|
return $this->qfas("SHOW TABLES FROM `$this->database`") ?: []; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
/** |
175
|
|
|
* @inheritdoc |
176
|
|
|
*/ |
177
|
18 |
|
protected function s_internal ($string, $single_quotes_around) { |
178
|
18 |
|
$return = $this->instance->real_escape_string($string); |
179
|
18 |
|
return $single_quotes_around ? "'$return'" : $return; |
180
|
|
|
} |
181
|
|
|
/** |
182
|
|
|
* @inheritdoc |
183
|
|
|
*/ |
184
|
1 |
|
function server () { |
185
|
1 |
|
return $this->instance->server_info; |
186
|
|
|
} |
187
|
|
|
/** |
188
|
|
|
* @inheritdoc |
189
|
|
|
*/ |
190
|
1 |
|
function __destruct () { |
191
|
1 |
|
if ($this->connected && is_object($this->instance)) { |
192
|
1 |
|
$this->instance->close(); |
193
|
1 |
|
$this->connected = false; |
194
|
|
|
} |
195
|
1 |
|
} |
196
|
|
|
} |
197
|
|
|
|