<commit_before><?php

namespace FTB\core\db;
use DB;
use Exception;

class MySQLSchemaController extends SQLSchemaController {

	private static $allowed_lock_modes = ['NONE', 'SHARED', 'EXCLUSIVE', 'DEFAULT'];

	public function addColumn($table, $name, $type, $options = []) {
		return $this->_alterColumn($table, $name, $type, $options, 'ADD');
	}

	public function alterColumn($table, $name, $type, $options = []) {
		return $this->_alterColumn($table, $name, $type, $options, 'MODIFY');
	}

	public function _alterColumn($table, $name, $type, $options = [], $action = 'MODIFY') {
		$type = strtoupper($type);
		if ($type !== 'TEXT' && strpos($type, 'VARCHAR') !== 0) {
			$options['charset'] = '';
			$options['collate'] = '';
		}

		$query = DB::with($table, $this->db);
		$table = $query->quoteKeyword($table);
		$name = $query->quoteKeyword($name);
		$sql = "ALTER TABLE $table $action COLUMN $name $type";
		if ($options['charset'] && $options['collate'] && $this->engine === static::ENGINE_MYSQL) {
			$sql .= " CHARACTER SET {$options['charset']} COLLATE {$options['collate']}";
		}
		if ($options['not_null']) {
			$sql .= " NOT NULL";
		}
		if (array_key_exists('default', $options)) {
			if ($options['default'] === null) {
				$sql .= " DEFAULT NULL";
			} else {
				$sql .= " DEFAULT " . $query->quote($options['default']);
			}
		}

		return $query->query($sql);
	}

	public function renameColumn($table, $old_name, $new_name) {
		$column_config = reset($this->showColumns($table, [$old_name]));
		$column_type = $column_config['column_type'];

		$query = DB::with($table, $this->db);
		$table = $query->quoteKeyword($table);
		$old_name = $query->quoteKeyword($old_name);
		$new_name = $query->quoteKeyword($new_name);

		$query_str = "ALTER TABLE $table CHANGE COLUMN $old_name $new_name $column_type";
		return $query->query($query_str);
	}

	public function addIndexes($table, array $indexes, array $options = []) {
		if (empty($indexes)) {
			throw new Exception('No indexes provided');
		}
		$query = DB::with($table, $this->db);
		$table = $query->quoteKeyword($table);
		$adds = [];
		foreach ($indexes as $index) {
			if ($index['type'] !== 'btree') {
				continue;
			}
			foreach ($index['columns'] as $key => $column) {
				$index['columns'][$key] = $query->quoteKeyword($column);
			}
			$name = $query->quoteKeyword($index['name']);
			$unique = $index['unique'] ? ' UNIQUE' : '';
			$adds[] = "ADD$unique INDEX $name (" . implode(',', $index['columns']) . ")";
		}
		$sql = "ALTER TABLE $table " . implode(', ', $adds);

		// Add lock mode if specified. These were introduced in the MySQL 5.6 "Online DDL" feature
		if ($options['lock'] && in_array($options['lock'], self::$allowed_lock_modes)) {
			$sql .= ", LOCK = {$options['lock']}";
		}

		return $query->query($sql);
	}

	public function addIndex($table, $name, $type, array $columns, $unique = false, array $options = []) {<commit_msg>DB: MySQL addIndexes now supports column length restrictions for TEXT/BLOB columns<commit_after><?php

namespace FTB\core\db;
use DB;
use Exception;

class MySQLSchemaController extends SQLSchemaController {

	private static $allowed_lock_modes = ['NONE', 'SHARED', 'EXCLUSIVE', 'DEFAULT'];

	public function addColumn($table, $name, $type, $options = []) {
		return $this->_alterColumn($table, $name, $type, $options, 'ADD');
	}

	public function alterColumn($table, $name, $type, $options = []) {
		return $this->_alterColumn($table, $name, $type, $options, 'MODIFY');
	}

	public function _alterColumn($table, $name, $type, $options = [], $action = 'MODIFY') {
		$type = strtoupper($type);
		if ($type !== 'TEXT' && strpos($type, 'VARCHAR') !== 0) {
			$options['charset'] = '';
			$options['collate'] = '';
		}

		$query = DB::with($table, $this->db);
		$table = $query->quoteKeyword($table);
		$name = $query->quoteKeyword($name);
		$sql = "ALTER TABLE $table $action COLUMN $name $type";
		if ($options['charset'] && $options['collate'] && $this->engine === static::ENGINE_MYSQL) {
			$sql .= " CHARACTER SET {$options['charset']} COLLATE {$options['collate']}";
		}
		if ($options['not_null']) {
			$sql .= " NOT NULL";
		}
		if (array_key_exists('default', $options)) {
			if ($options['default'] === null) {
				$sql .= " DEFAULT NULL";
			} else {
				$sql .= " DEFAULT " . $query->quote($options['default']);
			}
		}

		return $query->query($sql);
	}

	public function renameColumn($table, $old_name, $new_name) {
		$column_config = reset($this->showColumns($table, [$old_name]));
		$column_type = $column_config['column_type'];

		$query = DB::with($table, $this->db);
		$table = $query->quoteKeyword($table);
		$old_name = $query->quoteKeyword($old_name);
		$new_name = $query->quoteKeyword($new_name);

		$query_str = "ALTER TABLE $table CHANGE COLUMN $old_name $new_name $column_type";
		return $query->query($query_str);
	}

	public function addIndexes($table, array $indexes, array $options = []) {
		if (empty($indexes)) {
			throw new Exception('No indexes provided');
		}
		$query = DB::with($table, $this->db);
		$table = $query->quoteKeyword($table);
		$adds = [];
		foreach ($indexes as $index) {
			if ($index['type'] !== 'btree') {
				continue;
			}
			foreach ($index['columns'] as $key => $column) {
				if (is_array($column)) {
					$index['columns'][$key] = $query->quoteKeyword($column['name']);
					if (isset($column['length'])) {
						$index['columns'][$key] .= '(' . (int)$column['length'] . ')';
					}
				} else {
					$index['columns'][$key] = $query->quoteKeyword($column);
				}
			}
			$name = $query->quoteKeyword($index['name']);
			$unique = $index['unique'] ? ' UNIQUE' : '';
			$adds[] = "ADD$unique INDEX $name (" . implode(',', $index['columns']) . ")";
		}
		$sql = "ALTER TABLE $table " . implode(', ', $adds);

		// Add lock mode if specified. These were introduced in the MySQL 5.6 "Online DDL" feature
		if ($options['lock'] && in_array($options['lock'], self::$allowed_lock_modes)) {
			$sql .= ", LOCK = {$options['lock']}";
		}

		return $query->query($sql);
	}

	public function addIndex($table, $name, $type, array $columns, $unique = false, array $options = []) {
{'fraction_non_alphanumeric': 0.21499430801756383, 'fraction_numerical': 0.0009757684176288828, 'mean_word_length': 4.47640249332146, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1173142', 'n_tokens_mistral': 2315, 'n_tokens_neox': 2093, 'n_words': 465}