Changeset 278

Show
Ignore:
Timestamp:
01/25/08 18:07:19 (1 year ago)
Author:
chris
Message:

Fix loading a SqlChange? from the database.

Fix reversing INSERT statements loaded from database.

Add an undo() method which executes all the reverse changes.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • lex/trunk/src/com/qwirx/db/sql/SqlChange.java

    r275 r278  
    6666                        "WHERE id = " + id); 
    6767                ResultSet rs = stmt.executeQuery(); 
    68                 return new SqlChange(username, 
    69                         rs.getString(1), 
    70                         lookup(rs.getString(2)),  
    71                         rs.getString(3), null, conn); 
     68                if (!rs.next()) 
     69                { 
     70                        throw new IllegalArgumentException("No such changelog entry: " +  
     71                                id); 
     72                } 
     73                String databaseName = rs.getString(1); 
     74                String commandType  = rs.getString(2); 
     75                String tableName    = rs.getString(3); 
     76                SqlChange ch = new SqlChange(username, databaseName, 
     77                        lookup(commandType), tableName, null, conn); 
     78                ch.id = id; 
     79                return ch; 
    7280        } 
    7381         
     
    689697        public SqlChange[] reverse() throws DatabaseException 
    690698        { 
    691                 if (type == SqlChange.INSERT) 
    692                 { 
    693                         return new SqlChange[]{ 
    694                                 new SqlChange(username, database, SqlChange.DELETE, 
    695                                                 table, conditions, conn) 
    696                         }; 
    697                 } 
    698                 else if (type == SqlChange.UPDATE || type == SqlChange.DELETE) 
     699                if (type == SqlChange.INSERT ||  
     700                        type == SqlChange.UPDATE ||  
     701                        type == SqlChange.DELETE) 
    699702                { 
    700703                        List changedRows = getChangedRows(); 
     
    706709                                SqlChange rev = null; 
    707710                                 
    708                                 if (type == SqlChange.UPDATE) 
     711                                if (type == SqlChange.INSERT) 
     712                                { 
     713                                        rev = new SqlChange(username, database, SqlChange.DELETE, 
     714                                                table, "ID = " + cr.getUniqueID(), conn); 
     715                                } 
     716                                else if (type == SqlChange.UPDATE) 
    709717                                { 
    710718                                        rev = new SqlChange(username, database, SqlChange.UPDATE, 
     
    717725                                } 
    718726 
    719                                 for (Iterator j = cr.iterator(); j.hasNext();
     727                                if (rev.getType() != SqlChange.DELETE
    720728                                { 
    721                                         ChangedValue cv = (ChangedValue)j.next(); 
    722                                         rev.setObject(cv.getName(), cv.getOldValue()); 
     729                                        for (Iterator j = cr.iterator(); j.hasNext();) 
     730                                        { 
     731                                                ChangedValue cv = (ChangedValue)j.next(); 
     732                                                rev.setObject(cv.getName(), cv.getOldValue()); 
     733                                        } 
    723734                                } 
    724735                                 
     
    736747                                " cannot be reversed yet");      
    737748        } 
     749         
     750        /** 
     751         * Executes the reverse SqlChanges to undo the effects of executing() 
     752         * this change. 
     753         */ 
     754        public void undo() throws DatabaseException 
     755        { 
     756                SqlChange [] revs = reverse(); 
     757                for (int i = 0; i < revs.length; i++) 
     758                { 
     759                        revs[i].execute(); 
     760                } 
     761        } 
    738762}