Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set the charset/collation of the table/column on base of the connection charset/collation #192

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions xpdo/om/mysql/xpdomanager.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ public function removeObjectContainer($className) {

public function createObjectContainer($className) {
$created= false;
if ($this->xpdo->getConnection(array(xPDO::OPT_CONN_MUTABLE => true))) {
$connection = $this->xpdo->getConnection(array(xPDO::OPT_CONN_MUTABLE => true));
if ($connection) {
$charset = $connection->getOption('charset');
$collation = $connection->getOption('collation');
$instance= $this->xpdo->newObject($className);
if ($instance) {
$tableName= $this->xpdo->getTableName($className);
Expand All @@ -144,7 +147,7 @@ public function createObjectContainer($className) {
$fieldMeta = $this->xpdo->getFieldMeta($className, true);
$columns = array();
foreach ($fieldMeta as $key => $meta) {
$columns[] = $this->getColumnDef($className, $key, $meta);
$columns[] = $this->getColumnDef($className, $key, $meta, array('charset' => $charset, 'collation' => $collation));
/* Legacy index support for pre-2.0.0-rc3 models */
if ($legacyIndexes && isset ($meta['index']) && $meta['index'] !== 'pk') {
if ($meta['index'] === 'fulltext') {
Expand Down Expand Up @@ -250,6 +253,12 @@ public function createObjectContainer($className) {
$sql .= ")";
if (!empty($tableType)) {
$sql .= " ENGINE={$tableType}";
if (!empty($charset)) {
$sql .= " DEFAULT CHARSET={$charset}";
if (!empty($collation)) {
$sql .= " COLLATE={$collation}";
}
}
}
$created= $this->xpdo->exec($sql);
if ($created === false && $this->xpdo->errorCode() !== '' && $this->xpdo->errorCode() !== PDO::ERR_NONE) {
Expand Down Expand Up @@ -436,6 +445,13 @@ protected function getColumnDef($class, $name, $meta, array $options = array())
if (empty ($extra) && isset ($meta['extra'])) {
$extra= ' ' . $meta['extra'];
}
$charset = '';
if (!empty($options['charset']) && in_array($this->xpdo->driver->getPhpType($dbtype), array('string'))) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to support other PHP types (e.g. array, which gets serialized automatically) here? Perhaps this ought to check the dbtype for a whitelist of column types that support character sets instead?

$charset = ' CHARACTER SET ' . $options['charset'];
if (!empty($options['collation'])) {
$charset .= ' COLLATE ' . $options['collation'];
}
}
$default= '';
if (isset ($meta['default']) && !preg_match($lobsPattern, $dbtype)) {
$defaultVal= $meta['default'];
Expand All @@ -447,9 +463,9 @@ protected function getColumnDef($class, $name, $meta, array $options = array())
}
$attributes= (isset ($meta['attributes'])) ? ' ' . $meta['attributes'] : '';
if (strpos(strtolower($attributes), 'unsigned') !== false) {
$result = $this->xpdo->escape($name) . ' ' . $dbtype . $precision . $attributes . $null . $default . $extra;
$result = $this->xpdo->escape($name) . ' ' . $dbtype . $precision . $attributes . $charset . $null . $default . $extra;
} else {
$result = $this->xpdo->escape($name) . ' ' . $dbtype . $precision . $null . $default . $attributes . $extra;
$result = $this->xpdo->escape($name) . ' ' . $dbtype . $precision . $charset . $null . $default . $attributes . $extra;
}
return $result;
}
Expand Down