Commit 1d9dec7b authored by dazz's avatar dazz Committed by Benjamin Eberlei

[DBAL-407] Implement and detect SqLite connection exceptions

parent 3e580007
......@@ -30,6 +30,8 @@ class DBALException extends \Exception
const ERROR_NON_UNIQUE_FIELD_NAME = 7;
const ERROR_NOT_UNIQUE = 8;
const ERROR_SYNTAX = 9;
const ERROR_UNABLE_TO_OPEN = 10;
const ERROR_WRITE_READONLY = 11;
/**
* @param string $method
......
......@@ -152,6 +152,14 @@ class Driver implements \Doctrine\DBAL\Driver
if (strpos($exception->getMessage(), 'syntax error') !== false) {
return DBALException::ERROR_SYNTAX;
}
if (strpos($exception->getMessage(), 'unable to open database file') !== false) {
return DBALException::ERROR_UNABLE_TO_OPEN;
}
if (strpos($exception->getMessage(), 'attempt to write a readonly database') !== false) {
return DBALException::ERROR_WRITE_READONLY;
}
}
return 0;
......
......@@ -164,5 +164,47 @@ class ExceptionTest extends \Doctrine\Tests\DbalFunctionalTestCase
}
var_dump($e);
}
/**
* @dataProvider getSqLiteOpenConnection
*/
public function testConnectionExceptionSqLite($mode, $exceptionCode)
{
if ($this->_conn->getDatabasePlatform()->getName() != 'sqlite') {
$this->markTestSkipped("Only fails this way on sqlite");
}
$filename = sprintf('%s/%s', sys_get_temp_dir(), 'doctrine_failed_connection.db');
if (file_exists($filename)) {
unlink($filename);
}
touch($filename);
chmod($filename, $mode);
$params = array(
'driver' => 'pdo_sqlite',
'path' => $filename,
);
$conn = \Doctrine\DBAL\DriverManager::getConnection($params);
$schema = new \Doctrine\DBAL\Schema\Schema();
$table = $schema->createTable("no_connection");
$table->addColumn('id', 'integer');
$this->setExpectedException('\Doctrine\DBAL\DBALException', null, $exceptionCode);
foreach ($schema->toSql($conn->getDatabasePlatform()) AS $sql) {
$conn->executeQuery($sql);
}
}
public function getSqLiteOpenConnection(){
return array(
array(0000, DBALException::ERROR_UNABLE_TO_OPEN),
array(0444, DBALException::ERROR_WRITE_READONLY),
);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment