PHP Class PMA\libraries\DatabaseInterface

Show file Open project: phpmyadmin/phpmyadmin Class Usage Examples

Public Methods

Method Description
__construct ( PMA\libraries\dbi\DBIExtension $ext ) Constructor
affectedRows ( object $link = null, boolean $get_from_cache = true ) : integer | boolean returns the number of rows affected by last query
cacheTableContent ( array $contentPath, mixed $value ) : void Set an item in table cache using dot notation.
checkDbExtension ( string $extension = 'mysql' ) : boolean Checks whether database extension is loaded
clearTableCache ( ) : void Clear the table cache.
connect ( integer $mode, array $server = null ) : mixed connects to the database server
dataSeek ( object $result, integer $offset ) : boolean Adjusts the result pointer to an arbitrary row in the result
escapeString ( string $str, mixed $link = null ) : string returns properly escaped string for use in MySQL queries
fetchArray ( object $result ) : array returns array of rows with associative and numeric keys from $result
fetchAssoc ( object $result ) : array returns array of rows with associative keys from $result
fetchResult ( string $query, string | integer | array $key = null, string | integer $value = null, object $link = null, integer $options ) : array returns all rows in the resultset in one array
fetchRow ( object $result ) : array returns array of rows with numeric keys from $result
fetchSingleRow ( string $query, string $type = 'ASSOC', object $link = null ) : array | boolean returns only the first row from the result
fetchValue ( string $query, integer $row_number, integer | string $field, object $link = null ) : mixed returns a single value from the given result or query, if the query or the result has more than one row or field the first field of the first row is returned
fieldFlags ( object $result, integer $i ) : string returns concatenated string of human readable field flags
fieldLen ( object $result, integer $i ) : integer returns the length of the given field $i in $result
fieldName ( object $result, integer $i ) : string returns name of $i. field in $result
formatError ( integer $error_number, string $error_message ) : string Formats database error message in a friendly way.
freeResult ( object $result ) : void Frees memory associated with the result
getCachedTableContent ( array $contentPath, mixed $default = null ) : mixed Get a cached value from table cache.
getClientInfo ( ) : string returns a string that represents the client library version
getColumnMapFromSql ( string $sql_query, array $view_columns = [] ) : array returns detailed array with all columns for sql
getColumnNames ( string $database, string $table, mixed $link = null ) : null | array Returns all column names in given table
getColumns ( string $database, string $table, string $column = null, boolean $full = false, mixed $link = null ) : array Returns descriptions of columns in given table (all or given by $column)
getColumnsFull ( string $database = null, string $table = null, string $column = null, mixed $link = null ) : array returns detailed array with all columns for given table in database, or all tables/databases
getColumnsSql ( string $database, string $table, string $column = null, boolean $full = false ) : string Returns SQL query for fetching columns for a table
getCompatibilities ( ) : array Get supported SQL compatibility modes
getConnectionParams ( integer $mode, array $server = null ) : array Return connection parameters for the database server
getCurrentUser ( ) : string gets the current user with host
getCurrentUserAndHost ( ) : array Get the current user and host
getDatabasesFull ( string $database = null, boolean $force_stats = false, object $link = null, string $sort_by = 'SCHEMA_NAME', string $sort_order = 'ASC', integer $limit_offset, boolean | integer $limit_count = false ) : array returns array with databases containing extended infos about them
getDbCollation ( string $db ) : string returns collation of given db
getDefinition ( string $db, string $which, string $name, object $link = null ) : string returns the definition of a specific PROCEDURE, FUNCTION, EVENT or VIEW
getError ( object $link = null ) : string | boolean returns last error message or false if no errors occurred
getEvents ( string $db, string $name = '' ) : array returns details about the EVENTs for a specific database
getFieldsMeta ( object $result ) : array returns metainfo for fields in $result
getHostInfo ( object $link = null ) : string Returns a string representing the type of connection used
getKillQuery ( integer $process ) : string Gets SQL for killing a process.
getLink ( $link = null ) * Gets correct link object.
getProceduresOrFunctions ( string $db, string $which, object $link = null ) : array returns an array of PROCEDURE or FUNCTION names for a db
getProtoInfo ( object $link = null ) : integer Returns the version of the MySQL protocol used
getRoutines ( string $db, string $which = null, string $name = '' ) : array returns details about the PROCEDUREs or FUNCTIONs for a specific database or details about a specific routine
getServerCollation ( ) : string returns default server collation from show variables
getSystemDatabase ( ) : SystemDatabase Get the phpmyadmin database manager
getSystemSchemas ( ) : array Get the list of system schemas
getTable ( string $db_name, string $table_name ) : Table Get a table with database name and table name
getTableIndexes ( string $database, string $table, mixed $link = null ) : array Returns indexes of a table
getTableIndexesSql ( string $database, string $table, string $where = null ) : string Returns SQL for fetching information on table indexes (SHOW INDEXES)
getTables ( string $database, mixed $link = null ) : array returns array with table names for given db
getTablesFull ( string $database, string | array $table = '', boolean $tbl_is_group = false, mixed $link = null, integer $limit_offset, boolean | integer $limit_count = false, string $sort_by = 'Name', string $sort_order = 'ASC', string $table_type = null ) : array returns array of all tables in given db or dbs this function expects unquoted names: RIGHT: my_database WRONG: my_database WRONG: my\_database if $tbl_is_group is true, $table is used as filter for table names
getTriggers ( string $db, string $table = '', string $delimiter = '//' ) : array returns details about the TRIGGERs for a specific table or database
getVariable ( string $var, integer $type = self::GETVAR_SESSION, mixed $link = null ) : mixed returns value of given mysql server variable
getVirtualTables ( string $db ) : array Get VIEWs in a particular database
getWarnings ( object $link = null ) : array returns warnings for last query
insertId ( object $link = null ) : integer | boolean returns last inserted auto_increment id for given $link or $GLOBALS['userlink']
isAmazonRds ( ) : boolean Checks if this database server is running on Amazon RDS.
isSuperuser ( ) : boolean Checks if current user is superuser
isSystemSchema ( string $schema_name, boolean $testForMysqlSchema = false ) : boolean Checks whether given schema is a system schema
isUserType ( string $type ) : boolean Checks if current user has global create user/grant privilege or is a superuser (i.e. SELECT on mysql.users) while caching the result in session.
moreResults ( object $link = null ) : boolean Check if there are any more query results from a multi query
nextResult ( object $link = null ) : boolean Prepare next result from multi_query
numFields ( object $result ) : integer return number of fields in given $result
numRows ( object $result ) : string | integer returns the number of rows returned by last query
postConnect ( mixed $link ) : void Function called just after a connection to the MySQL database server has been established. It sets the connection collation, and determines the version of MySQL which is running.
query ( string $query, mixed $link = null, integer $options, boolean $cache_affected_rows = true ) : mixed runs a query
selectDb ( string $dbname, object $link = null ) : boolean selects given database
setVariable ( string $var, string $value, mixed $link = null ) : boolean Sets new value for a variable if it is different from the current value
storeResult ( object $link = null ) : mixed Store the result returned from multi query
tryMultiQuery ( string $multi_query = '', mysqli $link = null ) : mysqli_result Run multi query statement and return results
tryQuery ( string $query, object $link = null, integer $options, boolean $cache_affected_rows = true ) : mixed runs a query and returns the result

