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
|
|
|
*
|
2020-04-10 20:54:31 -04:00
|
|
|
* PHP version 7.4
|
2016-09-07 13:17:17 -04:00
|
|
|
*
|
|
|
|
* @package Query
|
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2020-03-18 11:31:56 -04:00
|
|
|
* @copyright 2012 - 2020 Timothy J. Warren
|
2016-09-07 13:17:17 -04:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2019-12-11 16:49:42 -05:00
|
|
|
* @link https://git.timshomepage.net/aviat/Query
|
|
|
|
* @version 3.0.0
|
2012-04-10 14:06:34 -04:00
|
|
|
*/
|
2015-07-16 16:56:13 -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;
|
2018-01-26 15:45:46 -05:00
|
|
|
use Query\Exception\NotImplementedException;
|
2016-09-07 17:39:19 -04:00
|
|
|
|
2012-04-10 14:06:34 -04:00
|
|
|
/**
|
|
|
|
* SQLite Specific SQL
|
|
|
|
*/
|
2016-09-07 17:39:19 -04:00
|
|
|
class SQL extends AbstractSQL {
|
2014-03-26 20:49:33 -04:00
|
|
|
|
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()';
|
|
|
|
}
|
2014-03-26 20:49:33 -04:00
|
|
|
|
2012-04-10 14:06:34 -04:00
|
|
|
/**
|
2019-12-11 16:49:06 -05:00
|
|
|
* Returns sql to list other databases. Meaningless for SQLite, as this
|
|
|
|
* just returns the database(s) that we are currently connected to.
|
2012-04-10 14:06:34 -04:00
|
|
|
*
|
2014-04-10 15:54:43 -04:00
|
|
|
* @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
|
|
|
{
|
2019-12-11 16:49:06 -05:00
|
|
|
return '';
|
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
|
2018-01-26 15:45:46 -05:00
|
|
|
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"
|
2012-04-10 14:06:34 -04:00
|
|
|
SQL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-04-23 16:27:43 -04:00
|
|
|
* List the system tables
|
2012-04-10 14:06:34 -04:00
|
|
|
*
|
2014-08-08 14:02:14 -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
|
|
|
{
|
2018-01-26 15:45:46 -05: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
|
|
|
|
*
|
2014-08-08 14:02:14 -04:00
|
|
|
* @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
|
|
|
{
|
2018-01-26 15:45:46 -05:00
|
|
|
return <<<SQL
|
|
|
|
SELECT "name" FROM "sqlite_master" WHERE "type"='trigger'
|
|
|
|
SQL;
|
2012-04-10 14:06:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return sql to list functions
|
|
|
|
*
|
2018-01-26 15:45:46 -05:00
|
|
|
* @throws NotImplementedException
|
2018-01-24 13:14:03 -05:00
|
|
|
* @return string
|
2012-04-10 14:06:34 -04:00
|
|
|
*/
|
2018-01-26 15:45:46 -05:00
|
|
|
public function functionList(): string
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
2018-01-26 15:45:46 -05:00
|
|
|
throw new NotImplementedException('Functionality does not exist in SQLite');
|
2012-04-10 14:06:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return sql to list stored procedures
|
|
|
|
*
|
2018-01-26 15:45:46 -05:00
|
|
|
* @throws NotImplementedException
|
2018-01-24 13:14:03 -05:00
|
|
|
* @return string
|
2012-04-10 14:06:34 -04:00
|
|
|
*/
|
2018-01-26 15:45:46 -05:00
|
|
|
public function procedureList(): string
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
2018-01-26 15:45:46 -05:00
|
|
|
throw new NotImplementedException('Functionality does not exist in SQLite');
|
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-26 15:45:46 -05:00
|
|
|
public function sequenceList(): string
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
2018-01-26 15:45:46 -05:00
|
|
|
return 'SELECT "name" FROM "sqlite_sequence"';
|
2012-04-10 14:06:34 -04:00
|
|
|
}
|
2014-03-26 20:49:33 -04:00
|
|
|
|
2012-05-07 16:05:51 -04:00
|
|
|
/**
|
|
|
|
* SQL to show list of field types
|
|
|
|
*
|
2014-03-17 19:35:25 -04:00
|
|
|
* @return string[]
|
2012-05-07 16:05:51 -04:00
|
|
|
*/
|
2018-01-24 13:14:03 -05:00
|
|
|
public function typeList(): array
|
2012-05-07 16:05:51 -04:00
|
|
|
{
|
2018-01-26 15:45:46 -05:00
|
|
|
return ['INTEGER', 'REAL', 'TEXT', 'BLOB', 'NULL'];
|
2012-05-07 16:05:51 -04:00
|
|
|
}
|
2014-03-26 20:49:33 -04:00
|
|
|
|
2012-05-09 13:54:38 -04:00
|
|
|
/**
|
2018-01-24 13:14:03 -05:00
|
|
|
* SQL to show information about columns in a table
|
2012-05-09 13:54:38 -04:00
|
|
|
*
|
|
|
|
* @param string $table
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-01-24 13:14:03 -05:00
|
|
|
public function columnList(string $table): string
|
2012-05-09 13:54:38 -04:00
|
|
|
{
|
2018-01-26 15:45:46 -05:00
|
|
|
return <<<SQL
|
|
|
|
PRAGMA table_info("$table")
|
|
|
|
SQL;
|
2012-05-09 13:54:38 -04:00
|
|
|
}
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2014-04-07 16:49:49 -04:00
|
|
|
/**
|
|
|
|
* Get the list of foreign keys for the current
|
|
|
|
* table
|
|
|
|
*
|
2014-04-10 15:54:43 -04:00
|
|
|
* @param string $table
|
2014-04-07 16:49:49 -04:00
|
|
|
* @return string
|
|
|
|
*/
|
2018-01-24 13:14:03 -05:00
|
|
|
public function fkList(string $table): string
|
2014-04-07 16:49:49 -04:00
|
|
|
{
|
2018-01-26 15:45:46 -05:00
|
|
|
return <<<SQL
|
|
|
|
PRAGMA foreign_key_list("$table")
|
|
|
|
SQL;
|
2014-04-08 14:43:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
{
|
2018-01-26 15:45:46 -05:00
|
|
|
return <<<SQL
|
|
|
|
PRAGMA index_list("$table")
|
|
|
|
SQL;
|
2014-04-07 16:49:49 -04:00
|
|
|
}
|
2016-10-13 21:55:23 -04:00
|
|
|
}
|