SQL::columnList()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
/**
3
 * Query
4
 *
5
 * SQL Query Builder / Database Abstraction Layer
6
 *
7
 * PHP version 7.1
8
 *
9
 * @package     Query
10
 * @author      Timothy J. Warren <[email protected]>
11
 * @copyright   2012 - 2018 Timothy J. Warren
12
 * @license     http://www.opensource.org/licenses/mit-license.html  MIT License
13
 * @link        https://git.timshomepage.net/aviat4ion/Query
14
 */
15
namespace Query\Drivers\Mysql;
16
17
use Query\Drivers\AbstractSQL;
18
19
/**
20
 * MySQL specific SQL
21
 */
22
class SQL extends AbstractSQL {
23
24
	/**
25
	 * Limit clause
26
	 *
27
	 * @param string $sql
28
	 * @param int $limit
29
	 * @param int|boolean $offset
30
	 * @return string
31
	 */
32
	public function limit(string $sql, int $limit, $offset=FALSE): string
33
	{
34
		if ( ! is_numeric($offset))
35
		{
36
			return $sql." LIMIT {$limit}";
37
		}
38
39
		return $sql." LIMIT {$offset}, {$limit}";
40
	}
41
42
	/**
43
	 * Get the query plan for the sql query
44
	 *
45
	 * @param string $sql
46
	 * @return string
47
	 */
48
	public function explain(string $sql): string
49
	{
50
		return "EXPLAIN EXTENDED {$sql}";
51
	}
52
53
	/**
54
	 * Random ordering keyword
55
	 *
56
	 * @return string
57
	 */
58
	public function random(): string
59
	{
60
		return ' RAND() DESC';
61
	}
62
63
	/**
64
	 * Returns sql to list other databases
65
	 *
66
	 * @return string
67
	 */
68
	public function dbList(): string
69
	{
70
		return <<<SQL
71
			SHOW DATABASES WHERE `Database` NOT IN ('information_schema','mysql')
72
SQL;
73
74
	}
75
76
	/**
77
	 * Returns sql to list tables
78
	 *
79
	 * @param string $database
80
	 * @return string
81
	 */
82
	public function tableList($database=''): string
83
	{
84
		// @codeCoverageIgnoreStart
85
		if ( ! empty($database))
86
		{
87
			return "SHOW TABLES FROM `{$database}`";
88
		}
89
		// @codeCoverageIgnoreEnd
90
91
		return 'SHOW TABLES';
92
	}
93
94
	/**
95
	 * Overridden in MySQL class
96
	 *
97
	 * @return string
98
	 */
99
	public function systemTableList(): string
100
	{
101
		return <<<SQL
102
			SELECT `TABLE_NAME` FROM `information_schema`.`TABLES`
103
			WHERE `TABLE_SCHEMA`='information_schema'
104
SQL;
105
	}
106
107
	/**
108
	 * Returns sql to list views
109
	 *
110
	 * @return string
111
	 */
112
	public function viewList(): string
113
	{
114
		return 'SELECT `table_name` FROM `information_schema`.`views`';
115
	}
116
117
	/**
118
	 * Returns sql to list triggers
119
	 *
120
	 * @return string
121
	 */
122
	public function triggerList(): string
123
	{
124
		return 'SHOW TRIGGERS';
125
	}
126
127
	/**
128
	 * Return sql to list functions
129
	 *
130
	 * @return string
131
	 */
132
	public function functionList(): string
133
	{
134
		return 'SHOW FUNCTION STATUS';
135
	}
136
137
	/**
138
	 * Return sql to list stored procedures
139
	 *
140
	 * @return string
141
	 */
142
	public function procedureList(): string
143
	{
144
		return 'SHOW PROCEDURE STATUS';
145
	}
146
147
	/**
148
	 * Return sql to list sequences
149
	 *
150
	 * @return string
151
	 */
152
	public function sequenceList(): ?string
153
	{
154
		return NULL;
155
	}
156
157
	/**
158
	 * SQL to show list of field types
159
	 *
160
	 * @return string
161
	 */
162
	public function typeList(): string
163
	{
164
		return 'SELECT DISTINCT `DATA_TYPE` FROM `information_schema`.`COLUMNS`';
165
	}
166
167
	/**
168
	 * SQL to show infromation about columns in a table
169
	 *
170
	 * @param string $table
171
	 * @return string
172
	 */
173
	public function columnList(string $table): string
174
	{
175
		return "SHOW FULL COLUMNS FROM {$table}";
176
	}
177
178
	/**
179
	 * Get the list of foreign keys for the current
180
	 * table
181
	 *
182
	 * @param string $table
183
	 * @return string
184
	 */
185
	public function fkList(string $table): string
186
	{
187
		return <<<SQL
188
			SELECT DISTINCT 
189
				`kcu`.`COLUMN_NAME` as `child_column`,
190
				`kcu`.`REFERENCED_TABLE_NAME` as `parent_table`,
191
				`kcu`.`REFERENCED_COLUMN_NAME` as `parent_column`,
192
				`rc`.`UPDATE_RULE` AS `update`,
193
				`rc`.`DELETE_RULE` AS `delete`
194
			FROM `INFORMATION_SCHEMA`.`TABLE_CONSTRAINTS` `tc`
195
			INNER JOIN `INFORMATION_SCHEMA`.`KEY_COLUMN_USAGE` `kcu`
196
				ON `kcu`.`CONSTRAINT_NAME`=`tc`.`CONSTRAINT_NAME`
197
			INNER JOIN `INFORMATION_SCHEMA`.`REFERENTIAL_CONSTRAINTS` `rc`
198
				ON `rc`.`CONSTRAINT_NAME`=`tc`.`CONSTRAINT_NAME`
199
			WHERE `tc`.`CONSTRAINT_TYPE`='FOREIGN KEY'
200
			AND `tc`.`TABLE_NAME`='{$table}'
201
SQL;
202
	}
203
204
	/**
205
	 * Get the list of indexes for the current table
206
	 *
207
	 * @param string $table
208
	 * @return string
209
	 */
210
	public function indexList(string $table): string
211
	{
212
		return "SHOW INDEX IN {$table}";
213
	}
214
}