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 ...@@ -105,6 +105,19 @@ class DBALException extends \Exception
return new self($msg, $driver->convertExceptionCode($driverEx), $driverEx); 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. * Returns a human-readable representation of an array of parameters.
* This properly handles binary data by returning a hex representation. * This properly handles binary data by returning a hex representation.
......
...@@ -18,7 +18,9 @@ ...@@ -18,7 +18,9 @@
*/ */
namespace Doctrine\DBAL\Driver\PDOSqlite; namespace Doctrine\DBAL\Driver\PDOSqlite;
use Doctrine\DBAL\DBALException; use Doctrine\DBAL\DBALException;
use PDOException;
/** /**
* The PDO Sqlite driver. * The PDO Sqlite driver.
...@@ -47,12 +49,16 @@ class Driver implements \Doctrine\DBAL\Driver ...@@ -47,12 +49,16 @@ class Driver implements \Doctrine\DBAL\Driver
unset($driverOptions['userDefinedFunctions']); unset($driverOptions['userDefinedFunctions']);
} }
try {
$pdo = new \Doctrine\DBAL\Driver\PDOConnection( $pdo = new \Doctrine\DBAL\Driver\PDOConnection(
$this->_constructPdoDsn($params), $this->_constructPdoDsn($params),
$username, $username,
$password, $password,
$driverOptions $driverOptions
); );
} catch (PDOException $ex) {
throw DBALException::driverException($this, $ex);
}
foreach ($this->_userDefinedFunctions as $fn => $data) { foreach ($this->_userDefinedFunctions as $fn => $data) {
$pdo->sqliteCreateFunction($fn, $data['callback'], $data['numArgs']); $pdo->sqliteCreateFunction($fn, $data['callback'], $data['numArgs']);
...@@ -119,8 +125,6 @@ class Driver implements \Doctrine\DBAL\Driver ...@@ -119,8 +125,6 @@ class Driver implements \Doctrine\DBAL\Driver
*/ */
public function convertExceptionCode(\Exception $exception) public function convertExceptionCode(\Exception $exception)
{ {
switch ($exception->getCode()) {
case 23000:
if (strpos($exception->getMessage(), 'must be unique') !== false) { if (strpos($exception->getMessage(), 'must be unique') !== false) {
return DBALException::ERROR_DUPLICATE_KEY; return DBALException::ERROR_DUPLICATE_KEY;
} }
...@@ -132,7 +136,7 @@ class Driver implements \Doctrine\DBAL\Driver ...@@ -132,7 +136,7 @@ class Driver implements \Doctrine\DBAL\Driver
if (strpos($exception->getMessage(), 'is not unique') !== false) { if (strpos($exception->getMessage(), 'is not unique') !== false) {
return DBALException::ERROR_NOT_UNIQUE; return DBALException::ERROR_NOT_UNIQUE;
} }
case 'HY000':
if (strpos($exception->getMessage(), 'no such table:') !== false) { if (strpos($exception->getMessage(), 'no such table:') !== false) {
return DBALException::ERROR_UNKNOWN_TABLE; return DBALException::ERROR_UNKNOWN_TABLE;
} }
...@@ -153,13 +157,12 @@ class Driver implements \Doctrine\DBAL\Driver ...@@ -153,13 +157,12 @@ class Driver implements \Doctrine\DBAL\Driver
return DBALException::ERROR_SYNTAX; 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) { if (strpos($exception->getMessage(), 'attempt to write a readonly database') !== false) {
return DBALException::ERROR_WRITE_READONLY; return DBALException::ERROR_WRITE_READONLY;
} }
if (strpos($exception->getMessage(), 'unable to open database file') !== false) {
return DBALException::ERROR_UNABLE_TO_OPEN;
} }
return 0; return 0;
......
...@@ -190,7 +190,8 @@ class ExceptionTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -190,7 +190,8 @@ class ExceptionTest extends \Doctrine\Tests\DbalFunctionalTestCase
} }
} }
public function getSqLiteOpenConnection(){ public function getSqLiteOpenConnection()
{
return array( return array(
array(0000, DBALException::ERROR_UNABLE_TO_OPEN), array(0000, DBALException::ERROR_UNABLE_TO_OPEN),
array(0444, DBALException::ERROR_WRITE_READONLY), array(0444, DBALException::ERROR_WRITE_READONLY),
...@@ -216,6 +217,7 @@ class ExceptionTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -216,6 +217,7 @@ class ExceptionTest extends \Doctrine\Tests\DbalFunctionalTestCase
$table->addColumn('id', 'integer'); $table->addColumn('id', 'integer');
$this->setExpectedException('\Doctrine\DBAL\DBALException', null, $exceptionCode); $this->setExpectedException('\Doctrine\DBAL\DBALException', null, $exceptionCode);
foreach ($schema->toSql($conn->getDatabasePlatform()) AS $sql) { foreach ($schema->toSql($conn->getDatabasePlatform()) AS $sql) {
$conn->executeQuery($sql); $conn->executeQuery($sql);
} }
...@@ -229,14 +231,5 @@ class ExceptionTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -229,14 +231,5 @@ class ExceptionTest extends \Doctrine\Tests\DbalFunctionalTestCase
array(array('host' => 'localnope'), 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);
}
} }
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