2016-10-12 22:12:25 -04:00
|
|
|
<?php declare(strict_types=1);
|
2012-04-18 15:53:06 -04:00
|
|
|
/**
|
|
|
|
* Query
|
|
|
|
*
|
2016-09-07 13:17:17 -04:00
|
|
|
* SQL Query Builder / Database Abstraction Layer
|
2012-04-18 15:53:06 -04:00
|
|
|
*
|
2018-01-19 13:43:19 -05:00
|
|
|
* PHP version 7.1
|
2016-09-07 13:17:17 -04:00
|
|
|
*
|
|
|
|
* @package Query
|
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2018-01-19 13:43:19 -05:00
|
|
|
* @copyright 2012 - 2018 Timothy J. Warren
|
2016-09-07 13:17:17 -04:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
* @link https://git.timshomepage.net/aviat4ion/Query
|
2012-04-18 15:53:06 -04:00
|
|
|
*/
|
2015-07-16 16:56:13 -04:00
|
|
|
namespace Query\Drivers\Mysql;
|
2014-04-02 17:08:50 -04:00
|
|
|
|
2016-10-13 21:55:23 -04:00
|
|
|
use PDO;
|
2016-09-07 17:39:19 -04:00
|
|
|
use Query\Drivers\AbstractUtil;
|
|
|
|
|
2012-04-18 15:53:06 -04:00
|
|
|
/**
|
|
|
|
* MySQL-specific backup, import and creation methods
|
|
|
|
*/
|
2016-09-07 17:39:19 -04:00
|
|
|
class Util extends AbstractUtil {
|
2012-04-18 15:53:06 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an SQL backup file for the current database's structure
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-01-22 15:43:56 -05:00
|
|
|
public function backupStructure(): string
|
2012-04-18 15:53:06 -04:00
|
|
|
{
|
2016-09-07 13:10:03 -04:00
|
|
|
$string = [];
|
2014-03-20 11:20:30 -04:00
|
|
|
|
2012-05-09 13:54:38 -04:00
|
|
|
// Get databases
|
2016-10-13 21:55:23 -04:00
|
|
|
$dbs = $this->getDriver()->getDbs();
|
2014-03-20 11:20:30 -04:00
|
|
|
|
2012-05-15 13:08:35 -04:00
|
|
|
foreach($dbs as &$d)
|
2012-05-09 13:54:38 -04:00
|
|
|
{
|
2012-05-15 13:35:59 -04:00
|
|
|
// Skip built-in dbs
|
2018-01-24 13:14:03 -05:00
|
|
|
// @codeCoverageIgnoreStart
|
2018-01-22 15:43:56 -05:00
|
|
|
if ($d === 'mysql')
|
2015-11-11 09:25:21 -05:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2018-01-24 13:14:03 -05:00
|
|
|
// @codeCoverageIgnoreEnd
|
2014-03-20 11:20:30 -04:00
|
|
|
|
2012-05-09 13:54:38 -04:00
|
|
|
// Get the list of tables
|
2016-10-13 21:55:23 -04:00
|
|
|
$tables = $this->getDriver()->driverQuery("SHOW TABLES FROM `{$d}`", TRUE);
|
2014-03-20 11:20:30 -04:00
|
|
|
|
2014-04-08 17:13:41 -04:00
|
|
|
foreach($tables as $table)
|
2012-05-15 13:08:35 -04:00
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
$array = $this->getDriver()->driverQuery("SHOW CREATE TABLE `{$d}`.`{$table}`", FALSE);
|
2014-04-08 17:13:41 -04:00
|
|
|
$row = current($array);
|
|
|
|
|
2015-11-11 09:25:21 -05:00
|
|
|
if ( ! isset($row['Create Table']))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2014-04-08 17:13:41 -04:00
|
|
|
|
|
|
|
|
|
|
|
$string[] = $row['Create Table'];
|
2012-05-15 13:08:35 -04:00
|
|
|
}
|
2012-05-09 13:54:38 -04:00
|
|
|
}
|
2014-03-20 11:20:30 -04:00
|
|
|
|
2012-05-15 13:35:59 -04:00
|
|
|
return implode("\n\n", $string);
|
2012-04-18 15:53:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an SQL backup file for the current database's data
|
|
|
|
*
|
2012-04-18 16:28:12 -04:00
|
|
|
* @param array $exclude
|
2012-04-18 15:53:06 -04:00
|
|
|
* @return string
|
|
|
|
*/
|
2018-01-22 15:43:56 -05:00
|
|
|
public function backupData($exclude=[]): string
|
2012-04-18 15:53:06 -04:00
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
$tables = $this->getDriver()->getTables();
|
2014-03-20 11:20:30 -04:00
|
|
|
|
2012-04-19 08:59:10 -04:00
|
|
|
// Filter out the tables you don't want
|
|
|
|
if( ! empty($exclude))
|
|
|
|
{
|
|
|
|
$tables = array_diff($tables, $exclude);
|
|
|
|
}
|
2014-03-20 11:20:30 -04:00
|
|
|
|
2016-10-13 21:55:23 -04:00
|
|
|
$outputSql = '';
|
2014-03-20 11:20:30 -04:00
|
|
|
|
2012-04-19 08:59:10 -04:00
|
|
|
// Select the rows from each Table
|
2012-07-05 14:19:49 -04:00
|
|
|
foreach($tables as $t)
|
2012-04-19 08:59:10 -04:00
|
|
|
{
|
|
|
|
$sql = "SELECT * FROM `{$t}`";
|
2016-10-13 21:55:23 -04:00
|
|
|
$res = $this->getDriver()->query($sql);
|
|
|
|
$rows = $res->fetchAll(PDO::FETCH_ASSOC);
|
2014-03-20 11:20:30 -04:00
|
|
|
|
2012-04-19 08:59:10 -04:00
|
|
|
// Skip empty tables
|
2015-11-11 09:25:21 -05:00
|
|
|
if (count($rows) < 1)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2014-03-20 11:20:30 -04:00
|
|
|
|
2012-04-19 08:59:10 -04:00
|
|
|
// Nab the column names by getting the keys of the first row
|
|
|
|
$columns = @array_keys($rows[0]);
|
|
|
|
|
2016-10-13 21:55:23 -04:00
|
|
|
$insertRows = [];
|
2014-03-20 11:20:30 -04:00
|
|
|
|
2012-04-19 08:59:10 -04:00
|
|
|
// Create the insert statements
|
2012-07-05 14:19:49 -04:00
|
|
|
foreach($rows as $row)
|
2012-04-19 08:59:10 -04:00
|
|
|
{
|
|
|
|
$row = array_values($row);
|
2014-03-20 11:20:30 -04:00
|
|
|
|
2012-07-05 14:19:49 -04:00
|
|
|
// Workaround for Quercus
|
|
|
|
foreach($row as &$r)
|
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
$r = $this->getDriver()->quote($r);
|
2012-07-05 14:19:49 -04:00
|
|
|
}
|
2012-04-19 08:59:10 -04:00
|
|
|
$row = array_map('trim', $row);
|
|
|
|
|
2016-10-13 21:55:23 -04:00
|
|
|
$rowString = 'INSERT INTO `'.trim($t).'` (`'.implode('`,`', $columns).'`) VALUES ('.implode(',', $row).');';
|
2012-04-19 08:59:10 -04:00
|
|
|
|
|
|
|
$row = NULL;
|
|
|
|
|
2016-10-13 21:55:23 -04:00
|
|
|
$insertRows[] = $rowString;
|
2012-04-19 08:59:10 -04:00
|
|
|
}
|
|
|
|
|
2016-10-13 21:55:23 -04:00
|
|
|
$outputSql .= "\n\n".implode("\n", $insertRows)."\n";
|
2012-04-19 08:59:10 -04:00
|
|
|
}
|
2014-03-20 11:20:30 -04:00
|
|
|
|
2016-10-13 21:55:23 -04:00
|
|
|
return $outputSql;
|
2012-04-18 15:53:06 -04:00
|
|
|
}
|
2016-10-13 21:55:23 -04:00
|
|
|
}
|