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()
{
return new DB;
return new static();
}
/**

View file

@ -13,7 +13,6 @@
*/
class DBMysql extends DB
{
/**
* prefix of a tablename (One or more XEs can be installed in a single DB)
* @var string
@ -49,15 +48,6 @@ class DBMysql extends DB
$this->_connect();
}
/**
* Create an instance of this class
* @return DBMysql return DBMysql object instance
*/
function create()
{
return new DBMysql;
}
/**
* DB Connect
* this method is private
@ -84,12 +74,18 @@ class DBMysql extends DB
$this->setError(mysql_errno(), mysql_error());
return;
}
// Error appears if the version is lower than 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());
return;
}
// Set charset
$charset = isset($connection["db_charset"]) ? $connection["db_charset"] : 'utf8';
$this->_query("SET NAMES $charset", $connection);
// select db
@mysql_select_db($connection["db_database"], $result);
if(mysql_error())
@ -101,18 +97,6 @@ class DBMysql extends DB
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
* this method is private
@ -186,7 +170,7 @@ class DBMysql extends DB
exit('XE cannot handle DB connection.');
}
// Run the query statement
$result = mysql_query($query, $connection);
$result = @mysql_query($query, $connection);
// Error Check
if(mysql_error($connection))
{
@ -570,17 +554,16 @@ class DBMysql extends DB
*/
function _createTable($xml_doc)
{
// xml parsing
// Parse XML
$oXml = new XmlParser();
$xml_obj = $oXml->parse($xml_doc);
// Create a table schema
// Get table name and column list
$table_name = $xml_obj->table->attrs->name;
if($this->isTableExists($table_name))
{
return;
}
$table_name = $this->prefix . $table_name;
if(!is_array($xml_obj->table->column))
{
$columns[] = $xml_obj->table->column;
@ -589,11 +572,17 @@ class DBMysql extends DB
{
$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();
$unique_list = array();
$index_list = array();
// Process columns
foreach($columns as $column)
{
$name = $column->attrs->name;
@ -605,9 +594,32 @@ class DBMysql extends DB
$unique = $column->attrs->unique;
$default = $column->attrs->default;
$auto_increment = $column->attrs->auto_increment;
$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' : '');
$column_charset = '';
// 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)
{
$primary_list[] = $name;
@ -621,33 +633,45 @@ class DBMysql extends DB
$index_list[$index][] = $name;
}
}
// Process indexes
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))
{
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))
{
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);
if(!$output)
if($output)
{
return true;
}
else
{
return false;
}
}
/**
@ -788,7 +812,7 @@ class DBMysql extends DB
* @param boolean $force
* @return DBParser
*/
function &getParser($force = FALSE)
function getParser($force = FALSE)
{
$dbParser = new DBParser('`', '`', $this->prefix);
return $dbParser;
@ -967,6 +991,36 @@ class DBMysql extends DB
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');