Source of file Util.php
Size: 2,560 Bytes - Last Modified: 2018-01-26T15:31:27+00:00
src/Drivers/Sqlite/Util.php
12345678910111213141516171819202122232425262728293031323334
Covered by 1 test(s):
35363738
Covered by 1 test(s):
3940
Covered by 1 test(s):
414243
Covered by 1 test(s):
44
Covered by 1 test(s):
4546
Covered by 1 test(s):
4748
Covered by 1 test(s):
495051
Covered by 1 test(s):
5253
Covered by 1 test(s):
54
Covered by 1 test(s):
55
Covered by 1 test(s):
5657
Covered by 1 test(s):
585960
Covered by 1 test(s):
6162
Covered by 1 test(s):
63646566
Covered by 1 test(s):
6768
Covered by 1 test(s):
697071
Covered by 1 test(s):
7273
Covered by 1 test(s):
747576
Covered by 1 test(s):
7778
Covered by 1 test(s):
798081
Covered by 1 test(s):
8283
Covered by 1 test(s):
8485
Covered by 1 test(s):
868788
Covered by 1 test(s):
8990
Covered by 1 test(s):
919293
Covered by 1 test(s):
949596979899100101102103104
Covered by 1 test(s):
105
Covered by 1 test(s):
106
Covered by 1 test(s):
107108
Covered by 1 test(s):
109110
Covered by 1 test(s):
111112
Covered by 1 test(s):
113114115
Covered by 1 test(s):
116117
| <?php declare(strict_types=1); /** * Query * * SQL Query Builder / Database Abstraction Layer * * PHP version 7.1 * * @package Query * @author Timothy J. Warren <tim@timshomepage.net> * @copyright 2012 - 2018 Timothy J. Warren * @license http://www.opensource.org/licenses/mit-license.html MIT License * @link https://git.timshomepage.net/aviat4ion/Query */ namespace Query\Drivers\Sqlite; use PDO; use Query\Drivers\AbstractUtil; /** * SQLite-specific backup, import and creation methods */ class Util extends AbstractUtil { /** * Create an SQL backup file for the current database's data * * @param array $excluded * @return string */ public function backupData(array $excluded=[]): string { // Get a list of all the objects $sql = 'SELECT DISTINCT "name" FROM "sqlite_master" WHERE "type"=\'table\''; if( ! empty($excluded)) { $sql .= " AND \"name\" NOT IN('".implode("','", $excluded)."')"; } $res = $this->getDriver()->query($sql); $result = $res->fetchAll(PDO::FETCH_ASSOC); unset($res); $outputSql = ''; // Get the data for each object foreach($result as $r) { $sql = 'SELECT * FROM "'.$r['name'].'"'; $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 foreach($objRes as $row) { $row = array_values($row); // Quote values as needed by type for($i=0, $icount=count($row); $i<$icount; $i++) { $row[$i] = is_numeric($row[$i]) ? $row[$i] : $this->getDriver()->quote($row[$i]); } $rowString = 'INSERT INTO "'.$r['name'].'" ("'.implode('","', $columns).'") VALUES ('.implode(',', $row).');'; unset($row); $insertRows[] = $rowString; } unset($objRes); $outputSql .= "\n\n".implode("\n", $insertRows); } return $outputSql; } /** * Create an SQL backup file for the current database's structure * * @return string */ 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 = []; foreach($result as $r) { $sqlArray[] = $r['sql']; } return implode(";\n", $sqlArray) . ";"; } } |