Dieser Commit ist enthalten in:
Ursprung
2b21070b1a
Commit
f7a7c71f86
1583 geänderte Dateien mit 454759 neuen und 0 gelöschten Zeilen
240
library/Msd/Db/Mysql.php
Normale Datei
240
library/Msd/Db/Mysql.php
Normale Datei
|
|
@ -0,0 +1,240 @@
|
|||
<?php
|
||||
/**
|
||||
* This file is part of MySQLDumper released under the GNU/GPL 2 license
|
||||
* http://www.mysqldumper.net
|
||||
*
|
||||
* @package MySQLDumper
|
||||
* @subpackage Db
|
||||
* @version SVN: $rev: 1205 $
|
||||
* @author $Author$
|
||||
*/
|
||||
|
||||
/**
|
||||
* Capsules all database related actions.
|
||||
*
|
||||
* @package MySQLDumper
|
||||
* @subpackage Db
|
||||
*/
|
||||
class Msd_Db_Mysql extends Msd_Db_MysqlCommon
|
||||
{
|
||||
/**
|
||||
* Mysql connection handle
|
||||
*
|
||||
* @var resource
|
||||
*/
|
||||
protected $_connectionHandle = null;
|
||||
|
||||
/**
|
||||
* Mysql result handle
|
||||
*
|
||||
* @var resource
|
||||
*/
|
||||
protected $_resultHandle = null;
|
||||
|
||||
/**
|
||||
* Establish a connection to MySQL.
|
||||
*
|
||||
* Creates a connection to the database and stores the connection handle in
|
||||
* $this->_connectionHandle.
|
||||
* Returns true on success or false if connection couldn't be established.
|
||||
*
|
||||
* @throws Exception
|
||||
* @return bool
|
||||
**/
|
||||
protected function _dbConnect()
|
||||
{
|
||||
if (is_resource($this->_connectionHandle)) {
|
||||
return true;
|
||||
}
|
||||
if ($this->_port == 0) {
|
||||
$this->_port = 3306;
|
||||
}
|
||||
$connectionString = $this->_server . ':' . $this->_port;
|
||||
if ($this->_socket != '') {
|
||||
$connectionString .= ':' . $this->_socket;
|
||||
}
|
||||
$this->_connectionHandle = @mysql_connect(
|
||||
$connectionString,
|
||||
$this->_user, $this->_password
|
||||
);
|
||||
if (false === $this->_connectionHandle) {
|
||||
throw new Msd_Exception(mysql_error(), mysql_errno());
|
||||
}
|
||||
$this->setConnectionCharset();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the connection handle if already established or creates one.
|
||||
*
|
||||
* @return resource The connection handle
|
||||
*/
|
||||
private function _getHandle()
|
||||
{
|
||||
if (!is_resource($this->_connectionHandle)) {
|
||||
$this->_dbConnect();
|
||||
}
|
||||
return $this->_connectionHandle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return version nr of MySql server.
|
||||
*
|
||||
* @return string Version number
|
||||
*/
|
||||
public function getServerInfo()
|
||||
{
|
||||
return mysql_get_server_info($this->_getHandle());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get version nr of MySql client.
|
||||
*
|
||||
* @return string Version nr
|
||||
*/
|
||||
public function getClientInfo()
|
||||
{
|
||||
return mysql_get_client_info();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the character set of the MySQL-connection.
|
||||
*
|
||||
* Trys to set the connection charset and returns it.
|
||||
*
|
||||
* @param string $charset The wanted charset of the connection
|
||||
*
|
||||
* @return string The set charset
|
||||
*/
|
||||
public function setConnectionCharset($charset = 'utf8')
|
||||
{
|
||||
if (function_exists('mysql_set_charset')
|
||||
&& @mysql_set_charset($charset, $this->_getHandle())) {
|
||||
$this->_connectionCharset = $charset;
|
||||
return $this->_connectionCharset;
|
||||
} else {
|
||||
$this->query('SET NAMES \'' . $charset . '\'', self::SIMPLE);
|
||||
$this->_connectionCharset = $charset;
|
||||
return $charset;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Select the given database to use it as the target for following queries
|
||||
*
|
||||
* Returns true if selection was succesfull otherwise false.
|
||||
*
|
||||
* @throws Exception
|
||||
* @param string $database The database to select
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function selectDb($database)
|
||||
{
|
||||
$res = @mysql_select_db($database, $this->_getHandle());
|
||||
if ($res === false) {
|
||||
throw new Msd_Exception(mysql_error(), mysql_errno());
|
||||
}
|
||||
$this->_dbSelected = $database;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Execute a query and set _resultHandle
|
||||
*
|
||||
* If $getRows is true all rows are fetched and returned.
|
||||
* If $getRows is false, query will be executed, but the result handle
|
||||
* is returned.
|
||||
*
|
||||
* @param string $query The query to execute
|
||||
* @param const $kind Type of result set
|
||||
* @param boolean $getRows Wether to fetch all rows and return them
|
||||
*
|
||||
* @return boolean|array
|
||||
*/
|
||||
public function query($query, $kind = self::ARRAY_OBJECT, $getRows = true)
|
||||
{
|
||||
$this->_resultHandle = @mysql_query($query, $this->_getHandle());
|
||||
if (false === $this->_resultHandle) {
|
||||
$this->sqlError(
|
||||
mysql_error($this->_connectionHandle),
|
||||
mysql_errno($this->_connectionHandle)
|
||||
);
|
||||
}
|
||||
if ($kind === self::SIMPLE || is_bool($this->_resultHandle)) {
|
||||
return $this->_resultHandle;
|
||||
}
|
||||
|
||||
// return result set?
|
||||
if ($getRows) {
|
||||
$ret = array();
|
||||
WHILE ($row = $this->getNextRow($kind)) {
|
||||
$ret[] = $row;
|
||||
}
|
||||
$this->_resultHandle = null;
|
||||
return $ret;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get next row from a result set that is returned by $this->query().
|
||||
*
|
||||
* Can be used to walk through result sets.
|
||||
*
|
||||
* @param const $kind
|
||||
*
|
||||
* @return array|object
|
||||
*/
|
||||
public function getNextRow($kind)
|
||||
{
|
||||
switch ($kind)
|
||||
{
|
||||
case self::ARRAY_OBJECT:
|
||||
return mysql_fetch_object($this->_resultHandle);
|
||||
break;
|
||||
case self::ARRAY_NUMERIC:
|
||||
return mysql_fetch_array($this->_resultHandle, MYSQL_NUM);
|
||||
break;
|
||||
case self::ARRAY_ASSOC:
|
||||
return mysql_fetch_array($this->_resultHandle, MYSQL_ASSOC);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the number of affected rows for the last query.
|
||||
*
|
||||
* @see inc/classes/db/MsdDbFactory#affectedRows()
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getAffectedRows()
|
||||
{
|
||||
return mysql_affected_rows($this->_getHandle());
|
||||
}
|
||||
|
||||
/**
|
||||
* Escape a value with real_escape_string() to use it in a query.
|
||||
*
|
||||
* @see inc/classes/db/MsdDbFactory#escape($val)
|
||||
* @param mixed $val The value to escape
|
||||
* @return mixed
|
||||
*/
|
||||
public function escape($val)
|
||||
{
|
||||
return mysql_real_escape_string($val, $this->_getHandle());
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the last MySQL error.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function getLastError()
|
||||
{
|
||||
return array(
|
||||
'code' => mysql_errno($this->_getHandle()),
|
||||
'message' => mysql_error($this->_getHandle()),
|
||||
);
|
||||
}
|
||||
}
|
||||
533
library/Msd/Db/MysqlCommon.php
Normale Datei
533
library/Msd/Db/MysqlCommon.php
Normale Datei
|
|
@ -0,0 +1,533 @@
|
|||
<?php
|
||||
/**
|
||||
* This file is part of MySQLDumper released under the GNU/GPL 2 license
|
||||
* http://www.mysqldumper.net
|
||||
*
|
||||
* @package MySQLDumper
|
||||
* @subpackage Db
|
||||
* @version SVN: $rev: 1205 $
|
||||
* @author $Author$
|
||||
*/
|
||||
|
||||
/**
|
||||
* Class offers some db related methods that are equal for Mysql and MySQLi.
|
||||
*
|
||||
* @package MySQLDumper
|
||||
* @subpackage Db
|
||||
*/
|
||||
abstract class Msd_Db_MysqlCommon extends Msd_Db
|
||||
{
|
||||
/**
|
||||
* Get the list of table and view names of given database
|
||||
*
|
||||
* @param string $dbName Name of database
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getTables($dbName)
|
||||
{
|
||||
$tables = array();
|
||||
$sql = 'SHOW TABLES FROM `' . $dbName . '`';
|
||||
$res = $this->query($sql, self::ARRAY_NUMERIC);
|
||||
foreach ($res as $val) {
|
||||
$tables[] = $val[0];
|
||||
}
|
||||
return $tables;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of tables of the given database. The result include tables
|
||||
* meta data.
|
||||
*
|
||||
* @param string $dbName Name of database
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getTablesMeta($dbName)
|
||||
{
|
||||
$tablesSql = 'SELECT * FROM `information_schema`.`TABLES` '
|
||||
. 'WHERE `TABLE_SCHEMA` = \'' . $this->escape($dbName) . '\'';
|
||||
$rawTables = $this->query($tablesSql, self::ARRAY_ASSOC);
|
||||
$tables = array();
|
||||
foreach ($rawTables as $rawTable) {
|
||||
$tables[$rawTable['TABLE_NAME']] = $rawTable;
|
||||
}
|
||||
return $tables;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get information of databases
|
||||
*
|
||||
* Gets list and info of all databases that the actual MySQL-User can access
|
||||
* and saves it in $this->databases.
|
||||
*
|
||||
* @param bool $addViews If set nr of views and routines are added
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getDatabases($addViews = false)
|
||||
{
|
||||
$query = 'SELECT `is`.*, count(`t`.`TABLE_NAME`) `tables`'
|
||||
. ' FROM `information_schema`.`SCHEMATA` `is`'
|
||||
. ' LEFT JOIN `information_schema`.`TABLES` `t` '
|
||||
. ' ON `t`.`TABLE_SCHEMA` = `is`.`SCHEMA_NAME`'
|
||||
. ' GROUP BY `is`.`SCHEMA_NAME`'
|
||||
. ' ORDER BY `is`.`SCHEMA_NAME` ASC';
|
||||
$res = $this->query($query, self::ARRAY_ASSOC, true);
|
||||
if ($addViews) {
|
||||
$views = $this->getNrOfViews();
|
||||
$routines = $this->getNrOfRoutines();
|
||||
$sizes = $this->getDatabaseSizes();
|
||||
}
|
||||
foreach ($res as $row) {
|
||||
$database = $row['SCHEMA_NAME'];
|
||||
if ($addViews) {
|
||||
$row['views'] = 0;
|
||||
$row['routines'] = 0;
|
||||
$row['size'] = 0;
|
||||
$row[$database] = 0;
|
||||
if (isset($sizes[$database])) {
|
||||
$row['size'] = $sizes[$database];
|
||||
}
|
||||
// add views
|
||||
if (isset($views[$database])) {
|
||||
$row['views'] = $views[$database];
|
||||
$row['tables'] -= $views[$database];
|
||||
}
|
||||
// add routines
|
||||
if (isset($routines[$database])) {
|
||||
$row['routines'] = $routines[$database];
|
||||
}
|
||||
}
|
||||
unset($row['SCHEMA_NAME']);
|
||||
$this->_databases[$database] = $row;
|
||||
}
|
||||
return $this->_databases;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return assoc array with the names of accessable databases
|
||||
*
|
||||
* @return array Assoc array with database names
|
||||
*/
|
||||
public function getDatabaseNames()
|
||||
{
|
||||
if ($this->_databases == null) {
|
||||
$this->getDatabases();
|
||||
}
|
||||
return array_keys($this->_databases);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the actual selected database.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getSelectedDb()
|
||||
{
|
||||
return $this->_dbSelected;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the CREATE Statement of a table.
|
||||
*
|
||||
* @param string $table
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getTableCreate($table)
|
||||
{
|
||||
$sql = 'SHOW CREATE TABLE `' . $table . '`';
|
||||
$res = $this->query($sql, self::ARRAY_ASSOC);
|
||||
return $res[0]['Create Table'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the full description of all columns of a table.
|
||||
*
|
||||
* Saves it to $this->metaTables[$database][$table].
|
||||
*
|
||||
* @param string $table
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getTableColumns($table)
|
||||
{
|
||||
$dbName = $this->getSelectedDb();
|
||||
$sql = 'SHOW FULL FIELDS FROM `' . $table . '`';
|
||||
$res = $this->query($sql, self::ARRAY_ASSOC);
|
||||
if (!isset($this->_metaTables[$dbName])) {
|
||||
$this->_metaTables[$dbName] = array();
|
||||
}
|
||||
if (is_array($res)) {
|
||||
$this->_metaTables[$dbName][$table] = array();
|
||||
foreach ($res as $r) {
|
||||
$this->_metaTables[$dbName][$table][$r['Field']] = $r;
|
||||
}
|
||||
}
|
||||
return $this->_metaTables[$dbName][$table];
|
||||
}
|
||||
|
||||
/**
|
||||
* Optimize given table.
|
||||
*
|
||||
* Returns false on error or Sql's Msg_text if query succeeds.
|
||||
*
|
||||
* @param string $table Name of table
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function optimizeTable($table)
|
||||
{
|
||||
return $this->_executeMaintainAction('OPTIMIZE', $table);
|
||||
}
|
||||
|
||||
/**
|
||||
* Analyze given table.
|
||||
*
|
||||
* Returns false on error or Sql's Msg_text if query succeeds.
|
||||
*
|
||||
* @param string $table Name of table
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function analyzeTable($table)
|
||||
{
|
||||
return $this->_executeMaintainAction('ANALYZE', $table);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check given table.
|
||||
*
|
||||
* Returns false on error or Sql's Msg_text if query succeeds.
|
||||
*
|
||||
* @param string $table Name of table
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function checkTable($table)
|
||||
{
|
||||
return $this->_executeMaintainAction('CHECK', $table);
|
||||
}
|
||||
|
||||
/**
|
||||
* Repair given table.
|
||||
*
|
||||
* Returns false on error or Sql's Msg_text if query succeeds.
|
||||
*
|
||||
* @param string $table Name of table
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function repairTable($table)
|
||||
{
|
||||
return $this->_executeMaintainAction('REPAIR', $table);
|
||||
}
|
||||
|
||||
/**
|
||||
* Truncate a table (delete all records)
|
||||
*
|
||||
* @param string $table The tabel to truncate
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function truncateTable($table)
|
||||
{
|
||||
$sql = 'TRUNCATE `' . $this->escape($table) . '`';
|
||||
$res = $this->query($sql, self::SIMPLE);
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute maintaining action on a table (optimize, analyze, check, repair)
|
||||
*
|
||||
* @param string $action Action to perform
|
||||
*
|
||||
* @return array Result array conataining messages
|
||||
*/
|
||||
private function _executeMaintainAction($action, $table)
|
||||
{
|
||||
$sql = $action . ' TABLE `' . $this->escape($table) . '`';
|
||||
try {
|
||||
$res = $this->query($sql, Msd_Db::ARRAY_ASSOC);
|
||||
if (isset($res[0]['Msg_text'])) {
|
||||
return $res[0];
|
||||
}
|
||||
} catch (Msd_Exception $e) {
|
||||
unset($e);
|
||||
}
|
||||
$ret = array('Table' => $table);
|
||||
return array_merge($ret, $this->getLastError());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get list of known charsets from MySQL-Server.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getCharsets()
|
||||
{
|
||||
if (!empty($this->_charsets)) {
|
||||
return $this->_charsets;
|
||||
}
|
||||
$sql = 'SELECT * FROM `information_schema`.`CHARACTER_SETS` ORDER BY `CHARACTER_SET_NAME`';
|
||||
$result = $this->query($sql, self::ARRAY_ASSOC);
|
||||
$this->_charsets = array();
|
||||
foreach ($result as $res) {
|
||||
$this->_charsets[$res['CHARACTER_SET_NAME']] = $res;
|
||||
}
|
||||
return $this->_charsets;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets extended table information for one or all tables.
|
||||
*
|
||||
* @param string | false $tableName
|
||||
* @param string | false $databaseName
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getTableStatus($tableName = false, $databaseName = false)
|
||||
{
|
||||
if ($databaseName === false) {
|
||||
$databaseName = $this->getSelectedDb();
|
||||
}
|
||||
$sql = "SELECT * FROM `information_schema`.`TABLES` WHERE "
|
||||
. "`TABLE_SCHEMA`='" . $this->escape($databaseName) . "'";
|
||||
if ($tableName !== false) {
|
||||
$sql .= " AND `TABLE_NAME` LIKE '" . $this->escape($tableName) . "'";
|
||||
}
|
||||
$res = $this->query($sql, self::ARRAY_ASSOC);
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get variables of SQL-Server and return them as assoc array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getVariables()
|
||||
{
|
||||
$ret = array();
|
||||
$variables = $this->query('SHOW VARIABLES', Msd_Db::ARRAY_ASSOC);
|
||||
foreach ($variables as $val) {
|
||||
$ret[$val['Variable_name']] = $val['Value'];
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get global status variables of SQL-Server and return them as assoc array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getGlobalStatus()
|
||||
{
|
||||
$ret = array();
|
||||
$variables = $this->query('SHOW GLOBAL STATUS', Msd_Db::ARRAY_ASSOC);
|
||||
foreach ($variables as $val) {
|
||||
$ret[$val['Variable_name']] = $val['Value'];
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of records of a table by query SELECT COUNT(*)
|
||||
*
|
||||
* @param string $tableName The name of the table
|
||||
* @param string $dbName The name of the database
|
||||
*
|
||||
* @return integer The number of rows isnide table
|
||||
*/
|
||||
public function getNrOfRowsBySelectCount($tableName, $dbName = null)
|
||||
{
|
||||
if ($dbName === null) {
|
||||
$dbName = $this->getSelectedDb();
|
||||
}
|
||||
;
|
||||
$sql = 'SELECT COUNT(*) as `Rows` FROM `%s`.`%s`';
|
||||
$sql = sprintf($sql, $this->escape($dbName), $this->escape($tableName));
|
||||
$rows = $this->query($sql, Msd_Db::ARRAY_ASSOC);
|
||||
return (int)$rows[0]['Rows'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the collations from information schema.
|
||||
*
|
||||
* @param string|null $charsetName Name of the charset
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getCollations($charsetName = null)
|
||||
{
|
||||
$where = "";
|
||||
if (!empty($charsetName)) {
|
||||
$where = "WHERE `CHARACTER_SET_NAME` = '" . $this->escape($charsetName) . "'";
|
||||
}
|
||||
$collationSql = "SELECT `CHARACTER_SET_NAME` `charset`, "
|
||||
. "GROUP_CONCAT(`COLLATION_NAME` ORDER BY `COLLATION_NAME`) "
|
||||
. "`collations` FROM `information_schema`."
|
||||
. "`COLLATION_CHARACTER_SET_APPLICABILITY` GROUP BY "
|
||||
. "`CHARACTER_SET_NAME` $where";
|
||||
$rawCollations = $this->query($collationSql, Msd_Db::ARRAY_ASSOC);
|
||||
$collations = array();
|
||||
foreach ($rawCollations as $charset) {
|
||||
$collations[$charset['charset']] = explode(
|
||||
",",
|
||||
$charset['collations']
|
||||
);
|
||||
}
|
||||
|
||||
return $collations;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the default collation for the charset or the given charset.
|
||||
*
|
||||
* @param string|null $charsetName Name of the charset
|
||||
*
|
||||
* @return array|string
|
||||
*/
|
||||
public function getDefaultCollations($charsetName = null)
|
||||
{
|
||||
if (!empty($charsetName)) {
|
||||
$defaultCollationSql = 'SELECT `DEFAULT_COLLATE_NAME` FROM '
|
||||
. '`information_schema`.`CHARACTER_SETS` WHERE '
|
||||
. '`CHARACTER_SET_NAME` = \'' . $this->escape($charsetName)
|
||||
. '\'';
|
||||
$result = $this->query($defaultCollationSql, self::ARRAY_NUMERIC);
|
||||
$defaultCollation = $result[0][0];
|
||||
} else {
|
||||
$defaultCollationSql = 'SELECT `CHARACTER_SET_NAME` `charset`, '
|
||||
. '`DEFAULT_COLLATE_NAME` `collation` FROM '
|
||||
. '`information_schema`.`CHARACTER_SETS`';
|
||||
$result = $this->query($defaultCollationSql, self::ARRAY_ASSOC);
|
||||
$defaultCollation = array();
|
||||
foreach ($result as $row) {
|
||||
$defaultCollation[$row['charset']] = $row['collation'];
|
||||
}
|
||||
}
|
||||
return $defaultCollation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the views of the given database.
|
||||
*
|
||||
* @param string $dbName Name of database
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getViews($dbName)
|
||||
{
|
||||
$sql = 'SELECT * FROM `information_schema`.`VIEWS` WHERE '
|
||||
. '`TABLE_SCHEMA` = \'' . $this->escape($dbName) . '\'';
|
||||
$rawViews = $this->query($sql, self::ARRAY_ASSOC);
|
||||
$views = array();
|
||||
foreach ($rawViews as $rawView) {
|
||||
$views[$rawView['TABLE_NAME']] = $rawView;
|
||||
}
|
||||
return $views;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of views per database.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getNrOfViews()
|
||||
{
|
||||
$sql = 'SELECT `TABLE_SCHEMA`, count(*) as `views` FROM '
|
||||
. '`information_schema`.`VIEWS` '
|
||||
. ' GROUP BY `TABLE_SCHEMA`';
|
||||
$res = $this->query($sql, self::ARRAY_ASSOC);
|
||||
$views = array();
|
||||
foreach ($res as $view) {
|
||||
$views[$view['TABLE_SCHEMA']] = $view['views'];
|
||||
}
|
||||
return $views;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of routines (procedures and functions).
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getNrOfRoutines()
|
||||
{
|
||||
$sql = 'SELECT `ROUTINE_SCHEMA`, count(`ROUTINE_NAME`) as `routines`'
|
||||
. ' FROM `information_schema`.`ROUTINES` '
|
||||
. ' GROUP BY `ROUTINE_SCHEMA`';
|
||||
$res = $this->query($sql, self::ARRAY_ASSOC);
|
||||
$routines = array();
|
||||
foreach ($res as $routine) {
|
||||
$routines[$routine['ROUTINE_SCHEMA']] = $routine['routines'];
|
||||
}
|
||||
return $routines;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the size of tabledata in bytes
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getDatabaseSizes()
|
||||
{
|
||||
$sql = 'SELECT `TABLE_SCHEMA`, sum(`DATA_LENGTH`) as `size`'
|
||||
. ' FROM `information_schema`.`TABLES` '
|
||||
. ' GROUP BY `TABLE_SCHEMA`';
|
||||
$res = $this->query($sql, self::ARRAY_ASSOC);
|
||||
$sizes = array();
|
||||
foreach ($res as $size) {
|
||||
$sizes[$size['TABLE_SCHEMA']] = $size['size'];
|
||||
}
|
||||
return $sizes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the stored procedurs of the given database.
|
||||
*
|
||||
* @param string $dbName Name of database
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getStoredProcedures($dbName)
|
||||
{
|
||||
$routinesSql = 'SELECT * FROM `information_schema`.`ROUTINES` WHERE '
|
||||
. '`ROUTINE_SCHEMA` = \'' . $this->escape($dbName) . '\'';
|
||||
$rawRoutines = $this->query($routinesSql, self::ARRAY_ASSOC);
|
||||
$routines = array();
|
||||
foreach ($rawRoutines as $rawRoutine) {
|
||||
$routines[$rawRoutine['ROUTINE_NAME']] = $rawRoutine;
|
||||
}
|
||||
return $routines;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new database via building a MySQL statement and its execution.
|
||||
*
|
||||
* @param string $databaseName Name of the new database
|
||||
* @param string $databaseCharset Charackter set of the new database
|
||||
* @param string $databaseCollation Collation of the new database
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function createDatabase(
|
||||
$databaseName,
|
||||
$databaseCharset = '',
|
||||
$databaseCollation = ''
|
||||
)
|
||||
{
|
||||
if ($databaseCharset != '') {
|
||||
$databaseCharset = "DEFAULT CHARSET "
|
||||
. $this->escape($databaseCharset);
|
||||
}
|
||||
if ($databaseCollation != '') {
|
||||
$databaseCollation = "DEFAULT COLLATE "
|
||||
. $this->escape($databaseCollation);
|
||||
}
|
||||
$sql = "CREATE DATABASE `" . $databaseName
|
||||
. "` $databaseCharset $databaseCollation";
|
||||
$dbCreated = $this->query($sql, Msd_Db::SIMPLE);
|
||||
return $dbCreated;
|
||||
}
|
||||
}
|
||||
240
library/Msd/Db/Mysqli.php
Normale Datei
240
library/Msd/Db/Mysqli.php
Normale Datei
|
|
@ -0,0 +1,240 @@
|
|||
<?php
|
||||
/**
|
||||
* This file is part of MySQLDumper released under the GNU/GPL 2 license
|
||||
* http://www.mysqldumper.net
|
||||
*
|
||||
* @package MySQLDumper
|
||||
* @subpackage Db
|
||||
* @version SVN: $rev: 1208 $
|
||||
* @author $Author$
|
||||
*/
|
||||
/**
|
||||
* Capsules all database related actions.
|
||||
*
|
||||
* @package MySQLDumper
|
||||
* @subpackage Db
|
||||
*/
|
||||
class Msd_Db_Mysqli extends Msd_Db_MysqlCommon
|
||||
{
|
||||
/**
|
||||
* @var mysqli
|
||||
*/
|
||||
private $_mysqli = null;
|
||||
|
||||
/**
|
||||
* @var resource
|
||||
*/
|
||||
private $_resultHandle = null;
|
||||
/**
|
||||
* Establish a connection to MySQL.
|
||||
*
|
||||
* Create a connection to MySQL and store the connection handle in
|
||||
* $this->connectionHandle.
|
||||
*
|
||||
* @return boolean
|
||||
**/
|
||||
protected function _dbConnect()
|
||||
{
|
||||
$errorReporting = error_reporting(0);
|
||||
if ($this->_port == 0) {
|
||||
$this->_port = 3306;
|
||||
}
|
||||
|
||||
$this->_mysqli = new mysqli(
|
||||
$this->_server,
|
||||
$this->_user,
|
||||
$this->_password,
|
||||
$this->_dbSelected,
|
||||
$this->_port,
|
||||
$this->_socket
|
||||
);
|
||||
error_reporting($errorReporting);
|
||||
if ($this->_mysqli->connect_errno) {
|
||||
$error = $this->_mysqli->connect_error;
|
||||
$errno = $this->_mysqli->connect_errno;
|
||||
$this->_mysqli = null;
|
||||
throw new Msd_Exception($error, $errno);
|
||||
}
|
||||
$this->setConnectionCharset();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the connection handle if already set or creates one.
|
||||
*
|
||||
* @return mysqli The instance of mysqli
|
||||
*/
|
||||
private function _getHandle()
|
||||
{
|
||||
if (!$this->_mysqli instanceof mysqli) {
|
||||
$this->_dbConnect();
|
||||
}
|
||||
return $this->_mysqli;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the version nr of MySql server.
|
||||
*
|
||||
* @return string Version nr
|
||||
*/
|
||||
public function getServerInfo()
|
||||
{
|
||||
return $this->_getHandle()->server_info;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return version nr of MySql client.
|
||||
*
|
||||
* @return string Version nr
|
||||
*/
|
||||
public function getClientInfo()
|
||||
{
|
||||
return $this->_getHandle()->client_info;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the character set of the MySQL-connection.
|
||||
*
|
||||
* Trys to set the connection charset and returns it.
|
||||
*
|
||||
* @param string $charset The wanted charset of the connection
|
||||
*
|
||||
* @return string The set charset
|
||||
*/
|
||||
public function setConnectionCharset($charset = 'utf8')
|
||||
{
|
||||
if (!@$this->_getHandle()->set_charset($charset)) {
|
||||
$this->sqlError(
|
||||
$charset . ' ' . $this->_mysqli->error,
|
||||
$this->_mysqli->errno
|
||||
);
|
||||
}
|
||||
$this->_connectionCharset = $this->_getHandle()->character_set_name();
|
||||
return $this->_connectionCharset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Select the given database to use it as the target for following queries.
|
||||
*
|
||||
* Returns true if selection was succesfull, otherwise false.
|
||||
*
|
||||
* @param string $database Database to select
|
||||
*
|
||||
* @return boolean True on success
|
||||
*/
|
||||
public function selectDb($database)
|
||||
{
|
||||
$res = @$this->_getHandle()->select_db($database);
|
||||
if ($res === false) {
|
||||
return $this->_getHandle()->error;
|
||||
} else {
|
||||
$this->_dbSelected = $database;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a query and set _resultHandle
|
||||
*
|
||||
* If $getRows is true all rows are fetched and returned.
|
||||
* If $getRows is false, query will be executed, but the result handle
|
||||
* is returned.
|
||||
*
|
||||
* @param string $query The query to execute
|
||||
* @param const $kind Type of result set
|
||||
* @param boolean $getRows Wether to fetch all rows and return them
|
||||
*
|
||||
* @return resource|array
|
||||
*/
|
||||
public function query($query, $kind = self::ARRAY_OBJECT, $getRows = true)
|
||||
{
|
||||
try {
|
||||
$this->_resultHandle = $this->_getHandle()->query($query);
|
||||
|
||||
if (false === $this->_resultHandle) {
|
||||
$this->sqlError(
|
||||
$this->_getHandle()->error,
|
||||
$this->_getHandle()->errno
|
||||
);
|
||||
}
|
||||
if (!$this->_resultHandle instanceof mysqli_result
|
||||
|| $kind === self::SIMPLE) {
|
||||
return $this->_resultHandle;
|
||||
}
|
||||
// return result set?
|
||||
if ($getRows) {
|
||||
$ret = array();
|
||||
WHILE ($row = $this->getNextRow($kind)) {
|
||||
$ret[] = $row;
|
||||
}
|
||||
$this->_resultHandle = null;
|
||||
return $ret;
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
$this->sqlError(
|
||||
$this->_getHandle()->error,
|
||||
$this->_getHandle()->errno
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get next row from a result set that is returned by $this->query().
|
||||
*
|
||||
* Can be used to walk through result sets.
|
||||
*
|
||||
* @param const $kind
|
||||
*
|
||||
* @return array|object
|
||||
*/
|
||||
public function getNextRow($kind)
|
||||
{
|
||||
switch ($kind)
|
||||
{
|
||||
case self::ARRAY_ASSOC:
|
||||
return $this->_resultHandle->fetch_assoc();
|
||||
case self::ARRAY_OBJECT:
|
||||
return $this->_resultHandle->fetch_object();
|
||||
break;
|
||||
case self::ARRAY_NUMERIC:
|
||||
return $this->_resultHandle->fetch_array(MYSQLI_NUM);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the number of affected rows for the last executed query.
|
||||
*
|
||||
* @see inc/classes/db/MsdDbFactory#affectedRows()
|
||||
* @return integer
|
||||
*/
|
||||
public function getAffectedRows()
|
||||
{
|
||||
return $this->_getHandle()->affected_rows;
|
||||
}
|
||||
|
||||
/**
|
||||
* Escape a value with real_escape_string() to use it in a query.
|
||||
*
|
||||
* @see inc/classes/db/MsdDbFactory#escape($val)
|
||||
* @param mixed $val The value to escape
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function escape($val)
|
||||
{
|
||||
return $this->_getHandle()->real_escape_string($val);
|
||||
}
|
||||
/**
|
||||
* Retrieves the last MySQL error.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getLastError()
|
||||
{
|
||||
return array(
|
||||
'code' => $this->_getHandle()->errno,
|
||||
'message' => $this->_getHandle()->error,
|
||||
);
|
||||
}
|
||||
}
|
||||
Laden …
Tabelle hinzufügen
Einen Link hinzufügen
In neuem Issue referenzieren