You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@empire-db.apache.org by fr...@apache.org on 2011/04/03 23:12:16 UTC
svn commit: r1088417 [2/4] - in /incubator/empire-db/branches/EMPIREDB-99:
empire-db-codegen/src/main/java/org/apache/empire/db/codegen/
empire-db-examples/empire-db-example-advanced/src/main/java/org/apache/empire/samples/db/advanced/
empire-db-exampl...
Modified: incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBDatabaseDriver.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBDatabaseDriver.java?rev=1088417&r1=1088416&r2=1088417&view=diff
==============================================================================
--- incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBDatabaseDriver.java (original)
+++ incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBDatabaseDriver.java Sun Apr 3 21:12:14 2011
@@ -33,8 +33,8 @@ import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
+import org.apache.empire.EmpireException;
import org.apache.empire.commons.DateUtils;
-import org.apache.empire.commons.ErrorObject;
import org.apache.empire.commons.Errors;
import org.apache.empire.commons.ObjectUtils;
import org.apache.empire.commons.StringUtils;
@@ -48,7 +48,7 @@ import org.slf4j.LoggerFactory;
* The DBDatabaseDriver class is an abstract base class for all database drivers.
* Its purpose is to handle everything that is - or might be - database vendor specific.
*/
-public abstract class DBDatabaseDriver extends ErrorObject implements Serializable
+public abstract class DBDatabaseDriver implements Serializable
{
private final static long serialVersionUID = 1L;
@@ -226,8 +226,7 @@ public abstract class DBDatabaseDriver e
} catch (Exception e)
{
log.error("getNextValue exception: " + e.toString());
- error(e);
- return null;
+ throw new EmpireException(e);
} finally
{ // Cleanup
db.closeStatement(stmt);
@@ -380,8 +379,7 @@ public abstract class DBDatabaseDriver e
return (type==DataType.DATE ? DateUtils.getDateOnly(ts) : ts);
}
// Other types
- error(Errors.NotSupported, "getColumnAutoValue() for "+type);
- return null;
+ throw new EmpireException(Errors.NotSupported, "getColumnAutoValue() for " + type);
}
/**
@@ -679,9 +677,9 @@ public abstract class DBDatabaseDriver e
/**
* Called when a database is opened
*/
- protected boolean attachDatabase(DBDatabase db, Connection conn)
+ protected void attachDatabase(DBDatabase db, Connection conn)
{
- return success();
+ // Overrided by the driver impl
}
/**
@@ -703,7 +701,7 @@ public abstract class DBDatabaseDriver e
*/
public boolean checkDatabase(DBDatabase db, String owner, Connection conn)
{
- return error(Errors.NotImplemented, "checkDatabase");
+ throw new EmpireException(Errors.NotImplemented, "checkDatabase");
}
/**
@@ -713,11 +711,10 @@ public abstract class DBDatabaseDriver e
* @param dbo the databse object
* @param script the script to complete
*
- * @return true on succes
*/
- public boolean getDDLScript(DBCmdType type, DBObject dbo, DBSQLScript script)
+ public void getDDLScript(DBCmdType type, DBObject dbo, DBSQLScript script)
{
- return error(Errors.NotSupported, "getDDLScript");
+ throw new EmpireException(Errors.NotSupported, "getDDLScript");
}
/**
Modified: incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBObject.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBObject.java?rev=1088417&r1=1088416&r2=1088417&view=diff
==============================================================================
--- incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBObject.java (original)
+++ incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBObject.java Sun Apr 3 21:12:14 2011
@@ -19,14 +19,14 @@
package org.apache.empire.db;
// java.sql
-import org.apache.empire.commons.ErrorObject;
+import java.io.Serializable;
+import java.sql.SQLException;
+
+import org.apache.empire.EmpireException;
import org.apache.empire.commons.ErrorType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import java.io.Serializable;
-import java.sql.SQLException;
-
/**
* Base class for all database related objects.
@@ -34,7 +34,7 @@ import java.sql.SQLException;
*
*
*/
-public abstract class DBObject extends ErrorObject implements Serializable
+public abstract class DBObject implements Serializable
{
private static final long serialVersionUID = 1L;
// Logger
@@ -56,15 +56,15 @@ public abstract class DBObject extends E
*
* @return the return value is always false
*/
- protected boolean error(ErrorType type, SQLException sqle)
+ protected void error(ErrorType type, SQLException sqle)
{
log.error("Database operation failed.", sqle);
// converts a database error message to a human readable error message.
DBDatabase db = getDatabase();
if (db!=null && db.getDriver()!=null)
- return error(type, db.getDriver().extractErrorMessage(sqle));
+ throw new EmpireException(type, db.getDriver().extractErrorMessage(sqle));
// Set the error Message
- return error(type, sqle.getMessage());
+ throw new EmpireException(type, sqle.getMessage());
}
/**
@@ -74,9 +74,9 @@ public abstract class DBObject extends E
*
* @return the return value is always false
*/
- protected boolean error(SQLException sqle)
+ protected void error(SQLException sqle)
{ // converts a database error message to a human readable error message.
- return error(DBErrors.SQLException, sqle);
+ error(DBErrors.SQLException, sqle);
}
}
\ No newline at end of file
Modified: incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBQuery.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBQuery.java?rev=1088417&r1=1088416&r2=1088417&view=diff
==============================================================================
--- incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBQuery.java (original)
+++ incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBQuery.java Sun Apr 3 21:12:14 2011
@@ -18,6 +18,13 @@
*/
package org.apache.empire.db;
+import java.sql.Connection;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.empire.EmpireException;
import org.apache.empire.commons.Errors;
import org.apache.empire.commons.ObjectUtils;
import org.apache.empire.commons.Options;
@@ -28,12 +35,6 @@ import org.apache.empire.db.expr.compare
import org.apache.empire.db.expr.join.DBJoinExpr;
import org.w3c.dom.Element;
-import java.sql.Connection;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.concurrent.atomic.AtomicInteger;
-
/**
* This class can be used to wrap a query from a DBCommand and use it like a DBRowSet.<BR>
@@ -112,12 +113,12 @@ public class DBQuery extends DBRowSet
}
@Override
- public boolean checkValue(Object value)
+ public void checkValue(Object value)
{
DBColumn column = expr.getUpdateColumn();
if (column==null)
- return true;
- return column.checkValue(value);
+ return;
+ column.checkValue(value);
}
@Override
@@ -271,8 +272,7 @@ public class DBQuery extends DBRowSet
{
if (record == null || record.getRowSet() != this)
{
- error(Errors.InvalidArg, record, "record");
- return null; // Invalid Argument
+ throw new EmpireException(Errors.InvalidArg, record, "record");
}
// get Key
return (Object[]) record.getRowSetData();
@@ -304,14 +304,12 @@ public class DBQuery extends DBRowSet
*
* @param rec the Record object
* @param keyValues an array of the primary key columns
- * @return true if successful
*/
@Override
- public boolean initRecord(DBRecord rec, Object[] keyValues)
+ public void initRecord(DBRecord rec, Object[] keyValues)
{
// Inititialisierung
- if (!prepareInitRecord(rec, DBRecord.REC_EMTPY, keyValues))
- return false;
+ prepareInitRecord(rec, DBRecord.REC_EMTPY, keyValues);
// Initialize all Fields
Object[] fields = rec.getFields();
for (int i = 0; i < fields.length; i++)
@@ -325,7 +323,7 @@ public class DBQuery extends DBRowSet
fields[columns.indexOf(keyColumns[i])] = keyValues[i];
}
// Init
- return completeInitRecord(rec);
+ completeInitRecord(rec);
}
/**
@@ -336,9 +334,9 @@ public class DBQuery extends DBRowSet
* @return an error, because querys could't add new records to the database
*/
@Override
- public boolean createRecord(DBRecord rec, Connection conn)
+ public void createRecord(DBRecord rec, Connection conn)
{
- return error(Errors.NotImplemented, "addRecord");
+ throw new EmpireException(Errors.NotImplemented, "addRecord");
}
/**
@@ -347,16 +345,15 @@ public class DBQuery extends DBRowSet
* @param rec rec the DBRecord object, contains all fields and the field properties
* @param key an array of the primary key columns
* @param conn a valid connection to the database.
- * @return true if successful
*/
@Override
- public boolean readRecord(DBRecord rec, Object[] key, Connection conn)
+ public void readRecord(DBRecord rec, Object[] key, Connection conn)
{
if (conn == null || rec == null)
- return error(Errors.InvalidArg, null, "conn|rec");
+ throw new EmpireException(Errors.InvalidArg, null, "conn|rec");
DBColumn[] keyColumns = getKeyColumns();
if (key == null || keyColumns.length != key.length)
- return error(DBErrors.RecordInvalidKey, key);
+ throw new EmpireException(DBErrors.RecordInvalidKey, key);
// Select
for (int i = 0; i < keyColumns.length; i++)
{ // Set key column constraint
@@ -366,16 +363,9 @@ public class DBQuery extends DBRowSet
cmd.where(keyColumns[i].is(value));
}
// Read Record
- if (!readRecord(rec, cmd, conn))
- { // Record not found
- if (getErrorType() == DBErrors.QueryNoResult)
- return error(DBErrors.RecordNotFound, key);
- // Return given error
- return false;
- }
+ readRecord(rec, cmd, conn);
// Set RowSetData
rec.changeState(DBRecord.REC_VALID, key.clone());
- return success();
}
/**
@@ -386,17 +376,17 @@ public class DBQuery extends DBRowSet
* @return true if succesfull
*/
@Override
- public boolean updateRecord(DBRecord rec, Connection conn)
+ public void updateRecord(DBRecord rec, Connection conn)
{
if (conn == null || rec == null)
- return error(Errors.InvalidArg, null, "conn|rec");
+ throw new EmpireException(Errors.InvalidArg, null, "conn|rec");
// Has record been modified?
if (rec.isModified() == false)
- return success(); // Nothing to update
+ return; // Nothing to update
// Must have key Columns
DBColumn[] keyColumns = getKeyColumns();
if (keyColumns==null)
- return error(DBErrors.NoPrimaryKey, getAlias());
+ throw new EmpireException(DBErrors.NoPrimaryKey, getAlias());
// Get the fields and the flags
Object[] fields = rec.getFields();
// Get all Update Commands
@@ -423,8 +413,7 @@ public class DBQuery extends DBRowSet
if (col.isReadOnly() && log.isDebugEnabled())
log.debug("updateRecord: Read-only column '" + col.getName() + " has been modified!");
// Check the value
- if (!col.checkValue(fields[i]))
- return error(col);
+ col.checkValue(fields[i]);
// Set
updCmd.set(col.to(fields[i]));
}
@@ -450,10 +439,10 @@ public class DBQuery extends DBRowSet
DBColumn right = join.getRight().getUpdateColumn();
if (left.getRowSet()==table && table.isKeyColumn(left))
if (!addJoinRestriction(upd, left, right, keyColumns, rec))
- return error(Errors.ItemNotFound, left.getFullName());
+ throw new EmpireException(Errors.ItemNotFound, left.getFullName());
if (right.getRowSet()==table && table.isKeyColumn(right))
if (!addJoinRestriction(upd, right, left, keyColumns, rec))
- return error(Errors.ItemNotFound, right.getFullName());
+ throw new EmpireException(Errors.ItemNotFound, right.getFullName());
}
// Evaluate Existing restrictions
for (i = 0; cmd.where != null && i < cmd.where.size(); i++)
@@ -477,7 +466,7 @@ public class DBQuery extends DBRowSet
}
else
{ // other constraints are not supported
- return error(Errors.NotSupported, "updateRecord");
+ throw new EmpireException(Errors.NotSupported, "updateRecord");
}
}
// Add Restrictions
@@ -524,15 +513,14 @@ public class DBQuery extends DBRowSet
{ // Error
if (affected == 0)
{ // Record not found
- error(DBErrors.RecordUpdateFailed, table.getName());
+ throw new EmpireException(DBErrors.RecordUpdateFailed, table.getName());
}
// Rollback
db.rollback(conn);
- return false;
}
else if (affected > 1)
{ // More than one record
- error(DBErrors.RecordUpdateInvalid, table.getName());
+ throw new EmpireException(DBErrors.RecordUpdateInvalid, table.getName());
}
else
{ // success
@@ -546,7 +534,6 @@ public class DBQuery extends DBRowSet
}
// success
rec.changeState(DBRecord.REC_VALID, keys);
- return success();
}
/**
@@ -584,9 +571,9 @@ public class DBQuery extends DBRowSet
* @return true if the record has been successfully deleted or false otherwise
*/
@Override
- public boolean deleteRecord(Object[] keys, Connection conn)
+ public void deleteRecord(Object[] keys, Connection conn)
{
- return error(Errors.NotImplemented, "deleteRecord");
+ throw new EmpireException(Errors.NotImplemented, "deleteRecord");
}
}
\ No newline at end of file
Modified: incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBReader.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBReader.java?rev=1088417&r1=1088416&r2=1088417&view=diff
==============================================================================
--- incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBReader.java (original)
+++ incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBReader.java Sun Apr 3 21:12:14 2011
@@ -18,17 +18,6 @@
*/
package org.apache.empire.db;
-import org.apache.commons.beanutils.ConstructorUtils;
-import org.apache.empire.commons.Errors;
-import org.apache.empire.commons.ObjectUtils;
-import org.apache.empire.data.ColumnExpr;
-import org.apache.empire.data.DataType;
-import org.apache.empire.xml.XMLUtil;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-
import java.io.IOException;
import java.io.NotSerializableException;
import java.io.ObjectOutputStream;
@@ -43,6 +32,18 @@ import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
+import org.apache.commons.beanutils.ConstructorUtils;
+import org.apache.empire.EmpireException;
+import org.apache.empire.commons.Errors;
+import org.apache.empire.commons.ObjectUtils;
+import org.apache.empire.data.ColumnExpr;
+import org.apache.empire.data.DataType;
+import org.apache.empire.xml.XMLUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
/**
* <P>
@@ -120,7 +121,6 @@ public class DBReader extends DBRecordDa
{
try
{ // clear previous error
- clearError();
// Check position
if (curCount >= maxCount)
return false;
@@ -131,7 +131,7 @@ public class DBReader extends DBRecordDa
return true;
} catch (SQLException e)
{
- return error(e);
+ throw new EmpireException(e);
}
}
@@ -187,7 +187,7 @@ public class DBReader extends DBRecordDa
if (curCount >= maxCount)
return false;
if (rset == null)
- return error(Errors.ObjectNotValid, getClass().getName());
+ throw new EmpireException(Errors.ObjectNotValid, getClass().getName());
// Check next Record
if (getCurrent == true)
{
@@ -346,7 +346,6 @@ public class DBReader extends DBRecordDa
}
try
{ // Check Value on Resultset
- clearError();
rset.getObject(index + 1);
return rset.wasNull();
} catch (Exception e)
@@ -367,19 +366,16 @@ public class DBReader extends DBRecordDa
{
if (index < 0 || index >= colList.length)
{ // Index out of range
- error(Errors.OutOfRange, index);
- return null;
+ throw new EmpireException(Errors.OutOfRange, index);
}
try
{ // Get Value from Resultset
- clearError();
DataType dataType = colList[index].getDataType();
return db.driver.getResultValue(rset, index + 1, dataType);
} catch (Exception e)
{ // Operation failed
- error(e);
- return null;
+ throw new EmpireException(e);
}
}
@@ -407,7 +403,7 @@ public class DBReader extends DBRecordDa
* @param conn a valid JDBC connection.
* @return true if successful
*/
- public boolean open(DBCommandExpr cmd, boolean scrollable, Connection conn)
+ public void open(DBCommandExpr cmd, boolean scrollable, Connection conn)
{
if (isOpen())
close();
@@ -416,12 +412,9 @@ public class DBReader extends DBRecordDa
// Create Statement
db = cmd.getDatabase();
rset = db.executeQuery(sqlCmd, cmd.getParamValues(), scrollable, conn);
- if (rset==null)
- return error(db);
// successfully opened
colList = cmd.getSelectExprList();
addOpenResultSet();
- return success();
}
/**
@@ -433,9 +426,9 @@ public class DBReader extends DBRecordDa
* @param conn a valid JDBC connection.
* @return true if successful
*/
- public boolean open(DBCommandExpr cmd, Connection conn)
+ public void open(DBCommandExpr cmd, Connection conn)
{
- return open(cmd, false, conn);
+ open(cmd, false, conn);
}
/**
@@ -450,18 +443,15 @@ public class DBReader extends DBRecordDa
* <P>
* @param cmd the SQL-Command with cmd.getSelect()
* @param conn a valid JDBC connection.
- * @return true if successful
*/
- public boolean getRecordData(DBCommandExpr cmd, Connection conn)
+ public void getRecordData(DBCommandExpr cmd, Connection conn)
{ // Open the record
- if (!open(cmd, conn))
- return false;
+ open(cmd, conn);
// Get First Record
if (!moveNext())
{ // Close
- return error(DBErrors.QueryNoResult, cmd.getSelect());
+ throw new EmpireException(DBErrors.QueryNoResult, cmd.getSelect());
}
- return success();
}
/**
@@ -505,16 +495,15 @@ public class DBReader extends DBRecordDa
{
try
{ // clear previous error
- clearError();
// Check Recordset
if (rset == null)
- return error(Errors.ObjectNotValid, getClass().getName());
+ throw new EmpireException(Errors.ObjectNotValid, getClass().getName());
// Forward only cursor?
int type = rset.getType();
if (type == ResultSet.TYPE_FORWARD_ONLY)
{
if (count < 0)
- return error(Errors.InvalidArg, count, "count");
+ throw new EmpireException(Errors.InvalidArg, count, "count");
// Move
for (; count > 0; count--)
{
@@ -544,7 +533,7 @@ public class DBReader extends DBRecordDa
} catch (SQLException e)
{ // an error ocurred
- return error(e);
+ throw new EmpireException(e);
}
}
@@ -557,22 +546,20 @@ public class DBReader extends DBRecordDa
{
try
{ // clear previous error
- clearError();
// Check Recordset
if (rset == null)
- return error(Errors.ObjectNotValid, getClass().getName());
+ throw new EmpireException(Errors.ObjectNotValid, getClass().getName());
// Move Next
if (rset.next() == false)
{ // Close recordset automatically after last record
close();
- clearError();
return false;
}
return true;
} catch (SQLException e)
{ // an error ocurred
- return error(e);
+ throw new EmpireException(e);
}
}
@@ -620,13 +607,11 @@ public class DBReader extends DBRecordDa
* @param rec the record which to initialize
* @return true if the record has been initialized sucessfully or false otherwise
*/
- public boolean initRecord(DBRowSet rowset, DBRecord rec)
+ public void initRecord(DBRowSet rowset, DBRecord rec)
{
if (rowset==null)
- return error(Errors.InvalidArg, rowset, "rowset");
- if (rowset.initRecord(rec, this)==false)
- return error(rowset);
- return success();
+ throw new EmpireException(Errors.InvalidArg, rowset, "rowset");
+ rowset.initRecord(rec, this);
}
/**
@@ -645,8 +630,7 @@ public class DBReader extends DBRecordDa
// Check Recordset
if (rset == null)
{ // Resultset not available
- error(Errors.ObjectNotValid, getClass().getName());
- return null;
+ throw new EmpireException(Errors.ObjectNotValid, getClass().getName());
}
// Query List
try
@@ -672,8 +656,7 @@ public class DBReader extends DBRecordDa
else
{ // Use Property Setters
T bean = t.newInstance();
- if (getBeanProperties(bean)==false)
- return null;
+ getBeanProperties(bean);
c.add(bean);
}
// Decrease count
@@ -684,16 +667,13 @@ public class DBReader extends DBRecordDa
return c;
} catch (InvocationTargetException e)
{
- error(e);
- return null;
+ throw new EmpireException(e);
} catch (IllegalAccessException e)
{
- error(e);
- return null;
+ throw new EmpireException(e);
} catch (InstantiationException e)
{
- error(e);
- return null;
+ throw new EmpireException(e);
}
}
@@ -723,17 +703,15 @@ public class DBReader extends DBRecordDa
/**
* Moves the cursor down one row from its current position.
*
- * @return true if successful
*/
@Override
- public boolean addColumnDesc(Element parent)
+ public void addColumnDesc(Element parent)
{
if (colList == null)
- return error(Errors.ObjectNotValid, getClass().getName());
+ throw new EmpireException(Errors.ObjectNotValid, getClass().getName());
// Add Field Description
for (int i = 0; i < colList.length; i++)
colList[i].addXml(parent, 0);
- return success();
}
/**
@@ -743,10 +721,10 @@ public class DBReader extends DBRecordDa
* @return true if successful
*/
@Override
- public boolean addRowValues(Element parent)
+ public void addRowValues(Element parent)
{
if (rset == null)
- return error(Errors.ObjectNotValid, getClass().getName());
+ throw new EmpireException(Errors.ObjectNotValid, getClass().getName());
// Add all children
for (int i = 0; i < colList.length; i++)
{ // Read all
@@ -764,7 +742,6 @@ public class DBReader extends DBRecordDa
elem.setAttribute("null", "yes"); // Null-Value
}
}
- return success();
}
/**
@@ -811,8 +788,7 @@ public class DBReader extends DBRecordDa
String rowsetElementName = getXmlDictionary().getRowSetElementName();
Element root = XMLUtil.createDocument(rowsetElementName);
// Add Field Description
- if (!addColumnDesc(root))
- return null;
+ addColumnDesc(root);
// Add row rset
addRows(root);
// return Document
Modified: incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBRecord.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBRecord.java?rev=1088417&r1=1088416&r2=1088417&view=diff
==============================================================================
--- incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBRecord.java (original)
+++ incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBRecord.java Sun Apr 3 21:12:14 2011
@@ -18,8 +18,14 @@
*/
package org.apache.empire.db;
+import java.lang.reflect.InvocationTargetException;
+import java.sql.Connection;
+import java.util.Collection;
+import java.util.List;
+
import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.PropertyUtilsBean;
+import org.apache.empire.EmpireException;
import org.apache.empire.commons.Errors;
import org.apache.empire.commons.ObjectUtils;
import org.apache.empire.commons.Options;
@@ -32,11 +38,6 @@ import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
-import java.lang.reflect.InvocationTargetException;
-import java.sql.Connection;
-import java.util.Collection;
-import java.util.List;
-
/**
*
@@ -321,11 +322,9 @@ public class DBRecord extends DBRecordDa
public boolean wasModified(int index)
{
if (rowset == null)
- return error(Errors.ObjectNotValid, getClass().getName());
+ throw new EmpireException(Errors.ObjectNotValid, getClass().getName());
if (index < 0 || index >= fields.length)
- return error(Errors.OutOfRange, index);
- // Check modified
- clearError();
+ throw new EmpireException(Errors.OutOfRange, index);
if (modified == null)
return false;
return modified[index];
@@ -407,16 +406,13 @@ public class DBRecord extends DBRecordDa
{ // Check state
if (fields == null)
{ // Record not valid
- error(Errors.ObjectNotValid, getClass().getName());
- return null;
+ throw new EmpireException(Errors.ObjectNotValid, getClass().getName());
}
// Check index
if (index < 0 || index>= fields.length)
{ // Index out of range
- error(Errors.OutOfRange, index);
- return null;
+ throw new EmpireException(Errors.OutOfRange, index);
}
- clearError();
// Special check for NO_VALUE
if (fields[index] == ObjectUtils.NO_VALUE)
return null;
@@ -435,13 +431,12 @@ public class DBRecord extends DBRecordDa
public boolean isValueValid(int index)
{ // Check state
if (fields == null)
- return error(Errors.ObjectNotValid, getClass().getName());
+ throw new EmpireException(Errors.ObjectNotValid, getClass().getName());
// Check index
if (index < 0 || index>= fields.length)
{ // Index out of range
- return error(Errors.OutOfRange, index);
+ throw new EmpireException(Errors.OutOfRange, index);
}
- clearError();
// Special check for NO_VALUE
return (fields[index] != ObjectUtils.NO_VALUE);
}
@@ -503,34 +498,29 @@ public class DBRecord extends DBRecordDa
*
* @param i the index of the column
* @param value the value
- * @return true if successful
*/
- public boolean setValue(int i, Object value)
+ public void setValue(int i, Object value)
{
if (rowset == null)
- return error(Errors.ObjectNotValid, getClass().getName());
+ throw new EmpireException(Errors.ObjectNotValid, getClass().getName());
if (i < 0 || i >= fields.length)
- return error(Errors.OutOfRange, i);
+ throw new EmpireException(Errors.OutOfRange, i);
// Strings special
if ((value instanceof String) && ((String)value).length()==0)
value = null;
// Has Value changed?
if (ObjectUtils.compareEqual(fields[i], value))
- return success(); // no change
+ return; // no change
// Field has changed
DBColumn column = rowset.getColumn(i);
if (column.isAutoGenerated())
{ // Read Only column may be set
- return error(DBErrors.FieldIsReadOnly, column.getName());
+ throw new EmpireException(DBErrors.FieldIsReadOnly, column.getName());
}
// Is Value valid
- if (column.checkValue(value) == false)
- { // Invalid Value for column
- return error(column);
- }
+ column.checkValue(value);
// Init original values
modifyValue(i, value);
- return success();
}
/**
@@ -542,12 +532,12 @@ public class DBRecord extends DBRecordDa
* @param value the value
* @return true if successful
*/
- public final boolean setValue(Column column, Object value)
+ public final void setValue(Column column, Object value)
{
if (rowset == null)
- return error(Errors.ObjectNotValid, getClass().getName());
+ throw new EmpireException(Errors.ObjectNotValid, getClass().getName());
// Get Column Index
- return setValue(rowset.getColumnIndex(column), value);
+ setValue(rowset.getColumnIndex(column), value);
}
/**
@@ -560,11 +550,10 @@ public class DBRecord extends DBRecordDa
public boolean isFieldReadOnly(DBColumn column)
{
if (rowset==null)
- { error(Errors.ObjectNotValid, getClass().getName());
- return true;
+ { throw new EmpireException(Errors.ObjectNotValid, getClass().getName());
}
// Ask RowSet
- return (rowset.isColumnReadOnly(column));
+ return rowset.isColumnReadOnly(column);
}
/**
@@ -585,7 +574,7 @@ public class DBRecord extends DBRecordDa
public boolean isFieldVisible(DBColumn column)
{
if (rowset==null)
- return error(Errors.ObjectNotValid, getClass().getName());
+ throw new EmpireException(Errors.ObjectNotValid, getClass().getName());
// Check if field is present and the value is valid
int index = rowset.getColumnIndex(column);
return (index>=0 && isValueValid(index));
@@ -607,27 +596,23 @@ public class DBRecord extends DBRecordDa
* @param table the rowset
* @param keyValues a Object array, the primary key(s)
* @param insert if true change the state of this object to REC_NEW
- * @return true if successful or false otherwise
*/
- public boolean init(DBRowSet table, Object[] keyValues, boolean insert)
+ public void init(DBRowSet table, Object[] keyValues, boolean insert)
{ // Init with keys
- if (table.initRecord(this, keyValues) == false)
- return error(table);
+ table.initRecord(this, keyValues);
if (insert)
state = DBRecord.REC_NEW;
- return success();
}
/**
* @param table
* @param conn
- * @return true on succes
* @deprecated use {@link DBRecord#create(DBRowSet, Connection)}
*/
@Deprecated
- public final boolean initNew(DBRowSet table, Connection conn)
+ public final void initNew(DBRowSet table, Connection conn)
{
- return (table.createRecord(this, conn) == false) ? error(table) : success();
+ table.createRecord(this, conn);
}
/**
@@ -636,9 +621,9 @@ public class DBRecord extends DBRecordDa
* @deprecated use {@link DBRecord#create(DBRowSet)}
*/
@Deprecated
- public final boolean initNew(DBRowSet table)
+ public final void initNew(DBRowSet table)
{
- return initNew(table, null);
+ initNew(table, null);
}
/**
@@ -651,11 +636,10 @@ public class DBRecord extends DBRecordDa
* <P>
* @param table the table for which to create a record
* @param conn a valid JDBC connection
- * @return true if successful
*/
- public boolean create(DBRowSet table, Connection conn)
+ public void create(DBRowSet table, Connection conn)
{
- return (table.createRecord(this, conn) == false) ? error(table) : success();
+ table.createRecord(this, conn);
}
/**
@@ -664,11 +648,10 @@ public class DBRecord extends DBRecordDa
* The record's state is set to NEW
* <P>
* @param table the table for which to create a record
- * @return true if successful
*/
- public boolean create(DBRowSet table)
+ public void create(DBRowSet table)
{
- return create(table, null);
+ create(table, null);
}
/**
@@ -679,11 +662,10 @@ public class DBRecord extends DBRecordDa
* @param table the rowset from which to read the record
* @param keys an array of the primary key values
* @param conn a valid connection to the database.
- * @return true if the record was sucessfully loaded or false if the record was not found or an error occurred.
*/
- public boolean read(DBRowSet table, Object[] keys, Connection conn)
+ public void read(DBRowSet table, Object[] keys, Connection conn)
{
- return (table.readRecord(this, keys, conn) == false) ? error(table) : success();
+ table.readRecord(this, keys, conn);
}
/**
@@ -694,16 +676,15 @@ public class DBRecord extends DBRecordDa
* @param table the rowset from which to read the record
* @param id the primary key of the record to load.
* @param conn a valid connection to the database.
- * @return true if the record was sucessfully loaded or false if the record was not found or an error occurred.
*/
- public final boolean read(DBRowSet table, Object id, Connection conn)
+ public final void read(DBRowSet table, Object id, Connection conn)
{
if (id instanceof Collection<?>)
{ // If it's a collection then convert it to an array
- return read(table, ((Collection<?>)id).toArray(), conn);
+ read(table, ((Collection<?>)id).toArray(), conn);
}
// Simple One-Column key
- return read(table, new Object[] { id }, conn);
+ read(table, new Object[] { id }, conn);
}
/**
@@ -713,13 +694,11 @@ public class DBRecord extends DBRecordDa
* @param conn a valid connection to the database.
* @return true if successful
*/
- public boolean update(Connection conn)
+ public void update(Connection conn)
{
if (rowset == null)
- return error(Errors.ObjectNotValid, getClass().getName());
- if (!rowset.updateRecord(this, conn))
- return error(rowset);
- return success();
+ throw new EmpireException(Errors.ObjectNotValid, getClass().getName());
+ rowset.updateRecord(this, conn);
}
/**
@@ -733,19 +712,17 @@ public class DBRecord extends DBRecordDa
* @param conn a valid connection to the database.
* @return true if successful
*/
- public boolean delete(Connection conn)
+ public void delete(Connection conn)
{
if (isValid()==false)
- return error(Errors.ObjectNotValid, getClass().getName());
+ throw new EmpireException(Errors.ObjectNotValid, getClass().getName());
// Delete only if record is not new
if (!isNew())
{
Object[] keys = rowset.getRecordKey(this);
- if (rowset.deleteRecord(keys, conn)==false)
- return error(rowset);
+ rowset.deleteRecord(keys, conn);
}
close();
- return success();
}
/**
@@ -754,10 +731,10 @@ public class DBRecord extends DBRecordDa
* @return true if successful
*/
@Override
- public boolean addColumnDesc(Element parent)
+ public void addColumnDesc(Element parent)
{
if (rowset == null)
- return error(Errors.ObjectNotValid, getClass().getName());
+ throw new EmpireException(Errors.ObjectNotValid, getClass().getName());
// Add Field Description
List<DBColumn> columns = rowset.getColumns();
for (int i = 0; i < columns.size(); i++)
@@ -767,7 +744,6 @@ public class DBRecord extends DBRecordDa
continue;
column.addXml(parent, 0);
}
- return success();
}
/**
@@ -777,10 +753,10 @@ public class DBRecord extends DBRecordDa
* @return true if successful
*/
@Override
- public boolean addRowValues(Element parent)
+ public void addRowValues(Element parent)
{
if (rowset == null)
- return error(Errors.ObjectNotValid, getClass().getName());
+ throw new EmpireException(Errors.ObjectNotValid, getClass().getName());
// set row key
DBColumn[] keyColumns = rowset.getKeyColumns();
if (keyColumns != null && keyColumns.length > 0)
@@ -816,7 +792,6 @@ public class DBRecord extends DBRecordDa
else
XMLUtil.addElement(parent, name).setAttribute("null", "yes"); // Null-Value
}
- return success();
}
/**
@@ -837,8 +812,8 @@ public class DBRecord extends DBRecordDa
public Document getXmlDocument()
{
if (rowset == null)
- { error(Errors.ObjectNotValid, getClass().getName());
- return null;
+ {
+ throw new EmpireException(Errors.ObjectNotValid, getClass().getName());
}
// Create Document
DBXmlDictionary xmlDic = getXmlDictionary();
@@ -846,13 +821,10 @@ public class DBRecord extends DBRecordDa
if (rowset.getName() != null)
root.setAttribute("name", rowset.getName());
// Add Field Description
- if (!addColumnDesc(root))
- return null;
+ addColumnDesc(root);
// Add row Values
- if (!addRowValues(XMLUtil.addElement(root, xmlDic.getRowElementName())))
- return null;
+ addRowValues(XMLUtil.addElement(root, xmlDic.getRowElementName()));
// return Document
- clearError();
return root.getOwnerDocument();
}
@@ -895,7 +867,7 @@ public class DBRecord extends DBRecordDa
* @param column the column for which to set the record value
* @return true if the value has been sucessfully set or false if not
*/
- protected boolean setBeanValue(Object bean, String property, Column column)
+ protected void setBeanValue(Object bean, String property, Column column)
{
try
{ /*
@@ -908,30 +880,28 @@ public class DBRecord extends DBRecordDa
Object value = pub.getSimpleProperty(bean, property);
// Now, set the record value
- return setValue( column, value );
+ setValue( column, value );
} catch (IllegalAccessException e)
{ log.error(bean.getClass().getName() + ": unable to get property '" + property + "'");
- return error(e);
+ throw new EmpireException(e);
} catch (InvocationTargetException e)
{ log.error(bean.getClass().getName() + ": unable to get property '" + property + "'");
- return error(e);
+ throw new EmpireException(e);
} catch (NoSuchMethodException e)
{
log.warn(bean.getClass().getName() + ": no getter available for property '" + property + "'");
- return error(e);
+ throw new EmpireException(e);
}
}
/**
* Sets record values from the suppied java bean.
*
- * @return true if at least one value has been set sucessfully
*/
- public boolean setBeanValues(Object bean, Collection<Column> ignoreList)
+ public void setBeanValues(Object bean, Collection<Column> ignoreList)
{
// Add all Columns
- int count = 0;
for (int i = 0; i < getFieldCount(); i++)
{ // Check Property
DBColumn column = getDBColumn(i);
@@ -941,19 +911,17 @@ public class DBRecord extends DBRecordDa
continue; // ignore this property
// Get Property Name
String property = column.getBeanPropertyName();
- if (setBeanValue(bean, property, column))
- count++;
+ setBeanValue(bean, property, column);
}
- return (count > 0);
}
/**
- * Sets record values from the suppied java bean.
- * @return true if at least one value has been set sucessfully
+ * Sets record values from the supplied java bean.
+ * @return true if at least one value has been set successfully
*/
- public final boolean setBeanValues(Object bean)
+ public final void setBeanValues(Object bean)
{
- return setBeanValues(bean, null);
+ setBeanValues(bean, null);
}
/**
Modified: incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBRecordData.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBRecordData.java?rev=1088417&r1=1088416&r2=1088417&view=diff
==============================================================================
--- incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBRecordData.java (original)
+++ incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBRecordData.java Sun Apr 3 21:12:14 2011
@@ -18,7 +18,12 @@
*/
package org.apache.empire.db;
// XML
+import java.lang.reflect.InvocationTargetException;
+import java.util.Collection;
+import java.util.Date;
+
import org.apache.commons.beanutils.BeanUtils;
+import org.apache.empire.EmpireException;
import org.apache.empire.commons.DateUtils;
import org.apache.empire.commons.ObjectUtils;
import org.apache.empire.commons.StringUtils;
@@ -29,10 +34,6 @@ import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
-import java.lang.reflect.InvocationTargetException;
-import java.util.Collection;
-import java.util.Date;
-
/**
* This interface defines for the classes DDRecordSet and DBRecord.
@@ -55,8 +56,8 @@ public abstract class DBRecordData exten
// Column lookup
public abstract ColumnExpr getColumnExpr(int i);
// xml
- public abstract boolean addColumnDesc(Element parent);
- public abstract boolean addRowValues (Element parent);
+ public abstract void addColumnDesc(Element parent);
+ public abstract void addRowValues (Element parent);
public abstract Document getXmlDocument();
// others
public abstract void close();
@@ -256,7 +257,7 @@ public abstract class DBRecordData exten
/**
* Set a single property value of a java bean object used by readProperties.
*/
- protected boolean getBeanProperty(Object bean, String property, Object value)
+ protected void getBeanProperty(Object bean, String property, Object value)
{
try
{ /*
@@ -279,16 +280,15 @@ public abstract class DBRecordData exten
* res); }
*/
// done
- return success();
} catch (IllegalAccessException e)
{
log.error(bean.getClass().getName() + ": unable to set property '" + property + "'");
- return error(e);
+ throw new EmpireException(e);
} catch (InvocationTargetException e)
{
log.error(bean.getClass().getName() + ": unable to set property '" + property + "'");
- return error(e);
+ throw new EmpireException(e);
/*
* } catch(NoSuchMethodException e) { log.warn(bean.getClass().getName() + ": cannot check value of property '" +
* property + "'"); return true;
@@ -301,7 +301,7 @@ public abstract class DBRecordData exten
*
* @return true if successful
*/
- public boolean getBeanProperties(Object bean, Collection<ColumnExpr> ignoreList)
+ public void getBeanProperties(Object bean, Collection<ColumnExpr> ignoreList)
{
// Add all Columns
for (int i = 0; i < getFieldCount(); i++)
@@ -311,13 +311,8 @@ public abstract class DBRecordData exten
continue; // ignore this property
// Get Property Name
String property = column.getBeanPropertyName();
- if (getBeanProperty(bean, property, this.getValue(i))==false)
- { // Error setting property.
- return false;
- }
+ getBeanProperty(bean, property, this.getValue(i));
}
- // Success, All Properties have been set
- return success();
}
/**
@@ -325,9 +320,9 @@ public abstract class DBRecordData exten
*
* @return true if successful
*/
- public final boolean getBeanProperties(Object bean)
+ public final void getBeanProperties(Object bean)
{
- return getBeanProperties(bean, null);
+ getBeanProperties(bean, null);
}
}
Modified: incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBRowSet.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBRowSet.java?rev=1088417&r1=1088416&r2=1088417&view=diff
==============================================================================
--- incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBRowSet.java (original)
+++ incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBRowSet.java Sun Apr 3 21:12:14 2011
@@ -18,6 +18,15 @@
*/
package org.apache.empire.db;
+import java.sql.Connection;
+import java.sql.Timestamp;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.empire.EmpireException;
import org.apache.empire.commons.Errors;
import org.apache.empire.commons.ObjectUtils;
import org.apache.empire.commons.StringUtils;
@@ -28,14 +37,6 @@ import org.apache.empire.db.expr.column.
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import java.sql.Connection;
-import java.sql.Timestamp;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
/**
* This class is the base class for all the DBTable,
@@ -95,9 +96,9 @@ public abstract class DBRowSet extends D
public abstract String getAlias();
- public abstract boolean createRecord(DBRecord rec, Connection conn);
+ public abstract void createRecord(DBRecord rec, Connection conn);
- public abstract boolean deleteRecord(Object[] keys, Connection conn);
+ public abstract void deleteRecord(Object[] keys, Connection conn);
/**
* Returns the full qualified name of the rowset.
@@ -289,15 +290,14 @@ public abstract class DBRowSet extends D
* @param source a column reference for one of this table's column
* @param target the target column to which the source column references
*/
- protected boolean addColumnReference(DBColumn source, DBColumn target)
+ protected void addColumnReference(DBColumn source, DBColumn target)
{
if (source.getRowSet()!=this)
- return error(Errors.InvalidArg, source.getFullName(), "column");
+ throw new EmpireException(Errors.InvalidArg, source.getFullName(), "column");
if (columnReferences== null)
columnReferences = new HashMap<DBColumn, DBColumn>();
// Check if column is already there
columnReferences.put(source, target);
- return success();
}
/**
@@ -354,15 +354,13 @@ public abstract class DBRowSet extends D
*
* @param rec the DBRecord object to initialise this DBRowSet object
* @param state the state of this DBRowSet object
- * @return true if successful
*/
- protected boolean prepareInitRecord(DBRecord rec, int state, Object rowSetData)
+ protected void prepareInitRecord(DBRecord rec, int state, Object rowSetData)
{
if (columns.size() < 1)
- return error(Errors.ObjectNotValid, getClass().getName());
+ throw new EmpireException(Errors.ObjectNotValid, getClass().getName());
// Init
rec.init(this, state, rowSetData);
- return success();
}
/**
@@ -371,13 +369,11 @@ public abstract class DBRowSet extends D
* <P>
* @param rec the Record object
* @param keyValues an array of the primary key columns
- * @return true if successful
*/
- public boolean initRecord(DBRecord rec, Object[] keyValues)
+ public void initRecord(DBRecord rec, Object[] keyValues)
{
// Inititialisierung
- if (!prepareInitRecord(rec, DBRecord.REC_EMTPY, null))
- return false;
+ prepareInitRecord(rec, DBRecord.REC_EMTPY, null);
// Initialize all Fields
Object[] fields = rec.getFields();
for (int i = 0; i < fields.length; i++)
@@ -394,7 +390,7 @@ public abstract class DBRowSet extends D
}
}
// Init
- return completeInitRecord(rec);
+ completeInitRecord(rec);
}
/**
@@ -408,7 +404,7 @@ public abstract class DBRowSet extends D
* @param recData the record data from which to initialized the record
* @return true if successful
*/
- public boolean initRecord(DBRecord rec, DBRecordData recData)
+ public void initRecord(DBRecord rec, DBRecordData recData)
{
// Initialize the record
prepareInitRecord(rec, DBRecord.REC_VALID, null);
@@ -424,7 +420,7 @@ public abstract class DBRowSet extends D
{ // Field not available in Record Data
if (primaryKey!=null && primaryKey.contains(column))
{ // Error: Primary Key not supplied
- return error(DBErrors.RecordInvalidKey, column.toString());
+ throw new EmpireException(DBErrors.RecordInvalidKey, column.toString());
}
if (timestampColumn == column)
{ // Check the update Time Stamp
@@ -437,43 +433,35 @@ public abstract class DBRowSet extends D
else
{ // Get Field value
fields[i] = recData.getValue(rdi);
- // Check for error
- if (fields[i]==null && recData.hasError())
- return error(recData);
}
} catch (Exception e)
{ // Unknown exception
log.error("initRecord exception: " + e.toString());
rec.close();
- return error(e);
+ throw new EmpireException(e);
}
}
// Done
- return completeInitRecord(rec);
+ completeInitRecord(rec);
}
/**
* Reads a single record from the database using the given command object.<BR>
- * If a reocord is found the DBRecord object will hold all record data.
+ * If a record is found the DBRecord object will hold all record data.
* <P>
* @param rec the DBRecord object which holds the record data
* @param cmd the SQL-Command used to query the record
* @param conn a valid JDBC connection.
* @return true if successful
*/
- protected boolean readRecord(DBRecord rec, DBCommand cmd, Connection conn)
+ protected void readRecord(DBRecord rec, DBCommand cmd, Connection conn)
{
DBReader reader = null;
try
{
- clearError();
reader = new DBReader();
- if (reader.getRecordData(cmd, conn)==false)
- return error(reader);
- if (initRecord(rec, reader)==false)
- return false;
- // Done
- return success();
+ reader.getRecordData(cmd, conn);
+ initRecord(rec, reader);
} finally
{
@@ -488,10 +476,9 @@ public abstract class DBRowSet extends D
* @param rec the DBRecord object to initialise
* @return true if successful
*/
- protected boolean completeInitRecord(DBRecord rec)
+ protected void completeInitRecord(DBRecord rec)
{
rec.onRecordChanged();
- return success();
}
/**
@@ -500,15 +487,15 @@ public abstract class DBRowSet extends D
* @param key the record key
* @return true if the constraints have been successfully set or false otherwise
*/
- protected boolean setKeyConstraints(DBCommand cmd, Object[] key)
+ protected void setKeyConstraints(DBCommand cmd, Object[] key)
{
// Check Primary key
if (primaryKey == null )
- return error(DBErrors.NoPrimaryKey, getName()); // Invalid Argument
+ throw new EmpireException(DBErrors.NoPrimaryKey, getName()); // Invalid Argument
// Check Columns
DBColumn[] keyColumns = primaryKey.getColumns();
if (key == null || key.length != keyColumns.length)
- return error(DBErrors.RecordInvalidKey, key); // Invalid Argument
+ throw new EmpireException(DBErrors.RecordInvalidKey, key); // Invalid Argument
// Add the key constraints
for (int i = 0; i < key.length; i++)
{ // Set key column constraint
@@ -516,8 +503,7 @@ public abstract class DBRowSet extends D
if (db.isPreparedStatementsEnabled())
value = cmd.addParam(keyColumns[i], value);
cmd.where(keyColumns[i].is(value));
- }
- return true;
+ }
}
/**
@@ -526,29 +512,19 @@ public abstract class DBRowSet extends D
* @param rec the DBRecord object which will hold the record data
* @param key the primary key values
* @param conn a valid JDBC connection.
- * @return true if successful
*/
- public boolean readRecord(DBRecord rec, Object[] key, Connection conn)
+ public void readRecord(DBRecord rec, Object[] key, Connection conn)
{
// Check Arguments
if (conn == null || rec == null)
- return error(Errors.InvalidArg, null, "conn|rec");
+ throw new EmpireException(Errors.InvalidArg, null, "conn|rec");
// Select
DBCommand cmd = db.createCommand();
cmd.select(columns);
// Set key constraints
- if (!setKeyConstraints(cmd, key))
- return false;
+ setKeyConstraints(cmd, key);
// Read Record
- if (!readRecord(rec, cmd, conn))
- { // Record not found
- if (getErrorType()==DBErrors.QueryNoResult)
- return error(DBErrors.RecordNotFound, key);
- // Return given error
- return false;
- }
- // Done
- return success();
+ readRecord(rec, cmd, conn);
}
/**
@@ -562,13 +538,12 @@ public abstract class DBRowSet extends D
{
// Check Arguments
if (conn == null)
- return error(Errors.InvalidArg, conn, "conn");
+ throw new EmpireException(Errors.InvalidArg, conn, "conn");
// Select
DBCommand cmd = db.createCommand();
cmd.select(count());
// Set key constraints
- if (!setKeyConstraints(cmd, key))
- return false;
+ setKeyConstraints(cmd, key);
// check exits
return (db.querySingleInt(cmd.getSelect(), conn)==1);
}
@@ -597,13 +572,12 @@ public abstract class DBRowSet extends D
* <P>
* @param rec the DBRecord object. contains all fields and the field properties
* @param conn a valid JDBC connection.
- * @return true if the update was sucessful or false otherwise
*/
- public boolean updateRecord(DBRecord rec, Connection conn)
+ public void updateRecord(DBRecord rec, Connection conn)
{
// Check Arguments
if (conn == null)
- return error(Errors.InvalidArg, conn, "conn");
+ throw new EmpireException(Errors.InvalidArg, conn, "conn");
// Get the new Timestamp
String name = getName();
Timestamp timestamp = (timestampColumn!=null) ? db.getUpdateTimestamp(conn) : null;
@@ -620,7 +594,7 @@ public abstract class DBRowSet extends D
if (primaryKey == null)
{ // Requires a primary key
log.error("updateRecord: " + name + " no primary key defined!");
- return error(DBErrors.NoPrimaryKey, name);
+ throw new EmpireException(DBErrors.NoPrimaryKey, name);
}
for (int i = 0; i < columns.size(); i++)
{ // search for the column
@@ -657,8 +631,7 @@ public abstract class DBRowSet extends D
if (col.isReadOnly())
log.warn("updateRecord: Read-only column '" + col.getName() + " has been modified!");
// Check the value
- if (!col.checkValue(value))
- return error(col);
+ col.checkValue(value);
// Set the column
cmd.set(col.to(value));
setCount++;
@@ -694,19 +667,20 @@ public abstract class DBRowSet extends D
// Add the value to the command
if (empty==false)
{ // Check the value
- if (!col.isAutoGenerated() && !col.checkValue(value))
- return error(col);
+ if (!col.isAutoGenerated()){
+ col.checkValue(value);
+ }
// Insert a field
cmd.set(col.to(value));
setCount++;
}
else if (primaryKey!=null && primaryKey.contains(col))
{ // All primary key fields must be supplied
- return error(DBErrors.FieldNotNull, col.getName());
+ throw new EmpireException(DBErrors.FieldNotNull, col.getName());
}
else if (col.isRequired())
{ // Error Column is required!
- return error(DBErrors.FieldNotNull, col.getName());
+ throw new EmpireException(DBErrors.FieldNotNull, col.getName());
}
}
sql = cmd.getInsert();
@@ -714,26 +688,26 @@ public abstract class DBRowSet extends D
default:
log.warn("updateRecord: " + name + " record has not been modified! ");
- return success();
+ return;
}
if (setCount == 0)
{ // Cannot update or insert fields
log.info("updateRecord: " + name + " nothing to update or insert!");
- return success();
+ return;
}
// Perform action
int affected = db.executeSQL(sql, cmd.getParamValues(), conn, setGenKey);
if (affected < 0)
{ // Update Failed
- return error(db);
+ throw new EmpireException(DBErrors.RecordUpdateFailed, name);
}
else if (affected == 0)
{ // Record not found
- return error(DBErrors.RecordUpdateFailed, name);
+ throw new EmpireException(DBErrors.RecordUpdateFailed, name);
}
else if (affected > 1)
{ // Multiple Records affected
- return error(DBErrors.RecordUpdateInvalid, name);
+ throw new EmpireException(DBErrors.RecordUpdateInvalid, name);
}
// Correct Timestamp
if (timestampColumn != null)
@@ -744,7 +718,6 @@ public abstract class DBRowSet extends D
}
// Change State
rec.changeState(DBRecord.REC_VALID, null);
- return success();
}
/**
@@ -754,9 +727,9 @@ public abstract class DBRowSet extends D
* @param conn a valid JDBC connection
* @return true if the record has been successfully deleted or false otherwise
*/
- public final boolean deleteRecord(Object id, Connection conn)
+ public final void deleteRecord(Object id, Connection conn)
{
- return deleteRecord(new Object[] { id }, conn);
+ deleteRecord(new Object[] { id }, conn);
}
/**
@@ -764,15 +737,14 @@ public abstract class DBRowSet extends D
* <P>
* @param key the key the record to be deleted
* @param conn a valid connection
- * @return true if all reference records could be deleted
*/
- protected final boolean deleteAllReferences(Object[] key, Connection conn)
+ protected final void deleteAllReferences(Object[] key, Connection conn)
{
// Merge Sub-Records
List<DBRelation> relations = db.getRelations();
DBColumn[] keyColumns = getKeyColumns();
if (keyColumns==null)
- return success(); // No primary key - no references!
+ return; // No primary key - no references!
// Find all relations
for (DBRelation rel : relations)
{ // References
@@ -782,13 +754,11 @@ public abstract class DBRowSet extends D
if (refs[i].getTargetColumn().equals(keyColumns[0]))
{ // Found a reference on RowSet
DBRowSet rs = refs[0].getSourceColumn().getRowSet();
- if (rs.deleteReferenceRecords(refs, key, conn)==false)
- return false;
+ rs.deleteReferenceRecords(refs, key, conn);
}
}
}
// No delete this record
- return success();
}
/**
@@ -797,13 +767,12 @@ public abstract class DBRowSet extends D
* @param refs the reference columns belonging to the releation
* @param parentKey the key of the parent element
* @param conn a valid connection
- * @return true if all records could be deleted or false otherwise
*/
- protected boolean deleteReferenceRecords(DBReference[] refs, Object[] parentKey, Connection conn)
+ protected void deleteReferenceRecords(DBReference[] refs, Object[] parentKey, Connection conn)
{
// Key length and reference length must match
if (refs.length!=parentKey.length)
- return error(DBErrors.RecordInvalidKey);
+ throw new EmpireException(DBErrors.RecordInvalidKey);
// Rowset
DBColumn[] keyColumns = getKeyColumns();
if (keyColumns==null || keyColumns.length==0)
@@ -811,8 +780,7 @@ public abstract class DBRowSet extends D
DBCommand cmd = db.createCommand();
for (int i=0; i<parentKey.length; i++)
cmd.where(refs[i].getSourceColumn().is(parentKey[i]));
- if (db.executeSQL(cmd.getDelete((DBTable)this), conn)<0)
- return error(db);
+ db.executeSQL(cmd.getDelete((DBTable)this), conn);
}
else
{ // Query all keys
@@ -828,12 +796,9 @@ public abstract class DBRowSet extends D
for (Object[] recKey : recKeys)
{
log.info("Deleting Record " + StringUtils.valueOf(recKey) + " from table " + getName());
- if (deleteRecord(recKey, conn)==false)
- return false;
+ deleteRecord(recKey, conn);
}
}
- // Done
- return success();
}
}
Modified: incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBSQLScript.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBSQLScript.java?rev=1088417&r1=1088416&r2=1088417&view=diff
==============================================================================
--- incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBSQLScript.java (original)
+++ incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBSQLScript.java Sun Apr 3 21:12:14 2011
@@ -23,7 +23,7 @@ import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
-import org.apache.empire.commons.ErrorObject;
+import org.apache.empire.EmpireException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -34,7 +34,7 @@ import org.slf4j.LoggerFactory;
* The class is used for obtaining and executing DDL commands supplied
* by the database driver (@see {@link DBDatabaseDriver#getDDLScript(DBCmdType, DBObject, DBSQLScript)})
*/
-public class DBSQLScript extends ErrorObject implements Iterable<String>
+public class DBSQLScript implements Iterable<String>
{
// Logger
private static final Logger log = LoggerFactory.getLogger(DBSQLScript.class);
@@ -49,10 +49,9 @@ public class DBSQLScript extends ErrorOb
* @param sql the statement
* @return true if successful
*/
- public boolean addStmt(String sql)
+ public void addStmt(String sql)
{
sqlCmdList.add(sql);
- return success();
}
/**
@@ -61,13 +60,11 @@ public class DBSQLScript extends ErrorOb
* @param sql the statement
* @return true if successful
*/
- public final boolean addStmt(StringBuilder sql)
+ public final void addStmt(StringBuilder sql)
{
- if (!addStmt(sql.toString()))
- return false;
+ addStmt(sql.toString());
// Clear Builder
sql.setLength(0);
- return true;
}
/**
@@ -102,9 +99,8 @@ public class DBSQLScript extends ErrorOb
* @param driver the driver used for statement execution
* @param conn the connection
* @param ignoreErrors true if errors should be ignored
- * @return true if the script has been run successful or false otherwise
*/
- public boolean run(DBDatabaseDriver driver, Connection conn, boolean ignoreErrors)
+ public void run(DBDatabaseDriver driver, Connection conn, boolean ignoreErrors)
{
log.debug("Running script containing " + String.valueOf(getCount()) + " statements.");
for(String stmt : sqlCmdList)
@@ -117,13 +113,12 @@ public class DBSQLScript extends ErrorOb
// SQLException
log.error(e.toString(), e);
if (ignoreErrors==false)
- return error(DBErrors.SQLException, e);
+ throw new EmpireException(DBErrors.SQLException, e);
// continue
log.debug("Ignoring error. Continuing with script...");
}
}
log.debug("Script completed.");
- return success();
}
/**
Modified: incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBTable.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBTable.java?rev=1088417&r1=1088416&r2=1088417&view=diff
==============================================================================
--- incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBTable.java (original)
+++ incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBTable.java Sun Apr 3 21:12:14 2011
@@ -19,16 +19,17 @@
package org.apache.empire.db;
// java
-import org.apache.empire.commons.Errors;
-import org.apache.empire.data.DataMode;
-import org.apache.empire.data.DataType;
-
import java.lang.reflect.Field;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.empire.EmpireException;
+import org.apache.empire.commons.Errors;
+import org.apache.empire.data.DataMode;
+import org.apache.empire.data.DataType;
+
/**
* This class represent one table of the database.
@@ -170,9 +171,9 @@ public class DBTable extends DBRowSet im
protected boolean addColumn(DBTableColumn column)
{ // find column by name
if (column==null || column.getRowSet()!=this)
- return error(Errors.InvalidArg, column, "column");
+ throw new EmpireException(Errors.InvalidArg, column, "column");
if (columns.contains(column) == true)
- return error(Errors.ItemExists, column.getName());
+ throw new EmpireException(Errors.ItemExists, column.getName());
// add now
columns.add(column);
return true;
@@ -270,11 +271,11 @@ public class DBTable extends DBRowSet im
public boolean setPrimaryKey(DBColumn[] columns)
{
if (columns==null || columns.length==0)
- return error(Errors.InvalidArg, columns, "columns");
+ throw new EmpireException(Errors.InvalidArg, columns, "columns");
// All columns must belong to this table
for (int i=0; i<columns.length; i++)
if (columns[i].getRowSet()!=this)
- return error(Errors.InvalidArg, columns[i].getFullName(), "columns");
+ throw new EmpireException(Errors.InvalidArg, columns[i].getFullName(), "columns");
// Set primary Key now
primaryKey = new DBIndex(name + "_PK", DBIndex.PRIMARYKEY, columns);
return true;
@@ -325,7 +326,7 @@ public class DBTable extends DBRowSet im
public boolean addIndex(String indexName, boolean unique, DBColumn[] indexColumns)
{
if (indexName==null || indexColumns==null || indexColumns.length==0)
- return error(Errors.InvalidArg, null, "name|columns");
+ throw new EmpireException(Errors.InvalidArg, null, "name|columns");
// add Index now
indexes.add(new DBIndex(indexName, (unique) ? DBIndex.UNIQUE : DBIndex.STANDARD, indexColumns));
return true;
@@ -378,14 +379,12 @@ public class DBTable extends DBRowSet im
* @param rec the DBRecord object. contains all fields and the field properties
* @param conn a valid connection to the database.
*
- * @return true if successful
*/
@Override
- public boolean createRecord(DBRecord rec, Connection conn)
+ public void createRecord(DBRecord rec, Connection conn)
{
// Inititialisierung
- if (!prepareInitRecord(rec, DBRecord.REC_NEW, null))
- return false;
+ prepareInitRecord(rec, DBRecord.REC_NEW, null);
// Set Defaults
int count = columns.size();
for (int i = 0; i < count; i++)
@@ -396,7 +395,7 @@ public class DBTable extends DBRowSet im
rec.modifyValue(i, value);
}
// Init
- return completeInitRecord(rec);
+ completeInitRecord(rec);
}
/**
@@ -429,44 +428,42 @@ public class DBTable extends DBRowSet im
* @return true if successful
*/
@Override
- public boolean deleteRecord(Object[] key, Connection conn)
+ public void deleteRecord(Object[] key, Connection conn)
{
// Check Primary key
if (primaryKey == null )
- return error(DBErrors.NoPrimaryKey, getName());
+ throw new EmpireException(DBErrors.NoPrimaryKey, getName());
// Check Columns
DBColumn[] keyColumns = primaryKey.getColumns();
if (key == null || key.length != keyColumns.length)
- return error(Errors.InvalidArg, key); // Invalid Argument
+ throw new EmpireException(Errors.InvalidArg, key); // Invalid Argument
// Delete References
- if (isCascadeDelete() && deleteAllReferences(key, conn)==false)
- return false; // Error deleting referenced records
+ if (isCascadeDelete()){
+ deleteAllReferences(key, conn);
+ }
// Build SQL-Statement
DBCommand cmd = db.createCommand();
// Set key constraints
- if (!setKeyConstraints(cmd, key))
- return false;
+ setKeyConstraints(cmd, key);
// Perform delete
String sqlCmd = cmd.getDelete(this);
int affected = db.executeSQL(sqlCmd, cmd.getParamValues(), conn);
if (affected < 0)
{ // Delete Failed
- return error(db);
+ throw new EmpireException(DBErrors.RecordDeleteFailed, name);
}
else if (affected == 0)
{ // Record not found
- return error(DBErrors.RecordDeleteFailed, name);
+ throw new EmpireException(DBErrors.RecordDeleteFailed, name);
}
else if (affected > 1)
{ // Multiple Records affected
- return error(DBErrors.RecordUpdateInvalid, name);
+ throw new EmpireException(DBErrors.RecordUpdateInvalid, name);
}
- // success
- return success();
}
}
\ No newline at end of file
Modified: incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBTableColumn.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBTableColumn.java?rev=1088417&r1=1088416&r2=1088417&view=diff
==============================================================================
--- incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBTableColumn.java (original)
+++ incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBTableColumn.java Sun Apr 3 21:12:14 2011
@@ -23,6 +23,7 @@ import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
+import org.apache.empire.EmpireException;
import org.apache.empire.commons.Attributes;
import org.apache.empire.commons.Errors;
import org.apache.empire.data.DataMode;
@@ -210,7 +211,7 @@ public class DBTableColumn extends DBCol
if (isAutoGenerated())
{
// cannot change auto-generated columns
- error(Errors.NoAccess);
+ throw new EmpireException(Errors.NoAccess);
}
else
{
@@ -258,14 +259,13 @@ public class DBTableColumn extends DBCol
* data type the value will be checked for compatibility.
*
* @param value the checked to check for validity
- * @return true if the value is valid or false otherwise.
*/
@Override
- public boolean checkValue(Object value)
+ public void checkValue(Object value)
{
// Check for NULL
if (isRequired() && (value == null || value.toString().length() < 1))
- return error(DBErrors.FieldNotNull, getName());
+ throw new EmpireException(DBErrors.FieldNotNull, getName());
// Is value valid
switch (type)
{
@@ -282,7 +282,7 @@ public class DBTableColumn extends DBCol
} catch (ParseException e)
{ // Error
log.error("checkValue exception: " + e.toString() + " column=" + getName() + " value=" + value);
- return error(DBErrors.FieldInvalidDateFormat, getName());
+ throw new EmpireException(DBErrors.FieldInvalidDateFormat, getName());
}
}
break;
@@ -299,7 +299,7 @@ public class DBTableColumn extends DBCol
} catch (NumberFormatException nfe)
{
log.error("checkValue exception: " + nfe.toString() + " column=" + getName() + " value=" + value);
- return error(DBErrors.FieldNotNumeric, getName());
+ throw new EmpireException(DBErrors.FieldNotNumeric, getName());
}
}
break;
@@ -315,7 +315,7 @@ public class DBTableColumn extends DBCol
} catch (NumberFormatException nfe)
{
log.error("checkValue exception: " + nfe.toString() + " column=" + getName() + " value=" + String.valueOf(value));
- return error(DBErrors.FieldNotNumeric, getName());
+ throw new EmpireException(DBErrors.FieldNotNumeric, getName());
}
}
break;
@@ -323,7 +323,7 @@ public class DBTableColumn extends DBCol
case TEXT:
case CHAR:
if (value!=null && value.toString().length() > size)
- return error(DBErrors.FieldValueTooLong, getName(), (int)size);
+ throw new EmpireException(DBErrors.FieldValueTooLong, getName(), (int)size);
break;
default:
@@ -332,7 +332,6 @@ public class DBTableColumn extends DBCol
break;
}
- return success();
}
/**
Modified: incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBView.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBView.java?rev=1088417&r1=1088416&r2=1088417&view=diff
==============================================================================
--- incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBView.java (original)
+++ incubator/empire-db/branches/EMPIREDB-99/empire-db/src/main/java/org/apache/empire/db/DBView.java Sun Apr 3 21:12:14 2011
@@ -21,6 +21,7 @@ package org.apache.empire.db;
import java.sql.Connection;
import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.empire.EmpireException;
import org.apache.empire.commons.Errors;
import org.apache.empire.commons.Options;
import org.apache.empire.data.DataType;
@@ -117,11 +118,11 @@ public abstract class DBView extends DBR
}
@Override
- public boolean checkValue(Object value)
+ public void checkValue(Object value)
{
if (updateColumn==null)
- return true;
- return updateColumn.checkValue(value);
+ return;
+ updateColumn.checkValue(value);
}
@Override
@@ -286,9 +287,9 @@ public abstract class DBView extends DBR
protected boolean addColumn(DBViewColumn col)
{ // find column by name
if (col == null || col.getRowSet() != this)
- return error(Errors.InvalidArg, col, "col");
+ throw new EmpireException(Errors.InvalidArg, col, "col");
if (columns.contains(col) == true)
- return error(Errors.ItemExists, col.getName());
+ throw new EmpireException(Errors.ItemExists, col.getName());
// add now
columns.add(col);
return true;
@@ -377,12 +378,12 @@ public abstract class DBView extends DBR
}
@Override
- public boolean updateRecord(DBRecord rec, Connection conn)
+ public void updateRecord(DBRecord rec, Connection conn)
{
if (updateable==false)
- return error(Errors.NotSupported, "updateRecord");
+ throw new EmpireException(Errors.NotSupported, "updateRecord");
// Update the record
- return super.updateRecord(rec, conn);
+ super.updateRecord(rec, conn);
}
/*
@@ -391,9 +392,9 @@ public abstract class DBView extends DBR
* @see org.apache.empire.db.DBRowSet#addRecord(org.apache.empire.db.DBRecord, java.sql.Connection)
*/
@Override
- public boolean createRecord(DBRecord rec, Connection conn)
+ public void createRecord(DBRecord rec, Connection conn)
{
- return error(Errors.NotSupported, "addRecord");
+ throw new EmpireException(Errors.NotSupported, "addRecord");
}
/*
@@ -402,8 +403,8 @@ public abstract class DBView extends DBR
* @see org.apache.empire.db.DBRowSet#deleteRecord(java.lang.Object[], java.sql.Connection, boolean)
*/
@Override
- public boolean deleteRecord(Object[] keys, Connection conn)
+ public void deleteRecord(Object[] keys, Connection conn)
{
- return error(Errors.NotSupported, "deleteRecord");
+ throw new EmpireException(Errors.NotSupported, "deleteRecord");
}
}
\ No newline at end of file