Query/apiDocumentation/files/src/Drivers/Sqlite/Util.php.txt

117 lines
2.5 KiB
Plaintext
Raw Normal View History

2023-01-20 11:30:51 -05:00
<?php declare(strict_types=1);
/**
* Query
*
* SQL Query Builder / Database Abstraction Layer
*
* PHP version 8.1
*
* @package Query
* @author Timothy J. Warren <tim@timshome.page>
* @copyright 2012 - 2023 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link https://git.timshomepage.net/aviat/Query
2023-03-17 16:34:59 -04:00
* @version 4.1.0
2023-01-20 11:30:51 -05:00
*/
2023-03-17 16:28:07 -04:00
2023-01-20 11:30:51 -05:00
namespace Query\Drivers\Sqlite;
use PDO;
use Query\Drivers\AbstractUtil;
/**
* SQLite-specific backup, import and creation methods
*/
2023-03-17 16:28:07 -04:00
class Util extends AbstractUtil
{
2023-01-20 11:30:51 -05:00
/**
* Create an SQL backup file for the current database's data
*/
public function backupData(array $excluded=[]): string
{
// Get a list of all the objects
$sql = 'SELECT DISTINCT "name"
FROM "sqlite_master"
WHERE "type"=\'table\'';
2023-03-17 16:28:07 -04:00
if ( ! empty($excluded))
2023-01-20 11:30:51 -05:00
{
2023-03-17 16:28:07 -04:00
$sql .= " AND \"name\" NOT IN('" . implode("','", $excluded) . "')";
2023-01-20 11:30:51 -05:00
}
$res = $this->getDriver()->query($sql);
$result = $res->fetchAll(PDO::FETCH_ASSOC);
unset($res);
$outputSql = '';
// Get the data for each object
2023-03-17 16:28:07 -04:00
foreach ($result as $r)
2023-01-20 11:30:51 -05:00
{
2023-03-17 16:28:07 -04:00
$sql = 'SELECT * FROM "' . $r['name'] . '"';
2023-01-20 11:30:51 -05:00
$res = $this->getDriver()->query($sql);
$objRes = $res->fetchAll(PDO::FETCH_ASSOC);
unset($res);
// If the row is empty, continue
if (empty($objRes))
{
continue;
}
// Nab the column names by getting the keys of the first row
$columns = array_keys(current($objRes));
$insertRows = [];
// Create the insert statements
2023-03-17 16:28:07 -04:00
foreach ($objRes as $row)
2023-01-20 11:30:51 -05:00
{
$row = array_values($row);
// Quote values as needed by type
foreach ($row as $i => $_)
{
$row[$i] = (is_numeric($row[$i]))
? $row[$i]
: $this->getDriver()->quote($row[$i]);
}
2023-03-17 16:28:07 -04:00
$rowString = 'INSERT INTO "' . $r['name'] . '" ("' . implode('","', $columns) . '") VALUES (' . implode(',', $row) . ');';
2023-01-20 11:30:51 -05:00
unset($row);
$insertRows[] = $rowString;
}
unset($objRes);
2023-03-17 16:28:07 -04:00
$outputSql .= "\n\n" . implode("\n", $insertRows);
2023-01-20 11:30:51 -05:00
}
return $outputSql;
}
/**
* Create an SQL backup file for the current database's structure
*/
public function backupStructure(): string
{
// Fairly easy for SQLite...just query the master table
$sql = 'SELECT "sql" FROM "sqlite_master"';
$res = $this->getDriver()->query($sql);
$result = $res->fetchAll(PDO::FETCH_ASSOC);
$sqlArray = [];
2023-03-17 16:28:07 -04:00
foreach ($result as $r)
2023-01-20 11:30:51 -05:00
{
$sqlArray[] = $r['sql'];
}
return implode(";\n", $sqlArray) . ';';
}
2023-03-17 16:28:07 -04:00
}