# phpDox - Source of Util.php

# Source of file Util.php

Size: 2,569 Bytes - Last Modified: 2020-04-17T14:56:27-04:00

## src/Drivers/Sqlite/Util.php

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

    <?php declare(strict_types=1);

    /**

     * Query

     *

     * SQL Query Builder / Database Abstraction Layer

     *

     * PHP version 7.4

     *

     * @package     Query

     * @author      Timothy J. Warren <tim@timshomepage.net>

     * @copyright   2012 - 2020 Timothy J. Warren

     * @license     http://www.opensource.org/licenses/mit-license.html  MIT License

     * @link        https://git.timshomepage.net/aviat/Query

     * @version     3.0.0

     */

    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

    				foreach ($row as $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) . ';';

    	}

    }