}
/**
- * Returns the database character set
+ * Returns the database character set that query results are in
*
* @return string
*/
public function getCharset()
{
- return $this->showVariable('character_set_database');
+ return $this->showVariable('character_set_results');
+ }
+
+ /**
+ * Set the client and result charset.
+ *
+ * @param string $charset The character set to use for client queries and results.
+ */
+ public function setCharset($charset)
+ {
+ $charset = $this->_mysqlCharsetName($charset);
+ $this->execute('SET NAMES ' . $this->quoteString($charset));
+ }
+
+ /**
+ * Get the MySQL name of a given character set.
+ *
+ * @param string $charset
+ * @return string MySQL-normalized charset.
+ */
+ public function _mysqlCharsetName($charset)
+ {
+ $charset = strtolower(preg_replace('/[^a-zA-Z0-9]/', '', $charset));
+ $validCharsets = $this->selectValues('SHOW CHARACTER SET');
+ if (!in_array($charset, $validCharsets)) {
+ throw new Horde_Db_Exception($charset . ' is not supported by MySQL');
+ }
+
+ return $charset;
}
/**
// If supported, request real datatypes from MySQL instead of returning
// everything as a string.
- if (defined('MYSQLI_OPT_INT_AND_FLOAT_NATIVE'))
+ if (defined('MYSQLI_OPT_INT_AND_FLOAT_NATIVE')) {
$mysqli->options(MYSQLI_OPT_INT_AND_FLOAT_NATIVE, true);
+ }
$this->_connection = $mysqli;
$this->_active = true;
+
+ // Set the default charset. http://dev.mysql.com/doc/refman/5.1/en/charset-connection.html
+ if (!empty($config['charset'])) {
+ $this->setCharset($config['charset']);
+ }
}
/**
parent::connect();
// ? $this->_connection->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
- // SET NAMES ?
+
+ // Set the default charset. http://dev.mysql.com/doc/refman/5.1/en/charset-connection.html
+ if (!empty($this->_config['charset'])) {
+ $this->setCharset($this->_config['charset']);
+ }
}
$this->assertTrue($this->_conn->supportsCountDistinct());
}
+ public function testGetCharset()
+ {
+ $this->assertEquals('utf8', strtolower($this->_conn->getCharset()));
+ }
+
/*##########################################################################
# Database Statements
public function testStructureDump()
{
$this->_createTestTable('sports');
+ // Avoid AUTO_INCREMENT being a part of the dump
+ $this->_conn->execute('TRUNCATE TABLE sports');
// single table
$structure = $this->_conn->structureDump('sports');
" `name` varchar(255) DEFAULT NULL,\n".
" `is_college` tinyint(1) DEFAULT NULL,\n".
" PRIMARY KEY (`id`)\n".
- ") ENGINE=InnoDB";
+ ") ENGINE=InnoDB DEFAULT CHARSET=utf8";
// MySQL differs in how it dumps table structure between versions, so do
// some normalization.
$this->assertTrue($this->_conn->supportsCountDistinct());
}
+ public function testGetCharset()
+ {
+ $this->assertEquals('utf8', strtolower($this->_conn->getCharset()));
+ }
+
/*##########################################################################
# Database Statements
public function testStructureDump()
{
$this->_createTestTable('sports');
+ // Avoid AUTO_INCREMENT being a part of the dump
+ $this->_conn->execute('TRUNCATE TABLE sports');
// single table
$structure = $this->_conn->structureDump('sports');
" `name` varchar(255) DEFAULT NULL,\n".
" `is_college` tinyint(1) DEFAULT NULL,\n".
" PRIMARY KEY (`id`)\n".
- ") ENGINE=InnoDB";
+ ") ENGINE=InnoDB DEFAULT CHARSET=utf8";
// MySQL differs in how it dumps table structure between versions, so do
// some normalization.