Changeset 282

Show
Ignore:
Timestamp:
02/11/08 12:15:56 (1 year ago)
Author:
chris
Message:

Fix capture of changes after an INSERT with fixed ID, where LAST_INSERT_ID() does not return the correct ID.

Files:

Legend:

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

    r280 r282  
    4545    private static final Logger m_LOG = Logger.getLogger(SqlChange.class); 
    4646    private String m_PrimaryKeyField = "ID"; 
     47    private String changeQuery = null; 
    4748         
    4849        public SqlChange(String username, String database,  
     
    206207                    { 
    207208                    throw new DatabaseException("Failed to capture new values " + 
    208                     "of rows. You may not have an ID column in the table.", e); 
     209                    "of rows. You may not have an ID column in the table.", e, 
     210                    sb.toString()); 
    209211                    } 
    210212                    else 
     
    212214                    throw new DatabaseException("Failed to capture old values " + 
    213215                                "of rows. You may have an error in your conditions: " + 
    214                                 conditions, e); 
     216                                conditions, e, sb.toString()); 
    215217                    } 
    216218                } 
     
    331333                        } 
    332334                         
    333                         this.conditions = newConditions.toString(); 
     335                        String newCond = newConditions.toString(); 
     336                        if (newCond.length() > 0) 
     337                        { 
     338                                this.conditions = newCond; 
     339                        } 
     340                        else if (type != SqlChange.DELETE) 
     341                        { 
     342                                throw new DatabaseException("No records created or updated",  
     343                                        changeQuery); 
     344                        } 
    334345        } 
    335346        catch (SQLException e) 
     
    391402                if (type == INSERT) 
    392403                { 
    393                     try 
    394                     { 
    395                         PreparedStatement stmt = prepareAndLogError( 
    396                                         "SELECT LAST_INSERT_ID()"); 
    397                         ResultSet rs = executeQueryAndLogError(stmt); 
    398                         rs.next(); 
    399                         insertedRowId = rs.getInt(1); 
    400                         rs.close(); 
    401                         stmt.close(); 
    402                     } 
    403                     catch (SQLException e) 
    404                     { 
    405                         throw new DatabaseException("Failed to get the ID " + 
    406                                         "of the last inserted row", e); 
    407                     } 
    408                      
     404                        if (fields.containsKey("ID")) 
     405                        { 
     406                                insertedRowId = Integer.parseInt(fields.get("ID").toString()); 
     407                        } 
     408                        else 
     409                        { 
     410                            try 
     411                            { 
     412                                PreparedStatement stmt = prepareAndLogError( 
     413                                                "SELECT LAST_INSERT_ID()"); 
     414                                ResultSet rs = executeQueryAndLogError(stmt); 
     415                                rs.next(); 
     416                                insertedRowId = rs.getInt(1); 
     417                                rs.close(); 
     418                                stmt.close(); 
     419                            } 
     420                            catch (SQLException e) 
     421                            { 
     422                                throw new DatabaseException("Failed to get the ID " + 
     423                                                "of the last inserted row", e); 
     424                            } 
     425                        } 
     426                         
    409427                        conditions = m_PrimaryKeyField + " = " + insertedRowId; 
    410428 
     
    518536        try 
    519537        { 
    520                 PreparedStatement stmt = prepareAndLogError(sb.toString()); 
     538                changeQuery = sb.toString(); 
     539                PreparedStatement stmt = prepareAndLogError(changeQuery); 
    521540                         
    522541                if (type == INSERT || type == UPDATE) 
     
    549568        catch (SQLException e)  
    550569        { 
     570                changeQuery = null; 
    551571            throw new DatabaseException("Failed to execute " + 
    552572                "the requested operation. Please check your field names " + 
     
    558578                        captureNewValues(); 
    559579                } 
    560                  
     580 
     581        changeQuery = null; 
     582 
    561583        try 
    562584        {