1 | <?php |
---|
2 | /* |
---|
3 | V4.94 23 Jan 2007 (c) 2000-2007 John Lim (jlim#natsoft.com.my). All rights reserved. |
---|
4 | Released under both BSD license and Lesser GPL library license. |
---|
5 | Whenever there is any discrepancy between the two licenses, |
---|
6 | the BSD license will take precedence. |
---|
7 | Set tabs to 8. |
---|
8 | |
---|
9 | MySQL code that does not support transactions. Use mysqlt if you need transactions. |
---|
10 | Requires mysql client. Works on Windows and Unix. |
---|
11 | |
---|
12 | 21 October 2003: MySQLi extension implementation by Arjen de Rijke (a.de.rijke@xs4all.nl) |
---|
13 | Based on adodb 3.40 |
---|
14 | */ |
---|
15 | |
---|
16 | // security - hide paths |
---|
17 | if (!defined('ADODB_DIR')) die(); |
---|
18 | |
---|
19 | if (! defined("_ADODB_MYSQLI_LAYER")) { |
---|
20 | define("_ADODB_MYSQLI_LAYER", 1 ); |
---|
21 | |
---|
22 | // PHP5 compat... |
---|
23 | if (! defined("MYSQLI_BINARY_FLAG")) define("MYSQLI_BINARY_FLAG", 128); |
---|
24 | if (!defined('MYSQLI_READ_DEFAULT_GROUP')) define('MYSQLI_READ_DEFAULT_GROUP',1); |
---|
25 | |
---|
26 | // disable adodb extension - currently incompatible. |
---|
27 | global $ADODB_EXTENSION; $ADODB_EXTENSION = false; |
---|
28 | |
---|
29 | class ADODB_mysqli extends ADOConnection { |
---|
30 | var $databaseType = 'mysqli'; |
---|
31 | var $dataProvider = 'native'; |
---|
32 | var $hasInsertID = true; |
---|
33 | var $hasAffectedRows = true; |
---|
34 | var $metaTablesSQL = "SHOW TABLES"; |
---|
35 | var $metaColumnsSQL = "SHOW COLUMNS FROM `%s`"; |
---|
36 | var $fmtTimeStamp = "'Y-m-d H:i:s'"; |
---|
37 | var $hasLimit = true; |
---|
38 | var $hasMoveFirst = true; |
---|
39 | var $hasGenID = true; |
---|
40 | var $isoDates = true; // accepts dates in ISO format |
---|
41 | var $sysDate = 'CURDATE()'; |
---|
42 | var $sysTimeStamp = 'NOW()'; |
---|
43 | var $hasTransactions = true; |
---|
44 | var $forceNewConnect = false; |
---|
45 | var $poorAffectedRows = true; |
---|
46 | var $clientFlags = 0; |
---|
47 | var $substr = "substring"; |
---|
48 | var $port = false; |
---|
49 | var $socket = false; |
---|
50 | var $_bindInputArray = false; |
---|
51 | var $nameQuote = '`'; /// string to use to quote identifiers and names |
---|
52 | var $optionFlags = array(array(MYSQLI_READ_DEFAULT_GROUP,0)); |
---|
53 | |
---|
54 | function ADODB_mysqli() |
---|
55 | { |
---|
56 | // if(!extension_loaded("mysqli")) |
---|
57 | ;//trigger_error("You must have the mysqli extension installed.", E_USER_ERROR); |
---|
58 | |
---|
59 | } |
---|
60 | |
---|
61 | function SetTransactionMode( $transaction_mode ) |
---|
62 | { |
---|
63 | $this->_transmode = $transaction_mode; |
---|
64 | if (empty($transaction_mode)) { |
---|
65 | $this->Execute('SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ'); |
---|
66 | return; |
---|
67 | } |
---|
68 | if (!stristr($transaction_mode,'isolation')) $transaction_mode = 'ISOLATION LEVEL '.$transaction_mode; |
---|
69 | $this->Execute("SET SESSION TRANSACTION ".$transaction_mode); |
---|
70 | } |
---|
71 | |
---|
72 | // returns true or false |
---|
73 | // To add: parameter int $port, |
---|
74 | // parameter string $socket |
---|
75 | function _connect($argHostname = NULL, |
---|
76 | $argUsername = NULL, |
---|
77 | $argPassword = NULL, |
---|
78 | $argDatabasename = NULL, $persist=false) |
---|
79 | { |
---|
80 | if(!extension_loaded("mysqli")) { |
---|
81 | return null; |
---|
82 | } |
---|
83 | $this->_connectionID = @mysqli_init(); |
---|
84 | |
---|
85 | if (is_null($this->_connectionID)) { |
---|
86 | // mysqli_init only fails if insufficient memory |
---|
87 | if ($this->debug) |
---|
88 | ADOConnection::outp("mysqli_init() failed : " . $this->ErrorMsg()); |
---|
89 | return false; |
---|
90 | } |
---|
91 | /* |
---|
92 | I suggest a simple fix which would enable adodb and mysqli driver to |
---|
93 | read connection options from the standard mysql configuration file |
---|
94 | /etc/my.cnf - "Bastien Duclaux" <bduclaux#yahoo.com> |
---|
95 | */ |
---|
96 | foreach($this->optionFlags as $arr) { |
---|
97 | mysqli_options($this->_connectionID,$arr[0],$arr[1]); |
---|
98 | } |
---|
99 | |
---|
100 | #if (!empty($this->port)) $argHostname .= ":".$this->port; |
---|
101 | $ok = mysqli_real_connect($this->_connectionID, |
---|
102 | $argHostname, |
---|
103 | $argUsername, |
---|
104 | $argPassword, |
---|
105 | $argDatabasename, |
---|
106 | $this->port, |
---|
107 | $this->socket, |
---|
108 | $this->clientFlags); |
---|
109 | |
---|
110 | if ($ok) { |
---|
111 | if ($argDatabasename) return $this->SelectDB($argDatabasename); |
---|
112 | return true; |
---|
113 | } else { |
---|
114 | if ($this->debug) |
---|
115 | ADOConnection::outp("Could't connect : " . $this->ErrorMsg()); |
---|
116 | return false; |
---|
117 | } |
---|
118 | } |
---|
119 | |
---|
120 | // returns true or false |
---|
121 | // How to force a persistent connection |
---|
122 | function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename) |
---|
123 | { |
---|
124 | return $this->_connect($argHostname, $argUsername, $argPassword, $argDatabasename, true); |
---|
125 | |
---|
126 | } |
---|
127 | |
---|
128 | // When is this used? Close old connection first? |
---|
129 | // In _connect(), check $this->forceNewConnect? |
---|
130 | function _nconnect($argHostname, $argUsername, $argPassword, $argDatabasename) |
---|
131 | { |
---|
132 | $this->forceNewConnect = true; |
---|
133 | return $this->_connect($argHostname, $argUsername, $argPassword, $argDatabasename); |
---|
134 | } |
---|
135 | |
---|
136 | function IfNull( $field, $ifNull ) |
---|
137 | { |
---|
138 | return " IFNULL($field, $ifNull) "; // if MySQL |
---|
139 | } |
---|
140 | |
---|
141 | // do not use $ADODB_COUNTRECS |
---|
142 | function GetOne($sql,$inputarr=false) |
---|
143 | { |
---|
144 | $ret = false; |
---|
145 | $rs = &$this->Execute($sql,$inputarr); |
---|
146 | if ($rs) { |
---|
147 | if (!$rs->EOF) $ret = reset($rs->fields); |
---|
148 | $rs->Close(); |
---|
149 | } |
---|
150 | return $ret; |
---|
151 | } |
---|
152 | |
---|
153 | function ServerInfo() |
---|
154 | { |
---|
155 | $arr['description'] = $this->GetOne("select version()"); |
---|
156 | $arr['version'] = ADOConnection::_findvers($arr['description']); |
---|
157 | return $arr; |
---|
158 | } |
---|
159 | |
---|
160 | |
---|
161 | function BeginTrans() |
---|
162 | { |
---|
163 | if ($this->transOff) return true; |
---|
164 | $this->transCnt += 1; |
---|
165 | |
---|
166 | //$this->Execute('SET AUTOCOMMIT=0'); |
---|
167 | mysqli_autocommit($this->_connectionID, false); |
---|
168 | $this->Execute('BEGIN'); |
---|
169 | return true; |
---|
170 | } |
---|
171 | |
---|
172 | function CommitTrans($ok=true) |
---|
173 | { |
---|
174 | if ($this->transOff) return true; |
---|
175 | if (!$ok) return $this->RollbackTrans(); |
---|
176 | |
---|
177 | if ($this->transCnt) $this->transCnt -= 1; |
---|
178 | $this->Execute('COMMIT'); |
---|
179 | |
---|
180 | //$this->Execute('SET AUTOCOMMIT=1'); |
---|
181 | mysqli_autocommit($this->_connectionID, true); |
---|
182 | return true; |
---|
183 | } |
---|
184 | |
---|
185 | function RollbackTrans() |
---|
186 | { |
---|
187 | if ($this->transOff) return true; |
---|
188 | if ($this->transCnt) $this->transCnt -= 1; |
---|
189 | $this->Execute('ROLLBACK'); |
---|
190 | //$this->Execute('SET AUTOCOMMIT=1'); |
---|
191 | mysqli_autocommit($this->_connectionID, true); |
---|
192 | return true; |
---|
193 | } |
---|
194 | |
---|
195 | function RowLock($tables,$where='',$flds='1 as adodb_ignore') |
---|
196 | { |
---|
197 | if ($this->transCnt==0) $this->BeginTrans(); |
---|
198 | if ($where) $where = ' where '.$where; |
---|
199 | $rs =& $this->Execute("select $flds from $tables $where for update"); |
---|
200 | return !empty($rs); |
---|
201 | } |
---|
202 | |
---|
203 | // if magic quotes disabled, use mysql_real_escape_string() |
---|
204 | // From readme.htm: |
---|
205 | // Quotes a string to be sent to the database. The $magic_quotes_enabled |
---|
206 | // parameter may look funny, but the idea is if you are quoting a |
---|
207 | // string extracted from a POST/GET variable, then |
---|
208 | // pass get_magic_quotes_gpc() as the second parameter. This will |
---|
209 | // ensure that the variable is not quoted twice, once by qstr and once |
---|
210 | // by the magic_quotes_gpc. |
---|
211 | // |
---|
212 | //Eg. $s = $db->qstr(_GET['name'],get_magic_quotes_gpc()); |
---|
213 | function qstr($s, $magic_quotes = false) |
---|
214 | { |
---|
215 | if (!$magic_quotes) { |
---|
216 | if (PHP_VERSION >= 5) |
---|
217 | return "'" . mysqli_real_escape_string($this->_connectionID, $s) . "'"; |
---|
218 | |
---|
219 | if ($this->replaceQuote[0] == '\\') |
---|
220 | $s = adodb_str_replace(array('\\',"\0"),array('\\\\',"\\\0"),$s); |
---|
221 | return "'".str_replace("'",$this->replaceQuote,$s)."'"; |
---|
222 | } |
---|
223 | // undo magic quotes for " |
---|
224 | $s = str_replace('\\"','"',$s); |
---|
225 | return "'$s'"; |
---|
226 | } |
---|
227 | |
---|
228 | function _insertid() |
---|
229 | { |
---|
230 | $result = @mysqli_insert_id($this->_connectionID); |
---|
231 | if ($result == -1){ |
---|
232 | if ($this->debug) ADOConnection::outp("mysqli_insert_id() failed : " . $this->ErrorMsg()); |
---|
233 | } |
---|
234 | return $result; |
---|
235 | } |
---|
236 | |
---|
237 | // Only works for INSERT, UPDATE and DELETE query's |
---|
238 | function _affectedrows() |
---|
239 | { |
---|
240 | $result = @mysqli_affected_rows($this->_connectionID); |
---|
241 | if ($result == -1) { |
---|
242 | if ($this->debug) ADOConnection::outp("mysqli_affected_rows() failed : " . $this->ErrorMsg()); |
---|
243 | } |
---|
244 | return $result; |
---|
245 | } |
---|
246 | |
---|
247 | // See http://www.mysql.com/doc/M/i/Miscellaneous_functions.html |
---|
248 | // Reference on Last_Insert_ID on the recommended way to simulate sequences |
---|
249 | var $_genIDSQL = "update %s set id=LAST_INSERT_ID(id+1);"; |
---|
250 | var $_genSeqSQL = "create table %s (id int not null)"; |
---|
251 | var $_genSeq2SQL = "insert into %s values (%s)"; |
---|
252 | var $_dropSeqSQL = "drop table %s"; |
---|
253 | |
---|
254 | function CreateSequence($seqname='adodbseq',$startID=1) |
---|
255 | { |
---|
256 | if (empty($this->_genSeqSQL)) return false; |
---|
257 | $u = strtoupper($seqname); |
---|
258 | |
---|
259 | $ok = $this->Execute(sprintf($this->_genSeqSQL,$seqname)); |
---|
260 | if (!$ok) return false; |
---|
261 | return $this->Execute(sprintf($this->_genSeq2SQL,$seqname,$startID-1)); |
---|
262 | } |
---|
263 | |
---|
264 | function GenID($seqname='adodbseq',$startID=1) |
---|
265 | { |
---|
266 | // post-nuke sets hasGenID to false |
---|
267 | if (!$this->hasGenID) return false; |
---|
268 | |
---|
269 | $getnext = sprintf($this->_genIDSQL,$seqname); |
---|
270 | $holdtransOK = $this->_transOK; // save the current status |
---|
271 | $rs = @$this->Execute($getnext); |
---|
272 | if (!$rs) { |
---|
273 | if ($holdtransOK) $this->_transOK = true; //if the status was ok before reset |
---|
274 | $u = strtoupper($seqname); |
---|
275 | $this->Execute(sprintf($this->_genSeqSQL,$seqname)); |
---|
276 | $cnt = $this->GetOne(sprintf($this->_genSeqCountSQL,$seqname)); |
---|
277 | if (!$cnt) $this->Execute(sprintf($this->_genSeq2SQL,$seqname,$startID-1)); |
---|
278 | $rs = $this->Execute($getnext); |
---|
279 | } |
---|
280 | |
---|
281 | if ($rs) { |
---|
282 | $this->genID = mysqli_insert_id($this->_connectionID); |
---|
283 | $rs->Close(); |
---|
284 | } else |
---|
285 | $this->genID = 0; |
---|
286 | |
---|
287 | return $this->genID; |
---|
288 | } |
---|
289 | |
---|
290 | function &MetaDatabases() |
---|
291 | { |
---|
292 | $query = "SHOW DATABASES"; |
---|
293 | $ret =& $this->Execute($query); |
---|
294 | if ($ret && is_object($ret)){ |
---|
295 | $arr = array(); |
---|
296 | while (!$ret->EOF){ |
---|
297 | $db = $ret->Fields('Database'); |
---|
298 | if ($db != 'mysql') $arr[] = $db; |
---|
299 | $ret->MoveNext(); |
---|
300 | } |
---|
301 | return $arr; |
---|
302 | } |
---|
303 | return $ret; |
---|
304 | } |
---|
305 | |
---|
306 | |
---|
307 | function &MetaIndexes ($table, $primary = FALSE) |
---|
308 | { |
---|
309 | // save old fetch mode |
---|
310 | global $ADODB_FETCH_MODE; |
---|
311 | |
---|
312 | $false = false; |
---|
313 | $save = $ADODB_FETCH_MODE; |
---|
314 | $ADODB_FETCH_MODE = ADODB_FETCH_NUM; |
---|
315 | if ($this->fetchMode !== FALSE) { |
---|
316 | $savem = $this->SetFetchMode(FALSE); |
---|
317 | } |
---|
318 | |
---|
319 | // get index details |
---|
320 | $rs = $this->Execute(sprintf('SHOW INDEXES FROM %s',$table)); |
---|
321 | |
---|
322 | // restore fetchmode |
---|
323 | if (isset($savem)) { |
---|
324 | $this->SetFetchMode($savem); |
---|
325 | } |
---|
326 | $ADODB_FETCH_MODE = $save; |
---|
327 | |
---|
328 | if (!is_object($rs)) { |
---|
329 | return $false; |
---|
330 | } |
---|
331 | |
---|
332 | $indexes = array (); |
---|
333 | |
---|
334 | // parse index data into array |
---|
335 | while ($row = $rs->FetchRow()) { |
---|
336 | if ($primary == FALSE AND $row[2] == 'PRIMARY') { |
---|
337 | continue; |
---|
338 | } |
---|
339 | |
---|
340 | if (!isset($indexes[$row[2]])) { |
---|
341 | $indexes[$row[2]] = array( |
---|
342 | 'unique' => ($row[1] == 0), |
---|
343 | 'columns' => array() |
---|
344 | ); |
---|
345 | } |
---|
346 | |
---|
347 | $indexes[$row[2]]['columns'][$row[3] - 1] = $row[4]; |
---|
348 | } |
---|
349 | |
---|
350 | // sort columns by order in the index |
---|
351 | foreach ( array_keys ($indexes) as $index ) |
---|
352 | { |
---|
353 | ksort ($indexes[$index]['columns']); |
---|
354 | } |
---|
355 | |
---|
356 | return $indexes; |
---|
357 | } |
---|
358 | |
---|
359 | |
---|
360 | // Format date column in sql string given an input format that understands Y M D |
---|
361 | function SQLDate($fmt, $col=false) |
---|
362 | { |
---|
363 | if (!$col) $col = $this->sysTimeStamp; |
---|
364 | $s = 'DATE_FORMAT('.$col.",'"; |
---|
365 | $concat = false; |
---|
366 | $len = strlen($fmt); |
---|
367 | for ($i=0; $i < $len; $i++) { |
---|
368 | $ch = $fmt[$i]; |
---|
369 | switch($ch) { |
---|
370 | case 'Y': |
---|
371 | case 'y': |
---|
372 | $s .= '%Y'; |
---|
373 | break; |
---|
374 | case 'Q': |
---|
375 | case 'q': |
---|
376 | $s .= "'),Quarter($col)"; |
---|
377 | |
---|
378 | if ($len > $i+1) $s .= ",DATE_FORMAT($col,'"; |
---|
379 | else $s .= ",('"; |
---|
380 | $concat = true; |
---|
381 | break; |
---|
382 | case 'M': |
---|
383 | $s .= '%b'; |
---|
384 | break; |
---|
385 | |
---|
386 | case 'm': |
---|
387 | $s .= '%m'; |
---|
388 | break; |
---|
389 | case 'D': |
---|
390 | case 'd': |
---|
391 | $s .= '%d'; |
---|
392 | break; |
---|
393 | |
---|
394 | case 'H': |
---|
395 | $s .= '%H'; |
---|
396 | break; |
---|
397 | |
---|
398 | case 'h': |
---|
399 | $s .= '%I'; |
---|
400 | break; |
---|
401 | |
---|
402 | case 'i': |
---|
403 | $s .= '%i'; |
---|
404 | break; |
---|
405 | |
---|
406 | case 's': |
---|
407 | $s .= '%s'; |
---|
408 | break; |
---|
409 | |
---|
410 | case 'a': |
---|
411 | case 'A': |
---|
412 | $s .= '%p'; |
---|
413 | break; |
---|
414 | |
---|
415 | case 'w': |
---|
416 | $s .= '%w'; |
---|
417 | break; |
---|
418 | |
---|
419 | case 'l': |
---|
420 | $s .= '%W'; |
---|
421 | break; |
---|
422 | |
---|
423 | default: |
---|
424 | |
---|
425 | if ($ch == '\\') { |
---|
426 | $i++; |
---|
427 | $ch = substr($fmt,$i,1); |
---|
428 | } |
---|
429 | $s .= $ch; |
---|
430 | break; |
---|
431 | } |
---|
432 | } |
---|
433 | $s.="')"; |
---|
434 | if ($concat) $s = "CONCAT($s)"; |
---|
435 | return $s; |
---|
436 | } |
---|
437 | |
---|
438 | // returns concatenated string |
---|
439 | // much easier to run "mysqld --ansi" or "mysqld --sql-mode=PIPES_AS_CONCAT" and use || operator |
---|
440 | function Concat() |
---|
441 | { |
---|
442 | $s = ""; |
---|
443 | $arr = func_get_args(); |
---|
444 | |
---|
445 | // suggestion by andrew005@mnogo.ru |
---|
446 | $s = implode(',',$arr); |
---|
447 | if (strlen($s) > 0) return "CONCAT($s)"; |
---|
448 | else return ''; |
---|
449 | } |
---|
450 | |
---|
451 | // dayFraction is a day in floating point |
---|
452 | function OffsetDate($dayFraction,$date=false) |
---|
453 | { |
---|
454 | if (!$date) $date = $this->sysDate; |
---|
455 | |
---|
456 | $fraction = $dayFraction * 24 * 3600; |
---|
457 | return $date . ' + INTERVAL ' . $fraction.' SECOND'; |
---|
458 | |
---|
459 | // return "from_unixtime(unix_timestamp($date)+$fraction)"; |
---|
460 | } |
---|
461 | |
---|
462 | function &MetaTables($ttype=false,$showSchema=false,$mask=false) |
---|
463 | { |
---|
464 | $save = $this->metaTablesSQL; |
---|
465 | if ($showSchema && is_string($showSchema)) { |
---|
466 | $this->metaTablesSQL .= " from $showSchema"; |
---|
467 | } |
---|
468 | |
---|
469 | if ($mask) { |
---|
470 | $mask = $this->qstr($mask); |
---|
471 | $this->metaTablesSQL .= " like $mask"; |
---|
472 | } |
---|
473 | $ret =& ADOConnection::MetaTables($ttype,$showSchema); |
---|
474 | |
---|
475 | $this->metaTablesSQL = $save; |
---|
476 | return $ret; |
---|
477 | } |
---|
478 | |
---|
479 | // "Innox - Juan Carlos Gonzalez" <jgonzalez#innox.com.mx> |
---|
480 | function MetaForeignKeys( $table, $owner = FALSE, $upper = FALSE, $associative = FALSE ) |
---|
481 | { |
---|
482 | global $ADODB_FETCH_MODE; |
---|
483 | |
---|
484 | if ($ADODB_FETCH_MODE == ADODB_FETCH_ASSOC || $this->fetchMode == ADODB_FETCH_ASSOC) $associative = true; |
---|
485 | |
---|
486 | if ( !empty($owner) ) { |
---|
487 | $table = "$owner.$table"; |
---|
488 | } |
---|
489 | $a_create_table = $this->getRow(sprintf('SHOW CREATE TABLE %s', $table)); |
---|
490 | if ($associative) $create_sql = $a_create_table["Create Table"]; |
---|
491 | else $create_sql = $a_create_table[1]; |
---|
492 | |
---|
493 | $matches = array(); |
---|
494 | |
---|
495 | if (!preg_match_all("/FOREIGN KEY \(`(.*?)`\) REFERENCES `(.*?)` \(`(.*?)`\)/", $create_sql, $matches)) return false; |
---|
496 | $foreign_keys = array(); |
---|
497 | $num_keys = count($matches[0]); |
---|
498 | for ( $i = 0; $i < $num_keys; $i ++ ) { |
---|
499 | $my_field = explode('`, `', $matches[1][$i]); |
---|
500 | $ref_table = $matches[2][$i]; |
---|
501 | $ref_field = explode('`, `', $matches[3][$i]); |
---|
502 | |
---|
503 | if ( $upper ) { |
---|
504 | $ref_table = strtoupper($ref_table); |
---|
505 | } |
---|
506 | |
---|
507 | $foreign_keys[$ref_table] = array(); |
---|
508 | $num_fields = count($my_field); |
---|
509 | for ( $j = 0; $j < $num_fields; $j ++ ) { |
---|
510 | if ( $associative ) { |
---|
511 | $foreign_keys[$ref_table][$ref_field[$j]] = $my_field[$j]; |
---|
512 | } else { |
---|
513 | $foreign_keys[$ref_table][] = "{$my_field[$j]}={$ref_field[$j]}"; |
---|
514 | } |
---|
515 | } |
---|
516 | } |
---|
517 | |
---|
518 | return $foreign_keys; |
---|
519 | } |
---|
520 | |
---|
521 | function &MetaColumns($table) |
---|
522 | { |
---|
523 | $false = false; |
---|
524 | if (!$this->metaColumnsSQL) |
---|
525 | return $false; |
---|
526 | |
---|
527 | global $ADODB_FETCH_MODE; |
---|
528 | $save = $ADODB_FETCH_MODE; |
---|
529 | $ADODB_FETCH_MODE = ADODB_FETCH_NUM; |
---|
530 | if ($this->fetchMode !== false) |
---|
531 | $savem = $this->SetFetchMode(false); |
---|
532 | $rs = $this->Execute(sprintf($this->metaColumnsSQL,$table)); |
---|
533 | if (isset($savem)) $this->SetFetchMode($savem); |
---|
534 | $ADODB_FETCH_MODE = $save; |
---|
535 | if (!is_object($rs)) |
---|
536 | return $false; |
---|
537 | |
---|
538 | $retarr = array(); |
---|
539 | while (!$rs->EOF) { |
---|
540 | $fld = new ADOFieldObject(); |
---|
541 | $fld->name = $rs->fields[0]; |
---|
542 | $type = $rs->fields[1]; |
---|
543 | |
---|
544 | // split type into type(length): |
---|
545 | $fld->scale = null; |
---|
546 | if (preg_match("/^(.+)\((\d+),(\d+)/", $type, $query_array)) { |
---|
547 | $fld->type = $query_array[1]; |
---|
548 | $fld->max_length = is_numeric($query_array[2]) ? $query_array[2] : -1; |
---|
549 | $fld->scale = is_numeric($query_array[3]) ? $query_array[3] : -1; |
---|
550 | } elseif (preg_match("/^(.+)\((\d+)/", $type, $query_array)) { |
---|
551 | $fld->type = $query_array[1]; |
---|
552 | $fld->max_length = is_numeric($query_array[2]) ? $query_array[2] : -1; |
---|
553 | } elseif (preg_match("/^(enum)\((.*)\)$/i", $type, $query_array)) { |
---|
554 | $fld->type = $query_array[1]; |
---|
555 | $fld->max_length = max(array_map("strlen",explode(",",$query_array[2]))) - 2; // PHP >= 4.0.6 |
---|
556 | $fld->max_length = ($fld->max_length == 0 ? 1 : $fld->max_length); |
---|
557 | } else { |
---|
558 | $fld->type = $type; |
---|
559 | $fld->max_length = -1; |
---|
560 | } |
---|
561 | $fld->not_null = ($rs->fields[2] != 'YES'); |
---|
562 | $fld->primary_key = ($rs->fields[3] == 'PRI'); |
---|
563 | $fld->auto_increment = (strpos($rs->fields[5], 'auto_increment') !== false); |
---|
564 | $fld->binary = (strpos($type,'blob') !== false); |
---|
565 | $fld->unsigned = (strpos($type,'unsigned') !== false); |
---|
566 | $fld->zerofill = (strpos($type,'zerofill') !== false); |
---|
567 | |
---|
568 | if (!$fld->binary) { |
---|
569 | $d = $rs->fields[4]; |
---|
570 | if ($d != '' && $d != 'NULL') { |
---|
571 | $fld->has_default = true; |
---|
572 | $fld->default_value = $d; |
---|
573 | } else { |
---|
574 | $fld->has_default = false; |
---|
575 | } |
---|
576 | } |
---|
577 | |
---|
578 | if ($save == ADODB_FETCH_NUM) { |
---|
579 | $retarr[] = $fld; |
---|
580 | } else { |
---|
581 | $retarr[strtoupper($fld->name)] = $fld; |
---|
582 | } |
---|
583 | $rs->MoveNext(); |
---|
584 | } |
---|
585 | |
---|
586 | $rs->Close(); |
---|
587 | return $retarr; |
---|
588 | } |
---|
589 | |
---|
590 | // returns true or false |
---|
591 | function SelectDB($dbName) |
---|
592 | { |
---|
593 | // $this->_connectionID = $this->mysqli_resolve_link($this->_connectionID); |
---|
594 | $this->database = $dbName; |
---|
595 | $this->databaseName = $dbName; # obsolete, retained for compat with older adodb versions |
---|
596 | |
---|
597 | if ($this->_connectionID) { |
---|
598 | $result = @mysqli_select_db($this->_connectionID, $dbName); |
---|
599 | if (!$result) { |
---|
600 | ADOConnection::outp("Select of database " . $dbName . " failed. " . $this->ErrorMsg()); |
---|
601 | } |
---|
602 | return $result; |
---|
603 | } |
---|
604 | return false; |
---|
605 | } |
---|
606 | |
---|
607 | // parameters use PostgreSQL convention, not MySQL |
---|
608 | function &SelectLimit($sql, |
---|
609 | $nrows = -1, |
---|
610 | $offset = -1, |
---|
611 | $inputarr = false, |
---|
612 | $arg3 = false, |
---|
613 | $secs = 0) |
---|
614 | { |
---|
615 | $offsetStr = ($offset >= 0) ? "$offset," : ''; |
---|
616 | if ($nrows < 0) $nrows = '18446744073709551615'; |
---|
617 | |
---|
618 | if ($secs) |
---|
619 | $rs =& $this->CacheExecute($secs, $sql . " LIMIT $offsetStr$nrows" , $inputarr , $arg3); |
---|
620 | else |
---|
621 | $rs =& $this->Execute($sql . " LIMIT $offsetStr$nrows" , $inputarr , $arg3); |
---|
622 | |
---|
623 | return $rs; |
---|
624 | } |
---|
625 | |
---|
626 | |
---|
627 | function Prepare($sql) |
---|
628 | { |
---|
629 | return $sql; |
---|
630 | |
---|
631 | $stmt = $this->_connectionID->prepare($sql); |
---|
632 | if (!$stmt) { |
---|
633 | echo $this->ErrorMsg(); |
---|
634 | return $sql; |
---|
635 | } |
---|
636 | return array($sql,$stmt); |
---|
637 | } |
---|
638 | |
---|
639 | |
---|
640 | // returns queryID or false |
---|
641 | function _query($sql, $inputarr) |
---|
642 | { |
---|
643 | global $ADODB_COUNTRECS; |
---|
644 | |
---|
645 | if (is_array($sql)) { |
---|
646 | $stmt = $sql[1]; |
---|
647 | $a = ''; |
---|
648 | foreach($inputarr as $k => $v) { |
---|
649 | if (is_string($v)) $a .= 's'; |
---|
650 | else if (is_integer($v)) $a .= 'i'; |
---|
651 | else $a .= 'd'; |
---|
652 | } |
---|
653 | |
---|
654 | $fnarr = array_merge( array($stmt,$a) , $inputarr); |
---|
655 | $ret = call_user_func_array('mysqli_stmt_bind_param',$fnarr); |
---|
656 | |
---|
657 | $ret = mysqli_stmt_execute($stmt); |
---|
658 | return $ret; |
---|
659 | } |
---|
660 | if (!$mysql_res = mysqli_query($this->_connectionID, $sql, ($ADODB_COUNTRECS) ? MYSQLI_STORE_RESULT : MYSQLI_USE_RESULT)) { |
---|
661 | if ($this->debug) ADOConnection::outp("Query: " . $sql . " failed. " . $this->ErrorMsg()); |
---|
662 | return false; |
---|
663 | } |
---|
664 | |
---|
665 | return $mysql_res; |
---|
666 | } |
---|
667 | |
---|
668 | /* Returns: the last error message from previous database operation */ |
---|
669 | function ErrorMsg() |
---|
670 | { |
---|
671 | if (empty($this->_connectionID)) |
---|
672 | $this->_errorMsg = @mysqli_connect_error(); |
---|
673 | else |
---|
674 | $this->_errorMsg = @mysqli_error($this->_connectionID); |
---|
675 | return $this->_errorMsg; |
---|
676 | } |
---|
677 | |
---|
678 | /* Returns: the last error number from previous database operation */ |
---|
679 | function ErrorNo() |
---|
680 | { |
---|
681 | if (empty($this->_connectionID)) |
---|
682 | return @mysqli_connect_errno(); |
---|
683 | else |
---|
684 | return @mysqli_errno($this->_connectionID); |
---|
685 | } |
---|
686 | |
---|
687 | // returns true or false |
---|
688 | function _close() |
---|
689 | { |
---|
690 | @mysqli_close($this->_connectionID); |
---|
691 | $this->_connectionID = false; |
---|
692 | } |
---|
693 | |
---|
694 | /* |
---|
695 | * Maximum size of C field |
---|
696 | */ |
---|
697 | function CharMax() |
---|
698 | { |
---|
699 | return 255; |
---|
700 | } |
---|
701 | |
---|
702 | /* |
---|
703 | * Maximum size of X field |
---|
704 | */ |
---|
705 | function TextMax() |
---|
706 | { |
---|
707 | return 4294967295; |
---|
708 | } |
---|
709 | |
---|
710 | |
---|
711 | |
---|
712 | // this is a set of functions for managing client encoding - very important if the encodings |
---|
713 | // of your database and your output target (i.e. HTML) don't match |
---|
714 | // for instance, you may have UTF8 database and server it on-site as latin1 etc. |
---|
715 | // GetCharSet - get the name of the character set the client is using now |
---|
716 | // Under Windows, the functions should work with MySQL 4.1.11 and above, the set of charsets supported |
---|
717 | // depends on compile flags of mysql distribution |
---|
718 | |
---|
719 | function GetCharSet() |
---|
720 | { |
---|
721 | //we will use ADO's builtin property charSet |
---|
722 | if (!method_exists($this->_connectionID,'character_set_name')) |
---|
723 | return false; |
---|
724 | |
---|
725 | $this->charSet = @$this->_connectionID->character_set_name(); |
---|
726 | if (!$this->charSet) { |
---|
727 | return false; |
---|
728 | } else { |
---|
729 | return $this->charSet; |
---|
730 | } |
---|
731 | } |
---|
732 | |
---|
733 | // SetCharSet - switch the client encoding |
---|
734 | function SetCharSet($charset_name) |
---|
735 | { |
---|
736 | if (!method_exists($this->_connectionID,'set_charset')) |
---|
737 | return false; |
---|
738 | |
---|
739 | if ($this->charSet !== $charset_name) { |
---|
740 | $if = @$this->_connectionID->set_charset($charset_name); |
---|
741 | if ($if == "0" & $this->GetCharSet() == $charset_name) { |
---|
742 | return true; |
---|
743 | } else return false; |
---|
744 | } else return true; |
---|
745 | } |
---|
746 | |
---|
747 | |
---|
748 | |
---|
749 | |
---|
750 | } |
---|
751 | |
---|
752 | /*-------------------------------------------------------------------------------------- |
---|
753 | Class Name: Recordset |
---|
754 | --------------------------------------------------------------------------------------*/ |
---|
755 | |
---|
756 | class ADORecordSet_mysqli extends ADORecordSet{ |
---|
757 | |
---|
758 | var $databaseType = "mysqli"; |
---|
759 | var $canSeek = true; |
---|
760 | |
---|
761 | function ADORecordSet_mysqli($queryID, $mode = false) |
---|
762 | { |
---|
763 | if ($mode === false) |
---|
764 | { |
---|
765 | global $ADODB_FETCH_MODE; |
---|
766 | $mode = $ADODB_FETCH_MODE; |
---|
767 | } |
---|
768 | |
---|
769 | switch ($mode) |
---|
770 | { |
---|
771 | case ADODB_FETCH_NUM: |
---|
772 | $this->fetchMode = MYSQLI_NUM; |
---|
773 | break; |
---|
774 | case ADODB_FETCH_ASSOC: |
---|
775 | $this->fetchMode = MYSQLI_ASSOC; |
---|
776 | break; |
---|
777 | case ADODB_FETCH_DEFAULT: |
---|
778 | case ADODB_FETCH_BOTH: |
---|
779 | default: |
---|
780 | $this->fetchMode = MYSQLI_BOTH; |
---|
781 | break; |
---|
782 | } |
---|
783 | $this->adodbFetchMode = $mode; |
---|
784 | $this->ADORecordSet($queryID); |
---|
785 | } |
---|
786 | |
---|
787 | function _initrs() |
---|
788 | { |
---|
789 | global $ADODB_COUNTRECS; |
---|
790 | |
---|
791 | $this->_numOfRows = $ADODB_COUNTRECS ? @mysqli_num_rows($this->_queryID) : -1; |
---|
792 | $this->_numOfFields = @mysqli_num_fields($this->_queryID); |
---|
793 | } |
---|
794 | |
---|
795 | /* |
---|
796 | 1 = MYSQLI_NOT_NULL_FLAG |
---|
797 | 2 = MYSQLI_PRI_KEY_FLAG |
---|
798 | 4 = MYSQLI_UNIQUE_KEY_FLAG |
---|
799 | 8 = MYSQLI_MULTIPLE_KEY_FLAG |
---|
800 | 16 = MYSQLI_BLOB_FLAG |
---|
801 | 32 = MYSQLI_UNSIGNED_FLAG |
---|
802 | 64 = MYSQLI_ZEROFILL_FLAG |
---|
803 | 128 = MYSQLI_BINARY_FLAG |
---|
804 | 256 = MYSQLI_ENUM_FLAG |
---|
805 | 512 = MYSQLI_AUTO_INCREMENT_FLAG |
---|
806 | 1024 = MYSQLI_TIMESTAMP_FLAG |
---|
807 | 2048 = MYSQLI_SET_FLAG |
---|
808 | 32768 = MYSQLI_NUM_FLAG |
---|
809 | 16384 = MYSQLI_PART_KEY_FLAG |
---|
810 | 32768 = MYSQLI_GROUP_FLAG |
---|
811 | 65536 = MYSQLI_UNIQUE_FLAG |
---|
812 | 131072 = MYSQLI_BINCMP_FLAG |
---|
813 | */ |
---|
814 | |
---|
815 | function &FetchField($fieldOffset = -1) |
---|
816 | { |
---|
817 | $fieldnr = $fieldOffset; |
---|
818 | if ($fieldOffset != -1) { |
---|
819 | $fieldOffset = mysqli_field_seek($this->_queryID, $fieldnr); |
---|
820 | } |
---|
821 | $o = mysqli_fetch_field($this->_queryID); |
---|
822 | /* Properties of an ADOFieldObject as set by MetaColumns */ |
---|
823 | $o->primary_key = $o->flags & MYSQLI_PRI_KEY_FLAG; |
---|
824 | $o->not_null = $o->flags & MYSQLI_NOT_NULL_FLAG; |
---|
825 | $o->auto_increment = $o->flags & MYSQLI_AUTO_INCREMENT_FLAG; |
---|
826 | $o->binary = $o->flags & MYSQLI_BINARY_FLAG; |
---|
827 | // $o->blob = $o->flags & MYSQLI_BLOB_FLAG; /* not returned by MetaColumns */ |
---|
828 | $o->unsigned = $o->flags & MYSQLI_UNSIGNED_FLAG; |
---|
829 | |
---|
830 | return $o; |
---|
831 | } |
---|
832 | |
---|
833 | function &GetRowAssoc($upper = true) |
---|
834 | { |
---|
835 | if ($this->fetchMode == MYSQLI_ASSOC && !$upper) |
---|
836 | return $this->fields; |
---|
837 | $row =& ADORecordSet::GetRowAssoc($upper); |
---|
838 | return $row; |
---|
839 | } |
---|
840 | |
---|
841 | /* Use associative array to get fields array */ |
---|
842 | function Fields($colname) |
---|
843 | { |
---|
844 | if ($this->fetchMode != MYSQLI_NUM) |
---|
845 | return @$this->fields[$colname]; |
---|
846 | |
---|
847 | if (!$this->bind) { |
---|
848 | $this->bind = array(); |
---|
849 | for ($i = 0; $i < $this->_numOfFields; $i++) { |
---|
850 | $o = $this->FetchField($i); |
---|
851 | $this->bind[strtoupper($o->name)] = $i; |
---|
852 | } |
---|
853 | } |
---|
854 | return $this->fields[$this->bind[strtoupper($colname)]]; |
---|
855 | } |
---|
856 | |
---|
857 | function _seek($row) |
---|
858 | { |
---|
859 | if ($this->_numOfRows == 0) |
---|
860 | return false; |
---|
861 | |
---|
862 | if ($row < 0) |
---|
863 | return false; |
---|
864 | |
---|
865 | mysqli_data_seek($this->_queryID, $row); |
---|
866 | $this->EOF = false; |
---|
867 | return true; |
---|
868 | } |
---|
869 | |
---|
870 | // 10% speedup to move MoveNext to child class |
---|
871 | // This is the only implementation that works now (23-10-2003). |
---|
872 | // Other functions return no or the wrong results. |
---|
873 | function MoveNext() |
---|
874 | { |
---|
875 | if ($this->EOF) return false; |
---|
876 | $this->_currentRow++; |
---|
877 | $this->fields = @mysqli_fetch_array($this->_queryID,$this->fetchMode); |
---|
878 | |
---|
879 | if (is_array($this->fields)) return true; |
---|
880 | $this->EOF = true; |
---|
881 | return false; |
---|
882 | } |
---|
883 | |
---|
884 | function _fetch() |
---|
885 | { |
---|
886 | $this->fields = mysqli_fetch_array($this->_queryID,$this->fetchMode); |
---|
887 | return is_array($this->fields); |
---|
888 | } |
---|
889 | |
---|
890 | function _close() |
---|
891 | { |
---|
892 | mysqli_free_result($this->_queryID); |
---|
893 | $this->_queryID = false; |
---|
894 | } |
---|
895 | |
---|
896 | /* |
---|
897 | |
---|
898 | 0 = MYSQLI_TYPE_DECIMAL |
---|
899 | 1 = MYSQLI_TYPE_CHAR |
---|
900 | 1 = MYSQLI_TYPE_TINY |
---|
901 | 2 = MYSQLI_TYPE_SHORT |
---|
902 | 3 = MYSQLI_TYPE_LONG |
---|
903 | 4 = MYSQLI_TYPE_FLOAT |
---|
904 | 5 = MYSQLI_TYPE_DOUBLE |
---|
905 | 6 = MYSQLI_TYPE_NULL |
---|
906 | 7 = MYSQLI_TYPE_TIMESTAMP |
---|
907 | 8 = MYSQLI_TYPE_LONGLONG |
---|
908 | 9 = MYSQLI_TYPE_INT24 |
---|
909 | 10 = MYSQLI_TYPE_DATE |
---|
910 | 11 = MYSQLI_TYPE_TIME |
---|
911 | 12 = MYSQLI_TYPE_DATETIME |
---|
912 | 13 = MYSQLI_TYPE_YEAR |
---|
913 | 14 = MYSQLI_TYPE_NEWDATE |
---|
914 | 247 = MYSQLI_TYPE_ENUM |
---|
915 | 248 = MYSQLI_TYPE_SET |
---|
916 | 249 = MYSQLI_TYPE_TINY_BLOB |
---|
917 | 250 = MYSQLI_TYPE_MEDIUM_BLOB |
---|
918 | 251 = MYSQLI_TYPE_LONG_BLOB |
---|
919 | 252 = MYSQLI_TYPE_BLOB |
---|
920 | 253 = MYSQLI_TYPE_VAR_STRING |
---|
921 | 254 = MYSQLI_TYPE_STRING |
---|
922 | 255 = MYSQLI_TYPE_GEOMETRY |
---|
923 | */ |
---|
924 | |
---|
925 | function MetaType($t, $len = -1, $fieldobj = false) |
---|
926 | { |
---|
927 | if (is_object($t)) { |
---|
928 | $fieldobj = $t; |
---|
929 | $t = $fieldobj->type; |
---|
930 | $len = $fieldobj->max_length; |
---|
931 | } |
---|
932 | |
---|
933 | |
---|
934 | $len = -1; // mysql max_length is not accurate |
---|
935 | switch (strtoupper($t)) { |
---|
936 | case 'STRING': |
---|
937 | case 'CHAR': |
---|
938 | case 'VARCHAR': |
---|
939 | case 'TINYBLOB': |
---|
940 | case 'TINYTEXT': |
---|
941 | case 'ENUM': |
---|
942 | case 'SET': |
---|
943 | |
---|
944 | case MYSQLI_TYPE_TINY_BLOB : |
---|
945 | case MYSQLI_TYPE_CHAR : |
---|
946 | case MYSQLI_TYPE_STRING : |
---|
947 | case MYSQLI_TYPE_ENUM : |
---|
948 | case MYSQLI_TYPE_SET : |
---|
949 | case 253 : |
---|
950 | if ($len <= $this->blobSize) return 'C'; |
---|
951 | |
---|
952 | case 'TEXT': |
---|
953 | case 'LONGTEXT': |
---|
954 | case 'MEDIUMTEXT': |
---|
955 | return 'X'; |
---|
956 | |
---|
957 | |
---|
958 | // php_mysql extension always returns 'blob' even if 'text' |
---|
959 | // so we have to check whether binary... |
---|
960 | case 'IMAGE': |
---|
961 | case 'LONGBLOB': |
---|
962 | case 'BLOB': |
---|
963 | case 'MEDIUMBLOB': |
---|
964 | |
---|
965 | case MYSQLI_TYPE_BLOB : |
---|
966 | case MYSQLI_TYPE_LONG_BLOB : |
---|
967 | case MYSQLI_TYPE_MEDIUM_BLOB : |
---|
968 | |
---|
969 | return !empty($fieldobj->binary) ? 'B' : 'X'; |
---|
970 | case 'YEAR': |
---|
971 | case 'DATE': |
---|
972 | case MYSQLI_TYPE_DATE : |
---|
973 | case MYSQLI_TYPE_YEAR : |
---|
974 | |
---|
975 | return 'D'; |
---|
976 | |
---|
977 | case 'TIME': |
---|
978 | case 'DATETIME': |
---|
979 | case 'TIMESTAMP': |
---|
980 | |
---|
981 | case MYSQLI_TYPE_DATETIME : |
---|
982 | case MYSQLI_TYPE_NEWDATE : |
---|
983 | case MYSQLI_TYPE_TIME : |
---|
984 | case MYSQLI_TYPE_TIMESTAMP : |
---|
985 | |
---|
986 | return 'T'; |
---|
987 | |
---|
988 | case 'INT': |
---|
989 | case 'INTEGER': |
---|
990 | case 'BIGINT': |
---|
991 | case 'TINYINT': |
---|
992 | case 'MEDIUMINT': |
---|
993 | case 'SMALLINT': |
---|
994 | |
---|
995 | case MYSQLI_TYPE_INT24 : |
---|
996 | case MYSQLI_TYPE_LONG : |
---|
997 | case MYSQLI_TYPE_LONGLONG : |
---|
998 | case MYSQLI_TYPE_SHORT : |
---|
999 | case MYSQLI_TYPE_TINY : |
---|
1000 | |
---|
1001 | if (!empty($fieldobj->primary_key)) return 'R'; |
---|
1002 | |
---|
1003 | return 'I'; |
---|
1004 | |
---|
1005 | |
---|
1006 | // Added floating-point types |
---|
1007 | // Maybe not necessery. |
---|
1008 | case 'FLOAT': |
---|
1009 | case 'DOUBLE': |
---|
1010 | // case 'DOUBLE PRECISION': |
---|
1011 | case 'DECIMAL': |
---|
1012 | case 'DEC': |
---|
1013 | case 'FIXED': |
---|
1014 | default: |
---|
1015 | //if (!is_numeric($t)) echo "<p>--- Error in type matching $t -----</p>"; |
---|
1016 | return 'N'; |
---|
1017 | } |
---|
1018 | } // function |
---|
1019 | |
---|
1020 | |
---|
1021 | } // rs class |
---|
1022 | |
---|
1023 | } |
---|
1024 | |
---|
1025 | ?> |
---|