Author: Frederik Holljen
Date: 2006-01-13 17:45:23 +0100 (Fri, 13 Jan 2006)
New Revision: 1846
Log:
- exceptions update for database part deux.
Added:
packages/Database/trunk/src/exceptions/handler_not_found.php
packages/Database/trunk/src/exceptions/missing_parameter.php
packages/Database/trunk/src/exceptions/transaction.php
packages/Database/trunk/tests/handler_test.php
packages/Database/trunk/tests/instance_test.php
Modified:
packages/Database/trunk/ChangeLog
packages/Database/trunk/src/db_autoload.php
packages/Database/trunk/src/exceptions/exception.php
packages/Database/trunk/src/factory.php
packages/Database/trunk/src/handler.php
packages/Database/trunk/src/handlers/mysql.php
packages/Database/trunk/src/instance.php
packages/Database/trunk/tests/factory_test.php
packages/Database/trunk/tests/suite.php
packages/Database/trunk/tests/transactions_test.php
Modified: packages/Database/trunk/ChangeLog
===================================================================
--- packages/Database/trunk/ChangeLog 2006-01-13 16:20:32 UTC (rev 1845)
+++ packages/Database/trunk/ChangeLog 2006-01-13 16:45:23 UTC (rev 1846)
@@ -5,7 +5,8 @@
- Added support for aliases of column names
- Added SQLite driver
-
+- Removed destructor in ezcDbHandler. We should not throw exceptions that
can't be caught.
+
ezcDbFactory
============
Modified: packages/Database/trunk/src/db_autoload.php
===================================================================
--- packages/Database/trunk/src/db_autoload.php 2006-01-13 16:20:32 UTC (rev
1845)
+++ packages/Database/trunk/src/db_autoload.php 2006-01-13 16:45:23 UTC (rev
1846)
@@ -13,6 +13,9 @@
'ezcDbFactory' => 'Database/factory.php',
'ezcDbInstance' => 'Database/instance.php',
'ezcDbException' => 'Database/exceptions/exception.php',
+ 'ezcDbHandlerNotFoundException' =>
'Database/exceptions/handler_not_found.php',
+ 'ezcDbTransactionException' => 'Database/exceptions/transaction.php',
+ 'ezcDbMissingParameterException' =>
'Database/exceptions/missing_parameter.php',
'ezcDbHandler' => 'Database/handler.php',
'ezcDbHandlerMysql' => 'Database/handlers/mysql.php',
'ezcDbHandlerOracle' => 'Database/handlers/oracle.php',
Modified: packages/Database/trunk/src/exceptions/exception.php
===================================================================
--- packages/Database/trunk/src/exceptions/exception.php 2006-01-13
16:20:32 UTC (rev 1845)
+++ packages/Database/trunk/src/exceptions/exception.php 2006-01-13
16:45:23 UTC (rev 1846)
@@ -14,60 +14,9 @@
*
* @package Database
*/
-class ezcDbException extends Exception
+class ezcDbException extends ezcBaseException
{
/**
- * A custom error.
- *
- * You will probably want to specify your error message
- * when throwing exception of this type.
- * In this case use the second parameter for the constructor.
- */
- const GENERIC_ERROR = 0;
-
- /**
- * Missing database name
- */
- const MISSING_DATABASE_NAME = 1;
-
- /**
- * Missing user name
- */
- const MISSING_USER_NAME = 2;
-
- /**
- * Missing password
- */
- const MISSING_PASSWORD = 3;
-
- /**
- * Unknown database implementation.
- *
- * Specified database handler (driver) cannot be found.
- * Thrown from [EMAIL PROTECTED] ezcDbFactory::create()}
- */
- const UNKNOWN_IMPL = 4;
-
- /**
- * Thrown from [EMAIL PROTECTED] ezcDbInstance}
- */
- const INSTANCE_NOT_FOUND = 5;
-
- /**
- * Functionality is not implemented.
- */
- const NOT_IMPLEMENTED = 6;
-
- /**
- * Logical transaction error.
- *
- * Exception of this type is usually raised
- * when number of issued COMMITs/ROLLBACKs is not equal
- * to number of BEGINs.
- */
- const TRANSACTION_ERROR = 7;
-
- /**
* Constructs an ezcDbAstractionException with the highlevel error
* message $message and the errorcode $code.
*
@@ -75,7 +24,7 @@
* @param string $additionalInfo
* @return void
*/
- public function __construct( $code, $message = null )
+ public function __construct( $message )
{
if ( $message === null )
{
@@ -122,7 +71,7 @@
}
}
- parent::__construct( $message, $code );
+ parent::__construct( $message );
}
}
?>
Added: packages/Database/trunk/src/exceptions/handler_not_found.php
===================================================================
--- packages/Database/trunk/src/exceptions/handler_not_found.php
2006-01-13 16:20:32 UTC (rev 1845)
+++ packages/Database/trunk/src/exceptions/handler_not_found.php
2006-01-13 16:45:23 UTC (rev 1846)
@@ -0,0 +1,40 @@
+<?php
+/**
+ * File containing the ezcDbException class.
+ *
+ * @package Database
+ * @version //autogentag//
+ * @copyright Copyright (C) 2005, 2006 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ */
+
+/**
+ * This exceptions is used when a database handler could not be found.
+ *
+ * @package Database
+ */
+class ezcDbHandlerNotFoundException extends ezcDbException
+{
+ /**
+ * Constructs a new exception.
+ *
+ * $name specifies the name of the name of the handler to use.
+ * $known is a list of the known database handlers.
+ */
+ public function __construct( $name, array $known = array() )
+ {
+ if( $name == '' || $name == null )
+ {
+ $name = 'no name provided';
+ }
+ $message = "Could not find the database handler: {$name}.";
+
+ if( count( $known ) > 0 )
+ {
+ $knownMessage = ' The known databases are: ' . implode( ', ',
$known ) . '.';
+ $message .= $knownMessage;
+ }
+ parent::__construct( $message );
+ }
+}
+?>
Property changes on:
packages/Database/trunk/src/exceptions/handler_not_found.php
___________________________________________________________________
Name: svn:eol-style
+ native
Added: packages/Database/trunk/src/exceptions/missing_parameter.php
===================================================================
--- packages/Database/trunk/src/exceptions/missing_parameter.php
2006-01-13 16:20:32 UTC (rev 1845)
+++ packages/Database/trunk/src/exceptions/missing_parameter.php
2006-01-13 16:45:23 UTC (rev 1846)
@@ -0,0 +1,29 @@
+<?php
+/**
+ * File containing the ezcDbException class.
+ *
+ * @package Database
+ * @version //autogentag//
+ * @copyright Copyright (C) 2005, 2006 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ */
+
+/**
+ * This exception is thrown when a database handler misses a required
parameter.
+ *
+ * @package Database
+ */
+class ezcDbMissingParameterException extends ezcDbException
+{
+ /**
+ * Constructs a new exception.
+ *
+ * @param string $option
+ * @param string $variableName
+ */
+ public function __construct( $option, $variableName )
+ {
+ parent::__construct( "The parameter $option is required in the
parameter $variableName." );
+ }
+}
+?>
Property changes on:
packages/Database/trunk/src/exceptions/missing_parameter.php
___________________________________________________________________
Name: svn:eol-style
+ native
Added: packages/Database/trunk/src/exceptions/transaction.php
===================================================================
--- packages/Database/trunk/src/exceptions/transaction.php 2006-01-13
16:20:32 UTC (rev 1845)
+++ packages/Database/trunk/src/exceptions/transaction.php 2006-01-13
16:45:23 UTC (rev 1846)
@@ -0,0 +1,31 @@
+<?php
+/**
+ * File containing the ezcDbException class.
+ *
+ * @package Database
+ * @version //autogentag//
+ * @copyright Copyright (C) 2005, 2006 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ */
+
+/**
+ * This class provides exception for misc errors that may occur in the
component,
+ * such as errors parsing database parameters and connecting to the database.
+ *
+ * @package Database
+ */
+class ezcDbTransactionException extends ezcDbException
+{
+ /**
+ * Constructs a new exception.
+ *
+ * $name specifies the name of the name of the handler to use.
+ * $known is a list of the known database handlers.
+ */
+ public function __construct( $msg )
+ {
+ $message = 'There was a transaction error caused by unmatched
beginTransaction()/commit() calls: {$msg}';
+ parent::__construct( $message );
+ }
+}
+?>
Property changes on: packages/Database/trunk/src/exceptions/transaction.php
___________________________________________________________________
Name: svn:eol-style
+ native
Modified: packages/Database/trunk/src/factory.php
===================================================================
--- packages/Database/trunk/src/factory.php 2006-01-13 16:20:32 UTC (rev
1845)
+++ packages/Database/trunk/src/factory.php 2006-01-13 16:45:23 UTC (rev
1846)
@@ -22,7 +22,7 @@
* $db = ezcDbFactory::create( $dbparams );
* $db->query( 'SELECT * FROM tbl' );
* </code>
- *
+ *
* Instead of passing an array with those parameters, you can also pass a DSN:
*
* <code>
@@ -110,7 +110,7 @@
* The list above is actually driver-dependent and may be extended in the
future.
* You can specify any parameters your database handler supports.
*
- * @throws ezcDbException::UNKNOWN_IMPL if specified implementation
is not found.
+ * @throws ezcDbHandlerNotFoundException if the requested database handler
could not be found.
* @param mixed $dbParams Database parameters
* (driver, host, port, user, pass, etc).
* May be specified either as array (key => val ....) or
as DSN string.
@@ -135,16 +135,11 @@
}
}
- if ( $impName === null )
+ if ( $impName === null || !array_key_exists( $impName,
self::$implementations ) )
{
- throw new ezcDbException( ezcDbException::UNKNOWN_IMPL, "The
parameters did not specify an implementation with the <type> array element." );
+ throw new ezcDbHandlerNotFoundException( $impName, array_keys(
self::$implementations ) );
}
- if ( !array_key_exists( $impName, self::$implementations ) )
- {
- throw new ezcDbException( ezcDbException::UNKNOWN_IMPL, "The
implementation <{$impName}> does not exist." );
- }
-
$className = self::$implementations[$impName];
$instance = new $className( $dbParams );
Modified: packages/Database/trunk/src/handler.php
===================================================================
--- packages/Database/trunk/src/handler.php 2006-01-13 16:20:32 UTC (rev
1845)
+++ packages/Database/trunk/src/handler.php 2006-01-13 16:45:23 UTC (rev
1846)
@@ -77,22 +77,6 @@
}
/**
- * Destroys the handler object.
- *
- * @throws ezcDbException if transaction nesting errors are detected.
- */
- public function __destruct()
- {
- if ( $this->transactionNestingLevel != 0 )
- {
- throw new ezcDbException(
- ezcDbException::TRANSACTION_ERROR,
- 'Transaction inconsistency. Check that you issued
COMMIT/ROLLBACK exactly as many times as BEGIN.'
- );
- }
- }
-
- /**
* Returns the name of the handler.
*
* Returns handler name
@@ -177,7 +161,7 @@
{
$this->level = 0;
- throw new ezcDbException( ezcDbException::TRANSACTION_ERROR,
'Transaction inconsistency: COMMIT without previous BEGIN.' );
+ throw new ezcDbTransactionException( "commit() called before
beginTransaction()." );
}
$retval = true;
@@ -219,7 +203,7 @@
if ( $this->transactionNestingLevel <= 0 )
{
$this->transactionNestingLevel = 0;
- throw new ezcDbException( ezcDbException::TRANSACTION_ERROR,
'Transaction inconsistency: ROLLBACK without previous BEGIN.' );
+ throw new ezcDbTransactionException( "rollback() called without
previous beginTransaction()." );
}
if ( $this->transactionNestingLevel == 1 )
Modified: packages/Database/trunk/src/handlers/mysql.php
===================================================================
--- packages/Database/trunk/src/handlers/mysql.php 2006-01-13 16:20:32 UTC
(rev 1845)
+++ packages/Database/trunk/src/handlers/mysql.php 2006-01-13 16:45:23 UTC
(rev 1846)
@@ -69,7 +69,7 @@
if ( !isset( $database ) )
{
- throw new ezcDbException( ezcDbException::MISSING_DATABASE_NAME );
+ throw new ezcDbMissingParameterException( 'database', 'dbParams' );
}
$dsn = "mysql:dbname=$database";
Modified: packages/Database/trunk/src/instance.php
===================================================================
--- packages/Database/trunk/src/instance.php 2006-01-13 16:20:32 UTC (rev
1845)
+++ packages/Database/trunk/src/instance.php 2006-01-13 16:45:23 UTC (rev
1846)
@@ -76,7 +76,7 @@
* If $identifier is ommited the default database instance
* specified by chooseDefault() is returned.
*
- * @throws ezcDbException::INSTANCE_NOT_FOUND if the specified instance is
not found.
+ * @throws ezcDbHandlerNotFoundException if the specified instance is not
found.
* @param string $identifier
* @return ezcDbHandler
*/
@@ -89,7 +89,7 @@
if ( !isset( self::$Instances[$identifier] ) )
{
- throw new ezcDbException( ezcDbException::INSTANCE_NOT_FOUND );
+ throw new ezcDbHandlerNotFoundException( $identifier );
}
return self::$Instances[$identifier];
@@ -105,7 +105,7 @@
* @param string the identifier of the database handler
* @return void
*/
- public static function set( $db, $identifier = false )
+ public static function set( ezcDbHandler $db, $identifier = false )
{
self::$Instances[$identifier] = $db;
}
Modified: packages/Database/trunk/tests/factory_test.php
===================================================================
--- packages/Database/trunk/tests/factory_test.php 2006-01-13 16:20:32 UTC
(rev 1845)
+++ packages/Database/trunk/tests/factory_test.php 2006-01-13 16:45:23 UTC
(rev 1846)
@@ -23,10 +23,8 @@
$this->fail( "Expected exception was not thrown" );
}
- catch ( ezcDbException $e )
+ catch ( ezcDbHandlerNotFoundException $e )
{
- $this->assertEquals( ezcDbException::UNKNOWN_IMPL, $e->getCode() );
- $this->assertEquals( "The parameters did not specify an
implementation with the <type> array element.", $e->getMessage() );
}
}
@@ -39,10 +37,8 @@
$this->fail( "Expected exception was not thrown" );
}
- catch ( ezcDbException $e )
+ catch ( ezcDbHandlerNotFoundException $e )
{
- $this->assertEquals( ezcDbException::UNKNOWN_IMPL, $e->getCode() );
- $this->assertEquals( "The implementation <unknown> does not
exist.", $e->getMessage() );
}
}
Added: packages/Database/trunk/tests/handler_test.php
===================================================================
--- packages/Database/trunk/tests/handler_test.php 2006-01-13 16:20:32 UTC
(rev 1845)
+++ packages/Database/trunk/tests/handler_test.php 2006-01-13 16:45:23 UTC
(rev 1846)
@@ -0,0 +1,43 @@
+<?php
+/**
+ * @copyright Copyright (C) 2005, 2006 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ * @version //autogentag//
+ * @filesource
+ * @package Database
+ * @subpackage Tests
+ */
+
+/**
+ * Test the handler classes.
+ *
+ * @package Database
+ * @subpackage Tests
+ */
+class ezcDatabaseHandlerTest extends ezcTestCase
+{
+ public function setUp()
+ {
+ }
+
+ public function testConstructorNoDatabaseName()
+ {
+ try
+ {
+ // we'll create an instance of the correct type simply by making a
similar one to the default.
+ $db = ezcDbInstance::get();
+ $className = get_class( $db );
+ $db = new $className( array() );
+ $this->fail( "Instantiating a handler with no database name should
not be successful" );
+ }catch( ezcDbMissingParameterException $e )
+ {
+ }
+ }
+
+ public static function suite()
+ {
+ return new ezcTestSuite( "ezcDatabaseHandlerTest" );
+ }
+}
+
+?>
Property changes on: packages/Database/trunk/tests/handler_test.php
___________________________________________________________________
Name: svn:eol-style
+ native
Added: packages/Database/trunk/tests/instance_test.php
===================================================================
--- packages/Database/trunk/tests/instance_test.php 2006-01-13 16:20:32 UTC
(rev 1845)
+++ packages/Database/trunk/tests/instance_test.php 2006-01-13 16:45:23 UTC
(rev 1846)
@@ -0,0 +1,70 @@
+<?php
+/**
+ * @copyright Copyright (C) 2005, 2006 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ * @version //autogentag//
+ * @filesource
+ * @package Database
+ * @subpackage Tests
+ */
+
+/**
+ * Test the instance class
+ *
+ * @package Database
+ * @subpackage Tests
+ */
+class ezcDatabaseInstanceTest extends ezcTestCase
+{
+ public function setUp()
+ {
+ }
+
+ public function testGetDefaultValid()
+ {
+ $this->fail( "todo" );
+ }
+
+ public function testGetWithIdentifierValid()
+ {
+ $this->fail( "todo" );
+ }
+
+ public function testSet()
+ {
+ $this->fail( "todo" );
+ }
+
+ public function testChooseDefault()
+ {
+ $this->fail( "todo" );
+ }
+
+ public function testReset()
+ {
+ $this->fail( "todo" );
+ }
+
+ public function testResetDefault()
+ {
+ $this->fail( "todo" );
+ }
+
+
+ public function testWithIdentifierInvalid()
+ {
+ try
+ {
+ ezcDbInstance::get( "NoSuchInstance" );
+ $this->fail( "Getting a non existent instance did not fail." );
+ }
+ catch( ezcDbHandlerNotFoundException $e ){}
+ }
+
+ public static function suite()
+ {
+ return new ezcTestSuite( "ezcDatabaseInstanceTest" );
+ }
+}
+
+?>
Property changes on: packages/Database/trunk/tests/instance_test.php
___________________________________________________________________
Name: svn:eol-style
+ native
Modified: packages/Database/trunk/tests/suite.php
===================================================================
--- packages/Database/trunk/tests/suite.php 2006-01-13 16:20:32 UTC (rev
1845)
+++ packages/Database/trunk/tests/suite.php 2006-01-13 16:45:23 UTC (rev
1846)
@@ -13,6 +13,8 @@
*/
require_once 'factory_test.php';
require_once 'transactions_test.php';
+require_once 'instance_test.php';
+require_once 'handler_test.php';
require_once 'sqlabstraction/expression_test.php';
require_once 'sqlabstraction/query_test.php';
require_once 'sqlabstraction/query_select_test.php';
@@ -34,6 +36,8 @@
$this->setName( 'Database' );
$this->addTest( ezcDatabaseFactoryTest::suite() );
$this->addTest( ezcDatabaseTransactionsTest::suite() );
+ $this->addTest( ezcDatabaseInstanceTest::suite() );
+ $this->addTest( ezcDatabaseHandlerTest::suite() );
$this->addTest( ezcQueryExpressionTest::suite() );
$this->addTest( ezcQueryTest::suite() );
$this->addTest( ezcQuerySelectTest::suite() );
Modified: packages/Database/trunk/tests/transactions_test.php
===================================================================
--- packages/Database/trunk/tests/transactions_test.php 2006-01-13 16:20:32 UTC
(rev 1845)
+++ packages/Database/trunk/tests/transactions_test.php 2006-01-13 16:45:23 UTC
(rev 1846)
@@ -69,13 +69,12 @@
$db->commit();
unset( $db );
}
- catch ( ezcDbException $e )
+ catch ( ezcDbTransactionException $e )
{
$this->fail( "Exception (" . get_class( $e ) . ") caught: " .
$e->getMessage() );
}
}
- // error: more BEGINs than COMMITs
public function test2()
{
try
@@ -89,13 +88,11 @@
$db->commit();
unset( $db ); // destroy db connection
}
- catch ( ezcDbException $e )
+ catch ( Exception $e )
{
- $this->assertEquals( $e->getCode(),
ezcDbException::TRANSACTION_ERROR );
- return;
+ $this->fail( "Should not throw exception here since the action
doesn't have to be user initiated" );
}
- $this->fail( "The case when there were more BEGINs than COMMITs did
not fail.\n" );
}
// error: more COMMITs than BEGINs
@@ -112,9 +109,8 @@
$db->commit();
unset( $db ); // destroy db connection
}
- catch ( ezcDbException $e )
+ catch ( ezcDbTransactionException $e )
{
- $this->assertEquals( $e->getCode(),
ezcDbException::TRANSACTION_ERROR );
return;
}
@@ -168,9 +164,8 @@
$db->commit();
unset( $db );
}
- catch ( ezcDbException $e )
+ catch ( ezcDbTransactionException $e )
{
- $this->assertEquals( $e->getCode(),
ezcDbException::TRANSACTION_ERROR );
return;
}
@@ -188,9 +183,8 @@
$db->rollback();
unset( $db );
}
- catch ( ezcDbException $e )
+ catch ( ezcDbTransactionException $e )
{
- $this->assertEquals( $e->getCode(),
ezcDbException::TRANSACTION_ERROR );
return;
}
--
svn-components mailing list
[email protected]
http://lists.ez.no/mailman/listinfo/svn-components