egroupware/phpgwapi/inc/adodb/drivers/adodb-mysql.inc.php

826 lines
21 KiB
PHP
Raw Normal View History

2003-10-19 21:05:23 +02:00
<?php
/*
2005-09-26 12:12:10 +02:00
V4.65 22 July 2005 (c) 2000-2005 John Lim (jlim@natsoft.com.my). All rights reserved.
Released under both BSD license and Lesser GPL library license.
Whenever there is any discrepancy between the two licenses,
2003-10-19 21:05:23 +02:00
the BSD license will take precedence.
Set tabs to 8.
2003-10-19 21:05:23 +02:00
MySQL code that does not support transactions. Use mysqlt if you need transactions.
Requires mysql client. Works on Windows and Unix.
2003-10-19 21:05:23 +02:00
28 Feb 2001: MetaColumns bug fix - suggested by Freek Dijkstra (phpeverywhere@macfreek.com)
*/
2003-10-19 21:05:23 +02:00
// security - hide paths
if (!defined('ADODB_DIR')) die();
2003-10-19 21:05:23 +02:00
if (! defined("_ADODB_MYSQL_LAYER")) {
define("_ADODB_MYSQL_LAYER", 1 );
class ADODB_mysql extends ADOConnection {
var $databaseType = 'mysql';
var $dataProvider = 'mysql';
var $hasInsertID = true;
var $hasAffectedRows = true;
var $metaTablesSQL = "SHOW TABLES";
2003-10-19 21:05:23 +02:00
var $metaColumnsSQL = "SHOW COLUMNS FROM %s";
var $fmtTimeStamp = "'Y-m-d H:i:s'";
var $hasLimit = true;
var $hasMoveFirst = true;
var $hasGenID = true;
var $isoDates = true; // accepts dates in ISO format
var $sysDate = 'CURDATE()';
var $sysTimeStamp = 'NOW()';
var $hasTransactions = false;
var $forceNewConnect = false;
var $poorAffectedRows = true;
var $clientFlags = 0;
var $substr = "substring";
var $nameQuote = '`'; /// string to use to quote identifiers and names
function ADODB_mysql()
{
2004-08-02 10:30:47 +02:00
if (defined('ADODB_EXTENSION')) $this->rsPrefix .= 'ext_';
2003-10-19 21:05:23 +02:00
}
2003-10-19 21:05:23 +02:00
function ServerInfo()
{
$arr['description'] = ADOConnection::GetOne("select version()");
2003-10-19 21:05:23 +02:00
$arr['version'] = ADOConnection::_findvers($arr['description']);
return $arr;
}
function IfNull( $field, $ifNull )
2003-10-19 21:05:23 +02:00
{
return " IFNULL($field, $ifNull) "; // if MySQL
}
function &MetaTables($ttype=false,$showSchema=false,$mask=false)
{
$save = $this->metaTablesSQL;
if ($showSchema && is_string($showSchema)) {
$this->metaTablesSQL .= " from $showSchema";
}
2003-10-19 21:05:23 +02:00
if ($mask) {
$mask = $this->qstr($mask);
$this->metaTablesSQL .= " like $mask";
}
$ret =& ADOConnection::MetaTables($ttype,$showSchema);
$this->metaTablesSQL = $save;
2003-10-19 21:05:23 +02:00
return $ret;
}
function &MetaIndexes ($table, $primary = FALSE, $owner=false)
{
// save old fetch mode
global $ADODB_FETCH_MODE;
2005-09-26 12:12:10 +02:00
$false = false;
$save = $ADODB_FETCH_MODE;
$ADODB_FETCH_MODE = ADODB_FETCH_NUM;
if ($this->fetchMode !== FALSE) {
$savem = $this->SetFetchMode(FALSE);
}
// get index details
$rs = $this->Execute(sprintf('SHOW INDEX FROM %s',$table));
// restore fetchmode
if (isset($savem)) {
$this->SetFetchMode($savem);
}
$ADODB_FETCH_MODE = $save;
if (!is_object($rs)) {
2005-09-26 12:12:10 +02:00
return $false;
}
$indexes = array ();
// parse index data into array
while ($row = $rs->FetchRow()) {
if ($primary == FALSE AND $row[2] == 'PRIMARY') {
continue;
}
if (!isset($indexes[$row[2]])) {
$indexes[$row[2]] = array(
'unique' => ($row[1] == 0),
'columns' => array()
);
}
$indexes[$row[2]]['columns'][$row[3] - 1] = $row[4];
}
// sort columns by order in the index
foreach ( array_keys ($indexes) as $index )
{
ksort ($indexes[$index]['columns']);
}
return $indexes;
}
2003-10-19 21:05:23 +02:00
// if magic quotes disabled, use mysql_real_escape_string()
function qstr($s,$magic_quotes=false)
{
if (!$magic_quotes) {
2003-10-19 21:05:23 +02:00
if (ADODB_PHPVER >= 0x4300) {
if (is_resource($this->_connectionID))
return "'".mysql_real_escape_string($s,$this->_connectionID)."'";
}
if ($this->replaceQuote[0] == '\\'){
$s = adodb_str_replace(array('\\',"\0"),array('\\\\',"\\\0"),$s);
}
return "'".str_replace("'",$this->replaceQuote,$s)."'";
2003-10-19 21:05:23 +02:00
}
2003-10-19 21:05:23 +02:00
// undo magic quotes for "
$s = str_replace('\\"','"',$s);
return "'$s'";
}
2003-10-19 21:05:23 +02:00
function _insertid()
{
2005-09-26 12:12:10 +02:00
return ADOConnection::GetOne('SELECT LAST_INSERT_ID()');
//return mysql_insert_id($this->_connectionID);
2003-10-19 21:05:23 +02:00
}
2003-10-19 21:05:23 +02:00
function GetOne($sql,$inputarr=false)
{
if (strncasecmp($sql,'sele',4) == 0) {
$rs =& $this->SelectLimit($sql,1,-1,$inputarr);
if ($rs) {
$rs->Close();
if ($rs->EOF) return false;
return reset($rs->fields);
}
} else {
return ADOConnection::GetOne($sql,$inputarr);
2003-10-19 21:05:23 +02:00
}
return false;
}
2004-08-02 10:30:47 +02:00
function BeginTrans()
{
if ($this->debug) ADOConnection::outp("Transactions not supported in 'mysql' driver. Use 'mysqlt' or 'mysqli' driver");
}
2003-10-19 21:05:23 +02:00
function _affectedrows()
{
return mysql_affected_rows($this->_connectionID);
}
2003-10-19 21:05:23 +02:00
// See http://www.mysql.com/doc/M/i/Miscellaneous_functions.html
// Reference on Last_Insert_ID on the recommended way to simulate sequences
var $_genIDSQL = "update %s set id=LAST_INSERT_ID(id+1);";
var $_genSeqSQL = "create table %s (id int not null)";
var $_genSeq2SQL = "insert into %s values (%s)";
var $_dropSeqSQL = "drop table %s";
2003-10-19 21:05:23 +02:00
function CreateSequence($seqname='adodbseq',$startID=1)
{
if (empty($this->_genSeqSQL)) return false;
$u = strtoupper($seqname);
2003-10-19 21:05:23 +02:00
$ok = $this->Execute(sprintf($this->_genSeqSQL,$seqname));
if (!$ok) return false;
return $this->Execute(sprintf($this->_genSeq2SQL,$seqname,$startID-1));
}
2003-10-19 21:05:23 +02:00
function GenID($seqname='adodbseq',$startID=1)
{
// post-nuke sets hasGenID to false
if (!$this->hasGenID) return false;
$savelog = $this->_logsql;
$this->_logsql = false;
2003-10-19 21:05:23 +02:00
$getnext = sprintf($this->_genIDSQL,$seqname);
$holdtransOK = $this->_transOK; // save the current status
2003-10-19 21:05:23 +02:00
$rs = @$this->Execute($getnext);
if (!$rs) {
if ($holdtransOK) $this->_transOK = true; //if the status was ok before reset
2003-10-19 21:05:23 +02:00
$u = strtoupper($seqname);
$this->Execute(sprintf($this->_genSeqSQL,$seqname));
$this->Execute(sprintf($this->_genSeq2SQL,$seqname,$startID-1));
$rs = $this->Execute($getnext);
}
$this->genID = mysql_insert_id($this->_connectionID);
2003-10-19 21:05:23 +02:00
if ($rs) $rs->Close();
$this->_logsql = $savelog;
2003-10-19 21:05:23 +02:00
return $this->genID;
}
2003-10-19 21:05:23 +02:00
function &MetaDatabases()
{
$qid = mysql_list_dbs($this->_connectionID);
$arr = array();
$i = 0;
$max = mysql_num_rows($qid);
while ($i < $max) {
$db = mysql_tablename($qid,$i);
if ($db != 'mysql') $arr[] = $db;
$i += 1;
}
return $arr;
}
2003-10-19 21:05:23 +02:00
// Format date column in sql string given an input format that understands Y M D
function SQLDate($fmt, $col=false)
{
2003-10-19 21:05:23 +02:00
if (!$col) $col = $this->sysTimeStamp;
$s = 'DATE_FORMAT('.$col.",'";
$concat = false;
$len = strlen($fmt);
for ($i=0; $i < $len; $i++) {
$ch = $fmt[$i];
switch($ch) {
2004-08-02 10:30:47 +02:00
default:
if ($ch == '\\') {
$i++;
$ch = substr($fmt,$i,1);
}
/** FALL THROUGH */
case '-':
case '/':
$s .= $ch;
break;
2003-10-19 21:05:23 +02:00
case 'Y':
case 'y':
$s .= '%Y';
break;
case 'M':
$s .= '%b';
break;
2003-10-19 21:05:23 +02:00
case 'm':
$s .= '%m';
break;
case 'D':
case 'd':
$s .= '%d';
break;
2004-08-02 10:30:47 +02:00
case 'Q':
case 'q':
$s .= "'),Quarter($col)";
2004-08-02 10:30:47 +02:00
if ($len > $i+1) $s .= ",DATE_FORMAT($col,'";
else $s .= ",('";
$concat = true;
break;
case 'H':
2003-10-19 21:05:23 +02:00
$s .= '%H';
break;
2003-10-19 21:05:23 +02:00
case 'h':
$s .= '%I';
break;
2003-10-19 21:05:23 +02:00
case 'i':
$s .= '%i';
break;
2003-10-19 21:05:23 +02:00
case 's':
$s .= '%s';
break;
2003-10-19 21:05:23 +02:00
case 'a':
case 'A':
$s .= '%p';
break;
2005-09-26 12:12:10 +02:00
case 'w':
$s .= '%w';
break;
2005-09-26 12:12:10 +02:00
case 'l':
$s .= '%W';
break;
2003-10-19 21:05:23 +02:00
}
}
$s.="')";
if ($concat) $s = "CONCAT($s)";
return $s;
}
2003-10-19 21:05:23 +02:00
// returns concatenated string
// much easier to run "mysqld --ansi" or "mysqld --sql-mode=PIPES_AS_CONCAT" and use || operator
function Concat()
{
$s = "";
$arr = func_get_args();
2003-10-19 21:05:23 +02:00
// suggestion by andrew005@mnogo.ru
$s = implode(',',$arr);
2003-10-19 21:05:23 +02:00
if (strlen($s) > 0) return "CONCAT($s)";
else return '';
}
2003-10-19 21:05:23 +02:00
function OffsetDate($dayFraction,$date=false)
{
2003-10-19 21:05:23 +02:00
if (!$date) $date = $this->sysDate;
return "from_unixtime(unix_timestamp($date)+($dayFraction)*24*3600)";
}
2003-10-19 21:05:23 +02:00
// returns true or false
function _connect($argHostname, $argUsername, $argPassword, $argDatabasename)
{
2005-09-26 12:12:10 +02:00
if (!empty($this->port)) $argHostname .= ":".$this->port;
2003-10-19 21:05:23 +02:00
if (ADODB_PHPVER >= 0x4300)
$this->_connectionID = mysql_connect($argHostname,$argUsername,$argPassword,
$this->forceNewConnect,$this->clientFlags);
else if (ADODB_PHPVER >= 0x4200)
$this->_connectionID = mysql_connect($argHostname,$argUsername,$argPassword,
$this->forceNewConnect);
else
$this->_connectionID = mysql_connect($argHostname,$argUsername,$argPassword);
2003-10-19 21:05:23 +02:00
if ($this->_connectionID === false) return false;
if ($argDatabasename) return $this->SelectDB($argDatabasename);
return true;
2003-10-19 21:05:23 +02:00
}
2003-10-19 21:05:23 +02:00
// returns true or false
function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
{
2005-09-26 12:12:10 +02:00
if (!empty($this->port)) $argHostname .= ":".$this->port;
2003-10-19 21:05:23 +02:00
if (ADODB_PHPVER >= 0x4300)
$this->_connectionID = mysql_pconnect($argHostname,$argUsername,$argPassword,$this->clientFlags);
else
$this->_connectionID = mysql_pconnect($argHostname,$argUsername,$argPassword);
if ($this->_connectionID === false) return false;
if ($this->autoRollback) $this->RollbackTrans();
if ($argDatabasename) return $this->SelectDB($argDatabasename);
return true;
2003-10-19 21:05:23 +02:00
}
2003-10-19 21:05:23 +02:00
function _nconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
{
$this->forceNewConnect = true;
return $this->_connect($argHostname, $argUsername, $argPassword, $argDatabasename);
}
function &MetaColumns($table,$upper=true)
2003-10-19 21:05:23 +02:00
{
2005-09-26 12:12:10 +02:00
$this->_findschema($table,$schema);
if ($schema) {
$dbName = $this->database;
$this->SelectDB($schema);
}
2003-10-19 21:05:23 +02:00
global $ADODB_FETCH_MODE;
2005-09-26 12:12:10 +02:00
$save = $ADODB_FETCH_MODE;
$ADODB_FETCH_MODE = ADODB_FETCH_NUM;
2005-09-26 12:12:10 +02:00
if ($this->fetchMode !== false) $savem = $this->SetFetchMode(false);
$rs = $this->Execute(sprintf($this->metaColumnsSQL,$table));
2005-09-26 12:12:10 +02:00
if ($schema) {
$this->SelectDB($dbName);
}
2005-09-26 12:12:10 +02:00
if (isset($savem)) $this->SetFetchMode($savem);
$ADODB_FETCH_MODE = $save;
if (!is_object($rs)) {
$false = false;
return $false;
}
2005-09-26 12:12:10 +02:00
$retarr = array();
while (!$rs->EOF){
$fld = new ADOFieldObject();
$fld->name = $rs->fields[0];
$type = $rs->fields[1];
2005-09-26 12:12:10 +02:00
// split type into type(length):
$fld->scale = null;
if (preg_match("/^(.+)\((\d+),(\d+)/", $type, $query_array)) {
$fld->type = $query_array[1];
$fld->max_length = is_numeric($query_array[2]) ? $query_array[2] : -1;
$fld->scale = is_numeric($query_array[3]) ? $query_array[3] : -1;
} elseif (preg_match("/^(.+)\((\d+)/", $type, $query_array)) {
$fld->type = $query_array[1];
$fld->max_length = is_numeric($query_array[2]) ? $query_array[2] : -1;
} elseif (preg_match("/^(enum)\((.*)\)$/i", $type, $query_array)) {
$fld->type = $query_array[1];
$arr = explode(",",$query_array[2]);
$fld->enums = $arr;
$zlen = max(array_map("strlen",$arr)) - 2; // PHP >= 4.0.6
$fld->max_length = ($zlen > 0) ? $zlen : 1;
} else {
$fld->type = $type;
$fld->max_length = -1;
}
$fld->not_null = ($rs->fields[2] != 'YES');
$fld->primary_key = ($rs->fields[3] == 'PRI');
$fld->auto_increment = (strpos($rs->fields[5], 'auto_increment') !== false);
$fld->binary = (strpos($type,'blob') !== false);
$fld->unsigned = (strpos($type,'unsigned') !== false);
2005-09-26 12:12:10 +02:00
if (!$fld->binary) {
$d = $rs->fields[4];
if ($d != '' && $d != 'NULL') {
$fld->has_default = true;
$fld->default_value = $d;
} else {
2005-09-26 12:12:10 +02:00
$fld->has_default = false;
2003-10-19 21:05:23 +02:00
}
2005-09-26 12:12:10 +02:00
}
2005-09-26 12:12:10 +02:00
if ($save == ADODB_FETCH_NUM) {
$retarr[] = $fld;
} else {
$retarr[strtoupper($fld->name)] = $fld;
}
2003-10-19 21:05:23 +02:00
$rs->MoveNext();
}
2003-10-19 21:05:23 +02:00
$rs->Close();
return $retarr;
2003-10-19 21:05:23 +02:00
}
2003-10-19 21:05:23 +02:00
// returns true or false
function SelectDB($dbName)
2003-10-19 21:05:23 +02:00
{
2005-09-26 12:12:10 +02:00
$this->database = $dbName;
2003-10-19 21:05:23 +02:00
if ($this->_connectionID) {
return @mysql_select_db($dbName,$this->_connectionID);
2003-10-19 21:05:23 +02:00
}
else return false;
2003-10-19 21:05:23 +02:00
}
2003-10-19 21:05:23 +02:00
// parameters use PostgreSQL convention, not MySQL
function &SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs=0)
{
// jason judge, see http://phplens.com/lens/lensforum/msgs.php?id=9220
if ($nrows < 0) $nrows = '18446744073709551615';
$offsetStr = (($offset>=0) ? (int)$offset.',' : '').(int)$nrows;
//if the sql ends by a 'for update' it should be AFTER the LIMIT
$FORUPDATE = '';
//with PHP5 we could have used stripos and str_ireplace
if ( (strtoupper(substr($sql,-10))) == 'FOR UPDATE') {
$sql = substr($sql,0,(strlen($sql)-10));
$FORUPDATE = 'for update';
} elseif ( (strtoupper(substr($sql,-11))) == 'FOR UPDATE;') {
$sql = substr($sql,0,(strlen($sql)-11));
$FORUPDATE = 'for update';
}
if ($secs)
$rs =& $this->CacheExecute($secs,$sql.' LIMIT '.$offsetStr.' '.$FORUPDATE,$inputarr);
else
$rs =& $this->Execute($sql.' LIMIT '.$offsetStr.' '.$FORUPDATE,$inputarr);
return $rs;
2003-10-19 21:05:23 +02:00
}
2003-10-19 21:05:23 +02:00
// returns queryID or false
function _query($sql,$inputarr)
{
//global $ADODB_COUNTRECS;
//if($ADODB_COUNTRECS)
2003-10-19 21:05:23 +02:00
return mysql_query($sql,$this->_connectionID);
//else return @mysql_unbuffered_query($sql,$this->_connectionID); // requires PHP >= 4.0.6
}
/* Returns: the last error message from previous database operation */
function ErrorMsg()
2003-10-19 21:05:23 +02:00
{
2003-10-19 21:05:23 +02:00
if ($this->_logsql) return $this->_errorMsg;
if (empty($this->_connectionID)) $this->_errorMsg = @mysql_error();
else $this->_errorMsg = @mysql_error($this->_connectionID);
return $this->_errorMsg;
}
/* Returns: the last error number from previous database operation */
function ErrorNo()
2003-10-19 21:05:23 +02:00
{
if ($this->_logsql) return $this->_errorCode;
if (empty($this->_connectionID)) return @mysql_errno();
else return @mysql_errno($this->_connectionID);
}
2003-10-19 21:05:23 +02:00
// returns true or false
function _close()
{
@mysql_close($this->_connectionID);
$this->_connectionID = false;
}
2003-10-19 21:05:23 +02:00
/*
* Maximum size of C field
*/
function CharMax()
{
return 255;
2003-10-19 21:05:23 +02:00
}
2003-10-19 21:05:23 +02:00
/*
* Maximum size of X field
*/
function TextMax()
{
return 4294967295;
2003-10-19 21:05:23 +02:00
}
2005-09-26 12:12:10 +02:00
// "Innox - Juan Carlos Gonzalez" <jgonzalez#innox.com.mx>
function MetaForeignKeys( $table, $owner = FALSE, $upper = FALSE, $asociative = FALSE )
{
if ( !empty($owner) ) {
$table = "$owner.$table";
}
$a_create_table = $this->getRow(sprintf('SHOW CREATE TABLE %s', $table));
$create_sql = $a_create_table[1];
$matches = array();
$foreign_keys = array();
if ( preg_match_all("/FOREIGN KEY \(`(.*?)`\) REFERENCES `(.*?)` \(`(.*?)`\)/", $create_sql, $matches) ) {
$num_keys = count($matches[0]);
for ( $i = 0; $i < $num_keys; $i ++ ) {
$my_field = explode('`, `', $matches[1][$i]);
$ref_table = $matches[2][$i];
$ref_field = explode('`, `', $matches[3][$i]);
if ( $upper ) {
$ref_table = strtoupper($ref_table);
}
$foreign_keys[$ref_table] = array();
$num_fields = count($my_field);
for ( $j = 0; $j < $num_fields; $j ++ ) {
if ( $asociative ) {
$foreign_keys[$ref_table][$ref_field[$j]] = $my_field[$j];
} else {
$foreign_keys[$ref_table][] = "{$my_field[$j]}={$ref_field[$j]}";
}
}
}
}
return $foreign_keys;
}
/**
2005-11-04 19:38:29 +01:00
* @var array $charset2mysql translate www charsets to mysql ones
*/
var $charset2mysql = array(
'utf-8' => 'utf8',
'iso-8859-1' => 'latin1',
'iso-8859-2' => 'latin2',
'windows-1251' => 'cp1251',
'koi8-r' => 'koi8r', // 4.0: koi8_ru
'euc-kr' => 'euckr', // 4.0: euc_kr
'euc-jp' => 'ujis', // 4.0: -
'iso-8859-7' => 'greek', // 4.0: -
);
/**
* gets the client encoding from the connection
*
* mysqli_client_encoding only returns the default charset, not the one currently used!
*
2005-11-04 19:38:29 +01:00
* @return string/boolean charset or false
*/
function GetCharSet()
{
$this->charSet = $this->GetOne('SELECT @@character_set_connection');
if ($this->charSet) {
$mysql2charset = array_flip($this->charset2mysql);
if (isset($mysql2charset[$this->charSet])) {
$this->charSet = $mysql2charset[$this->charSet];
}
}
return $this->charSet ? $this->charSet : false;
}
2005-11-04 19:38:29 +01:00
/**
* sets the client encoding from the connection
*
* @param string $charset_name
* @return boolean true on success, false otherwise
*/
function SetCharSet($charset_name)
{
$mysql_charset = isset($this->charset2mysql[$charset_name]) ? $this->charset2mysql[$charset_name] : $charset_name;
if (!mysql_query('SET NAMES '.$this->qstr($mysql_charset),$this->_connectionID)) return false;
if ($this->GetCharSet()) {
return $this->charSet == $charset_name || $this->charset2mysql[$this->charSet] == $charset_name;
}
return false;
2005-11-04 19:38:29 +01:00
}
2003-10-19 21:05:23 +02:00
}
2005-11-04 19:38:29 +01:00
2003-10-19 21:05:23 +02:00
/*--------------------------------------------------------------------------------------
Class Name: Recordset
--------------------------------------------------------------------------------------*/
2004-08-02 10:30:47 +02:00
class ADORecordSet_mysql extends ADORecordSet{
2003-10-19 21:05:23 +02:00
var $databaseType = "mysql";
var $canSeek = true;
function ADORecordSet_mysql($queryID,$mode=false)
2003-10-19 21:05:23 +02:00
{
if ($mode === false) {
2003-10-19 21:05:23 +02:00
global $ADODB_FETCH_MODE;
$mode = $ADODB_FETCH_MODE;
}
switch ($mode)
{
case ADODB_FETCH_NUM: $this->fetchMode = MYSQL_NUM; break;
case ADODB_FETCH_ASSOC:$this->fetchMode = MYSQL_ASSOC; break;
case ADODB_FETCH_DEFAULT:
2005-09-26 12:12:10 +02:00
case ADODB_FETCH_BOTH:
default:
$this->fetchMode = MYSQL_BOTH; break;
2003-10-19 21:05:23 +02:00
}
2005-09-26 12:12:10 +02:00
$this->adodbFetchMode = $mode;
$this->ADORecordSet($queryID);
2003-10-19 21:05:23 +02:00
}
2003-10-19 21:05:23 +02:00
function _initrs()
{
//GLOBAL $ADODB_COUNTRECS;
// $this->_numOfRows = ($ADODB_COUNTRECS) ? @mysql_num_rows($this->_queryID):-1;
$this->_numOfRows = @mysql_num_rows($this->_queryID);
$this->_numOfFields = @mysql_num_fields($this->_queryID);
}
function &FetchField($fieldOffset = -1)
{
2003-10-19 21:05:23 +02:00
if ($fieldOffset != -1) {
$o = @mysql_fetch_field($this->_queryID, $fieldOffset);
$f = @mysql_field_flags($this->_queryID,$fieldOffset);
$o->max_length = @mysql_field_len($this->_queryID,$fieldOffset); // suggested by: Jim Nicholson (jnich@att.com)
//$o->max_length = -1; // mysql returns the max length less spaces -- so it is unrealiable
$o->binary = (strpos($f,'binary')!== false);
}
else if ($fieldOffset == -1) { /* The $fieldOffset argument is not provided thus its -1 */
$o = @mysql_fetch_field($this->_queryID);
$o->max_length = @mysql_field_len($this->_queryID); // suggested by: Jim Nicholson (jnich@att.com)
//$o->max_length = -1; // mysql returns the max length less spaces -- so it is unrealiable
}
2003-10-19 21:05:23 +02:00
return $o;
}
function &GetRowAssoc($upper=true)
{
if ($this->fetchMode == MYSQL_ASSOC && !$upper) return $this->fields;
$row =& ADORecordSet::GetRowAssoc($upper);
return $row;
2003-10-19 21:05:23 +02:00
}
2003-10-19 21:05:23 +02:00
/* Use associative array to get fields array */
function Fields($colname)
{
2003-10-19 21:05:23 +02:00
// added @ by "Michael William Miller" <mille562@pilot.msu.edu>
if ($this->fetchMode != MYSQL_NUM) return @$this->fields[$colname];
2003-10-19 21:05:23 +02:00
if (!$this->bind) {
$this->bind = array();
for ($i=0; $i < $this->_numOfFields; $i++) {
$o = $this->FetchField($i);
$this->bind[strtoupper($o->name)] = $i;
}
}
return $this->fields[$this->bind[strtoupper($colname)]];
}
2003-10-19 21:05:23 +02:00
function _seek($row)
{
if ($this->_numOfRows == 0) return false;
return @mysql_data_seek($this->_queryID,$row);
}
2004-08-02 10:30:47 +02:00
function MoveNext()
2003-10-19 21:05:23 +02:00
{
2004-08-02 10:30:47 +02:00
//return adodb_movenext($this);
//if (defined('ADODB_EXTENSION')) return adodb_movenext($this);
2005-09-26 12:12:10 +02:00
if (@$this->fields = mysql_fetch_array($this->_queryID,$this->fetchMode)) {
2004-08-02 10:30:47 +02:00
$this->_currentRow += 1;
return true;
}
if (!$this->EOF) {
$this->_currentRow += 1;
$this->EOF = true;
2003-10-19 21:05:23 +02:00
}
return false;
2004-08-02 10:30:47 +02:00
}
2003-10-19 21:05:23 +02:00
function _fetch()
{
$this->fields = @mysql_fetch_array($this->_queryID,$this->fetchMode);
return is_array($this->fields);
}
2003-10-19 21:05:23 +02:00
function _close() {
@mysql_free_result($this->_queryID);
$this->_queryID = false;
2003-10-19 21:05:23 +02:00
}
2003-10-19 21:05:23 +02:00
function MetaType($t,$len=-1,$fieldobj=false)
{
if (is_object($t)) {
$fieldobj = $t;
$t = $fieldobj->type;
$len = $fieldobj->max_length;
}
2003-10-19 21:05:23 +02:00
$len = -1; // mysql max_length is not accurate
switch (strtoupper($t)) {
case 'STRING':
2003-10-19 21:05:23 +02:00
case 'CHAR':
case 'VARCHAR':
case 'TINYBLOB':
case 'TINYTEXT':
case 'ENUM':
case 'SET':
2003-10-19 21:05:23 +02:00
if ($len <= $this->blobSize) return 'C';
2003-10-19 21:05:23 +02:00
case 'TEXT':
case 'LONGTEXT':
2003-10-19 21:05:23 +02:00
case 'MEDIUMTEXT':
return 'X';
2003-10-19 21:05:23 +02:00
// php_mysql extension always returns 'blob' even if 'text'
// so we have to check whether binary...
case 'IMAGE':
case 'LONGBLOB':
2003-10-19 21:05:23 +02:00
case 'BLOB':
case 'MEDIUMBLOB':
return !empty($fieldobj->binary) ? 'B' : 'X';
2003-10-19 21:05:23 +02:00
case 'YEAR':
case 'DATE': return 'D';
2003-10-19 21:05:23 +02:00
case 'TIME':
case 'DATETIME':
case 'TIMESTAMP': return 'T';
case 'INT':
2003-10-19 21:05:23 +02:00
case 'INTEGER':
case 'BIGINT':
case 'TINYINT':
case 'MEDIUMINT':
case 'SMALLINT':
2005-09-26 12:12:10 +02:00
if (!empty($fieldobj->primary_key)) return 'R';
2003-10-19 21:05:23 +02:00
else return 'I';
2003-10-19 21:05:23 +02:00
default: return 'N';
}
}
}
2004-08-02 10:30:47 +02:00
class ADORecordSet_ext_mysql extends ADORecordSet_mysql {
function ADORecordSet_ext_mysql($queryID,$mode=false)
2004-08-02 10:30:47 +02:00
{
if ($mode === false) {
2004-08-02 10:30:47 +02:00
global $ADODB_FETCH_MODE;
$mode = $ADODB_FETCH_MODE;
}
switch ($mode)
{
case ADODB_FETCH_NUM: $this->fetchMode = MYSQL_NUM; break;
case ADODB_FETCH_ASSOC:$this->fetchMode = MYSQL_ASSOC; break;
case ADODB_FETCH_DEFAULT:
2005-09-26 12:12:10 +02:00
case ADODB_FETCH_BOTH:
default:
$this->fetchMode = MYSQL_BOTH; break;
2004-08-02 10:30:47 +02:00
}
2005-09-26 12:12:10 +02:00
$this->adodbFetchMode = $mode;
$this->ADORecordSet($queryID);
2004-08-02 10:30:47 +02:00
}
2004-08-02 10:30:47 +02:00
function MoveNext()
{
2005-09-26 12:12:10 +02:00
return @adodb_movenext($this);
2004-08-02 10:30:47 +02:00
}
}
2003-10-19 21:05:23 +02:00
}
?>