2016-11-06 12:36:07 +00:00
|
|
|
<?php
|
|
|
|
namespace devilbox;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @requires devilbox::Logger
|
|
|
|
*/
|
2017-05-06 09:13:33 +00:00
|
|
|
class Mysql extends _Base implements _iBase
|
2016-11-06 12:36:07 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
/*********************************************************************************
|
|
|
|
*
|
|
|
|
* Statics
|
|
|
|
*
|
|
|
|
*********************************************************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mysql instance
|
|
|
|
* @var Mysql|null
|
|
|
|
*/
|
|
|
|
protected static $instance = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Singleton Instance getter.
|
|
|
|
*
|
|
|
|
* @param string $user Username
|
|
|
|
* @param string $pass Password
|
|
|
|
* @param string $host Host
|
|
|
|
* @return object|null
|
|
|
|
*/
|
2017-05-06 09:13:33 +00:00
|
|
|
public static function getInstance($host, $user, $pass)
|
2016-11-06 12:36:07 +00:00
|
|
|
{
|
|
|
|
if (!isset(static::$instance)) {
|
|
|
|
static::$instance = new static($user, $pass, $host);
|
|
|
|
}
|
|
|
|
// If current MySQL instance was unable to connect
|
|
|
|
if ((static::$instance->getConnectError())) {
|
2017-05-06 09:13:33 +00:00
|
|
|
// loadClass('Logger')->error('Instance has errors:' . "\r\n" . var_export(static::$instance, true) . "\r\n");
|
2016-11-06 12:36:07 +00:00
|
|
|
}
|
|
|
|
return static::$instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connect to database
|
|
|
|
*
|
|
|
|
* @param string $err Reference to error message
|
|
|
|
* @param string $user MySQL username
|
|
|
|
* @param string $pass MySQL password
|
|
|
|
* @param string $host MySQL hostname
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2017-05-06 09:13:33 +00:00
|
|
|
public static function testConnection(&$err, $host, $user, $pass)
|
2016-11-06 12:36:07 +00:00
|
|
|
{
|
|
|
|
$err = false;
|
|
|
|
|
|
|
|
// Silence errors and try to connect
|
|
|
|
error_reporting(0);
|
|
|
|
$link = mysqli_connect($host, $user, $pass);
|
|
|
|
error_reporting(-1);
|
|
|
|
|
|
|
|
if (mysqli_connect_errno()) {
|
|
|
|
$err = 'Failed to connect: ' .mysqli_connect_error();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
mysqli_close($link);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*********************************************************************************
|
|
|
|
*
|
|
|
|
* Private Variables
|
|
|
|
*
|
|
|
|
*********************************************************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MySQL Resource
|
|
|
|
* @var resource|null
|
|
|
|
*/
|
|
|
|
private $_link = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*********************************************************************************
|
|
|
|
*
|
|
|
|
* Construct/Destructor
|
|
|
|
*
|
|
|
|
*********************************************************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Use singleton getInstance() instead.
|
|
|
|
*
|
|
|
|
* @param string $user Username
|
|
|
|
* @param string $pass Password
|
|
|
|
* @param string $host Host
|
|
|
|
*/
|
|
|
|
public function __construct($user, $pass, $host)
|
|
|
|
{
|
|
|
|
// Silence errors and try to connect
|
|
|
|
error_reporting(0);
|
|
|
|
$link = mysqli_connect($host, $user, $pass);
|
|
|
|
error_reporting(-1);
|
|
|
|
|
|
|
|
if (mysqli_connect_errno()) {
|
2017-05-06 09:13:33 +00:00
|
|
|
$this->setConnectError('Failed to connect: ' .mysqli_connect_error());
|
|
|
|
$this->setConnectErrno(mysqli_connect_errno());
|
|
|
|
//loadClass('Logger')->error($this->_connect_error);
|
2016-11-06 12:36:07 +00:00
|
|
|
} else {
|
|
|
|
$this->_link = $link;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor
|
|
|
|
*/
|
|
|
|
public function __destruct()
|
|
|
|
{
|
|
|
|
if ($this->_link) {
|
|
|
|
mysqli_close($this->_link);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************
|
|
|
|
*
|
|
|
|
* MySQL Select functions
|
|
|
|
*
|
|
|
|
*********************************************************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Query Database
|
|
|
|
*
|
|
|
|
* @param string $query MySQL Query
|
|
|
|
* @param function $callback Callback function
|
|
|
|
* @return mixed[]
|
|
|
|
*/
|
|
|
|
public function select($query, $callback = null)
|
|
|
|
{
|
|
|
|
if (!$this->_link) {
|
2017-05-06 09:13:33 +00:00
|
|
|
loadClass('Logger')->error('MySQL error, link is no resource in select(): '.$query);
|
2016-11-06 12:36:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!($result = mysqli_query($this->_link, $query))) {
|
2017-05-06 09:13:33 +00:00
|
|
|
$this->setError(mysqli_error($this->_link));
|
|
|
|
$this->setErrno(mysqli_errno($this->_link));
|
|
|
|
loadClass('Logger')->error($this->getError());
|
2016-11-06 12:36:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = array();
|
|
|
|
|
|
|
|
if ($callback) {
|
|
|
|
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
|
|
|
|
$callback($row, $data);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
|
|
|
|
$data[] = $row;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mysqli_free_result($result);
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all MySQL Databases.
|
|
|
|
* @return mixed[] Array of databases
|
|
|
|
*/
|
|
|
|
public function getDatabases()
|
|
|
|
{
|
|
|
|
$callback = function ($row, &$data) {
|
|
|
|
$data[$row['database']] = array(
|
|
|
|
'charset' => $row['charset'],
|
|
|
|
'collation' => $row['collation']
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
$sql = "SELECT
|
|
|
|
S.SCHEMA_NAME AS 'database',
|
|
|
|
S.DEFAULT_COLLATION_NAME AS 'collation',
|
|
|
|
S.default_character_set_name AS 'charset'
|
|
|
|
FROM
|
2017-05-06 09:13:33 +00:00
|
|
|
information_schema.SCHEMATA AS S;";
|
2016-11-06 12:36:07 +00:00
|
|
|
|
|
|
|
$databases = $this->select($sql, $callback);
|
|
|
|
|
|
|
|
return $databases ? $databases : array();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get Database size in Megabytes.
|
|
|
|
*
|
|
|
|
* @param string $database Database name.
|
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
public function getDBSize($database)
|
|
|
|
{
|
|
|
|
$callback = function ($row, &$data) {
|
|
|
|
$data = $row['size'];
|
|
|
|
};
|
|
|
|
|
|
|
|
$sql = "SELECT
|
|
|
|
ROUND( SUM((T.data_length+T.index_length)/1048576), 2 ) AS 'size'
|
|
|
|
FROM
|
|
|
|
information_schema.TABLES AS T
|
|
|
|
WHERE
|
|
|
|
T.TABLE_SCHEMA = '".$database."';";
|
|
|
|
|
|
|
|
$size = $this->select($sql, $callback);
|
|
|
|
return $size ? $size : 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get Number of Tables per Database
|
|
|
|
*
|
|
|
|
* @param string $database Database name.
|
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
public function getTableCount($database)
|
|
|
|
{
|
|
|
|
$callback = function ($row, &$data) {
|
|
|
|
$data = $row['count'];
|
|
|
|
};
|
|
|
|
|
|
|
|
$sql = "SELECT
|
|
|
|
COUNT(*) AS 'count'
|
|
|
|
FROM
|
|
|
|
information_schema.TABLES AS T
|
|
|
|
WHERE
|
|
|
|
T.TABLE_SCHEMA = '".$database."';";
|
|
|
|
|
|
|
|
$count = $this->select($sql, $callback);
|
|
|
|
return $count ? $count : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-06 09:13:33 +00:00
|
|
|
/**
|
|
|
|
* Read out MySQL Server configuration by variable
|
|
|
|
*
|
|
|
|
* @param string|null $key Config key name
|
|
|
|
* @return string|mixed[]
|
|
|
|
*/
|
|
|
|
public function getConfig($key = null)
|
|
|
|
{
|
|
|
|
// Get all configs as array
|
|
|
|
if ($key === null) {
|
|
|
|
$callback = function ($row, &$data) {
|
|
|
|
$key = $row['Variable_name'];
|
|
|
|
$val = $row['Value'];
|
|
|
|
$data[$key] = $val;
|
|
|
|
};
|
|
|
|
|
2017-05-08 07:22:10 +00:00
|
|
|
$config = $this->select('SHOW VARIABLES;', $callback);
|
|
|
|
if (!$config) {
|
|
|
|
$config = array();
|
|
|
|
}
|
|
|
|
return $config;
|
2017-05-06 09:13:33 +00:00
|
|
|
|
|
|
|
} else { // Get single config
|
|
|
|
|
|
|
|
$key = str_replace('-', '_', $key);
|
|
|
|
|
|
|
|
$callback = function ($row, &$data) use ($key) {
|
|
|
|
$data = isset($row['Value']) ? $row['Value'] : false;
|
|
|
|
};
|
2016-11-06 12:36:07 +00:00
|
|
|
|
2017-05-06 09:13:33 +00:00
|
|
|
$sql = 'SHOW VARIABLES WHERE Variable_Name = "'.$key.'";';
|
|
|
|
$val = $this->select($sql, $callback);
|
|
|
|
|
|
|
|
if (is_array($val) && $val) {
|
|
|
|
return array_values($val)[0];
|
|
|
|
} else {
|
|
|
|
return $val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-06 12:36:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*********************************************************************************
|
|
|
|
*
|
2017-05-06 09:13:33 +00:00
|
|
|
* Interface required functions
|
2016-11-06 12:36:07 +00:00
|
|
|
*
|
|
|
|
*********************************************************************************/
|
|
|
|
|
|
|
|
/**
|
2017-05-06 09:13:33 +00:00
|
|
|
* Get MySQL Name.
|
2016-11-06 12:36:07 +00:00
|
|
|
*
|
2017-05-06 09:13:33 +00:00
|
|
|
* @return string MySQL short name.
|
2016-11-06 12:36:07 +00:00
|
|
|
*/
|
2017-05-06 09:13:33 +00:00
|
|
|
public function getName($default = 'MySQL')
|
2016-11-06 12:36:07 +00:00
|
|
|
{
|
2017-05-06 09:13:33 +00:00
|
|
|
if (!static::isAvailable('mysql')) {
|
|
|
|
return $default;
|
|
|
|
}
|
2016-11-06 12:36:07 +00:00
|
|
|
|
2017-05-06 09:13:33 +00:00
|
|
|
$name = $this->egrep('/[a-zA-Z0-9]+/', $this->getConfig('version_comment'));
|
2016-11-06 12:36:07 +00:00
|
|
|
|
2017-05-06 09:13:33 +00:00
|
|
|
if (!$name) {
|
|
|
|
loadClass('Logger')->error('Could not get MySQL Name');
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
return $name;
|
2016-11-06 12:36:07 +00:00
|
|
|
}
|
|
|
|
|
2017-05-06 09:13:33 +00:00
|
|
|
|
2016-11-06 12:36:07 +00:00
|
|
|
/**
|
2017-05-06 09:13:33 +00:00
|
|
|
* Get MySQL Version.
|
2016-11-06 12:36:07 +00:00
|
|
|
*
|
2017-05-06 09:13:33 +00:00
|
|
|
* @return string MySQL version.
|
2016-11-06 12:36:07 +00:00
|
|
|
*/
|
2017-05-06 09:13:33 +00:00
|
|
|
public function getVersion()
|
2016-11-06 12:36:07 +00:00
|
|
|
{
|
2017-05-06 09:13:33 +00:00
|
|
|
if (!static::isAvailable('mysql')) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$version = $this->egrep('/[.0-9]+/', $this->getConfig('version'));
|
|
|
|
|
|
|
|
if (!$version) {
|
|
|
|
loadClass('Logger')->error('Could not get MySQL version');
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return $version;
|
2016-11-06 12:36:07 +00:00
|
|
|
}
|
|
|
|
}
|