home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / adodb / drivers / adodb-mysql.inc.php < prev    next >
Encoding:
PHP Script  |  2008-02-13  |  20.5 KB  |  790 lines

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