Permalink
Browse files

Added support for charset to the PDO connection string. Also changed …

…host the string was constructed so that empty values are left out.
  • Loading branch information...
1 parent c617dfd commit ba87cffc10e94caa477933818ca3745e6e2dbd7b @rikh42 committed Mar 17, 2013
Showing with 28 additions and 2 deletions.
  1. +28 −2 core/ConnectionInfo.php
View
@@ -20,15 +20,17 @@ class ConnectionInfo
protected $user = '';
protected $password = '';
protected $database = '';
+ protected $charset = '';
protected $pdo = null;
- public function __construct($host, $port, $user, $password, $database)
+ public function __construct($host, $port, $user, $password, $database, $charset='')
{
$this->host = $host;
$this->port = $port;
$this->user = $user;
$this->password = $password;
$this->database = $database;
+ $this->charset = $charset;
}
public function setServer($host, $port)
@@ -51,6 +53,12 @@ public function setDatabase($database)
$this->pdo = null;
}
+ public function setCharset($charset)
+ {
+ $this->charset = $charset;
+ $this->pdo = null;
+ }
+
public function setPDO(PDO $pdo)
{
$this->pdo = $pdo;
@@ -68,7 +76,25 @@ public function resetPDO()
public function getConnectionString()
{
- return 'mysql:host='.$this->host.';port='.$this->port.';dbname='.$this->database;
+ $items = array();
+ if (!empty($this->host)) {
+ $items[] = "host={$this->host}";
+ }
+
+ if (!empty($this->port)) {
+ $items[] = "port={$this->port}";
+ }
+
+ if (!empty($this->database)) {
+ $items[] = "dbname={$this->database}";
+ }
+
+ if (!empty($this->charset)) {
+ $items[] = "charset={$this->charset}";
+ }
+
+ $cs = implode(';', $items);
+ return 'mysql:'.$cs;
}
public function getUsername()

0 comments on commit ba87cff

Please sign in to comment.