name), $newTrigger->timing->value, $newTrigger->event->value, Util::backquote($newTrigger->table), $newTrigger->statement, $delimiter, ); } public static function getInformationSchemaDataForCreateRequest(string $user, string $host): string { return 'SELECT 1 FROM `INFORMATION_SCHEMA`.`USER_PRIVILEGES` ' . "WHERE `PRIVILEGE_TYPE` = 'CREATE USER' AND " . "'''" . $user . "''@''" . $host . "''' LIKE `GRANTEE` LIMIT 1"; } public static function getInformationSchemaDataForGranteeRequest(string $user, string $host): string { 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` 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; } public static function getInformationSchemaColumnsFullRequest( string|null $escapedDatabase, string|null $escapedTable, string|null $escapedColumn, ): string { $sqlWheres = []; // get columns information from information_schema if ($escapedDatabase !== null) { $sqlWheres[] = '`TABLE_SCHEMA` = ' . $escapedDatabase . ' '; } if ($escapedTable !== null) { $sqlWheres[] = '`TABLE_NAME` = ' . $escapedTable . ' '; } if ($escapedColumn !== null) { $sqlWheres[] = '`COLUMN_NAME` = ' . $escapedColumn . ' '; } // for PMA bc: // `[SCHEMA_FIELD_NAME]` AS `[SHOW_FULL_COLUMNS_FIELD_NAME]` $sql = '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`'; if ($sqlWheres !== []) { $sql .= "\n" . ' WHERE ' . implode(' AND ', $sqlWheres); } return $sql; } /** @return string[] */ public static function getInformationSchemaColumns( string|null $database, string|null $table, string|null $column, ): array { $arrayKeys = []; if ($database === null) { $arrayKeys[] = 'TABLE_SCHEMA'; } if ($table === null) { $arrayKeys[] = 'TABLE_NAME'; } if ($column === null) { $arrayKeys[] = 'COLUMN_NAME'; } return $arrayKeys; } /** * 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 */ 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 . ');'; } /** * 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) . ')'; } }