2011-06-10 21:55:32 +00:00
|
|
|
<?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: 1212 $
|
|
|
|
* @author $Author$
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* DB-Factory
|
|
|
|
*
|
|
|
|
* @abstract
|
|
|
|
* @package MySQLDumper
|
|
|
|
* @subpackage Db
|
|
|
|
*/
|
|
|
|
abstract class Msd_Db
|
|
|
|
{
|
|
|
|
// define result set types
|
2012-08-08 22:36:28 +00:00
|
|
|
/**
|
|
|
|
* Return result-set as an numeric array.
|
|
|
|
*
|
|
|
|
* @const int
|
|
|
|
*/
|
|
|
|
const ARRAY_NUMERIC = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return result-set as an associative array.
|
|
|
|
*
|
|
|
|
* @const int
|
|
|
|
*/
|
|
|
|
const ARRAY_ASSOC = 1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* return result-set as an array of objects
|
|
|
|
*
|
|
|
|
* @const int
|
|
|
|
*/
|
|
|
|
const ARRAY_OBJECT = 2;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the return value of the query function/method.
|
|
|
|
*
|
|
|
|
* @const int
|
|
|
|
*/
|
|
|
|
const SIMPLE = 3;
|
|
|
|
|
2011-06-10 21:55:32 +00:00
|
|
|
/**
|
|
|
|
* SQL-Server
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $_server;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SQL user used to authenticate at server
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $_user;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SQL user password used to authenticate
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $_password;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Port used for connection to server
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $_port;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Socket used for connection
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $_socket;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List of databases adn default settings
|
2012-08-08 22:36:28 +00:00
|
|
|
*
|
2011-06-10 21:55:32 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_databases = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* charset to use (default utf8)
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $_connectionCharset = 'utf8';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the selected db
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $_dbSelected = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List of cached tables
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_tables = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Meta informations about tables
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_metaTables = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Charsets the server supports (cached)
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_charsets = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Init database object
|
|
|
|
*
|
|
|
|
* @param array $options Array containing connection options
|
|
|
|
*
|
2012-08-08 22:36:28 +00:00
|
|
|
* @return Msd_Db
|
2011-06-10 21:55:32 +00:00
|
|
|
*/
|
2012-08-08 22:36:28 +00:00
|
|
|
protected function __construct($options)
|
2011-06-10 21:55:32 +00:00
|
|
|
{
|
2012-08-08 22:36:28 +00:00
|
|
|
$this->_server = $options['host'];
|
|
|
|
$this->_user = $options['user'];
|
2011-06-10 21:55:32 +00:00
|
|
|
$this->_password = $options['pass'];
|
2012-08-08 22:36:28 +00:00
|
|
|
$this->_port = (int) $options['port'];
|
|
|
|
$this->_socket = $options['socket'];
|
2011-06-10 21:55:32 +00:00
|
|
|
}
|
2012-08-08 22:36:28 +00:00
|
|
|
|
2011-06-10 21:55:32 +00:00
|
|
|
/**
|
|
|
|
* Create database adapter
|
|
|
|
*
|
|
|
|
* @param array $options Connection options
|
|
|
|
* @param boolean $forceMysql Whether to force the use of MySQL
|
|
|
|
*
|
2012-08-08 22:36:28 +00:00
|
|
|
* @return Msd_Db_MysqlCommon
|
2011-06-10 21:55:32 +00:00
|
|
|
*/
|
|
|
|
public static function getAdapter($options = null, $forceMysql = false)
|
|
|
|
{
|
|
|
|
if ($options === null) {
|
2012-08-08 22:36:28 +00:00
|
|
|
$config = Msd_Registry::getConfig();
|
2011-06-10 21:55:32 +00:00
|
|
|
$options = array(
|
2012-08-08 22:36:28 +00:00
|
|
|
'host' => $config->getParam('dbuser.host'),
|
|
|
|
'user' => $config->getParam('dbuser.user'),
|
|
|
|
'pass' => $config->getParam('dbuser.pass'),
|
|
|
|
'port' => (int) $config->getParam('dbuser.port'),
|
2012-08-04 17:09:48 +00:00
|
|
|
'socket' => $config->getParam('dbuser.socket'),
|
2011-06-10 21:55:32 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
if (function_exists('mysqli_connect') && !$forceMysql) {
|
|
|
|
$dbObject = new Msd_Db_Mysqli($options);
|
|
|
|
} else {
|
|
|
|
$dbObject = new Msd_Db_Mysql($options);
|
|
|
|
}
|
|
|
|
return $dbObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Establish a connection to SQL-Server. The connection is stored and used
|
|
|
|
* for further DB requests.
|
|
|
|
*
|
|
|
|
* @return bool if connection is successfull
|
|
|
|
* */
|
2012-08-08 22:36:28 +00:00
|
|
|
abstract protected function _dbConnect();
|
|
|
|
|
2011-06-10 21:55:32 +00:00
|
|
|
/**
|
|
|
|
* Get selected database
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-08-08 22:36:28 +00:00
|
|
|
abstract public function getSelectedDb();
|
|
|
|
|
2011-06-10 21:55:32 +00:00
|
|
|
/**
|
|
|
|
* Get version nr of sql server
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-08-08 22:36:28 +00:00
|
|
|
abstract public function getServerInfo();
|
|
|
|
|
2011-06-10 21:55:32 +00:00
|
|
|
/**
|
|
|
|
* Get version nr of sql client
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-08-08 22:36:28 +00:00
|
|
|
abstract public function getClientInfo();
|
|
|
|
|
2011-06-10 21:55:32 +00:00
|
|
|
/**
|
|
|
|
* Get all known character sets of this SQL-Server.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-08-08 22:36:28 +00:00
|
|
|
abstract public function getCharsets();
|
|
|
|
|
2011-06-10 21:55:32 +00:00
|
|
|
/**
|
|
|
|
* Set character set of the MySQL-connection.
|
|
|
|
*
|
|
|
|
* Trys to set the connection charset and returns it.
|
|
|
|
* Throw Exception on failure.
|
|
|
|
*
|
|
|
|
* @param string $charset
|
2012-08-08 22:36:28 +00:00
|
|
|
*
|
2011-06-10 21:55:32 +00:00
|
|
|
* @throws Exception
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-08-08 22:36:28 +00:00
|
|
|
abstract public function setConnectionCharset(
|
|
|
|
$charset = 'utf8');
|
|
|
|
|
2011-06-10 21:55:32 +00:00
|
|
|
/**
|
|
|
|
* Get list of databases
|
|
|
|
*
|
|
|
|
* Gets list of all databases that the actual SQL-Server-User has access to
|
|
|
|
* and saves it in $this->databases.
|
|
|
|
* Returns true on success or false on error.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-08-08 22:36:28 +00:00
|
|
|
abstract public function getDatabases();
|
|
|
|
|
2011-06-10 21:55:32 +00:00
|
|
|
/**
|
|
|
|
* Select the given database to use it as the target for following queries.
|
|
|
|
*
|
|
|
|
* Returns true if selection was succesfull otherwise false.
|
|
|
|
*
|
|
|
|
* @throws Exception
|
2012-08-08 22:36:28 +00:00
|
|
|
*
|
2011-06-10 21:55:32 +00:00
|
|
|
* @param string $database
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2012-08-08 22:36:28 +00:00
|
|
|
abstract public function selectDb($database);
|
|
|
|
|
2011-06-10 21:55:32 +00:00
|
|
|
/**
|
|
|
|
* Execute a query and set _resultHandle
|
|
|
|
*
|
|
|
|
* If $getRows is true alls rows are fetched and returned
|
|
|
|
*
|
|
|
|
* @param string $query The query to execute
|
2012-08-08 22:36:28 +00:00
|
|
|
* @param int $kind Type of result set
|
2011-06-10 21:55:32 +00:00
|
|
|
* @param boolean $getRows Whether to fetch all rows and return them
|
|
|
|
*
|
|
|
|
* @return boolean|array
|
|
|
|
*/
|
2012-08-08 22:36:28 +00:00
|
|
|
abstract public function query($query,
|
|
|
|
$kind = self::ARRAY_OBJECT, $getRows = true);
|
2011-06-10 21:55:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get next row from result set
|
|
|
|
*
|
2012-08-08 22:36:28 +00:00
|
|
|
* @param int $kind
|
2011-06-10 21:55:32 +00:00
|
|
|
*
|
|
|
|
* @return array|object
|
|
|
|
*/
|
|
|
|
abstract public function getNextRow($kind);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the list of tables of given database
|
|
|
|
*
|
|
|
|
* @param string $dbName Name of database
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-08-08 22:36:28 +00:00
|
|
|
abstract public function getTables($dbName);
|
|
|
|
|
2011-06-10 21:55:32 +00:00
|
|
|
/**
|
|
|
|
* Gets extended table information for one or all tables
|
|
|
|
*
|
|
|
|
* @param string $table
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-08-08 22:36:28 +00:00
|
|
|
abstract public function getTableStatus($table = false);
|
|
|
|
|
2011-06-10 21:55:32 +00:00
|
|
|
/**
|
|
|
|
* Returns the CREATE Statement of a table.
|
|
|
|
*
|
|
|
|
* @throws Exception
|
2012-08-08 22:36:28 +00:00
|
|
|
*
|
2011-06-10 21:55:32 +00:00
|
|
|
* @param string $table Get CREATE-Statement for this table
|
|
|
|
*
|
|
|
|
* @return string Create statement
|
|
|
|
*/
|
2012-08-08 22:36:28 +00:00
|
|
|
abstract public function getTableCreate($table);
|
|
|
|
|
2011-06-10 21:55:32 +00:00
|
|
|
/**
|
|
|
|
* Gets the full description of all columns of a table
|
|
|
|
*
|
|
|
|
* Saves list to $this->metaTables[$database][$table].
|
|
|
|
*
|
|
|
|
* @param string $table Table to read meta info from
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-08-08 22:36:28 +00:00
|
|
|
abstract public function getTableColumns($table);
|
|
|
|
|
2011-06-10 21:55:32 +00:00
|
|
|
/**
|
|
|
|
* Gets the number of affected rows of the last query
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
2012-08-08 22:36:28 +00:00
|
|
|
abstract public function getAffectedRows();
|
|
|
|
|
2011-06-10 21:55:32 +00:00
|
|
|
/**
|
|
|
|
* Gets the servers variables
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-08-08 22:36:28 +00:00
|
|
|
|
|
|
|
abstract public function getVariables();
|
|
|
|
|
2011-06-10 21:55:32 +00:00
|
|
|
/**
|
|
|
|
* Escape a value for inserting it in query
|
|
|
|
*
|
|
|
|
* @param string $val
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-08-08 22:36:28 +00:00
|
|
|
abstract public function escape($val);
|
|
|
|
|
2011-06-10 21:55:32 +00:00
|
|
|
/**
|
|
|
|
* Optimize a table. Returns true on success or MySQL-Error.
|
|
|
|
*
|
|
|
|
* @param $table string Name of table
|
|
|
|
*
|
|
|
|
* @return string|bool Returned optimize message or false on error
|
|
|
|
*/
|
2012-08-08 22:36:28 +00:00
|
|
|
|
|
|
|
abstract public function optimizeTable($table);
|
|
|
|
|
2011-06-10 21:55:32 +00:00
|
|
|
/**
|
|
|
|
* Creates a new database with the given name, charackter set and collation.
|
|
|
|
*
|
|
|
|
* @abstract
|
|
|
|
*
|
|
|
|
* @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 bool
|
|
|
|
*/
|
|
|
|
abstract public function createDatabase(
|
|
|
|
$databaseName,
|
|
|
|
$databaseCharset = '',
|
|
|
|
$databaseCollation = ''
|
|
|
|
);
|
2012-08-08 22:36:28 +00:00
|
|
|
|
2011-06-10 21:55:32 +00:00
|
|
|
/**
|
|
|
|
* Retrieves the collations from information schema.
|
|
|
|
*
|
|
|
|
* @param string|null $charsetName Name of the charset
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
abstract public function getCollations($charsetName = null);
|
2012-08-08 22:36:28 +00:00
|
|
|
|
2011-06-10 21:55:32 +00:00
|
|
|
/**
|
|
|
|
* Retrieves the default collation for the charset or the given charset.
|
|
|
|
*
|
|
|
|
* @param string|null $charsetName Name of the charset
|
|
|
|
*
|
|
|
|
* @return array|string
|
|
|
|
*/
|
|
|
|
abstract public function getDefaultCollations($charsetName = null);
|
2012-08-08 22:36:28 +00:00
|
|
|
|
2011-06-10 21:55:32 +00:00
|
|
|
/**
|
|
|
|
* Retrieves the last MySQL error.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
abstract public function getLastError();
|
2012-08-08 22:36:28 +00:00
|
|
|
|
2011-06-10 21:55:32 +00:00
|
|
|
/**
|
|
|
|
* Handles a SQL-Error
|
|
|
|
*
|
|
|
|
* @param string $errmsg
|
|
|
|
* @param int $errno
|
2012-08-08 22:36:28 +00:00
|
|
|
*
|
|
|
|
* @throws Msd_Exception
|
2011-06-10 21:55:32 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2012-08-08 22:36:28 +00:00
|
|
|
public function sqlError($errmsg, $errno)
|
2011-06-10 21:55:32 +00:00
|
|
|
{
|
|
|
|
throw new Msd_Exception($errmsg, $errno);
|
|
|
|
}
|
|
|
|
}
|