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