Private Methods

Method Description
_cacheTableData ( array $tables, string $table ) : void Caches table data so Table does not require to issue SHOW TABLE STATUS again
_dbgQuery ( string $query, object $link, object | boolean $result, integer $time ) : void Stores query data into session data for debugging purposes
_fetchValue ( array $row, string | null $value ) : mixed Returns row or element of a row
_getSqlForTablesFull ( string[] $this_databases, string $sql_where_table ) : string returns the beginning of the SQL statement to fetch the list of tables
_getTableCondition ( array | string $table, boolean $tbl_is_group, string $table_type ) : string returns a segment of the SQL WHERE clause regarding table name and type
_usortComparisonCallback ( string $a, string $b ) : integer usort comparison callback

Method Details

__construct() public method

Constructor
public __construct ( PMA\libraries\dbi\DBIExtension $ext )
$ext PMA\libraries\dbi\DBIExtension Object to be used for database queries

affectedRows() public method

returns the number of rows affected by last query
public affectedRows ( object $link = null, boolean $get_from_cache = true ) : integer | boolean
$link object the connection object
$get_from_cache boolean whether to retrieve from cache
return integer | boolean

cacheTableContent() public method

Set an item in table cache using dot notation.
public cacheTableContent ( array $contentPath, mixed $value ) : void
$contentPath array Array with the target path
$value mixed Target value
return void

