Query/src/Drivers/Mysql/Util.php

124 lines
2.6 KiB
PHP
Raw Normal View History

2016-10-12 22:12:25 -04:00
<?php declare(strict_types=1);
/**
* Query
*
2016-09-07 13:17:17 -04:00
* SQL Query Builder / Database Abstraction Layer
*
2022-09-29 11:33:08 -04:00
* PHP version 8.1
2016-09-07 13:17:17 -04:00
*
* @package Query
2023-01-20 11:30:51 -05:00
* @author Timothy J. Warren <tim@timshome.page>
* @copyright 2012 - 2023 Timothy J. Warren
2016-09-07 13:17:17 -04:00
* @license http://www.opensource.org/licenses/mit-license.html MIT License
2019-12-11 16:49:42 -05:00
* @link https://git.timshomepage.net/aviat/Query
2022-09-29 11:33:08 -04:00
* @version 4.0.0
*/
2023-03-17 15:18:33 -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;
/**
* MySQL-specific backup, import and creation methods
*/
2023-03-17 15:18:33 -04:00
class Util extends AbstractUtil
{
/**
* Create an SQL backup file for the current database's structure
*/
2018-01-22 15:43:56 -05:00
public function backupStructure(): string
{
2016-09-07 13:10:03 -04:00
$string = [];
// Get databases
$driver = $this->getDriver();
$dbs = $driver->getDbs();
2023-03-17 15:18:33 -04:00
foreach ($dbs as &$d)
{
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
// Get the list of tables
$tables = $driver->driverQuery("SHOW TABLES FROM `{$d}`", TRUE);
2023-03-17 15:18:33 -04:00
foreach ($tables as $table)
2012-05-15 13:08:35 -04:00
{
$array = $driver->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-15 13:35:59 -04:00
return implode("\n\n", $string);
}
/**
* Create an SQL backup file for the current database's data
*/
public function backupData(array $exclude=[]): string
{
$driver = $this->getDriver();
$tables = $driver->getTables();
// Filter out the tables you don't want
2023-03-17 15:18:33 -04:00
if ( ! empty($exclude))
{
$tables = array_diff($tables, $exclude);
}
2016-10-13 21:55:23 -04:00
$outputSql = '';
// Select the rows from each Table
2023-03-17 15:18:33 -04:00
foreach ($tables as $t)
{
$sql = "SELECT * FROM `{$t}`";
$res = $driver->query($sql);
2016-10-13 21:55:23 -04:00
$rows = $res->fetchAll(PDO::FETCH_ASSOC);
// Skip empty tables
if ((is_countable($rows) ? count($rows) : 0) < 1)
2015-11-11 09:25:21 -05:00
{
continue;
}
// 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 = [];
// Create the insert statements
2023-03-17 15:18:33 -04:00
foreach ($rows as $row)
{
$row = array_values($row);
2022-09-29 11:31:25 -04:00
// Quote strings
2023-03-17 15:18:33 -04:00
$row = array_map(static fn ($r) => is_string($r) ? $driver->quote($r) : $r, $row);
$row = array_map('trim', $row);
$rowString = 'INSERT INTO `' . trim((string) $t) . '` (`' . implode('`,`', $columns) . '`) VALUES (' . implode(',', $row) . ');';
$row = NULL;
2016-10-13 21:55:23 -04:00
$insertRows[] = $rowString;
}
2023-03-17 15:18:33 -04:00
$outputSql .= "\n\n" . implode("\n", $insertRows) . "\n";
}
2016-10-13 21:55:23 -04:00
return $outputSql;
}
2023-03-17 15:18:33 -04:00
}