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
|
|
|
*
|
2019-12-11 16:49:42 -05:00
|
|
|
* PHP version 7.2
|
2016-09-07 13:17:17 -04:00
|
|
|
*
|
|
|
|
* @package Query
|
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2019-12-11 16:49:42 -05:00
|
|
|
* @copyright 2012 - 2019 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
|
|
|
|
* @version 3.0.0
|
2012-04-18 15:53:06 -04:00
|
|
|
*/
|
2015-07-16 16:56:13 -04:00
|
|
|
namespace Query\Drivers\Sqlite;
|
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
|
|
|
/**
|
|
|
|
* SQLite-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 data
|
|
|
|
*
|
|
|
|
* @param array $excluded
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-01-22 15:43:56 -05:00
|
|
|
public function backupData(array $excluded=[]): string
|
2012-04-18 15:53:06 -04:00
|
|
|
{
|
|
|
|
// Get a list of all the objects
|
2014-04-08 15:19:47 -04:00
|
|
|
$sql = 'SELECT DISTINCT "name"
|
|
|
|
FROM "sqlite_master"
|
|
|
|
WHERE "type"=\'table\'';
|
2012-04-18 15:53:06 -04:00
|
|
|
|
|
|
|
if( ! empty($excluded))
|
|
|
|
{
|
2014-04-08 15:19:47 -04:00
|
|
|
$sql .= " AND \"name\" NOT IN('".implode("','", $excluded)."')";
|
2012-04-18 15:53:06 -04:00
|
|
|
}
|
|
|
|
|
2016-10-13 21:55:23 -04:00
|
|
|
$res = $this->getDriver()->query($sql);
|
|
|
|
$result = $res->fetchAll(PDO::FETCH_ASSOC);
|
2012-04-18 15:53:06 -04:00
|
|
|
|
|
|
|
unset($res);
|
|
|
|
|
2016-10-13 21:55:23 -04:00
|
|
|
$outputSql = '';
|
2012-04-18 15:53:06 -04:00
|
|
|
|
|
|
|
// Get the data for each object
|
|
|
|
foreach($result as $r)
|
|
|
|
{
|
|
|
|
$sql = 'SELECT * FROM "'.$r['name'].'"';
|
2016-10-13 21:55:23 -04:00
|
|
|
$res = $this->getDriver()->query($sql);
|
|
|
|
$objRes = $res->fetchAll(PDO::FETCH_ASSOC);
|
2012-04-18 15:53:06 -04:00
|
|
|
|
|
|
|
unset($res);
|
2014-03-20 11:20:30 -04:00
|
|
|
|
2014-02-07 14:18:08 -05:00
|
|
|
// If the row is empty, continue;
|
2016-10-13 21:55:23 -04:00
|
|
|
if (empty($objRes))
|
2015-11-11 09:25:21 -05:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2012-04-18 15:53:06 -04:00
|
|
|
|
|
|
|
// Nab the column names by getting the keys of the first row
|
2016-10-13 21:55:23 -04:00
|
|
|
$columns = array_keys(current($objRes));
|
2012-04-18 15:53:06 -04:00
|
|
|
|
2016-10-13 21:55:23 -04:00
|
|
|
$insertRows = [];
|
2012-04-18 15:53:06 -04:00
|
|
|
|
|
|
|
// Create the insert statements
|
2016-10-13 21:55:23 -04:00
|
|
|
foreach($objRes as $row)
|
2012-04-18 15:53:06 -04:00
|
|
|
{
|
|
|
|
$row = array_values($row);
|
|
|
|
|
|
|
|
// Quote values as needed by type
|
2019-12-11 16:49:06 -05:00
|
|
|
foreach ($row as $i => $_)
|
2012-04-18 15:53:06 -04:00
|
|
|
{
|
2019-12-11 16:49:06 -05:00
|
|
|
$row[$i] = (is_numeric($row[$i]))
|
|
|
|
? $row[$i]
|
|
|
|
: $this->getDriver()->quote($row[$i]);
|
2012-04-18 15:53:06 -04:00
|
|
|
}
|
|
|
|
|
2016-10-13 21:55:23 -04:00
|
|
|
$rowString = 'INSERT INTO "'.$r['name'].'" ("'.implode('","', $columns).'") VALUES ('.implode(',', $row).');';
|
2012-04-18 15:53:06 -04:00
|
|
|
|
|
|
|
unset($row);
|
|
|
|
|
2016-10-13 21:55:23 -04:00
|
|
|
$insertRows[] = $rowString;
|
2012-04-18 15:53:06 -04:00
|
|
|
}
|
|
|
|
|
2016-10-13 21:55:23 -04:00
|
|
|
unset($objRes);
|
2012-04-18 15:53:06 -04:00
|
|
|
|
2016-10-13 21:55:23 -04:00
|
|
|
$outputSql .= "\n\n".implode("\n", $insertRows);
|
2012-04-18 15:53:06 -04:00
|
|
|
}
|
|
|
|
|
2016-10-13 21:55:23 -04:00
|
|
|
return $outputSql;
|
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
|
|
|
{
|
|
|
|
// Fairly easy for SQLite...just query the master table
|
|
|
|
$sql = 'SELECT "sql" FROM "sqlite_master"';
|
2016-10-13 21:55:23 -04:00
|
|
|
$res = $this->getDriver()->query($sql);
|
|
|
|
$result = $res->fetchAll(PDO::FETCH_ASSOC);
|
2012-04-18 15:53:06 -04:00
|
|
|
|
2016-10-13 21:55:23 -04:00
|
|
|
$sqlArray = [];
|
2012-04-18 15:53:06 -04:00
|
|
|
|
|
|
|
foreach($result as $r)
|
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
$sqlArray[] = $r['sql'];
|
2012-04-18 15:53:06 -04:00
|
|
|
}
|
|
|
|
|
2019-12-11 16:49:06 -05:00
|
|
|
return implode(";\n", $sqlArray) . ';';
|
2012-04-18 15:53:06 -04:00
|
|
|
}
|
2016-10-13 21:55:23 -04:00
|
|
|
}
|