From b8479459d6ecc17d36445894382465fc6688a656 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Lorenzo=20Rodr=C3=ADguez?= Date: Thu, 14 Oct 2010 01:10:51 -0430 Subject: [PATCH] Inital work for connecting to mysql using PDO and gettng the table list, testing is easier as it now uses mocks --- cake/libs/model/datasources/dbo/dbo_mysql.php | 60 ++++++++++++------- .../model/datasources/dbo/dbo_mysql.test.php | 26 +++++++- 2 files changed, 65 insertions(+), 21 deletions(-) diff --git a/cake/libs/model/datasources/dbo/dbo_mysql.php b/cake/libs/model/datasources/dbo/dbo_mysql.php index 69770db6116..671c3265935 100644 --- a/cake/libs/model/datasources/dbo/dbo_mysql.php +++ b/cake/libs/model/datasources/dbo/dbo_mysql.php @@ -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); diff --git a/cake/tests/cases/libs/model/datasources/dbo/dbo_mysql.test.php b/cake/tests/cases/libs/model/datasources/dbo/dbo_mysql.test.php index 82ac17c2371..11d81938cb7 100644 --- a/cake/tests/cases/libs/model/datasources/dbo/dbo_mysql.test.php +++ b/cake/tests/cases/libs/model/datasources/dbo/dbo_mysql.test.php @@ -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')); + } }