Query/src/Drivers/Sqlite/SQL.php

171 lines
3.1 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
*
2022-09-29 11:33:08 -04:00
* PHP version 8.1
2016-09-07 13:17:17 -04:00
*
* @package Query
2023-01-20 11:30:51 -05:00
* @author Timothy J. Warren <tim@timshome.page>
* @copyright 2012 - 2023 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
2023-03-17 16:34:21 -04:00
* @version 4.1.0
2012-04-10 14:06:34 -04:00
*/
2023-03-17 15:18:33 -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;
use Query\Exception\NotImplementedException;
2016-09-07 17:39:19 -04:00
2012-04-10 14:06:34 -04:00
/**
* SQLite Specific SQL
*/
2023-03-17 15:18:33 -04:00
class SQL extends AbstractSQL
{
2014-02-04 20:59:30 -05:00
/**
* Get the query plan for the sql query
*/
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
*/
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
/**
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
*/
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
*/
2018-01-24 13:14:03 -05:00
public function tableList(): string
2012-04-10 14:06:34 -04:00
{
2023-03-17 15:18:33 -04:00
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"
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
*
* @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
{
return [
'sqlite_master',
'sqlite_temp_master',
2023-03-17 15:18:33 -04:00
'sqlite_sequence',
];
2012-04-10 14:06:34 -04:00
}
/**
* Returns sql to list views
*/
2018-01-24 13:14:03 -05:00
public function viewList(): string
2012-04-10 14:06:34 -04:00
{
2023-03-17 15:18:33 -04:00
return <<<'SQL'
2012-04-10 14:06:34 -04:00
SELECT "name" FROM "sqlite_master" WHERE "type" = 'view'
SQL;
}
/**
* Returns sql to list triggers
*/
2018-01-24 13:14:03 -05:00
public function triggerList(): string
2012-04-10 14:06:34 -04:00
{
2023-03-17 15:18:33 -04: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
*
* @throws NotImplementedException
2012-04-10 14:06:34 -04:00
*/
public function functionList(): string
2012-04-10 14:06:34 -04:00
{
throw new NotImplementedException('Functionality does not exist in SQLite');
2012-04-10 14:06:34 -04:00
}
/**
* Return sql to list stored procedures
*
* @throws NotImplementedException
2012-04-10 14:06:34 -04:00
*/
public function procedureList(): string
2012-04-10 14:06:34 -04:00
{
throw new NotImplementedException('Functionality does not exist in SQLite');
2012-04-10 14:06:34 -04:00
}
/**
* Return sql to list sequences
*/
public function sequenceList(): string
2012-04-10 14:06:34 -04:00
{
return 'SELECT "name" FROM "sqlite_sequence"';
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
{
return ['INTEGER', 'REAL', 'TEXT', 'BLOB', 'NULL'];
}
/**
2018-01-24 13:14:03 -05:00
* SQL to show information about columns in a table
*/
2018-01-24 13:14:03 -05:00
public function columnList(string $table): string
{
return <<<SQL
PRAGMA table_info("{$table}")
SQL;
}
2012-04-10 14:06:34 -04:00
/**
* Get the list of foreign keys for the current
* table
*/
2018-01-24 13:14:03 -05:00
public function fkList(string $table): string
{
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
*/
2018-01-24 13:14:03 -05:00
public function indexList(string $table): string
2014-04-08 14:43:07 -04:00
{
return <<<SQL
PRAGMA index_list("{$table}")
SQL;
}
2023-03-17 15:18:33 -04:00
}