Skip to content

Commit

Permalink
Adding basic connection code and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jul 2, 2012
1 parent 7085a9e commit dc7d287
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 7 deletions.
23 changes: 19 additions & 4 deletions lib/Cake/Model/Datasource/Database/Connection.php
Expand Up @@ -3,7 +3,8 @@
namespace Cake\Model\Datasource\Database;

use PDOException,
Exception;
Cake\Model\Datasource\Database\Exception\MissingDriverException,
Cake\Model\Datasource\Database\Exception\MissingExtensionException;

/**
* Represents a conection with a database server
Expand All @@ -27,7 +28,7 @@ class Connection {
protected $_driver;

/**
* Whether connection was stablished or not
* Whether connection was established or not
*
* @var boolean
**/
Expand All @@ -37,10 +38,20 @@ class Connection {
* Constructor
*
* @param array $config configuration for conencting to database
* @throws \Cake\Model\Datasource\Database\Exception\MissingDriverException if driver class can not be found
* @throws \Cake\Model\Datasource\Database\Exception\MissingExtensionException if driver cannot be used
* @return void
**/
public function __construct($config) {
$this->_config = $config;
if (!class_exists($config['datasource'])) {
throw new MissingDriverException(array('driver' => $config['datasource']));
}
$this->_driver = new $config['datasource'];

if (!$this->_driver->enabled()) {
throw new MissingExtensionException(array('driver' => get_class($this->_driver)));
}
}

/**
Expand All @@ -49,7 +60,10 @@ public function __construct($config) {
* @return boolean true on success or false if already connected
**/
public function connect() {

if ($this->_connected) {
return false;
}
return $this->_connected = $this->_driver->connect($this->_config);
}

/**
Expand All @@ -58,7 +72,8 @@ public function connect() {
* @return void
**/
public function disconnect() {

$this->_driver->disconnect();
$this->_connected = false;
}

/**
Expand Down
15 changes: 15 additions & 0 deletions lib/Cake/Model/Datasource/Database/Driver.php
Expand Up @@ -17,11 +17,26 @@ abstract class Driver {
**/
public abstract function connect(array $config);

/**
* Disconnects from database server
*
* @return void
**/
public abstract function disconnect();

/**
* Returns wheter php is able to use this driver for connecting to database
*
* @return boolean true if it is valid to use this driver
**/
public abstract function enabled();

/**
* Prepares a sql statement to be executed
*
* @param string $sql
* @return Cake\Model\Datasource\Database\Statement
**/
public abstract function prepare($sql);

}
16 changes: 13 additions & 3 deletions lib/Cake/Model/Datasource/Database/Driver/Mysql.php
Expand Up @@ -2,9 +2,10 @@

namespace Cake\Model\Datasource\Database\Driver;

use PDO;
use PDO,
Cake\Model\Datasource\Database\Statement;

class Mysql extends Cake\Model\Datasource\Database\Driver {
class Mysql extends \Cake\Model\Datasource\Database\Driver {

/**
* Base configuration settings for MySQL driver
Expand All @@ -16,7 +17,7 @@ class Mysql extends Cake\Model\Datasource\Database\Driver {
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => '',
'database' => 'cake',
'port' => '3306',
'flags' => array(),
'encoding' => 'utf8'
Expand Down Expand Up @@ -60,6 +61,15 @@ public function connect(array $config) {
return true;
}

/**
* Disconnects from database server
*
* @return void
**/
public function disconnect() {
$this->_connection = null;
}

/**
* Returns wheter php is able to use this driver for connecting to database
*
Expand Down
@@ -0,0 +1,25 @@
<?php
/**
*
* PHP Version 5.x
*
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The Open Group Test Suite License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.Model.Datasource.Database.Exception
* @since CakePHP(tm) v 3.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Model\Datasource\Database\Exception;


class MissingDriverException extends \Cake\Error\Exception {

protected $_messageTemplate = 'Database driver %s could not be found.';

}
@@ -0,0 +1,26 @@
<?php
/**
*
* PHP Version 5.x
*
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The Open Group Test Suite License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.Model.Datasource.Database.Exception
* @since CakePHP(tm) v 3.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Model\Datasource\Database\Exception;


class MissingExtensionException extends \Cake\Error\Exception {

protected $_messageTemplate = 'Database driver %s cannot be used due to a missing PHP extension or unmet dependency';

}

0 comments on commit dc7d287

Please sign in to comment.