mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-03-12 20:49:50 +00:00
Inital work for connecting to mysql using PDO and gettng the table list, testing is easier as it now uses mocks
This commit is contained in:
parent
efbefeb71a
commit
b8479459d6
2 changed files with 64 additions and 20 deletions
|
@ -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 @@ class DboMysql extends DboMysqlBase {
|
|||
* Executes given SQL statement.
|
||||
*
|
||||
* @param string $sql SQL statement
|
||||
* @return resource Result resource identifier
|
||||
* @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) {
|
||||
return mysql_query($sql, $this->connection);
|
||||
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 @@ class DboMysql extends DboMysqlBase {
|
|||
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 @@ class MysqlTestModel extends Model {
|
|||
* @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 @@ class DboMysqlTest extends CakeTestCase {
|
|||
$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'));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue