# phpDox - Source of SQL.php

# Source of file SQL.php

Size: 3,507 Bytes - Last Modified: 2020-04-10T20:54:13-04:00

## src/Drivers/Sqlite/SQL.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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196

    <?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 Query\Drivers\AbstractSQL;

    use Query\Exception\NotImplementedException;

    /**

     * SQLite Specific SQL

     */

    class SQL extends AbstractSQL {

    	/**

    	 * Get the query plan for the sql query

    	 *

    	 * @param string $sql

    	 * @return string

    	 */

    	public function explain(string $sql): string

    	{

    		return "EXPLAIN QUERY PLAN {$sql}";

    	}

    	/**

    	 * Random ordering keyword

    	 *

    	 * @return string

    	 */

    	public function random(): string

    	{

    		return ' RANDOM()';

    	}

    	/**

    	 * Returns sql to list other databases. Meaningless for SQLite, as this

    	 * just returns the database(s) that we are currently connected to.

    	 *

    	 * @return string

    	 */

    	public function dbList(): string

    	{

    		return '';

    	}

    	/**

    	 * Returns sql to list tables

    	 *

    	 * @return string

    	 */

    	public function tableList(): string

    	{

    		return <<<SQL

                SELECT "name" FROM (

    				SELECT * FROM "sqlite_master" UNION ALL

    				SELECT * FROM "sqlite_temp_master"

    			)

            	WHERE "type"='table'

            	AND "name" NOT LIKE "sqlite_%"

            	ORDER BY "name"

    SQL;

    	}

    	/**

    	 * List the system tables

    	 *

    	 * @return string[]

    	 */

    	public function systemTableList(): array

    	{

    		return [

    			'sqlite_master',

    			'sqlite_temp_master',

    			'sqlite_sequence'

    		];

    	}

    	/**

    	 * Returns sql to list views

    	 *

    	 * @return string

    	 */

    	public function viewList(): string

    	{

    		return <<<SQL

    			SELECT "name" FROM "sqlite_master" WHERE "type" = 'view'

    SQL;

    	}

    	/**

    	 * Returns sql to list triggers

    	 *

    	 * @return string

    	 */

    	public function triggerList(): string

    	{

    		return <<<SQL

    			SELECT "name" FROM "sqlite_master" WHERE "type"='trigger'

    SQL;

    	}

    	/**

    	 * Return sql to list functions

    	 *

    	 * @throws NotImplementedException

    	 * @return string

    	 */

    	public function functionList(): string

    	{

    		throw new NotImplementedException('Functionality does not exist in SQLite');

    	}

    	/**

    	 * Return sql to list stored procedures

    	 *

    	 * @throws NotImplementedException

    	 * @return string

    	 */

    	public function procedureList(): string

    	{

    		throw new NotImplementedException('Functionality does not exist in SQLite');

    	}

    	/**

    	 * Return sql to list sequences

    	 *

    	 * @return string

    	 */

    	public function sequenceList(): string

    	{

    		return 'SELECT "name" FROM "sqlite_sequence"';

    	}

    	/**

    	 * SQL to show list of field types

    	 *

    	 * @return string[]

    	 */

    	public function typeList(): array

    	{

    		return ['INTEGER', 'REAL', 'TEXT', 'BLOB', 'NULL'];

    	}

    	/**

    	 * SQL to show information about columns in a table

    	 *

    	 * @param string $table

    	 * @return string

    	 */

    	public function columnList(string $table): string

    	{

    		return <<<SQL

    			PRAGMA table_info("$table")

    SQL;

    	}

    	/**

    	 * Get the list of foreign keys for the current

    	 * table

    	 *

    	 * @param string $table

    	 * @return string

    	 */

    	public function fkList(string $table): string

    	{

    		return <<<SQL

    			PRAGMA foreign_key_list("$table")

    SQL;

    	}

    	/**

    	 * Get the list of indexes for the current table

    	 *

    	 * @param string $table

    	 * @return string

    	 */

    	public function indexList(string $table): string

    	{

    		return <<<SQL

    			PRAGMA index_list("$table")

    SQL;

    	}

    }