checkDbExtension() public static method

Checks whether database extension is loaded
public static checkDbExtension ( string $extension = 'mysql' ) : boolean
$extension string mysql extension to check
return boolean

clearTableCache() public method

Clear the table cache.
public clearTableCache ( ) : void
return void

connect() public method

connects to the database server
public connect ( integer $mode, array $server = null ) : mixed
$mode integer Connection mode on of CONNECT_USER, CONNECT_CONTROL or CONNECT_AUXILIARY.
$server array Server information like host/port/socket/persistent
return mixed false on error or a connection object on success

dataSeek() public method

Adjusts the result pointer to an arbitrary row in the result
public dataSeek ( object $result, integer $offset ) : boolean
$result object database result
$offset integer offset to seek
return boolean true on success, false on failure

escapeString() public method

returns properly escaped string for use in MySQL queries
public escapeString ( string $str, mixed $link = null ) : string
$str string string to be escaped
$link mixed optional database link to use
return string a MySQL escaped string

fetchArray() public method

returns array of rows with associative and numeric keys from $result
public fetchArray ( object $result ) : array
$result object result set identifier
return array

fetchAssoc() public method

returns array of rows with associative keys from $result
public fetchAssoc ( object $result ) : array
$result object result set identifier
return array

fetchResult() public method

$sql = 'SELECT * FROM user'; $users = $GLOBALS['dbi']->fetchResult($sql); produces $users[] = array('id' => 123, 'name' => 'John Doe') $sql = 'SELECT id, name FROM user'; $users = $GLOBALS['dbi']->fetchResult($sql, 'id'); produces $users['123'] = array('id' => 123, 'name' => 'John Doe') $sql = 'SELECT id, name FROM user'; $users = $GLOBALS['dbi']->fetchResult($sql, 0); produces $users['123'] = array(0 => 123, 1 => 'John Doe') $sql = 'SELECT id, name FROM user'; $users = $GLOBALS['dbi']->fetchResult($sql, 'id', 'name'); or $users = $GLOBALS['dbi']->fetchResult($sql, 0, 1); produces $users['123'] = 'John Doe' $sql = 'SELECT name FROM user'; $users = $GLOBALS['dbi']->fetchResult($sql); produces $users[] = 'John Doe' $sql = 'SELECT group, name FROM user' $users = $GLOBALS['dbi']->fetchResult($sql, array('group', null), 'name'); produces $users['admin'][] = 'John Doe' $sql = 'SELECT group, name FROM user' $users = $GLOBALS['dbi']->fetchResult($sql, array('group', 'name'), 'id'); produces $users['admin']['John Doe'] = '123'
public fetchResult ( string $query, string | integer | array $key = null, string | integer $value = null, object $link = null, integer $options ) : array
$query string query to execute
$key string | integer | array field-name or offset used as key for array or array of those
$value string | integer value-name or offset used as value for array
$link object mysql link
$options integer query options
return array resultrows or values indexed by $key

fetchRow() public method

returns array of rows with numeric keys from $result
public fetchRow ( object $result ) : array
$result object result set identifier
return array

fetchSingleRow() public method

$sql = 'SELECT * FROM user WHERE id = 123'; $user = $GLOBALS['dbi']->fetchSingleRow($sql); produces $user = array('id' => 123, 'name' => 'John Doe')
public fetchSingleRow ( string $query, string $type = 'ASSOC', object $link = null ) : array | boolean
$query string The query to execute
$type string NUM|ASSOC|BOTH returned array should either numeric associative or both
$link object mysql link
return array | boolean first row from result or false if result is empty

fetchValue() public method

