initial commit

This commit is contained in:
Chris Sewell
2012-11-28 03:55:08 -05:00
parent 7adb399b2e
commit cf140a2e97
3247 changed files with 492437 additions and 0 deletions

View File

@ -0,0 +1,442 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Wrappers for Drizzle extension classes
*
* Drizzle extension exposes libdrizzle functions and requires user to have it in mind while using them.
* This wrapper is not complete and hides a lot of original functionality, but allows for easy usage
* of the drizzle PHP extension.
*
* @package PhpMyAdmin-DBI-Drizzle
*/
// TODO: drizzle module segfaults while freeing resources, often. This allows at least for some development
function _drizzle_shutdown_flush() {
flush();
}
register_shutdown_function('_drizzle_shutdown_flush');
function _dlog_argstr($args)
{
$r = array();
foreach ($args as $arg) {
if (is_object($arg)) {
$r[] = get_class($arg);
} elseif (is_bool($arg)) {
$r[] = $arg ? 'true' : 'false';
} elseif (is_null($arg)) {
$r[] = 'null';
} else {
$r[] = $arg;
}
}
return implode(', ', $r);
}
function _dlog($end = false)
{
/*
static $fp = null;
if (!$fp) {
$fp = fopen('./drizzle_log.log', 'a');
flock($fp, LOCK_EX);
fwrite($fp, "\r\n[" . date('H:i:s') . "]\t" . $_SERVER['REQUEST_URI'] . "\r\n");
register_shutdown_function(function() use ($fp) {
fwrite($fp, '[' . date('H:i:s') . "]\tEND\r\n\r\n");
});
}
if ($end) {
fwrite($fp, '[' . date('H:i:s') . "]\tok\r\n");
} else {
$bt = debug_backtrace(true);
$caller = (isset($bt[1]['class']) ? $bt[1]['class'] . '::' : '') . $bt[1]['function'];
if ($bt[1]['function'] == '__call') {
$caller .= '^' . $bt[1]['args'][0];
$args = _dlog_argstr($bt[1]['args'][1]);
} else {
$args = _dlog_argstr($bt[1]['args']);
}
fwrite($fp, '[' . date('H:i:s') . "]\t" . $caller . "\t" . $args . "\r\n");
for ($i = 2; $i <= count($bt)-1; $i++) {
if (!isset($bt[$i])) {
break;
}
$caller = (isset($bt[$i]['class']) ? $bt[$i]['class'] . '::' : '') . $bt[$i]['function'];
$caller .= ' (' . $bt[$i]['file'] . ':' . $bt[$i]['line'] . ')';
fwrite($fp, str_repeat(' ', 20) . $caller . "\r\n");
}
}
//*/
}
/**
* Wrapper for Drizzle class
*/
class PMA_Drizzle extends Drizzle
{
/**
* Fetch mode: result rows contain column names
*/
const FETCH_ASSOC = 1;
/**
* Fetch mode: result rows contain only numeric indices
*/
const FETCH_NUM = 2;
/**
* Fetch mode: result rows have both column names and numeric indices
*/
const FETCH_BOTH = 3;
/**
* Result buffering: entire result set is buffered upon execution
*/
const BUFFER_RESULT = 1;
/**
* Result buffering: buffering occurs only on row level
*/
const BUFFER_ROW = 2;
/**
* Constructor
*/
public function __construct()
{_dlog();
parent::__construct();
}
/**
* Creates a new database conection using TCP
*
* @param $host
* @param $port
* @param $user
* @param $password
* @param $db
* @param $options
* @return PMA_DrizzleCon
*/
public function addTcp($host, $port, $user, $password, $db, $options)
{_dlog();
$dcon = parent::addTcp($host, $port, $user, $password, $db, $options);
return $dcon instanceof DrizzleCon
? new PMA_DrizzleCon($dcon)
: $dcon;
}
/**
* Creates a new connection using unix domain socket
*
* @param $uds
* @param $user
* @param $password
* @param $db
* @param $options
* @return PMA_DrizzleCon
*/
public function addUds($uds, $user, $password, $db, $options)
{_dlog();
$dcon = parent::addUds($uds, $user, $password, $db, $options);
return $dcon instanceof DrizzleCon
? new PMA_DrizzleCon($dcon)
: $dcon;
}
}
/**
* Wrapper around DrizzleCon class
*
* Its main task is to wrap results with PMA_DrizzleResult class
*/
class PMA_DrizzleCon
{
/**
* Instance of DrizzleCon class
* @var DrizzleCon
*/
private $dcon;
/**
* Result of the most recent query
* @var PMA_DrizzleResult
*/
private $lastResult;
/**
* Constructor
*
* @param DrizzleCon $dcon
*/
public function __construct(DrizzleCon $dcon)
{_dlog();
$this->dcon = $dcon;
}
/**
* Executes given query. Opens database connection if not already done.
*
* @param string $query
* @param int $bufferMode PMA_Drizzle::BUFFER_RESULT, PMA_Drizzle::BUFFER_ROW
* @param int $fetchMode PMA_Drizzle::FETCH_ASSOC, PMA_Drizzle::FETCH_NUM or PMA_Drizzle::FETCH_BOTH
* @return PMA_DrizzleResult
*/
public function query($query, $bufferMode = PMA_Drizzle::BUFFER_RESULT, $fetchMode = PMA_Drizzle::FETCH_ASSOC)
{_dlog();
$result = $this->dcon->query($query);
if ($result instanceof DrizzleResult) {
_dlog(true);
$this->lastResult = new PMA_DrizzleResult($result, $bufferMode, $fetchMode);
return $this->lastResult;
}
return $result;
}
/**
* Returns the number of rows affected by last query
*
* @return int|false
*/
public function affectedRows()
{
return $this->lastResult
? $this->lastResult->affectedRows()
: false;
}
/**
* Pass calls of undefined methods to DrizzleCon object
*
* @param $method
* @param $args
* @return mixed
*/
public function __call($method, $args)
{_dlog();
return call_user_func_array(array($this->dcon, $method), $args);
}
/**
* Returns original Drizzle connection object
*
* @return DrizzleCon
*/
public function getConnectionObject()
{_dlog();
return $this->dcon;
}
}
/**
* Wrapper around DrizzleResult. Allows for reading result rows as an associative array
* and hides complexity behind buffering.
*/
class PMA_DrizzleResult
{
/**
* Instamce of DrizzleResult class
* @var DrizzleResult
*/
private $dresult;
/**
* Fetch mode
* @var int
*/
private $fetchMode;
/**
* Buffering mode
* @var int
*/
private $bufferMode;
/**
* Cached column data
* @var DrizzleColumn[]
*/
private $columns = null;
/**
* Cached column names
* @var string[]
*/
private $columnNames = null;
/**
* Constructor
*
* @param DrizzleResult $dresult
* @param int $bufferMode
* @param int $fetchMode
*/
public function __construct(DrizzleResult $dresult, $bufferMode, $fetchMode)
{_dlog();
$this->dresult = $dresult;
$this->bufferMode = $bufferMode;
$this->fetchMode = $fetchMode;
if ($this->bufferMode == PMA_Drizzle::BUFFER_RESULT) {
$this->dresult->buffer();
}
}
/**
* Sets fetch mode
*
* @param int $fetchMode
*/
public function setFetchMode($fetchMode)
{_dlog();
$this->fetchMode = $fetchMode;
}
/**
* Reads information about columns contained in current result set into {@see $columns} and {@see $columnNames} arrays
*/
private function _readColumns()
{_dlog();
$this->columns = array();
$this->columnNames = array();
if ($this->bufferMode == PMA_Drizzle::BUFFER_RESULT) {
while (($column = $this->dresult->columnNext()) !== null) {
$this->columns[] = $column;
$this->columnNames[] = $column->name();
}
} else {
while (($column = $this->dresult->columnRead()) !== null) {
$this->columns[] = $column;
$this->columnNames[] = $column->name();
}
}
}
/**
* Returns columns in current result
*
* @return DrizzleColumn[]
*/
public function getColumns()
{_dlog();
if (!$this->columns) {
$this->_readColumns();
}
return $this->columns;
}
/**
* Returns number if columns in result
*
* @return int
*/
public function numColumns()
{_dlog();
return $this->dresult->columnCount();
}
/**
* Transforms result row to conform to current fetch mode
*
* @param mixed &$row
* @param int $fetchMode
*/
private function _transformResultRow(&$row, $fetchMode)
{
if (!$row) {
return;
}
switch ($fetchMode) {
case PMA_Drizzle::FETCH_ASSOC:
$row = array_combine($this->columnNames, $row);
break;
case PMA_Drizzle::FETCH_BOTH:
$length = count($row);
for ($i = 0; $i < $length; $i++) {
$row[$this->columnNames[$i]] = $row[$i];
}
break;
default:
break;
}
}
/**
* Fetches next for from this result set
*
* @param int $fetchMode fetch mode to use, if none given the default one is used
* @return array|null
*/
public function fetchRow($fetchMode = null)
{_dlog();
// read column names on first fetch, only buffered results allow for reading it later
if (!$this->columns) {
$this->_readColumns();
}
if ($fetchMode === null) {
$fetchMode = $this->fetchMode;
}
$row = null;
switch ($this->bufferMode) {
case PMA_Drizzle::BUFFER_RESULT:
$row = $this->dresult->rowNext();
break;
case PMA_Drizzle::BUFFER_ROW:
$row = $this->dresult->rowBuffer();
break;
}
$this->_transformResultRow($row, $fetchMode);
return $row;
}
/**
* Adjusts the result pointer to an arbitrary row in buffered result
*
* @param $row_index
* @return bool
*/
public function seek($row_index)
{_dlog();
if ($this->bufferMode != PMA_Drizzle::BUFFER_RESULT) {
trigger_error("Can't seek in an unbuffered result set", E_USER_WARNING);
return false;
}
// rowSeek always returns NULL (drizzle extension v.0.5, API v.7)
if ($row_index >= 0 && $row_index < $this->dresult->rowCount()) {
$this->dresult->rowSeek($row_index);
return true;
}
return false;
}
/**
* Returns the number of rows in buffered result set
*
* @return int|false
*/
public function numRows()
{_dlog();
if ($this->bufferMode != PMA_Drizzle::BUFFER_RESULT) {
trigger_error("Can't count rows in an unbuffered result set", E_USER_WARNING);
return false;
}
return $this->dresult->rowCount();
}
/**
* Returns the number of rows affected by query
*
* @return int|false
*/
public function affectedRows()
{_dlog();
return $this->dresult->affectedRows();
}
/**
* Frees resources taken by this result
*/
public function free()
{_dlog();
unset($this->columns);
unset($this->columnNames);
drizzle_result_free($this->dresult);
unset($this->dresult);
}
}

