From fe0c985d83a291d1f6fdaf09aa17efc16962c679 Mon Sep 17 00:00:00 2001 From: tof <> Date: Fri, 18 Apr 2008 09:02:05 +0000 Subject: Implemented #800 --- HISTORY | 1 + framework/Data/TDbConnection.php | 61 ++++++++++++++++++++++++++++++++++------ 2 files changed, 53 insertions(+), 9 deletions(-) diff --git a/HISTORY b/HISTORY index e3107577..151ec365 100644 --- a/HISTORY +++ b/HISTORY @@ -38,6 +38,7 @@ ENH: Ticket#743 - Added status code header in HTTP response when in error (Qiang ENH: Ticket#745 - TWizard action MoveTo - allow specify step ID's (Christophe) ENH: Ticket#757 - TDateFormat and TNumberFormat now implement IDataRenderer (Qiang) ENH: Ticket#783 - Added date/time type support to soap implementation (Hongliang) +ENH: Ticket#800 - Added database connection charset for MySql (donkee) ENH: Active Record supports multiple foreign references of the same table (Wei) ENH: Added TDbCommand.queryColumn() (Qiang) ENH: Active Record now supports implicitly declared related properties (Qiang) diff --git a/framework/Data/TDbConnection.php b/framework/Data/TDbConnection.php index 1500eefa..08a8695a 100644 --- a/framework/Data/TDbConnection.php +++ b/framework/Data/TDbConnection.php @@ -26,7 +26,9 @@ Prado::using('System.Data.TDbCommand'); * To establish a connection, set {@link setActive Active} to true after * specifying {@link setConnectionString ConnectionString}, {@link setUsername Username} * and {@link setPassword Password}. - * + * + * Since 3.1.2, the connection charset can be set (for MySQL databases only) using the {@link setCharset Charset} property. + * * The following example shows how to create a TDbConnection instance and establish * the actual connection: * @@ -81,7 +83,8 @@ class TDbConnection extends TComponent { private $_dsn=''; private $_username=''; - private $_password=''; + private $_password=''; + private $_charset=''; private $_attributes=array(); private $_active=false; private $_pdo=null; @@ -91,17 +94,21 @@ class TDbConnection extends TComponent * Constructor. * Note, the DB connection is not established when this connection * instance is created. Set {@link setActive Active} property to true - * to establish the connection. + * to establish the connection. + * Since 3.1.2, you can set the charset for MySql connection + * * @param string The Data Source Name, or DSN, contains the information required to connect to the database. * @param string The user name for the DSN string. - * @param string The password for the DSN string. + * @param string The password for the DSN string. + * @param string Charset used for DB Connection (MySql only). If not set, will use the default charset of your database server * @see http://www.php.net/manual/en/function.PDO-construct.php */ - public function __construct($dsn='',$username='',$password='') + public function __construct($dsn='',$username='',$password='', $charset='') { $this->_dsn=$dsn; $this->_username=$username; - $this->_password=$password; + $this->_password=$password; + $this->_charset=$charset; } /** @@ -160,7 +167,8 @@ class TDbConnection extends TComponent $this->_pdo=new PDO($this->getConnectionString(),$this->getUsername(), $this->getPassword(),$this->_attributes); $this->_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - $this->_active=true; + $this->_active=true; + $this->setConnectionCharset(); } catch(PDOException $e) { @@ -178,7 +186,25 @@ class TDbConnection extends TComponent $this->_pdo=null; $this->_active=false; } - + + /* + * Set the database connection charset. + * Only MySql databases are supported for now. + * @since 3.1.2 + */ + protected function setConnectionCharset() + { + if ($this->_charset === '' || $this->_active === false) + return; + switch ($this->_pdo->getAttribute(PDO::ATTR_DRIVER_NAME)) + { + case 'mysql': + $stmt = $this->_pdo->prepare('SET CHARACTER SET ?'); + $stmt->execute(array($this->_charset)); + break; + } + } + /** * @return string The Data Source Name, or DSN, contains the information required to connect to the database. */ @@ -227,7 +253,24 @@ class TDbConnection extends TComponent { $this->_password=$value; } - + + /** + * @return string the charset used for database connection. Defaults to emtpy string. + */ + public function getCharset () + { + return $this>_charset; + } + + /** + * @param string the charset used for database connection + */ + public function setCharset ($value) + { + $this->_charset=$value; + $this->setConnectionCharset(); + } + /** * @return PDO the PDO instance, null if the connection is not established yet */ -- cgit v1.2.3