$sql = 'SELECT name FROM user WHERE id = 123'; $user_name = $GLOBALS['dbi']->fetchValue($sql); produces $user_name = 'John Doe'
public fetchValue ( string $query, integer $row_number, integer | string $field, object $link = null ) : mixed
$query string The query to execute
$row_number integer row to fetch the value from, starting at 0, with 0 being default
$field integer | string field to fetch the value from, starting at 0, with 0 being default
$link object mysql link
return mixed value of first field in first row from result or false if not found

fieldFlags() public method

returns concatenated string of human readable field flags
public fieldFlags ( object $result, integer $i ) : string
$result object result set identifier
$i integer field
return string field flags

fieldLen() public method

returns the length of the given field $i in $result
public fieldLen ( object $result, integer $i ) : integer
$result object result set identifier
$i integer field
return integer length of field

fieldName() public method

returns name of $i. field in $result
public fieldName ( object $result, integer $i ) : string
$result object result set identifier
$i integer field
return string name of $i. field in $result

formatError() public method

This is needed because some errors messages cannot be obtained by mysql_error().
public formatError ( integer $error_number, string $error_message ) : string
$error_number integer Error code
$error_message string Error message as returned by server
return string HML text with error details

freeResult() public method

Frees memory associated with the result
public freeResult ( object $result ) : void
$result object database result
return void

getCachedTableContent() public method

Get a cached value from table cache.
public getCachedTableContent ( array $contentPath, mixed $default = null ) : mixed
$contentPath array Array of the name of the target value
$default mixed Return value on cache miss
return mixed cached value or default

getClientInfo() public method

returns a string that represents the client library version
public getClientInfo ( ) : string
return string MySQL client library version

getColumnMapFromSql() public method

returns detailed array with all columns for sql
public getColumnMapFromSql ( string $sql_query, array $view_columns = [] ) : array
$sql_query string target SQL query to get columns
$view_columns array alias for columns
return array

getColumnNames() public method

Returns all column names in given table
public getColumnNames ( string $database, string $table, mixed $link = null ) : null | array
$database string name of database
$table string name of table to retrieve columns from
$link mixed mysql link resource
return null | array

getColumns() public method

Returns descriptions of columns in given table (all or given by $column)
public getColumns ( string $database, string $table, string $column = null, boolean $full = false, mixed $link = null ) : array
$database string name of database
$table string name of table to retrieve columns from
$column string name of column, null to show all columns
$full boolean whether to return full info or only column names
$link mixed mysql link resource
return array array indexed by column names or, if $column is given, flat array description

getColumnsFull() public method

returns detailed array with all columns for given table in database, or all tables/databases
public getColumnsFull ( string $database = null, string $table = null, string $column = null, mixed $link = null ) : array
$database string name of database
$table string name of table to retrieve columns from
$column string name of specific column
$link mixed mysql link resource
return array

getColumnsSql() public method

The 'Key' column is not calculated properly, use $GLOBALS['dbi']->getColumns() to get correct values.
See also: getColumns()
public getColumnsSql ( string $database, string $table, string $column = null, boolean $full = false ) : string
$database string name of database
$table string name of table to retrieve columns from
$column string name of column, null to show all columns
$full boolean whether to return full info or only column names
return string

getCompatibilities() public method

Get supported SQL compatibility modes
public getCompatibilities ( ) : array
return array supported SQL compatibility modes

getConnectionParams() public method

Return connection parameters for the database server
public getConnectionParams ( integer $mode, array $server = null ) : array
$mode integer Connection mode on of CONNECT_USER, CONNECT_CONTROL or CONNECT_AUXILIARY.
$server array Server information like host/port/socket/persistent
return array user, host and server settings array

getCurrentUser() public method

gets the current user with host
public getCurrentUser ( ) : string
return string the current user i.e. user@host

getCurrentUserAndHost() public method

Get the current user and host
public getCurrentUserAndHost ( ) : array
return array array of username and hostname

getDatabasesFull() public method

returns array with databases containing extended infos about them
public getDatabasesFull ( string $database = null, boolean $force_stats = false, object $link = null, string $sort_by = 'SCHEMA_NAME', string $sort_order = 'ASC', integer $limit_offset, boolean | integer $limit_count = false ) : array
$database string database
$force_stats boolean retrieve stats also for MySQL < 5
$link object mysql link
$sort_by string column to order by
$sort_order string ASC or DESC
$limit_offset integer starting offset for LIMIT
$limit_count boolean | integer row count for LIMIT or true for $GLOBALS['cfg']['MaxDbList']
return array $databases