View File

@ -0,0 +1,603 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Interface to the Drizzle extension
*
* WARNING - EXPERIMENTAL, never use in production, drizzle module segfaults often and when you least expect it to
*
* TODO: This file and drizzle-wrappers.lib.php should be devoid of any segault related hacks.
* TODO: Crashing versions of drizzle module and/or libdrizzle should be blacklisted
*
* @package PhpMyAdmin-DBI-Drizzle
*/
if (! defined('PHPMYADMIN')) {
exit;
}
require_once './libraries/logging.lib.php';
require_once './libraries/dbi/drizzle-wrappers.lib.php';
/**
* MySQL client API
*/
if (!defined('PMA_MYSQL_CLIENT_API')) {
define('PMA_MYSQL_CLIENT_API', (int)drizzle_version());
}
/**
* Helper function for connecting to the database server
*
* @param PMA_Drizzle $drizzle
* @param string $host
* @param int $port
* @param string $uds
* @param string $user
* @param string $password
* @param string $db
* @param int $options
* @return PMA_DrizzleCon
*/
function PMA_DBI_real_connect($drizzle, $host, $port, $uds, $user, $password, $db = null, $options = DRIZZLE_CON_NONE)
{
if ($uds) {
$con = $drizzle->addUds($uds, $user, $password, $db, $options);
} else {
$con = $drizzle->addTcp($host, $port, $user, $password, $db, $options);
}
return $con;
}
/**
* connects to the database server
*
* @param string $user drizzle user name
* @param string $password drizzle user password
* @param bool $is_controluser
* @param array $server host/port/socket
* @param bool $auxiliary_connection (when true, don't go back to login if connection fails)
* @return mixed false on error or a mysqli object on success
*/
function PMA_DBI_connect($user, $password, $is_controluser = false, $server = null, $auxiliary_connection = false)
{
global $cfg;
if ($server) {
$server_port = (empty($server['port']))
? false
: (int)$server['port'];
$server_socket = (empty($server['socket']))
? ''
: $server['socket'];
$server['host'] = (empty($server['host']))
? 'localhost'
: $server['host'];
} else {
$server_port = (empty($cfg['Server']['port']))
? false
: (int) $cfg['Server']['port'];
$server_socket = (empty($cfg['Server']['socket']))
? null
: $cfg['Server']['socket'];
}
if (strtolower($GLOBALS['cfg']['Server']['connect_type']) == 'tcp') {
$GLOBALS['cfg']['Server']['socket'] = '';
}
$drizzle = new PMA_Drizzle();
$client_flags = 0;
/* Optionally compress connection */
if ($GLOBALS['cfg']['Server']['compress']) {
$client_flags |= DRIZZLE_CAPABILITIES_COMPRESS;
}
/* Optionally enable SSL */
if ($GLOBALS['cfg']['Server']['ssl']) {
$client_flags |= DRIZZLE_CAPABILITIES_SSL;
}
if (!$server) {
$link = @PMA_DBI_real_connect($drizzle, $cfg['Server']['host'], $server_port, $server_socket, $user, $password, false, $client_flags);
// Retry with empty password if we're allowed to
if ($link == false && isset($cfg['Server']['nopassword']) && $cfg['Server']['nopassword'] && !$is_controluser) {
$link = @PMA_DBI_real_connect($drizzle, $cfg['Server']['host'], $server_port, $server_socket, $user, null, false, $client_flags);
}
} else {
$link = @PMA_DBI_real_connect($drizzle, $server['host'], $server_port, $server_socket, $user, $password);
}
if ($link == false) {
if ($is_controluser) {
trigger_error(__('Connection for controluser as defined in your configuration failed.'), E_USER_WARNING);
return false;
}
// we could be calling PMA_DBI_connect() to connect to another
// server, for example in the Synchronize feature, so do not
// go back to main login if it fails
if (! $auxiliary_connection) {
PMA_log_user($user, 'drizzle-denied');
PMA_auth_fails();
} else {
return false;
}
} else {
PMA_DBI_postConnect($link, $is_controluser);
}
return $link;
}
/**
* selects given database
*
* @param string $dbname database name to select
* @param PMA_DrizzleCom $link connection object
* @return bool
*/
function PMA_DBI_select_db($dbname, $link = null)
{
if (empty($link)) {
if (isset($GLOBALS['userlink'])) {
$link = $GLOBALS['userlink'];
} else {
return false;
}
}
return $link->selectDb($dbname);
}
/**
* runs a query and returns the result
*
* @param string $query query to execute
* @param PMA_DrizzleCon $link connection object
* @param int $options
* @return PMA_DrizzleResult
*/
function PMA_DBI_real_query($query, $link, $options)
{
$buffer_mode = $options & PMA_DBI_QUERY_UNBUFFERED
? PMA_Drizzle::BUFFER_ROW
: PMA_Drizzle::BUFFER_RESULT;
$res = $link->query($query, $buffer_mode);
return $res;
}
/**
* returns array of rows with associative and numeric keys from $result
*
* @param PMA_DrizzleResult $result
* @return array
*/
function PMA_DBI_fetch_array($result)
{
return $result->fetchRow(PMA_Drizzle::FETCH_BOTH);
}
/**
* returns array of rows with associative keys from $result
*
* @param PMA_DrizzleResult $result
* @return array
*/
function PMA_DBI_fetch_assoc($result)
{
return $result->fetchRow(PMA_Drizzle::FETCH_ASSOC);
}
/**
* returns array of rows with numeric keys from $result
*
* @param PMA_DrizzleResult $result
* @return array
*/
function PMA_DBI_fetch_row($result)
{
return $result->fetchRow(PMA_Drizzle::FETCH_NUM);
}
/**
* Adjusts the result pointer to an arbitrary row in the result
*
* @param PMA_DrizzleResult $result
* @param int $offset
* @return boolean true on success, false on failure
*/
function PMA_DBI_data_seek($result, $offset)
{
return $result->seek($offset);
}
/**
* Frees memory associated with the result
*
* @param PMA_DrizzleResult $result
*/
function PMA_DBI_free_result($result)
{
if ($result instanceof PMA_DrizzleResult) {
$result->free();
}
}
/**
* Check if there are any more query results from a multi query
*
* @return bool false
*/
function PMA_DBI_more_results() {
// N.B.: PHP's 'mysql' extension does not support
// multi_queries so this function will always
// return false. Use the 'mysqli' extension, if
// you need support for multi_queries.
return false;
}
/**
* Prepare next result from multi_query
*
* @return bool false
*/
function PMA_DBI_next_result() {
// N.B.: PHP's 'mysql' extension does not support
// multi_queries so this function will always
// return false. Use the 'mysqli' extension, if
// you need support for multi_queries.
return false;
}
/**
* Returns a string representing the type of connection used
* @param PMA_DrizzleCon $link connection object
* @return string type of connection used
*/
function PMA_DBI_get_host_info($link = null)
{
if (null === $link) {
if (isset($GLOBALS['userlink'])) {
$link = $GLOBALS['userlink'];
} else {
return false;
}
}
$str = $link->port()
? $link->host() . ':' . $link->port() . ' via TCP/IP'
: 'Localhost via UNIX socket';
return $str;
}
/**
* Returns the version of the Drizzle protocol used
* @param PMA_DrizzleCon $link connection object
* @return int version of the Drizzle protocol used
*/
function PMA_DBI_get_proto_info($link = null)
{
if (null === $link) {
if (isset($GLOBALS['userlink'])) {
$link = $GLOBALS['userlink'];
} else {
return false;
}
}
return $link->protocolVersion();
}
/**
* returns a string that represents the client library version
* @return string Drizzle client library version
*/
function PMA_DBI_get_client_info()
{
return 'libdrizzle (Drizzle ' . drizzle_version() . ')';
}
/**
* returns last error message or false if no errors occured
*
* @param PMA_DrizzleCon $link connection object
* @return string|bool $error or false
*/
function PMA_DBI_getError($link = null)
{
$GLOBALS['errno'] = 0;
/* Treat false same as null because of controllink */
if ($link === false) {
$link = null;
}
if (null === $link && isset($GLOBALS['userlink'])) {
$link =& $GLOBALS['userlink'];
// Do not stop now. We still can get the error code
// with mysqli_connect_errno()
// } else {
// return false;
}
if (null !== $link) {
$error_number = drizzle_con_errno($link->getConnectionObject());
$error_message = drizzle_con_error($link->getConnectionObject());
} else {
$error_number = drizzle_errno();
$error_message = drizzle_error();
}
if (0 == $error_number) {
return false;
}
// keep the error number for further check after the call to PMA_DBI_getError()
$GLOBALS['errno'] = $error_number;
return PMA_DBI_formatError($error_number, $error_message);
}
/**
* returns the number of rows returned by last query
*
* @param PMA_DrizzleResult $result
* @return string|int
*/
function PMA_DBI_num_rows($result)
{
// see the note for PMA_DBI_try_query();
if (!is_bool($result)) {
return @$result->numRows();
} else {
return 0;
}
}
/**
* returns last inserted auto_increment id for given $link or $GLOBALS['userlink']
*
* @param PMA_DrizzleCon $link connection object
* @return string|int
*/
function PMA_DBI_insert_id($link = null)
{
if (empty($link)) {
if (isset($GLOBALS['userlink'])) {
$link = $GLOBALS['userlink'];
} else {
return false;
}
}
// copied from mysql and mysqli
// When no controluser is defined, using mysqli_insert_id($link)
// does not always return the last insert id due to a mixup with
// the tracking mechanism, but this works:
return PMA_DBI_fetch_value('SELECT LAST_INSERT_ID();', 0, 0, $link);
// Curiously, this problem does not happen with the mysql extension but
// there is another problem with BIGINT primary keys so PMA_DBI_insert_id()
// in the mysql extension also uses this logic.
}
/**
* returns the number of rows affected by last query
*
* @param PMA_DrizzleResult $link connection object
* @param bool $get_from_cache
* @return string|int
*/
function PMA_DBI_affected_rows($link = null, $get_from_cache = true)
{
if (empty($link)) {
if (isset($GLOBALS['userlink'])) {
$link = $GLOBALS['userlink'];
} else {
return false;
}
}
if ($get_from_cache) {
return $GLOBALS['cached_affected_rows'];
} else {
return $link->affectedRows();
}
}
/**
* returns metainfo for fields in $result
*
* @param PMA_DrizzleResult $result
* @return array meta info for fields in $result
*/
function PMA_DBI_get_fields_meta($result)
{
// Build an associative array for a type look up
$typeAr = array();
/*$typeAr[DRIZZLE_COLUMN_TYPE_DECIMAL] = 'real';
$typeAr[DRIZZLE_COLUMN_TYPE_NEWDECIMAL] = 'real';
$typeAr[DRIZZLE_COLUMN_TYPE_BIT] = 'int';
$typeAr[DRIZZLE_COLUMN_TYPE_TINY] = 'int';
$typeAr[DRIZZLE_COLUMN_TYPE_SHORT] = 'int';
$typeAr[DRIZZLE_COLUMN_TYPE_LONG] = 'int';
$typeAr[DRIZZLE_COLUMN_TYPE_FLOAT] = 'real';
$typeAr[DRIZZLE_COLUMN_TYPE_DOUBLE] = 'real';
$typeAr[DRIZZLE_COLUMN_TYPE_NULL] = 'null';
$typeAr[DRIZZLE_COLUMN_TYPE_TIMESTAMP] = 'timestamp';
$typeAr[DRIZZLE_COLUMN_TYPE_LONGLONG] = 'int';
$typeAr[DRIZZLE_COLUMN_TYPE_INT24] = 'int';
$typeAr[DRIZZLE_COLUMN_TYPE_DATE] = 'date';
$typeAr[DRIZZLE_COLUMN_TYPE_TIME] = 'date';
$typeAr[DRIZZLE_COLUMN_TYPE_DATETIME] = 'datetime';
$typeAr[DRIZZLE_COLUMN_TYPE_YEAR] = 'year';
$typeAr[DRIZZLE_COLUMN_TYPE_NEWDATE] = 'date';
$typeAr[DRIZZLE_COLUMN_TYPE_ENUM] = 'unknown';
$typeAr[DRIZZLE_COLUMN_TYPE_SET] = 'unknown';
$typeAr[DRIZZLE_COLUMN_TYPE_VIRTUAL] = 'unknown';
$typeAr[DRIZZLE_COLUMN_TYPE_TINY_BLOB] = 'blob';
$typeAr[DRIZZLE_COLUMN_TYPE_MEDIUM_BLOB] = 'blob';
$typeAr[DRIZZLE_COLUMN_TYPE_LONG_BLOB] = 'blob';
$typeAr[DRIZZLE_COLUMN_TYPE_BLOB] = 'blob';
$typeAr[DRIZZLE_COLUMN_TYPE_VAR_STRING] = 'string';
$typeAr[DRIZZLE_COLUMN_TYPE_VARCHAR] = 'string';
$typeAr[DRIZZLE_COLUMN_TYPE_STRING] = 'string';
$typeAr[DRIZZLE_COLUMN_TYPE_GEOMETRY] = 'geometry';*/
$typeAr[DRIZZLE_COLUMN_TYPE_DRIZZLE_BLOB] = 'blob';
$typeAr[DRIZZLE_COLUMN_TYPE_DRIZZLE_DATE] = 'date';
$typeAr[DRIZZLE_COLUMN_TYPE_DRIZZLE_DATETIME] = 'datetime';
$typeAr[DRIZZLE_COLUMN_TYPE_DRIZZLE_DOUBLE] = 'real';
$typeAr[DRIZZLE_COLUMN_TYPE_DRIZZLE_ENUM] = 'unknown';
$typeAr[DRIZZLE_COLUMN_TYPE_DRIZZLE_LONG] = 'int';
$typeAr[DRIZZLE_COLUMN_TYPE_DRIZZLE_LONGLONG] = 'int';
$typeAr[DRIZZLE_COLUMN_TYPE_DRIZZLE_MAX] = 'unknown';
$typeAr[DRIZZLE_COLUMN_TYPE_DRIZZLE_NULL] = 'null';
$typeAr[DRIZZLE_COLUMN_TYPE_DRIZZLE_TIMESTAMP] = 'timestamp';
$typeAr[DRIZZLE_COLUMN_TYPE_DRIZZLE_TINY] = 'int';
$typeAr[DRIZZLE_COLUMN_TYPE_DRIZZLE_VARCHAR] = 'string';
// array of DrizzleColumn
$columns = $result->getColumns();
// columns in a standarized format
$std_columns = array();
foreach ($columns as $k => $column) {
$c = new stdClass();
$c->name = $column->name();
$c->orgname = $column->origName();
$c->table = $column->table();
$c->orgtable = $column->origTable();
$c->def = $column->defaultValue();
$c->db = $column->db();
$c->catalog = $column->catalog();
// $column->maxSize() returns always 0 while size() seems
// to return a correct value (drizzle extension v.0.5, API v.7)
$c->max_length = $column->size();
$c->decimals = $column->decimals();
$c->charsetnr = $column->charset();
$c->type = $typeAr[$column->typeDrizzle()];
$c->_type = $column->type();
$c->flags = PMA_DBI_field_flags($result, $k);
$c->_flags = $column->flags();
$c->multiple_key = (int) (bool) ($c->_flags & DRIZZLE_COLUMN_FLAGS_MULTIPLE_KEY);
$c->primary_key = (int) (bool) ($c->_flags & DRIZZLE_COLUMN_FLAGS_PRI_KEY);
$c->unique_key = (int) (bool) ($c->_flags & DRIZZLE_COLUMN_FLAGS_UNIQUE_KEY);
$c->not_null = (int) (bool) ($c->_flags & DRIZZLE_COLUMN_FLAGS_NOT_NULL);
$c->unsigned = (int) (bool) ($c->_flags & DRIZZLE_COLUMN_FLAGS_UNSIGNED);
$c->zerofill = (int) (bool) ($c->_flags & DRIZZLE_COLUMN_FLAGS_ZEROFILL);
$c->numeric = (int) (bool) ($c->_flags & DRIZZLE_COLUMN_FLAGS_NUM);
$c->blob = (int) (bool) ($c->_flags & DRIZZLE_COLUMN_FLAGS_BLOB);
$std_columns[] = $c;
}
return $std_columns;
}
/**
* return number of fields in given $result
*
* @param PMA_DrizzleResult $result
* @return int field count
*/
function PMA_DBI_num_fields($result)
{
return $result->numColumns();
}
/**
* returns the length of the given field $i in $result
*
* @param PMA_DrizzleResult $result
* @param int $i field
* @return int length of field
*/
function PMA_DBI_field_len($result, $i)
{
$colums = $result->getColumns();
return $colums[$i]->size();
}
/**
* returns name of $i. field in $result
*
* @param PMA_DrizzleResult $result
* @param int $i field
* @return string name of $i. field in $result
*/
function PMA_DBI_field_name($result, $i)
{
$colums = $result->getColumns();
return $colums[$i]->name();
}
/**
* returns concatenated string of human readable field flags
*
* @param PMA_DrizzleResult $result
* @param int $i field
* @return string field flags
*/
function PMA_DBI_field_flags($result, $i)
{
$columns = $result->getColumns();
$f = $columns[$i];
$type = $f->typeDrizzle();
$charsetnr = $f->charset();
$f = $f->flags();
$flags = '';
if ($f & DRIZZLE_COLUMN_FLAGS_UNIQUE_KEY) {
$flags .= 'unique ';
}
if ($f & DRIZZLE_COLUMN_FLAGS_NUM) {
$flags .= 'num ';
}
if ($f & DRIZZLE_COLUMN_FLAGS_PART_KEY) {
$flags .= 'part_key ';
}
if ($f & DRIZZLE_COLUMN_FLAGS_SET) {
$flags .= 'set ';
}
if ($f & DRIZZLE_COLUMN_FLAGS_TIMESTAMP) {
$flags .= 'timestamp ';
}
if ($f & DRIZZLE_COLUMN_FLAGS_AUTO_INCREMENT) {
$flags .= 'auto_increment ';
}
if ($f & DRIZZLE_COLUMN_FLAGS_ENUM) {
$flags .= 'enum ';
}
// See http://dev.mysql.com/doc/refman/6.0/en/c-api-datatypes.html:
// to determine if a string is binary, we should not use MYSQLI_BINARY_FLAG
// but instead the charsetnr member of the MYSQL_FIELD
// structure. Watch out: some types like DATE returns 63 in charsetnr
// so we have to check also the type.
// Unfortunately there is no equivalent in the mysql extension.
if (($type == DRIZZLE_COLUMN_TYPE_DRIZZLE_BLOB || $type == DRIZZLE_COLUMN_TYPE_DRIZZLE_VARCHAR) && 63 == $charsetnr) {
$flags .= 'binary ';
}
if ($f & DRIZZLE_COLUMN_FLAGS_ZEROFILL) {
$flags .= 'zerofill ';
}
if ($f & DRIZZLE_COLUMN_FLAGS_UNSIGNED) {
$flags .= 'unsigned ';
}
if ($f & DRIZZLE_COLUMN_FLAGS_BLOB) {
$flags .= 'blob ';
}
if ($f & DRIZZLE_COLUMN_FLAGS_MULTIPLE_KEY) {
$flags .= 'multiple_key ';
}
if ($f & DRIZZLE_COLUMN_FLAGS_UNIQUE_KEY) {
$flags .= 'unique_key ';
}
if ($f & DRIZZLE_COLUMN_FLAGS_PRI_KEY) {
$flags .= 'primary_key ';
}
if ($f & DRIZZLE_COLUMN_FLAGS_NOT_NULL) {
$flags .= 'not_null ';
}
return trim($flags);
}
?>

