MDL-34271 use the same collation when adding new tables or columns

This commit is contained in:
Petr Škoda 2012-07-11 16:39:20 +02:00
parent ed047dabc9
commit 1eece17648
2 changed files with 113 additions and 3 deletions

View file

@ -119,6 +119,9 @@ class mysql_sql_generator extends sql_generator {
public function getCreateTableSQL($xmldb_table) {
// First find out if want some special db engine.
$engine = $this->mdb->get_dbengine();
// Do we know collation?
$collation = $this->mdb->get_dbcollation();
$sqlarr = parent::getCreateTableSQL($xmldb_table);
// Let's inject the extra MySQL tweaks.
@ -127,6 +130,12 @@ class mysql_sql_generator extends sql_generator {
if ($engine) {
$sqlarr[$i] .= " ENGINE = $engine";
}
if ($collation) {
if (strpos($collation, 'utf8_') === 0) {
$sqlarr[$i] .= " DEFAULT CHARACTER SET utf8";
}
$sqlarr[$i] .= " DEFAULT COLLATE = $collation";
}
}
}
@ -141,9 +150,26 @@ class mysql_sql_generator extends sql_generator {
* @return array of sql statements
*/
public function getCreateTempTableSQL($xmldb_table) {
// Do we know collation?
$collation = $this->mdb->get_dbcollation();
$this->temptables->add_temptable($xmldb_table->getName());
$sqlarr = parent::getCreateTableSQL($xmldb_table); // we do not want the engine hack included in create table SQL
$sqlarr = preg_replace('/^CREATE TABLE (.*)/s', 'CREATE TEMPORARY TABLE $1', $sqlarr);
$sqlarr = parent::getCreateTableSQL($xmldb_table);
// Let's inject the extra MySQL tweaks.
foreach ($sqlarr as $i=>$sql) {
if (strpos($sql, 'CREATE TABLE ') === 0) {
// We do not want the engine hack included in create table SQL.
$sqlarr[$i] = preg_replace('/^CREATE TABLE (.*)/s', 'CREATE TEMPORARY TABLE $1', $sql);
if ($collation) {
if (strpos($collation, 'utf8_') === 0) {
$sqlarr[$i] .= " DEFAULT CHARACTER SET utf8";
}
$sqlarr[$i] .= " DEFAULT COLLATE $collation";
}
}
}
return $sqlarr;
}
@ -224,9 +250,21 @@ class mysql_sql_generator extends sql_generator {
$xmldb_length='255';
}
$dbtype .= '(' . $xmldb_length . ')';
if ($collation = $this->mdb->get_dbcollation()) {
if (strpos($collation, 'utf8_') === 0) {
$dbtype .= " CHARACTER SET utf8";
}
$dbtype .= " COLLATE $collation";
}
break;
case XMLDB_TYPE_TEXT:
$dbtype = 'LONGTEXT';
if ($collation = $this->mdb->get_dbcollation()) {
if (strpos($collation, 'utf8_') === 0) {
$dbtype .= " CHARACTER SET utf8";
}
$dbtype .= " COLLATE $collation";
}
break;
case XMLDB_TYPE_BINARY:
$dbtype = 'LONGBLOB';