Skip to content
Permalink
Browse files

Inital work for connecting to mysql using PDO and gettng the table li…

…st, testing is easier as it now uses mocks
  • Loading branch information...
lorenzo committed Oct 14, 2010
1 parent efbefeb commit b8479459d6ecc17d36445894382465fc6688a656
@@ -539,6 +539,15 @@ class DboMysql extends DboMysqlBase {
'port' => '3306'
);
protected $_errors = array();
/**
* Reference to the PDO object connection
*
* @var PDO $_connection
*/
protected $_connection = null;
/**
* Connects to the database using options in the given configuration array.
*
@@ -547,27 +556,31 @@ class DboMysql extends DboMysqlBase {
function connect() {
$config = $this->config;
$this->connected = false;
if (!$config['persistent']) {
$this->connection = mysql_connect($config['host'] . ':' . $config['port'], $config['login'], $config['password'], true);
$config['connect'] = 'mysql_connect';
} else {
$this->connection = mysql_pconnect($config['host'] . ':' . $config['port'], $config['login'], $config['password']);
}
if (mysql_select_db($config['database'], $this->connection)) {
try {
$flags = array(PDO::ATTR_PERSISTENT => $config['persistent']);
if (!empty($config['encoding'])) {
$flags[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES ' . $config['encoding'];
}
$this->_connection = new PDO(
"mysql:{$config['host']}:{$config['port']};dbname={$config['database']}",
$config['login'],
$config['password'],
$flags
);
$this->connected = true;
} catch (PDOException $e) {
$this->errors[] = $e->getMessage();
}
if (!empty($config['encoding'])) {
$this->setEncoding($config['encoding']);
}
$this->_useAlias = (bool)version_compare(mysql_get_server_info($this->connection), "4.1", ">=");
//$this->_useAlias = (bool)version_compare(mysql_get_server_info($this->connection), "4.1", ">=");
return $this->connected;
}
public function getConnection() {
return $this->_connection;
}
/**
* Check whether the MySQL extension is installed/loaded
*
@@ -593,10 +606,17 @@ function disconnect() {
* Executes given SQL statement.
*
* @param string $sql SQL statement
* @return resource Result resource identifier
*/
protected function _execute($sql) {
return mysql_query($sql, $this->connection);
* @param array $params list of params to be bound to query
* @return PDOStatement if query executes with no problem, false otherwise
*/
protected function _execute($sql, $params = array()) {
$query = $this->_connection->prepare($sql);
$query->setFetchMode(PDO::FETCH_LAZY);
if (!$query->execute($params)) {
$this->errors[] = $query->errorInfo();
return false;
}
return $query;
}
/**
@@ -609,14 +629,14 @@ function listSources() {
if ($cache != null) {
return $cache;
}
$result = $this->_execute('SHOW TABLES FROM ' . $this->name($this->config['database']) . ';');
$result = $this->_execute('SHOW TABLES FROM ' . $this->config['database']);
if (!$result) {
return array();
} else {
$tables = array();
while ($line = mysql_fetch_row($result)) {
while ($line = $result->fetch()) {
$tables[] = $line[0];
}
parent::listSources($tables);
@@ -158,7 +158,7 @@ function schema() {
* @subpackage cake.tests.cases.libs.model.datasources.dbo
*/
class DboMysqlTest extends CakeTestCase {
public $fixtures = array('core.binary_test');
//public $fixtures = array('core.binary_test');
/**
* The Dbo instance to be tested
*
@@ -838,4 +838,28 @@ function testDescribeGettingFieldParameters() {
$this->db->execute($this->db->dropSchema($schema));
}

/**
* Tests that listSources method sends the correct query and parses the result accordingly
* @return void
*/
public function testListSources() {
$db = $this->getMock('DboMysql', array('connect', '_execute'));
$queryResult = $this->getMock('PDOStatement');
$db->expects($this->once())
->method('_execute')
->with('SHOW TABLES FROM cake')
->will($this->returnValue($queryResult));
$queryResult->expects($this->at(0))
->method('fetch')
->will($this->returnValue(array('cake_table')));
$queryResult->expects($this->at(1))
->method('fetch')
->will($this->returnValue(array('another_table')));
$queryResult->expects($this->at(2))
->method('fetch')
->will($this->returnValue(null));

$tables = $db->listSources();
$this->assertEqual($tables, array('cake_table', 'another_table'));
}
}

0 comments on commit b847945

Please sign in to comment.
You can’t perform that action at this time.