[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
1 <?php 2 /* 3 V5.19 23-Apr-2014 (c) 2000-2014 John Lim (jlim#natsoft.com). 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 28 Feb 2001: MetaColumns bug fix - suggested by Freek Dijkstra ([email protected]) 13 */ 14 15 // security - hide paths 16 if (!defined('ADODB_DIR')) die(); 17 18 if (! defined("_ADODB_MYSQL_LAYER")) { 19 define("_ADODB_MYSQL_LAYER", 1 ); 20 21 class ADODB_mysql extends ADOConnection { 22 var $databaseType = 'mysql'; 23 var $dataProvider = 'mysql'; 24 var $hasInsertID = true; 25 var $hasAffectedRows = true; 26 var $metaTablesSQL = "SELECT 27 TABLE_NAME, 28 CASE WHEN TABLE_TYPE = 'VIEW' THEN 'V' ELSE 'T' END 29 FROM INFORMATION_SCHEMA.TABLES 30 WHERE TABLE_SCHEMA="; 31 var $metaColumnsSQL = "SHOW COLUMNS FROM `%s`"; 32 var $fmtTimeStamp = "'Y-m-d H:i:s'"; 33 var $hasLimit = true; 34 var $hasMoveFirst = true; 35 var $hasGenID = true; 36 var $isoDates = true; // accepts dates in ISO format 37 var $sysDate = 'CURDATE()'; 38 var $sysTimeStamp = 'NOW()'; 39 var $hasTransactions = false; 40 var $forceNewConnect = false; 41 var $poorAffectedRows = true; 42 var $clientFlags = 0; 43 var $charSet = ''; 44 var $substr = "substring"; 45 var $nameQuote = '`'; /// string to use to quote identifiers and names 46 var $compat323 = false; // true if compat with mysql 3.23 47 48 function ADODB_mysql() 49 { 50 if (defined('ADODB_EXTENSION')) $this->rsPrefix .= 'ext_'; 51 } 52 53 54 // SetCharSet - switch the client encoding 55 function SetCharSet($charset_name) 56 { 57 if (!function_exists('mysql_set_charset')) 58 return false; 59 60 if ($this->charSet !== $charset_name) { 61 $ok = @mysql_set_charset($charset_name,$this->_connectionID); 62 if ($ok) { 63 $this->charSet = $charset_name; 64 return true; 65 } 66 return false; 67 } 68 return true; 69 } 70 71 function ServerInfo() 72 { 73 $arr['description'] = ADOConnection::GetOne("select version()"); 74 $arr['version'] = ADOConnection::_findvers($arr['description']); 75 return $arr; 76 } 77 78 function IfNull( $field, $ifNull ) 79 { 80 return " IFNULL($field, $ifNull) "; // if MySQL 81 } 82 83 function MetaProcedures($NamePattern = false, $catalog = null, $schemaPattern = null) 84 { 85 // save old fetch mode 86 global $ADODB_FETCH_MODE; 87 88 $false = false; 89 $save = $ADODB_FETCH_MODE; 90 $ADODB_FETCH_MODE = ADODB_FETCH_NUM; 91 92 if ($this->fetchMode !== FALSE) { 93 $savem = $this->SetFetchMode(FALSE); 94 } 95 96 $procedures = array (); 97 98 // get index details 99 100 $likepattern = ''; 101 if ($NamePattern) { 102 $likepattern = " LIKE '".$NamePattern."'"; 103 } 104 $rs = $this->Execute('SHOW PROCEDURE STATUS'.$likepattern); 105 if (is_object($rs)) { 106 107 // parse index data into array 108 while ($row = $rs->FetchRow()) { 109 $procedures[$row[1]] = array( 110 'type' => 'PROCEDURE', 111 'catalog' => '', 112 113 'schema' => '', 114 'remarks' => $row[7], 115 ); 116 } 117 } 118 119 $rs = $this->Execute('SHOW FUNCTION STATUS'.$likepattern); 120 if (is_object($rs)) { 121 // parse index data into array 122 while ($row = $rs->FetchRow()) { 123 $procedures[$row[1]] = array( 124 'type' => 'FUNCTION', 125 'catalog' => '', 126 'schema' => '', 127 'remarks' => $row[7] 128 ); 129 } 130 } 131 132 // restore fetchmode 133 if (isset($savem)) { 134 $this->SetFetchMode($savem); 135 136 } 137 $ADODB_FETCH_MODE = $save; 138 139 140 return $procedures; 141 } 142 143 /** 144 * Retrieves a list of tables based on given criteria 145 * 146 * @param string $ttype Table type = 'TABLE', 'VIEW' or false=both (default) 147 * @param string $showSchema schema name, false = current schema (default) 148 * @param string $mask filters the table by name 149 * 150 * @return array list of tables 151 */ 152 function MetaTables($ttype=false,$showSchema=false,$mask=false) 153 { 154 $save = $this->metaTablesSQL; 155 if ($showSchema && is_string($showSchema)) { 156 $this->metaTablesSQL .= $this->qstr($showSchema); 157 } else { 158 $this->metaTablesSQL .= "schema()"; 159 } 160 161 if ($mask) { 162 $mask = $this->qstr($mask); 163 $this->metaTablesSQL .= " AND table_name LIKE $mask"; 164 } 165 $ret = ADOConnection::MetaTables($ttype,$showSchema); 166 167 $this->metaTablesSQL = $save; 168 return $ret; 169 } 170 171 172 function MetaIndexes ($table, $primary = FALSE, $owner=false) 173 { 174 // save old fetch mode 175 global $ADODB_FETCH_MODE; 176 177 $false = false; 178 $save = $ADODB_FETCH_MODE; 179 $ADODB_FETCH_MODE = ADODB_FETCH_NUM; 180 if ($this->fetchMode !== FALSE) { 181 $savem = $this->SetFetchMode(FALSE); 182 } 183 184 // get index details 185 $rs = $this->Execute(sprintf('SHOW INDEX FROM %s',$table)); 186 187 // restore fetchmode 188 if (isset($savem)) { 189 $this->SetFetchMode($savem); 190 } 191 $ADODB_FETCH_MODE = $save; 192 193 if (!is_object($rs)) { 194 return $false; 195 } 196 197 $indexes = array (); 198 199 // parse index data into array 200 while ($row = $rs->FetchRow()) { 201 if ($primary == FALSE AND $row[2] == 'PRIMARY') { 202 continue; 203 } 204 205 if (!isset($indexes[$row[2]])) { 206 $indexes[$row[2]] = array( 207 'unique' => ($row[1] == 0), 208 'columns' => array() 209 ); 210 } 211 212 $indexes[$row[2]]['columns'][$row[3] - 1] = $row[4]; 213 } 214 215 // sort columns by order in the index 216 foreach ( array_keys ($indexes) as $index ) 217 { 218 ksort ($indexes[$index]['columns']); 219 } 220 221 return $indexes; 222 } 223 224 225 // if magic quotes disabled, use mysql_real_escape_string() 226 function qstr($s,$magic_quotes=false) 227 { 228 if (is_null($s)) return 'NULL'; 229 if (!$magic_quotes) { 230 231 if (ADODB_PHPVER >= 0x4300) { 232 if (is_resource($this->_connectionID)) 233 return "'".mysql_real_escape_string($s,$this->_connectionID)."'"; 234 } 235 if ($this->replaceQuote[0] == '\\'){ 236 $s = adodb_str_replace(array('\\',"\0"),array('\\\\',"\\\0"),$s); 237 } 238 return "'".str_replace("'",$this->replaceQuote,$s)."'"; 239 } 240 241 // undo magic quotes for " 242 $s = str_replace('\\"','"',$s); 243 return "'$s'"; 244 } 245 246 function _insertid() 247 { 248 return ADOConnection::GetOne('SELECT LAST_INSERT_ID()'); 249 //return mysql_insert_id($this->_connectionID); 250 } 251 252 function GetOne($sql,$inputarr=false) 253 { 254 global $ADODB_GETONE_EOF; 255 if ($this->compat323 == false && strncasecmp($sql,'sele',4) == 0) { 256 $rs = $this->SelectLimit($sql,1,-1,$inputarr); 257 if ($rs) { 258 $rs->Close(); 259 if ($rs->EOF) return $ADODB_GETONE_EOF; 260 return reset($rs->fields); 261 } 262 } else { 263 return ADOConnection::GetOne($sql,$inputarr); 264 } 265 return false; 266 } 267 268 function BeginTrans() 269 { 270 if ($this->debug) ADOConnection::outp("Transactions not supported in 'mysql' driver. Use 'mysqlt' or 'mysqli' driver"); 271 } 272 273 function _affectedrows() 274 { 275 return mysql_affected_rows($this->_connectionID); 276 } 277 278 // See http://www.mysql.com/doc/M/i/Miscellaneous_functions.html 279 // Reference on Last_Insert_ID on the recommended way to simulate sequences 280 var $_genIDSQL = "update %s set id=LAST_INSERT_ID(id+1);"; 281 var $_genSeqSQL = "create table %s (id int not null)"; 282 var $_genSeqCountSQL = "select count(*) from %s"; 283 var $_genSeq2SQL = "insert into %s values (%s)"; 284 var $_dropSeqSQL = "drop table %s"; 285 286 function CreateSequence($seqname='adodbseq',$startID=1) 287 { 288 if (empty($this->_genSeqSQL)) return false; 289 $u = strtoupper($seqname); 290 291 $ok = $this->Execute(sprintf($this->_genSeqSQL,$seqname)); 292 if (!$ok) return false; 293 return $this->Execute(sprintf($this->_genSeq2SQL,$seqname,$startID-1)); 294 } 295 296 297 function GenID($seqname='adodbseq',$startID=1) 298 { 299 // post-nuke sets hasGenID to false 300 if (!$this->hasGenID) return false; 301 302 $savelog = $this->_logsql; 303 $this->_logsql = false; 304 $getnext = sprintf($this->_genIDSQL,$seqname); 305 $holdtransOK = $this->_transOK; // save the current status 306 $rs = @$this->Execute($getnext); 307 if (!$rs) { 308 if ($holdtransOK) $this->_transOK = true; //if the status was ok before reset 309 $u = strtoupper($seqname); 310 $this->Execute(sprintf($this->_genSeqSQL,$seqname)); 311 $cnt = $this->GetOne(sprintf($this->_genSeqCountSQL,$seqname)); 312 if (!$cnt) $this->Execute(sprintf($this->_genSeq2SQL,$seqname,$startID-1)); 313 $rs = $this->Execute($getnext); 314 } 315 316 if ($rs) { 317 $this->genID = mysql_insert_id($this->_connectionID); 318 $rs->Close(); 319 } else 320 $this->genID = 0; 321 322 $this->_logsql = $savelog; 323 return $this->genID; 324 } 325 326 function MetaDatabases() 327 { 328 $qid = mysql_list_dbs($this->_connectionID); 329 $arr = array(); 330 $i = 0; 331 $max = mysql_num_rows($qid); 332 while ($i < $max) { 333 $db = mysql_tablename($qid,$i); 334 if ($db != 'mysql') $arr[] = $db; 335 $i += 1; 336 } 337 return $arr; 338 } 339 340 341 // Format date column in sql string given an input format that understands Y M D 342 function SQLDate($fmt, $col=false) 343 { 344 if (!$col) $col = $this->sysTimeStamp; 345 $s = 'DATE_FORMAT('.$col.",'"; 346 $concat = false; 347 $len = strlen($fmt); 348 for ($i=0; $i < $len; $i++) { 349 $ch = $fmt[$i]; 350 switch($ch) { 351 352 default: 353 if ($ch == '\\') { 354 $i++; 355 $ch = substr($fmt,$i,1); 356 } 357 /** FALL THROUGH */ 358 case '-': 359 case '/': 360 $s .= $ch; 361 break; 362 363 case 'Y': 364 case 'y': 365 $s .= '%Y'; 366 break; 367 case 'M': 368 $s .= '%b'; 369 break; 370 371 case 'm': 372 $s .= '%m'; 373 break; 374 case 'D': 375 case 'd': 376 $s .= '%d'; 377 break; 378 379 case 'Q': 380 case 'q': 381 $s .= "'),Quarter($col)"; 382 383 if ($len > $i+1) $s .= ",DATE_FORMAT($col,'"; 384 else $s .= ",('"; 385 $concat = true; 386 break; 387 388 case 'H': 389 $s .= '%H'; 390 break; 391 392 case 'h': 393 $s .= '%I'; 394 break; 395 396 case 'i': 397 $s .= '%i'; 398 break; 399 400 case 's': 401 $s .= '%s'; 402 break; 403 404 case 'a': 405 case 'A': 406 $s .= '%p'; 407 break; 408 409 case 'w': 410 $s .= '%w'; 411 break; 412 413 case 'W': 414 $s .= '%U'; 415 break; 416 417 case 'l': 418 $s .= '%W'; 419 break; 420 } 421 } 422 $s.="')"; 423 if ($concat) $s = "CONCAT($s)"; 424 return $s; 425 } 426 427 428 // returns concatenated string 429 // much easier to run "mysqld --ansi" or "mysqld --sql-mode=PIPES_AS_CONCAT" and use || operator 430 function Concat() 431 { 432 $s = ""; 433 $arr = func_get_args(); 434 435 // suggestion by [email protected] 436 $s = implode(',',$arr); 437 if (strlen($s) > 0) return "CONCAT($s)"; 438 else return ''; 439 } 440 441 function OffsetDate($dayFraction,$date=false) 442 { 443 if (!$date) $date = $this->sysDate; 444 445 $fraction = $dayFraction * 24 * 3600; 446 return '('. $date . ' + INTERVAL ' . $fraction.' SECOND)'; 447 448 // return "from_unixtime(unix_timestamp($date)+$fraction)"; 449 } 450 451 // returns true or false 452 function _connect($argHostname, $argUsername, $argPassword, $argDatabasename) 453 { 454 if (!empty($this->port)) $argHostname .= ":".$this->port; 455 456 if (ADODB_PHPVER >= 0x4300) 457 $this->_connectionID = mysql_connect($argHostname,$argUsername,$argPassword, 458 $this->forceNewConnect,$this->clientFlags); 459 else if (ADODB_PHPVER >= 0x4200) 460 $this->_connectionID = mysql_connect($argHostname,$argUsername,$argPassword, 461 $this->forceNewConnect); 462 else 463 $this->_connectionID = mysql_connect($argHostname,$argUsername,$argPassword); 464 465 if ($this->_connectionID === false) return false; 466 if ($argDatabasename) return $this->SelectDB($argDatabasename); 467 return true; 468 } 469 470 // returns true or false 471 function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename) 472 { 473 if (!empty($this->port)) $argHostname .= ":".$this->port; 474 475 if (ADODB_PHPVER >= 0x4300) 476 $this->_connectionID = mysql_pconnect($argHostname,$argUsername,$argPassword,$this->clientFlags); 477 else 478 $this->_connectionID = mysql_pconnect($argHostname,$argUsername,$argPassword); 479 if ($this->_connectionID === false) return false; 480 if ($this->autoRollback) $this->RollbackTrans(); 481 if ($argDatabasename) return $this->SelectDB($argDatabasename); 482 return true; 483 } 484 485 function _nconnect($argHostname, $argUsername, $argPassword, $argDatabasename) 486 { 487 $this->forceNewConnect = true; 488 return $this->_connect($argHostname, $argUsername, $argPassword, $argDatabasename); 489 } 490 491 function MetaColumns($table, $normalize=true) 492 { 493 $this->_findschema($table,$schema); 494 if ($schema) { 495 $dbName = $this->database; 496 $this->SelectDB($schema); 497 } 498 global $ADODB_FETCH_MODE; 499 $save = $ADODB_FETCH_MODE; 500 $ADODB_FETCH_MODE = ADODB_FETCH_NUM; 501 502 if ($this->fetchMode !== false) $savem = $this->SetFetchMode(false); 503 $rs = $this->Execute(sprintf($this->metaColumnsSQL,$table)); 504 505 if ($schema) { 506 $this->SelectDB($dbName); 507 } 508 509 if (isset($savem)) $this->SetFetchMode($savem); 510 $ADODB_FETCH_MODE = $save; 511 if (!is_object($rs)) { 512 $false = false; 513 return $false; 514 } 515 516 $retarr = array(); 517 while (!$rs->EOF){ 518 $fld = new ADOFieldObject(); 519 $fld->name = $rs->fields[0]; 520 $type = $rs->fields[1]; 521 522 // split type into type(length): 523 $fld->scale = null; 524 if (preg_match("/^(.+)\((\d+),(\d+)/", $type, $query_array)) { 525 $fld->type = $query_array[1]; 526 $fld->max_length = is_numeric($query_array[2]) ? $query_array[2] : -1; 527 $fld->scale = is_numeric($query_array[3]) ? $query_array[3] : -1; 528 } elseif (preg_match("/^(.+)\((\d+)/", $type, $query_array)) { 529 $fld->type = $query_array[1]; 530 $fld->max_length = is_numeric($query_array[2]) ? $query_array[2] : -1; 531 } elseif (preg_match("/^(enum)\((.*)\)$/i", $type, $query_array)) { 532 $fld->type = $query_array[1]; 533 $arr = explode(",",$query_array[2]); 534 $fld->enums = $arr; 535 $zlen = max(array_map("strlen",$arr)) - 2; // PHP >= 4.0.6 536 $fld->max_length = ($zlen > 0) ? $zlen : 1; 537 } else { 538 $fld->type = $type; 539 $fld->max_length = -1; 540 } 541 $fld->not_null = ($rs->fields[2] != 'YES'); 542 $fld->primary_key = ($rs->fields[3] == 'PRI'); 543 $fld->auto_increment = (strpos($rs->fields[5], 'auto_increment') !== false); 544 $fld->binary = (strpos($type,'blob') !== false || strpos($type,'binary') !== false); 545 $fld->unsigned = (strpos($type,'unsigned') !== false); 546 $fld->zerofill = (strpos($type,'zerofill') !== false); 547 548 if (!$fld->binary) { 549 $d = $rs->fields[4]; 550 if ($d != '' && $d != 'NULL') { 551 $fld->has_default = true; 552 $fld->default_value = $d; 553 } else { 554 $fld->has_default = false; 555 } 556 } 557 558 if ($save == ADODB_FETCH_NUM) { 559 $retarr[] = $fld; 560 } else { 561 $retarr[strtoupper($fld->name)] = $fld; 562 } 563 $rs->MoveNext(); 564 } 565 566 $rs->Close(); 567 return $retarr; 568 } 569 570 // returns true or false 571 function SelectDB($dbName) 572 { 573 $this->database = $dbName; 574 $this->databaseName = $dbName; # obsolete, retained for compat with older adodb versions 575 if ($this->_connectionID) { 576 return @mysql_select_db($dbName,$this->_connectionID); 577 } 578 else return false; 579 } 580 581 // parameters use PostgreSQL convention, not MySQL 582 function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs=0) 583 { 584 $offsetStr =($offset>=0) ? ((integer)$offset)."," : ''; 585 // jason judge, see http://phplens.com/lens/lensforum/msgs.php?id=9220 586 if ($nrows < 0) $nrows = '18446744073709551615'; 587 588 if ($secs) 589 $rs = $this->CacheExecute($secs,$sql." LIMIT $offsetStr".((integer)$nrows),$inputarr); 590 else 591 $rs = $this->Execute($sql." LIMIT $offsetStr".((integer)$nrows),$inputarr); 592 return $rs; 593 } 594 595 // returns queryID or false 596 function _query($sql,$inputarr=false) 597 { 598 599 return mysql_query($sql,$this->_connectionID); 600 /* 601 global $ADODB_COUNTRECS; 602 if($ADODB_COUNTRECS) 603 return mysql_query($sql,$this->_connectionID); 604 else 605 return @mysql_unbuffered_query($sql,$this->_connectionID); // requires PHP >= 4.0.6 606 */ 607 } 608 609 /* Returns: the last error message from previous database operation */ 610 function ErrorMsg() 611 { 612 613 if ($this->_logsql) return $this->_errorMsg; 614 if (empty($this->_connectionID)) $this->_errorMsg = @mysql_error(); 615 else $this->_errorMsg = @mysql_error($this->_connectionID); 616 return $this->_errorMsg; 617 } 618 619 /* Returns: the last error number from previous database operation */ 620 function ErrorNo() 621 { 622 if ($this->_logsql) return $this->_errorCode; 623 if (empty($this->_connectionID)) return @mysql_errno(); 624 else return @mysql_errno($this->_connectionID); 625 } 626 627 // returns true or false 628 function _close() 629 { 630 @mysql_close($this->_connectionID); 631 632 $this->charSet = ''; 633 $this->_connectionID = false; 634 } 635 636 637 /* 638 * Maximum size of C field 639 */ 640 function CharMax() 641 { 642 return 255; 643 } 644 645 /* 646 * Maximum size of X field 647 */ 648 function TextMax() 649 { 650 return 4294967295; 651 } 652 653 // "Innox - Juan Carlos Gonzalez" <jgonzalez#innox.com.mx> 654 function MetaForeignKeys( $table, $owner = FALSE, $upper = FALSE, $associative = FALSE ) 655 { 656 global $ADODB_FETCH_MODE; 657 if ($ADODB_FETCH_MODE == ADODB_FETCH_ASSOC || $this->fetchMode == ADODB_FETCH_ASSOC) $associative = true; 658 659 if ( !empty($owner) ) { 660 $table = "$owner.$table"; 661 } 662 $a_create_table = $this->getRow(sprintf('SHOW CREATE TABLE %s', $table)); 663 if ($associative) { 664 $create_sql = isset($a_create_table["Create Table"]) ? $a_create_table["Create Table"] : $a_create_table["Create View"]; 665 } else $create_sql = $a_create_table[1]; 666 667 $matches = array(); 668 669 if (!preg_match_all("/FOREIGN KEY \(`(.*?)`\) REFERENCES `(.*?)` \(`(.*?)`\)/", $create_sql, $matches)) return false; 670 $foreign_keys = array(); 671 $num_keys = count($matches[0]); 672 for ( $i = 0; $i < $num_keys; $i ++ ) { 673 $my_field = explode('`, `', $matches[1][$i]); 674 $ref_table = $matches[2][$i]; 675 $ref_field = explode('`, `', $matches[3][$i]); 676 677 if ( $upper ) { 678 $ref_table = strtoupper($ref_table); 679 } 680 681 // see https://sourceforge.net/tracker/index.php?func=detail&aid=2287278&group_id=42718&atid=433976 682 if (!isset($foreign_keys[$ref_table])) { 683 $foreign_keys[$ref_table] = array(); 684 } 685 $num_fields = count($my_field); 686 for ( $j = 0; $j < $num_fields; $j ++ ) { 687 if ( $associative ) { 688 $foreign_keys[$ref_table][$ref_field[$j]] = $my_field[$j]; 689 } else { 690 $foreign_keys[$ref_table][] = "{$my_field[$j]}={$ref_field[$j]}"; 691 } 692 } 693 } 694 695 return $foreign_keys; 696 } 697 698 699 } 700 701 /*-------------------------------------------------------------------------------------- 702 Class Name: Recordset 703 --------------------------------------------------------------------------------------*/ 704 705 706 class ADORecordSet_mysql extends ADORecordSet{ 707 708 var $databaseType = "mysql"; 709 var $canSeek = true; 710 711 function ADORecordSet_mysql($queryID,$mode=false) 712 { 713 if ($mode === false) { 714 global $ADODB_FETCH_MODE; 715 $mode = $ADODB_FETCH_MODE; 716 } 717 switch ($mode) 718 { 719 case ADODB_FETCH_NUM: $this->fetchMode = MYSQL_NUM; break; 720 case ADODB_FETCH_ASSOC:$this->fetchMode = MYSQL_ASSOC; break; 721 case ADODB_FETCH_DEFAULT: 722 case ADODB_FETCH_BOTH: 723 default: 724 $this->fetchMode = MYSQL_BOTH; break; 725 } 726 $this->adodbFetchMode = $mode; 727 $this->ADORecordSet($queryID); 728 } 729 730 function _initrs() 731 { 732 //GLOBAL $ADODB_COUNTRECS; 733 // $this->_numOfRows = ($ADODB_COUNTRECS) ? @mysql_num_rows($this->_queryID):-1; 734 $this->_numOfRows = @mysql_num_rows($this->_queryID); 735 $this->_numOfFields = @mysql_num_fields($this->_queryID); 736 } 737 738 function FetchField($fieldOffset = -1) 739 { 740 if ($fieldOffset != -1) { 741 $o = @mysql_fetch_field($this->_queryID, $fieldOffset); 742 $f = @mysql_field_flags($this->_queryID,$fieldOffset); 743 if ($o) $o->max_length = @mysql_field_len($this->_queryID,$fieldOffset); // suggested by: Jim Nicholson (jnich#att.com) 744 //$o->max_length = -1; // mysql returns the max length less spaces -- so it is unrealiable 745 if ($o) $o->binary = (strpos($f,'binary')!== false); 746 } 747 else { /* The $fieldOffset argument is not provided thus its -1 */ 748 $o = @mysql_fetch_field($this->_queryID); 749 //if ($o) $o->max_length = @mysql_field_len($this->_queryID); // suggested by: Jim Nicholson (jnich#att.com) 750 $o->max_length = -1; // mysql returns the max length less spaces -- so it is unrealiable 751 } 752 753 return $o; 754 } 755 756 function GetRowAssoc($upper=true) 757 { 758 if ($this->fetchMode == MYSQL_ASSOC && !$upper) $row = $this->fields; 759 else $row = ADORecordSet::GetRowAssoc($upper); 760 return $row; 761 } 762 763 /* Use associative array to get fields array */ 764 function Fields($colname) 765 { 766 // added @ by "Michael William Miller" <[email protected]> 767 if ($this->fetchMode != MYSQL_NUM) return @$this->fields[$colname]; 768 769 if (!$this->bind) { 770 $this->bind = array(); 771 for ($i=0; $i < $this->_numOfFields; $i++) { 772 $o = $this->FetchField($i); 773 $this->bind[strtoupper($o->name)] = $i; 774 } 775 } 776 return $this->fields[$this->bind[strtoupper($colname)]]; 777 } 778 779 function _seek($row) 780 { 781 if ($this->_numOfRows == 0) return false; 782 return @mysql_data_seek($this->_queryID,$row); 783 } 784 785 function MoveNext() 786 { 787 //return adodb_movenext($this); 788 //if (defined('ADODB_EXTENSION')) return adodb_movenext($this); 789 if (@$this->fields = mysql_fetch_array($this->_queryID,$this->fetchMode)) { 790 $this->_currentRow += 1; 791 return true; 792 } 793 if (!$this->EOF) { 794 $this->_currentRow += 1; 795 $this->EOF = true; 796 } 797 return false; 798 } 799 800 function _fetch() 801 { 802 $this->fields = @mysql_fetch_array($this->_queryID,$this->fetchMode); 803 return is_array($this->fields); 804 } 805 806 function _close() { 807 @mysql_free_result($this->_queryID); 808 $this->_queryID = false; 809 } 810 811 function MetaType($t,$len=-1,$fieldobj=false) 812 { 813 if (is_object($t)) { 814 $fieldobj = $t; 815 $t = $fieldobj->type; 816 $len = $fieldobj->max_length; 817 } 818 819 $len = -1; // mysql max_length is not accurate 820 switch (strtoupper($t)) { 821 case 'STRING': 822 case 'CHAR': 823 case 'VARCHAR': 824 case 'TINYBLOB': 825 case 'TINYTEXT': 826 case 'ENUM': 827 case 'SET': 828 if ($len <= $this->blobSize) return 'C'; 829 830 case 'TEXT': 831 case 'LONGTEXT': 832 case 'MEDIUMTEXT': 833 return 'X'; 834 835 // php_mysql extension always returns 'blob' even if 'text' 836 // so we have to check whether binary... 837 case 'IMAGE': 838 case 'LONGBLOB': 839 case 'BLOB': 840 case 'MEDIUMBLOB': 841 case 'BINARY': 842 return !empty($fieldobj->binary) ? 'B' : 'X'; 843 844 case 'YEAR': 845 case 'DATE': return 'D'; 846 847 case 'TIME': 848 case 'DATETIME': 849 case 'TIMESTAMP': return 'T'; 850 851 case 'INT': 852 case 'INTEGER': 853 case 'BIGINT': 854 case 'TINYINT': 855 case 'MEDIUMINT': 856 case 'SMALLINT': 857 858 if (!empty($fieldobj->primary_key)) return 'R'; 859 else return 'I'; 860 861 default: return 'N'; 862 } 863 } 864 865 } 866 867 class ADORecordSet_ext_mysql extends ADORecordSet_mysql { 868 function ADORecordSet_ext_mysql($queryID,$mode=false) 869 { 870 if ($mode === false) { 871 global $ADODB_FETCH_MODE; 872 $mode = $ADODB_FETCH_MODE; 873 } 874 switch ($mode) 875 { 876 case ADODB_FETCH_NUM: $this->fetchMode = MYSQL_NUM; break; 877 case ADODB_FETCH_ASSOC:$this->fetchMode = MYSQL_ASSOC; break; 878 case ADODB_FETCH_DEFAULT: 879 case ADODB_FETCH_BOTH: 880 default: 881 $this->fetchMode = MYSQL_BOTH; break; 882 } 883 $this->adodbFetchMode = $mode; 884 $this->ADORecordSet($queryID); 885 } 886 887 function MoveNext() 888 { 889 return @adodb_movenext($this); 890 } 891 } 892 893 894 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |