Source of file Util.php

Size: 2,638 Bytes - Last Modified: 2018-01-24T19:48:07+00:00

src/Drivers/Sqlite/Util.php

12345678910111213141516171819202122232425262728293031323334353637
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testBackupData
38394041
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testBackupData
4243
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testBackupData
444546
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testBackupData
47
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testBackupData
4849
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testBackupData
5051
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testBackupData
525354
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testBackupData
5556
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testBackupData
57
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testBackupData
58
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testBackupData
5960
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testBackupData
616263
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testBackupData
6465
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testBackupData
66676869
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testBackupData
7071
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testBackupData
727374
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testBackupData
7576
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testBackupData
777879
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testBackupData
8081
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testBackupData
828384
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testBackupData
8586
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testBackupData
8788
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testBackupData
899091
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testBackupData
9293
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testBackupData
949596
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testBackupData
979899100101102103104105106107
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testBackupStructure
108
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testBackupStructure
109
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testBackupStructure
110111
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testBackupStructure
112113
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testBackupStructure
114115
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testBackupStructure
116117118
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testBackupStructure
119120
<?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
 *
 * @method mixed query(string $sql)
 * @method string quote(string $str)
 */
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) . ";";
	}
}