Changeset 275

Show
Ignore:
Timestamp:
01/23/08 18:08:32 (1 year ago)
Author:
chris
Message:

Correctly track changes to NULL row values (update from and delete from).

Add ability to (partially) reload a saved SqlChange?.

Files:

Legend:

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

    r273 r275  
    9090        { 
    9191                ChangedRow reverse = new ChangedRow(m_UniqueID); 
    92                 for (Iterator i = iterator(); i.hasNext(); ) { 
     92                for (Iterator i = iterator(); i.hasNext(); )  
     93                { 
    9394                        ChangedValue orig = (ChangedValue)( i.next() ); 
    9495                        reverse.put(orig.reverse()); 
  • lex/trunk/src/com/qwirx/db/sql/SqlChange.java

    r273 r275  
    5959        } 
    6060 
     61        public static SqlChange load(int id, Connection conn, String username) 
     62        throws SQLException 
     63        { 
     64                PreparedStatement stmt = conn.prepareStatement("SELECT " + 
     65                        "DB_Name, Cmd_Type, Table_Name FROM change_log " + 
     66                        "WHERE id = " + id); 
     67                ResultSet rs = stmt.executeQuery(); 
     68                return new SqlChange(username, 
     69                        rs.getString(1), 
     70                        lookup(rs.getString(2)),  
     71                        rs.getString(3), null, conn); 
     72        } 
     73         
    6174        final static class Type implements ChangeType 
    6275        { 
     
    7790                UPDATE = new Type("UPDATE"), 
    7891                DELETE = new Type("DELETE"); 
     92         
     93        public static final Type lookup(String typeName) 
     94        { 
     95                if (typeName.equals("INSERT")) 
     96                { 
     97                        return INSERT; 
     98                } 
     99                if (typeName.equals("UPDATE")) 
     100                { 
     101                        return UPDATE; 
     102                } 
     103                if (typeName.equals("DELETE")) 
     104                { 
     105                        return DELETE; 
     106                } 
     107                 
     108                throw new IllegalArgumentException("Unknown change type " + typeName); 
     109        } 
    79110         
    80111        public ChangeType getType() { return type; } 
     
    256287                            } 
    257288 
    258                             stmt = prepareAndLogError("DELETE FROM changed_values " + 
    259                                 "WHERE Row_ID = ? AND ((Old_Value = New_Value) OR " + 
    260                                 "(Old_Value IS NULL AND New_Value IS NULL))"); 
    261  
    262                             stmt.setInt(1, logRowEntryId); 
    263                             stmt.executeUpdate(); 
     289                            // It's never safe to delete row logs when storing old values, 
     290                            // as either it's an UPDATE (where we don't yet know what the 
     291                            // new values will be, and we need the rows to exist) or a  
     292                            // DELETE (where we want to know the original value, even 
     293                            // if it's NULL). 
     294                             
     295                            if (storeAsNewValue) 
     296                            { 
     297                                    stmt = prepareAndLogError("DELETE FROM changed_values " + 
     298                                        "WHERE Row_ID = ? AND ((Old_Value = New_Value) OR " + 
     299                                        "(Old_Value IS NULL AND New_Value IS NULL))"); 
     300                                    stmt.setInt(1, logRowEntryId); 
     301                                    stmt.executeUpdate(); 
     302                            } 
    264303                        } 
    265304 
  • lex/trunk/src/com/qwirx/db/sql/SqlDatabase.java

    r270 r275  
    190190                return new SqlChange(username, database,  
    191191                        (SqlChange.Type)type, table, (String)conditions, m_Connection); 
     192        } 
     193         
     194        public SqlChange loadChange(int id) throws DatabaseException 
     195        { 
     196                try 
     197                { 
     198                        return SqlChange.load(id, m_Connection, username); 
     199                } 
     200                catch (SQLException e) 
     201                { 
     202                        throw new DatabaseException("Failed to reload SqlChange record", e); 
     203                } 
    192204        } 
    193205