Query/src/Drivers/Sqlite/SQL.php

177 lines
2.9 KiB
PHP
Raw Normal View History

2016-10-12 22:12:25 -04:00
<?php declare(strict_types=1);
2012-04-10 14:06:34 -04:00
/**
* Query
*
2016-09-07 13:17:17 -04:00
* SQL Query Builder / Database Abstraction Layer
2012-04-10 14:06:34 -04:00
*
2018-01-19 13:43:19 -05:00
* PHP version 7.1
2016-09-07 13:17:17 -04:00
*
* @package Query
* @author Timothy J. Warren <tim@timshomepage.net>
2018-01-19 13:43:19 -05:00
* @copyright 2012 - 2018 Timothy J. Warren
2016-09-07 13:17:17 -04:00
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link https://git.timshomepage.net/aviat4ion/Query
2012-04-10 14:06:34 -04:00
*/
namespace Query\Drivers\Sqlite;
2014-04-02 17:08:50 -04:00
2016-09-07 17:39:19 -04:00
use Query\Drivers\AbstractSQL;
2012-04-10 14:06:34 -04:00
/**
* SQLite Specific SQL
*/
2016-09-07 17:39:19 -04:00
class SQL extends AbstractSQL {
2014-02-04 20:59:30 -05:00
/**
* Get the query plan for the sql query
*
* @param string $sql
* @return string
*/
2018-01-24 13:14:03 -05:00
public function explain(string $sql): string
2014-02-04 20:59:30 -05:00
{
return "EXPLAIN QUERY PLAN {$sql}";
}
2012-04-10 14:06:34 -04:00
/**
* Random ordering keyword
*
* @return string
*/
2018-01-24 13:14:03 -05:00
public function random(): string
2012-04-10 14:06:34 -04:00
{
return ' RANDOM()';
}
2012-04-10 14:06:34 -04:00
/**
* Returns sql to list other databases
*
* @return string
2012-04-10 14:06:34 -04:00
*/
2018-01-24 13:14:03 -05:00
public function dbList(): string
2012-04-10 14:06:34 -04:00
{
2014-04-08 14:43:07 -04:00
return 'PRAGMA database_list';
2012-04-10 14:06:34 -04:00
}
/**
* Returns sql to list tables
*
* @return string
*/
2018-01-24 13:14:03 -05:00
public function tableList(): string
2012-04-10 14:06:34 -04:00
{
return <<<SQL
2014-04-08 15:38:18 -04:00
SELECT DISTINCT "name"
2012-04-10 14:06:34 -04:00
FROM "sqlite_master"
WHERE "type"='table'
AND "name" NOT LIKE 'sqlite_%'
2012-04-10 14:06:34 -04:00
ORDER BY "name" DESC
SQL;
}
/**
2014-04-23 16:27:43 -04:00
* List the system tables
2012-04-10 14:06:34 -04:00
*
* @return string[]
2012-04-10 14:06:34 -04:00
*/
2018-01-24 13:14:03 -05:00
public function systemTableList(): array
2012-04-10 14:06:34 -04:00
{
2016-09-07 13:10:03 -04:00
return ['sqlite_master', 'sqlite_temp_master', 'sqlite_sequence'];
2012-04-10 14:06:34 -04:00
}
/**
* Returns sql to list views
*
* @return string
*/
2018-01-24 13:14:03 -05:00
public function viewList(): string
2012-04-10 14:06:34 -04:00
{
return <<<SQL
SELECT "name" FROM "sqlite_master" WHERE "type" = 'view'
SQL;
}
/**
* Returns sql to list triggers
*
* @return string
2012-04-10 14:06:34 -04:00
*/
2018-01-24 13:14:03 -05:00
public function triggerList(): string
2012-04-10 14:06:34 -04:00
{
return 'SELECT "name" FROM "sqlite_master" WHERE "type"=\'trigger\'';
2012-04-10 14:06:34 -04:00
}
/**
* Return sql to list functions
*
2018-01-24 13:14:03 -05:00
* @return string
2012-04-10 14:06:34 -04:00
*/
2018-01-24 13:14:03 -05:00
public function functionList(): ?string
2012-04-10 14:06:34 -04:00
{
return NULL;
2012-04-10 14:06:34 -04:00
}
/**
* Return sql to list stored procedures
*
2018-01-24 13:14:03 -05:00
* @return string
2012-04-10 14:06:34 -04:00
*/
2018-01-24 13:14:03 -05:00
public function procedureList(): ?string
2012-04-10 14:06:34 -04:00
{
return NULL;
2012-04-10 14:06:34 -04:00
}
/**
* Return sql to list sequences
*
2018-01-24 13:14:03 -05:00
* @return string
2012-04-10 14:06:34 -04:00
*/
2018-01-24 13:14:03 -05:00
public function sequenceList(): ?string
2012-04-10 14:06:34 -04:00
{
return NULL;
2012-04-10 14:06:34 -04:00
}
/**
* SQL to show list of field types
*
* @return string[]
*/
2018-01-24 13:14:03 -05:00
public function typeList(): array
{
2016-09-07 13:10:03 -04:00
return ['INTEGER', 'REAL', 'TEXT', 'BLOB'];
}
/**
2018-01-24 13:14:03 -05:00
* SQL to show information about columns in a table
*
* @param string $table
* @return string
*/
2018-01-24 13:14:03 -05:00
public function columnList(string $table): string
{
2016-10-13 21:55:23 -04:00
return 'PRAGMA table_info("' . $table . '")';
}
2012-04-10 14:06:34 -04:00
/**
* Get the list of foreign keys for the current
* table
*
* @param string $table
* @return string
*/
2018-01-24 13:14:03 -05:00
public function fkList(string $table): string
{
2014-04-08 14:43:07 -04:00
return 'PRAGMA foreign_key_list("' . $table . '")';
}
/**
* Get the list of indexes for the current table
*
* @param string $table
2014-04-23 16:27:43 -04:00
* @return string
2014-04-08 14:43:07 -04:00
*/
2018-01-24 13:14:03 -05:00
public function indexList(string $table): string
2014-04-08 14:43:07 -04:00
{
return 'PRAGMA index_list("' . $table . '")';
}
2016-10-13 21:55:23 -04:00
}