getDbCollation() public method

returns collation of given db
public getDbCollation ( string $db ) : string
$db string name of db
return string collation of $db

getDefinition() public method

returns the definition of a specific PROCEDURE, FUNCTION, EVENT or VIEW
public getDefinition ( string $db, string $which, string $name, object $link = null ) : string
$db string db name
$which string PROCEDURE | FUNCTION | EVENT | VIEW
$name string the procedure|function|event|view name
$link object MySQL link
return string the definition

getError() public method

returns last error message or false if no errors occurred
public getError ( object $link = null ) : string | boolean
$link object connection link
return string | boolean $error or false

getEvents() public method

returns details about the EVENTs for a specific database
public getEvents ( string $db, string $name = '' ) : array
$db string db name
$name string event name
return array information about EVENTs

getFieldsMeta() public method

returns metainfo for fields in $result
public getFieldsMeta ( object $result ) : array
$result object result set identifier
return array meta info for fields in $result

getHostInfo() public method

Returns a string representing the type of connection used
public getHostInfo ( object $link = null ) : string
$link object mysql link
return string type of connection used

getKillQuery() public method

Gets SQL for killing a process.
public getKillQuery ( integer $process ) : string
$process integer Process ID
return string

getProceduresOrFunctions() public method

returns an array of PROCEDURE or FUNCTION names for a db
public getProceduresOrFunctions ( string $db, string $which, object $link = null ) : array
$db string db name
$which string PROCEDURE | FUNCTION
$link object mysql link
return array the procedure names or function names

getProtoInfo() public method

Returns the version of the MySQL protocol used
public getProtoInfo ( object $link = null ) : integer
$link object mysql link
return integer version of the MySQL protocol used

getRoutines() public method

returns details about the PROCEDUREs or FUNCTIONs for a specific database or details about a specific routine
public getRoutines ( string $db, string $which = null, string $name = '' ) : array
$db string db name
$which string PROCEDURE | FUNCTION or null for both
$name string name of the routine (to fetch a specific routine)
return array information about ROCEDUREs or FUNCTIONs

getServerCollation() public method

returns default server collation from show variables
public getServerCollation ( ) : string
return string $server_collation

getSystemDatabase() public method

Get the phpmyadmin database manager
public getSystemDatabase ( ) : SystemDatabase
return SystemDatabase

getSystemSchemas() public method

Get the list of system schemas
public getSystemSchemas ( ) : array
return array list of system schemas

getTable() public method

Get a table with database name and table name
public getTable ( string $db_name, string $table_name ) : Table
$db_name string DB name
$table_name string Table name
return Table

getTableIndexes() public method

Returns indexes of a table
public getTableIndexes ( string $database, string $table, mixed $link = null ) : array
$database string name of database
$table string name of the table whose indexes are to be retrieved
$link mixed mysql link resource
return array $indexes

getTableIndexesSql() public method

Returns SQL for fetching information on table indexes (SHOW INDEXES)
public getTableIndexesSql ( string $database, string $table, string $where = null ) : string
$database string name of database
$table string name of the table whose indexes are to be retrieved
$where string additional conditions for WHERE
return string SQL for getting indexes

getTables() public method

returns array with table names for given db
public getTables ( string $database, mixed $link = null ) : array
$database string name of database
$link mixed mysql link resource|object
return array tables names

getTablesFull() public method

$GLOBALS['dbi']->getTablesFull('my_database'); $GLOBALS['dbi']->getTablesFull('my_database', 'my_table')); $GLOBALS['dbi']->getTablesFull('my_database', 'my_tables_', true));
public getTablesFull ( string $database, string | array $table = '', boolean $tbl_is_group = false, mixed $link = null, integer $limit_offset, boolean | integer $limit_count = false, string $sort_by = 'Name', string $sort_order = 'ASC', string $table_type = null ) : array
$database string database
$table string | array table name(s)
$tbl_is_group boolean $table is a table group
$link mixed mysql link
$limit_offset integer zero-based offset for the count
$limit_count boolean | integer number of tables to return
$sort_by string table attribute to sort by
$sort_order string direction to sort (ASC or DESC)
$table_type string whether table or view
return array list of tables in given db(s)

