Changeset 2432

Show
Ignore:
Timestamp:
04/18/2008 04:02:05 AM
Author:
tof
Message:

Implemented #800

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/HISTORY

    r2427 r2432  
    3939ENH: Ticket#757 - TDateFormat and TNumberFormat now implement IDataRenderer (Qiang) 
    4040ENH: Ticket#783 - Added date/time type support to soap implementation (Hongliang) 
     41ENH: Ticket#800 - Added database connection charset for MySql (donkee) 
    4142ENH: Active Record supports multiple foreign references of the same table (Wei) 
    4243ENH: Added TDbCommand.queryColumn() (Qiang) 
  • trunk/framework/Data/TDbConnection.php

    r2400 r2432  
    2828 * and {@link setPassword Password}. 
    2929 * 
     30 * Since 3.1.2, the connection charset can be set (for MySQL databases only) using the {@link setCharset Charset} property. 
     31 *  
    3032 * The following example shows how to create a TDbConnection instance and establish 
    3133 * the actual connection: 
     
    8385        private $_username=''; 
    8486        private $_password=''; 
     87        private $_charset=''; 
    8588        private $_attributes=array(); 
    8689        private $_active=false; 
     
    9396         * instance is created. Set {@link setActive Active} property to true 
    9497         * to establish the connection. 
     98         * Since 3.1.2, you can set the charset for MySql connection 
     99         *  
    95100         * @param string The Data Source Name, or DSN, contains the information required to connect to the database. 
    96101         * @param string The user name for the DSN string. 
    97102         * @param string The password for the DSN string. 
     103         * @param string Charset used for DB Connection (MySql only). If not set, will use the default charset of your database server 
    98104         * @see http://www.php.net/manual/en/function.PDO-construct.php 
    99105         */ 
    100         public function __construct($dsn='',$username='',$password=''
     106        public function __construct($dsn='',$username='',$password='', $charset=''
    101107        { 
    102108                $this->_dsn=$dsn; 
    103109                $this->_username=$username; 
    104110                $this->_password=$password; 
     111                $this->_charset=$charset; 
    105112        } 
    106113 
     
    162169                                $this->_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    163170                                $this->_active=true; 
     171                                $this->setConnectionCharset(); 
    164172                        } 
    165173                        catch(PDOException $e) 
     
    180188        } 
    181189 
     190        /* 
     191         * Set the database connection charset. 
     192         * Only MySql databases are supported for now.  
     193         * @since 3.1.2 
     194         */ 
     195        protected function setConnectionCharset() 
     196        { 
     197                if ($this->_charset === '' || $this->_active === false) 
     198                        return; 
     199                switch ($this->_pdo->getAttribute(PDO::ATTR_DRIVER_NAME)) 
     200                { 
     201                        case 'mysql': 
     202                                $stmt = $this->_pdo->prepare('SET CHARACTER SET ?'); 
     203                                $stmt->execute(array($this->_charset)); 
     204                        break; 
     205                } 
     206        } 
     207                 
    182208        /** 
    183209         * @return string The Data Source Name, or DSN, contains the information required to connect to the database. 
     
    229255        } 
    230256 
     257        /** 
     258         * @return string the charset used for database connection. Defaults to emtpy string. 
     259         */ 
     260        public function getCharset () 
     261        { 
     262                return $this>_charset; 
     263        } 
     264         
     265        /** 
     266         * @param string the charset used for database connection 
     267         */ 
     268        public function setCharset ($value) 
     269        { 
     270                $this->_charset=$value; 
     271                $this->setConnectionCharset(); 
     272        } 
     273         
    231274        /** 
    232275         * @return PDO the PDO instance, null if the connection is not established yet