View File

@ -0,0 +1,476 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Interface to the classic MySQL extension
*
* @package PhpMyAdmin-DBI-MySQL
*/
if (! defined('PHPMYADMIN')) {
exit;
}
require_once './libraries/logging.lib.php';
/**
* MySQL client API
*/
if (! defined('PMA_MYSQL_CLIENT_API')) {
$client_api = explode('.', mysql_get_client_info());
define('PMA_MYSQL_CLIENT_API', (int)sprintf('%d%02d%02d', $client_api[0], $client_api[1], intval($client_api[2])));
unset($client_api);
}
/**
* Helper function for connecting to the database server
*
* @param string $server
* @param string $user
* @param string $password
* @param int $client_flags
* @param bool $persistent
* @return mixed false on error or a mysql connection resource on success
*/
function PMA_DBI_real_connect($server, $user, $password, $client_flags, $persistent = false)
{
global $cfg;
if (empty($client_flags)) {
if ($cfg['PersistentConnections'] || $persistent) {
$link = @mysql_pconnect($server, $user, $password);
} else {
$link = @mysql_connect($server, $user, $password);
}
} else {
if ($cfg['PersistentConnections'] || $persistent) {
$link = @mysql_pconnect($server, $user, $password, $client_flags);
} else {
$link = @mysql_connect($server, $user, $password, false, $client_flags);
}
}
return $link;
}
/**
* connects to the database server
*
* @param string $user mysql user name
* @param string $password mysql user password
* @param bool $is_controluser
* @param array $server host/port/socket/persistent
* @param bool $auxiliary_connection (when true, don't go back to login if connection fails)
* @return mixed false on error or a mysqli object on success
*/
function PMA_DBI_connect($user, $password, $is_controluser = false, $server = null, $auxiliary_connection = false)
{
global $cfg;
if ($server) {
$server_port = (empty($server['port']))
? ''
: ':' . (int)$server['port'];
$server_socket = (empty($server['socket']))
? ''
: ':' . $server['socket'];
} else {
$server_port = (empty($cfg['Server']['port']))
? ''
: ':' . (int)$cfg['Server']['port'];
$server_socket = (empty($cfg['Server']['socket']))
? ''
: ':' . $cfg['Server']['socket'];
}
$client_flags = 0;
// always use CLIENT_LOCAL_FILES as defined in mysql_com.h
// for the case where the client library was not compiled
// with --enable-local-infile
$client_flags |= 128;
/* Optionally compress connection */
if (defined('MYSQL_CLIENT_COMPRESS') && $cfg['Server']['compress']) {
$client_flags |= MYSQL_CLIENT_COMPRESS;
}
/* Optionally enable SSL */
if (defined('MYSQL_CLIENT_SSL') && $cfg['Server']['ssl']) {
$client_flags |= MYSQL_CLIENT_SSL;
}
if (!$server) {
$link = PMA_DBI_real_connect($cfg['Server']['host'] . $server_port . $server_socket, $user, $password, empty($client_flags) ? null : $client_flags);
// Retry with empty password if we're allowed to
if (empty($link) && $cfg['Server']['nopassword'] && !$is_controluser) {
$link = PMA_DBI_real_connect($cfg['Server']['host'] . $server_port . $server_socket, $user, '', empty($client_flags) ? null : $client_flags);
}
} else {
if (!isset($server['host'])) {
$link = PMA_DBI_real_connect($server_socket, $user, $password, null);
} else {
$link = PMA_DBI_real_connect($server['host'] . $server_port . $server_socket, $user, $password, null);
}
}
if (empty($link)) {
if ($is_controluser) {
trigger_error(__('Connection for controluser as defined in your configuration failed.'), E_USER_WARNING);
return false;
}
// we could be calling PMA_DBI_connect() to connect to another
// server, for example in the Synchronize feature, so do not
// go back to main login if it fails
if (! $auxiliary_connection) {
PMA_log_user($user, 'mysql-denied');
PMA_auth_fails();
} else {
return false;
}
} // end if
if (! $server) {
PMA_DBI_postConnect($link, $is_controluser);
}
return $link;
}
/**
* selects given database
*
* @param string $dbname name of db to select
* @param resource $link mysql link resource
* @return bool
*/
function PMA_DBI_select_db($dbname, $link = null)
{
if (empty($link)) {
if (isset($GLOBALS['userlink'])) {
$link = $GLOBALS['userlink'];
} else {
return false;
}
}
return mysql_select_db($dbname, $link);
}
/**
* runs a query and returns the result
*
* @param string $query query to run
* @param resource $link mysql link resource
* @param int $options
* @return mixed
*/
function PMA_DBI_real_query($query, $link, $options)
{
if ($options == ($options | PMA_DBI_QUERY_STORE)) {
return mysql_query($query, $link);
} elseif ($options == ($options | PMA_DBI_QUERY_UNBUFFERED)) {
return mysql_unbuffered_query($query, $link);
} else {
return mysql_query($query, $link);
}
}
/**
* returns array of rows with associative and numeric keys from $result
*
* @param resource $result
* @return array
*/
function PMA_DBI_fetch_array($result)
{
return mysql_fetch_array($result, MYSQL_BOTH);
}
/**
* returns array of rows with associative keys from $result
*
* @param resource $result
* @return array
*/
function PMA_DBI_fetch_assoc($result)
{
return mysql_fetch_array($result, MYSQL_ASSOC);
}
/**
* returns array of rows with numeric keys from $result
*
* @param resource $result
* @return array
*/
function PMA_DBI_fetch_row($result)
{
return mysql_fetch_array($result, MYSQL_NUM);
}
/**
* Adjusts the result pointer to an arbitrary row in the result
*
* @param $result
* @param $offset
* @return bool true on success, false on failure
*/
function PMA_DBI_data_seek($result, $offset)
{
return mysql_data_seek($result, $offset);
}
/**
* Frees memory associated with the result
*
* @param resource $result
*/
function PMA_DBI_free_result($result)
{
if (is_resource($result) && get_resource_type($result) === 'mysql result') {
mysql_free_result($result);
}
}
/**
* Check if there are any more query results from a multi query
*
* @return bool false
*/
function PMA_DBI_more_results()
{
// N.B.: PHP's 'mysql' extension does not support
// multi_queries so this function will always
// return false. Use the 'mysqli' extension, if
// you need support for multi_queries.
return false;
}
/**
* Prepare next result from multi_query
*
* @return boo false
*/
function PMA_DBI_next_result()
{
// N.B.: PHP's 'mysql' extension does not support
// multi_queries so this function will always
// return false. Use the 'mysqli' extension, if
// you need support for multi_queries.
return false;
}
/**
* Returns a string representing the type of connection used
*
* @param resource $link mysql link
* @return string type of connection used
*/
function PMA_DBI_get_host_info($link = null)
{
if (null === $link) {
if (isset($GLOBALS['userlink'])) {
$link = $GLOBALS['userlink'];
} else {
return false;
}
}
return mysql_get_host_info($link);
}
/**
* Returns the version of the MySQL protocol used
*
* @param resource $link mysql link
* @return int version of the MySQL protocol used
*/
function PMA_DBI_get_proto_info($link = null)
{
if (null === $link) {
if (isset($GLOBALS['userlink'])) {
$link = $GLOBALS['userlink'];
} else {
return false;
}
}
return mysql_get_proto_info($link);
}
/**
* returns a string that represents the client library version
*
* @return string MySQL client library version
*/
function PMA_DBI_get_client_info()
{
return mysql_get_client_info();
}
/**
* returns last error message or false if no errors occured
*
* @param resource $link mysql link
* @return string|bool $error or false
*/
function PMA_DBI_getError($link = null)
{
$GLOBALS['errno'] = 0;
/* Treat false same as null because of controllink */
if ($link === false) {
$link = null;
}
if (null === $link && isset($GLOBALS['userlink'])) {
$link =& $GLOBALS['userlink'];
// Do not stop now. On the initial connection, we don't have a $link,
// we don't have a $GLOBALS['userlink'], but we can catch the error code
// } else {
// return false;
}
if (null !== $link && false !== $link) {
$error_number = mysql_errno($link);
$error_message = mysql_error($link);
} else {
$error_number = mysql_errno();
$error_message = mysql_error();
}
if (0 == $error_number) {
return false;
}
// keep the error number for further check after the call to PMA_DBI_getError()
$GLOBALS['errno'] = $error_number;
return PMA_DBI_formatError($error_number, $error_message);
}
/**
* returns the number of rows returned by last query
*
* @param resource $result
* @return string|int
*/
function PMA_DBI_num_rows($result)
{
if (!is_bool($result)) {
return mysql_num_rows($result);
} else {
return 0;
}
}
/**
* returns last inserted auto_increment id for given $link or $GLOBALS['userlink']
*
* @param resource $link the mysql object
* @return string|int
*/
function PMA_DBI_insert_id($link = null)
{
if (empty($link)) {
if (isset($GLOBALS['userlink'])) {
$link = $GLOBALS['userlink'];
} else {
return false;
}
}
// If the primary key is BIGINT we get an incorrect result
// (sometimes negative, sometimes positive)
// and in the present function we don't know if the PK is BIGINT
// so better play safe and use LAST_INSERT_ID()
//
return PMA_DBI_fetch_value('SELECT LAST_INSERT_ID();', 0, 0, $link);
}
/**
* returns the number of rows affected by last query
*
* @param resource $link the mysql object
* @param bool $get_from_cache
* @return string|int
*/
function PMA_DBI_affected_rows($link = null, $get_from_cache = true)
{
if (empty($link)) {
if (isset($GLOBALS['userlink'])) {
$link = $GLOBALS['userlink'];
} else {
return false;
}
}
if ($get_from_cache) {
return $GLOBALS['cached_affected_rows'];
} else {
return mysql_affected_rows($link);
}
}
/**
* returns metainfo for fields in $result
*
* @todo add missing keys like in mysqli_query (decimals)
* @param resource $result
* @return array meta info for fields in $result
*/
function PMA_DBI_get_fields_meta($result)
{
$fields = array();
$num_fields = mysql_num_fields($result);
for ($i = 0; $i < $num_fields; $i++) {
$field = mysql_fetch_field($result, $i);
$field->flags = mysql_field_flags($result, $i);
$field->orgtable = mysql_field_table($result, $i);
$field->orgname = mysql_field_name($result, $i);
$fields[] = $field;
}
return $fields;
}
/**
* return number of fields in given $result
*
* @param resource $result
* @return int field count
*/
function PMA_DBI_num_fields($result)
{
return mysql_num_fields($result);
}
/**
* returns the length of the given field $i in $result
*
* @param resource $result
* @param int $i field
* @return int length of field
*/
function PMA_DBI_field_len($result, $i)
{
return mysql_field_len($result, $i);
}
/**
* returns name of $i. field in $result
*
* @param resource $result
* @param int $i field
* @return string name of $i. field in $result
*/
function PMA_DBI_field_name($result, $i)
{
return mysql_field_name($result, $i);
}
/**
* returns concatenated string of human readable field flags
*
* @param resource $result
* @param int $i field
* @return string field flags
*/
function PMA_DBI_field_flags($result, $i)
{
return mysql_field_flags($result, $i);
}
?>

