Source of file SQL.php

Size: 3,004 Bytes - Last Modified: 2018-01-24T17:42:33+00:00

src/Drivers/Sqlite/SQL.php

1234567891011121314151617181920212223242526272829303132
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteQueryBuilderTest::testQueryExplain
33343536373839404142
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteQueryBuilderTest::testOrderByRandom
43444546474849505152
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testGetDBs
5354555657585960616263
Covered by 3 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testCreateTable
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testDeleteTable
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testGetTables
646566676869707172737475767778
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testGetSystemTables
7980818283848586878889
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testGetViews
90919293949596979899100
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testGetTriggers
101102103104105106107108109110
Covered by 2 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testNullMethods
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testGetFunctions
111112113114115116117118119120
Covered by 2 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testNullMethods
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testGetProcedures
121122123124125126127128129130
Covered by 2 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testNullMethods
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testGetSequences
131132133134135136137138139140
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testGetTypes
141142143144145146147148149150151
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testGetColumns
152153154155156157158159160161162163
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testGetFKs
164165166167168169170171172173174175
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testGetIndexes
176177
<?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 Query\Drivers\AbstractSQL;

/**
 * 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
	 *
	 * @return string
	 */
	public function dbList(): string
	{
		return 'PRAGMA database_list';
	}

	/**
	 * Returns sql to list tables
	 *
	 * @return string
	 */
	public function tableList(): string
	{
		return <<<SQL
			SELECT DISTINCT "name"
			FROM "sqlite_master"
			WHERE "type"='table'
			AND "name" NOT LIKE 'sqlite_%'
			ORDER BY "name" DESC
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 'SELECT "name" FROM "sqlite_master" WHERE "type"=\'trigger\'';
	}

	/**
	 * Return sql to list functions
	 *
	 * @return string
	 */
	public function functionList(): ?string
	{
		return NULL;
	}

	/**
	 * Return sql to list stored procedures
	 *
	 * @return string
	 */
	public function procedureList(): ?string
	{
		return NULL;
	}

	/**
	 * Return sql to list sequences
	 *
	 * @return string
	 */
	public function sequenceList(): ?string
	{
		return NULL;
	}

	/**
	 * SQL to show list of field types
	 *
	 * @return string[]
	 */
	public function typeList(): array
	{
		return ['INTEGER', 'REAL', 'TEXT', 'BLOB'];
	}

	/**
	 * SQL to show information about columns in a table
	 *
	 * @param string $table
	 * @return string
	 */
	public function columnList(string $table): string
	{
		return 'PRAGMA table_info("' . $table . '")';
	}

	/**
	 * Get the list of foreign keys for the current
	 * table
	 *
	 * @param string $table
	 * @return string
	 */
	public function fkList(string $table): string
	{
		return 'PRAGMA foreign_key_list("' . $table . '")';
	}


	/**
	 * Get the list of indexes for the current table
	 *
	 * @param string $table
	 * @return string
	 */
	public function indexList(string $table): string
	{
		return 'PRAGMA index_list("' . $table . '")';
	}
}