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']);
} }
$pdo = new \Doctrine\DBAL\Driver\PDOConnection( try {
$this->_constructPdoDsn($params), $pdo = new \Doctrine\DBAL\Driver\PDOConnection(
$username, $this->_constructPdoDsn($params),
$password, $username,
$driverOptions $password,
); $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,47 +125,44 @@ class Driver implements \Doctrine\DBAL\Driver ...@@ -119,47 +125,44 @@ class Driver implements \Doctrine\DBAL\Driver
*/ */
public function convertExceptionCode(\Exception $exception) public function convertExceptionCode(\Exception $exception)
{ {
switch ($exception->getCode()) { if (strpos($exception->getMessage(), 'must be unique') !== false) {
case 23000: return DBALException::ERROR_DUPLICATE_KEY;
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(), '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(), 'is not unique') !== false) { }
return DBALException::ERROR_NOT_UNIQUE;
} if (strpos($exception->getMessage(), 'no such table:') !== false) {
case 'HY000': return DBALException::ERROR_UNKNOWN_TABLE;
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(), '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(), '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(), '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(), '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;
} 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; return 0;
......
...@@ -51,7 +51,7 @@ class ExceptionTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -51,7 +51,7 @@ class ExceptionTest extends \Doctrine\Tests\DbalFunctionalTestCase
} }
$schema = new \Doctrine\DBAL\Schema\Schema(); $schema = new \Doctrine\DBAL\Schema\Schema();
$table = $schema->createTable("constraint_error_table"); $table = $schema->createTable("constraint_error_table");
$table->addColumn('id', 'integer', array()); $table->addColumn('id', 'integer', array());
$table->setPrimaryKey(array('id')); $table->setPrimaryKey(array('id'));
...@@ -72,7 +72,7 @@ class ExceptionTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -72,7 +72,7 @@ class ExceptionTest extends \Doctrine\Tests\DbalFunctionalTestCase
$this->setExpectedException('\Doctrine\DBAL\DBALException', null, DBALException::ERROR_FOREIGN_KEY_CONSTRAINT); $this->setExpectedException('\Doctrine\DBAL\DBALException', null, DBALException::ERROR_FOREIGN_KEY_CONSTRAINT);
$this->_conn->delete('constraint_error_table', array('id' => 1)); $this->_conn->delete('constraint_error_table', array('id' => 1));
} }
public function testNotNullException() public function testNotNullException()
{ {
$schema = new \Doctrine\DBAL\Schema\Schema(); $schema = new \Doctrine\DBAL\Schema\Schema();
...@@ -168,16 +168,16 @@ class ExceptionTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -168,16 +168,16 @@ class ExceptionTest extends \Doctrine\Tests\DbalFunctionalTestCase
$filename = sprintf('%s/%s', sys_get_temp_dir(), 'doctrine_failed_connection.db'); $filename = sprintf('%s/%s', sys_get_temp_dir(), 'doctrine_failed_connection.db');
if (file_exists($filename)) { if (file_exists($filename)) {
unlink($filename); unlink($filename);
} }
touch($filename); touch($filename);
chmod($filename, $mode); chmod($filename, $mode);
$params = array( $params = array(
'driver' => 'pdo_sqlite', 'driver' => 'pdo_sqlite',
'path' => $filename, 'path' => $filename,
); );
$conn = \Doctrine\DBAL\DriverManager::getConnection($params); $conn = \Doctrine\DBAL\DriverManager::getConnection($params);
$schema = new \Doctrine\DBAL\Schema\Schema(); $schema = new \Doctrine\DBAL\Schema\Schema();
...@@ -186,15 +186,16 @@ class ExceptionTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -186,15 +186,16 @@ class ExceptionTest extends \Doctrine\Tests\DbalFunctionalTestCase
$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);
} }
} }
public function getSqLiteOpenConnection(){ public function getSqLiteOpenConnection()
return array( {
array(0000, DBALException::ERROR_UNABLE_TO_OPEN), return array(
array(0444, DBALException::ERROR_WRITE_READONLY), array(0000, DBALException::ERROR_UNABLE_TO_OPEN),
); array(0444, DBALException::ERROR_WRITE_READONLY),
);
} }
/** /**
...@@ -216,27 +217,19 @@ class ExceptionTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -216,27 +217,19 @@ 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);
} }
} }
public function getConnectionParams() public function getConnectionParams()
{ {
return array( return array(
array(array('user' => 'not_existing'), DBALException::ERROR_ACCESS_DENIED), array(array('user' => 'not_existing'), DBALException::ERROR_ACCESS_DENIED),
array(array('password' => 'really_not'), DBALException::ERROR_ACCESS_DENIED), array(array('password' => 'really_not'), DBALException::ERROR_ACCESS_DENIED),
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);
} }
} }
\ 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