View File

@ -0,0 +1,679 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Interface to the improved MySQL extension (MySQLi)
*
* @package PhpMyAdmin-DBI-MySQLi
*/
if (! defined('PHPMYADMIN')) {
exit;
}
require_once './libraries/logging.lib.php';
/**
* MySQL client API
*/
if (!defined('PMA_MYSQL_CLIENT_API')) {
$client_api = explode('.', mysqli_get_client_info());
define('PMA_MYSQL_CLIENT_API', (int)sprintf('%d%02d%02d', $client_api[0], $client_api[1], intval($client_api[2])));
unset($client_api);
}
/**
* some PHP versions are reporting extra messages like "No index used in query"
*/
mysqli_report(MYSQLI_REPORT_OFF);
/**
* some older mysql client libs are missing these constants ...
*/
if (! defined('MYSQLI_BINARY_FLAG')) {
define('MYSQLI_BINARY_FLAG', 128);
}
/**
* @see http://bugs.php.net/36007
*/
if (! defined('MYSQLI_TYPE_NEWDECIMAL')) {
define('MYSQLI_TYPE_NEWDECIMAL', 246);
}
if (! defined('MYSQLI_TYPE_BIT')) {
define('MYSQLI_TYPE_BIT', 16);
}
// for Drizzle
if (! defined('MYSQLI_TYPE_VARCHAR')) {
define('MYSQLI_TYPE_VARCHAR', 15);
}
/**
* Helper function for connecting to the database server
*
* @param mysqli $link
* @param string $host
* @param string $user
* @param string $password
* @param string $dbname
* @param int $server_port
* @param string $server_socket
* @param int $client_flags
* @param bool $persistent
* @return bool
*/
function PMA_DBI_real_connect($link, $host, $user, $password, $dbname, $server_port, $server_socket, $client_flags = null, $persistent = false)
{
global $cfg;
// mysqli persistent connections only on PHP 5.3+
if (PMA_PHP_INT_VERSION >= 50300) {
if ($cfg['PersistentConnections'] || $persistent) {
$host = 'p:' . $host;
}
}
if ($client_flags === null) {
return @mysqli_real_connect(
$link,
$host,
$user,
$password,
$dbname,
$server_port,
$server_socket
);
} else {
return @mysqli_real_connect(
$link,
$host,
$user,
$password,
$dbname,
$server_port,
$server_socket,
$client_flags
);
}
}
/**
* connects to the database server
*
* @param string $user mysql user name
* @param string $password mysql user password
* @param bool $is_controluser
* @param array $server host/port/socket
* @param bool $auxiliary_connection (when true, don't go back to login if connection fails)
* @return mixed false on error or a mysqli object on success
*/
function PMA_DBI_connect($user, $password, $is_controluser = false, $server = null, $auxiliary_connection = false)
{
global $cfg;
if ($server) {
$server_port = (empty($server['port']))
? false
: (int)$server['port'];
$server_socket = (empty($server['socket']))
? ''
: $server['socket'];
$server['host'] = (empty($server['host']))
? 'localhost'
: $server['host'];
} else {
$server_port = (empty($cfg['Server']['port']))
? false
: (int) $cfg['Server']['port'];
$server_socket = (empty($cfg['Server']['socket']))
? null
: $cfg['Server']['socket'];
}
// NULL enables connection to the default socket
$link = mysqli_init();
mysqli_options($link, MYSQLI_OPT_LOCAL_INFILE, true);
$client_flags = 0;
/* Optionally compress connection */
if ($cfg['Server']['compress'] && defined('MYSQLI_CLIENT_COMPRESS')) {
$client_flags |= MYSQLI_CLIENT_COMPRESS;
}
/* Optionally enable SSL */
if ($cfg['Server']['ssl'] && defined('MYSQLI_CLIENT_SSL')) {
$client_flags |= MYSQLI_CLIENT_SSL;
}
if (!$server) {
$return_value = @PMA_DBI_real_connect(
$link,
$cfg['Server']['host'],
$user,
$password,
false,
$server_port,
$server_socket,
$client_flags
);
// Retry with empty password if we're allowed to
if ($return_value == false && isset($cfg['Server']['nopassword']) && $cfg['Server']['nopassword'] && !$is_controluser) {
$return_value = @PMA_DBI_real_connect(
$link,
$cfg['Server']['host'],
$user,
'',
false,
$server_port,
$server_socket,
$client_flags
);
}
} else {
$return_value = @PMA_DBI_real_connect(
$link,
$server['host'],
$user,
$password,
false,
$server_port,
$server_socket
);
}
if ($return_value == false) {
if ($is_controluser) {
trigger_error(
__('Connection for controluser as defined in your configuration failed.'),
E_USER_WARNING
);
return false;
}
// we could be calling PMA_DBI_connect() to connect to another
// server, for example in the Synchronize feature, so do not
// go back to main login if it fails
if (! $auxiliary_connection) {
PMA_log_user($user, 'mysql-denied');
PMA_auth_fails();
} else {
return false;
}
} else {
PMA_DBI_postConnect($link, $is_controluser);
}
return $link;
}
/**
* selects given database
*
* @param string $dbname database name to select
* @param mysqli $link the mysqli object
* @return boolean
*/
function PMA_DBI_select_db($dbname, $link = null)
{
if (empty($link)) {
if (isset($GLOBALS['userlink'])) {
$link = $GLOBALS['userlink'];
} else {
return false;
}
}
return mysqli_select_db($link, $dbname);
}
/**
* runs a query and returns the result
*
* @param string $query query to execute
* @param mysqli $link mysqli object
* @param int $options
* @return mysqli_result|bool
*/
function PMA_DBI_real_query($query, $link, $options)
{
if ($options == ($options | PMA_DBI_QUERY_STORE)) {
$method = MYSQLI_STORE_RESULT;
} elseif ($options == ($options | PMA_DBI_QUERY_UNBUFFERED)) {
$method = MYSQLI_USE_RESULT;
} else {
$method = 0;
}
return mysqli_query($link, $query, $method);
}
/**
* returns array of rows with associative and numeric keys from $result
*
* @param mysqli_result $result
* @return array
*/
function PMA_DBI_fetch_array($result)
{
return mysqli_fetch_array($result, MYSQLI_BOTH);
}
/**
* returns array of rows with associative keys from $result
*
* @param mysqli_result $result
* @return array
*/
function PMA_DBI_fetch_assoc($result)
{
return mysqli_fetch_array($result, MYSQLI_ASSOC);
}
/**
* returns array of rows with numeric keys from $result
*
* @param mysqli_result $result
* @return array
*/
function PMA_DBI_fetch_row($result)
{
return mysqli_fetch_array($result, MYSQLI_NUM);
}
/**
* Adjusts the result pointer to an arbitrary row in the result
*
* @param $result
* @param $offset
* @return bool true on success, false on failure
*/
function PMA_DBI_data_seek($result, $offset)
{
return mysqli_data_seek($result, $offset);
}
/**
* Frees memory associated with the result
*
* @param mysqli_result $result
*/
function PMA_DBI_free_result($result)
{
if ($result instanceof mysqli_result) {
mysqli_free_result($result);
}
}
/**
* Check if there are any more query results from a multi query
*
* @param mysqli $link the mysqli object
* @return bool true or false
*/
function PMA_DBI_more_results($link = null)
{
if (empty($link)) {
if (isset($GLOBALS['userlink'])) {
$link = $GLOBALS['userlink'];
} else {
return false;
}
}
return mysqli_more_results($link);
}
/**
* Prepare next result from multi_query
*
* @param mysqli $link the mysqli object
* @return bool true or false
*/
function PMA_DBI_next_result($link = null)
{
if (empty($link)) {
if (isset($GLOBALS['userlink'])) {
$link = $GLOBALS['userlink'];
} else {
return false;
}
}
return mysqli_next_result($link);
}
/**
* Returns a string representing the type of connection used
*
* @param resource $link mysql link
* @return string type of connection used
*/
function PMA_DBI_get_host_info($link = null)
{
if (null === $link) {
if (isset($GLOBALS['userlink'])) {
$link = $GLOBALS['userlink'];
} else {
return false;
}
}
return mysqli_get_host_info($link);
}
/**
* Returns the version of the MySQL protocol used
*
* @param resource $link mysql link
* @return integer version of the MySQL protocol used
*/
function PMA_DBI_get_proto_info($link = null)
{
if (null === $link) {
if (isset($GLOBALS['userlink'])) {
$link = $GLOBALS['userlink'];
} else {
return false;
}
}
return mysqli_get_proto_info($link);
}
/**
* returns a string that represents the client library version
*
* @return string MySQL client library version
*/
function PMA_DBI_get_client_info()
{
return mysqli_get_client_info();
}
/**
* returns last error message or false if no errors occured
*
* @param resource $link mysql link
* @return string|bool $error or false
*/
function PMA_DBI_getError($link = null)
{
$GLOBALS['errno'] = 0;
/* Treat false same as null because of controllink */
if ($link === false) {
$link = null;
}
if (null === $link && isset($GLOBALS['userlink'])) {
$link =& $GLOBALS['userlink'];
// Do not stop now. We still can get the error code
// with mysqli_connect_errno()
// } else {
// return false;
}
if (null !== $link) {
$error_number = mysqli_errno($link);
$error_message = mysqli_error($link);
} else {
$error_number = mysqli_connect_errno();
$error_message = mysqli_connect_error();
}
if (0 == $error_number) {
return false;
}
// keep the error number for further check after the call to PMA_DBI_getError()
$GLOBALS['errno'] = $error_number;
return PMA_DBI_formatError($error_number, $error_message);
}
/**
* returns the number of rows returned by last query
*
* @param mysqli_result $result
* @return string|int
*/
function PMA_DBI_num_rows($result)
{
// see the note for PMA_DBI_try_query();
if (!is_bool($result)) {
return @mysqli_num_rows($result);
} else {
return 0;
}
}
/**
* returns last inserted auto_increment id for given $link or $GLOBALS['userlink']
*
* @param mysqli $link the mysqli object
* @return string|int
*/
function PMA_DBI_insert_id($link = null)
{
if (empty($link)) {
if (isset($GLOBALS['userlink'])) {
$link = $GLOBALS['userlink'];
} else {
return false;
}
}
// When no controluser is defined, using mysqli_insert_id($link)
// does not always return the last insert id due to a mixup with
// the tracking mechanism, but this works:
return PMA_DBI_fetch_value('SELECT LAST_INSERT_ID();', 0, 0, $link);
// Curiously, this problem does not happen with the mysql extension but
// there is another problem with BIGINT primary keys so PMA_DBI_insert_id()
// in the mysql extension also uses this logic.
}
/**
* returns the number of rows affected by last query
*
* @param mysqli $link the mysqli object
* @param boolean $get_from_cache
* @return string|int
*/
function PMA_DBI_affected_rows($link = null, $get_from_cache = true)
{
if (empty($link)) {
if (isset($GLOBALS['userlink'])) {
$link = $GLOBALS['userlink'];
} else {
return false;
}
}
if ($get_from_cache) {
return $GLOBALS['cached_affected_rows'];
} else {
return mysqli_affected_rows($link);
}
}
/**
* returns metainfo for fields in $result
*
* @param mysqli_result $result
* @return array meta info for fields in $result
*/
function PMA_DBI_get_fields_meta($result)
{
// Build an associative array for a type look up
$typeAr = array();
$typeAr[MYSQLI_TYPE_DECIMAL] = 'real';
$typeAr[MYSQLI_TYPE_NEWDECIMAL] = 'real';
$typeAr[MYSQLI_TYPE_BIT] = 'int';
$typeAr[MYSQLI_TYPE_TINY] = 'int';
$typeAr[MYSQLI_TYPE_SHORT] = 'int';
$typeAr[MYSQLI_TYPE_LONG] = 'int';
$typeAr[MYSQLI_TYPE_FLOAT] = 'real';
$typeAr[MYSQLI_TYPE_DOUBLE] = 'real';
$typeAr[MYSQLI_TYPE_NULL] = 'null';
$typeAr[MYSQLI_TYPE_TIMESTAMP] = 'timestamp';
$typeAr[MYSQLI_TYPE_LONGLONG] = 'int';
$typeAr[MYSQLI_TYPE_INT24] = 'int';
$typeAr[MYSQLI_TYPE_DATE] = 'date';
$typeAr[MYSQLI_TYPE_TIME] = 'time';
$typeAr[MYSQLI_TYPE_DATETIME] = 'datetime';
$typeAr[MYSQLI_TYPE_YEAR] = 'year';
$typeAr[MYSQLI_TYPE_NEWDATE] = 'date';
$typeAr[MYSQLI_TYPE_ENUM] = 'unknown';
$typeAr[MYSQLI_TYPE_SET] = 'unknown';
$typeAr[MYSQLI_TYPE_TINY_BLOB] = 'blob';
$typeAr[MYSQLI_TYPE_MEDIUM_BLOB] = 'blob';
$typeAr[MYSQLI_TYPE_LONG_BLOB] = 'blob';
$typeAr[MYSQLI_TYPE_BLOB] = 'blob';
$typeAr[MYSQLI_TYPE_VAR_STRING] = 'string';
$typeAr[MYSQLI_TYPE_STRING] = 'string';
$typeAr[MYSQLI_TYPE_VARCHAR] = 'string'; // for Drizzle
// MySQL returns MYSQLI_TYPE_STRING for CHAR
// and MYSQLI_TYPE_CHAR === MYSQLI_TYPE_TINY
// so this would override TINYINT and mark all TINYINT as string
// https://sf.net/tracker/?func=detail&aid=1532111&group_id=23067&atid=377408
//$typeAr[MYSQLI_TYPE_CHAR] = 'string';
$typeAr[MYSQLI_TYPE_GEOMETRY] = 'geometry';
$typeAr[MYSQLI_TYPE_BIT] = 'bit';
$fields = mysqli_fetch_fields($result);
// this happens sometimes (seen under MySQL 4.0.25)
if (!is_array($fields)) {
return false;
}
foreach ($fields as $k => $field) {
$fields[$k]->_type = $field->type;
$fields[$k]->type = $typeAr[$field->type];
$fields[$k]->_flags = $field->flags;
$fields[$k]->flags = PMA_DBI_field_flags($result, $k);
// Enhance the field objects for mysql-extension compatibilty
//$flags = explode(' ', $fields[$k]->flags);
//array_unshift($flags, 'dummy');
$fields[$k]->multiple_key
= (int) (bool) ($fields[$k]->_flags & MYSQLI_MULTIPLE_KEY_FLAG);
$fields[$k]->primary_key
= (int) (bool) ($fields[$k]->_flags & MYSQLI_PRI_KEY_FLAG);
$fields[$k]->unique_key
= (int) (bool) ($fields[$k]->_flags & MYSQLI_UNIQUE_KEY_FLAG);
$fields[$k]->not_null
= (int) (bool) ($fields[$k]->_flags & MYSQLI_NOT_NULL_FLAG);
$fields[$k]->unsigned
= (int) (bool) ($fields[$k]->_flags & MYSQLI_UNSIGNED_FLAG);
$fields[$k]->zerofill
= (int) (bool) ($fields[$k]->_flags & MYSQLI_ZEROFILL_FLAG);
$fields[$k]->numeric
= (int) (bool) ($fields[$k]->_flags & MYSQLI_NUM_FLAG);
$fields[$k]->blob
= (int) (bool) ($fields[$k]->_flags & MYSQLI_BLOB_FLAG);
}
return $fields;
}
/**
* return number of fields in given $result
*
* @param mysqli_result $result
* @return int field count
*/
function PMA_DBI_num_fields($result)
{
return mysqli_num_fields($result);
}
/**
* returns the length of the given field $i in $result
*
* @param mysqli_result $result
* @param int $i field
* @return int length of field
*/
function PMA_DBI_field_len($result, $i)
{
return mysqli_fetch_field_direct($result, $i)->length;
}
/**
* returns name of $i. field in $result
*
* @param mysqli_result $result
* @param int $i field
* @return string name of $i. field in $result
*/
function PMA_DBI_field_name($result, $i)
{
return mysqli_fetch_field_direct($result, $i)->name;
}
/**
* returns concatenated string of human readable field flags
*
* @param mysqli_result $result
* @param int $i field
* @return string field flags
*/
function PMA_DBI_field_flags($result, $i)
{
// This is missing from PHP 5.2.5, see http://bugs.php.net/bug.php?id=44846
if (! defined('MYSQLI_ENUM_FLAG')) {
define('MYSQLI_ENUM_FLAG', 256); // see MySQL source include/mysql_com.h
}
$f = mysqli_fetch_field_direct($result, $i);
$type = $f->type;
$charsetnr = $f->charsetnr;
$f = $f->flags;
$flags = '';
if ($f & MYSQLI_UNIQUE_KEY_FLAG) {
$flags .= 'unique ';
}
if ($f & MYSQLI_NUM_FLAG) {
$flags .= 'num ';
}
if ($f & MYSQLI_PART_KEY_FLAG) {
$flags .= 'part_key ';
}
if ($f & MYSQLI_SET_FLAG) {
$flags .= 'set ';
}
if ($f & MYSQLI_TIMESTAMP_FLAG) {
$flags .= 'timestamp ';
}
if ($f & MYSQLI_AUTO_INCREMENT_FLAG) {
$flags .= 'auto_increment ';
}
if ($f & MYSQLI_ENUM_FLAG) {
$flags .= 'enum ';
}
// See http://dev.mysql.com/doc/refman/6.0/en/c-api-datatypes.html:
// to determine if a string is binary, we should not use MYSQLI_BINARY_FLAG
// but instead the charsetnr member of the MYSQL_FIELD
// structure. Watch out: some types like DATE returns 63 in charsetnr
// so we have to check also the type.
// Unfortunately there is no equivalent in the mysql extension.
if (($type == MYSQLI_TYPE_TINY_BLOB || $type == MYSQLI_TYPE_BLOB || $type == MYSQLI_TYPE_MEDIUM_BLOB || $type == MYSQLI_TYPE_LONG_BLOB || $type == MYSQLI_TYPE_VAR_STRING || $type == MYSQLI_TYPE_STRING) && 63 == $charsetnr) {
$flags .= 'binary ';
}
if ($f & MYSQLI_ZEROFILL_FLAG) {
$flags .= 'zerofill ';
}
if ($f & MYSQLI_UNSIGNED_FLAG) {
$flags .= 'unsigned ';
}
if ($f & MYSQLI_BLOB_FLAG) {
$flags .= 'blob ';
}
if ($f & MYSQLI_MULTIPLE_KEY_FLAG) {
$flags .= 'multiple_key ';
}
if ($f & MYSQLI_UNIQUE_KEY_FLAG) {
$flags .= 'unique_key ';
}
if ($f & MYSQLI_PRI_KEY_FLAG) {
$flags .= 'primary_key ';
}
if ($f & MYSQLI_NOT_NULL_FLAG) {
$flags .= 'not_null ';
}
return trim($flags);
}
?>