-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathGenerator.php
More file actions
487 lines (439 loc) · 16.8 KB
/
Generator.php
File metadata and controls
487 lines (439 loc) · 16.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Query;
use PhpMyAdmin\Database\RoutineType;
use PhpMyAdmin\Triggers\Trigger;
use PhpMyAdmin\Util;
use function array_map;
use function count;
use function implode;
use function sprintf;
/**
* Handles generating SQL queries
*/
class Generator
{
/**
* returns a segment of the SQL WHERE clause regarding table name
*
* @param bool $tblIsGroup $table is a table group
*
* @return string a segment of the WHERE clause
*/
public static function getTableNameCondition(string $collate, string $escapedTabletable, bool $tblIsGroup): string
{
$sqlWhereTable = 'AND t.`TABLE_NAME` ';
if ($tblIsGroup) {
$sqlWhereTable .= 'LIKE ' . $escapedTabletable . '%';
} else {
$sqlWhereTable .= $collate . ' = ' . $escapedTabletable;
}
return $sqlWhereTable;
}
/**
* returns a segment of the SQL WHERE clause regarding table name
*
* @param string[] $escapedTables tables
*
* @return string a segment of the WHERE clause
*/
public static function getTableNameConditionForMultiple(string $collate, array $escapedTables): string
{
return 'AND t.`TABLE_NAME` ' . $collate . ' IN (' . implode(', ', $escapedTables) . ')';
}
/**
* returns a segment of the SQL WHERE clause regarding table type
*
* @param string|null $tableType whether table or view
*
* @return string a segment of the WHERE clause
*/
public static function getTableTypeCondition(
string|null $tableType,
): string {
$sqlWhereTable = '';
if ($tableType === 'view') {
$sqlWhereTable .= " AND t.`TABLE_TYPE` NOT IN ('BASE TABLE', 'SYSTEM VERSIONED')";
} elseif ($tableType === 'table') {
$sqlWhereTable .= " AND t.`TABLE_TYPE` IN ('BASE TABLE', 'SYSTEM VERSIONED')";
}
return $sqlWhereTable;
}
/**
* returns the beginning of the SQL statement to fetch the list of tables
*
* @param string $thisDatabases databases to list
* @param string $sqlWhereTable additional condition
*
* @return string the SQL statement
*/
public static function getSqlForTablesFull(string $collate, string $thisDatabases, string $sqlWhereTable): string
{
return 'SELECT *,'
. ' `TABLE_SCHEMA` AS `Db`,'
. ' `TABLE_NAME` AS `Name`,'
. ' `TABLE_TYPE` AS `TABLE_TYPE`,'
. ' `ENGINE` AS `Engine`,'
. ' `ENGINE` AS `Type`,'
. ' `VERSION` AS `Version`,'
. ' `ROW_FORMAT` AS `Row_format`,'
. ' `TABLE_ROWS` AS `Rows`,'
. ' `AVG_ROW_LENGTH` AS `Avg_row_length`,'
. ' `DATA_LENGTH` AS `Data_length`,'
. ' `MAX_DATA_LENGTH` AS `Max_data_length`,'
. ' `INDEX_LENGTH` AS `Index_length`,'
. ' `DATA_FREE` AS `Data_free`,'
. ' `AUTO_INCREMENT` AS `Auto_increment`,'
. ' `CREATE_TIME` AS `Create_time`,'
. ' `UPDATE_TIME` AS `Update_time`,'
. ' `CHECK_TIME` AS `Check_time`,'
. ' `TABLE_COLLATION` AS `Collation`,'
. ' `CHECKSUM` AS `Checksum`,'
. ' `CREATE_OPTIONS` AS `Create_options`,'
. ' `TABLE_COMMENT` AS `Comment`'
. ' FROM `information_schema`.`TABLES` t'
. ' WHERE `TABLE_SCHEMA` ' . $collate
. ' IN (' . $thisDatabases . ')'
. ' ' . $sqlWhereTable;
}
/**
* Returns SQL for fetching information on table indexes (SHOW INDEXES)
*
* @param string $database name of database
* @param string $table name of the table whose indexes are to be retrieved
* @param string $where additional conditions for WHERE
*
* @return string SQL for getting indexes
*/
public static function getTableIndexesSql(
string $database,
string $table,
string $where = '',
): string {
$sql = 'SHOW INDEXES FROM ' . Util::backquote($database) . '.' . Util::backquote($table);
if ($where !== '') {
$sql .= ' WHERE (' . $where . ')';
}
return $sql;
}
/**
* Returns SQL query for fetching columns for a table
*/
public static function getColumns(
string $collate,
string $quotedDatabase,
string $quotedTable,
string|null $quotedColumn = null,
): string {
return 'SELECT'
. ' `COLUMN_NAME` AS `Field`,'
. ' `COLUMN_TYPE` AS `Type`,'
. ' `COLLATION_NAME` AS `Collation`,'
. ' `IS_NULLABLE` AS `Null`,'
. ' `COLUMN_KEY` AS `Key`,'
. ' `COLUMN_DEFAULT` AS `Default`,'
. ' `EXTRA` AS `Extra`,'
. ' `PRIVILEGES` AS `Privileges`,'
. ' `COLUMN_COMMENT` AS `Comment`'
. ' FROM `information_schema`.`COLUMNS`'
. ' WHERE `TABLE_SCHEMA` ' . $collate
. ' = ' . $quotedDatabase
. ' AND `TABLE_NAME` ' . $collate
. ' = ' . $quotedTable
. ($quotedColumn !== null ? ' AND `COLUMN_NAME` = ' . $quotedColumn : '')
. ' ORDER BY `ORDINAL_POSITION`';
}
public static function getColumnNamesAndTypes(string $collate, string $quotedDatabase, string $quotedTable): string
{
return 'SELECT'
. ' `COLUMN_NAME`,'
. ' `COLUMN_TYPE`'
. ' FROM `information_schema`.`COLUMNS`'
. ' WHERE `TABLE_SCHEMA` ' . $collate
. ' = ' . $quotedDatabase
. ' AND `TABLE_NAME` ' . $collate
. ' = ' . $quotedTable
. ' ORDER BY `ORDINAL_POSITION`';
}
public static function getInformationSchemaRoutinesRequest(
string $collate,
string $quotedDbName,
RoutineType|null $routineType,
string|null $quotedRoutineName,
int $limit = 0,
int $offset = 0,
): string {
$query = 'SELECT'
. ' `SPECIFIC_NAME` AS `Name`,'
. ' `ROUTINE_TYPE` AS `Type`,'
. ' `DEFINER` AS `Definer`,'
. ' `DTD_IDENTIFIER`'
. ' FROM `information_schema`.`ROUTINES`'
. ' WHERE `ROUTINE_SCHEMA` ' . $collate
. ' = ' . $quotedDbName;
if ($routineType !== null) {
$query .= " AND `ROUTINE_TYPE` = '" . $routineType->value . "'";
}
if ($quotedRoutineName !== null) {
$query .= ' AND `SPECIFIC_NAME` = ' . $quotedRoutineName;
}
$query .= ' ORDER BY `SPECIFIC_NAME`';
if ($limit > 0) {
$query .= ' LIMIT ' . $limit;
}
if ($offset > 0) {
$query .= ' OFFSET ' . $offset;
}
return $query;
}
public static function getInformationSchemaRoutinesCountRequest(
string $collate,
string $quotedDbName,
RoutineType|null $routineType,
string|null $quotedRoutineName = null,
): string {
$query = 'SELECT COUNT(*) AS `count`'
. ' FROM `information_schema`.`ROUTINES`'
. ' WHERE `ROUTINE_SCHEMA` ' . $collate
. ' = ' . $quotedDbName;
if ($routineType !== null) {
$query .= " AND `ROUTINE_TYPE` = '" . $routineType->value . "'";
}
if ($quotedRoutineName !== null) {
$query .= ' AND `SPECIFIC_NAME` = ' . $quotedRoutineName;
}
return $query;
}
public static function getInformationSchemaEventsRequest(
string $collate,
string $escapedDb,
string|null $escapedEventName,
): string {
$query = 'SELECT'
. ' `EVENT_SCHEMA` AS `Db`,'
. ' `EVENT_NAME` AS `Name`,'
. ' `DEFINER` AS `Definer`,'
. ' `TIME_ZONE` AS `Time zone`,'
. ' `EVENT_TYPE` AS `Type`,'
. ' `EXECUTE_AT` AS `Execute at`,'
. ' `INTERVAL_VALUE` AS `Interval value`,'
. ' `INTERVAL_FIELD` AS `Interval field`,'
. ' `STARTS` AS `Starts`,'
. ' `ENDS` AS `Ends`,'
. ' `STATUS` AS `Status`,'
. ' `ORIGINATOR` AS `Originator`,'
. ' `CHARACTER_SET_CLIENT` AS `character_set_client`,'
. ' `COLLATION_CONNECTION` AS `collation_connection`, '
. '`DATABASE_COLLATION` AS `Database Collation`'
. ' FROM `information_schema`.`EVENTS`'
. ' WHERE `EVENT_SCHEMA` ' . $collate
. ' = ' . $escapedDb;
if ($escapedEventName !== null) {
$query .= ' AND `EVENT_NAME` = ' . $escapedEventName;
}
return $query;
}
public static function getInformationSchemaTriggersRequest(
string $collate,
string $escapedDb,
string|null $escapedTable,
): string {
$query = 'SELECT TRIGGER_SCHEMA, TRIGGER_NAME, EVENT_MANIPULATION'
. ', EVENT_OBJECT_TABLE, ACTION_TIMING, ACTION_STATEMENT'
. ', EVENT_OBJECT_SCHEMA, EVENT_OBJECT_TABLE, DEFINER'
. ' FROM information_schema.TRIGGERS'
. ' WHERE EVENT_OBJECT_SCHEMA ' . $collate . '= ' . $escapedDb;
if ($escapedTable !== null) {
$query .= ' AND EVENT_OBJECT_TABLE ' . $collate . ' = ' . $escapedTable . ';';
}
return $query;
}
public static function getCreateTrigger(Trigger $newTrigger, string $delimiter): string
{
return sprintf(
"CREATE TRIGGER %s %s %s ON %s\n FOR EACH ROW %s\n%s\n",
Util::backquote($newTrigger->name),
$newTrigger->timing->value,
$newTrigger->event->value,
Util::backquote($newTrigger->table),
$newTrigger->statement,
$delimiter,
);
}
public static function getInformationSchemaDataForCreateRequest(
string $user,
string $host,
string $collation,
): string {
// second part of query is for MariaDB that not show roles inside INFORMATION_SCHEMA db
return 'SELECT 1 FROM `INFORMATION_SCHEMA`.`USER_PRIVILEGES` '
. "WHERE `PRIVILEGE_TYPE` = 'CREATE USER' AND "
. "'''" . $user . "''@''" . $host . "''' LIKE `GRANTEE`"
. ' UNION '
. 'SELECT 1 FROM mysql.user '
. "WHERE `create_user_priv` = 'Y' COLLATE " . $collation . ' AND '
. "'" . $user . "' LIKE `User` AND '' LIKE `Host`"
. ' LIMIT 1';
}
public static function getInformationSchemaDataForGranteeRequest(
string $user,
string $host,
string $collation,
): string {
// second part of query is for MariaDB that not show roles inside INFORMATION_SCHEMA db
return 'SELECT 1 FROM ('
. 'SELECT `GRANTEE`, `IS_GRANTABLE` FROM '
. '`INFORMATION_SCHEMA`.`COLUMN_PRIVILEGES` UNION '
. 'SELECT `GRANTEE`, `IS_GRANTABLE` FROM '
. '`INFORMATION_SCHEMA`.`TABLE_PRIVILEGES` UNION '
. 'SELECT `GRANTEE`, `IS_GRANTABLE` FROM '
. '`INFORMATION_SCHEMA`.`SCHEMA_PRIVILEGES` UNION '
. 'SELECT `GRANTEE`, `IS_GRANTABLE` FROM '
. '`INFORMATION_SCHEMA`.`USER_PRIVILEGES`) t '
. "WHERE `IS_GRANTABLE` = 'YES' AND "
. "'''" . $user . "''@''" . $host . "''' LIKE `GRANTEE` "
. ' UNION '
. 'SELECT 1 FROM mysql.user '
. "WHERE `create_user_priv` = 'Y' COLLATE " . $collation . ' AND '
. "'" . $user . "' LIKE `User` AND '' LIKE `Host`"
. ' LIMIT 1';
}
public static function getInformationSchemaForeignKeyConstraintsRequest(
string $escapedDatabase,
string $tablesListForQueryCsv,
): string {
return 'SELECT'
. ' TABLE_NAME,'
. ' COLUMN_NAME,'
. ' REFERENCED_TABLE_NAME,'
. ' REFERENCED_COLUMN_NAME'
. ' FROM information_schema.key_column_usage'
. ' WHERE referenced_table_name IS NOT NULL'
. ' AND TABLE_SCHEMA = ' . $escapedDatabase
. ' AND TABLE_NAME IN (' . $tablesListForQueryCsv . ')'
. ' AND REFERENCED_TABLE_NAME IN (' . $tablesListForQueryCsv . ');';
}
public static function getInformationSchemaDatabasesFullRequest(
bool $forceStats,
string $sqlWhereSchema,
string $sortBy,
string $sortOrder,
string $limit,
): string {
$sql = 'SELECT *, CAST(BIN_NAME AS CHAR CHARACTER SET utf8) AS SCHEMA_NAME FROM (';
$sql .= 'SELECT BINARY s.SCHEMA_NAME AS BIN_NAME, s.DEFAULT_COLLATION_NAME';
if ($forceStats) {
$sql .= ','
. ' COUNT(t.TABLE_SCHEMA) AS SCHEMA_TABLES,'
. ' SUM(t.TABLE_ROWS) AS SCHEMA_TABLE_ROWS,'
. ' SUM(t.DATA_LENGTH) AS SCHEMA_DATA_LENGTH,'
. ' SUM(t.MAX_DATA_LENGTH) AS SCHEMA_MAX_DATA_LENGTH,'
. ' SUM(t.INDEX_LENGTH) AS SCHEMA_INDEX_LENGTH,'
. ' SUM(t.DATA_LENGTH + t.INDEX_LENGTH) AS SCHEMA_LENGTH,'
. ' SUM(IF(t.ENGINE <> \'InnoDB\', t.DATA_FREE, 0)) AS SCHEMA_DATA_FREE';
}
$sql .= ' FROM `information_schema`.SCHEMATA s ';
if ($forceStats) {
$sql .= ' LEFT JOIN `information_schema`.TABLES t ON BINARY t.TABLE_SCHEMA = BINARY s.SCHEMA_NAME';
}
$sql .= $sqlWhereSchema
. ' GROUP BY BINARY s.SCHEMA_NAME, s.DEFAULT_COLLATION_NAME'
. ' ORDER BY ';
if ($sortBy === 'SCHEMA_NAME' || $sortBy === 'DEFAULT_COLLATION_NAME') {
$sql .= 'BINARY ';
}
$sql .= Util::backquote($sortBy)
. ' ' . $sortOrder
. $limit;
$sql .= ') a';
return $sql;
}
/**
* Function to get sql query for renaming the index using SQL RENAME INDEX Syntax
*/
public static function getSqlQueryForIndexRename(
string $dbName,
string $tableName,
string $oldIndexName,
string $newIndexName,
): string {
return sprintf(
'ALTER TABLE %s.%s RENAME INDEX %s TO %s;',
Util::backquote($dbName),
Util::backquote($tableName),
Util::backquote($oldIndexName),
Util::backquote($newIndexName),
);
}
/**
* Function to get sql query to re-order the table
*/
public static function getQueryForReorderingTable(
string $table,
string $orderField,
string|null $order,
): string {
return 'ALTER TABLE '
. Util::backquote($table)
. ' ORDER BY '
. Util::backquote($orderField)
. ($order === 'desc' ? ' DESC;' : ' ASC;');
}
/**
* Function to get sql query to partition the table
*
* @param string[] $partitionNames
*/
public static function getQueryForPartitioningTable(
string $table,
string $partitionOperation,
array $partitionNames,
): string {
$sqlQuery = 'ALTER TABLE '
. Util::backquote($table) . ' '
. $partitionOperation
. ' PARTITION ';
if ($partitionOperation === 'COALESCE') {
return $sqlQuery . count($partitionNames);
}
return $sqlQuery . implode(', ', $partitionNames) . ';';
}
/**
* @param string[] $selectedColumns
* @psalm-param 'FULLTEXT'|'INDEX'|'SPATIAL'|'UNIQUE' $indexType
*/
public static function getAddIndexSql(string $indexType, string $table, array $selectedColumns): string
{
$columnsSql = implode(', ', array_map(Util::backquote(...), $selectedColumns));
return 'ALTER TABLE ' . Util::backquote($table) . ' ADD ' . $indexType . '(' . $columnsSql . ');';
}
public static function getAddPrimaryKeyStatement(string $table, string $column, bool $hasDropPrimaryKey): string
{
if ($hasDropPrimaryKey) {
return sprintf(
'ALTER TABLE %s DROP PRIMARY KEY, ADD PRIMARY KEY(%s);',
Util::backquote($table),
Util::backquote($column),
);
}
return sprintf('ALTER TABLE %s ADD PRIMARY KEY(%s);', Util::backquote($table), Util::backquote($column));
}
/**
* Builds the SQL insert query
*
* @param bool $isInsertIgnore $_POST['submit_type'] === 'insertignore'
* @param string[] $queryFields column names array
* @param string[] $valueSets array of query values
*/
public static function buildInsertSqlQuery(
string $table,
bool $isInsertIgnore,
array $queryFields,
array $valueSets,
): string {
return ($isInsertIgnore ? 'INSERT IGNORE ' : 'INSERT ') . 'INTO '
. Util::backquote($table)
. ' (' . implode(', ', $queryFields) . ') VALUES ('
. implode('), (', $valueSets) . ')';
}
}