Source of file SQL.php

Size: 3,407 Bytes - Last Modified: 2018-01-26T16:43:29+00:00

src/Drivers/Sqlite/SQL.php

123456789101112131415161718192021222324252627282930313233
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteQueryBuilderTest::testQueryExplain
34353637383940414243
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteQueryBuilderTest::testOrderByRandom
44454647484950515253
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testGetDBs
5455565758596061626364
Covered by 3 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testCreateTable
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testDeleteTable
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testGetTables
656667686970717273747576777879808182
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testGetSystemTables
8384858687888990919293949596
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testGetViews
979899100101102103104105106107108
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testGetTriggers
109110111112113114115116117118119120
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testGetFunctions
121122123124125126127128129130131
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testGetProcedures
132133134135136137138139140141
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testGetSequences
142143144145146147148149150151
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testGetTypes
152153154155156157158159160161162163
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testGetColumns
164165166167168169170171172173174175176177
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testGetFKs
178179180181182183184185186187188189190191
Covered by 1 test(s):
  • Query\Tests\Drivers\SQLite\SQLiteDriverTest::testGetIndexes
192193194
<?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;
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
	 *
	 * @return string
	 */
	public function dbList(): string
	{
		return 'PRAGMA database_list';
	}

	/**
	 * 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;
	}
}