Author: Frederik Holljen
Date: 2006-01-13 14:03:10 +0100 (Fri, 13 Jan 2006)
New Revision: 1824
Log:
- Database exceptions cleanup part 1
Removed:
packages/Database/trunk/src/exceptions/abstraction_exception.php
Modified:
packages/Database/trunk/src/db_autoload.php
packages/Database/trunk/src/exceptions/query_exception.php
packages/Database/trunk/src/sqlabstraction/query_delete.php
packages/Database/trunk/src/sqlabstraction/query_insert.php
packages/Database/trunk/src/sqlabstraction/query_select.php
packages/Database/trunk/src/sqlabstraction/query_update.php
packages/Database/trunk/tests/sqlabstraction/query_delete_test.php
packages/Database/trunk/tests/sqlabstraction/query_update_test.php
Modified: packages/Database/trunk/src/db_autoload.php
===================================================================
--- packages/Database/trunk/src/db_autoload.php 2006-01-13 12:39:30 UTC (rev
1823)
+++ packages/Database/trunk/src/db_autoload.php 2006-01-13 13:03:10 UTC (rev
1824)
@@ -13,7 +13,6 @@
'ezcDbFactory' => 'Database/factory.php',
'ezcDbInstance' => 'Database/instance.php',
'ezcDbException' => 'Database/exceptions/exception.php',
- 'ezcDbAbstractionException' =>
'Database/exceptions/abstraction_exception.php',
'ezcDbHandler' => 'Database/handler.php',
'ezcDbHandlerMysql' => 'Database/handlers/mysql.php',
'ezcDbHandlerOracle' => 'Database/handlers/oracle.php',
Deleted: packages/Database/trunk/src/exceptions/abstraction_exception.php
===================================================================
--- packages/Database/trunk/src/exceptions/abstraction_exception.php
2006-01-13 12:39:30 UTC (rev 1823)
+++ packages/Database/trunk/src/exceptions/abstraction_exception.php
2006-01-13 13:03:10 UTC (rev 1824)
@@ -1,92 +0,0 @@
-<?php
-/**
- * File containing the ezcDbAbstractionException 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 ezcDbAbstractionException extends Exception
-{
- /**
- * 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;
-
- /**
- * Functionality is not implemented.
- */
- const NOT_IMPLEMENTED = 1;
-
- /**
- * Invalid argument passed to a method.
- */
- const INVALID_ARGUMENT = 2;
-
- /**
- * Invalid argument passed to a method.
- */
- const INVALID_ARGUMENT_NUM = 3;
-
- /**
- * Invalid call to method
- */
- const INVALID_CALL = 4;
-
-
- /**
- * Constructs an ezcDbAstractionException with the highlevel error
- * message $message and the lowlevel errormessage $additionalInfo.
- *
- * @param string $message
- * @param string $additionalInfo
- * @return void
- */
- public function __construct( $code, $message = null )
- {
- if ( $message === null )
- {
- // If message is not specified, let's set the default one.
- switch ( $code )
- {
- case self::NOT_IMPLEMENTED:
- $message = 'Functionality is not implemented. This
probably means that you should override the method that threw the exception.';
- break;
-
- case self::INVALID_ARGUMENT:
- $message = 'Invalid argument.';
- break;
-
- case self::INVALID_ARGUMENT_NUM:
- $message = 'Method called with an invalid number of
arguments.';
- break;
-
-
- case self::GENERIC_ERROR:
- $message = 'Unknown error occured.';
- break;
-
- case self::INVALID_CALL:
- $message = 'Invalid method call.';
-
- default:
- $message = '';
- }
- }
-
- parent::__construct( $message, $code );
- }
-}
-?>
Modified: packages/Database/trunk/src/exceptions/query_exception.php
===================================================================
--- packages/Database/trunk/src/exceptions/query_exception.php 2006-01-13
12:39:30 UTC (rev 1823)
+++ packages/Database/trunk/src/exceptions/query_exception.php 2006-01-13
13:03:10 UTC (rev 1824)
@@ -9,60 +9,23 @@
*/
/**
- * Exceptions related to the SQL abstraction library.
+ * Base class for exceptions related to the SQL abstraction.
*
* @package Database
*/
-class ezcQueryException extends Exception
+class ezcQueryException 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;
/**
- * Thrown when building a SELECT query fails.
- */
- const SELECT_BUILD_FAILED = 10;
- const SELECT_INVALID_CALL = 11;
- const SELECT_INVALID_ARGUMENT_NUM = 12;
-
- /**
- * Thrown when building an INSERT query fails.
- */
- const INSERT_BUILD_FAILED = 20;
-
- /**
* Constructs an ezcQueryException with the highlevel error
* message $message and the errorcode $code.
*
* @param string $message
* @param string $additionalInfo
- * @return void
*/
- public function __construct( $code, $message = null )
+ public function __construct( $message )
{
- if ( $message === null )
- {
- // If message is not specified, let's set the default one.
- switch ( $code )
- {
- case self::SELECT_INVALID_CALL:
- $message = 'Invalid second call to method. This method can
be called only once.';
- break;
- case self::SELECT_INVALID_ARGUMENT_NUM:
- $message = 'You must pass at least one argument to this
method.';
- break;
- case self::GENERIC_ERROR:
- default:
- $message = 'Unknown error occured.';
- }
- }
- parent::__construct( $message, $code );
+ parent::__construct( $message );
}
}
?>
Modified: packages/Database/trunk/src/sqlabstraction/query_delete.php
===================================================================
--- packages/Database/trunk/src/sqlabstraction/query_delete.php 2006-01-13
12:39:30 UTC (rev 1823)
+++ packages/Database/trunk/src/sqlabstraction/query_delete.php 2006-01-13
13:03:10 UTC (rev 1824)
@@ -97,16 +97,16 @@
*/
public function where()
{
- if ( $this->whereString != '' )
+ if ( $this->whereString != null )
{
- throw new ezcQueryException(
ezcQueryException::SELECT_INVALID_CALL );
+ throw new ezcQueryException( "double call to where()" );
}
$args = func_get_args();
$expressions = self::arrayFlatten( $args );
if ( count( $expressions ) < 1 )
{
- throw new ezcQueryException(
ezcQueryException::SELECT_INVALID_ARGUMENT_NUM );
+ throw new ezcQueryException( "where called without arguments" );
}
$this->whereString = 'WHERE ' . join( ' AND ', $expressions );
@@ -125,8 +125,7 @@
{
if ( $this->table == null )
{
- throw new ezcQueryException(
ezcQueryException::INSERT_BUILD_FAILED,
- "You must call deleteFrom() when
executing a delete query." );
+ throw new ezcQueryException( "You must call deleteFrom() when
executing a delete query." );
}
$query = "DELETE FROM {$this->table}";
Modified: packages/Database/trunk/src/sqlabstraction/query_insert.php
===================================================================
--- packages/Database/trunk/src/sqlabstraction/query_insert.php 2006-01-13
12:39:30 UTC (rev 1823)
+++ packages/Database/trunk/src/sqlabstraction/query_insert.php 2006-01-13
13:03:10 UTC (rev 1824)
@@ -107,8 +107,7 @@
if ( $this->table == null || empty( $this->values ) )
{
$problem = $this->table == null ? 'table' : 'values';
- throw new ezcQueryException(
ezcQueryException::INSERT_BUILD_FAILED,
- "No " . $problem . " set." );
+ throw new ezcQueryException( "No " . $problem . " set." );
}
$query = "INSERT INTO {$this->table}";
$columns = implode( ', ', array_keys( $this->values ) );
Modified: packages/Database/trunk/src/sqlabstraction/query_select.php
===================================================================
--- packages/Database/trunk/src/sqlabstraction/query_select.php 2006-01-13
12:39:30 UTC (rev 1823)
+++ packages/Database/trunk/src/sqlabstraction/query_select.php 2006-01-13
13:03:10 UTC (rev 1824)
@@ -153,9 +153,9 @@
*/
public function select()
{
- if ( $this->selectString != '' )
+ if ( $this->selectString != null )
{
- throw new ezcQueryException(
ezcQueryException::SELECT_INVALID_CALL );
+ throw new ezcQueryException( "Select already set" );
}
$args = func_get_args();
@@ -163,7 +163,7 @@
if ( count( $cols ) < 1 )
{
- throw new ezcQueryException(
ezcQueryException::SELECT_INVALID_ARGUMENT_NUM );
+ throw new ezcQueryException( "Must select from at least one
column" );
}
$cols = $this->getIdentifiers( $cols );
@@ -216,14 +216,14 @@
{
if ( $this->fromString != '' )
{
- throw new ezcQueryException(
ezcQueryException::SELECT_INVALID_CALL );
+ throw new ezcQueryException( "From already set" );
}
$args = func_get_args();
$tables = self::arrayFlatten( $args );
if ( count( $tables ) < 1 )
{
- throw new ezcQueryException(
ezcQueryException::INVALID_ARGUMENT_NUM );
+ throw new ezcQueryException( "Must select from at least one table"
);
}
$tables = $this->getIdentifiers( $tables );
@@ -327,9 +327,9 @@
*/
public function where()
{
- if ( $this->whereString != '' )
+ if ( $this->whereString != null )
{
- throw new ezcQueryException(
ezcQueryException::SELECT_INVALID_CALL );
+ throw new ezcQueryException( "where must have at least one logical
expression" );
}
$args = func_get_args();
Modified: packages/Database/trunk/src/sqlabstraction/query_update.php
===================================================================
--- packages/Database/trunk/src/sqlabstraction/query_update.php 2006-01-13
12:39:30 UTC (rev 1823)
+++ packages/Database/trunk/src/sqlabstraction/query_update.php 2006-01-13
13:03:10 UTC (rev 1824)
@@ -125,16 +125,16 @@
*/
public function where()
{
- if ( $this->whereString != '' )
+ if ( $this->whereString != null )
{
- throw new ezcQueryException(
ezcQueryException::SELECT_INVALID_CALL );
+ throw new ezcQueryException( "Double calls to where() are not
allowed" );
}
$args = func_get_args();
$expressions = self::arrayFlatten( $args );
if ( count( $expressions ) < 1 )
{
- throw new ezcQueryException(
ezcQueryException::SELECT_INVALID_ARGUMENT_NUM );
+ throw new ezcQueryException( "Need at least one parameter" );
}
$this->whereString = 'WHERE ' . join( ' AND ', $expressions );
@@ -154,8 +154,7 @@
if ( $this->table == null || empty( $this->values ) )
{
$problem = $this->table == null ? 'table' : 'values';
- throw new ezcQueryException(
ezcQueryException::INSERT_BUILD_FAILED,
- "No " . $problem . " set." );
+ throw new ezcQueryException( "No " . $problem . " set." );
}
$query = "UPDATE {$this->table} SET ";
Modified: packages/Database/trunk/tests/sqlabstraction/query_delete_test.php
===================================================================
--- packages/Database/trunk/tests/sqlabstraction/query_delete_test.php
2006-01-13 12:39:30 UTC (rev 1823)
+++ packages/Database/trunk/tests/sqlabstraction/query_delete_test.php
2006-01-13 13:03:10 UTC (rev 1824)
@@ -54,6 +54,30 @@
$this->assertEquals( $reference, $this->q->getQuery() );
}
+ public function testWithDoubleWhere()
+ {
+ try
+ {
+ $this->q->deleteFrom( 'legends' )
+ ->where( $this->q->expr->eq( 'Gretzky', 'Lindros' ) )
+ ->where( $this->q->expr->eq( 'Gretzky', 'Lindros' ) );
+ }
+ catch( ezcQueryException $e ){ return; }
+ $this->fail( "Got no exception when an exception was expected" );
+ }
+
+ public function testInvalidWhereCall()
+ {
+ try
+ {
+ $this->q->deleteFrom( 'legends' )
+ ->where();
+ }
+ catch( ezcQueryException $e ){ return; }
+ $this->fail( "Got no exception when an exception was expected" );
+ }
+
+
public function testNoDeleteFrom()
{
try{
Modified: packages/Database/trunk/tests/sqlabstraction/query_update_test.php
===================================================================
--- packages/Database/trunk/tests/sqlabstraction/query_update_test.php
2006-01-13 12:39:30 UTC (rev 1823)
+++ packages/Database/trunk/tests/sqlabstraction/query_update_test.php
2006-01-13 13:03:10 UTC (rev 1824)
@@ -58,8 +58,41 @@
public function testWithWhere()
{
+ $reference = "UPDATE legends SET Gretzky = 99, Lindros = 88 WHERE
Gretzky = Lindros";
+ $this->q->update( 'legends' )
+ ->set( 'Gretzky', '99' )
+ ->set( 'Lindros', '88' )
+ ->where( $this->q->expr->eq( 'Gretzky', 'Lindros' ) );
+ $this->assertEquals( $reference, $this->q->getQuery() );
}
+ public function testWithDoubleWhere()
+ {
+ try
+ {
+ $this->q->update( 'legends' )
+ ->set( 'Gretzky', '99' )
+ ->set( 'Lindros', '88' )
+ ->where( $this->q->expr->eq( 'Gretzky', 'Lindros' ) )
+ ->where( $this->q->expr->eq( 'Gretzky', 'Lindros' ) );
+ }
+ catch( ezcQueryException $e ){ return; }
+ $this->fail( "Got no exception when an exception was expected" );
+ }
+
+ public function testInvalidWhereCall()
+ {
+ try
+ {
+ $this->q->update( 'legends' )
+ ->set( 'Gretzky', '99' )
+ ->set( 'Lindros', '88' )
+ ->where();
+ }
+ catch( ezcQueryException $e ){ return; }
+ $this->fail( "Got no exception when an exception was expected" );
+ }
+
public function testNoTable()
{
try{
--
svn-components mailing list
[email protected]
http://lists.ez.no/mailman/listinfo/svn-components