Struggeling with relocating
Dieser Commit ist enthalten in:
Commit
89ea01c429
301 geänderte Dateien mit 59926 neuen und 0 gelöschten Zeilen
249
inc/classes/db/MsdDbFactory.php
Normale Datei
249
inc/classes/db/MsdDbFactory.php
Normale Datei
|
|
@ -0,0 +1,249 @@
|
|||
<?php
|
||||
/**
|
||||
* This file is part of MySQLDumper released under the GNU/GPL 2 license
|
||||
* http://www.mysqldumper.net
|
||||
*
|
||||
* @package MySQLDumper
|
||||
* @version SVN: $rev: 1212 $
|
||||
* @author $Author$
|
||||
* @lastmodified $Date$
|
||||
*/
|
||||
|
||||
/**
|
||||
* DB-Factory
|
||||
*
|
||||
* @abstract
|
||||
*/
|
||||
abstract class MsdDbFactory
|
||||
{
|
||||
const FORCE_MYSQL = false; // for debugging
|
||||
// define result set types
|
||||
const ARRAY_NUMERIC = 0; // return resultset as numeric array
|
||||
const ARRAY_ASSOC = 1; // return resultset as associative array
|
||||
const ARRAY_OBJECT = 2; // return resultset as array of object
|
||||
const SIMPLE = 3; // return result as boolean
|
||||
|
||||
/**
|
||||
* Init database object
|
||||
*
|
||||
* @param string $server SQL-Server
|
||||
* @param string $user SQL-User used to authenticate at server
|
||||
* @param string $password SQL-User-Password usued to authenticate
|
||||
* @param string $port [optional] Port to use
|
||||
* @param string $socket [optional] Socket to use (only for *nix)
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function __construct($server, $user, $password, $port = 3306, $socket ='')
|
||||
{
|
||||
$this->server = $server;
|
||||
$this->user = $user;
|
||||
$this->password = $password;
|
||||
$this->port = $port;
|
||||
$this->socket = $socket;
|
||||
$this->connectionCharset = 'utf8';
|
||||
$this->connectionHandle = false;
|
||||
$this->dbSelected = '';
|
||||
$this->tables = array();
|
||||
$this->metaTables = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create database adapter
|
||||
*
|
||||
* @param string $server
|
||||
* @param string $user
|
||||
* @param string $password
|
||||
* @param string $port
|
||||
* @param string $socket
|
||||
*
|
||||
* @return MsdDbFactory
|
||||
*/
|
||||
static function getAdapter($server, $user, $password, $port = 3306, $socket = '')
|
||||
{
|
||||
if (function_exists('mysqli_connect') && !self::FORCE_MYSQL) {
|
||||
include_once ('./inc/classes/db/mysqli/MsdDbMysqli.php');
|
||||
$db = new MsdDbMysqli($server, $user, $password, $port, $socket);
|
||||
} else {
|
||||
include_once ('./inc/classes/db/mysql/MsdDbMysql.php');
|
||||
$db = new MsdDbMysql($server, $user, $password, $port, $socket);
|
||||
}
|
||||
return $db;
|
||||
}
|
||||
|
||||
/**
|
||||
* Establish a connection to SQL-Server.
|
||||
*
|
||||
* Doesn't need to be called directly because of lazy loading.
|
||||
* Will be called automatically if any other method is called that
|
||||
* needs a connection.
|
||||
* Creates a connection to the database and stores the connection handle
|
||||
* in $this->connection_handle.
|
||||
* If $select_database is set, the database is selected for next queries.
|
||||
* Returns true on success or false if connection couldn't be established.
|
||||
*
|
||||
* @param string $select_database
|
||||
* @param string $connection_charset
|
||||
*
|
||||
* @return bool
|
||||
* */
|
||||
abstract public function dbConnect($selectDatabase = false);
|
||||
|
||||
/**
|
||||
* Get selected database
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract public function getSelectedDb();
|
||||
|
||||
/**
|
||||
* Get version nr of sql server
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract public function getServerInfo();
|
||||
|
||||
/**
|
||||
* Get version nr of sql client
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract public function getClientInfo();
|
||||
|
||||
/**
|
||||
* Get all known character sets of this SQL-Server.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
abstract public function getCharsets();
|
||||
/**
|
||||
* Set character set of the MySQL-connection.
|
||||
*
|
||||
* Trys to set the connection charset and returns it.
|
||||
* Throw Exception on failure.
|
||||
*
|
||||
* @param string $charset
|
||||
* @throws Exception
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract public function setConnectionCharset($charset = 'utf8');
|
||||
/**
|
||||
* 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 bool
|
||||
*/
|
||||
abstract public function getDatabases();
|
||||
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function selectDb($database);
|
||||
|
||||
/**
|
||||
* Execute a SQL-Server-Query
|
||||
*
|
||||
* @param $query The query to execute
|
||||
* @param $kind Type of result set
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
abstract public function query($query, $kind = self::ARRAY_OBJECT);
|
||||
|
||||
/**
|
||||
* Get table list of given database(s)
|
||||
*
|
||||
* Stores them in $this->tables[database]
|
||||
* When no database is given, all databases are scanned for tables.
|
||||
* Returns table list for selected or for all databases.
|
||||
*
|
||||
* @param string|array $database
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
abstract public function getTables($database = false);
|
||||
|
||||
/**
|
||||
* Gets extended table information for one or all tables
|
||||
*
|
||||
* @param string $table
|
||||
* @param string $database
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
abstract public function getTableStatus($table = false, $database = false);
|
||||
|
||||
/**
|
||||
* Returns the CREATE Statement of a table.
|
||||
*
|
||||
* @throws Exception
|
||||
* @param string $table Get CREATE-Statement for this table
|
||||
* @param string $database Database holding the table
|
||||
*
|
||||
* @return string Create statement
|
||||
*/
|
||||
abstract public function getTableCreate($table, $database = false);
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @param string $database Database holding the table
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
abstract public function getTableColumns($table, $database = false);
|
||||
|
||||
/**
|
||||
* Gets the number of affected rows of the last query
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
abstract function getAffectedRows();
|
||||
|
||||
/**
|
||||
* Escape a value for inserting it in query
|
||||
*
|
||||
* @param string $val
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract function escape($val);
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
abstract function optimizeTable($table);
|
||||
|
||||
/**
|
||||
* Handles a SQL-Error
|
||||
*
|
||||
* @param string $errmsg
|
||||
* @param int $errno
|
||||
* @throws MsdEception
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function sqlError($errmsg, $errno)
|
||||
{
|
||||
throw new Exception($errmsg, $errno);
|
||||
}
|
||||
|
||||
}
|
||||
439
inc/classes/db/mysql/MsdDbMysql.php
Normale Datei
439
inc/classes/db/mysql/MsdDbMysql.php
Normale Datei
|
|
@ -0,0 +1,439 @@
|
|||
<?php
|
||||
/**
|
||||
* This file is part of MySQLDumper released under the GNU/GPL 2 license
|
||||
* http://www.mysqldumper.net
|
||||
*
|
||||
* @package MySQLDumper
|
||||
* @subpackage MsdDbFactory
|
||||
* @version SVN: $rev: 1205 $
|
||||
* @author $Author$
|
||||
* @lastmodified $Date$
|
||||
*/
|
||||
|
||||
/**
|
||||
* Capsules all database related actions.
|
||||
* Uses some magic getters to allow lazy connecting.
|
||||
*
|
||||
*/
|
||||
class MsdDbMysql extends MsdDbFactory
|
||||
{
|
||||
/**
|
||||
* Get all known character sets of this SQL-Server.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getCharsets()
|
||||
{
|
||||
if (isset($this->charsets)) return $this->charsets;
|
||||
if (false === $this->connectionHandle) $this->dbConnect();
|
||||
$result = $this->query('SHOW CHARACTER SET', self::ARRAY_ASSOC);
|
||||
$this->charsets = array();
|
||||
foreach ($result as $r) {
|
||||
$this->charsets[$r['Charset']] = $r;
|
||||
}
|
||||
@ksort($this->charsets);
|
||||
return $this->charsets;
|
||||
}
|
||||
/**
|
||||
* Establish a connection to MySQL.
|
||||
*
|
||||
* Creates a connection to the database and stores the connection handle in
|
||||
* $this->connectionHandle.
|
||||
* If $select_database is set, the database is selected for further queries.
|
||||
* Returns true on success or false if connection couldn't be established.
|
||||
*
|
||||
* @param string $select_database
|
||||
* @param string $connectionCharset
|
||||
*
|
||||
* @return bool
|
||||
**/
|
||||
public function dbConnect($selectDatabase = false)
|
||||
{
|
||||
if (is_resource($this->connectionHandle)) return true;
|
||||
|
||||
$connectionString = $this->server . ':' . $this->port;
|
||||
if ($this->socket > '') $connectionString .= ':' . $this->socket;
|
||||
if (false === $this->connectionHandle) {
|
||||
$this->connectionHandle = @mysql_connect(
|
||||
$connectionString,
|
||||
$this->user, $this->password
|
||||
);
|
||||
if (false === $this->connectionHandle) {
|
||||
return '(' . mysql_errno() . ') ' . mysql_error();
|
||||
}
|
||||
}
|
||||
$this->setConnectionCharset($this->connectionCharset);
|
||||
if (false === $selectDatabase && $this->dbSelected > '') {
|
||||
$selectDatabase = $this->dbSelected;
|
||||
}
|
||||
if ($selectDatabase) {
|
||||
try
|
||||
{
|
||||
$this->selectDb($selectDatabase, $this->connectionHandle);
|
||||
$this->dbSelected = $selectDatabase;
|
||||
return true;
|
||||
} catch (Exception $e) {
|
||||
$this->sqlError(
|
||||
mysql_error($this->connectionHandle),
|
||||
mysql_errno($this->connectionHandle)
|
||||
);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get version nr of sql server
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getServerInfo()
|
||||
{
|
||||
if (false === $this->connectionHandle) {
|
||||
$this->dbConnect();
|
||||
}
|
||||
$version = mysql_get_server_info($this->connectionHandle);
|
||||
return $version;
|
||||
}
|
||||
/**
|
||||
* Get version nr of sql client
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getClientInfo()
|
||||
{
|
||||
if (false === $this->connectionHandle) $this->dbConnect();
|
||||
$version = mysql_get_client_info();
|
||||
return $version;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set character set of the MySQL-connection.
|
||||
*
|
||||
* Trys to set the connection charset and returns it.
|
||||
* Throw Exception on failure.
|
||||
*
|
||||
* @param string $charset
|
||||
* @throws Exception
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function setConnectionCharset($charset = 'utf8')
|
||||
{
|
||||
if (false === $this->connectionHandle) $this->dbConnect();
|
||||
if (function_exists('mysql_set_charset')
|
||||
&& @mysql_set_charset($charset, $this->connectionHandle)) {
|
||||
$this->connectionCharset = $charset;
|
||||
return $this->connectionCharset;
|
||||
} else {
|
||||
$this->query('SET NAMES \'' . $charset . '\'', self::SIMPLE);
|
||||
$this->connectionCharset = $charset;
|
||||
return $charset;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get list of databases
|
||||
*
|
||||
* Gets list of all databases that the actual MySQL-User has access to
|
||||
* and saves it in $this->databases.
|
||||
* Returns true on success or false on error.
|
||||
*
|
||||
* @return array List of found database names
|
||||
*/
|
||||
public function getDatabases()
|
||||
{
|
||||
if (false === $this->connectionHandle) $this->dbConnect();
|
||||
$this->databases = array();
|
||||
$databases = @mysql_list_dbs($this->connectionHandle);
|
||||
if (is_resource($databases)) {
|
||||
WHILE ($row = mysql_fetch_array($databases, MYSQL_ASSOC)) {
|
||||
$this->databases[] = $row['Database'];
|
||||
}
|
||||
} else {
|
||||
//mysql_list_dbs seems not to be allowed for this user
|
||||
// try to get list via "SHOW DATABASES"
|
||||
$res = $this->query('SHOW DATABASES', self::ARRAY_ASSOC);
|
||||
foreach ($res as $r) {
|
||||
$this->databases[] = $r['Database'];
|
||||
}
|
||||
}
|
||||
sort($this->databases);
|
||||
return $this->databases;
|
||||
}
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function selectDb($database)
|
||||
{
|
||||
if (!is_resource($this->connectionHandle)) {
|
||||
$this->dbConnect();
|
||||
}
|
||||
$res=@mysql_select_db($database, $this->connectionHandle);
|
||||
if ($res===false) {
|
||||
return mysql_error();
|
||||
} else {
|
||||
$this->dbSelected = $database;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get selected database
|
||||
*
|
||||
* @see inc/classes/db/MsdDbFactory#getSelectedDb()
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getSelectedDb()
|
||||
{
|
||||
return $this->dbSelected;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a query
|
||||
*
|
||||
* @param $query The query to execute
|
||||
* @param $kind Type of result set
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function query($query, $kind = self::ARRAY_OBJECT)
|
||||
{
|
||||
if (false === $this->connectionHandle) {
|
||||
$this->dbConnect();
|
||||
}
|
||||
$res = @mysql_query($query, $this->connectionHandle);
|
||||
if (false === $res) {
|
||||
$this->sqlError(
|
||||
mysql_error($this->connectionHandle),
|
||||
mysql_errno($this->connectionHandle)
|
||||
);
|
||||
}
|
||||
if ($kind === self::SIMPLE || is_bool($res)) {
|
||||
return $res;
|
||||
}
|
||||
$ret = array();
|
||||
if ($kind === self::ARRAY_OBJECT) {
|
||||
WHILE ($row = mysql_fetch_object($res)) {
|
||||
$ret[] = $row;
|
||||
}
|
||||
} elseif ($kind === self::ARRAY_NUMERIC) {
|
||||
WHILE ($row = mysql_fetch_array($res, MYSQL_NUM)) {
|
||||
$ret[] = $row;
|
||||
}
|
||||
} elseif ($kind === self::ARRAY_ASSOC) {
|
||||
WHILE ($row = mysql_fetch_array($res, MYSQL_ASSOC)) {
|
||||
$ret[] = $row;
|
||||
}
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
/**
|
||||
* Get table list of given database(s)
|
||||
*
|
||||
* Stores them in $this->tables[database]
|
||||
* When no database is given, all databases are scanned for tables.
|
||||
* Returns table list for selected or for all databases.
|
||||
*
|
||||
* @param string|array $database
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getTables($database = false)
|
||||
{
|
||||
if (false === $this->connectionHandle) $this->dbConnect();
|
||||
if (!isset($this->tables)) $this->tables = array();
|
||||
if ($database !== false) {
|
||||
//list tables of selected database
|
||||
if (is_array($database)) {
|
||||
$databases = $database;
|
||||
} else {
|
||||
$databases = array();
|
||||
$databases[0] = $database;
|
||||
}
|
||||
} else {
|
||||
//list tables for all databases
|
||||
$this->getDatabases();
|
||||
$databases = $this->databases;
|
||||
}
|
||||
// get tablenames inside each database
|
||||
foreach ($databases as $db) {
|
||||
$this->tables[$db] = array();
|
||||
$sql = 'SHOW TABLES FROM `' . $db . '`';
|
||||
$res = $this->query($sql, self::ARRAY_NUMERIC);
|
||||
foreach ($res as $val) {
|
||||
$this->tables[$db][] = $val[0];
|
||||
}
|
||||
}
|
||||
return is_string($database) ? $this->tables[$database] : $this->tables;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets extended table information for one or all tables
|
||||
*
|
||||
* @param string $table
|
||||
* @param string $database
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getTableStatus($table = false, $database = false)
|
||||
{
|
||||
if ($database !== false && $database != $this->dbSelected) {
|
||||
$this->selectDb($database);
|
||||
} elseif (!$this->_mysqli instanceof mysqli) {
|
||||
$this->selectDb($this->dbSelected);
|
||||
}
|
||||
if (!isset($this->tableinfo)) {
|
||||
$this->tableinfo = array();
|
||||
}
|
||||
if (!isset($this->tableinfo[$this->dbSelected])) {
|
||||
$this->tableinfo[$this->dbSelected] = array();
|
||||
}
|
||||
$sql = 'SHOW TABLE STATUS';
|
||||
if ($table !== false) {
|
||||
$sql .= ' LIKE \'' . $table . '\'';
|
||||
}
|
||||
$res = $this->query($sql, self::ARRAY_ASSOC);
|
||||
if (is_array($res)) {
|
||||
foreach ($res as $r) {
|
||||
$tablename = $r['Name'];
|
||||
unset($r['Name']);
|
||||
$this->tableinfo[$this->dbSelected][$tablename] = $r;
|
||||
}
|
||||
} elseif ($res === false) {
|
||||
$this->sqlError(
|
||||
mysql_error($this->connectionHandle),
|
||||
mysql_errno($this->connectionHandle)
|
||||
);
|
||||
}
|
||||
if ($table !== false) {
|
||||
if (isset($this->tableinfo[$this->dbSelected][$table])) {
|
||||
return $this->tableinfo[$this->dbSelected][$table];
|
||||
}
|
||||
}
|
||||
return $this->tableinfo[$this->dbSelected];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the CREATE-Statement for the given table or false on error.
|
||||
*
|
||||
* @throws Exception
|
||||
* @param string $table
|
||||
* @param string $database
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getTableCreate($table, $database = false)
|
||||
{
|
||||
if (false === $database) $database = $this->dbSelected;
|
||||
if ($database != $this->dbSelected) {
|
||||
if (false === $this->selectDB($database)) {
|
||||
$this->sqlError(
|
||||
mysql_error($this->connectionHandle),
|
||||
mysql_errno($this->connectionHandle)
|
||||
);
|
||||
}
|
||||
}
|
||||
if (!is_resource($this->connectionHandle)) {
|
||||
$this->dbConnect($this->dbSelected);
|
||||
}
|
||||
$sql = 'SHOW CREATE TABLE `' . $table . '`';
|
||||
$res = $this->query($sql, self::ARRAY_ASSOC);
|
||||
if (isset($res[0]['Create Table'])) {
|
||||
return $res[0]['Create Table'];
|
||||
} else {
|
||||
$this->sqlError(
|
||||
mysql_error($this->connectionHandle),
|
||||
mysql_errno($this->connectionHandle)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the full description of all columns of a table
|
||||
*
|
||||
* Saves it to $this->metaTables[$database][$table].
|
||||
*
|
||||
* @param string $table
|
||||
* @param string $database
|
||||
* @return array
|
||||
*/
|
||||
public function getTableColumns($table, $database = false)
|
||||
{
|
||||
if (false === $database) $database = $this->dbSelected;
|
||||
if ($database != $this->dbSelected) {
|
||||
if (false === $this->selectDB($database)) {
|
||||
$this->sqlError(
|
||||
mysql_error($this->connectionHandle),
|
||||
mysql_errno($this->connectionHandle)
|
||||
);
|
||||
}
|
||||
}
|
||||
if (!is_resource($this->connectionHandle)) {
|
||||
$this->dbConnect($this->dbSelected);
|
||||
}
|
||||
$sql = 'SHOW FULL COLUMNS FROM `' . $table . '`';
|
||||
$res = $this->query($sql, self::ARRAY_ASSOC);
|
||||
if (!isset($this->metaTables[$database])) {
|
||||
$this->metaTables[$database] = array();
|
||||
}
|
||||
if ($res) {
|
||||
$this->metaTables[$database][$table] = array();
|
||||
foreach ($res as $r) {
|
||||
$this->metaTables[$database][$table][$r['Field']] = $r;
|
||||
}
|
||||
}
|
||||
return $this->metaTables[$database][$table];
|
||||
}
|
||||
|
||||
/**
|
||||
* 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->connectionHandle);
|
||||
}
|
||||
|
||||
/**
|
||||
* Escape a value 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->connectionHandle);
|
||||
}
|
||||
/**
|
||||
* Optimize a table. Returns true on success or MySQL-Error.
|
||||
*
|
||||
* @param string $table Name of table
|
||||
*
|
||||
* @return string|bool Returned optimize message or false on error
|
||||
*/
|
||||
function optimizeTable($table)
|
||||
{
|
||||
$sql = 'OPTIMIZE TABLE `' . $this->dbSelected . '`.`' . $table . '`';
|
||||
$res = $this->query($sql, MsdDbFactory::ARRAY_ASSOC);
|
||||
if (isset($res[0]['Msg_text'])) {
|
||||
return $res[0];
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
413
inc/classes/db/mysqli/MsdDbMysqli.php
Normale Datei
413
inc/classes/db/mysqli/MsdDbMysqli.php
Normale Datei
|
|
@ -0,0 +1,413 @@
|
|||
<?php
|
||||
/**
|
||||
* This file is part of MySQLDumper released under the GNU/GPL 2 license
|
||||
* http://www.mysqldumper.net
|
||||
*
|
||||
* @package MySQLDumper
|
||||
* @subpackage MsdDbFactory
|
||||
* @version SVN: $rev: 1208 $
|
||||
* @author $Author$
|
||||
* @lastmodified $Date$
|
||||
*/
|
||||
/**
|
||||
* Capsules all database related actions.
|
||||
* Uses some magic getters to allow lazy connecting.
|
||||
*
|
||||
*/
|
||||
class MsdDbMysqli extends MsdDbFactory
|
||||
{
|
||||
/**
|
||||
* @var resource
|
||||
*/
|
||||
private $_mysqli = null;
|
||||
/**
|
||||
* Get known charsets of MySQL-Server
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getCharsets()
|
||||
{
|
||||
if (isset($this->charsets)) return $this->charsets;
|
||||
if (!$this->_mysqli instanceof mysqli) $this->dbConnect();
|
||||
$result = $this->query('SHOW CHARACTER SET', self::ARRAY_ASSOC);
|
||||
$this->charsets = array();
|
||||
foreach ($result as $r) {
|
||||
$this->charsets[$r['Charset']] = $r;
|
||||
}
|
||||
@ksort($this->charsets);
|
||||
return $this->charsets;
|
||||
}
|
||||
/**
|
||||
* Establish a connection to MySQL.
|
||||
*
|
||||
* Create a connection to MySQL and store the connection handle in
|
||||
* $this->connectionHandle.
|
||||
* If $select_database is set, the database is selected for further queries.
|
||||
* Returns true on success or false if connection couldn't be established.
|
||||
*
|
||||
* @param string $select_database
|
||||
* @param string $connectionCharset
|
||||
* @return boolean
|
||||
**/
|
||||
public function dbConnect($selectDatabase = false)
|
||||
{
|
||||
if ($this->_mysqli instanceof mysqli) {
|
||||
return true;
|
||||
}
|
||||
$this->_mysqli = @new mysqli(
|
||||
$this->server,
|
||||
$this->user,
|
||||
$this->password,
|
||||
$this->dbSelected,
|
||||
$this->port,
|
||||
$this->socket
|
||||
);
|
||||
if ($this->_mysqli->connect_errno) {
|
||||
$error = $this->_mysqli->connect_error;
|
||||
$errno = $this->_mysqli->connect_errno;
|
||||
$this->_mysqli = null;
|
||||
return '(' . $errno . ') ' . $error;
|
||||
}
|
||||
$this->setConnectionCharset($this->connectionCharset);
|
||||
if (false === $selectDatabase && $this->dbSelected > '') {
|
||||
$selectDatabase = $this->dbSelected;
|
||||
}
|
||||
if ($selectDatabase) {
|
||||
if ($this->selectDb($selectDatabase)) {
|
||||
$this->dbSelected = $selectDatabase;
|
||||
return true;
|
||||
} else {
|
||||
$this->sqlError(mysqli_error(mysqli_error(), mysqli_errno()));
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* Get version nr of sql server
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getServerInfo()
|
||||
{
|
||||
if (!$this->_mysqli instanceof mysqli) {
|
||||
$this->dbConnect();
|
||||
}
|
||||
return $this->_mysqli->server_info;
|
||||
}
|
||||
/**
|
||||
* Get version nr of sql client
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getClientInfo()
|
||||
{
|
||||
if (!$this->_mysqli instanceof mysqli) $this->dbConnect();
|
||||
return $this->_mysqli->client_info;
|
||||
}
|
||||
/**
|
||||
* Set character set of the MySQL-connection.
|
||||
*
|
||||
* Trys to set the connection charset and returns it.
|
||||
* Throw Exception on failure.
|
||||
*
|
||||
* @param string $charset
|
||||
* @throws Exception
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function setConnectionCharset($charset = 'utf8')
|
||||
{
|
||||
if (!$this->_mysqli instanceof mysqli) {
|
||||
$this->dbConnect();
|
||||
}
|
||||
if (!@$this->_mysqli->set_charset($charset)) {
|
||||
$this->sqlError(
|
||||
$charset . ' ' . $this->_mysqli->error,
|
||||
$this->_mysqli->errno
|
||||
);
|
||||
}
|
||||
$this->connectionCharset = $this->_mysqli->character_set_name();
|
||||
return $this->connectionCharset;
|
||||
}
|
||||
/**
|
||||
* Get list of databases
|
||||
*
|
||||
* Gets list of all databases that the actual MySQL-User has access to
|
||||
* and saves it in $this->databases.
|
||||
* Returns true on success or false on error.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function getDatabases()
|
||||
{
|
||||
if (!$this->_mysqli instanceof mysqli) $this->dbConnect();
|
||||
$res = $this->query('SHOW DATABASES', self::ARRAY_ASSOC);
|
||||
$this->databases = array();
|
||||
foreach ($res as $r) {
|
||||
$this->databases[] = $r['Database'];
|
||||
}
|
||||
sort($this->databases);
|
||||
return $this->databases;
|
||||
}
|
||||
/**
|
||||
* 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 Database to select
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function selectDb($database)
|
||||
{
|
||||
if (!$this->_mysqli instanceof mysqli) {
|
||||
$this->dbConnect();
|
||||
}
|
||||
$res=@$this->_mysqli->select_db($database);
|
||||
if ($res===false) {
|
||||
return $this->_mysqli->error;
|
||||
} else {
|
||||
$this->dbSelected = $database;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Get selected database
|
||||
*
|
||||
* @see inc/classes/db/MsdDbFactory#getSelectedDb()
|
||||
* @return string
|
||||
*/
|
||||
public function getSelectedDb()
|
||||
{
|
||||
return $this->dbSelected;
|
||||
}
|
||||
/**
|
||||
* Execute a MySQL-Query
|
||||
*
|
||||
* @throws Exception
|
||||
* @param string $query The query to execute
|
||||
* @param const $kind Type of result set
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function query($query, $kind = self::ARRAY_OBJECT)
|
||||
{
|
||||
if (!$this->_mysqli instanceof mysqli) {
|
||||
$this->dbConnect($this->dbSelected);
|
||||
}
|
||||
//echo "<br>Mysqli: executing query: " . $query;
|
||||
$result = $this->_mysqli->query($query);
|
||||
if (false === $result) {
|
||||
$this->sqlError($this->_mysqli->error, $this->_mysqli->errno);
|
||||
}
|
||||
if (!$result instanceof mysqli_result || $kind === self::SIMPLE) {
|
||||
return $result;
|
||||
}
|
||||
$ret = array();
|
||||
if ($kind === self::ARRAY_OBJECT) {
|
||||
WHILE ($row = $result->fetch_object()) {
|
||||
$ret[] = $row;
|
||||
}
|
||||
} elseif ($kind === self::ARRAY_NUMERIC) {
|
||||
WHILE ($row = $result->fetch_array(MYSQLI_NUM)) {
|
||||
$ret[] = $row;
|
||||
}
|
||||
} elseif ($kind === self::ARRAY_ASSOC) {
|
||||
WHILE ($row = $result->fetch_assoc()) {
|
||||
$ret[] = $row;
|
||||
}
|
||||
}
|
||||
if ($result instanceof mysqli) {
|
||||
$result->close();
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
/**
|
||||
* Get table list of given database(s)
|
||||
*
|
||||
* Stores them in $this->tables[database]
|
||||
* When no database is given, all databases are scanned for tables.
|
||||
* Returns table list for selected or for all databases.
|
||||
*
|
||||
* @param string|array $database
|
||||
* @return array
|
||||
*/
|
||||
public function getTables($database = false)
|
||||
{
|
||||
if (!$this->_mysqli instanceof mysqli) {
|
||||
$this->dbConnect($this->dbSelected);
|
||||
}
|
||||
if (!isset($this->tables)) {
|
||||
$this->tables = array();
|
||||
}
|
||||
if ($database !== false) {
|
||||
//only list tables of selected database
|
||||
if (is_array($database)) {
|
||||
$databases = $database;
|
||||
} else {
|
||||
$databases = array();
|
||||
$databases[0] = $database;
|
||||
}
|
||||
} else {
|
||||
//list tables for all databases
|
||||
$databases = $this->databases;
|
||||
}
|
||||
// get tablenames for each database
|
||||
foreach ($databases as $db) {
|
||||
$this->tables[$db] = array();
|
||||
$sql = 'SHOW TABLES FROM `' . $db . '`';
|
||||
$res = $this->query($sql, self::ARRAY_NUMERIC);
|
||||
foreach ($res as $val) {
|
||||
$this->tables[$db][] = $val[0];
|
||||
}
|
||||
}
|
||||
return is_string($database) ? $this->tables[$database] : $this->tables;
|
||||
}
|
||||
/**
|
||||
* Gets extended table information for one or all tables
|
||||
*
|
||||
* @param string $table
|
||||
* @param string $database
|
||||
* @return array
|
||||
*/
|
||||
public function getTableStatus($table = false, $database = false)
|
||||
{
|
||||
if ($database !== false && $database != $this->dbSelected) {
|
||||
$this->selectDb($database);
|
||||
}
|
||||
if (!$this->_mysqli instanceof mysqli) {
|
||||
$this->selectDb($this->dbSelected);
|
||||
}
|
||||
if (!isset($this->tableinfo)) {
|
||||
$this->tableinfo = array();
|
||||
}
|
||||
if (!isset($this->tableinfo[$this->dbSelected])) {
|
||||
$this->tableinfo[$this->dbSelected] = array();
|
||||
}
|
||||
$sql = 'SHOW TABLE STATUS';
|
||||
if ($table !== false) {
|
||||
$sql .= ' LIKE \'' . $table . '\'';
|
||||
}
|
||||
$res = $this->query($sql, self::ARRAY_ASSOC);
|
||||
if (is_array($res)) {
|
||||
foreach ($res as $r) {
|
||||
$tablename = $r['Name'];
|
||||
unset($r['Name']);
|
||||
$this->tableinfo[$this->dbSelected][$tablename] = $r;
|
||||
}
|
||||
} elseif ($res === false) {
|
||||
$this->sqlError($this->_mysqli->error, $this->_mysqli->errno);
|
||||
}
|
||||
if ($table !== false) {
|
||||
if (isset($this->tableinfo[$this->dbSelected][$table])) {
|
||||
return $this->tableinfo[$this->dbSelected][$table];
|
||||
}
|
||||
}
|
||||
return $this->tableinfo[$this->dbSelected];
|
||||
}
|
||||
/**
|
||||
* Returns the CREATE Statement of a table.
|
||||
*
|
||||
* @throws Exception
|
||||
* @param string $table
|
||||
* @param string $database
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getTableCreate($table, $database = false)
|
||||
{
|
||||
if (false === $database) {
|
||||
$database = $this->dbSelected;
|
||||
}
|
||||
if ($database != $this->dbSelected) {
|
||||
if (false === $this->selectDB($database)) {
|
||||
$this->sqlError($this->_mysqli->error, $this->_mysqli->errno);
|
||||
}
|
||||
}
|
||||
if (!$this->_mysqli instanceof mysqli) {
|
||||
$this->dbConnect($this->dbSelected);
|
||||
}
|
||||
$sql = 'SHOW CREATE TABLE `' . $table . '`';
|
||||
$res = $this->query($sql, self::ARRAY_ASSOC);
|
||||
if (isset($res[0]['Create Table'])) {
|
||||
return $res[0]['Create Table'];
|
||||
} else {
|
||||
$this->sqlError($this->_mysqli->error, $this->_mysqli->errno);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Gets the full description of all columns of a table
|
||||
*
|
||||
* Saves it to $this->metaTables[$database][$table].
|
||||
*
|
||||
* @param string $table
|
||||
* @param string $database
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getTableColumns($table, $database = false)
|
||||
{
|
||||
if (!$this->_mysqli instanceof mysqli) {
|
||||
$this->dbConnect($this->dbSelected);
|
||||
}
|
||||
if (false === $database) {
|
||||
$database = $this->dbSelected;
|
||||
}
|
||||
if ($database != $this->dbSelected) {
|
||||
if (false === $this->selectDB($database)) {
|
||||
$this->sqlError($this->_mysqli->error, $this->_mysqli->errno);
|
||||
}
|
||||
}
|
||||
$sql = 'SHOW FULL FIELDS FROM `' . $table . '`';
|
||||
$res = $this->query($sql, self::ARRAY_ASSOC);
|
||||
if (!isset($this->metaTables[$database])) $this->metaTables[$database] = array();
|
||||
if (is_array($res)) {
|
||||
$this->metaTables[$database][$table] = array();
|
||||
foreach ($res as $r) {
|
||||
$this->metaTables[$database][$table][$r['Field']] = $r;
|
||||
}
|
||||
}
|
||||
return $this->metaTables[$database][$table];
|
||||
}
|
||||
/**
|
||||
* Gets the number of affected rows for the last query
|
||||
*
|
||||
* @see inc/classes/db/MsdDbFactory#affectedRows()
|
||||
* @return integer
|
||||
*/
|
||||
public function getAffectedRows()
|
||||
{
|
||||
return $this->_mysqli->affected_rows;
|
||||
}
|
||||
/**
|
||||
* Escape a value 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->_mysqli->real_escape_string($val);
|
||||
}
|
||||
/**
|
||||
* Optimize a table. Returns true on success or MySQL-Error.
|
||||
*
|
||||
* @param string $table Name of table
|
||||
*
|
||||
* @return boolean or string true on success or mysql_error() on failure
|
||||
*/
|
||||
function optimizeTable($table)
|
||||
{
|
||||
$sql = 'OPTIMIZE TABLE `' . $this->dbSelected . '`.`' . $table . '`';
|
||||
$res = $this->query($sql, MsdDbFactory::ARRAY_ASSOC);
|
||||
if (isset($res[0]['Msg_text'])) {
|
||||
return $res[0];
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Laden …
Tabelle hinzufügen
Einen Link hinzufügen
In neuem Issue referenzieren