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