# phpDox - Source of Driver.php

# Source of file Driver.php

Size: 3,677 Bytes - Last Modified: 2020-04-17T14:56:27-04:00

## src/Drivers/Sqlite/Driver.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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

    <?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 function is_array;

    use InvalidArgumentException;

    use PDO;

    use Query\Drivers\AbstractDriver;

    /**

     * SQLite specific class

     */

    class Driver extends AbstractDriver {

    	/**

    	 * SQLite has a truncate optimization,

    	 * but no support for the actual keyword

    	 * @var boolean

    	 */

    	protected bool $hasTruncate = FALSE;

    	/**

    	 * Open SQLite Database

    	 *

    	 * @param string $dsn

    	 * @param string $user

    	 * @param string $pass

    	 * @param array $driverOptions

    	 */

    	public function __construct(string $dsn, string $user=NULL, string $pass=NULL, array $driverOptions=[])

    	{

    		if (strpos($dsn, 'sqlite:') === FALSE)

    		{

    			$dsn = "sqlite:{$dsn}";

    		}

    		parent::__construct($dsn, $user, $pass);

    	}

    	/**

    	 * Return list of dbs for the current connection, if possible. Meaningless for SQLite.

    	 *

    	 * @return array | null

    	 */

    	public function getDbs(): ?array

    	{

    		return NULL;

    	}

    	/**

    	 * List tables for the current database

    	 *

    	 * @return mixed

    	 */

    	public function getTables(): array

    	{

    		$sql = $this->getSql()->tableList();

    		$res = $this->query($sql);

    		return dbFilter($res->fetchAll(PDO::FETCH_ASSOC), 'name');

    	}

    	/**

    	 * Retrieve foreign keys for the table

    	 *

    	 * @param string $table

    	 * @return array

    	 */

    	public function getFks($table): array

    	{

    		$returnRows = [];

    		foreach(parent::getFks($table) as $row)

    		{

    			$returnRows[] = [

    				'child_column' => $row['from'],

    				'parent_table' => $row['table'],

    				'parent_column' => $row['to'],

    				'update' => $row['on_update'],

    				'delete' => $row['on_delete']

    			];

    		}

    		return $returnRows;

    	}

    	/**

    	 * Create sql for batch insert

    	 *

    	 * @codeCoverageIgnore

    	 * @param string $table

    	 * @param array $data

    	 * @return array

    	 */

    	public function insertBatch(string $table, array $data=[]): array

    	{

    		// If greater than version 3.7.11, supports the same syntax as

    		// MySQL and Postgres

    		if (version_compare($this->getVersion(), '3.7.11', '>='))

    		{

    			return parent::insertBatch($table, $data);

    		}

    		// --------------------------------------------------------------------------

    		// Otherwise, do a union query as an analogue to a 'proper' batch insert

    		// --------------------------------------------------------------------------

    		// Each member of the data array needs to be an array

    		if ( ! is_array(current($data)))

    		{

    			throw new InvalidArgumentException('$data must be an array of arrays');

    		}

    		// Start the block of sql statements

    		$table = $this->quoteTable($table);

    		$sql = "INSERT INTO {$table} \n";

    		// Create a key-value mapping for each field

    		$first = array_shift($data);

    		$cols = [];

    		foreach($first as $colName => $datum)

    		{

    			$cols[] = $this->_quote($datum) . ' AS ' . $this->quoteIdent($colName);

    		}

    		$sql .= 'SELECT ' . implode(', ', $cols) . "\n";

    		foreach($data as $union)

    		{

    			$vals = array_map([$this, 'quote'], $union);

    			$sql .= 'UNION SELECT ' . implode(',', $vals) . "\n";

    		}

    		return [$sql, NULL];

    	}

    	/**

    	 * Generate the returning clause for the current database

    	 *

    	 * @param string $query

    	 * @param string $select

    	 * @return string

    	 */

    	public function returning(string $query, string $select): string

    	{

    		// Return the same query, as the returning clause is not supported

    		return $query;

    	}

    }