Add initial support for utf8mb4 in MySQL

This commit is contained in:
Kijin Sung 2016-01-11 10:27:54 +09:00
parent 5a060c9e9d
commit 708fbb4f06
4 changed files with 109 additions and 46 deletions

View file

@ -178,7 +178,7 @@ class DB
*/ */
function create() function create()
{ {
return new DB; return new static();
} }
/** /**

View file

@ -13,7 +13,6 @@
*/ */
class DBMysql extends DB class DBMysql extends DB
{ {
/** /**
* prefix of a tablename (One or more XEs can be installed in a single DB) * prefix of a tablename (One or more XEs can be installed in a single DB)
* @var string * @var string
@ -49,15 +48,6 @@ class DBMysql extends DB
$this->_connect(); $this->_connect();
} }
/**
* Create an instance of this class
* @return DBMysql return DBMysql object instance
*/
function create()
{
return new DBMysql;
}
/** /**
* DB Connect * DB Connect
* this method is private * this method is private
@ -84,12 +74,18 @@ class DBMysql extends DB
$this->setError(mysql_errno(), mysql_error()); $this->setError(mysql_errno(), mysql_error());
return; return;
} }
// Error appears if the version is lower than 4.1 // Error appears if the version is lower than 4.1
if(version_compare(mysql_get_server_info($result), '4.1', '<')) if(version_compare(mysql_get_server_info($result), '4.1', '<'))
{ {
$this->setError(-1, 'XE cannot be installed under the version of mysql 4.1. Current mysql version is ' . mysql_get_server_info()); $this->setError(-1, 'XE cannot be installed under the version of mysql 4.1. Current mysql version is ' . mysql_get_server_info());
return; return;
} }
// Set charset
$charset = isset($connection["db_charset"]) ? $connection["db_charset"] : 'utf8';
$this->_query("SET NAMES $charset", $connection);
// select db // select db
@mysql_select_db($connection["db_database"], $result); @mysql_select_db($connection["db_database"], $result);
if(mysql_error()) if(mysql_error())
@ -101,18 +97,6 @@ class DBMysql extends DB
return $result; return $result;
} }
/**
* If have a task after connection, add a taks in this method
* this method is private
* @param resource $connection
* @return void
*/
function _afterConnect($connection)
{
// Set utf8 if a database is MySQL
$this->_query("set names 'utf8'", $connection);
}
/** /**
* DB disconnection * DB disconnection
* this method is private * this method is private
@ -186,7 +170,7 @@ class DBMysql extends DB
exit('XE cannot handle DB connection.'); exit('XE cannot handle DB connection.');
} }
// Run the query statement // Run the query statement
$result = mysql_query($query, $connection); $result = @mysql_query($query, $connection);
// Error Check // Error Check
if(mysql_error($connection)) if(mysql_error($connection))
{ {
@ -570,17 +554,16 @@ class DBMysql extends DB
*/ */
function _createTable($xml_doc) function _createTable($xml_doc)
{ {
// xml parsing // Parse XML
$oXml = new XmlParser(); $oXml = new XmlParser();
$xml_obj = $oXml->parse($xml_doc); $xml_obj = $oXml->parse($xml_doc);
// Create a table schema
// Get table name and column list
$table_name = $xml_obj->table->attrs->name; $table_name = $xml_obj->table->attrs->name;
if($this->isTableExists($table_name)) if($this->isTableExists($table_name))
{ {
return; return;
} }
$table_name = $this->prefix . $table_name;
if(!is_array($xml_obj->table->column)) if(!is_array($xml_obj->table->column))
{ {
$columns[] = $xml_obj->table->column; $columns[] = $xml_obj->table->column;
@ -589,11 +572,17 @@ class DBMysql extends DB
{ {
$columns = $xml_obj->table->column; $columns = $xml_obj->table->column;
} }
// Determine the character set and collation for this table
$charset = $this->getBestSupportedCharset();
$collation = $charset . '_unicode_ci';
// Initialize the list of indexes
$primary_list = array(); $primary_list = array();
$unique_list = array(); $unique_list = array();
$index_list = array(); $index_list = array();
// Process columns
foreach($columns as $column) foreach($columns as $column)
{ {
$name = $column->attrs->name; $name = $column->attrs->name;
@ -605,9 +594,32 @@ class DBMysql extends DB
$unique = $column->attrs->unique; $unique = $column->attrs->unique;
$default = $column->attrs->default; $default = $column->attrs->default;
$auto_increment = $column->attrs->auto_increment; $auto_increment = $column->attrs->auto_increment;
$column_charset = '';
$column_schema[] = sprintf('`%s` %s%s %s %s %s', $name, $this->column_type[$type], $size ? '(' . $size . ')' : '', isset($default) ? "default '" . $default . "'" : '', $notnull ? 'not null' : '', $auto_increment ? 'auto_increment' : '');
// MySQL only supports 767 bytes for indexed columns.
// This is 191 characters in utf8mb4 and 255 characters in utf8.
if(($primary_key || $unique || $index) && preg_match('/(char|text)/i', $type))
{
if($size > 191 && $charset === 'utf8mb4')
{
$column_charset = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci';
}
if($size > 255)
{
$size = 255;
}
}
$column_schema[] = sprintf('`%s` %s%s %s %s %s %s',
$name,
$this->column_type[$type],
$size ? "($size)" : '',
$column_charset,
isset($default) ? "DEFAULT '$default'" : '',
$notnull ? 'NOT NULL' : '',
$auto_increment ? 'AUTO_INCREMENT' : ''
);
if($primary_key) if($primary_key)
{ {
$primary_list[] = $name; $primary_list[] = $name;
@ -621,33 +633,45 @@ class DBMysql extends DB
$index_list[$index][] = $name; $index_list[$index][] = $name;
} }
} }
// Process indexes
if(count($primary_list)) if(count($primary_list))
{ {
$column_schema[] = sprintf("primary key (%s)", '`' . implode($primary_list, '`,`') . '`'); $column_schema[] = sprintf("PRIMARY KEY (%s)", '`' . implode($primary_list, '`, `') . '`');
} }
if(count($unique_list)) if(count($unique_list))
{ {
foreach($unique_list as $key => $val) foreach($unique_list as $key => $val)
{ {
$column_schema[] = sprintf("unique %s (%s)", $key, '`' . implode($val, '`,`') . '`'); $column_schema[] = sprintf("UNIQUE %s (%s)", $key, '`' . implode($val, '`, `') . '`');
} }
} }
if(count($index_list)) if(count($index_list))
{ {
foreach($index_list as $key => $val) foreach($index_list as $key => $val)
{ {
$column_schema[] = sprintf("index %s (%s)", $key, '`' . implode($val, '`,`') . '`'); $column_schema[] = sprintf("INDEX %s (%s)", $key, '`' . implode($val, '`, `') . '`');
} }
} }
$schema = sprintf('create table `%s` (%s%s) %s;', $this->addQuotes($table_name), "\n", implode($column_schema, ",\n"), "ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci"); // Generate table schema
$engine = stripos(get_class($this), 'innodb') === false ? 'MYISAM' : 'INNODB';
$schema = sprintf("CREATE TABLE `%s` (%s) %s",
$this->addQuotes($this->prefix . $table_name),
"\n" . implode($column_schema, ",\n") . "\n",
"ENGINE = $engine CHARACTER SET $charset COLLATE $collation"
);
// Execute the complete query
$output = $this->_query($schema); $output = $this->_query($schema);
if(!$output) if($output)
{
return true;
}
else
{
return false; return false;
}
} }
/** /**
@ -788,7 +812,7 @@ class DBMysql extends DB
* @param boolean $force * @param boolean $force
* @return DBParser * @return DBParser
*/ */
function &getParser($force = FALSE) function getParser($force = FALSE)
{ {
$dbParser = new DBParser('`', '`', $this->prefix); $dbParser = new DBParser('`', '`', $this->prefix);
return $dbParser; return $dbParser;
@ -967,6 +991,36 @@ class DBMysql extends DB
return $select . ' ' . $from . ' ' . $where . ' ' . $groupBy . ' ' . $orderBy . ' ' . $limit; return $select . ' ' . $from . ' ' . $where . ' ' . $groupBy . ' ' . $orderBy . ' ' . $limit;
} }
/**
* Find out the best supported character set
*
* @return string
*/
function getBestSupportedCharset()
{
static $cache = null;
if ($cache !== null)
{
return $cache;
}
if($output = $this->_fetch($this->_query("SHOW CHARACTER SET LIKE 'utf8mb4%'")))
{
$mb4_support = false;
foreach($output as $row)
{
if($row->Charset === 'utf8mb4')
{
$mb4_support = true;
}
}
return $cache = ($mb4_support ? 'utf8' : 'utf8');
}
else
{
return $cache = 'utf8';
}
}
} }
DBMysql::$isSupported = function_exists('mysql_connect'); DBMysql::$isSupported = function_exists('mysql_connect');

View file

@ -132,6 +132,15 @@ class installController extends install
// Check if available to connect to the DB // Check if available to connect to the DB
if(!$oDB->isConnected()) return $oDB->getError(); if(!$oDB->isConnected()) return $oDB->getError();
// Check DB charset if using MySQL
if(stripos($db_info->master_db['db_type'], 'mysql') !== false)
{
$db_charset = $oDB->getBestSupportedCharset();
$db_info->master_db['db_charset'] = $db_charset;
$db_info->slave_db[0]['db_charset'] = $db_charset;
Context::setDBInfo($db_info);
}
// Install all the modules // Install all the modules
try { try {
$oDB->begin(); $oDB->begin();
@ -547,7 +556,7 @@ class installController extends install
function installModule($module, $module_path) function installModule($module, $module_path)
{ {
// create db instance // create db instance
$oDB = &DB::getInstance(); $oDB = DB::getInstance();
// Create a table if the schema xml exists in the "schemas" directory of the module // Create a table if the schema xml exists in the "schemas" directory of the module
$schema_dir = sprintf('%s/schemas/', $module_path); $schema_dir = sprintf('%s/schemas/', $module_path);
$schema_files = FileHandler::readDir($schema_dir, NULL, false, true); $schema_files = FileHandler::readDir($schema_dir, NULL, false, true);

View file

@ -1,7 +1,7 @@
<table name="sites"> <table name="sites">
<column name="site_srl" type="number" size="11" notnull="notnull" primary_key="primary_key" /> <column name="site_srl" type="number" size="11" notnull="notnull" primary_key="primary_key" />
<column name="index_module_srl" type="number" size="11" default="0" /> <column name="index_module_srl" type="number" size="11" default="0" />
<column name="domain" type="varchar" size="255" notnull="notnull" /> <column name="domain" type="varchar" size="191" notnull="notnull" />
<column name="default_language" type="varchar" size="255" /> <column name="default_language" type="varchar" size="255" />
<column name="regdate" type="date" /> <column name="regdate" type="date" />
</table> </table>