Commit 89592347 authored by Benjamin Eberlei's avatar Benjamin Eberlei

Cleanup Sqlite support, testcase and fix a bug.

parent 92073ad3
......@@ -105,6 +105,19 @@ class DBALException extends \Exception
return new self($msg, $driver->convertExceptionCode($driverEx), $driverEx);
}
/**
* @param \Doctrine\DBAL\Driver $driver
* @param \Exception $driverEx
*
* @return \Doctrine\DBAL\DBALException
*/
public static function driverException(Driver $driver, \Exception $driverEx)
{
$msg = "An exception occured in driver: " . $driverEx->getMessage();
return new self($msg, $driver->convertExceptionCode($driverEx), $driverEx);
}
/**
* Returns a human-readable representation of an array of parameters.
* This properly handles binary data by returning a hex representation.
......
......@@ -18,7 +18,9 @@
*/
namespace Doctrine\DBAL\Driver\PDOSqlite;
use Doctrine\DBAL\DBALException;
use PDOException;
/**
* The PDO Sqlite driver.
......@@ -47,12 +49,16 @@ class Driver implements \Doctrine\DBAL\Driver
unset($driverOptions['userDefinedFunctions']);
}
$pdo = new \Doctrine\DBAL\Driver\PDOConnection(
$this->_constructPdoDsn($params),
$username,
$password,
$driverOptions
);
try {
$pdo = new \Doctrine\DBAL\Driver\PDOConnection(
$this->_constructPdoDsn($params),
$username,
$password,
$driverOptions
);
} catch (PDOException $ex) {
throw DBALException::driverException($this, $ex);
}
foreach ($this->_userDefinedFunctions as $fn => $data) {
$pdo->sqliteCreateFunction($fn, $data['callback'], $data['numArgs']);
......@@ -119,47 +125,44 @@ class Driver implements \Doctrine\DBAL\Driver
*/
public function convertExceptionCode(\Exception $exception)
{
switch ($exception->getCode()) {
case 23000:
if (strpos($exception->getMessage(), 'must be unique') !== false) {
return DBALException::ERROR_DUPLICATE_KEY;
}
if (strpos($exception->getMessage(), 'may not be NULL') !== false) {
return DBALException::ERROR_NOT_NULL;
}
if (strpos($exception->getMessage(), 'is not unique') !== false) {
return DBALException::ERROR_NOT_UNIQUE;
}
case 'HY000':
if (strpos($exception->getMessage(), 'no such table:') !== false) {
return DBALException::ERROR_UNKNOWN_TABLE;
}
if (strpos($exception->getMessage(), 'already exists') !== false) {
return DBALException::ERROR_TABLE_ALREADY_EXISTS;
}
if (strpos($exception->getMessage(), 'has no column named') !== false) {
return DBALException::ERROR_BAD_FIELD_NAME;
}
if (strpos($exception->getMessage(), 'ambiguous column name') !== false) {
return DBALException::ERROR_NON_UNIQUE_FIELD_NAME;
}
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;
}
if (strpos($exception->getMessage(), 'must be unique') !== false) {
return DBALException::ERROR_DUPLICATE_KEY;
}
if (strpos($exception->getMessage(), 'may not be NULL') !== false) {
return DBALException::ERROR_NOT_NULL;
}
if (strpos($exception->getMessage(), 'is not unique') !== false) {
return DBALException::ERROR_NOT_UNIQUE;
}
if (strpos($exception->getMessage(), 'no such table:') !== false) {
return DBALException::ERROR_UNKNOWN_TABLE;
}
if (strpos($exception->getMessage(), 'already exists') !== false) {
return DBALException::ERROR_TABLE_ALREADY_EXISTS;
}
if (strpos($exception->getMessage(), 'has no column named') !== false) {
return DBALException::ERROR_BAD_FIELD_NAME;
}
if (strpos($exception->getMessage(), 'ambiguous column name') !== false) {
return DBALException::ERROR_NON_UNIQUE_FIELD_NAME;
}
if (strpos($exception->getMessage(), 'syntax error') !== false) {
return DBALException::ERROR_SYNTAX;
}
if (strpos($exception->getMessage(), 'attempt to write a readonly database') !== false) {
return DBALException::ERROR_WRITE_READONLY;
}
if (strpos($exception->getMessage(), 'unable to open database file') !== false) {
return DBALException::ERROR_UNABLE_TO_OPEN;
}
return 0;
......
......@@ -51,7 +51,7 @@ class ExceptionTest extends \Doctrine\Tests\DbalFunctionalTestCase
}
$schema = new \Doctrine\DBAL\Schema\Schema();
$table = $schema->createTable("constraint_error_table");
$table->addColumn('id', 'integer', array());
$table->setPrimaryKey(array('id'));
......@@ -72,7 +72,7 @@ class ExceptionTest extends \Doctrine\Tests\DbalFunctionalTestCase
$this->setExpectedException('\Doctrine\DBAL\DBALException', null, DBALException::ERROR_FOREIGN_KEY_CONSTRAINT);
$this->_conn->delete('constraint_error_table', array('id' => 1));
}
public function testNotNullException()
{
$schema = new \Doctrine\DBAL\Schema\Schema();
......@@ -168,16 +168,16 @@ class ExceptionTest extends \Doctrine\Tests\DbalFunctionalTestCase
$filename = sprintf('%s/%s', sys_get_temp_dir(), 'doctrine_failed_connection.db');
if (file_exists($filename)) {
unlink($filename);
}
unlink($filename);
}
touch($filename);
chmod($filename, $mode);
$params = array(
'driver' => 'pdo_sqlite',
'path' => $filename,
);
'driver' => 'pdo_sqlite',
'path' => $filename,
);
$conn = \Doctrine\DBAL\DriverManager::getConnection($params);
$schema = new \Doctrine\DBAL\Schema\Schema();
......@@ -186,15 +186,16 @@ class ExceptionTest extends \Doctrine\Tests\DbalFunctionalTestCase
$this->setExpectedException('\Doctrine\DBAL\DBALException', null, $exceptionCode);
foreach ($schema->toSql($conn->getDatabasePlatform()) AS $sql) {
$conn->executeQuery($sql);
}
$conn->executeQuery($sql);
}
}
public function getSqLiteOpenConnection(){
return array(
array(0000, DBALException::ERROR_UNABLE_TO_OPEN),
array(0444, DBALException::ERROR_WRITE_READONLY),
);
public function getSqLiteOpenConnection()
{
return array(
array(0000, DBALException::ERROR_UNABLE_TO_OPEN),
array(0444, DBALException::ERROR_WRITE_READONLY),
);
}
/**
......@@ -216,27 +217,19 @@ class ExceptionTest extends \Doctrine\Tests\DbalFunctionalTestCase
$table->addColumn('id', 'integer');
$this->setExpectedException('\Doctrine\DBAL\DBALException', null, $exceptionCode);
foreach ($schema->toSql($conn->getDatabasePlatform()) AS $sql) {
$conn->executeQuery($sql);
}
$conn->executeQuery($sql);
}
}
public function getConnectionParams()
{
return array(
array(array('user' => 'not_existing'), DBALException::ERROR_ACCESS_DENIED),
array(array('password' => 'really_not'), DBALException::ERROR_ACCESS_DENIED),
array(array('host' => 'localnope'), DBALException::ERROR_ACCESS_DENIED),
);
}
protected function onNotSuccessfulTest(\Exception $e)
{
parent::onNotSuccessfulTest($e);
if ("PHPUnit_Framework_SkippedTestError" == get_class($e)) {
return;
}
var_dump($e);
return array(
array(array('user' => 'not_existing'), DBALException::ERROR_ACCESS_DENIED),
array(array('password' => 'really_not'), DBALException::ERROR_ACCESS_DENIED),
array(array('host' => 'localnope'), DBALException::ERROR_ACCESS_DENIED),
);
}
}
\ 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