### Eclipse Workspace Patch 1.0 #P castor Index: cpa/src/main/java/org/exolab/castor/jdo/engine/SQLStatementRemove.java =================================================================== --- cpa/src/main/java/org/exolab/castor/jdo/engine/SQLStatementRemove.java (revision 8172) +++ cpa/src/main/java/org/exolab/castor/jdo/engine/SQLStatementRemove.java (working copy) @@ -1,125 +0,0 @@ -/* - * Copyright 2006 Assaf Arkin, Thomas Yip, Bruce Snyder, Werner Guttmann, Ralf Joachim - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * $Id$ - */ -package org.exolab.castor.jdo.engine; - -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.SQLException; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.castor.core.util.Messages; -import org.exolab.castor.jdo.PersistenceException; -import org.exolab.castor.jdo.engine.nature.ClassDescriptorJDONature; -import org.exolab.castor.persist.spi.Identity; -import org.exolab.castor.persist.spi.PersistenceFactory; -import org.exolab.castor.persist.spi.QueryExpression; - -public final class SQLStatementRemove { - /** The Jakarta - * Commons Logging instance used for all logging. */ - private static final Log LOG = LogFactory.getLog(SQLStatementRemove.class); - - private final SQLEngine _engine; - - private final PersistenceFactory _factory; - - private final String _type; - - private final String _mapTo; - - private String _statement; - - public SQLStatementRemove(final SQLEngine engine, final PersistenceFactory factory) { - _engine = engine; - _factory = factory; - _type = engine.getDescriptor().getJavaClass().getName(); - _mapTo = new ClassDescriptorJDONature(engine.getDescriptor()).getTableName(); - - buildStatement(); - } - - private void buildStatement() { - SQLColumnInfo[] ids = _engine.getColumnInfoForIdentities(); - StringBuffer sql = new StringBuffer("DELETE FROM "); - sql.append(_factory.quoteName(_mapTo)); - sql.append(JDBCSyntax.WHERE); - for (int i = 0; i < ids.length; i++) { - if (i > 0) { sql.append(" AND "); } - sql.append(_factory.quoteName(ids[i].getName())); - sql.append(QueryExpression.OP_EQUALS); - sql.append(JDBCSyntax.PARAMETER); - } - _statement = sql.toString(); - - if (LOG.isTraceEnabled()) { - LOG.trace(Messages.format("jdo.removing", _type, _statement)); - } - } - - public Object executeStatement(final Connection conn, final Identity identity) - throws PersistenceException { - SQLColumnInfo[] ids = _engine.getColumnInfoForIdentities(); - SQLEngine extended = _engine.getExtends(); - PreparedStatement stmt = null; - - try { - stmt = conn.prepareStatement(_statement); - - if (LOG.isTraceEnabled()) { - LOG.trace(Messages.format("jdo.removing", _type, stmt.toString())); - } - - int count = 1; - // bind the identity of the preparedStatement - if (identity.size() != ids.length) { - throw new PersistenceException("Size of identity field mismatched!"); - } - - for (int i = 0; i < ids.length; i++) { - stmt.setObject(count++, ids[i].toSQL(identity.get(i))); - } - - if (LOG.isDebugEnabled()) { - LOG.debug(Messages.format("jdo.removing", _type, stmt.toString())); - } - - int result = stmt.executeUpdate(); - if (result < 1) { - throw new PersistenceException("Object to be deleted does not exist! " + identity); - } - - // Must delete record in parent table last. - // All other dependents have been deleted before. - if (extended != null) { - extended.delete(conn, identity); - } - } catch (SQLException except) { - LOG.fatal(Messages.format("jdo.deleteFatal", _type, _statement), except); - throw new PersistenceException(Messages.format("persist.nested", except), except); - } finally { - try { - if (stmt != null) { stmt.close(); } - } catch (Exception e) { - LOG.warn("Problem closing JDBC statement", e); - } - } - - return null; - } -} Index: cpa/src/main/java/org/exolab/castor/jdo/engine/SQLStatementRemove.java =================================================================== --- cpa/src/main/java/org/exolab/castor/jdo/engine/SQLStatementRemove.java (revision 0) +++ cpa/src/main/java/org/exolab/castor/jdo/engine/SQLStatementRemove.java (revision 8172) @@ -0,0 +1,252 @@ +/* + * Copyright 2006 Assaf Arkin, Thomas Yip, Bruce Snyder, Werner Guttmann, Ralf Joachim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + *$Id$ + * $Id$ + */ +package org.exolab.castor.jdo.engine; + +//Imports +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.castor.core.util.Messages; +import org.exolab.castor.jdo.PersistenceException; +import org.exolab.castor.jdo.engine.nature.ClassDescriptorJDONature; +import org.exolab.castor.persist.spi.Identity; +import org.exolab.castor.persist.spi.PersistenceFactory; +import org.exolab.castor.persist.spi.QueryExpression; + + + +/** + * SQLStatementRemove Class + * @Revision Ahmad + * + */ +public final class SQLStatementRemove { + + + /** + * The + * Jakarta Commons Logging + * + * This instance used for all logging. + * */ + + private static final Log LOG = LogFactory.getLog(SQLStatementRemove.class); + + private final SQLEngine _engine; + + private final String _mapTo; + + private String _statement; + + private final PersistenceFactory _factory; + + private final String _type; + + private final SQLColumnInfo[] _ids; + + /** + * @param engine + * @param factory + * Constructor for this class 6 May 2009 $ @author Ahmad + */ + public SQLStatementRemove(final SQLEngine engine, final PersistenceFactory factory) { + + //An instance of SQLEngine + _engine = engine; + + //An instance of PersistenceFactory + _factory = factory; + + //Get the name of entity class from descriptor + _type = engine.getDescriptor().getJavaClass().getName(); + + //Get the name of table on which entity is mapped + _mapTo = new ClassDescriptorJDONature(engine.getDescriptor()).getTableName(); + + //Get ID's list from engine provided + _ids = _engine.getColumnInfoForIdentities(); + + //build structured SQL Query and store in string + buildStatement(); + } + + /** + * @return the statement + */ + public String getStatement() { + return _statement; + } + + + /** + * @param statement the statement to set + */ + public void setStatement(String statement) { + _statement = statement; + } + + + /** + * @return the engine + */ + public SQLEngine getEngine() { + return _engine; + } + + + /** + * @return the factory + */ + public PersistenceFactory getFactory() { + return _factory; + } + + /** + * Method that builds the structured SQL query from the scratch and stores + * it in StringBuffer + */ + public void buildStatement () { + + //Construct SQL string and stores in StringBuffer + StringBuffer sql = new StringBuffer("DELETE FROM "); + sql.append(_factory.quoteName(_mapTo)); + sql.append(JDBCSyntax.WHERE); + + //Loop over id's + for (int i = 0; i < _ids.length; i++) { + + //Building query using factory features + if (i > 0) { sql.append(" AND "); } + sql.append(_factory.quoteName(_ids[i].getName())); + sql.append(QueryExpression.OP_EQUALS); + sql.append(JDBCSyntax.PARAMETER); + } + + //Stores the constructed string in the classfield + _statement = sql.toString(); + + //Append info to logs + if (LOG.isTraceEnabled()) { + LOG.trace(Messages.format("jdo.removing", _type, _statement)); + } + } //END buildStatement + + /** + * Method to execute SQL Statement using prepared statement and the parameters + * @param conn + * @param identity + * @return + * @throws PersistenceException + * 6 May 2009 + */ + public Object executeStatement(final Connection conn, final Identity identity) + throws PersistenceException { + + //SQLColumnInfo array to hold the list of ids + SQLColumnInfo[] ids = _engine.getColumnInfoForIdentities(); + + + //Declare PreparedStatement object + PreparedStatement stmt = null; + try { + + //Get PreparedStatement and then bind it with parameters list for execution + stmt = getPreparedStatement (conn, identity); + + //execute the prepared statement and storing the return value + int result = stmt.executeUpdate(); + + //Throw exception if executeUpdate returns < 1 + if (result < 1) { + throw new PersistenceException("Object to be deleted does not exist! " + identity); + } + + + } catch (SQLException except) { + LOG.fatal(Messages.format("jdo.deleteFatal", _type, _statement), except); + throw new PersistenceException(Messages.format("persist.nested", except), except); + } finally { + try { + //Close the preparedstatement object if it is not null + if (stmt != null) { + + stmt.close(); + } + } catch (Exception e) { + LOG.warn("Problem closing JDBC statement", e); + } + } + + return null; + + } //End executeStatement + + /** + * Method to prepare sql statement and binds parameter to it + * + * @param conn + * @param identity + * @return + * @throws PersistenceException 6 May 2009 + */ + + public PreparedStatement getPreparedStatement (final Connection conn, final Identity identity) + throws PersistenceException { + + //Declare PreparedStatement Object + PreparedStatement stmt = null; + + + try { + stmt = conn.prepareStatement(_statement); + + if (LOG.isTraceEnabled()) { + LOG.trace(Messages.format("jdo.removing", _type, stmt.toString())); + } + + //Counter declaration + int count = 1; + + //Loop through the ids and add positional arguments to the statement object + for (int i = 0; i < _ids.length; i++) { + + //Add positional Arguments to prepared statement + stmt.setObject(count++, _ids[i].toSQL(identity.get(i))); + } + + //Append info to logs + if (LOG.isDebugEnabled()) { + LOG.debug(Messages.format("jdo.removing", _type, stmt.toString())); + } + + } catch (SQLException except) { + LOG.fatal(Messages.format("jdo.deleteFatal", _type, _statement), except); + throw new PersistenceException(Messages.format("persist.nested", except), except); + } + + + //Return statement object + return stmt; + + } //END preparedStatement + + +} Index: cpa/src/main/java/org/exolab/castor/persist/ClassMolder.java =================================================================== --- cpa/src/main/java/org/exolab/castor/persist/ClassMolder.java (revision 8172) +++ cpa/src/main/java/org/exolab/castor/persist/ClassMolder.java (working copy) @@ -937,14 +937,27 @@ _persistence.delete(tx.getConnection(oid.getMolder().getLockEngine()), ids); + // Must delete record in parent table last. + // All other dependents have been deleted before. + ClassMolder extended = this.getExtends(); + Connection con = tx.getConnection(oid.getMolder().getLockEngine()); + + while (extended != null) { + + extended._persistence.delete(con, ids); + extended = extended._extends; + + } + // All field along the extend path will be deleted by transaction // However, everything off the path must be deleted by ClassMolder. Vector extendPath = new Vector(); ClassMolder base = this; while (base != null) { - extendPath.add(base); + extendPath.add(base); base = base._extends; + } base = _depends; @@ -1212,7 +1225,7 @@ public ClassMolder getExtends() { return _extends; } - + /** * Get the depends class' ClassMolder. */