add a function for setting a mysql connection's charset, turning a charset name into...
authorChuck Hagenbuch <chuck@horde.org>
Tue, 23 Dec 2008 19:14:23 +0000 (14:14 -0500)
committerChuck Hagenbuch <chuck@horde.org>
Tue, 23 Dec 2008 19:14:23 +0000 (14:14 -0500)
framework/Db/lib/Horde/Db/Adapter/Mysql/Schema.php
framework/Db/lib/Horde/Db/Adapter/Mysqli.php
framework/Db/lib/Horde/Db/Adapter/Pdo/Mysql.php
framework/Db/test/Horde/Db/Adapter/MysqliTest.php
framework/Db/test/Horde/Db/Adapter/Pdo/MysqlTest.php

index 0236600..92a300a 100644 (file)
@@ -134,13 +134,41 @@ class Horde_Db_Adapter_Mysql_Schema extends Horde_Db_Adapter_Abstract_Schema
     }
 
     /**
-     * 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;
     }
 
     /**
index 662b6bc..e779899 100644 (file)
@@ -134,11 +134,17 @@ class Horde_Db_Adapter_Mysqli extends Horde_Db_Adapter_Abstract
 
         // 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']);
+        }
     }
 
     /**
index ff2b6ee..c9d5940 100644 (file)
@@ -59,7 +59,11 @@ class Horde_Db_Adapter_Pdo_Mysql extends Horde_Db_Adapter_Pdo_Abstract
         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']);
+        }
     }
 
 
index 7eed22d..c058adf 100644 (file)
@@ -128,6 +128,11 @@ class Horde_Db_Adapter_MysqliTest extends PHPUnit_Framework_TestCase
         $this->assertTrue($this->_conn->supportsCountDistinct());
     }
 
+    public function testGetCharset()
+    {
+        $this->assertEquals('utf8', strtolower($this->_conn->getCharset()));
+    }
+
 
     /*##########################################################################
     # Database Statements
@@ -653,6 +658,8 @@ class Horde_Db_Adapter_MysqliTest extends PHPUnit_Framework_TestCase
     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');
@@ -662,7 +669,7 @@ class Horde_Db_Adapter_MysqliTest extends PHPUnit_Framework_TestCase
         "  `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.
index c1a14f7..2ee5de6 100644 (file)
@@ -128,6 +128,11 @@ class Horde_Db_Adapter_Pdo_MysqlTest extends PHPUnit_Framework_TestCase
         $this->assertTrue($this->_conn->supportsCountDistinct());
     }
 
+    public function testGetCharset()
+    {
+        $this->assertEquals('utf8', strtolower($this->_conn->getCharset()));
+    }
+
 
     /*##########################################################################
     # Database Statements
@@ -653,6 +658,8 @@ class Horde_Db_Adapter_Pdo_MysqlTest extends PHPUnit_Framework_TestCase
     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');
@@ -662,7 +669,7 @@ class Horde_Db_Adapter_Pdo_MysqlTest extends PHPUnit_Framework_TestCase
         "  `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.