getTriggers() public method

returns details about the TRIGGERs for a specific table or database
public getTriggers ( string $db, string $table = '', string $delimiter = '//' ) : array
$db string db name
$table string table name
$delimiter string the delimiter to use (may be empty)
return array information about triggers (may be empty)

getVariable() public method

returns value of given mysql server variable
public getVariable ( string $var, integer $type = self::GETVAR_SESSION, mixed $link = null ) : mixed
$var string mysql server variable name
$type integer DatabaseInterface::GETVAR_SESSION | DatabaseInterface::GETVAR_GLOBAL
$link mixed mysql link resource|object
return mixed value for mysql server variable

getVirtualTables() public method

Get VIEWs in a particular database
public getVirtualTables ( string $db ) : array
$db string Database name to look in
return array $views Set of VIEWs inside the database

getWarnings() public method

returns warnings for last query
public getWarnings ( object $link = null ) : array
$link object mysql link resource
return array warnings

insertId() public method

returns last inserted auto_increment id for given $link or $GLOBALS['userlink']
public insertId ( object $link = null ) : integer | boolean
$link object the connection object
return integer | boolean

isAmazonRds() public method

Checks if this database server is running on Amazon RDS.
public isAmazonRds ( ) : boolean
return boolean

isSuperuser() public method

Checks if current user is superuser
public isSuperuser ( ) : boolean
return boolean Whether user is a superuser

isSystemSchema() public method

Checks whether given schema is a system schema
public isSystemSchema ( string $schema_name, boolean $testForMysqlSchema = false ) : boolean
$schema_name string Name of schema (database) to test
$testForMysqlSchema boolean Whether 'mysql' schema should be treated the same as IS and DD
return boolean

isUserType() public method

Checks if current user has global create user/grant privilege or is a superuser (i.e. SELECT on mysql.users) while caching the result in session.
public isUserType ( string $type ) : boolean
$type string type of user to check for i.e. 'create', 'grant', 'super'
return boolean Whether user is a given type of user

moreResults() public method

Check if there are any more query results from a multi query
public moreResults ( object $link = null ) : boolean
$link object the connection object
return boolean true or false

nextResult() public method

Prepare next result from multi_query
public nextResult ( object $link = null ) : boolean
$link object the connection object
return boolean true or false

numFields() public method

return number of fields in given $result
public numFields ( object $result ) : integer
$result object result set identifier
return integer field count

numRows() public method

returns the number of rows returned by last query
public numRows ( object $result ) : string | integer
$result object result set identifier
return string | integer

postConnect() public method

Function called just after a connection to the MySQL database server has been established. It sets the connection collation, and determines the version of MySQL which is running.
public postConnect ( mixed $link ) : void
$link mixed mysql link resource|object
return void

query() public method

runs a query
public query ( string $query, mixed $link = null, integer $options, boolean $cache_affected_rows = true ) : mixed
$query string SQL query to execute
$link mixed optional database link to use
$options integer optional query options
$cache_affected_rows boolean whether to cache affected rows
return mixed

selectDb() public method

selects given database
public selectDb ( string $dbname, object $link = null ) : boolean
$dbname string database name to select
$link object connection object
return boolean

setVariable() public method

Sets new value for a variable if it is different from the current value
public setVariable ( string $var, string $value, mixed $link = null ) : boolean
$var string variable name
$value string value to set
$link mixed mysql link resource|object
return boolean whether query was a successful

storeResult() public method

Store the result returned from multi query
public storeResult ( object $link = null ) : mixed
$link object the connection object
return mixed false when empty results / result set when not empty

tryMultiQuery() public method

Run multi query statement and return results
public tryMultiQuery ( string $multi_query = '', mysqli $link = null ) : mysqli_result
$multi_query string multi query statement to execute
$link mysqli mysqli object
return mysqli_result collection | boolean(false)

tryQuery() public method

runs a query and returns the result
public tryQuery ( string $query, object $link = null, integer $options, boolean $cache_affected_rows = true ) : mixed
$query string query to run
$link object mysql link resource
$options integer query options
$cache_affected_rows boolean whether to cache affected row
return mixed