[Opensource] deleteAll() method of DBObject

liuhey liuhey at neusoft.com
Fri Dec 5 01:10:29 PST 2003


When I read the code of  com.jcorporate.expresso.core.dbobj.DBObject.java, I found that the deleteAll() method  was modified.
I don't know why it searches the records to be deleted firstly and then deletes them.
Why not use the sql " delete  from table where .."(the earlier version) directly?
And the new version maybe have a bug. Because it ignores the customWhereClause property of the DBObject.
So if i use the customWhereClause to set the search condition. It will delete all the records of the table.



the earlier version:
 public synchronized void deleteAll()
            throws DBException {
        DBConnectionPool myPool = DBConnectionPool.getInstance(this.getDataContext());
        DBConnection localConnection = this.getLocalConnection();

        if (recordSet == null) {
            recordSet = new ArrayList();
        } else {
            recordSet.clear();
        }

        myUpdates = null;

        FastStringBuffer myStatement = new FastStringBuffer(64);
        myStatement.append("DELETE FROM ");
        myStatement.append(this.getJDBCMetaData().getTargetTable());

        if (customWhereClause != null) {
            myStatement.append(customWhereClause);
            customWhereClause = null;
        } else {
            myStatement.append(buildWhereClause(true));
        }

        DBConnection myConnection = null;

        try {
            if (localConnection != null) {
                myConnection = localConnection;
            } else {
                myConnection = myPool.getConnection(this.myClassName);
            }

            myConnection.executeUpdate(myStatement.toString());
            if (this.isCached()) {
                CacheManager.getInstance().clear(this.getDataContext(), this.myClassName);
            }
        } catch (CacheException ce) {
            log.error("Error clearing cache for deleteAll().", ce);
        } catch (DBException de) {
            throw de;
        } finally {
            if (localConnection == null) {
                myPool.release(myConnection);
            }
        }
    } /* deleteAll() */

New version:

 public synchronized void deleteAll()
        throws DBException
    {
 

        //Build an object that we're going to query with.
        DBObject testObject = this.getThisDBObj();
        DataObjectMetaData metadata = this.getMetaData();
        testObject.setDataContext(this.getDataContext());
        for (Iterator i = this.getMetaData().getFieldListArray().iterator();
             i.hasNext(); ) {
            String fieldName = (String) i.next();
            DataFieldMetaData fieldMetadata = metadata.getFieldMetadata(fieldName);
            if (fieldMetadata.isLongObjectType() || fieldMetadata.isVirtual()) {
                if (log.isDebugEnabled()) {
                    log.debug("Skipping field type" + fieldName);
                }
            } else {
                testObject.setField(fieldName, this.getField(fieldName));
            }
        }
        if (this.getLocalConnection() != null) {
            testObject.setConnection(this.getLocalConnection());
        }
        testObject.setMaxRecords(100);

        //
        //Iterate for each 100 items.  So we don't load more records at once
        //than memory can handle
        //
        ArrayList al = new ArrayList(100);
        do {
            al = testObject.searchAndRetrieveList();
            for (Iterator i = al.iterator(); i.hasNext();) {
                DBObject oneObject = (DBObject)i.next();
                if (this.getLocalConnection() != null) {
                    oneObject.setConnection(this.getLocalConnection());
                }
                oneObject.delete();
            }

            al.clear();
        } while (al.size() >= 100);


     }



-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.jcorporate.com/pipermail/opensource/attachments/20031205/dcc73f65/attachment-0002.htm


More information about the Opensource mailing list