Skip to content

Commit

Permalink
Refactored MySQL::connect into Database::connect, addedd exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
jrgns committed May 25, 2012
1 parent e60ec57 commit 36fb107
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 32 deletions.
54 changes: 53 additions & 1 deletion data/source/Database.php
Expand Up @@ -8,9 +8,13 @@

namespace lithium\data\source;

use PDO;
use PDOException;
use lithium\util\String;
use lithium\util\Inflector;
use InvalidArgumentException;
use lithium\core\NetworkException;
use lithium\core\ConfigException;

/**
* The `Database` class provides the base-level abstraction for SQL-oriented relational databases.
Expand All @@ -22,6 +26,11 @@
*/
abstract class Database extends \lithium\data\Source {

/**
* @var PDO
*/
public $connection;

/**
* The supported column types and their default values
*
Expand Down Expand Up @@ -155,6 +164,49 @@ public function __construct(array $config = array()) {
parent::__construct($config + $defaults);
}

public function connect() {
$this->_isConnected = false;
$config = $this->_config;

if (empty($config['dsn'])) {
throw new ConfigException('No DSN setup for DB Connection');
} else {
$dsn = $config['dsn'];
}

//Construct Options
$options = array(
PDO::ATTR_PERSISTENT => $config['persistent'],
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
if (array_key_exists('options', $config)) {
$options = array_merge($options, $config['options']);
}

//Connect
try {
$this->connection = new PDO($dsn, $config['login'], $config['password'], $options);
} catch (PDOException $e) {
preg_match('/SQLSTATE\[(.+?)\]/', $e->getMessage(), $code);
$code = empty($code[1]) ? 0 : $code[1];
switch (true) {
case $code == 'HY000':
case substr($code, 0, 2) == '08':
throw new NetworkException($e->getMessage());
break;
case in_array($code, array('28000', '4200')):
default:
throw new ConfigException($e->getMessage());
break;
}
}
$this->_isConnected = true;
if ($this->_config['encoding']) {
$this->encoding($this->_config['encoding']);
}
return $this->_isConnected;
}

/**
* Field name handler to ensure proper escaping.
*
Expand Down Expand Up @@ -1012,4 +1064,4 @@ protected function _toNativeBoolean($value) {
}
}

?>
?>
43 changes: 12 additions & 31 deletions data/source/database/adapter/MySql.php
Expand Up @@ -12,6 +12,7 @@
use PDOStatement;
use PDOException;
use lithium\data\model\QueryException;
use lithium\core\ConfigException;

/**
* Extends the `Database` class to implement the necessary SQL-formatting and resultset-fetching
Expand All @@ -23,11 +24,6 @@
*/
class MySql extends \lithium\data\source\Database {

/**
* @var PDO
*/
public $connection;

protected $_classes = array(
'entity' => 'lithium\data\entity\Record',
'set' => 'lithium\data\collection\RecordSet',
Expand Down Expand Up @@ -119,39 +115,24 @@ public static function enabled($feature = null) {
* Connects to the database using the options provided to the class constructor.
*
* @return boolean Returns `true` if a database connection could be established, otherwise
* `false`.
* `false`.
*/
public function connect() {
$config = $this->_config;
$this->_isConnected = false;
$host = $config['host'];

if (!$config['database']) {
return false;
if (!$this->_config['database']) {
throw new ConfigException('No Database configured');
}

$options = array(
PDO::ATTR_PERSISTENT => $config['persistent'],
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);

try {
//Construct the DSN
if (empty($this->_config['dsn'])) {
$host = $this->_config['host'];
list($host, $port) = explode(':', $host) + array(1 => "3306");
$dsn = sprintf("mysql:host=%s;port=%s;dbname=%s", $host, $port, $config['database']);
$this->connection = new PDO($dsn, $config['login'], $config['password'], $options);
} catch (PDOException $e) {
return false;
$this->_config['dsn'] = sprintf("mysql:host=%s;port=%s;dbname=%s", $host, $port, $this->_config['database']);
}
if (parent::connect()) {

$this->_isConnected = true;
$info = $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION);

if ($config['encoding']) {
$this->encoding($config['encoding']);
$this->_useAlias = (boolean) version_compare($info, "4.1", ">=");
}

$info = $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION);

$this->_useAlias = (boolean) version_compare($info, "4.1", ">=");
return $this->_isConnected;
}

Expand Down Expand Up @@ -439,4 +420,4 @@ protected function _column($real) {
}
}

?>
?>

0 comments on commit 36fb107

Please sign in to comment.