Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Adding initial Mysql driver implementation
  • Loading branch information
lorenzo committed Jul 2, 2012
1 parent 417ca21 commit a9112f1
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/Cake/Model/Datasource/Database/Driver.php
Expand Up @@ -12,9 +12,10 @@ abstract class Driver {
/**
* Establishes a conenction to the databse server
*
* @param array $config configuretion to be used for creating connection
* @return boolean true con success
**/
public abstract function connect($config = array());
public abstract function connect(array $config);

/**
* Returns wheter php is able to use this driver for connecting to database
Expand Down
73 changes: 73 additions & 0 deletions lib/Cake/Model/Datasource/Database/Driver/Mysql.php
@@ -0,0 +1,73 @@
<?php

namespace Cake\Model\Datasource\Database\Driver;

use PDO;

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

/**
* Base configuration settings for MySQL driver
*
* @var array
*/
protected $_baseConfig = array(
'persistent' => true,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => '',
'port' => '3306',
'flags' => array(),
'encoding' => 'utf8'
);

/**
* PDO instance associated to this connection
*
* @var PDO
**/
protected $_conenction;

/**
* Establishes a conenction to the databse server
*
* @param array $config configuretion to be used for creating connection
* @return boolean true on success
**/
public function connect(array $config) {
$config += $this->_baseConfig;
$flags = array(
PDO::ATTR_PERSISTENT => $config['persistent'],
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . $config['encoding']
) + $config['flags'];

if (empty($config['unix_socket'])) {
$dsn = "mysql:host={$config['host']};port={$config['port']};dbname={$config['database']}";
} else {
$dsn = "mysql:unix_socket={$config['unix_socket']};dbname={$config['database']}";
}

$this->_connection = new PDO(
$dsn,
$config['login'],
$config['password'],
$flags
);

return true;
}

/**
* 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 function enabled() {
return in_array('mysql', PDO::getAvailableDrivers());
}

}

0 comments on commit a9112f1

Please sign in to comment.