Source of file SQL.php

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

src/Drivers/Sqlite/SQL.php

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
<?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;
	}
}