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