fix fullTableName duplicate prefix, closes #2355

This commit is contained in:
Rachman Chavik 2011-12-14 14:35:20 +07:00
parent d2eac08e6b
commit a8d0447e61
3 changed files with 68 additions and 1 deletions

View file

@ -920,7 +920,7 @@ class DboSource extends DataSource {
if (is_object($model)) {
$schemaName = $model->schemaName;
$table = $model->tablePrefix . $model->table;
} elseif (isset($this->config['prefix'])) {
} elseif (!empty($this->config['prefix']) && strpos($model, $this->config['prefix']) === false) {
$table = $this->config['prefix'] . strval($model);
} else {
$table = strval($model);

View file

@ -3540,4 +3540,37 @@ class MysqlTest extends CakeTestCase {
$this->Dbo->delete($Article, true);
$this->Dbo->delete($Article, '2=2');
}
/**
* Test truncate with a mock.
*
* @return void
*/
public function testTruncateStatements() {
$this->loadFixtures('Article', 'User');
$db = ConnectionManager::getDatasource('test');
$schema = $db->config['database'];
$Article = new Article();
$this->Dbo = $this->getMock('Mysql', array('execute'), array($db->config));
$this->Dbo->expects($this->at(0))->method('execute')
->with("TRUNCATE TABLE `$schema`.`articles`");
$this->Dbo->truncate($Article);
$this->Dbo->expects($this->at(0))->method('execute')
->with("TRUNCATE TABLE `$schema`.`articles`");
$this->Dbo->truncate('articles');
// #2355: prevent duplicate prefix
$this->Dbo->config['prefix'] = 'tbl_';
$Article->tablePrefix = 'tbl_';
$this->Dbo->expects($this->at(0))->method('execute')
->with("TRUNCATE TABLE `$schema`.`tbl_articles`");
$this->Dbo->truncate($Article);
$this->Dbo->expects($this->at(0))->method('execute')
->with("TRUNCATE TABLE `$schema`.`tbl_articles`");
$this->Dbo->truncate('articles');
}
}

View file

@ -871,4 +871,38 @@ class PostgresTest extends CakeTestCase {
$result = $this->Dbo->getEncoding();
$this->assertEquals('EUC-JP', $result) ;
}
/**
* Test truncate with a mock.
*
* @return void
*/
public function testTruncateStatements() {
$this->loadFixtures('Article', 'User');
$db = ConnectionManager::getDatasource('test');
$schema = $db->config['schema'];
$Article = new Article();
$this->Dbo = $this->getMock('Postgres', array('execute'), array($db->config));
$this->Dbo->expects($this->at(0))->method('execute')
->with("DELETE FROM \"$schema\".\"articles\"");
$this->Dbo->truncate($Article);
$this->Dbo->expects($this->at(0))->method('execute')
->with("DELETE FROM \"$schema\".\"articles\"");
$this->Dbo->truncate('articles');
// #2355: prevent duplicate prefix
$this->Dbo->config['prefix'] = 'tbl_';
$Article->tablePrefix = 'tbl_';
$this->Dbo->expects($this->at(0))->method('execute')
->with("DELETE FROM \"$schema\".\"tbl_articles\"");
$this->Dbo->truncate($Article);
$this->Dbo->expects($this->at(0))->method('execute')
->with("DELETE FROM \"$schema\".\"tbl_articles\"");
$this->Dbo->truncate('articles');
}
}