get_results($wpdb->prepare( "SHOW INDEX FROM %i WHERE Key_name = %s", $tableName, $indexName )); return !empty($results); } /** * Add an index if it does not exist. * * @param string $indexName Index name * @param string|array $columns Column name(s) * @param bool $unique Whether the index is unique * @return void */ protected static function addIndexIfNotExists($indexName, $columns, $unique = false) { if (static::hasIndex($indexName)) { return; } $type = $unique ? 'UNIQUE INDEX' : 'INDEX'; if (is_array($columns)) { $colsSql = '`' . implode('`, `', $columns) . '`'; } else { $colsSql = "`{$columns}`"; } Schema::alterTable( static::$tableName, "ADD {$type} `{$indexName}` ({$colsSql})" ); } /** * Drop an index if it exists. * * @param string $indexName Index name * @return void */ protected static function dropIndexIfExists($indexName) { if (!static::hasIndex($indexName)) { return; } Schema::dropIndex(static::$tableName, $indexName); } public static function getTableName(bool $withPrefix = true): string { return ($withPrefix ? static::getDbPrefix() : '') . static::$tableName; } public static function getDbPrefix(): string { global $wpdb; return $wpdb->prefix; } public static function getCharsetCollate(): string { global $wpdb; return $wpdb->get_charset_collate(); } public static function dropTable() { Schema::dropTableIfExists(static::getTableName(false)); } abstract public static function getSqlSchema(): string; }