@@ -19,396 +19,396 @@ |
||
19 | 19 | */ |
20 | 20 | class Db extends Generic implements Db_Interface |
21 | 21 | { |
22 | - /* public: this is an api revision, not a CVS revision. */ |
|
23 | - public $type = 'pgsql'; |
|
24 | - public $port = ''; |
|
25 | - public $defaultPort = '5432'; |
|
26 | - |
|
27 | - |
|
28 | - /** |
|
29 | - * adds if not blank |
|
30 | - * |
|
31 | - * @param string $add the value to set |
|
32 | - * @param string $me the key/field to set the value for |
|
33 | - * @param false|string $quote optional indicate the value needs quoted |
|
34 | - * @return string |
|
35 | - */ |
|
36 | - public function ifadd($add, $me, $quote = false) |
|
37 | - { |
|
38 | - if ('' != $add) { |
|
39 | - return ' '.$me.($quote === false ? '' : $quote).$add.($quote === false ? '' : $quote); |
|
40 | - } |
|
41 | - return ''; |
|
42 | - } |
|
43 | - |
|
44 | - /** |
|
45 | - * @param $string |
|
46 | - * @return string |
|
47 | - */ |
|
48 | - public function real_escape($string = '') |
|
49 | - { |
|
50 | - return $this->escape($string); |
|
51 | - } |
|
52 | - |
|
53 | - /** |
|
54 | - * alias function of select_db, changes the database we are working with. |
|
55 | - * |
|
56 | - * @param string $database the name of the database to use |
|
57 | - * @return void |
|
58 | - */ |
|
59 | - public function useDb($database) |
|
60 | - { |
|
61 | - $this->selectDb($database); |
|
62 | - } |
|
63 | - |
|
64 | - /** |
|
65 | - * changes the database we are working with. |
|
66 | - * |
|
67 | - * @param string $database the name of the database to use |
|
68 | - * @return void |
|
69 | - */ |
|
70 | - public function selectDb($database) |
|
71 | - { |
|
72 | - /*if ($database != $this->database) { |
|
22 | + /* public: this is an api revision, not a CVS revision. */ |
|
23 | + public $type = 'pgsql'; |
|
24 | + public $port = ''; |
|
25 | + public $defaultPort = '5432'; |
|
26 | + |
|
27 | + |
|
28 | + /** |
|
29 | + * adds if not blank |
|
30 | + * |
|
31 | + * @param string $add the value to set |
|
32 | + * @param string $me the key/field to set the value for |
|
33 | + * @param false|string $quote optional indicate the value needs quoted |
|
34 | + * @return string |
|
35 | + */ |
|
36 | + public function ifadd($add, $me, $quote = false) |
|
37 | + { |
|
38 | + if ('' != $add) { |
|
39 | + return ' '.$me.($quote === false ? '' : $quote).$add.($quote === false ? '' : $quote); |
|
40 | + } |
|
41 | + return ''; |
|
42 | + } |
|
43 | + |
|
44 | + /** |
|
45 | + * @param $string |
|
46 | + * @return string |
|
47 | + */ |
|
48 | + public function real_escape($string = '') |
|
49 | + { |
|
50 | + return $this->escape($string); |
|
51 | + } |
|
52 | + |
|
53 | + /** |
|
54 | + * alias function of select_db, changes the database we are working with. |
|
55 | + * |
|
56 | + * @param string $database the name of the database to use |
|
57 | + * @return void |
|
58 | + */ |
|
59 | + public function useDb($database) |
|
60 | + { |
|
61 | + $this->selectDb($database); |
|
62 | + } |
|
63 | + |
|
64 | + /** |
|
65 | + * changes the database we are working with. |
|
66 | + * |
|
67 | + * @param string $database the name of the database to use |
|
68 | + * @return void |
|
69 | + */ |
|
70 | + public function selectDb($database) |
|
71 | + { |
|
72 | + /*if ($database != $this->database) { |
|
73 | 73 | $this->database = $database; |
74 | 74 | $this->linkId = null; |
75 | 75 | $this->connect(); |
76 | 76 | }*/ |
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * Db::connect() |
|
81 | - * @return void |
|
82 | - */ |
|
83 | - public function connect() |
|
84 | - { |
|
85 | - if (0 == $this->linkId) { |
|
86 | - $connectString = trim($this->ifadd($this->host, 'host='). |
|
87 | - $this->ifadd($this->port, 'port='). |
|
88 | - $this->ifadd($this->database, 'dbname='). |
|
89 | - $this->ifadd($this->user, 'user='). |
|
90 | - $this->ifadd($this->password, 'password=', "'")); |
|
91 | - $this->linkId = pg_connect($connectString); |
|
92 | - if (!$this->linkId) { |
|
93 | - $this->halt('Link-ID == FALSE, connect failed'); |
|
94 | - } |
|
95 | - } |
|
96 | - } |
|
97 | - |
|
98 | - /* This only affects systems not using persistent connections */ |
|
99 | - |
|
100 | - /** |
|
101 | - * Db::disconnect() |
|
102 | - * @return bool |
|
103 | - */ |
|
104 | - public function disconnect() |
|
105 | - { |
|
106 | - return @pg_close($this->linkId); |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * Db::queryReturn() |
|
111 | - * |
|
112 | - * Sends an SQL query to the server like the normal query() command but iterates through |
|
113 | - * any rows and returns the row or rows immediately or FALSE on error |
|
114 | - * |
|
115 | - * @param mixed $query SQL Query to be used |
|
116 | - * @param string $line optionally pass __LINE__ calling the query for logging |
|
117 | - * @param string $file optionally pass __FILE__ calling the query for logging |
|
118 | - * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
|
119 | - */ |
|
120 | - public function queryReturn($query, $line = '', $file = '') |
|
121 | - { |
|
122 | - $this->query($query, $line, $file); |
|
123 | - if ($this->num_rows() == 0) { |
|
124 | - return false; |
|
125 | - } elseif ($this->num_rows() == 1) { |
|
126 | - $this->next_record(MYSQL_ASSOC); |
|
127 | - return $this->Record; |
|
128 | - } else { |
|
129 | - $out = []; |
|
130 | - while ($this->next_record(MYSQL_ASSOC)) { |
|
131 | - $out[] = $this->Record; |
|
132 | - } |
|
133 | - return $out; |
|
134 | - } |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * db:qr() |
|
139 | - * |
|
140 | - * alias of queryReturn() |
|
141 | - * |
|
142 | - * @param mixed $query SQL Query to be used |
|
143 | - * @param string $line optionally pass __LINE__ calling the query for logging |
|
144 | - * @param string $file optionally pass __FILE__ calling the query for logging |
|
145 | - * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
|
146 | - */ |
|
147 | - public function qr($query, $line = '', $file = '') |
|
148 | - { |
|
149 | - return $this->queryReturn($query, $line, $file); |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * Db::query() |
|
154 | - * |
|
155 | - * Sends an SQL query to the database |
|
156 | - * |
|
157 | - * @param mixed $queryString |
|
158 | - * @param string $line |
|
159 | - * @param string $file |
|
160 | - * @return mixed 0 if no query or query id handler, safe to ignore this return |
|
161 | - */ |
|
162 | - public function query($queryString, $line = '', $file = '') |
|
163 | - { |
|
164 | - /* No empty queries, please, since PHP4 chokes on them. */ |
|
165 | - /* The empty query string is passed on from the constructor, |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * Db::connect() |
|
81 | + * @return void |
|
82 | + */ |
|
83 | + public function connect() |
|
84 | + { |
|
85 | + if (0 == $this->linkId) { |
|
86 | + $connectString = trim($this->ifadd($this->host, 'host='). |
|
87 | + $this->ifadd($this->port, 'port='). |
|
88 | + $this->ifadd($this->database, 'dbname='). |
|
89 | + $this->ifadd($this->user, 'user='). |
|
90 | + $this->ifadd($this->password, 'password=', "'")); |
|
91 | + $this->linkId = pg_connect($connectString); |
|
92 | + if (!$this->linkId) { |
|
93 | + $this->halt('Link-ID == FALSE, connect failed'); |
|
94 | + } |
|
95 | + } |
|
96 | + } |
|
97 | + |
|
98 | + /* This only affects systems not using persistent connections */ |
|
99 | + |
|
100 | + /** |
|
101 | + * Db::disconnect() |
|
102 | + * @return bool |
|
103 | + */ |
|
104 | + public function disconnect() |
|
105 | + { |
|
106 | + return @pg_close($this->linkId); |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * Db::queryReturn() |
|
111 | + * |
|
112 | + * Sends an SQL query to the server like the normal query() command but iterates through |
|
113 | + * any rows and returns the row or rows immediately or FALSE on error |
|
114 | + * |
|
115 | + * @param mixed $query SQL Query to be used |
|
116 | + * @param string $line optionally pass __LINE__ calling the query for logging |
|
117 | + * @param string $file optionally pass __FILE__ calling the query for logging |
|
118 | + * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
|
119 | + */ |
|
120 | + public function queryReturn($query, $line = '', $file = '') |
|
121 | + { |
|
122 | + $this->query($query, $line, $file); |
|
123 | + if ($this->num_rows() == 0) { |
|
124 | + return false; |
|
125 | + } elseif ($this->num_rows() == 1) { |
|
126 | + $this->next_record(MYSQL_ASSOC); |
|
127 | + return $this->Record; |
|
128 | + } else { |
|
129 | + $out = []; |
|
130 | + while ($this->next_record(MYSQL_ASSOC)) { |
|
131 | + $out[] = $this->Record; |
|
132 | + } |
|
133 | + return $out; |
|
134 | + } |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * db:qr() |
|
139 | + * |
|
140 | + * alias of queryReturn() |
|
141 | + * |
|
142 | + * @param mixed $query SQL Query to be used |
|
143 | + * @param string $line optionally pass __LINE__ calling the query for logging |
|
144 | + * @param string $file optionally pass __FILE__ calling the query for logging |
|
145 | + * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
|
146 | + */ |
|
147 | + public function qr($query, $line = '', $file = '') |
|
148 | + { |
|
149 | + return $this->queryReturn($query, $line, $file); |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * Db::query() |
|
154 | + * |
|
155 | + * Sends an SQL query to the database |
|
156 | + * |
|
157 | + * @param mixed $queryString |
|
158 | + * @param string $line |
|
159 | + * @param string $file |
|
160 | + * @return mixed 0 if no query or query id handler, safe to ignore this return |
|
161 | + */ |
|
162 | + public function query($queryString, $line = '', $file = '') |
|
163 | + { |
|
164 | + /* No empty queries, please, since PHP4 chokes on them. */ |
|
165 | + /* The empty query string is passed on from the constructor, |
|
166 | 166 | * when calling the class without a query, e.g. in situations |
167 | 167 | * like these: '$db = new db_Subclass;' |
168 | 168 | */ |
169 | - if ($queryString == '') { |
|
170 | - return 0; |
|
171 | - } |
|
172 | - |
|
173 | - $this->connect(); |
|
174 | - |
|
175 | - /* printf("<br>Debug: query = %s<br>\n", $queryString); */ |
|
176 | - |
|
177 | - $this->queryId = @pg_exec($this->linkId, $queryString); |
|
178 | - $this->Row = 0; |
|
179 | - |
|
180 | - $this->Error = pg_errormessage($this->linkId); |
|
181 | - $this->Errno = ($this->Error == '') ? 0 : 1; |
|
182 | - if (!$this->queryId) { |
|
183 | - $this->halt('Invalid SQL: '.$queryString, $line, $file); |
|
184 | - } |
|
185 | - |
|
186 | - return $this->queryId; |
|
187 | - } |
|
188 | - |
|
189 | - /** |
|
190 | - * Db::free() |
|
191 | - * |
|
192 | - * @return void |
|
193 | - */ |
|
194 | - public function free() |
|
195 | - { |
|
196 | - @pg_freeresult($this->queryId); |
|
197 | - $this->queryId = 0; |
|
198 | - } |
|
199 | - |
|
200 | - /** |
|
201 | - * Db::next_record() |
|
202 | - * @param mixed $resultType |
|
203 | - * @return bool |
|
204 | - */ |
|
205 | - public function next_record($resultType = PGSQL_BOTH) |
|
206 | - { |
|
207 | - $this->Record = @pg_fetch_array($this->queryId, $this->Row++, $resultType); |
|
208 | - |
|
209 | - $this->Error = pg_errormessage($this->linkId); |
|
210 | - $this->Errno = ($this->Error == '') ? 0 : 1; |
|
211 | - |
|
212 | - $stat = is_array($this->Record); |
|
213 | - if (!$stat && $this->autoFree) { |
|
214 | - pg_freeresult($this->queryId); |
|
215 | - $this->queryId = 0; |
|
216 | - } |
|
217 | - return $stat; |
|
218 | - } |
|
219 | - |
|
220 | - /** |
|
221 | - * Db::seek() |
|
222 | - * |
|
223 | - * @param mixed $pos |
|
224 | - * @return void |
|
225 | - */ |
|
226 | - public function seek($pos) |
|
227 | - { |
|
228 | - $this->Row = $pos; |
|
229 | - } |
|
230 | - |
|
231 | - /** |
|
232 | - * Db::transactionBegin() |
|
233 | - * |
|
234 | - * @return mixed |
|
235 | - */ |
|
236 | - public function transactionBegin() |
|
237 | - { |
|
238 | - return $this->query('begin'); |
|
239 | - } |
|
240 | - |
|
241 | - /** |
|
242 | - * Db::transactionCommit() |
|
243 | - * @return bool|mixed |
|
244 | - */ |
|
245 | - public function transactionCommit() |
|
246 | - { |
|
247 | - if (!$this->Errno) { |
|
248 | - return pg_exec($this->linkId, 'commit'); |
|
249 | - } else { |
|
250 | - return false; |
|
251 | - } |
|
252 | - } |
|
253 | - |
|
254 | - /** |
|
255 | - * Db::transactionAbort() |
|
256 | - * @return mixed |
|
257 | - */ |
|
258 | - public function transactionAbort() |
|
259 | - { |
|
260 | - return pg_exec($this->linkId, 'rollback'); |
|
261 | - } |
|
262 | - |
|
263 | - /** |
|
264 | - * Db::getLastInsertId() |
|
265 | - * @param mixed $table |
|
266 | - * @param mixed $field |
|
267 | - * @return int |
|
268 | - */ |
|
269 | - public function getLastInsertId($table, $field) |
|
270 | - { |
|
271 | - /* This will get the last insert ID created on the current connection. Should only be called |
|
169 | + if ($queryString == '') { |
|
170 | + return 0; |
|
171 | + } |
|
172 | + |
|
173 | + $this->connect(); |
|
174 | + |
|
175 | + /* printf("<br>Debug: query = %s<br>\n", $queryString); */ |
|
176 | + |
|
177 | + $this->queryId = @pg_exec($this->linkId, $queryString); |
|
178 | + $this->Row = 0; |
|
179 | + |
|
180 | + $this->Error = pg_errormessage($this->linkId); |
|
181 | + $this->Errno = ($this->Error == '') ? 0 : 1; |
|
182 | + if (!$this->queryId) { |
|
183 | + $this->halt('Invalid SQL: '.$queryString, $line, $file); |
|
184 | + } |
|
185 | + |
|
186 | + return $this->queryId; |
|
187 | + } |
|
188 | + |
|
189 | + /** |
|
190 | + * Db::free() |
|
191 | + * |
|
192 | + * @return void |
|
193 | + */ |
|
194 | + public function free() |
|
195 | + { |
|
196 | + @pg_freeresult($this->queryId); |
|
197 | + $this->queryId = 0; |
|
198 | + } |
|
199 | + |
|
200 | + /** |
|
201 | + * Db::next_record() |
|
202 | + * @param mixed $resultType |
|
203 | + * @return bool |
|
204 | + */ |
|
205 | + public function next_record($resultType = PGSQL_BOTH) |
|
206 | + { |
|
207 | + $this->Record = @pg_fetch_array($this->queryId, $this->Row++, $resultType); |
|
208 | + |
|
209 | + $this->Error = pg_errormessage($this->linkId); |
|
210 | + $this->Errno = ($this->Error == '') ? 0 : 1; |
|
211 | + |
|
212 | + $stat = is_array($this->Record); |
|
213 | + if (!$stat && $this->autoFree) { |
|
214 | + pg_freeresult($this->queryId); |
|
215 | + $this->queryId = 0; |
|
216 | + } |
|
217 | + return $stat; |
|
218 | + } |
|
219 | + |
|
220 | + /** |
|
221 | + * Db::seek() |
|
222 | + * |
|
223 | + * @param mixed $pos |
|
224 | + * @return void |
|
225 | + */ |
|
226 | + public function seek($pos) |
|
227 | + { |
|
228 | + $this->Row = $pos; |
|
229 | + } |
|
230 | + |
|
231 | + /** |
|
232 | + * Db::transactionBegin() |
|
233 | + * |
|
234 | + * @return mixed |
|
235 | + */ |
|
236 | + public function transactionBegin() |
|
237 | + { |
|
238 | + return $this->query('begin'); |
|
239 | + } |
|
240 | + |
|
241 | + /** |
|
242 | + * Db::transactionCommit() |
|
243 | + * @return bool|mixed |
|
244 | + */ |
|
245 | + public function transactionCommit() |
|
246 | + { |
|
247 | + if (!$this->Errno) { |
|
248 | + return pg_exec($this->linkId, 'commit'); |
|
249 | + } else { |
|
250 | + return false; |
|
251 | + } |
|
252 | + } |
|
253 | + |
|
254 | + /** |
|
255 | + * Db::transactionAbort() |
|
256 | + * @return mixed |
|
257 | + */ |
|
258 | + public function transactionAbort() |
|
259 | + { |
|
260 | + return pg_exec($this->linkId, 'rollback'); |
|
261 | + } |
|
262 | + |
|
263 | + /** |
|
264 | + * Db::getLastInsertId() |
|
265 | + * @param mixed $table |
|
266 | + * @param mixed $field |
|
267 | + * @return int |
|
268 | + */ |
|
269 | + public function getLastInsertId($table, $field) |
|
270 | + { |
|
271 | + /* This will get the last insert ID created on the current connection. Should only be called |
|
272 | 272 | * after an insert query is run on a table that has an auto incrementing field. Of note, table |
273 | 273 | * and field are required because pgsql returns the last inserted OID, which is unique across |
274 | 274 | * an entire installation. These params allow us to retrieve the sequenced field without adding |
275 | 275 | * conditional code to the apps. |
276 | 276 | */ |
277 | - if (!isset($table) || $table == '' || !isset($field) || $field == '') { |
|
278 | - return -1; |
|
279 | - } |
|
280 | - |
|
281 | - $oid = pg_getlastoid($this->queryId); |
|
282 | - if ($oid == -1) { |
|
283 | - return -1; |
|
284 | - } |
|
285 | - |
|
286 | - $result = @pg_exec($this->linkId, "select $field from $table where oid=$oid"); |
|
287 | - if (!$result) { |
|
288 | - return -1; |
|
289 | - } |
|
290 | - |
|
291 | - $Record = @pg_fetch_array($result, 0); |
|
292 | - @pg_freeresult($result); |
|
293 | - if (!is_array($Record)) /* OID not found? */ |
|
294 | - { |
|
295 | - return -1; |
|
296 | - } |
|
297 | - |
|
298 | - return $Record[0]; |
|
299 | - } |
|
300 | - |
|
301 | - /** |
|
302 | - * Db::lock() |
|
303 | - * @param mixed $table |
|
304 | - * @param string $mode |
|
305 | - * @return int|mixed |
|
306 | - */ |
|
307 | - public function lock($table, $mode = 'write') |
|
308 | - { |
|
309 | - $result = $this->transactionBegin(); |
|
310 | - |
|
311 | - if ($mode == 'write') { |
|
312 | - if (is_array($table)) { |
|
313 | - foreach ($table as $t) { |
|
314 | - $result = pg_exec($this->linkId, 'lock table '.$t[1].' in share mode'); |
|
315 | - } |
|
316 | - } else { |
|
317 | - $result = pg_exec($this->linkId, 'lock table '.$table.' in share mode'); |
|
318 | - } |
|
319 | - } else { |
|
320 | - $result = 1; |
|
321 | - } |
|
322 | - |
|
323 | - return $result; |
|
324 | - } |
|
325 | - |
|
326 | - /** |
|
327 | - * Db::unlock() |
|
328 | - * @return bool|mixed |
|
329 | - */ |
|
330 | - public function unlock() |
|
331 | - { |
|
332 | - return $this->transactionCommit(); |
|
333 | - } |
|
334 | - |
|
335 | - /** |
|
336 | - * Db::affectedRows() |
|
337 | - * @return void |
|
338 | - */ |
|
339 | - public function affectedRows() |
|
340 | - { |
|
341 | - return pg_cmdtuples($this->queryId); |
|
342 | - } |
|
343 | - |
|
344 | - /** |
|
345 | - * Db::num_rows() |
|
346 | - * @return int |
|
347 | - */ |
|
348 | - public function num_rows() |
|
349 | - { |
|
350 | - return pg_numrows($this->queryId); |
|
351 | - } |
|
352 | - |
|
353 | - /** |
|
354 | - * Db::num_fields() |
|
355 | - * @return int |
|
356 | - */ |
|
357 | - public function num_fields() |
|
358 | - { |
|
359 | - return pg_numfields($this->queryId); |
|
360 | - } |
|
361 | - |
|
362 | - /** |
|
363 | - * @param mixed $msg |
|
364 | - * @param string $line |
|
365 | - * @param string $file |
|
366 | - * @return mixed|void |
|
367 | - */ |
|
368 | - public function haltmsg($msg, $line = '', $file = '') |
|
369 | - { |
|
370 | - $this->log("Database error: $msg", $line, $file, 'error'); |
|
371 | - if ($this->Errno != '0' || $this->Error != '()') { |
|
372 | - $this->log('PostgreSQL Error: '.pg_last_error($this->linkId), $line, $file, 'error'); |
|
373 | - } |
|
374 | - $this->logBackTrace($msg, $line, $file); |
|
375 | - } |
|
376 | - |
|
377 | - /** |
|
378 | - * Db::tableNames() |
|
379 | - * |
|
380 | - * @return array |
|
381 | - */ |
|
382 | - public function tableNames() |
|
383 | - { |
|
384 | - $return = []; |
|
385 | - $this->query("select relname from pg_class where relkind = 'r' and not relname like 'pg_%'"); |
|
386 | - $i = 0; |
|
387 | - while ($this->next_record()) { |
|
388 | - $return[$i]['table_name'] = $this->f(0); |
|
389 | - $return[$i]['tablespace_name'] = $this->database; |
|
390 | - $return[$i]['database'] = $this->database; |
|
391 | - ++$i; |
|
392 | - } |
|
393 | - return $return; |
|
394 | - } |
|
395 | - |
|
396 | - /** |
|
397 | - * Db::indexNames() |
|
398 | - * |
|
399 | - * @return array |
|
400 | - */ |
|
401 | - public function indexNames() |
|
402 | - { |
|
403 | - $return = []; |
|
404 | - $this->query("SELECT relname FROM pg_class WHERE NOT relname ~ 'pg_.*' AND relkind ='i' ORDER BY relname"); |
|
405 | - $i = 0; |
|
406 | - while ($this->next_record()) { |
|
407 | - $return[$i]['index_name'] = $this->f(0); |
|
408 | - $return[$i]['tablespace_name'] = $this->database; |
|
409 | - $return[$i]['database'] = $this->database; |
|
410 | - ++$i; |
|
411 | - } |
|
412 | - return $return; |
|
413 | - } |
|
277 | + if (!isset($table) || $table == '' || !isset($field) || $field == '') { |
|
278 | + return -1; |
|
279 | + } |
|
280 | + |
|
281 | + $oid = pg_getlastoid($this->queryId); |
|
282 | + if ($oid == -1) { |
|
283 | + return -1; |
|
284 | + } |
|
285 | + |
|
286 | + $result = @pg_exec($this->linkId, "select $field from $table where oid=$oid"); |
|
287 | + if (!$result) { |
|
288 | + return -1; |
|
289 | + } |
|
290 | + |
|
291 | + $Record = @pg_fetch_array($result, 0); |
|
292 | + @pg_freeresult($result); |
|
293 | + if (!is_array($Record)) /* OID not found? */ |
|
294 | + { |
|
295 | + return -1; |
|
296 | + } |
|
297 | + |
|
298 | + return $Record[0]; |
|
299 | + } |
|
300 | + |
|
301 | + /** |
|
302 | + * Db::lock() |
|
303 | + * @param mixed $table |
|
304 | + * @param string $mode |
|
305 | + * @return int|mixed |
|
306 | + */ |
|
307 | + public function lock($table, $mode = 'write') |
|
308 | + { |
|
309 | + $result = $this->transactionBegin(); |
|
310 | + |
|
311 | + if ($mode == 'write') { |
|
312 | + if (is_array($table)) { |
|
313 | + foreach ($table as $t) { |
|
314 | + $result = pg_exec($this->linkId, 'lock table '.$t[1].' in share mode'); |
|
315 | + } |
|
316 | + } else { |
|
317 | + $result = pg_exec($this->linkId, 'lock table '.$table.' in share mode'); |
|
318 | + } |
|
319 | + } else { |
|
320 | + $result = 1; |
|
321 | + } |
|
322 | + |
|
323 | + return $result; |
|
324 | + } |
|
325 | + |
|
326 | + /** |
|
327 | + * Db::unlock() |
|
328 | + * @return bool|mixed |
|
329 | + */ |
|
330 | + public function unlock() |
|
331 | + { |
|
332 | + return $this->transactionCommit(); |
|
333 | + } |
|
334 | + |
|
335 | + /** |
|
336 | + * Db::affectedRows() |
|
337 | + * @return void |
|
338 | + */ |
|
339 | + public function affectedRows() |
|
340 | + { |
|
341 | + return pg_cmdtuples($this->queryId); |
|
342 | + } |
|
343 | + |
|
344 | + /** |
|
345 | + * Db::num_rows() |
|
346 | + * @return int |
|
347 | + */ |
|
348 | + public function num_rows() |
|
349 | + { |
|
350 | + return pg_numrows($this->queryId); |
|
351 | + } |
|
352 | + |
|
353 | + /** |
|
354 | + * Db::num_fields() |
|
355 | + * @return int |
|
356 | + */ |
|
357 | + public function num_fields() |
|
358 | + { |
|
359 | + return pg_numfields($this->queryId); |
|
360 | + } |
|
361 | + |
|
362 | + /** |
|
363 | + * @param mixed $msg |
|
364 | + * @param string $line |
|
365 | + * @param string $file |
|
366 | + * @return mixed|void |
|
367 | + */ |
|
368 | + public function haltmsg($msg, $line = '', $file = '') |
|
369 | + { |
|
370 | + $this->log("Database error: $msg", $line, $file, 'error'); |
|
371 | + if ($this->Errno != '0' || $this->Error != '()') { |
|
372 | + $this->log('PostgreSQL Error: '.pg_last_error($this->linkId), $line, $file, 'error'); |
|
373 | + } |
|
374 | + $this->logBackTrace($msg, $line, $file); |
|
375 | + } |
|
376 | + |
|
377 | + /** |
|
378 | + * Db::tableNames() |
|
379 | + * |
|
380 | + * @return array |
|
381 | + */ |
|
382 | + public function tableNames() |
|
383 | + { |
|
384 | + $return = []; |
|
385 | + $this->query("select relname from pg_class where relkind = 'r' and not relname like 'pg_%'"); |
|
386 | + $i = 0; |
|
387 | + while ($this->next_record()) { |
|
388 | + $return[$i]['table_name'] = $this->f(0); |
|
389 | + $return[$i]['tablespace_name'] = $this->database; |
|
390 | + $return[$i]['database'] = $this->database; |
|
391 | + ++$i; |
|
392 | + } |
|
393 | + return $return; |
|
394 | + } |
|
395 | + |
|
396 | + /** |
|
397 | + * Db::indexNames() |
|
398 | + * |
|
399 | + * @return array |
|
400 | + */ |
|
401 | + public function indexNames() |
|
402 | + { |
|
403 | + $return = []; |
|
404 | + $this->query("SELECT relname FROM pg_class WHERE NOT relname ~ 'pg_.*' AND relkind ='i' ORDER BY relname"); |
|
405 | + $i = 0; |
|
406 | + while ($this->next_record()) { |
|
407 | + $return[$i]['index_name'] = $this->f(0); |
|
408 | + $return[$i]['tablespace_name'] = $this->database; |
|
409 | + $return[$i]['database'] = $this->database; |
|
410 | + ++$i; |
|
411 | + } |
|
412 | + return $return; |
|
413 | + } |
|
414 | 414 | } |
@@ -1,12 +1,12 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | - /** |
|
4 | - * Generic SQL Driver Related Functionality |
|
5 | - * @author Joe Huss <[email protected]> |
|
6 | - * @copyright 2019 |
|
7 | - * @package MyAdmin |
|
8 | - * @category SQL |
|
9 | - */ |
|
3 | + /** |
|
4 | + * Generic SQL Driver Related Functionality |
|
5 | + * @author Joe Huss <[email protected]> |
|
6 | + * @copyright 2019 |
|
7 | + * @package MyAdmin |
|
8 | + * @category SQL |
|
9 | + */ |
|
10 | 10 | |
11 | 11 | namespace MyDb; |
12 | 12 | |
@@ -17,184 +17,184 @@ discard block |
||
17 | 17 | */ |
18 | 18 | class Loader |
19 | 19 | { |
20 | - /* public: connection parameters */ |
|
21 | - public $Type = 'mysqli'; |
|
22 | - public $host = 'localhost'; |
|
23 | - public $database = ''; |
|
24 | - public $user = ''; |
|
25 | - public $password = ''; |
|
26 | - |
|
27 | - /* public: configuration parameters */ |
|
28 | - public $autoStripslashes = false; |
|
29 | - public $Debug = 0; // Set to 1 for debugging messages. |
|
30 | - public $haltOnError = 'yes'; // "yes" (halt with message), "no" (ignore errors quietly), "report" (ignore error, but spit a warning) |
|
31 | - |
|
32 | - /* public: result array and current row number */ |
|
33 | - public $Record = []; |
|
34 | - public $Row; |
|
35 | - |
|
36 | - /* public: current error number and error text */ |
|
37 | - public $Errno = 0; |
|
38 | - public $Error = ''; |
|
39 | - |
|
40 | - public $type; |
|
41 | - |
|
42 | - /* private: link and query handles */ |
|
43 | - public $linkId = 0; |
|
44 | - public $queryId = 0; |
|
45 | - |
|
46 | - public $characterSet = ''; |
|
47 | - public $collation = ''; |
|
48 | - |
|
49 | - /** |
|
50 | - * Constructs the db handler, can optionally specify connection parameters |
|
51 | - * |
|
52 | - * @param string $Type Optional The database type mysql/mysqli/pdo/adodb/pgsql |
|
53 | - * @param string $database Optional The database name |
|
54 | - * @param string $user Optional The username to connect with |
|
55 | - * @param string $password Optional The password to use |
|
56 | - * @param string $host Optional The hostname where the server is, or default to localhost |
|
57 | - * @param string $query Optional query to perform immediately |
|
58 | - */ |
|
59 | - public function __construct($Type = '', $database = '', $user = '', $password = '', $host = 'localhost', $query = '') |
|
60 | - { |
|
61 | - $this->Type = $Type; |
|
62 | - if (!defined('db')) { |
|
63 | - switch ($this->Type) { |
|
64 | - case 'mysqli': |
|
65 | - include_once 'class.db_mysqli.inc.php'; |
|
66 | - break; |
|
67 | - case 'mysql': |
|
68 | - include_once 'class.db_mysql.inc.php'; |
|
69 | - break; |
|
70 | - case 'adodb': |
|
71 | - include_once 'class.db_adodb.inc.php'; |
|
72 | - break; |
|
73 | - case 'mdb2': |
|
74 | - include_once 'class.db_mdb2.inc.php'; |
|
75 | - break; |
|
76 | - case 'pdo': |
|
77 | - include_once 'class.db_pdo.inc.php'; |
|
78 | - break; |
|
79 | - case 'pgsql': |
|
80 | - include_once 'class.db_pgsql.inc.php'; |
|
81 | - break; |
|
82 | - default: |
|
83 | - $this->log('Could not find DB class '.$this->Type, __LINE__, __FILE__); |
|
84 | - break; |
|
85 | - } |
|
86 | - } |
|
87 | - $this->database = $database; |
|
88 | - $this->user = $user; |
|
89 | - $this->password = $password; |
|
90 | - $this->host = $host; |
|
91 | - if ($query != '') { |
|
92 | - $this->query($query); |
|
93 | - } |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * @param $message |
|
98 | - * @param string $line |
|
99 | - * @param string $file |
|
100 | - */ |
|
101 | - public function log($message, $line = '', $file = '') |
|
102 | - { |
|
103 | - error_log($message); |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * @return int |
|
108 | - */ |
|
109 | - public function linkId() |
|
110 | - { |
|
111 | - return $this->linkId; |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * @return int |
|
116 | - */ |
|
117 | - public function queryId() |
|
118 | - { |
|
119 | - return $this->queryId; |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * @param $str |
|
124 | - * @return string |
|
125 | - */ |
|
126 | - public function dbAddslashes($str) |
|
127 | - { |
|
128 | - if (!isset($str) || $str == '') { |
|
129 | - return ''; |
|
130 | - } |
|
131 | - |
|
132 | - return addslashes($str); |
|
133 | - } |
|
134 | - |
|
135 | - /** |
|
136 | - * db:qr() |
|
137 | - * |
|
138 | - * alias of queryReturn() |
|
139 | - * |
|
140 | - * @param mixed $query SQL Query to be used |
|
141 | - * @param string $line optionally pass __LINE__ calling the query for logging |
|
142 | - * @param string $file optionally pass __FILE__ calling the query for logging |
|
143 | - * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
|
144 | - */ |
|
145 | - public function qr($query, $line = '', $file = '') |
|
146 | - { |
|
147 | - return $this->queryReturn($query, $line, $file); |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * error handling |
|
152 | - * |
|
153 | - * @param mixed $msg |
|
154 | - * @param string $line |
|
155 | - * @param string $file |
|
156 | - * @return void |
|
157 | - */ |
|
158 | - public function halt($msg, $line = '', $file = '') |
|
159 | - { |
|
160 | - $this->unlock(false); |
|
161 | - |
|
162 | - if ($this->haltOnError == 'no') { |
|
163 | - return; |
|
164 | - } |
|
165 | - $this->haltmsg($msg); |
|
166 | - |
|
167 | - if ($file) { |
|
168 | - error_log("File: $file"); |
|
169 | - } |
|
170 | - if ($line) { |
|
171 | - error_log("Line: $line"); |
|
172 | - } |
|
173 | - if ($this->haltOnError != 'report') { |
|
174 | - echo '<p><b>Session halted.</b>'; |
|
175 | - // FIXME! Add check for error levels |
|
176 | - if (isset($GLOBALS['tf'])) { |
|
177 | - $GLOBALS['tf']->terminate(); |
|
178 | - } |
|
179 | - } |
|
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * @param $msg |
|
184 | - */ |
|
185 | - public function haltmsg($msg) |
|
186 | - { |
|
187 | - $this->log("Database error: $msg", __LINE__, __FILE__); |
|
188 | - if ($this->Errno != '0' || $this->Error != '()') { |
|
189 | - $this->log('SQL Error: '.$this->Errno.' ('.$this->Error.')', __LINE__, __FILE__); |
|
190 | - } |
|
191 | - } |
|
192 | - |
|
193 | - /** |
|
194 | - * @return array |
|
195 | - */ |
|
196 | - public function indexNames() |
|
197 | - { |
|
198 | - return []; |
|
199 | - } |
|
20 | + /* public: connection parameters */ |
|
21 | + public $Type = 'mysqli'; |
|
22 | + public $host = 'localhost'; |
|
23 | + public $database = ''; |
|
24 | + public $user = ''; |
|
25 | + public $password = ''; |
|
26 | + |
|
27 | + /* public: configuration parameters */ |
|
28 | + public $autoStripslashes = false; |
|
29 | + public $Debug = 0; // Set to 1 for debugging messages. |
|
30 | + public $haltOnError = 'yes'; // "yes" (halt with message), "no" (ignore errors quietly), "report" (ignore error, but spit a warning) |
|
31 | + |
|
32 | + /* public: result array and current row number */ |
|
33 | + public $Record = []; |
|
34 | + public $Row; |
|
35 | + |
|
36 | + /* public: current error number and error text */ |
|
37 | + public $Errno = 0; |
|
38 | + public $Error = ''; |
|
39 | + |
|
40 | + public $type; |
|
41 | + |
|
42 | + /* private: link and query handles */ |
|
43 | + public $linkId = 0; |
|
44 | + public $queryId = 0; |
|
45 | + |
|
46 | + public $characterSet = ''; |
|
47 | + public $collation = ''; |
|
48 | + |
|
49 | + /** |
|
50 | + * Constructs the db handler, can optionally specify connection parameters |
|
51 | + * |
|
52 | + * @param string $Type Optional The database type mysql/mysqli/pdo/adodb/pgsql |
|
53 | + * @param string $database Optional The database name |
|
54 | + * @param string $user Optional The username to connect with |
|
55 | + * @param string $password Optional The password to use |
|
56 | + * @param string $host Optional The hostname where the server is, or default to localhost |
|
57 | + * @param string $query Optional query to perform immediately |
|
58 | + */ |
|
59 | + public function __construct($Type = '', $database = '', $user = '', $password = '', $host = 'localhost', $query = '') |
|
60 | + { |
|
61 | + $this->Type = $Type; |
|
62 | + if (!defined('db')) { |
|
63 | + switch ($this->Type) { |
|
64 | + case 'mysqli': |
|
65 | + include_once 'class.db_mysqli.inc.php'; |
|
66 | + break; |
|
67 | + case 'mysql': |
|
68 | + include_once 'class.db_mysql.inc.php'; |
|
69 | + break; |
|
70 | + case 'adodb': |
|
71 | + include_once 'class.db_adodb.inc.php'; |
|
72 | + break; |
|
73 | + case 'mdb2': |
|
74 | + include_once 'class.db_mdb2.inc.php'; |
|
75 | + break; |
|
76 | + case 'pdo': |
|
77 | + include_once 'class.db_pdo.inc.php'; |
|
78 | + break; |
|
79 | + case 'pgsql': |
|
80 | + include_once 'class.db_pgsql.inc.php'; |
|
81 | + break; |
|
82 | + default: |
|
83 | + $this->log('Could not find DB class '.$this->Type, __LINE__, __FILE__); |
|
84 | + break; |
|
85 | + } |
|
86 | + } |
|
87 | + $this->database = $database; |
|
88 | + $this->user = $user; |
|
89 | + $this->password = $password; |
|
90 | + $this->host = $host; |
|
91 | + if ($query != '') { |
|
92 | + $this->query($query); |
|
93 | + } |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * @param $message |
|
98 | + * @param string $line |
|
99 | + * @param string $file |
|
100 | + */ |
|
101 | + public function log($message, $line = '', $file = '') |
|
102 | + { |
|
103 | + error_log($message); |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * @return int |
|
108 | + */ |
|
109 | + public function linkId() |
|
110 | + { |
|
111 | + return $this->linkId; |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * @return int |
|
116 | + */ |
|
117 | + public function queryId() |
|
118 | + { |
|
119 | + return $this->queryId; |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * @param $str |
|
124 | + * @return string |
|
125 | + */ |
|
126 | + public function dbAddslashes($str) |
|
127 | + { |
|
128 | + if (!isset($str) || $str == '') { |
|
129 | + return ''; |
|
130 | + } |
|
131 | + |
|
132 | + return addslashes($str); |
|
133 | + } |
|
134 | + |
|
135 | + /** |
|
136 | + * db:qr() |
|
137 | + * |
|
138 | + * alias of queryReturn() |
|
139 | + * |
|
140 | + * @param mixed $query SQL Query to be used |
|
141 | + * @param string $line optionally pass __LINE__ calling the query for logging |
|
142 | + * @param string $file optionally pass __FILE__ calling the query for logging |
|
143 | + * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
|
144 | + */ |
|
145 | + public function qr($query, $line = '', $file = '') |
|
146 | + { |
|
147 | + return $this->queryReturn($query, $line, $file); |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * error handling |
|
152 | + * |
|
153 | + * @param mixed $msg |
|
154 | + * @param string $line |
|
155 | + * @param string $file |
|
156 | + * @return void |
|
157 | + */ |
|
158 | + public function halt($msg, $line = '', $file = '') |
|
159 | + { |
|
160 | + $this->unlock(false); |
|
161 | + |
|
162 | + if ($this->haltOnError == 'no') { |
|
163 | + return; |
|
164 | + } |
|
165 | + $this->haltmsg($msg); |
|
166 | + |
|
167 | + if ($file) { |
|
168 | + error_log("File: $file"); |
|
169 | + } |
|
170 | + if ($line) { |
|
171 | + error_log("Line: $line"); |
|
172 | + } |
|
173 | + if ($this->haltOnError != 'report') { |
|
174 | + echo '<p><b>Session halted.</b>'; |
|
175 | + // FIXME! Add check for error levels |
|
176 | + if (isset($GLOBALS['tf'])) { |
|
177 | + $GLOBALS['tf']->terminate(); |
|
178 | + } |
|
179 | + } |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * @param $msg |
|
184 | + */ |
|
185 | + public function haltmsg($msg) |
|
186 | + { |
|
187 | + $this->log("Database error: $msg", __LINE__, __FILE__); |
|
188 | + if ($this->Errno != '0' || $this->Error != '()') { |
|
189 | + $this->log('SQL Error: '.$this->Errno.' ('.$this->Error.')', __LINE__, __FILE__); |
|
190 | + } |
|
191 | + } |
|
192 | + |
|
193 | + /** |
|
194 | + * @return array |
|
195 | + */ |
|
196 | + public function indexNames() |
|
197 | + { |
|
198 | + return []; |
|
199 | + } |
|
200 | 200 | } |
@@ -20,81 +20,81 @@ |
||
20 | 20 | */ |
21 | 21 | class Db extends MysqliDb implements Db_Interface |
22 | 22 | { |
23 | - public $host = 'localhost'; |
|
24 | - public $user = 'pdns'; |
|
25 | - public $password = ''; |
|
26 | - public $database = 'pdns'; |
|
27 | - public $type = 'mdb2'; |
|
28 | - public $error = false; |
|
29 | - public $message = ''; |
|
23 | + public $host = 'localhost'; |
|
24 | + public $user = 'pdns'; |
|
25 | + public $password = ''; |
|
26 | + public $database = 'pdns'; |
|
27 | + public $type = 'mdb2'; |
|
28 | + public $error = false; |
|
29 | + public $message = ''; |
|
30 | 30 | |
31 | - /** |
|
32 | - * Db::quote() |
|
33 | - * @param string $text |
|
34 | - * @param string $type |
|
35 | - * @return string |
|
36 | - */ |
|
37 | - public function quote($text = '', $type = 'text') |
|
38 | - { |
|
39 | - switch ($type) { |
|
40 | - case 'text': |
|
41 | - return "'".$this->escape($text)."'"; |
|
42 | - break; |
|
43 | - case 'integer': |
|
44 | - return (int) $text; |
|
45 | - break; |
|
46 | - default: |
|
47 | - return $text; |
|
48 | - break; |
|
49 | - } |
|
50 | - } |
|
31 | + /** |
|
32 | + * Db::quote() |
|
33 | + * @param string $text |
|
34 | + * @param string $type |
|
35 | + * @return string |
|
36 | + */ |
|
37 | + public function quote($text = '', $type = 'text') |
|
38 | + { |
|
39 | + switch ($type) { |
|
40 | + case 'text': |
|
41 | + return "'".$this->escape($text)."'"; |
|
42 | + break; |
|
43 | + case 'integer': |
|
44 | + return (int) $text; |
|
45 | + break; |
|
46 | + default: |
|
47 | + return $text; |
|
48 | + break; |
|
49 | + } |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * Db::queryOne() |
|
54 | - * |
|
55 | - * @param mixed $query |
|
56 | - * @param string $line |
|
57 | - * @param string $file |
|
58 | - * @return bool |
|
59 | - */ |
|
60 | - public function queryOne($query, $line = '', $file = '') |
|
61 | - { |
|
62 | - $this->query($query, $line, $file); |
|
63 | - if ($this->num_rows() > 0) { |
|
64 | - $this->next_record(); |
|
65 | - return $this->f(0); |
|
66 | - } else { |
|
67 | - return 0; |
|
68 | - } |
|
69 | - } |
|
52 | + /** |
|
53 | + * Db::queryOne() |
|
54 | + * |
|
55 | + * @param mixed $query |
|
56 | + * @param string $line |
|
57 | + * @param string $file |
|
58 | + * @return bool |
|
59 | + */ |
|
60 | + public function queryOne($query, $line = '', $file = '') |
|
61 | + { |
|
62 | + $this->query($query, $line, $file); |
|
63 | + if ($this->num_rows() > 0) { |
|
64 | + $this->next_record(); |
|
65 | + return $this->f(0); |
|
66 | + } else { |
|
67 | + return 0; |
|
68 | + } |
|
69 | + } |
|
70 | 70 | |
71 | - /** |
|
72 | - * Db::queryRow() |
|
73 | - * |
|
74 | - * @param mixed $query |
|
75 | - * @param string $line |
|
76 | - * @param string $file |
|
77 | - * @return array|bool |
|
78 | - */ |
|
79 | - public function queryRow($query, $line = '', $file = '') |
|
80 | - { |
|
81 | - $this->query($query, $line, $file); |
|
82 | - if ($this->num_rows() > 0) { |
|
83 | - $this->next_record(); |
|
84 | - return $this->Record; |
|
85 | - } else { |
|
86 | - return 0; |
|
87 | - } |
|
88 | - } |
|
71 | + /** |
|
72 | + * Db::queryRow() |
|
73 | + * |
|
74 | + * @param mixed $query |
|
75 | + * @param string $line |
|
76 | + * @param string $file |
|
77 | + * @return array|bool |
|
78 | + */ |
|
79 | + public function queryRow($query, $line = '', $file = '') |
|
80 | + { |
|
81 | + $this->query($query, $line, $file); |
|
82 | + if ($this->num_rows() > 0) { |
|
83 | + $this->next_record(); |
|
84 | + return $this->Record; |
|
85 | + } else { |
|
86 | + return 0; |
|
87 | + } |
|
88 | + } |
|
89 | 89 | |
90 | - /** |
|
91 | - * Db::lastInsertId() |
|
92 | - * @param mixed $table |
|
93 | - * @param mixed $field |
|
94 | - * @return int |
|
95 | - */ |
|
96 | - public function lastInsertId($table, $field) |
|
97 | - { |
|
98 | - return $this->getLastInsertId($table, $field); |
|
99 | - } |
|
90 | + /** |
|
91 | + * Db::lastInsertId() |
|
92 | + * @param mixed $table |
|
93 | + * @param mixed $field |
|
94 | + * @return int |
|
95 | + */ |
|
96 | + public function lastInsertId($table, $field) |
|
97 | + { |
|
98 | + return $this->getLastInsertId($table, $field); |
|
99 | + } |
|
100 | 100 | } |
@@ -16,57 +16,57 @@ |
||
16 | 16 | */ |
17 | 17 | interface Db_Interface |
18 | 18 | { |
19 | - /** |
|
20 | - * Db_Interface constructor. |
|
21 | - * |
|
22 | - * @param string $database |
|
23 | - * @param string $user |
|
24 | - * @param string $password |
|
25 | - * @param string $host |
|
26 | - * @param string $query |
|
27 | - * @param string $port |
|
28 | - */ |
|
29 | - public function __construct($database = '', $user = '', $password = '', $host = 'localhost', $query = '', $port = ''); |
|
19 | + /** |
|
20 | + * Db_Interface constructor. |
|
21 | + * |
|
22 | + * @param string $database |
|
23 | + * @param string $user |
|
24 | + * @param string $password |
|
25 | + * @param string $host |
|
26 | + * @param string $query |
|
27 | + * @param string $port |
|
28 | + */ |
|
29 | + public function __construct($database = '', $user = '', $password = '', $host = 'localhost', $query = '', $port = ''); |
|
30 | 30 | |
31 | - /** |
|
32 | - * @param $message |
|
33 | - * @param string $line |
|
34 | - * @param string $file |
|
35 | - * @return mixed |
|
36 | - */ |
|
37 | - public function log($message, $line = '', $file = ''); |
|
31 | + /** |
|
32 | + * @param $message |
|
33 | + * @param string $line |
|
34 | + * @param string $file |
|
35 | + * @return mixed |
|
36 | + */ |
|
37 | + public function log($message, $line = '', $file = ''); |
|
38 | 38 | |
39 | - public function linkId(); |
|
39 | + public function linkId(); |
|
40 | 40 | |
41 | - public function queryId(); |
|
41 | + public function queryId(); |
|
42 | 42 | |
43 | - /** |
|
44 | - * @param $str |
|
45 | - * @return mixed |
|
46 | - */ |
|
47 | - public function dbAddslashes($str); |
|
43 | + /** |
|
44 | + * @param $str |
|
45 | + * @return mixed |
|
46 | + */ |
|
47 | + public function dbAddslashes($str); |
|
48 | 48 | |
49 | - /** |
|
50 | - * @param $query |
|
51 | - * @param string $line |
|
52 | - * @param string $file |
|
53 | - * @return mixed |
|
54 | - */ |
|
55 | - public function qr($query, $line = '', $file = ''); |
|
49 | + /** |
|
50 | + * @param $query |
|
51 | + * @param string $line |
|
52 | + * @param string $file |
|
53 | + * @return mixed |
|
54 | + */ |
|
55 | + public function qr($query, $line = '', $file = ''); |
|
56 | 56 | |
57 | - /** |
|
58 | - * @param $msg |
|
59 | - * @param string $line |
|
60 | - * @param string $file |
|
61 | - * @return mixed |
|
62 | - */ |
|
63 | - public function halt($msg, $line = '', $file = ''); |
|
57 | + /** |
|
58 | + * @param $msg |
|
59 | + * @param string $line |
|
60 | + * @param string $file |
|
61 | + * @return mixed |
|
62 | + */ |
|
63 | + public function halt($msg, $line = '', $file = ''); |
|
64 | 64 | |
65 | - /** |
|
66 | - * @param $msg |
|
67 | - * @return mixed |
|
68 | - */ |
|
69 | - public function haltmsg($msg); |
|
65 | + /** |
|
66 | + * @param $msg |
|
67 | + * @return mixed |
|
68 | + */ |
|
69 | + public function haltmsg($msg); |
|
70 | 70 | |
71 | - public function indexNames(); |
|
71 | + public function indexNames(); |
|
72 | 72 | } |
@@ -19,258 +19,258 @@ discard block |
||
19 | 19 | */ |
20 | 20 | class Db extends Generic implements Db_Interface |
21 | 21 | { |
22 | - public $driver = 'mysql'; |
|
23 | - public $Rows = []; |
|
24 | - public $type = 'adodb'; |
|
25 | - |
|
26 | - /** |
|
27 | - * Db::connect() |
|
28 | - * @param string $database |
|
29 | - * @param string $host |
|
30 | - * @param string $user |
|
31 | - * @param string $password |
|
32 | - * @param string $driver |
|
33 | - * @return bool|\the |
|
34 | - */ |
|
35 | - public function connect($database = '', $host = '', $user = '', $password = '', $driver = 'mysql') |
|
36 | - { |
|
37 | - /* Handle defaults */ |
|
38 | - if ('' == $database) { |
|
39 | - $database = $this->database; |
|
40 | - } |
|
41 | - if ('' == $host) { |
|
42 | - $host = $this->host; |
|
43 | - } |
|
44 | - if ('' == $user) { |
|
45 | - $user = $this->user; |
|
46 | - } |
|
47 | - if ('' == $password) { |
|
48 | - $password = $this->password; |
|
49 | - } |
|
50 | - if ('' == $driver) { |
|
51 | - $driver = $this->driver; |
|
52 | - } |
|
53 | - /* establish connection, select database */ |
|
54 | - if ($this->linkId === false) { |
|
55 | - $this->linkId = NewADOConnection($driver); |
|
56 | - $this->linkId->Connect($host, $user, $password, $database); |
|
57 | - } |
|
58 | - return $this->linkId; |
|
59 | - } |
|
60 | - |
|
61 | - /* This only affects systems not using persistent connections */ |
|
62 | - |
|
63 | - /** |
|
64 | - * Db::disconnect() |
|
65 | - * @return void |
|
66 | - */ |
|
67 | - public function disconnect() |
|
68 | - { |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * @param $string |
|
73 | - * @return string |
|
74 | - */ |
|
75 | - public function real_escape($string = '') |
|
76 | - { |
|
77 | - return escapeshellarg($string); |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * discard the query result |
|
82 | - * @return void |
|
83 | - */ |
|
84 | - public function free() |
|
85 | - { |
|
86 | - // @mysql_free_result($this->queryId); |
|
87 | - // $this->queryId = 0; |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * Db::queryReturn() |
|
92 | - * |
|
93 | - * Sends an SQL query to the server like the normal query() command but iterates through |
|
94 | - * any rows and returns the row or rows immediately or FALSE on error |
|
95 | - * |
|
96 | - * @param mixed $query SQL Query to be used |
|
97 | - * @param string $line optionally pass __LINE__ calling the query for logging |
|
98 | - * @param string $file optionally pass __FILE__ calling the query for logging |
|
99 | - * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
|
100 | - */ |
|
101 | - public function queryReturn($query, $line = '', $file = '') |
|
102 | - { |
|
103 | - $this->query($query, $line, $file); |
|
104 | - if ($this->num_rows() == 0) { |
|
105 | - return false; |
|
106 | - } elseif ($this->num_rows() == 1) { |
|
107 | - $this->next_record(MYSQL_ASSOC); |
|
108 | - return $this->Record; |
|
109 | - } else { |
|
110 | - $out = []; |
|
111 | - while ($this->next_record(MYSQL_ASSOC)) { |
|
112 | - $out[] = $this->Record; |
|
113 | - } |
|
114 | - return $out; |
|
115 | - } |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * db:qr() |
|
120 | - * |
|
121 | - * alias of queryReturn() |
|
122 | - * |
|
123 | - * @param mixed $query SQL Query to be used |
|
124 | - * @param string $line optionally pass __LINE__ calling the query for logging |
|
125 | - * @param string $file optionally pass __FILE__ calling the query for logging |
|
126 | - * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
|
127 | - */ |
|
128 | - public function qr($query, $line = '', $file = '') |
|
129 | - { |
|
130 | - return $this->queryReturn($query, $line, $file); |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * Db::query() |
|
135 | - * |
|
136 | - * Sends an SQL query to the database |
|
137 | - * |
|
138 | - * @param mixed $queryString |
|
139 | - * @param string $line |
|
140 | - * @param string $file |
|
141 | - * @return mixed 0 if no query or query id handler, safe to ignore this return |
|
142 | - */ |
|
143 | - public function query($queryString, $line = '', $file = '') |
|
144 | - { |
|
145 | - /* No empty queries, please, since PHP4 chokes on them. */ |
|
146 | - /* The empty query string is passed on from the constructor, |
|
22 | + public $driver = 'mysql'; |
|
23 | + public $Rows = []; |
|
24 | + public $type = 'adodb'; |
|
25 | + |
|
26 | + /** |
|
27 | + * Db::connect() |
|
28 | + * @param string $database |
|
29 | + * @param string $host |
|
30 | + * @param string $user |
|
31 | + * @param string $password |
|
32 | + * @param string $driver |
|
33 | + * @return bool|\the |
|
34 | + */ |
|
35 | + public function connect($database = '', $host = '', $user = '', $password = '', $driver = 'mysql') |
|
36 | + { |
|
37 | + /* Handle defaults */ |
|
38 | + if ('' == $database) { |
|
39 | + $database = $this->database; |
|
40 | + } |
|
41 | + if ('' == $host) { |
|
42 | + $host = $this->host; |
|
43 | + } |
|
44 | + if ('' == $user) { |
|
45 | + $user = $this->user; |
|
46 | + } |
|
47 | + if ('' == $password) { |
|
48 | + $password = $this->password; |
|
49 | + } |
|
50 | + if ('' == $driver) { |
|
51 | + $driver = $this->driver; |
|
52 | + } |
|
53 | + /* establish connection, select database */ |
|
54 | + if ($this->linkId === false) { |
|
55 | + $this->linkId = NewADOConnection($driver); |
|
56 | + $this->linkId->Connect($host, $user, $password, $database); |
|
57 | + } |
|
58 | + return $this->linkId; |
|
59 | + } |
|
60 | + |
|
61 | + /* This only affects systems not using persistent connections */ |
|
62 | + |
|
63 | + /** |
|
64 | + * Db::disconnect() |
|
65 | + * @return void |
|
66 | + */ |
|
67 | + public function disconnect() |
|
68 | + { |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * @param $string |
|
73 | + * @return string |
|
74 | + */ |
|
75 | + public function real_escape($string = '') |
|
76 | + { |
|
77 | + return escapeshellarg($string); |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * discard the query result |
|
82 | + * @return void |
|
83 | + */ |
|
84 | + public function free() |
|
85 | + { |
|
86 | + // @mysql_free_result($this->queryId); |
|
87 | + // $this->queryId = 0; |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * Db::queryReturn() |
|
92 | + * |
|
93 | + * Sends an SQL query to the server like the normal query() command but iterates through |
|
94 | + * any rows and returns the row or rows immediately or FALSE on error |
|
95 | + * |
|
96 | + * @param mixed $query SQL Query to be used |
|
97 | + * @param string $line optionally pass __LINE__ calling the query for logging |
|
98 | + * @param string $file optionally pass __FILE__ calling the query for logging |
|
99 | + * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
|
100 | + */ |
|
101 | + public function queryReturn($query, $line = '', $file = '') |
|
102 | + { |
|
103 | + $this->query($query, $line, $file); |
|
104 | + if ($this->num_rows() == 0) { |
|
105 | + return false; |
|
106 | + } elseif ($this->num_rows() == 1) { |
|
107 | + $this->next_record(MYSQL_ASSOC); |
|
108 | + return $this->Record; |
|
109 | + } else { |
|
110 | + $out = []; |
|
111 | + while ($this->next_record(MYSQL_ASSOC)) { |
|
112 | + $out[] = $this->Record; |
|
113 | + } |
|
114 | + return $out; |
|
115 | + } |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * db:qr() |
|
120 | + * |
|
121 | + * alias of queryReturn() |
|
122 | + * |
|
123 | + * @param mixed $query SQL Query to be used |
|
124 | + * @param string $line optionally pass __LINE__ calling the query for logging |
|
125 | + * @param string $file optionally pass __FILE__ calling the query for logging |
|
126 | + * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
|
127 | + */ |
|
128 | + public function qr($query, $line = '', $file = '') |
|
129 | + { |
|
130 | + return $this->queryReturn($query, $line, $file); |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * Db::query() |
|
135 | + * |
|
136 | + * Sends an SQL query to the database |
|
137 | + * |
|
138 | + * @param mixed $queryString |
|
139 | + * @param string $line |
|
140 | + * @param string $file |
|
141 | + * @return mixed 0 if no query or query id handler, safe to ignore this return |
|
142 | + */ |
|
143 | + public function query($queryString, $line = '', $file = '') |
|
144 | + { |
|
145 | + /* No empty queries, please, since PHP4 chokes on them. */ |
|
146 | + /* The empty query string is passed on from the constructor, |
|
147 | 147 | * when calling the class without a query, e.g. in situations |
148 | 148 | * like these: '$db = new db_Subclass;' |
149 | 149 | */ |
150 | - if ($queryString == '') { |
|
151 | - return 0; |
|
152 | - } |
|
153 | - if (!$this->connect()) { |
|
154 | - return 0; |
|
155 | - /* we already complained in connect() about that. */ |
|
156 | - } |
|
157 | - |
|
158 | - // New query, discard previous result. |
|
159 | - if ($this->queryId !== false) { |
|
160 | - $this->free(); |
|
161 | - } |
|
162 | - |
|
163 | - if ($this->Debug) { |
|
164 | - printf("Debug: query = %s<br>\n", $queryString); |
|
165 | - } |
|
166 | - if (isset($GLOBALS['log_queries']) && $GLOBALS['log_queries'] !== false) { |
|
167 | - $this->log($queryString, $line, $file); |
|
168 | - } |
|
169 | - |
|
170 | - try { |
|
171 | - $this->queryId = $this->linkId->Execute($queryString); |
|
172 | - } catch (exception $e) { |
|
173 | - $this->emailError($queryString, $e, $line, $file); |
|
174 | - } |
|
175 | - $this->log("ADOdb Query $queryString (S:$success) - ".count($this->Rows).' Rows', __LINE__, __FILE__); |
|
176 | - $this->Row = 0; |
|
177 | - |
|
178 | - // Will return nada if it fails. That's fine. |
|
179 | - return $this->queryId; |
|
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * Db::next_record() |
|
184 | - * @param mixed $resultType |
|
185 | - * @return bool |
|
186 | - */ |
|
187 | - public function next_record($resultType = MYSQL_ASSOC) |
|
188 | - { |
|
189 | - if (!$this->queryId) { |
|
190 | - $this->halt('next_record called with no query pending.'); |
|
191 | - return 0; |
|
192 | - } |
|
193 | - ++$this->Row; |
|
194 | - $this->Record = $this->queryId->FetchRow(); |
|
195 | - $stat = is_array($this->Record); |
|
196 | - if (!$stat && $this->autoFree) { |
|
197 | - $this->free(); |
|
198 | - } |
|
199 | - return $stat; |
|
200 | - } |
|
201 | - |
|
202 | - /* public: position in result set */ |
|
203 | - |
|
204 | - /** |
|
205 | - * Db::seek() |
|
206 | - * @param integer $pos |
|
207 | - * @return int |
|
208 | - */ |
|
209 | - public function seek($pos = 0) |
|
210 | - { |
|
211 | - if (isset($this->Rows[$pos])) { |
|
212 | - $this->Row = $pos; |
|
213 | - } else { |
|
214 | - $this->halt("seek($pos) failed: result has ".count($this->Rows).' rows'); |
|
215 | - /* half assed attempt to save the day, |
|
150 | + if ($queryString == '') { |
|
151 | + return 0; |
|
152 | + } |
|
153 | + if (!$this->connect()) { |
|
154 | + return 0; |
|
155 | + /* we already complained in connect() about that. */ |
|
156 | + } |
|
157 | + |
|
158 | + // New query, discard previous result. |
|
159 | + if ($this->queryId !== false) { |
|
160 | + $this->free(); |
|
161 | + } |
|
162 | + |
|
163 | + if ($this->Debug) { |
|
164 | + printf("Debug: query = %s<br>\n", $queryString); |
|
165 | + } |
|
166 | + if (isset($GLOBALS['log_queries']) && $GLOBALS['log_queries'] !== false) { |
|
167 | + $this->log($queryString, $line, $file); |
|
168 | + } |
|
169 | + |
|
170 | + try { |
|
171 | + $this->queryId = $this->linkId->Execute($queryString); |
|
172 | + } catch (exception $e) { |
|
173 | + $this->emailError($queryString, $e, $line, $file); |
|
174 | + } |
|
175 | + $this->log("ADOdb Query $queryString (S:$success) - ".count($this->Rows).' Rows', __LINE__, __FILE__); |
|
176 | + $this->Row = 0; |
|
177 | + |
|
178 | + // Will return nada if it fails. That's fine. |
|
179 | + return $this->queryId; |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * Db::next_record() |
|
184 | + * @param mixed $resultType |
|
185 | + * @return bool |
|
186 | + */ |
|
187 | + public function next_record($resultType = MYSQL_ASSOC) |
|
188 | + { |
|
189 | + if (!$this->queryId) { |
|
190 | + $this->halt('next_record called with no query pending.'); |
|
191 | + return 0; |
|
192 | + } |
|
193 | + ++$this->Row; |
|
194 | + $this->Record = $this->queryId->FetchRow(); |
|
195 | + $stat = is_array($this->Record); |
|
196 | + if (!$stat && $this->autoFree) { |
|
197 | + $this->free(); |
|
198 | + } |
|
199 | + return $stat; |
|
200 | + } |
|
201 | + |
|
202 | + /* public: position in result set */ |
|
203 | + |
|
204 | + /** |
|
205 | + * Db::seek() |
|
206 | + * @param integer $pos |
|
207 | + * @return int |
|
208 | + */ |
|
209 | + public function seek($pos = 0) |
|
210 | + { |
|
211 | + if (isset($this->Rows[$pos])) { |
|
212 | + $this->Row = $pos; |
|
213 | + } else { |
|
214 | + $this->halt("seek($pos) failed: result has ".count($this->Rows).' rows'); |
|
215 | + /* half assed attempt to save the day, |
|
216 | 216 | * but do not consider this documented or even |
217 | 217 | * desirable behaviour. |
218 | 218 | */ |
219 | - return 0; |
|
220 | - } |
|
221 | - return 1; |
|
222 | - } |
|
223 | - |
|
224 | - /** |
|
225 | - * Db::transactionBegin() |
|
226 | - * @return bool |
|
227 | - */ |
|
228 | - public function transactionBegin() |
|
229 | - { |
|
230 | - return true; |
|
231 | - } |
|
232 | - |
|
233 | - /** |
|
234 | - * Db::transactionCommit() |
|
235 | - * @return bool |
|
236 | - */ |
|
237 | - public function transactionCommit() |
|
238 | - { |
|
239 | - return true; |
|
240 | - } |
|
241 | - |
|
242 | - /** |
|
243 | - * Db::transactionAbort() |
|
244 | - * @return bool |
|
245 | - */ |
|
246 | - public function transactionAbort() |
|
247 | - { |
|
248 | - return true; |
|
249 | - } |
|
250 | - |
|
251 | - /** |
|
252 | - * Db::getLastInsertId() |
|
253 | - * |
|
254 | - * @param mixed $table |
|
255 | - * @param mixed $field |
|
256 | - * @return mixed |
|
257 | - */ |
|
258 | - public function getLastInsertId($table, $field) |
|
259 | - { |
|
260 | - return $this->linkId->Insert_ID($table, $field); |
|
261 | - } |
|
262 | - |
|
263 | - /* public: table locking */ |
|
264 | - |
|
265 | - /** |
|
266 | - * Db::lock() |
|
267 | - * @param mixed $table |
|
268 | - * @param string $mode |
|
269 | - * @return void |
|
270 | - */ |
|
271 | - public function lock($table, $mode = 'write') |
|
272 | - { |
|
273 | - /* $this->connect(); |
|
219 | + return 0; |
|
220 | + } |
|
221 | + return 1; |
|
222 | + } |
|
223 | + |
|
224 | + /** |
|
225 | + * Db::transactionBegin() |
|
226 | + * @return bool |
|
227 | + */ |
|
228 | + public function transactionBegin() |
|
229 | + { |
|
230 | + return true; |
|
231 | + } |
|
232 | + |
|
233 | + /** |
|
234 | + * Db::transactionCommit() |
|
235 | + * @return bool |
|
236 | + */ |
|
237 | + public function transactionCommit() |
|
238 | + { |
|
239 | + return true; |
|
240 | + } |
|
241 | + |
|
242 | + /** |
|
243 | + * Db::transactionAbort() |
|
244 | + * @return bool |
|
245 | + */ |
|
246 | + public function transactionAbort() |
|
247 | + { |
|
248 | + return true; |
|
249 | + } |
|
250 | + |
|
251 | + /** |
|
252 | + * Db::getLastInsertId() |
|
253 | + * |
|
254 | + * @param mixed $table |
|
255 | + * @param mixed $field |
|
256 | + * @return mixed |
|
257 | + */ |
|
258 | + public function getLastInsertId($table, $field) |
|
259 | + { |
|
260 | + return $this->linkId->Insert_ID($table, $field); |
|
261 | + } |
|
262 | + |
|
263 | + /* public: table locking */ |
|
264 | + |
|
265 | + /** |
|
266 | + * Db::lock() |
|
267 | + * @param mixed $table |
|
268 | + * @param string $mode |
|
269 | + * @return void |
|
270 | + */ |
|
271 | + public function lock($table, $mode = 'write') |
|
272 | + { |
|
273 | + /* $this->connect(); |
|
274 | 274 | |
275 | 275 | * $query = "lock tables "; |
276 | 276 | * if (is_array($table)) |
@@ -300,15 +300,15 @@ discard block |
||
300 | 300 | * } |
301 | 301 | * return $res; |
302 | 302 | */ |
303 | - } |
|
303 | + } |
|
304 | 304 | |
305 | - /** |
|
306 | - * Db::unlock() |
|
307 | - * @return void |
|
308 | - */ |
|
309 | - public function unlock() |
|
310 | - { |
|
311 | - /* $this->connect(); |
|
305 | + /** |
|
306 | + * Db::unlock() |
|
307 | + * @return void |
|
308 | + */ |
|
309 | + public function unlock() |
|
310 | + { |
|
311 | + /* $this->connect(); |
|
312 | 312 | |
313 | 313 | * $res = @mysql_query("unlock tables"); |
314 | 314 | * if (!$res) |
@@ -318,72 +318,72 @@ discard block |
||
318 | 318 | * } |
319 | 319 | * return $res; |
320 | 320 | */ |
321 | - } |
|
322 | - |
|
323 | - /* public: evaluate the result (size, width) */ |
|
324 | - |
|
325 | - /** |
|
326 | - * Db::affectedRows() |
|
327 | - * |
|
328 | - * @return mixed |
|
329 | - */ |
|
330 | - public function affectedRows() |
|
331 | - { |
|
332 | - return @$this->linkId->Affected_Rows(); |
|
333 | - // return @$this->queryId->rowCount(); |
|
334 | - } |
|
335 | - |
|
336 | - /** |
|
337 | - * Db::num_rows() |
|
338 | - * |
|
339 | - * @return mixed |
|
340 | - */ |
|
341 | - public function num_rows() |
|
342 | - { |
|
343 | - return $this->queryId->NumRows(); |
|
344 | - } |
|
345 | - |
|
346 | - /** |
|
347 | - * Db::num_fields() |
|
348 | - * |
|
349 | - * @return mixed |
|
350 | - */ |
|
351 | - public function num_fields() |
|
352 | - { |
|
353 | - return $this->queryId->NumCols(); |
|
354 | - } |
|
355 | - |
|
356 | - /** |
|
357 | - * @param mixed $msg |
|
358 | - * @param string $line |
|
359 | - * @param string $file |
|
360 | - * @return mixed|void |
|
361 | - */ |
|
362 | - public function haltmsg($msg, $line = '', $file = '') |
|
363 | - { |
|
364 | - $this->log("Database error: $msg", $line, $file, 'error'); |
|
365 | - if ($this->linkId->ErrorNo() != '0' && $this->linkId->ErrorMsg() != '') { |
|
366 | - $this->log('ADOdb SQL Error: '.$this->linkId->ErrorMsg(), $line, $file, 'error'); |
|
367 | - } |
|
368 | - $this->logBackTrace($msg, $line, $file); |
|
369 | - } |
|
370 | - |
|
371 | - /** |
|
372 | - * Db::tableNames() |
|
373 | - * |
|
374 | - * @return array |
|
375 | - */ |
|
376 | - public function tableNames() |
|
377 | - { |
|
378 | - $return = []; |
|
379 | - $this->query('SHOW TABLES'); |
|
380 | - $i = 0; |
|
381 | - while ($info = $this->queryId->FetchRow()) { |
|
382 | - $return[$i]['table_name'] = $info[0]; |
|
383 | - $return[$i]['tablespace_name'] = $this->database; |
|
384 | - $return[$i]['database'] = $this->database; |
|
385 | - ++$i; |
|
386 | - } |
|
387 | - return $return; |
|
388 | - } |
|
321 | + } |
|
322 | + |
|
323 | + /* public: evaluate the result (size, width) */ |
|
324 | + |
|
325 | + /** |
|
326 | + * Db::affectedRows() |
|
327 | + * |
|
328 | + * @return mixed |
|
329 | + */ |
|
330 | + public function affectedRows() |
|
331 | + { |
|
332 | + return @$this->linkId->Affected_Rows(); |
|
333 | + // return @$this->queryId->rowCount(); |
|
334 | + } |
|
335 | + |
|
336 | + /** |
|
337 | + * Db::num_rows() |
|
338 | + * |
|
339 | + * @return mixed |
|
340 | + */ |
|
341 | + public function num_rows() |
|
342 | + { |
|
343 | + return $this->queryId->NumRows(); |
|
344 | + } |
|
345 | + |
|
346 | + /** |
|
347 | + * Db::num_fields() |
|
348 | + * |
|
349 | + * @return mixed |
|
350 | + */ |
|
351 | + public function num_fields() |
|
352 | + { |
|
353 | + return $this->queryId->NumCols(); |
|
354 | + } |
|
355 | + |
|
356 | + /** |
|
357 | + * @param mixed $msg |
|
358 | + * @param string $line |
|
359 | + * @param string $file |
|
360 | + * @return mixed|void |
|
361 | + */ |
|
362 | + public function haltmsg($msg, $line = '', $file = '') |
|
363 | + { |
|
364 | + $this->log("Database error: $msg", $line, $file, 'error'); |
|
365 | + if ($this->linkId->ErrorNo() != '0' && $this->linkId->ErrorMsg() != '') { |
|
366 | + $this->log('ADOdb SQL Error: '.$this->linkId->ErrorMsg(), $line, $file, 'error'); |
|
367 | + } |
|
368 | + $this->logBackTrace($msg, $line, $file); |
|
369 | + } |
|
370 | + |
|
371 | + /** |
|
372 | + * Db::tableNames() |
|
373 | + * |
|
374 | + * @return array |
|
375 | + */ |
|
376 | + public function tableNames() |
|
377 | + { |
|
378 | + $return = []; |
|
379 | + $this->query('SHOW TABLES'); |
|
380 | + $i = 0; |
|
381 | + while ($info = $this->queryId->FetchRow()) { |
|
382 | + $return[$i]['table_name'] = $info[0]; |
|
383 | + $return[$i]['tablespace_name'] = $this->database; |
|
384 | + $return[$i]['database'] = $this->database; |
|
385 | + ++$i; |
|
386 | + } |
|
387 | + return $return; |
|
388 | + } |
|
389 | 389 | } |
@@ -20,256 +20,256 @@ discard block |
||
20 | 20 | */ |
21 | 21 | class Db extends Generic implements Db_Interface |
22 | 22 | { |
23 | - /* public: connection parameters */ |
|
24 | - public $driver = 'mysql'; |
|
25 | - public $Rows = []; |
|
26 | - /* public: this is an api revision, not a CVS revision. */ |
|
27 | - public $type = 'pdo'; |
|
28 | - |
|
29 | - /** |
|
30 | - * changes the database we are working with. |
|
31 | - * |
|
32 | - * @param string $database the name of the database to use |
|
33 | - * @return void |
|
34 | - */ |
|
35 | - public function selectDb($database) |
|
36 | - { |
|
37 | - $dSN = "{$this->driver}:dbname={$database};host={$this->host}"; |
|
38 | - if ($this->characterSet != '') { |
|
39 | - $dSN .= ';charset='.$this->characterSet; |
|
40 | - } |
|
41 | - $this->linkId = new PDO($dSN, $this->user, $this->password); |
|
42 | - $this->database = $database; |
|
43 | - } |
|
44 | - |
|
45 | - |
|
46 | - /** |
|
47 | - * alias function of select_db, changes the database we are working with. |
|
48 | - * |
|
49 | - * @param string $database the name of the database to use |
|
50 | - * @return void |
|
51 | - */ |
|
52 | - public function useDb($database) |
|
53 | - { |
|
54 | - $this->selectDb($database); |
|
55 | - } |
|
56 | - |
|
57 | - /* public: connection management */ |
|
58 | - |
|
59 | - /** |
|
60 | - * Db::connect() |
|
61 | - * @param string $database |
|
62 | - * @param string $host |
|
63 | - * @param string $user |
|
64 | - * @param string $password |
|
65 | - * @param string $driver |
|
66 | - * @return bool|int|PDO |
|
67 | - */ |
|
68 | - public function connect($database = '', $host = '', $user = '', $password = '', $driver = 'mysql') |
|
69 | - { |
|
70 | - /* Handle defaults */ |
|
71 | - if ('' == $database) { |
|
72 | - $database = $this->database; |
|
73 | - } |
|
74 | - if ('' == $host) { |
|
75 | - $host = $this->host; |
|
76 | - } |
|
77 | - if ('' == $user) { |
|
78 | - $user = $this->user; |
|
79 | - } |
|
80 | - if ('' == $password) { |
|
81 | - $password = $this->password; |
|
82 | - } |
|
83 | - if ('' == $driver) { |
|
84 | - $driver = $this->driver; |
|
85 | - } |
|
86 | - /* establish connection, select database */ |
|
87 | - $dSN = "{$driver}:dbname={$database};host={$host}"; |
|
88 | - if ($this->characterSet != '') { |
|
89 | - $dSN .= ';charset='.$this->characterSet; |
|
90 | - } |
|
91 | - if ($this->linkId === false) { |
|
92 | - try { |
|
93 | - $this->linkId = new PDO($dSN, $user, $password); |
|
94 | - } catch (\PDOException $e) { |
|
95 | - $this->halt('Connection Failed '.$e->getMessage()); |
|
96 | - return 0; |
|
97 | - } |
|
98 | - } |
|
99 | - return $this->linkId; |
|
100 | - } |
|
101 | - |
|
102 | - /* This only affects systems not using persistent connections */ |
|
103 | - |
|
104 | - /** |
|
105 | - * Db::disconnect() |
|
106 | - * @return void |
|
107 | - */ |
|
108 | - public function disconnect() |
|
109 | - { |
|
110 | - } |
|
111 | - |
|
112 | - /* public: discard the query result */ |
|
113 | - |
|
114 | - /** |
|
115 | - * Db::free() |
|
116 | - * @return void |
|
117 | - */ |
|
118 | - public function free() |
|
119 | - { |
|
120 | - // @mysql_free_result($this->queryId); |
|
121 | - // $this->queryId = 0; |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * Db::query() |
|
126 | - * |
|
127 | - * Sends an SQL query to the database |
|
128 | - * |
|
129 | - * @param mixed $queryString |
|
130 | - * @param string $line |
|
131 | - * @param string $file |
|
132 | - * @return mixed 0 if no query or query id handler, safe to ignore this return |
|
133 | - */ |
|
134 | - public function query($queryString, $line = '', $file = '') |
|
135 | - { |
|
136 | - /* No empty queries, please, since PHP4 chokes on them. */ |
|
137 | - /* The empty query string is passed on from the constructor, |
|
23 | + /* public: connection parameters */ |
|
24 | + public $driver = 'mysql'; |
|
25 | + public $Rows = []; |
|
26 | + /* public: this is an api revision, not a CVS revision. */ |
|
27 | + public $type = 'pdo'; |
|
28 | + |
|
29 | + /** |
|
30 | + * changes the database we are working with. |
|
31 | + * |
|
32 | + * @param string $database the name of the database to use |
|
33 | + * @return void |
|
34 | + */ |
|
35 | + public function selectDb($database) |
|
36 | + { |
|
37 | + $dSN = "{$this->driver}:dbname={$database};host={$this->host}"; |
|
38 | + if ($this->characterSet != '') { |
|
39 | + $dSN .= ';charset='.$this->characterSet; |
|
40 | + } |
|
41 | + $this->linkId = new PDO($dSN, $this->user, $this->password); |
|
42 | + $this->database = $database; |
|
43 | + } |
|
44 | + |
|
45 | + |
|
46 | + /** |
|
47 | + * alias function of select_db, changes the database we are working with. |
|
48 | + * |
|
49 | + * @param string $database the name of the database to use |
|
50 | + * @return void |
|
51 | + */ |
|
52 | + public function useDb($database) |
|
53 | + { |
|
54 | + $this->selectDb($database); |
|
55 | + } |
|
56 | + |
|
57 | + /* public: connection management */ |
|
58 | + |
|
59 | + /** |
|
60 | + * Db::connect() |
|
61 | + * @param string $database |
|
62 | + * @param string $host |
|
63 | + * @param string $user |
|
64 | + * @param string $password |
|
65 | + * @param string $driver |
|
66 | + * @return bool|int|PDO |
|
67 | + */ |
|
68 | + public function connect($database = '', $host = '', $user = '', $password = '', $driver = 'mysql') |
|
69 | + { |
|
70 | + /* Handle defaults */ |
|
71 | + if ('' == $database) { |
|
72 | + $database = $this->database; |
|
73 | + } |
|
74 | + if ('' == $host) { |
|
75 | + $host = $this->host; |
|
76 | + } |
|
77 | + if ('' == $user) { |
|
78 | + $user = $this->user; |
|
79 | + } |
|
80 | + if ('' == $password) { |
|
81 | + $password = $this->password; |
|
82 | + } |
|
83 | + if ('' == $driver) { |
|
84 | + $driver = $this->driver; |
|
85 | + } |
|
86 | + /* establish connection, select database */ |
|
87 | + $dSN = "{$driver}:dbname={$database};host={$host}"; |
|
88 | + if ($this->characterSet != '') { |
|
89 | + $dSN .= ';charset='.$this->characterSet; |
|
90 | + } |
|
91 | + if ($this->linkId === false) { |
|
92 | + try { |
|
93 | + $this->linkId = new PDO($dSN, $user, $password); |
|
94 | + } catch (\PDOException $e) { |
|
95 | + $this->halt('Connection Failed '.$e->getMessage()); |
|
96 | + return 0; |
|
97 | + } |
|
98 | + } |
|
99 | + return $this->linkId; |
|
100 | + } |
|
101 | + |
|
102 | + /* This only affects systems not using persistent connections */ |
|
103 | + |
|
104 | + /** |
|
105 | + * Db::disconnect() |
|
106 | + * @return void |
|
107 | + */ |
|
108 | + public function disconnect() |
|
109 | + { |
|
110 | + } |
|
111 | + |
|
112 | + /* public: discard the query result */ |
|
113 | + |
|
114 | + /** |
|
115 | + * Db::free() |
|
116 | + * @return void |
|
117 | + */ |
|
118 | + public function free() |
|
119 | + { |
|
120 | + // @mysql_free_result($this->queryId); |
|
121 | + // $this->queryId = 0; |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * Db::query() |
|
126 | + * |
|
127 | + * Sends an SQL query to the database |
|
128 | + * |
|
129 | + * @param mixed $queryString |
|
130 | + * @param string $line |
|
131 | + * @param string $file |
|
132 | + * @return mixed 0 if no query or query id handler, safe to ignore this return |
|
133 | + */ |
|
134 | + public function query($queryString, $line = '', $file = '') |
|
135 | + { |
|
136 | + /* No empty queries, please, since PHP4 chokes on them. */ |
|
137 | + /* The empty query string is passed on from the constructor, |
|
138 | 138 | * when calling the class without a query, e.g. in situations |
139 | 139 | * like these: '$db = new db_Subclass;' |
140 | 140 | */ |
141 | - if ($queryString == '') { |
|
142 | - return 0; |
|
143 | - } |
|
144 | - if (!$this->connect()) { |
|
145 | - return 0; |
|
146 | - /* we already complained in connect() about that. */ |
|
147 | - } |
|
148 | - // New query, discard previous result. |
|
149 | - if ($this->queryId !== false) { |
|
150 | - $this->free(); |
|
151 | - } |
|
152 | - |
|
153 | - if ($this->Debug) { |
|
154 | - printf("Debug: query = %s<br>\n", $queryString); |
|
155 | - } |
|
156 | - if (isset($GLOBALS['log_queries']) && $GLOBALS['log_queries'] !== false) { |
|
157 | - $this->log($queryString, $line, $file); |
|
158 | - } |
|
159 | - |
|
160 | - $this->queryId = $this->linkId->prepare($queryString); |
|
161 | - $success = $this->queryId->execute(); |
|
162 | - $this->Rows = $this->queryId->fetchAll(); |
|
163 | - //$this->log("PDO Query $queryString (S:$success) - ".count($this->Rows).' Rows', __LINE__, __FILE__); |
|
164 | - $this->Row = -1; |
|
165 | - if ($success === false) { |
|
166 | - $this->emailError($queryString, json_encode($this->queryId->errorInfo(), JSON_PRETTY_PRINT), $line, $file); |
|
167 | - } |
|
168 | - |
|
169 | - // Will return nada if it fails. That's fine. |
|
170 | - return $this->queryId; |
|
171 | - } |
|
172 | - |
|
173 | - /* public: walk result set */ |
|
174 | - |
|
175 | - /** |
|
176 | - * Db::next_record() |
|
177 | - * @param mixed $resultType |
|
178 | - * @return bool |
|
179 | - */ |
|
180 | - public function next_record($resultType = MYSQLI_ASSOC) |
|
181 | - { |
|
182 | - // PDO result types so far seem to be +1 |
|
183 | - ++$resultType; |
|
184 | - if (!$this->queryId) { |
|
185 | - $this->halt('next_record called with no query pending.'); |
|
186 | - return 0; |
|
187 | - } |
|
188 | - |
|
189 | - ++$this->Row; |
|
190 | - $this->Record = $this->Rows[$this->Row]; |
|
191 | - |
|
192 | - $stat = is_array($this->Record); |
|
193 | - if (!$stat && $this->autoFree) { |
|
194 | - $this->free(); |
|
195 | - } |
|
196 | - return $stat; |
|
197 | - } |
|
198 | - |
|
199 | - /* public: position in result set */ |
|
200 | - |
|
201 | - /** |
|
202 | - * Db::seek() |
|
203 | - * @param integer $pos |
|
204 | - * @return int |
|
205 | - */ |
|
206 | - public function seek($pos = 0) |
|
207 | - { |
|
208 | - if (isset($this->Rows[$pos])) { |
|
209 | - $this->Row = $pos; |
|
210 | - } else { |
|
211 | - $this->halt("seek($pos) failed: result has ".count($this->Rows).' rows'); |
|
212 | - /* half assed attempt to save the day, |
|
141 | + if ($queryString == '') { |
|
142 | + return 0; |
|
143 | + } |
|
144 | + if (!$this->connect()) { |
|
145 | + return 0; |
|
146 | + /* we already complained in connect() about that. */ |
|
147 | + } |
|
148 | + // New query, discard previous result. |
|
149 | + if ($this->queryId !== false) { |
|
150 | + $this->free(); |
|
151 | + } |
|
152 | + |
|
153 | + if ($this->Debug) { |
|
154 | + printf("Debug: query = %s<br>\n", $queryString); |
|
155 | + } |
|
156 | + if (isset($GLOBALS['log_queries']) && $GLOBALS['log_queries'] !== false) { |
|
157 | + $this->log($queryString, $line, $file); |
|
158 | + } |
|
159 | + |
|
160 | + $this->queryId = $this->linkId->prepare($queryString); |
|
161 | + $success = $this->queryId->execute(); |
|
162 | + $this->Rows = $this->queryId->fetchAll(); |
|
163 | + //$this->log("PDO Query $queryString (S:$success) - ".count($this->Rows).' Rows', __LINE__, __FILE__); |
|
164 | + $this->Row = -1; |
|
165 | + if ($success === false) { |
|
166 | + $this->emailError($queryString, json_encode($this->queryId->errorInfo(), JSON_PRETTY_PRINT), $line, $file); |
|
167 | + } |
|
168 | + |
|
169 | + // Will return nada if it fails. That's fine. |
|
170 | + return $this->queryId; |
|
171 | + } |
|
172 | + |
|
173 | + /* public: walk result set */ |
|
174 | + |
|
175 | + /** |
|
176 | + * Db::next_record() |
|
177 | + * @param mixed $resultType |
|
178 | + * @return bool |
|
179 | + */ |
|
180 | + public function next_record($resultType = MYSQLI_ASSOC) |
|
181 | + { |
|
182 | + // PDO result types so far seem to be +1 |
|
183 | + ++$resultType; |
|
184 | + if (!$this->queryId) { |
|
185 | + $this->halt('next_record called with no query pending.'); |
|
186 | + return 0; |
|
187 | + } |
|
188 | + |
|
189 | + ++$this->Row; |
|
190 | + $this->Record = $this->Rows[$this->Row]; |
|
191 | + |
|
192 | + $stat = is_array($this->Record); |
|
193 | + if (!$stat && $this->autoFree) { |
|
194 | + $this->free(); |
|
195 | + } |
|
196 | + return $stat; |
|
197 | + } |
|
198 | + |
|
199 | + /* public: position in result set */ |
|
200 | + |
|
201 | + /** |
|
202 | + * Db::seek() |
|
203 | + * @param integer $pos |
|
204 | + * @return int |
|
205 | + */ |
|
206 | + public function seek($pos = 0) |
|
207 | + { |
|
208 | + if (isset($this->Rows[$pos])) { |
|
209 | + $this->Row = $pos; |
|
210 | + } else { |
|
211 | + $this->halt("seek($pos) failed: result has ".count($this->Rows).' rows'); |
|
212 | + /* half assed attempt to save the day, |
|
213 | 213 | * but do not consider this documented or even |
214 | 214 | * desirable behaviour. |
215 | 215 | */ |
216 | - return 0; |
|
217 | - } |
|
218 | - return 1; |
|
219 | - } |
|
220 | - |
|
221 | - /** |
|
222 | - * Initiates a transaction |
|
223 | - * @return bool |
|
224 | - */ |
|
225 | - public function transactionBegin() |
|
226 | - { |
|
227 | - return $this->linkId->beginTransaction(); |
|
228 | - } |
|
229 | - |
|
230 | - /** |
|
231 | - * Commits a transaction |
|
232 | - * @return bool |
|
233 | - */ |
|
234 | - public function transactionCommit() |
|
235 | - { |
|
236 | - return $this->linkId->commit(); |
|
237 | - } |
|
238 | - |
|
239 | - /** |
|
240 | - * Rolls back a transaction |
|
241 | - * @return bool |
|
242 | - */ |
|
243 | - public function transactionAbort() |
|
244 | - { |
|
245 | - return $this->linkId->rollBack(); |
|
246 | - } |
|
247 | - |
|
248 | - /** |
|
249 | - * Db::getLastInsertId() |
|
250 | - * @param mixed $table |
|
251 | - * @param mixed $field |
|
252 | - * @return int |
|
253 | - */ |
|
254 | - public function getLastInsertId($table, $field) |
|
255 | - { |
|
256 | - if (!isset($table) || $table == '' || !isset($field) || $field == '') { |
|
257 | - return -1; |
|
258 | - } |
|
259 | - return $this->linkId->lastInsertId(); |
|
260 | - } |
|
261 | - |
|
262 | - /* public: table locking */ |
|
263 | - |
|
264 | - /** |
|
265 | - * Db::lock() |
|
266 | - * @param mixed $table |
|
267 | - * @param string $mode |
|
268 | - * @return void |
|
269 | - */ |
|
270 | - public function lock($table, $mode = 'write') |
|
271 | - { |
|
272 | - /* $this->connect(); |
|
216 | + return 0; |
|
217 | + } |
|
218 | + return 1; |
|
219 | + } |
|
220 | + |
|
221 | + /** |
|
222 | + * Initiates a transaction |
|
223 | + * @return bool |
|
224 | + */ |
|
225 | + public function transactionBegin() |
|
226 | + { |
|
227 | + return $this->linkId->beginTransaction(); |
|
228 | + } |
|
229 | + |
|
230 | + /** |
|
231 | + * Commits a transaction |
|
232 | + * @return bool |
|
233 | + */ |
|
234 | + public function transactionCommit() |
|
235 | + { |
|
236 | + return $this->linkId->commit(); |
|
237 | + } |
|
238 | + |
|
239 | + /** |
|
240 | + * Rolls back a transaction |
|
241 | + * @return bool |
|
242 | + */ |
|
243 | + public function transactionAbort() |
|
244 | + { |
|
245 | + return $this->linkId->rollBack(); |
|
246 | + } |
|
247 | + |
|
248 | + /** |
|
249 | + * Db::getLastInsertId() |
|
250 | + * @param mixed $table |
|
251 | + * @param mixed $field |
|
252 | + * @return int |
|
253 | + */ |
|
254 | + public function getLastInsertId($table, $field) |
|
255 | + { |
|
256 | + if (!isset($table) || $table == '' || !isset($field) || $field == '') { |
|
257 | + return -1; |
|
258 | + } |
|
259 | + return $this->linkId->lastInsertId(); |
|
260 | + } |
|
261 | + |
|
262 | + /* public: table locking */ |
|
263 | + |
|
264 | + /** |
|
265 | + * Db::lock() |
|
266 | + * @param mixed $table |
|
267 | + * @param string $mode |
|
268 | + * @return void |
|
269 | + */ |
|
270 | + public function lock($table, $mode = 'write') |
|
271 | + { |
|
272 | + /* $this->connect(); |
|
273 | 273 | |
274 | 274 | * $query = "lock tables "; |
275 | 275 | * if (is_array($table)) |
@@ -299,15 +299,15 @@ discard block |
||
299 | 299 | * } |
300 | 300 | * return $res; |
301 | 301 | */ |
302 | - } |
|
302 | + } |
|
303 | 303 | |
304 | - /** |
|
305 | - * Db::unlock() |
|
306 | - * @return void |
|
307 | - */ |
|
308 | - public function unlock() |
|
309 | - { |
|
310 | - /* $this->connect(); |
|
304 | + /** |
|
305 | + * Db::unlock() |
|
306 | + * @return void |
|
307 | + */ |
|
308 | + public function unlock() |
|
309 | + { |
|
310 | + /* $this->connect(); |
|
311 | 311 | |
312 | 312 | * $res = @mysql_query("unlock tables"); |
313 | 313 | * if (!$res) |
@@ -317,68 +317,68 @@ discard block |
||
317 | 317 | * } |
318 | 318 | * return $res; |
319 | 319 | */ |
320 | - } |
|
321 | - |
|
322 | - /* public: evaluate the result (size, width) */ |
|
323 | - |
|
324 | - /** |
|
325 | - * Db::affectedRows() |
|
326 | - * |
|
327 | - * @return mixed |
|
328 | - */ |
|
329 | - public function affectedRows() |
|
330 | - { |
|
331 | - return @$this->queryId->rowCount(); |
|
332 | - } |
|
333 | - |
|
334 | - /** |
|
335 | - * Db::num_rows() |
|
336 | - * @return int |
|
337 | - */ |
|
338 | - public function num_rows() |
|
339 | - { |
|
340 | - return count($this->Rows); |
|
341 | - } |
|
342 | - |
|
343 | - /** |
|
344 | - * Db::num_fields() |
|
345 | - * @return int |
|
346 | - */ |
|
347 | - public function num_fields() |
|
348 | - { |
|
349 | - $keys = array_keys($this->Rows); |
|
350 | - return count($this->Rows[$keys[0]]); |
|
351 | - } |
|
352 | - |
|
353 | - /** |
|
354 | - * @param mixed $msg |
|
355 | - * @param string $line |
|
356 | - * @param string $file |
|
357 | - * @return mixed|void |
|
358 | - */ |
|
359 | - public function haltmsg($msg, $line = '', $file = '') |
|
360 | - { |
|
361 | - $this->log("Database error: $msg", $line, $file, 'error'); |
|
362 | - if ($this->Errno != '0' || $this->Error != '()') { |
|
363 | - $this->log('PDO MySQL Error: '.json_encode($this->linkId->errorInfo()), $line, $file, 'error'); |
|
364 | - } |
|
365 | - $this->logBackTrace($msg, $line, $file); |
|
366 | - } |
|
367 | - |
|
368 | - /** |
|
369 | - * Db::tableNames() |
|
370 | - * |
|
371 | - * @return array |
|
372 | - */ |
|
373 | - public function tableNames() |
|
374 | - { |
|
375 | - $return = []; |
|
376 | - $this->query('SHOW TABLES'); |
|
377 | - foreach ($this->Rows as $i => $info) { |
|
378 | - $return[$i]['table_name'] = $info[0]; |
|
379 | - $return[$i]['tablespace_name'] = $this->database; |
|
380 | - $return[$i]['database'] = $this->database; |
|
381 | - } |
|
382 | - return $return; |
|
383 | - } |
|
320 | + } |
|
321 | + |
|
322 | + /* public: evaluate the result (size, width) */ |
|
323 | + |
|
324 | + /** |
|
325 | + * Db::affectedRows() |
|
326 | + * |
|
327 | + * @return mixed |
|
328 | + */ |
|
329 | + public function affectedRows() |
|
330 | + { |
|
331 | + return @$this->queryId->rowCount(); |
|
332 | + } |
|
333 | + |
|
334 | + /** |
|
335 | + * Db::num_rows() |
|
336 | + * @return int |
|
337 | + */ |
|
338 | + public function num_rows() |
|
339 | + { |
|
340 | + return count($this->Rows); |
|
341 | + } |
|
342 | + |
|
343 | + /** |
|
344 | + * Db::num_fields() |
|
345 | + * @return int |
|
346 | + */ |
|
347 | + public function num_fields() |
|
348 | + { |
|
349 | + $keys = array_keys($this->Rows); |
|
350 | + return count($this->Rows[$keys[0]]); |
|
351 | + } |
|
352 | + |
|
353 | + /** |
|
354 | + * @param mixed $msg |
|
355 | + * @param string $line |
|
356 | + * @param string $file |
|
357 | + * @return mixed|void |
|
358 | + */ |
|
359 | + public function haltmsg($msg, $line = '', $file = '') |
|
360 | + { |
|
361 | + $this->log("Database error: $msg", $line, $file, 'error'); |
|
362 | + if ($this->Errno != '0' || $this->Error != '()') { |
|
363 | + $this->log('PDO MySQL Error: '.json_encode($this->linkId->errorInfo()), $line, $file, 'error'); |
|
364 | + } |
|
365 | + $this->logBackTrace($msg, $line, $file); |
|
366 | + } |
|
367 | + |
|
368 | + /** |
|
369 | + * Db::tableNames() |
|
370 | + * |
|
371 | + * @return array |
|
372 | + */ |
|
373 | + public function tableNames() |
|
374 | + { |
|
375 | + $return = []; |
|
376 | + $this->query('SHOW TABLES'); |
|
377 | + foreach ($this->Rows as $i => $info) { |
|
378 | + $return[$i]['table_name'] = $info[0]; |
|
379 | + $return[$i]['tablespace_name'] = $this->database; |
|
380 | + $return[$i]['database'] = $this->database; |
|
381 | + } |
|
382 | + return $return; |
|
383 | + } |
|
384 | 384 | } |
@@ -14,385 +14,385 @@ |
||
14 | 14 | */ |
15 | 15 | abstract class Generic |
16 | 16 | { |
17 | - /* public: connection parameters */ |
|
18 | - public $host = 'localhost'; |
|
19 | - public $database = ''; |
|
20 | - public $user = ''; |
|
21 | - public $password = ''; |
|
22 | - public $port = ''; |
|
23 | - |
|
24 | - /* public: configuration parameters */ |
|
25 | - public $autoStripslashes = false; |
|
26 | - public $Debug = 0; // Set to 1 for debugging messages. |
|
27 | - public $haltOnError = 'yes'; // "yes" (halt with message), "no" (ignore errors quietly), "report" (ignore error, but spit a warning) |
|
28 | - |
|
29 | - public $maxConnectErrors = 5; |
|
30 | - public $connectionAttempt = 0; |
|
31 | - public $maxMatches = 10000000; |
|
32 | - |
|
33 | - /** |
|
34 | - * @var int |
|
35 | - */ |
|
36 | - public $autoFree = 0; // Set to 1 for automatic mysql_free_result() |
|
37 | - |
|
38 | - /* public: result array and current row number */ |
|
39 | - public $Record = []; |
|
40 | - public $Row; |
|
41 | - |
|
42 | - /* public: current error number and error text */ |
|
43 | - public $Errno = 0; |
|
44 | - public $Error = ''; |
|
45 | - |
|
46 | - /* public: this is an api revision, not a CVS revision. */ |
|
47 | - public $type = 'generic'; |
|
48 | - |
|
49 | - /** |
|
50 | - * @var int|object |
|
51 | - */ |
|
52 | - public $linkId = 0; |
|
53 | - public $queryId = 0; |
|
54 | - |
|
55 | - public $characterSet = 'utf8mb4'; |
|
56 | - public $collation = 'utf8mb4_unicode_ci'; |
|
57 | - |
|
58 | - /** |
|
59 | - * Logged queries. |
|
60 | - * @var array |
|
61 | - */ |
|
62 | - protected $log = []; |
|
63 | - |
|
64 | - /** |
|
65 | - * Constructs the db handler, can optionally specify connection parameters |
|
66 | - * |
|
67 | - * @param string $database Optional The database name |
|
68 | - * @param string $user Optional The username to connect with |
|
69 | - * @param string $password Optional The password to use |
|
70 | - * @param string $host Optional The hostname where the server is, or default to localhost |
|
71 | - * @param string $query Optional query to perform immediately |
|
72 | - * @param string $port optional port for the connection |
|
73 | - */ |
|
74 | - public function __construct($database = '', $user = '', $password = '', $host = 'localhost', $query = '', $port = '') |
|
75 | - { |
|
76 | - $this->database = $database; |
|
77 | - $this->user = $user; |
|
78 | - $this->password = $password; |
|
79 | - $this->host = $host; |
|
80 | - $this->port = $port; |
|
81 | - if ($query != '') { |
|
82 | - $this->query($query); |
|
83 | - } |
|
84 | - $this->connection_atttempt = 0; |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * @param string $message |
|
89 | - * @param string $line |
|
90 | - * @param string $file |
|
91 | - * @return void |
|
92 | - */ |
|
93 | - public function log($message, $line = '', $file = '', $level = 'info') |
|
94 | - { |
|
95 | - error_log($message); |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * @return int|object |
|
100 | - */ |
|
101 | - public function linkId() |
|
102 | - { |
|
103 | - return $this->linkId; |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * @return int|object |
|
108 | - */ |
|
109 | - public function queryId() |
|
110 | - { |
|
111 | - return $this->queryId; |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * @param $string |
|
116 | - * @return string |
|
117 | - */ |
|
118 | - public function real_escape($string = '') |
|
119 | - { |
|
120 | - if ((!is_resource($this->linkId) || $this->linkId == 0) && !$this->connect()) { |
|
121 | - return $this->escape($string); |
|
122 | - } |
|
123 | - return mysqli_real_escape_string($this->linkId, $string); |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * @param $string |
|
128 | - * @return string |
|
129 | - */ |
|
130 | - public function escape($string = '') |
|
131 | - { |
|
132 | - //if (function_exists('mysql_escape_string')) |
|
133 | - //return mysql_escape_string($string); |
|
134 | - return str_replace(['\\', "\0", "\n", "\r", "'", '"', "\x1a"], ['\\\\', '\\0', '\\n', '\\r', "\\'", '\\"', '\\Z'], $string); |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * @param mixed $str |
|
139 | - * @return string |
|
140 | - */ |
|
141 | - public function dbAddslashes($str = '') |
|
142 | - { |
|
143 | - if (!isset($str) || $str == '') { |
|
144 | - return ''; |
|
145 | - } |
|
146 | - return addslashes($str); |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * Db::toTimestamp() |
|
151 | - * @param mixed $epoch |
|
152 | - * @return bool|string |
|
153 | - */ |
|
154 | - public function toTimestamp($epoch) |
|
155 | - { |
|
156 | - return date('Y-m-d H:i:s', is_float($epoch) ? intval($epoch) : $epoch); |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * Db::fromTimestamp() |
|
161 | - * @param mixed $timestamp |
|
162 | - * @return bool|int|mixed |
|
163 | - */ |
|
164 | - public function fromTimestamp($timestamp) |
|
165 | - { |
|
166 | - if (preg_match('/([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})/', $timestamp, $parts)) { |
|
167 | - $time = mktime($parts[4], $parts[5], $parts[6], $parts[2], $parts[3], $parts[1]); |
|
168 | - } elseif (preg_match('/([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})/', $timestamp, $parts)) { |
|
169 | - $time = mktime($parts[4], $parts[5], $parts[6], $parts[2], $parts[3], $parts[1]); |
|
170 | - } elseif (preg_match('/([0-9]{4})([0-9]{2})([0-9]{2})/', $timestamp, $parts)) { |
|
171 | - $time = mktime(1, 1, 1, $parts[2], $parts[3], $parts[1]); |
|
172 | - } elseif (is_numeric($timestamp) && $timestamp >= 943938000) { |
|
173 | - $time = $timestamp; |
|
174 | - } else { |
|
175 | - $this->log('Cannot Match Timestamp from '.$timestamp, __LINE__, __FILE__); |
|
176 | - $time = false; |
|
177 | - } |
|
178 | - return $time; |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * perform a query with limited result set |
|
183 | - * |
|
184 | - * @param string $queryString |
|
185 | - * @param string|int $numRows |
|
186 | - * @param int $offset |
|
187 | - * @param string|int $line |
|
188 | - * @param string $file |
|
189 | - * @return mixed |
|
190 | - */ |
|
191 | - public function limitQuery($queryString, $numRows = '', $offset = 0, $line = '', $file = '') |
|
192 | - { |
|
193 | - if (!$numRows) { |
|
194 | - $numRows = $this->maxMatches; |
|
195 | - } |
|
196 | - if ($offset == 0) { |
|
197 | - $queryString .= ' LIMIT '.(int) $numRows; |
|
198 | - } else { |
|
199 | - $queryString .= ' LIMIT '.(int) $offset.','.(int) $numRows; |
|
200 | - } |
|
201 | - |
|
202 | - if ($this->Debug) { |
|
203 | - printf("Debug: limitQuery = %s<br>offset=%d, num_rows=%d<br>\n", $queryString, $offset, $numRows); |
|
204 | - } |
|
205 | - |
|
206 | - return $this->query($queryString, $line, $file); |
|
207 | - } |
|
208 | - |
|
209 | - /** |
|
210 | - * db:qr() |
|
211 | - * |
|
212 | - * alias of queryReturn() |
|
213 | - * |
|
214 | - * @param mixed $query SQL Query to be used |
|
215 | - * @param string $line optionally pass __LINE__ calling the query for logging |
|
216 | - * @param string $file optionally pass __FILE__ calling the query for logging |
|
217 | - * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
|
218 | - */ |
|
219 | - public function qr($query, $line = '', $file = '') |
|
220 | - { |
|
221 | - return $this->queryReturn($query, $line, $file); |
|
222 | - } |
|
223 | - |
|
224 | - /** |
|
225 | - * gets a field |
|
226 | - * |
|
227 | - * @param mixed $name |
|
228 | - * @param string $stripSlashes |
|
229 | - * @return string |
|
230 | - */ |
|
231 | - public function f($name, $stripSlashes = '') |
|
232 | - { |
|
233 | - if (is_null($this->Record)) { |
|
234 | - return null; |
|
235 | - } elseif ($stripSlashes || ($this->autoStripslashes && !$stripSlashes)) { |
|
236 | - return stripslashes($this->Record[$name]); |
|
237 | - } else { |
|
238 | - return $this->Record[$name]; |
|
239 | - } |
|
240 | - } |
|
241 | - |
|
242 | - /** |
|
243 | - * error handling |
|
244 | - * |
|
245 | - * @param mixed $msg |
|
246 | - * @param string $line |
|
247 | - * @param string $file |
|
248 | - * @return void |
|
249 | - */ |
|
250 | - public function halt($msg, $line = '', $file = '') |
|
251 | - { |
|
252 | - $this->unlock(false); |
|
253 | - /* Just in case there is a table currently locked */ |
|
254 | - |
|
255 | - //$this->Error = @$this->linkId->error; |
|
256 | - //$this->Errno = @$this->linkId->errno; |
|
257 | - if ($this->haltOnError == 'no') { |
|
258 | - return true; |
|
259 | - } |
|
260 | - if ($msg != '') { |
|
261 | - $this->haltmsg($msg, $line, $file); |
|
262 | - } |
|
263 | - if ($this->haltOnError != 'report') { |
|
264 | - echo '<p><b>Session halted.</b>'; |
|
265 | - //if (isset($GLOBALS['tf'])) |
|
266 | - //$GLOBALS['tf']->terminate(); |
|
267 | - die(); |
|
268 | - } |
|
269 | - return true; |
|
270 | - } |
|
271 | - |
|
272 | - /** |
|
273 | - * @param mixed $msg |
|
274 | - * @param string $line |
|
275 | - * @param string $file |
|
276 | - * @return mixed|void |
|
277 | - */ |
|
278 | - public function logBackTrace($msg, $line = '', $file = '') |
|
279 | - { |
|
280 | - $backtrace = (function_exists('debug_backtrace') ? debug_backtrace() : []); |
|
281 | - $this->log( |
|
282 | - ('' !== getenv('REQUEST_URI') ? ' '.getenv('REQUEST_URI') : ''). |
|
283 | - ((isset($_POST) && count($_POST)) ? ' POST='.json_encode($_POST) : ''). |
|
284 | - ((isset($_GET) && count($_GET)) ? ' GET='.json_encode($_GET) : ''). |
|
285 | - ((isset($_FILES) && count($_FILES)) ? ' FILES='.json_encode($_FILES) : ''). |
|
286 | - ('' !== getenv('HTTP_USER_AGENT') ? ' AGENT="'.getenv('HTTP_USER_AGENT').'"' : ''). |
|
287 | - (isset($_SERVER['REQUEST_METHOD']) ? ' METHOD="'.$_SERVER['REQUEST_METHOD'].'"'. |
|
288 | - ($_SERVER['REQUEST_METHOD'] === 'POST' ? ' POST="'.json_encode($_POST).'"' : '') : ''), |
|
289 | - $line, |
|
290 | - $file, |
|
291 | - 'error' |
|
292 | - ); |
|
293 | - for ($level = 1, $levelMax = count($backtrace); $level < $levelMax; $level++) { |
|
294 | - $message = (isset($backtrace[$level]['file']) ? 'File: '.$backtrace[$level]['file'] : ''). |
|
295 | - (isset($backtrace[$level]['line']) ? ' Line: '.$backtrace[$level]['line'] : ''). |
|
296 | - ' Function: '.(isset($backtrace[$level] ['class']) ? '(class '.$backtrace[$level] ['class'].') ' : ''). |
|
297 | - (isset($backtrace[$level] ['type']) ? $backtrace[$level] ['type'].' ' : ''). |
|
298 | - $backtrace[$level] ['function'].'('; |
|
299 | - if (isset($backtrace[$level] ['args'])) { |
|
300 | - for ($argument = 0, $argumentMax = count($backtrace[$level]['args']); $argument < $argumentMax; $argument++) { |
|
301 | - $message .= ($argument > 0 ? ', ' : ''). |
|
302 | - (is_object($backtrace[$level]['args'][$argument]) ? 'class '.get_class($backtrace[$level]['args'][$argument]) : json_encode($backtrace[$level]['args'][$argument])); |
|
303 | - } |
|
304 | - } |
|
305 | - $message .= ')'; |
|
306 | - $this->log($message, $line, $file, 'error'); |
|
307 | - } |
|
308 | - } |
|
309 | - |
|
310 | - public function emailError($queryString, $error, $line, $file) |
|
311 | - { |
|
312 | - $subject = php_uname('n').' MySQLi Error '.$queryString; |
|
313 | - if (class_exists('\\TFSmarty')) { |
|
314 | - $smarty = new \TFSmarty(); |
|
315 | - $smarty->assign([ |
|
316 | - 'type' => $this->type, |
|
317 | - 'queryString' => $queryString, |
|
318 | - 'error' => $error, |
|
319 | - 'line' => $line, |
|
320 | - 'file' => $file, |
|
321 | - 'request' => $_REQUEST, |
|
322 | - 'server' => $_SERVER, |
|
323 | - ]); |
|
324 | - if (isset($GLOBALS['tf'])) { |
|
325 | - $smarty->assign('account_id', $GLOBALS['tf']->session->account_id); |
|
326 | - } |
|
327 | - $email = $smarty->fetch('email/admin/sql_error.tpl'); |
|
328 | - (new \MyAdmin\Mail())->adminMail($subject, $email, '[email protected]', ''); |
|
329 | - (new \MyAdmin\Mail())->adminMail($subject, $email, '[email protected]', ''); |
|
330 | - } |
|
331 | - $this->haltmsg('Invalid SQL: '.$queryString, $line, $file); |
|
332 | - } |
|
333 | - |
|
334 | - /** |
|
335 | - * @param mixed $msg |
|
336 | - * @param string $line |
|
337 | - * @param string $file |
|
338 | - * @return mixed|void |
|
339 | - */ |
|
340 | - public function haltmsg($msg, $line = '', $file = '') |
|
341 | - { |
|
342 | - $email = "DB Error {$msg} {$file}:{$line}"; |
|
343 | - if (class_exists('\\MyAdmin\Mail')) { |
|
344 | - \MyAdmin\Mail::failsafeMail($email, $email, ['[email protected]', '[email protected]']); |
|
345 | - return; |
|
346 | - } |
|
347 | - $this->log("Database error: $msg", $line, $file, 'error'); |
|
348 | - if ($this->Errno != '0' || !in_array($this->Error, ['', '()'])) { |
|
349 | - $sqlstate = mysqli_sqlstate($this->linkId); |
|
350 | - $this->log("MySQLi SQLState: {$sqlstate}. Error: ".$this->Errno.' ('.$this->Error.')', $line, $file, 'error'); |
|
351 | - } |
|
352 | - $this->logBackTrace($msg, $line, $file); |
|
353 | - } |
|
354 | - |
|
355 | - /** |
|
356 | - * @return array |
|
357 | - */ |
|
358 | - public function indexNames() |
|
359 | - { |
|
360 | - return []; |
|
361 | - } |
|
362 | - |
|
363 | - |
|
364 | - /** |
|
365 | - * Add query to logged queries. |
|
366 | - * @param string $statement |
|
367 | - * @param float $time Elapsed seconds with microseconds |
|
368 | - * @param string|int $line Line Number |
|
369 | - * @param string $file File Name |
|
370 | - */ |
|
371 | - public function addLog($statement, $time, $line = '', $file = '') |
|
372 | - { |
|
373 | - $query = [ |
|
374 | - 'statement' => $statement, |
|
375 | - 'time' => $time * 1000 |
|
376 | - ]; |
|
377 | - if ($line != '') { |
|
378 | - $query['line'] = $line; |
|
379 | - } |
|
380 | - if ($file != '') { |
|
381 | - $query['file'] = $file; |
|
382 | - } |
|
383 | - if (!isset($GLOBALS['db_queries'])) { |
|
384 | - $GLOBALS['db_queries'] = []; |
|
385 | - } |
|
386 | - $GLOBALS['db_queries'][] = $query; |
|
387 | - array_push($this->log, $query); |
|
388 | - } |
|
389 | - |
|
390 | - /** |
|
391 | - * Return logged queries. |
|
392 | - * @return array Logged queries |
|
393 | - */ |
|
394 | - public function getLog() |
|
395 | - { |
|
396 | - return $this->log; |
|
397 | - } |
|
17 | + /* public: connection parameters */ |
|
18 | + public $host = 'localhost'; |
|
19 | + public $database = ''; |
|
20 | + public $user = ''; |
|
21 | + public $password = ''; |
|
22 | + public $port = ''; |
|
23 | + |
|
24 | + /* public: configuration parameters */ |
|
25 | + public $autoStripslashes = false; |
|
26 | + public $Debug = 0; // Set to 1 for debugging messages. |
|
27 | + public $haltOnError = 'yes'; // "yes" (halt with message), "no" (ignore errors quietly), "report" (ignore error, but spit a warning) |
|
28 | + |
|
29 | + public $maxConnectErrors = 5; |
|
30 | + public $connectionAttempt = 0; |
|
31 | + public $maxMatches = 10000000; |
|
32 | + |
|
33 | + /** |
|
34 | + * @var int |
|
35 | + */ |
|
36 | + public $autoFree = 0; // Set to 1 for automatic mysql_free_result() |
|
37 | + |
|
38 | + /* public: result array and current row number */ |
|
39 | + public $Record = []; |
|
40 | + public $Row; |
|
41 | + |
|
42 | + /* public: current error number and error text */ |
|
43 | + public $Errno = 0; |
|
44 | + public $Error = ''; |
|
45 | + |
|
46 | + /* public: this is an api revision, not a CVS revision. */ |
|
47 | + public $type = 'generic'; |
|
48 | + |
|
49 | + /** |
|
50 | + * @var int|object |
|
51 | + */ |
|
52 | + public $linkId = 0; |
|
53 | + public $queryId = 0; |
|
54 | + |
|
55 | + public $characterSet = 'utf8mb4'; |
|
56 | + public $collation = 'utf8mb4_unicode_ci'; |
|
57 | + |
|
58 | + /** |
|
59 | + * Logged queries. |
|
60 | + * @var array |
|
61 | + */ |
|
62 | + protected $log = []; |
|
63 | + |
|
64 | + /** |
|
65 | + * Constructs the db handler, can optionally specify connection parameters |
|
66 | + * |
|
67 | + * @param string $database Optional The database name |
|
68 | + * @param string $user Optional The username to connect with |
|
69 | + * @param string $password Optional The password to use |
|
70 | + * @param string $host Optional The hostname where the server is, or default to localhost |
|
71 | + * @param string $query Optional query to perform immediately |
|
72 | + * @param string $port optional port for the connection |
|
73 | + */ |
|
74 | + public function __construct($database = '', $user = '', $password = '', $host = 'localhost', $query = '', $port = '') |
|
75 | + { |
|
76 | + $this->database = $database; |
|
77 | + $this->user = $user; |
|
78 | + $this->password = $password; |
|
79 | + $this->host = $host; |
|
80 | + $this->port = $port; |
|
81 | + if ($query != '') { |
|
82 | + $this->query($query); |
|
83 | + } |
|
84 | + $this->connection_atttempt = 0; |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * @param string $message |
|
89 | + * @param string $line |
|
90 | + * @param string $file |
|
91 | + * @return void |
|
92 | + */ |
|
93 | + public function log($message, $line = '', $file = '', $level = 'info') |
|
94 | + { |
|
95 | + error_log($message); |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * @return int|object |
|
100 | + */ |
|
101 | + public function linkId() |
|
102 | + { |
|
103 | + return $this->linkId; |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * @return int|object |
|
108 | + */ |
|
109 | + public function queryId() |
|
110 | + { |
|
111 | + return $this->queryId; |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * @param $string |
|
116 | + * @return string |
|
117 | + */ |
|
118 | + public function real_escape($string = '') |
|
119 | + { |
|
120 | + if ((!is_resource($this->linkId) || $this->linkId == 0) && !$this->connect()) { |
|
121 | + return $this->escape($string); |
|
122 | + } |
|
123 | + return mysqli_real_escape_string($this->linkId, $string); |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * @param $string |
|
128 | + * @return string |
|
129 | + */ |
|
130 | + public function escape($string = '') |
|
131 | + { |
|
132 | + //if (function_exists('mysql_escape_string')) |
|
133 | + //return mysql_escape_string($string); |
|
134 | + return str_replace(['\\', "\0", "\n", "\r", "'", '"', "\x1a"], ['\\\\', '\\0', '\\n', '\\r', "\\'", '\\"', '\\Z'], $string); |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * @param mixed $str |
|
139 | + * @return string |
|
140 | + */ |
|
141 | + public function dbAddslashes($str = '') |
|
142 | + { |
|
143 | + if (!isset($str) || $str == '') { |
|
144 | + return ''; |
|
145 | + } |
|
146 | + return addslashes($str); |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * Db::toTimestamp() |
|
151 | + * @param mixed $epoch |
|
152 | + * @return bool|string |
|
153 | + */ |
|
154 | + public function toTimestamp($epoch) |
|
155 | + { |
|
156 | + return date('Y-m-d H:i:s', is_float($epoch) ? intval($epoch) : $epoch); |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * Db::fromTimestamp() |
|
161 | + * @param mixed $timestamp |
|
162 | + * @return bool|int|mixed |
|
163 | + */ |
|
164 | + public function fromTimestamp($timestamp) |
|
165 | + { |
|
166 | + if (preg_match('/([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})/', $timestamp, $parts)) { |
|
167 | + $time = mktime($parts[4], $parts[5], $parts[6], $parts[2], $parts[3], $parts[1]); |
|
168 | + } elseif (preg_match('/([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})/', $timestamp, $parts)) { |
|
169 | + $time = mktime($parts[4], $parts[5], $parts[6], $parts[2], $parts[3], $parts[1]); |
|
170 | + } elseif (preg_match('/([0-9]{4})([0-9]{2})([0-9]{2})/', $timestamp, $parts)) { |
|
171 | + $time = mktime(1, 1, 1, $parts[2], $parts[3], $parts[1]); |
|
172 | + } elseif (is_numeric($timestamp) && $timestamp >= 943938000) { |
|
173 | + $time = $timestamp; |
|
174 | + } else { |
|
175 | + $this->log('Cannot Match Timestamp from '.$timestamp, __LINE__, __FILE__); |
|
176 | + $time = false; |
|
177 | + } |
|
178 | + return $time; |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * perform a query with limited result set |
|
183 | + * |
|
184 | + * @param string $queryString |
|
185 | + * @param string|int $numRows |
|
186 | + * @param int $offset |
|
187 | + * @param string|int $line |
|
188 | + * @param string $file |
|
189 | + * @return mixed |
|
190 | + */ |
|
191 | + public function limitQuery($queryString, $numRows = '', $offset = 0, $line = '', $file = '') |
|
192 | + { |
|
193 | + if (!$numRows) { |
|
194 | + $numRows = $this->maxMatches; |
|
195 | + } |
|
196 | + if ($offset == 0) { |
|
197 | + $queryString .= ' LIMIT '.(int) $numRows; |
|
198 | + } else { |
|
199 | + $queryString .= ' LIMIT '.(int) $offset.','.(int) $numRows; |
|
200 | + } |
|
201 | + |
|
202 | + if ($this->Debug) { |
|
203 | + printf("Debug: limitQuery = %s<br>offset=%d, num_rows=%d<br>\n", $queryString, $offset, $numRows); |
|
204 | + } |
|
205 | + |
|
206 | + return $this->query($queryString, $line, $file); |
|
207 | + } |
|
208 | + |
|
209 | + /** |
|
210 | + * db:qr() |
|
211 | + * |
|
212 | + * alias of queryReturn() |
|
213 | + * |
|
214 | + * @param mixed $query SQL Query to be used |
|
215 | + * @param string $line optionally pass __LINE__ calling the query for logging |
|
216 | + * @param string $file optionally pass __FILE__ calling the query for logging |
|
217 | + * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
|
218 | + */ |
|
219 | + public function qr($query, $line = '', $file = '') |
|
220 | + { |
|
221 | + return $this->queryReturn($query, $line, $file); |
|
222 | + } |
|
223 | + |
|
224 | + /** |
|
225 | + * gets a field |
|
226 | + * |
|
227 | + * @param mixed $name |
|
228 | + * @param string $stripSlashes |
|
229 | + * @return string |
|
230 | + */ |
|
231 | + public function f($name, $stripSlashes = '') |
|
232 | + { |
|
233 | + if (is_null($this->Record)) { |
|
234 | + return null; |
|
235 | + } elseif ($stripSlashes || ($this->autoStripslashes && !$stripSlashes)) { |
|
236 | + return stripslashes($this->Record[$name]); |
|
237 | + } else { |
|
238 | + return $this->Record[$name]; |
|
239 | + } |
|
240 | + } |
|
241 | + |
|
242 | + /** |
|
243 | + * error handling |
|
244 | + * |
|
245 | + * @param mixed $msg |
|
246 | + * @param string $line |
|
247 | + * @param string $file |
|
248 | + * @return void |
|
249 | + */ |
|
250 | + public function halt($msg, $line = '', $file = '') |
|
251 | + { |
|
252 | + $this->unlock(false); |
|
253 | + /* Just in case there is a table currently locked */ |
|
254 | + |
|
255 | + //$this->Error = @$this->linkId->error; |
|
256 | + //$this->Errno = @$this->linkId->errno; |
|
257 | + if ($this->haltOnError == 'no') { |
|
258 | + return true; |
|
259 | + } |
|
260 | + if ($msg != '') { |
|
261 | + $this->haltmsg($msg, $line, $file); |
|
262 | + } |
|
263 | + if ($this->haltOnError != 'report') { |
|
264 | + echo '<p><b>Session halted.</b>'; |
|
265 | + //if (isset($GLOBALS['tf'])) |
|
266 | + //$GLOBALS['tf']->terminate(); |
|
267 | + die(); |
|
268 | + } |
|
269 | + return true; |
|
270 | + } |
|
271 | + |
|
272 | + /** |
|
273 | + * @param mixed $msg |
|
274 | + * @param string $line |
|
275 | + * @param string $file |
|
276 | + * @return mixed|void |
|
277 | + */ |
|
278 | + public function logBackTrace($msg, $line = '', $file = '') |
|
279 | + { |
|
280 | + $backtrace = (function_exists('debug_backtrace') ? debug_backtrace() : []); |
|
281 | + $this->log( |
|
282 | + ('' !== getenv('REQUEST_URI') ? ' '.getenv('REQUEST_URI') : ''). |
|
283 | + ((isset($_POST) && count($_POST)) ? ' POST='.json_encode($_POST) : ''). |
|
284 | + ((isset($_GET) && count($_GET)) ? ' GET='.json_encode($_GET) : ''). |
|
285 | + ((isset($_FILES) && count($_FILES)) ? ' FILES='.json_encode($_FILES) : ''). |
|
286 | + ('' !== getenv('HTTP_USER_AGENT') ? ' AGENT="'.getenv('HTTP_USER_AGENT').'"' : ''). |
|
287 | + (isset($_SERVER['REQUEST_METHOD']) ? ' METHOD="'.$_SERVER['REQUEST_METHOD'].'"'. |
|
288 | + ($_SERVER['REQUEST_METHOD'] === 'POST' ? ' POST="'.json_encode($_POST).'"' : '') : ''), |
|
289 | + $line, |
|
290 | + $file, |
|
291 | + 'error' |
|
292 | + ); |
|
293 | + for ($level = 1, $levelMax = count($backtrace); $level < $levelMax; $level++) { |
|
294 | + $message = (isset($backtrace[$level]['file']) ? 'File: '.$backtrace[$level]['file'] : ''). |
|
295 | + (isset($backtrace[$level]['line']) ? ' Line: '.$backtrace[$level]['line'] : ''). |
|
296 | + ' Function: '.(isset($backtrace[$level] ['class']) ? '(class '.$backtrace[$level] ['class'].') ' : ''). |
|
297 | + (isset($backtrace[$level] ['type']) ? $backtrace[$level] ['type'].' ' : ''). |
|
298 | + $backtrace[$level] ['function'].'('; |
|
299 | + if (isset($backtrace[$level] ['args'])) { |
|
300 | + for ($argument = 0, $argumentMax = count($backtrace[$level]['args']); $argument < $argumentMax; $argument++) { |
|
301 | + $message .= ($argument > 0 ? ', ' : ''). |
|
302 | + (is_object($backtrace[$level]['args'][$argument]) ? 'class '.get_class($backtrace[$level]['args'][$argument]) : json_encode($backtrace[$level]['args'][$argument])); |
|
303 | + } |
|
304 | + } |
|
305 | + $message .= ')'; |
|
306 | + $this->log($message, $line, $file, 'error'); |
|
307 | + } |
|
308 | + } |
|
309 | + |
|
310 | + public function emailError($queryString, $error, $line, $file) |
|
311 | + { |
|
312 | + $subject = php_uname('n').' MySQLi Error '.$queryString; |
|
313 | + if (class_exists('\\TFSmarty')) { |
|
314 | + $smarty = new \TFSmarty(); |
|
315 | + $smarty->assign([ |
|
316 | + 'type' => $this->type, |
|
317 | + 'queryString' => $queryString, |
|
318 | + 'error' => $error, |
|
319 | + 'line' => $line, |
|
320 | + 'file' => $file, |
|
321 | + 'request' => $_REQUEST, |
|
322 | + 'server' => $_SERVER, |
|
323 | + ]); |
|
324 | + if (isset($GLOBALS['tf'])) { |
|
325 | + $smarty->assign('account_id', $GLOBALS['tf']->session->account_id); |
|
326 | + } |
|
327 | + $email = $smarty->fetch('email/admin/sql_error.tpl'); |
|
328 | + (new \MyAdmin\Mail())->adminMail($subject, $email, '[email protected]', ''); |
|
329 | + (new \MyAdmin\Mail())->adminMail($subject, $email, '[email protected]', ''); |
|
330 | + } |
|
331 | + $this->haltmsg('Invalid SQL: '.$queryString, $line, $file); |
|
332 | + } |
|
333 | + |
|
334 | + /** |
|
335 | + * @param mixed $msg |
|
336 | + * @param string $line |
|
337 | + * @param string $file |
|
338 | + * @return mixed|void |
|
339 | + */ |
|
340 | + public function haltmsg($msg, $line = '', $file = '') |
|
341 | + { |
|
342 | + $email = "DB Error {$msg} {$file}:{$line}"; |
|
343 | + if (class_exists('\\MyAdmin\Mail')) { |
|
344 | + \MyAdmin\Mail::failsafeMail($email, $email, ['[email protected]', '[email protected]']); |
|
345 | + return; |
|
346 | + } |
|
347 | + $this->log("Database error: $msg", $line, $file, 'error'); |
|
348 | + if ($this->Errno != '0' || !in_array($this->Error, ['', '()'])) { |
|
349 | + $sqlstate = mysqli_sqlstate($this->linkId); |
|
350 | + $this->log("MySQLi SQLState: {$sqlstate}. Error: ".$this->Errno.' ('.$this->Error.')', $line, $file, 'error'); |
|
351 | + } |
|
352 | + $this->logBackTrace($msg, $line, $file); |
|
353 | + } |
|
354 | + |
|
355 | + /** |
|
356 | + * @return array |
|
357 | + */ |
|
358 | + public function indexNames() |
|
359 | + { |
|
360 | + return []; |
|
361 | + } |
|
362 | + |
|
363 | + |
|
364 | + /** |
|
365 | + * Add query to logged queries. |
|
366 | + * @param string $statement |
|
367 | + * @param float $time Elapsed seconds with microseconds |
|
368 | + * @param string|int $line Line Number |
|
369 | + * @param string $file File Name |
|
370 | + */ |
|
371 | + public function addLog($statement, $time, $line = '', $file = '') |
|
372 | + { |
|
373 | + $query = [ |
|
374 | + 'statement' => $statement, |
|
375 | + 'time' => $time * 1000 |
|
376 | + ]; |
|
377 | + if ($line != '') { |
|
378 | + $query['line'] = $line; |
|
379 | + } |
|
380 | + if ($file != '') { |
|
381 | + $query['file'] = $file; |
|
382 | + } |
|
383 | + if (!isset($GLOBALS['db_queries'])) { |
|
384 | + $GLOBALS['db_queries'] = []; |
|
385 | + } |
|
386 | + $GLOBALS['db_queries'][] = $query; |
|
387 | + array_push($this->log, $query); |
|
388 | + } |
|
389 | + |
|
390 | + /** |
|
391 | + * Return logged queries. |
|
392 | + * @return array Logged queries |
|
393 | + */ |
|
394 | + public function getLog() |
|
395 | + { |
|
396 | + return $this->log; |
|
397 | + } |
|
398 | 398 | } |
@@ -19,503 +19,503 @@ |
||
19 | 19 | */ |
20 | 20 | class Db extends Generic implements Db_Interface |
21 | 21 | { |
22 | - /** |
|
23 | - * @var string |
|
24 | - */ |
|
25 | - public $type = 'mysqli'; |
|
26 | - |
|
27 | - /** |
|
28 | - * alias function of select_db, changes the database we are working with. |
|
29 | - * |
|
30 | - * @param string $database the name of the database to use |
|
31 | - * @return void |
|
32 | - */ |
|
33 | - public function useDb($database) |
|
34 | - { |
|
35 | - $this->selectDb($database); |
|
36 | - } |
|
37 | - |
|
38 | - /** |
|
39 | - * changes the database we are working with. |
|
40 | - * |
|
41 | - * @param string $database the name of the database to use |
|
42 | - * @return void |
|
43 | - */ |
|
44 | - public function selectDb($database) |
|
45 | - { |
|
46 | - $this->connect(); |
|
47 | - mysqli_select_db($this->linkId, $database); |
|
48 | - } |
|
49 | - |
|
50 | - /* public: connection management */ |
|
51 | - |
|
52 | - /** |
|
53 | - * Db::connect() |
|
54 | - * @param string $database |
|
55 | - * @param string $host |
|
56 | - * @param string $user |
|
57 | - * @param string $password |
|
58 | - * @return int|\mysqli |
|
59 | - */ |
|
60 | - public function connect($database = '', $host = '', $user = '', $password = '', $port = '') |
|
61 | - { |
|
62 | - /* Handle defaults */ |
|
63 | - if ($database == '') { |
|
64 | - $database = $this->database; |
|
65 | - } |
|
66 | - if ($host == '') { |
|
67 | - $host = $this->host; |
|
68 | - } |
|
69 | - if ($user == '') { |
|
70 | - $user = $this->user; |
|
71 | - } |
|
72 | - if ($password == '') { |
|
73 | - $password = $this->password; |
|
74 | - } |
|
75 | - if ($port == '') { |
|
76 | - $port = $this->port; |
|
77 | - } |
|
78 | - /* establish connection, select database */ |
|
79 | - if (!is_object($this->linkId)) { |
|
80 | - $this->connectionAttempt++; |
|
81 | - if ($this->connectionAttempt >= $this->maxConnectErrors - 1) { |
|
82 | - error_log("MySQLi Connection Attempt #{$this->connectionAttempt}/{$this->maxConnectErrors}"); |
|
83 | - } |
|
84 | - if ($this->connectionAttempt >= $this->maxConnectErrors) { |
|
85 | - $this->halt("connect($host, $user, \$password) failed. ".$this->linkId->connect_error); |
|
86 | - return 0; |
|
87 | - } |
|
88 | - //error_log("real_connect($host, $user, $password, $database, $port)"); |
|
89 | - $this->linkId = mysqli_init(); |
|
90 | - $this->linkId->options(MYSQLI_INIT_COMMAND, "SET NAMES {$this->characterSet} COLLATE {$this->collation}, COLLATION_CONNECTION = {$this->collation}, COLLATION_DATABASE = {$this->collation}"); |
|
91 | - if (!$this->linkId->real_connect($host, $user, $password, $database, $port != '' ? $port : NULL)) { |
|
92 | - $this->halt("connect($host, $user, \$password) failed. ".$this->linkId->connect_error); |
|
93 | - return 0; |
|
94 | - } |
|
95 | - $this->linkId->set_charset($this->characterSet); |
|
96 | - if ($this->linkId->connect_errno) { |
|
97 | - $this->halt("connect($host, $user, \$password) failed. ".$this->linkId->connect_error); |
|
98 | - return 0; |
|
99 | - } |
|
100 | - } |
|
101 | - return $this->linkId; |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * Db::disconnect() |
|
106 | - * @return bool |
|
107 | - */ |
|
108 | - public function disconnect() |
|
109 | - { |
|
110 | - $return = !is_int($this->linkId) && method_exists($this->linkId, 'close') ? $this->linkId->close() : false; |
|
111 | - $this->linkId = 0; |
|
112 | - return $return; |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * @param $string |
|
117 | - * @return string |
|
118 | - */ |
|
119 | - public function real_escape($string = '') |
|
120 | - { |
|
121 | - if ((!is_resource($this->linkId) || $this->linkId == 0) && !$this->connect()) { |
|
122 | - return $this->escape($string); |
|
123 | - } |
|
124 | - return mysqli_real_escape_string($this->linkId, $string); |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * discard the query result |
|
129 | - * @return void |
|
130 | - */ |
|
131 | - public function free() |
|
132 | - { |
|
133 | - if (is_resource($this->queryId)) { |
|
134 | - @mysqli_free_result($this->queryId); |
|
135 | - } |
|
136 | - $this->queryId = 0; |
|
137 | - } |
|
138 | - |
|
139 | - /** |
|
140 | - * Db::queryReturn() |
|
141 | - * |
|
142 | - * Sends an SQL query to the server like the normal query() command but iterates through |
|
143 | - * any rows and returns the row or rows immediately or FALSE on error |
|
144 | - * |
|
145 | - * @param mixed $query SQL Query to be used |
|
146 | - * @param string $line optionally pass __LINE__ calling the query for logging |
|
147 | - * @param string $file optionally pass __FILE__ calling the query for logging |
|
148 | - * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
|
149 | - */ |
|
150 | - public function queryReturn($query, $line = '', $file = '') |
|
151 | - { |
|
152 | - $this->query($query, $line, $file); |
|
153 | - if ($this->num_rows() == 0) { |
|
154 | - return false; |
|
155 | - } elseif ($this->num_rows() == 1) { |
|
156 | - $this->next_record(MYSQLI_ASSOC); |
|
157 | - return $this->Record; |
|
158 | - } else { |
|
159 | - $out = []; |
|
160 | - while ($this->next_record(MYSQLI_ASSOC)) { |
|
161 | - $out[] = $this->Record; |
|
162 | - } |
|
163 | - return $out; |
|
164 | - } |
|
165 | - } |
|
166 | - |
|
167 | - /** |
|
168 | - * db:qr() |
|
169 | - * |
|
170 | - * alias of queryReturn() |
|
171 | - * |
|
172 | - * @param mixed $query SQL Query to be used |
|
173 | - * @param string $line optionally pass __LINE__ calling the query for logging |
|
174 | - * @param string $file optionally pass __FILE__ calling the query for logging |
|
175 | - * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
|
176 | - */ |
|
177 | - public function qr($query, $line = '', $file = '') |
|
178 | - { |
|
179 | - return $this->queryReturn($query, $line, $file); |
|
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * creates a prepaired statement from query |
|
184 | - * |
|
185 | - * @param string $query sql query like INSERT INTO table (col) VALUES (?) or SELECT * from table WHERE col1 = ? and col2 = ? or UPDATE table SET col1 = ?, col2 = ? WHERE col3 = ? |
|
186 | - * @return int|\MyDb\Mysqli\mysqli_stmt |
|
187 | - * @param string $line |
|
188 | - * @param string $file |
|
189 | - */ |
|
190 | - public function prepare($query, $line = '', $file = '') |
|
191 | - { |
|
192 | - if (!$this->connect()) { |
|
193 | - return 0; |
|
194 | - } |
|
195 | - $haltPrev = $this->haltOnError; |
|
196 | - $this->haltOnError = 'no'; |
|
197 | - $start = microtime(true); |
|
198 | - $prepare = mysqli_prepare($this->linkId, $query); |
|
199 | - if (!isset($GLOBALS['disable_db_queries'])) { |
|
200 | - $this->addLog($query, microtime(true) - $start, $line, $file); |
|
201 | - } |
|
202 | - return $prepare; |
|
203 | - } |
|
204 | - |
|
205 | - /** |
|
206 | - * Db::query() |
|
207 | - * |
|
208 | - * Sends an SQL query to the database |
|
209 | - * |
|
210 | - * @param mixed $queryString |
|
211 | - * @param string $line |
|
212 | - * @param string $file |
|
213 | - * @return mixed 0 if no query or query id handler, safe to ignore this return |
|
214 | - */ |
|
215 | - public function query($queryString, $line = '', $file = '') |
|
216 | - { |
|
217 | - /* No empty queries, please, since PHP4 chokes on them. */ |
|
218 | - /* The empty query string is passed on from the constructor, |
|
22 | + /** |
|
23 | + * @var string |
|
24 | + */ |
|
25 | + public $type = 'mysqli'; |
|
26 | + |
|
27 | + /** |
|
28 | + * alias function of select_db, changes the database we are working with. |
|
29 | + * |
|
30 | + * @param string $database the name of the database to use |
|
31 | + * @return void |
|
32 | + */ |
|
33 | + public function useDb($database) |
|
34 | + { |
|
35 | + $this->selectDb($database); |
|
36 | + } |
|
37 | + |
|
38 | + /** |
|
39 | + * changes the database we are working with. |
|
40 | + * |
|
41 | + * @param string $database the name of the database to use |
|
42 | + * @return void |
|
43 | + */ |
|
44 | + public function selectDb($database) |
|
45 | + { |
|
46 | + $this->connect(); |
|
47 | + mysqli_select_db($this->linkId, $database); |
|
48 | + } |
|
49 | + |
|
50 | + /* public: connection management */ |
|
51 | + |
|
52 | + /** |
|
53 | + * Db::connect() |
|
54 | + * @param string $database |
|
55 | + * @param string $host |
|
56 | + * @param string $user |
|
57 | + * @param string $password |
|
58 | + * @return int|\mysqli |
|
59 | + */ |
|
60 | + public function connect($database = '', $host = '', $user = '', $password = '', $port = '') |
|
61 | + { |
|
62 | + /* Handle defaults */ |
|
63 | + if ($database == '') { |
|
64 | + $database = $this->database; |
|
65 | + } |
|
66 | + if ($host == '') { |
|
67 | + $host = $this->host; |
|
68 | + } |
|
69 | + if ($user == '') { |
|
70 | + $user = $this->user; |
|
71 | + } |
|
72 | + if ($password == '') { |
|
73 | + $password = $this->password; |
|
74 | + } |
|
75 | + if ($port == '') { |
|
76 | + $port = $this->port; |
|
77 | + } |
|
78 | + /* establish connection, select database */ |
|
79 | + if (!is_object($this->linkId)) { |
|
80 | + $this->connectionAttempt++; |
|
81 | + if ($this->connectionAttempt >= $this->maxConnectErrors - 1) { |
|
82 | + error_log("MySQLi Connection Attempt #{$this->connectionAttempt}/{$this->maxConnectErrors}"); |
|
83 | + } |
|
84 | + if ($this->connectionAttempt >= $this->maxConnectErrors) { |
|
85 | + $this->halt("connect($host, $user, \$password) failed. ".$this->linkId->connect_error); |
|
86 | + return 0; |
|
87 | + } |
|
88 | + //error_log("real_connect($host, $user, $password, $database, $port)"); |
|
89 | + $this->linkId = mysqli_init(); |
|
90 | + $this->linkId->options(MYSQLI_INIT_COMMAND, "SET NAMES {$this->characterSet} COLLATE {$this->collation}, COLLATION_CONNECTION = {$this->collation}, COLLATION_DATABASE = {$this->collation}"); |
|
91 | + if (!$this->linkId->real_connect($host, $user, $password, $database, $port != '' ? $port : NULL)) { |
|
92 | + $this->halt("connect($host, $user, \$password) failed. ".$this->linkId->connect_error); |
|
93 | + return 0; |
|
94 | + } |
|
95 | + $this->linkId->set_charset($this->characterSet); |
|
96 | + if ($this->linkId->connect_errno) { |
|
97 | + $this->halt("connect($host, $user, \$password) failed. ".$this->linkId->connect_error); |
|
98 | + return 0; |
|
99 | + } |
|
100 | + } |
|
101 | + return $this->linkId; |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * Db::disconnect() |
|
106 | + * @return bool |
|
107 | + */ |
|
108 | + public function disconnect() |
|
109 | + { |
|
110 | + $return = !is_int($this->linkId) && method_exists($this->linkId, 'close') ? $this->linkId->close() : false; |
|
111 | + $this->linkId = 0; |
|
112 | + return $return; |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * @param $string |
|
117 | + * @return string |
|
118 | + */ |
|
119 | + public function real_escape($string = '') |
|
120 | + { |
|
121 | + if ((!is_resource($this->linkId) || $this->linkId == 0) && !$this->connect()) { |
|
122 | + return $this->escape($string); |
|
123 | + } |
|
124 | + return mysqli_real_escape_string($this->linkId, $string); |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * discard the query result |
|
129 | + * @return void |
|
130 | + */ |
|
131 | + public function free() |
|
132 | + { |
|
133 | + if (is_resource($this->queryId)) { |
|
134 | + @mysqli_free_result($this->queryId); |
|
135 | + } |
|
136 | + $this->queryId = 0; |
|
137 | + } |
|
138 | + |
|
139 | + /** |
|
140 | + * Db::queryReturn() |
|
141 | + * |
|
142 | + * Sends an SQL query to the server like the normal query() command but iterates through |
|
143 | + * any rows and returns the row or rows immediately or FALSE on error |
|
144 | + * |
|
145 | + * @param mixed $query SQL Query to be used |
|
146 | + * @param string $line optionally pass __LINE__ calling the query for logging |
|
147 | + * @param string $file optionally pass __FILE__ calling the query for logging |
|
148 | + * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
|
149 | + */ |
|
150 | + public function queryReturn($query, $line = '', $file = '') |
|
151 | + { |
|
152 | + $this->query($query, $line, $file); |
|
153 | + if ($this->num_rows() == 0) { |
|
154 | + return false; |
|
155 | + } elseif ($this->num_rows() == 1) { |
|
156 | + $this->next_record(MYSQLI_ASSOC); |
|
157 | + return $this->Record; |
|
158 | + } else { |
|
159 | + $out = []; |
|
160 | + while ($this->next_record(MYSQLI_ASSOC)) { |
|
161 | + $out[] = $this->Record; |
|
162 | + } |
|
163 | + return $out; |
|
164 | + } |
|
165 | + } |
|
166 | + |
|
167 | + /** |
|
168 | + * db:qr() |
|
169 | + * |
|
170 | + * alias of queryReturn() |
|
171 | + * |
|
172 | + * @param mixed $query SQL Query to be used |
|
173 | + * @param string $line optionally pass __LINE__ calling the query for logging |
|
174 | + * @param string $file optionally pass __FILE__ calling the query for logging |
|
175 | + * @return mixed FALSE if no rows, if a single row it returns that, if multiple it returns an array of rows, associative responses only |
|
176 | + */ |
|
177 | + public function qr($query, $line = '', $file = '') |
|
178 | + { |
|
179 | + return $this->queryReturn($query, $line, $file); |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * creates a prepaired statement from query |
|
184 | + * |
|
185 | + * @param string $query sql query like INSERT INTO table (col) VALUES (?) or SELECT * from table WHERE col1 = ? and col2 = ? or UPDATE table SET col1 = ?, col2 = ? WHERE col3 = ? |
|
186 | + * @return int|\MyDb\Mysqli\mysqli_stmt |
|
187 | + * @param string $line |
|
188 | + * @param string $file |
|
189 | + */ |
|
190 | + public function prepare($query, $line = '', $file = '') |
|
191 | + { |
|
192 | + if (!$this->connect()) { |
|
193 | + return 0; |
|
194 | + } |
|
195 | + $haltPrev = $this->haltOnError; |
|
196 | + $this->haltOnError = 'no'; |
|
197 | + $start = microtime(true); |
|
198 | + $prepare = mysqli_prepare($this->linkId, $query); |
|
199 | + if (!isset($GLOBALS['disable_db_queries'])) { |
|
200 | + $this->addLog($query, microtime(true) - $start, $line, $file); |
|
201 | + } |
|
202 | + return $prepare; |
|
203 | + } |
|
204 | + |
|
205 | + /** |
|
206 | + * Db::query() |
|
207 | + * |
|
208 | + * Sends an SQL query to the database |
|
209 | + * |
|
210 | + * @param mixed $queryString |
|
211 | + * @param string $line |
|
212 | + * @param string $file |
|
213 | + * @return mixed 0 if no query or query id handler, safe to ignore this return |
|
214 | + */ |
|
215 | + public function query($queryString, $line = '', $file = '') |
|
216 | + { |
|
217 | + /* No empty queries, please, since PHP4 chokes on them. */ |
|
218 | + /* The empty query string is passed on from the constructor, |
|
219 | 219 | * when calling the class without a query, e.g. in situations |
220 | 220 | * like these: '$db = new db_Subclass;' |
221 | 221 | */ |
222 | - if ($queryString == '') { |
|
223 | - return 0; |
|
224 | - } |
|
225 | - if (!$this->connect()) { |
|
226 | - return 0; |
|
227 | - /* we already complained in connect() about that. */ |
|
228 | - } |
|
229 | - $haltPrev = $this->haltOnError; |
|
230 | - $this->haltOnError = 'no'; |
|
231 | - // New query, discard previous result. |
|
232 | - if (is_resource($this->queryId)) { |
|
233 | - $this->free(); |
|
234 | - } |
|
235 | - if ($this->Debug) { |
|
236 | - printf("Debug: query = %s<br>\n", $queryString); |
|
237 | - } |
|
238 | - if (isset($GLOBALS['log_queries']) && $GLOBALS['log_queries'] !== false) { |
|
239 | - $this->log($queryString, $line, $file); |
|
240 | - } |
|
241 | - $tries = 2; |
|
242 | - $try = 0; |
|
243 | - $this->queryId = false; |
|
244 | - while ((null === $this->queryId || $this->queryId === false) && $try <= $tries) { |
|
245 | - $try++; |
|
246 | - if ($try > 1) { |
|
247 | - @mysqli_close($this->linkId); |
|
248 | - $this->linkId = 0; |
|
249 | - } |
|
250 | - $start = microtime(true); |
|
251 | - $onlyRollback = true; |
|
252 | - $fails = -1; |
|
253 | - while ($fails < 30 && (null === $this->queryId || $this->queryId === false)) { |
|
254 | - $this->connect(); |
|
255 | - $fails++; |
|
256 | - try { |
|
257 | - $this->queryId = @mysqli_query($this->linkId, $queryString, MYSQLI_STORE_RESULT); |
|
258 | - if (in_array((int)@mysqli_errno($this->linkId), [1213, 2006, 3101, 1180])) { |
|
259 | - //error_log("got ".@mysqli_errno($this->linkId)." sql error fails {$fails} on query {$queryString} from {$line}:{$file}"); |
|
260 | - usleep(250000); // 0.25 second |
|
261 | - } else { |
|
262 | - $onlyRollback = false; |
|
263 | - if (in_array((int)@mysqli_errno($this->linkId), [1064])) { |
|
264 | - $tries = 0; |
|
265 | - } |
|
266 | - break; |
|
267 | - } |
|
268 | - } catch (\mysqli_sql_exception $e) { |
|
269 | - if (in_array((int)$e->getCode(), [1213, 2006, 3101, 1180])) { |
|
270 | - //error_log("got ".$e->getCode()." sql error fails {$fails}"); |
|
271 | - usleep(250000); // 0.25 second |
|
272 | - } else { |
|
273 | - error_log('Got mysqli_sql_exception code '.$e->getCode().' error '.$e->getMessage().' on query '.$queryString.' from '.$line.':'.$file); |
|
274 | - $onlyRollback = false; |
|
275 | - if (in_array((int)@mysqli_errno($this->linkId), [1064])) { |
|
276 | - $tries = 0; |
|
277 | - } |
|
278 | - break; |
|
279 | - } |
|
280 | - } |
|
281 | - } |
|
282 | - if (!isset($GLOBALS['disable_db_queries'])) { |
|
283 | - $this->addLog($queryString, microtime(true) - $start, $line, $file); |
|
284 | - } |
|
285 | - $this->Row = 0; |
|
286 | - $this->Errno = @mysqli_errno($this->linkId); |
|
287 | - $this->Error = @mysqli_error($this->linkId); |
|
288 | - if ($try == 1 && (null === $this->queryId || $this->queryId === false)) { |
|
289 | - //$this->emailError($queryString, 'Error #'.$this->Errno.': '.$this->Error, $line, $file); |
|
290 | - } |
|
291 | - } |
|
292 | - $this->haltOnError = $haltPrev; |
|
293 | - if ($onlyRollback === true && false === $this->queryId) { |
|
294 | - error_log('Got MySQLi 3101 Rollback Error '.$fails.' Times, Giving Up on '.$queryString.' from '.$line.':'.$file.' on '.__LINE__.':'.__FILE__); |
|
295 | - } |
|
296 | - if (null === $this->queryId || $this->queryId === false) { |
|
297 | - $this->emailError($queryString, 'Error #'.$this->Errno.': '.$this->Error, $line, $file); |
|
298 | - $this->halt('', $line, $file); |
|
299 | - } |
|
300 | - |
|
301 | - // Will return nada if it fails. That's fine. |
|
302 | - return $this->queryId; |
|
303 | - } |
|
304 | - |
|
305 | - /** |
|
306 | - * @return array|null|object |
|
307 | - */ |
|
308 | - public function fetchObject() |
|
309 | - { |
|
310 | - $this->Record = @mysqli_fetch_object($this->queryId); |
|
311 | - return $this->Record; |
|
312 | - } |
|
313 | - |
|
314 | - /* public: walk result set */ |
|
315 | - |
|
316 | - /** |
|
317 | - * Db::next_record() |
|
318 | - * |
|
319 | - * @param mixed $resultType |
|
320 | - * @return bool |
|
321 | - */ |
|
322 | - public function next_record($resultType = MYSQLI_BOTH) |
|
323 | - { |
|
324 | - if ($this->queryId === false) { |
|
325 | - $this->haltmsg('next_record called with no query pending.'); |
|
326 | - return 0; |
|
327 | - } |
|
328 | - |
|
329 | - $this->Record = @mysqli_fetch_array($this->queryId, $resultType); |
|
330 | - ++$this->Row; |
|
331 | - $this->Errno = mysqli_errno($this->linkId); |
|
332 | - $this->Error = mysqli_error($this->linkId); |
|
333 | - |
|
334 | - $stat = is_array($this->Record); |
|
335 | - if (!$stat && $this->autoFree && is_resource($this->queryId)) { |
|
336 | - $this->free(); |
|
337 | - } |
|
338 | - return $stat; |
|
339 | - } |
|
340 | - |
|
341 | - /** |
|
342 | - * switch to position in result set |
|
343 | - * |
|
344 | - * @param integer $pos the row numbe starting at 0 to switch to |
|
345 | - * @return bool whetherit was successfu or not. |
|
346 | - */ |
|
347 | - public function seek($pos = 0) |
|
348 | - { |
|
349 | - $status = @mysqli_data_seek($this->queryId, $pos); |
|
350 | - if ($status) { |
|
351 | - $this->Row = $pos; |
|
352 | - } else { |
|
353 | - $this->haltmsg("seek({$pos}) failed: result has ".$this->num_rows().' rows', __LINE__, __FILE__); |
|
354 | - /* half assed attempt to save the day, but do not consider this documented or even desirable behaviour. */ |
|
355 | - $rows = $this->num_rows(); |
|
356 | - @mysqli_data_seek($this->queryId, $rows); |
|
357 | - $this->Row = $rows; |
|
358 | - return false; |
|
359 | - } |
|
360 | - return true; |
|
361 | - } |
|
362 | - |
|
363 | - /** |
|
364 | - * Initiates a transaction |
|
365 | - * |
|
366 | - * @return bool |
|
367 | - */ |
|
368 | - public function transactionBegin() |
|
369 | - { |
|
370 | - if (version_compare(PHP_VERSION, '5.5.0') < 0) { |
|
371 | - return true; |
|
372 | - } |
|
373 | - if (!$this->connect()) { |
|
374 | - return 0; |
|
375 | - } |
|
376 | - return mysqli_begin_transaction($this->linkId); |
|
377 | - } |
|
378 | - |
|
379 | - /** |
|
380 | - * Commits a transaction |
|
381 | - * |
|
382 | - * @return bool |
|
383 | - */ |
|
384 | - public function transactionCommit() |
|
385 | - { |
|
386 | - if (version_compare(PHP_VERSION, '5.5.0') < 0 || $this->linkId === 0) { |
|
387 | - return true; |
|
388 | - } |
|
389 | - return mysqli_commit($this->linkId); |
|
390 | - } |
|
391 | - |
|
392 | - /** |
|
393 | - * Rolls back a transaction |
|
394 | - * |
|
395 | - * @return bool |
|
396 | - */ |
|
397 | - public function transactionAbort() |
|
398 | - { |
|
399 | - if (version_compare(PHP_VERSION, '5.5.0') < 0 || $this->linkId === 0) { |
|
400 | - return true; |
|
401 | - } |
|
402 | - return mysqli_rollback($this->linkId); |
|
403 | - } |
|
404 | - |
|
405 | - /** |
|
406 | - * This will get the last insert ID created on the current connection. Should only be called after an insert query is |
|
407 | - * run on a table that has an auto incrementing field. $table and $field are required, but unused here since it's |
|
408 | - * unnecessary for mysql. For compatibility with pgsql, the params must be supplied. |
|
409 | - * |
|
410 | - * @param string $table |
|
411 | - * @param string $field |
|
412 | - * @return int|string |
|
413 | - */ |
|
414 | - public function getLastInsertId($table, $field) |
|
415 | - { |
|
416 | - if (!isset($table) || $table == '' || !isset($field) || $field == '') { |
|
417 | - return -1; |
|
418 | - } |
|
419 | - |
|
420 | - return @mysqli_insert_id($this->linkId); |
|
421 | - } |
|
422 | - |
|
423 | - /* public: table locking */ |
|
424 | - |
|
425 | - /** |
|
426 | - * Db::lock() |
|
427 | - * @param mixed $table |
|
428 | - * @param string $mode |
|
429 | - * @return bool|int|\mysqli_result |
|
430 | - */ |
|
431 | - public function lock($table, $mode = 'write') |
|
432 | - { |
|
433 | - $this->connect(); |
|
434 | - $query = 'lock tables '; |
|
435 | - if (is_array($table)) { |
|
436 | - foreach ($table as $key => $value) { |
|
437 | - if ($key == 'read' && $key != 0) { |
|
438 | - $query .= "$value read, "; |
|
439 | - } else { |
|
440 | - $query .= "$value $mode, "; |
|
441 | - } |
|
442 | - } |
|
443 | - $query = mb_substr($query, 0, -2); |
|
444 | - } else { |
|
445 | - $query .= "$table $mode"; |
|
446 | - } |
|
447 | - $res = @mysqli_query($this->linkId, $query); |
|
448 | - if (!$res) { |
|
449 | - $this->halt("lock($table, $mode) failed."); |
|
450 | - return 0; |
|
451 | - } |
|
452 | - return $res; |
|
453 | - } |
|
454 | - |
|
455 | - /** |
|
456 | - * Db::unlock() |
|
457 | - * @param bool $haltOnError optional, defaults to TRUE, whether or not to halt on error |
|
458 | - * @return bool|int|\mysqli_result |
|
459 | - */ |
|
460 | - public function unlock($haltOnError = true) |
|
461 | - { |
|
462 | - $this->connect(); |
|
463 | - |
|
464 | - $res = @mysqli_query($this->linkId, 'unlock tables'); |
|
465 | - if ($haltOnError === true && !$res) { |
|
466 | - $this->halt('unlock() failed.'); |
|
467 | - return 0; |
|
468 | - } |
|
469 | - return $res; |
|
470 | - } |
|
471 | - |
|
472 | - /* public: evaluate the result (size, width) */ |
|
473 | - |
|
474 | - /** |
|
475 | - * Db::affectedRows() |
|
476 | - * @return int |
|
477 | - */ |
|
478 | - public function affectedRows() |
|
479 | - { |
|
480 | - return @mysqli_affected_rows($this->linkId); |
|
481 | - } |
|
482 | - |
|
483 | - /** |
|
484 | - * Db::num_rows() |
|
485 | - * @return int |
|
486 | - */ |
|
487 | - public function num_rows() |
|
488 | - { |
|
489 | - return @mysqli_num_rows($this->queryId); |
|
490 | - } |
|
491 | - |
|
492 | - /** |
|
493 | - * Db::num_fields() |
|
494 | - * @return int |
|
495 | - */ |
|
496 | - public function num_fields() |
|
497 | - { |
|
498 | - return @mysqli_num_fields($this->queryId); |
|
499 | - } |
|
500 | - |
|
501 | - /** |
|
502 | - * gets an array of the table names in teh current datase |
|
503 | - * |
|
504 | - * @return array |
|
505 | - */ |
|
506 | - public function tableNames() |
|
507 | - { |
|
508 | - $return = []; |
|
509 | - $this->query('SHOW TABLES'); |
|
510 | - $i = 0; |
|
511 | - while ($info = $this->queryId->fetch_row()) { |
|
512 | - $return[$i]['table_name'] = $info[0]; |
|
513 | - $return[$i]['tablespace_name'] = $this->database; |
|
514 | - $return[$i]['database'] = $this->database; |
|
515 | - ++$i; |
|
516 | - } |
|
517 | - return $return; |
|
518 | - } |
|
222 | + if ($queryString == '') { |
|
223 | + return 0; |
|
224 | + } |
|
225 | + if (!$this->connect()) { |
|
226 | + return 0; |
|
227 | + /* we already complained in connect() about that. */ |
|
228 | + } |
|
229 | + $haltPrev = $this->haltOnError; |
|
230 | + $this->haltOnError = 'no'; |
|
231 | + // New query, discard previous result. |
|
232 | + if (is_resource($this->queryId)) { |
|
233 | + $this->free(); |
|
234 | + } |
|
235 | + if ($this->Debug) { |
|
236 | + printf("Debug: query = %s<br>\n", $queryString); |
|
237 | + } |
|
238 | + if (isset($GLOBALS['log_queries']) && $GLOBALS['log_queries'] !== false) { |
|
239 | + $this->log($queryString, $line, $file); |
|
240 | + } |
|
241 | + $tries = 2; |
|
242 | + $try = 0; |
|
243 | + $this->queryId = false; |
|
244 | + while ((null === $this->queryId || $this->queryId === false) && $try <= $tries) { |
|
245 | + $try++; |
|
246 | + if ($try > 1) { |
|
247 | + @mysqli_close($this->linkId); |
|
248 | + $this->linkId = 0; |
|
249 | + } |
|
250 | + $start = microtime(true); |
|
251 | + $onlyRollback = true; |
|
252 | + $fails = -1; |
|
253 | + while ($fails < 30 && (null === $this->queryId || $this->queryId === false)) { |
|
254 | + $this->connect(); |
|
255 | + $fails++; |
|
256 | + try { |
|
257 | + $this->queryId = @mysqli_query($this->linkId, $queryString, MYSQLI_STORE_RESULT); |
|
258 | + if (in_array((int)@mysqli_errno($this->linkId), [1213, 2006, 3101, 1180])) { |
|
259 | + //error_log("got ".@mysqli_errno($this->linkId)." sql error fails {$fails} on query {$queryString} from {$line}:{$file}"); |
|
260 | + usleep(250000); // 0.25 second |
|
261 | + } else { |
|
262 | + $onlyRollback = false; |
|
263 | + if (in_array((int)@mysqli_errno($this->linkId), [1064])) { |
|
264 | + $tries = 0; |
|
265 | + } |
|
266 | + break; |
|
267 | + } |
|
268 | + } catch (\mysqli_sql_exception $e) { |
|
269 | + if (in_array((int)$e->getCode(), [1213, 2006, 3101, 1180])) { |
|
270 | + //error_log("got ".$e->getCode()." sql error fails {$fails}"); |
|
271 | + usleep(250000); // 0.25 second |
|
272 | + } else { |
|
273 | + error_log('Got mysqli_sql_exception code '.$e->getCode().' error '.$e->getMessage().' on query '.$queryString.' from '.$line.':'.$file); |
|
274 | + $onlyRollback = false; |
|
275 | + if (in_array((int)@mysqli_errno($this->linkId), [1064])) { |
|
276 | + $tries = 0; |
|
277 | + } |
|
278 | + break; |
|
279 | + } |
|
280 | + } |
|
281 | + } |
|
282 | + if (!isset($GLOBALS['disable_db_queries'])) { |
|
283 | + $this->addLog($queryString, microtime(true) - $start, $line, $file); |
|
284 | + } |
|
285 | + $this->Row = 0; |
|
286 | + $this->Errno = @mysqli_errno($this->linkId); |
|
287 | + $this->Error = @mysqli_error($this->linkId); |
|
288 | + if ($try == 1 && (null === $this->queryId || $this->queryId === false)) { |
|
289 | + //$this->emailError($queryString, 'Error #'.$this->Errno.': '.$this->Error, $line, $file); |
|
290 | + } |
|
291 | + } |
|
292 | + $this->haltOnError = $haltPrev; |
|
293 | + if ($onlyRollback === true && false === $this->queryId) { |
|
294 | + error_log('Got MySQLi 3101 Rollback Error '.$fails.' Times, Giving Up on '.$queryString.' from '.$line.':'.$file.' on '.__LINE__.':'.__FILE__); |
|
295 | + } |
|
296 | + if (null === $this->queryId || $this->queryId === false) { |
|
297 | + $this->emailError($queryString, 'Error #'.$this->Errno.': '.$this->Error, $line, $file); |
|
298 | + $this->halt('', $line, $file); |
|
299 | + } |
|
300 | + |
|
301 | + // Will return nada if it fails. That's fine. |
|
302 | + return $this->queryId; |
|
303 | + } |
|
304 | + |
|
305 | + /** |
|
306 | + * @return array|null|object |
|
307 | + */ |
|
308 | + public function fetchObject() |
|
309 | + { |
|
310 | + $this->Record = @mysqli_fetch_object($this->queryId); |
|
311 | + return $this->Record; |
|
312 | + } |
|
313 | + |
|
314 | + /* public: walk result set */ |
|
315 | + |
|
316 | + /** |
|
317 | + * Db::next_record() |
|
318 | + * |
|
319 | + * @param mixed $resultType |
|
320 | + * @return bool |
|
321 | + */ |
|
322 | + public function next_record($resultType = MYSQLI_BOTH) |
|
323 | + { |
|
324 | + if ($this->queryId === false) { |
|
325 | + $this->haltmsg('next_record called with no query pending.'); |
|
326 | + return 0; |
|
327 | + } |
|
328 | + |
|
329 | + $this->Record = @mysqli_fetch_array($this->queryId, $resultType); |
|
330 | + ++$this->Row; |
|
331 | + $this->Errno = mysqli_errno($this->linkId); |
|
332 | + $this->Error = mysqli_error($this->linkId); |
|
333 | + |
|
334 | + $stat = is_array($this->Record); |
|
335 | + if (!$stat && $this->autoFree && is_resource($this->queryId)) { |
|
336 | + $this->free(); |
|
337 | + } |
|
338 | + return $stat; |
|
339 | + } |
|
340 | + |
|
341 | + /** |
|
342 | + * switch to position in result set |
|
343 | + * |
|
344 | + * @param integer $pos the row numbe starting at 0 to switch to |
|
345 | + * @return bool whetherit was successfu or not. |
|
346 | + */ |
|
347 | + public function seek($pos = 0) |
|
348 | + { |
|
349 | + $status = @mysqli_data_seek($this->queryId, $pos); |
|
350 | + if ($status) { |
|
351 | + $this->Row = $pos; |
|
352 | + } else { |
|
353 | + $this->haltmsg("seek({$pos}) failed: result has ".$this->num_rows().' rows', __LINE__, __FILE__); |
|
354 | + /* half assed attempt to save the day, but do not consider this documented or even desirable behaviour. */ |
|
355 | + $rows = $this->num_rows(); |
|
356 | + @mysqli_data_seek($this->queryId, $rows); |
|
357 | + $this->Row = $rows; |
|
358 | + return false; |
|
359 | + } |
|
360 | + return true; |
|
361 | + } |
|
362 | + |
|
363 | + /** |
|
364 | + * Initiates a transaction |
|
365 | + * |
|
366 | + * @return bool |
|
367 | + */ |
|
368 | + public function transactionBegin() |
|
369 | + { |
|
370 | + if (version_compare(PHP_VERSION, '5.5.0') < 0) { |
|
371 | + return true; |
|
372 | + } |
|
373 | + if (!$this->connect()) { |
|
374 | + return 0; |
|
375 | + } |
|
376 | + return mysqli_begin_transaction($this->linkId); |
|
377 | + } |
|
378 | + |
|
379 | + /** |
|
380 | + * Commits a transaction |
|
381 | + * |
|
382 | + * @return bool |
|
383 | + */ |
|
384 | + public function transactionCommit() |
|
385 | + { |
|
386 | + if (version_compare(PHP_VERSION, '5.5.0') < 0 || $this->linkId === 0) { |
|
387 | + return true; |
|
388 | + } |
|
389 | + return mysqli_commit($this->linkId); |
|
390 | + } |
|
391 | + |
|
392 | + /** |
|
393 | + * Rolls back a transaction |
|
394 | + * |
|
395 | + * @return bool |
|
396 | + */ |
|
397 | + public function transactionAbort() |
|
398 | + { |
|
399 | + if (version_compare(PHP_VERSION, '5.5.0') < 0 || $this->linkId === 0) { |
|
400 | + return true; |
|
401 | + } |
|
402 | + return mysqli_rollback($this->linkId); |
|
403 | + } |
|
404 | + |
|
405 | + /** |
|
406 | + * This will get the last insert ID created on the current connection. Should only be called after an insert query is |
|
407 | + * run on a table that has an auto incrementing field. $table and $field are required, but unused here since it's |
|
408 | + * unnecessary for mysql. For compatibility with pgsql, the params must be supplied. |
|
409 | + * |
|
410 | + * @param string $table |
|
411 | + * @param string $field |
|
412 | + * @return int|string |
|
413 | + */ |
|
414 | + public function getLastInsertId($table, $field) |
|
415 | + { |
|
416 | + if (!isset($table) || $table == '' || !isset($field) || $field == '') { |
|
417 | + return -1; |
|
418 | + } |
|
419 | + |
|
420 | + return @mysqli_insert_id($this->linkId); |
|
421 | + } |
|
422 | + |
|
423 | + /* public: table locking */ |
|
424 | + |
|
425 | + /** |
|
426 | + * Db::lock() |
|
427 | + * @param mixed $table |
|
428 | + * @param string $mode |
|
429 | + * @return bool|int|\mysqli_result |
|
430 | + */ |
|
431 | + public function lock($table, $mode = 'write') |
|
432 | + { |
|
433 | + $this->connect(); |
|
434 | + $query = 'lock tables '; |
|
435 | + if (is_array($table)) { |
|
436 | + foreach ($table as $key => $value) { |
|
437 | + if ($key == 'read' && $key != 0) { |
|
438 | + $query .= "$value read, "; |
|
439 | + } else { |
|
440 | + $query .= "$value $mode, "; |
|
441 | + } |
|
442 | + } |
|
443 | + $query = mb_substr($query, 0, -2); |
|
444 | + } else { |
|
445 | + $query .= "$table $mode"; |
|
446 | + } |
|
447 | + $res = @mysqli_query($this->linkId, $query); |
|
448 | + if (!$res) { |
|
449 | + $this->halt("lock($table, $mode) failed."); |
|
450 | + return 0; |
|
451 | + } |
|
452 | + return $res; |
|
453 | + } |
|
454 | + |
|
455 | + /** |
|
456 | + * Db::unlock() |
|
457 | + * @param bool $haltOnError optional, defaults to TRUE, whether or not to halt on error |
|
458 | + * @return bool|int|\mysqli_result |
|
459 | + */ |
|
460 | + public function unlock($haltOnError = true) |
|
461 | + { |
|
462 | + $this->connect(); |
|
463 | + |
|
464 | + $res = @mysqli_query($this->linkId, 'unlock tables'); |
|
465 | + if ($haltOnError === true && !$res) { |
|
466 | + $this->halt('unlock() failed.'); |
|
467 | + return 0; |
|
468 | + } |
|
469 | + return $res; |
|
470 | + } |
|
471 | + |
|
472 | + /* public: evaluate the result (size, width) */ |
|
473 | + |
|
474 | + /** |
|
475 | + * Db::affectedRows() |
|
476 | + * @return int |
|
477 | + */ |
|
478 | + public function affectedRows() |
|
479 | + { |
|
480 | + return @mysqli_affected_rows($this->linkId); |
|
481 | + } |
|
482 | + |
|
483 | + /** |
|
484 | + * Db::num_rows() |
|
485 | + * @return int |
|
486 | + */ |
|
487 | + public function num_rows() |
|
488 | + { |
|
489 | + return @mysqli_num_rows($this->queryId); |
|
490 | + } |
|
491 | + |
|
492 | + /** |
|
493 | + * Db::num_fields() |
|
494 | + * @return int |
|
495 | + */ |
|
496 | + public function num_fields() |
|
497 | + { |
|
498 | + return @mysqli_num_fields($this->queryId); |
|
499 | + } |
|
500 | + |
|
501 | + /** |
|
502 | + * gets an array of the table names in teh current datase |
|
503 | + * |
|
504 | + * @return array |
|
505 | + */ |
|
506 | + public function tableNames() |
|
507 | + { |
|
508 | + $return = []; |
|
509 | + $this->query('SHOW TABLES'); |
|
510 | + $i = 0; |
|
511 | + while ($info = $this->queryId->fetch_row()) { |
|
512 | + $return[$i]['table_name'] = $info[0]; |
|
513 | + $return[$i]['tablespace_name'] = $this->database; |
|
514 | + $return[$i]['database'] = $this->database; |
|
515 | + ++$i; |
|
516 | + } |
|
517 | + return $return; |
|
518 | + } |
|
519 | 519 | } |
520 | 520 | |
521 | 521 | /** |