1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Jaeger |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2015-2016, mithra62 |
6
|
|
|
* @link http://jaeger-app.com |
7
|
|
|
* @version 1.0 |
8
|
|
|
* @filesource ./Db/Mysqli.php |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace JaegerApp\Db; |
12
|
|
|
|
13
|
|
|
use voku\db\DB as vDb; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Jaeger - MySQLi Database Object |
17
|
|
|
* |
18
|
|
|
* Wrapper for the MySQLi database interface |
19
|
|
|
* |
20
|
|
|
* @package Database |
21
|
|
|
* @author Eric Lamb <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class Mysqli implements DbInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* The primary table we're working with |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $table = null; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Any filtering for a WHERE SQL clause |
33
|
|
|
* @var mixed |
34
|
|
|
*/ |
35
|
|
|
protected $where = false; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The database connection credentials |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
protected $credentials = array(); |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The database object we're piggybacking on |
45
|
|
|
* @var \voku\db\DB |
46
|
|
|
*/ |
47
|
|
|
protected $db = null; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Changes the databse connection to use a new database |
51
|
|
|
* @param string $db_name |
52
|
|
|
*/ |
53
|
|
|
public function setDbName($db_name) |
54
|
|
|
{ |
55
|
|
|
@mysqli_select_db($this->getDb()->getLink(), $db_name); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* (non-PHPdoc) |
60
|
|
|
* @see \JaegerApp\Db\DbInterface::select() |
61
|
|
|
*/ |
62
|
|
|
public function select($table, $where = '1=1') |
63
|
|
|
{ |
64
|
|
|
$this->table = $table; |
65
|
|
|
$this->where = $where; |
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* (non-PHPdoc) |
71
|
|
|
* @see \JaegerApp\Db\DbInterface::get() |
72
|
|
|
*/ |
73
|
|
|
public function get() |
74
|
|
|
{ |
75
|
|
|
$data = $this->getDb()->select($this->table, $this->where); |
76
|
|
|
return $data->fetchAllArray(); |
77
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* (non-PHPdoc) |
82
|
|
|
* @see \JaegerApp\Db\DbInterface::query() |
83
|
|
|
*/ |
84
|
|
|
public function query($sql = '', $return = false) |
85
|
|
|
{ |
86
|
|
|
$data = $this->getDb()->query($sql, $return); |
87
|
|
|
if( $data instanceof \voku\db\Result ) |
88
|
|
|
{ |
89
|
|
|
return $data->fetchAllArray(); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* (non-PHPdoc) |
95
|
|
|
* @see \JaegerApp\Db\DbInterface::getTableStatus() |
96
|
|
|
*/ |
97
|
|
|
public function getTableStatus() |
98
|
|
|
{ |
99
|
|
|
$tables = $this->query("SHOW TABLE STATUS", true); |
100
|
|
|
return $tables; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* (non-PHPdoc) |
105
|
|
|
* @see \JaegerApp\Db\DbInterface::getCreateTable() |
106
|
|
|
*/ |
107
|
|
|
public function getCreateTable($table, $if_not_exists = false) |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
$sql = sprintf('SHOW CREATE TABLE `%s` ;', $table); |
110
|
|
|
$statement = $this->query($sql, true); |
111
|
|
|
$string = false; |
112
|
|
|
if (! empty($statement['0']['Create Table'])) { |
113
|
|
|
$string = $statement['0']['Create Table']; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if ($if_not_exists) { |
117
|
|
|
$replace = substr($string, 0, 12); |
118
|
|
|
if ($replace == 'CREATE TABLE') { |
119
|
|
|
$string = str_replace('CREATE TABLE', 'CREATE TABLE IF NOT EXISTS ', $string); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $string; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* (non-PHPdoc) |
128
|
|
|
* @see \JaegerApp\Db\DbInterface::clear() |
129
|
|
|
*/ |
130
|
|
|
public function clear() |
131
|
|
|
{ |
132
|
|
|
$this->table = null; |
133
|
|
|
$this->where = null; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* (non-PHPdoc) |
138
|
|
|
* @see \JaegerApp\Db\DbInterface::totalRows() |
139
|
|
|
*/ |
140
|
|
|
public function totalRows($table) |
|
|
|
|
141
|
|
|
{ |
142
|
|
|
$sql = sprintf('SELECT COUNT(*) AS count FROM `%s`', $table); |
143
|
|
|
$statement = $this->query($sql, true); |
144
|
|
|
if ($statement) { |
145
|
|
|
if (isset($statement['0']['count'])) { |
146
|
|
|
return $statement['0']['count']; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return '0'; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* (non-PHPdoc) |
155
|
|
|
* @see \JaegerApp\Db\DbInterface::getColumnns() |
156
|
|
|
*/ |
157
|
|
View Code Duplication |
public function getColumns($table) |
|
|
|
|
158
|
|
|
{ |
159
|
|
|
$sql = sprintf('SHOW COLUMNS FROM `%s`', $table); |
160
|
|
|
$statement = $this->query($sql, true); |
161
|
|
|
if ($statement) { |
162
|
|
|
return $statement; |
163
|
|
|
} |
164
|
|
|
return array(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* (non-PHPdoc) |
169
|
|
|
* @see \JaegerApp\Db\DbInterface::escape() |
170
|
|
|
*/ |
171
|
|
|
public function escape($string) |
172
|
|
|
{ |
173
|
|
|
return $this->getDb()->escape($string); |
|
|
|
|
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* (non-PHPdoc) |
178
|
|
|
* @see \JaegerApp\Db\DbInterface::getAllTables() |
179
|
|
|
*/ |
180
|
|
|
public function getAllTables() |
181
|
|
|
{ |
182
|
|
|
return $this->getDb()->getAllTables(); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* (non-PHPdoc) |
187
|
|
|
* @see \JaegerApp\Db\DbInterface::insert() |
188
|
|
|
*/ |
189
|
|
|
public function insert($table, array $data = array()) |
190
|
|
|
{ |
191
|
|
|
return $this->getDb()->insert($table, $data); |
|
|
|
|
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* (non-PHPdoc) |
196
|
|
|
* @see \JaegerApp\Db\DbInterface::update() |
197
|
|
|
*/ |
198
|
|
|
public function update($table, $data, $where) |
199
|
|
|
{ |
200
|
|
|
return $this->getDb()->update($table, $data, $where); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* |
205
|
|
|
* @param array $credentials |
206
|
|
|
* @return \JaegerApp\Db\Mysqli |
207
|
|
|
*/ |
208
|
|
|
public function setCredentials(array $credentials) |
209
|
|
|
{ |
210
|
|
|
$this->credentials = $credentials; |
211
|
|
|
return $this; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* (non-PHPdoc) |
216
|
|
|
* @see \JaegerApp\Db\DbInterface::getDb() |
217
|
|
|
*/ |
218
|
|
|
public function getDb($force = false) |
219
|
|
|
{ |
220
|
|
|
if (is_null($this->db)) { |
221
|
|
|
|
222
|
|
|
$this->db = vDb::getInstance($this->credentials['host'], $this->credentials['user'], $this->credentials['password'], $this->credentials['database']); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
return $this->db; |
226
|
|
|
} |
227
|
|
|
} |
If you suppress an error, we recommend checking for the error condition explicitly: