Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
11 / 11
CRAP
100.00% covered (success)
100.00%
1 / 1
SQL
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
11 / 11
14
100.00% covered (success)
100.00%
1 / 1
 explain
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 random
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 dbList
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 tableList
n/a
0 / 0
n/a
0 / 0
1
 systemTableList
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 viewList
n/a
0 / 0
n/a
0 / 0
1
 triggerList
n/a
0 / 0
n/a
0 / 0
1
 functionList
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 procedureList
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 sequenceList
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 typeList
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 columnList
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 fkList
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 indexList
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
1<?php declare(strict_types=1);
2/**
3 * Query
4 *
5 * SQL Query Builder / Database Abstraction Layer
6 *
7 * PHP version 8.1
8 *
9 * @package     Query
10 * @author      Timothy J. Warren <tim@timshome.page>
11 * @copyright   2012 - 2023 Timothy J. Warren
12 * @license     http://www.opensource.org/licenses/mit-license.html  MIT License
13 * @link        https://git.timshomepage.net/aviat/Query
14 * @version     4.0.0
15 */
16
17namespace Query\Drivers\Sqlite;
18
19use Query\Drivers\AbstractSQL;
20use Query\Exception\NotImplementedException;
21
22/**
23 * SQLite Specific SQL
24 */
25class SQL extends AbstractSQL
26{
27    /**
28     * Get the query plan for the sql query
29     */
30    public function explain(string $sql): string
31    {
32        return "EXPLAIN QUERY PLAN {$sql}";
33    }
34
35    /**
36     * Random ordering keyword
37     */
38    public function random(): string
39    {
40        return ' RANDOM()';
41    }
42
43    /**
44     * Returns sql to list other databases. Meaningless for SQLite, as this
45     * just returns the database(s) that we are currently connected to.
46     */
47    public function dbList(): string
48    {
49        return '';
50    }
51
52    /**
53     * Returns sql to list tables
54     */
55    public function tableList(): string
56    {
57        return <<<'SQL'
58            SELECT "name" FROM (
59                SELECT * FROM "sqlite_master" UNION ALL
60                SELECT * FROM "sqlite_temp_master"
61            )
62            WHERE "type"='table'
63            AND "name" NOT LIKE "sqlite_%"
64            ORDER BY "name"
65SQL;
66    }
67
68    /**
69     * List the system tables
70     *
71     * @return string[]
72     */
73    public function systemTableList(): array
74    {
75        return [
76            'sqlite_master',
77            'sqlite_temp_master',
78            'sqlite_sequence',
79        ];
80    }
81
82    /**
83     * Returns sql to list views
84     */
85    public function viewList(): string
86    {
87        return <<<'SQL'
88            SELECT "name" FROM "sqlite_master" WHERE "type" = 'view'
89SQL;
90    }
91
92    /**
93     * Returns sql to list triggers
94     */
95    public function triggerList(): string
96    {
97        return <<<'SQL'
98            SELECT "name" FROM "sqlite_master" WHERE "type"='trigger'
99SQL;
100    }
101
102    /**
103     * Return sql to list functions
104     *
105     * @throws NotImplementedException
106     */
107    public function functionList(): string
108    {
109        throw new NotImplementedException('Functionality does not exist in SQLite');
110    }
111
112    /**
113     * Return sql to list stored procedures
114     *
115     * @throws NotImplementedException
116     */
117    public function procedureList(): string
118    {
119        throw new NotImplementedException('Functionality does not exist in SQLite');
120    }
121
122    /**
123     * Return sql to list sequences
124     */
125    public function sequenceList(): string
126    {
127        return 'SELECT "name" FROM "sqlite_sequence"';
128    }
129
130    /**
131     * SQL to show list of field types
132     *
133     * @return string[]
134     */
135    public function typeList(): array
136    {
137        return ['INTEGER', 'REAL', 'TEXT', 'BLOB', 'NULL'];
138    }
139
140    /**
141     * SQL to show information about columns in a table
142     */
143    public function columnList(string $table): string
144    {
145        return <<<SQL
146            PRAGMA table_info("{$table}")
147SQL;
148    }
149
150    /**
151     * Get the list of foreign keys for the current
152     * table
153     */
154    public function fkList(string $table): string
155    {
156        return <<<SQL
157            PRAGMA foreign_key_list("{$table}")
158SQL;
159    }
160
161    /**
162     * Get the list of indexes for the current table
163     */
164    public function indexList(string $table): string
165    {
166        return <<<SQL
167            PRAGMA index_list("{$table}")
168SQL;
169    }
170}