Moves code out of the try/catch blocks in the connect() methods.

That code wouldn't throw a PDOException, so having it in the
try/catch block is pointless. Moving it out makes it easier to
read.
This commit is contained in:
Phally 2013-07-15 06:18:09 +02:00
parent c7e4076770
commit 1fe424a62b
3 changed files with 33 additions and 24 deletions
lib/Cake/Model/Datasource/Database

View file

@ -136,20 +136,24 @@ class Mysql extends DboSource {
public function connect() { public function connect() {
$config = $this->config; $config = $this->config;
$this->connected = false; $this->connected = false;
try {
$flags = array( $flags = array(
PDO::ATTR_PERSISTENT => $config['persistent'], PDO::ATTR_PERSISTENT => $config['persistent'],
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true, PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
); );
if (!empty($config['encoding'])) { if (!empty($config['encoding'])) {
$flags[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES ' . $config['encoding']; $flags[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES ' . $config['encoding'];
} }
if (empty($config['unix_socket'])) { if (empty($config['unix_socket'])) {
$dsn = "mysql:host={$config['host']};port={$config['port']};dbname={$config['database']}"; $dsn = "mysql:host={$config['host']};port={$config['port']};dbname={$config['database']}";
} else { } else {
$dsn = "mysql:unix_socket={$config['unix_socket']};dbname={$config['database']}"; $dsn = "mysql:unix_socket={$config['unix_socket']};dbname={$config['database']}";
} }
try {
$this->_connection = new PDO( $this->_connection = new PDO(
$dsn, $dsn,
$config['login'], $config['login'],

View file

@ -110,11 +110,13 @@ class Postgres extends DboSource {
public function connect() { public function connect() {
$config = $this->config; $config = $this->config;
$this->connected = false; $this->connected = false;
try {
$flags = array( $flags = array(
PDO::ATTR_PERSISTENT => $config['persistent'], PDO::ATTR_PERSISTENT => $config['persistent'],
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
); );
try {
$this->_connection = new PDO( $this->_connection = new PDO(
"pgsql:host={$config['host']};port={$config['port']};dbname={$config['database']}", "pgsql:host={$config['host']};port={$config['port']};dbname={$config['database']}",
$config['login'], $config['login'],

View file

@ -117,14 +117,17 @@ class Sqlserver extends DboSource {
public function connect() { public function connect() {
$config = $this->config; $config = $this->config;
$this->connected = false; $this->connected = false;
try {
$flags = array( $flags = array(
PDO::ATTR_PERSISTENT => $config['persistent'], PDO::ATTR_PERSISTENT => $config['persistent'],
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
); );
if (!empty($config['encoding'])) { if (!empty($config['encoding'])) {
$flags[PDO::SQLSRV_ATTR_ENCODING] = $config['encoding']; $flags[PDO::SQLSRV_ATTR_ENCODING] = $config['encoding'];
} }
try {
$this->_connection = new PDO( $this->_connection = new PDO(
"sqlsrv:server={$config['host']};Database={$config['database']}", "sqlsrv:server={$config['host']};Database={$config['database']}",
$config['login'], $config['login'],