From 83b3a92c9d6b973214edd303a45de9181b141145 Mon Sep 17 00:00:00 2001 From: rowanbeentje Date: Sat, 3 Oct 2009 14:06:59 +0000 Subject: - Ensure that blob and text fields which are loaded on-demand never have placeholder values written to database. This addresses the data corruption part of Issue #423. --- Source/TableContent.m | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) (limited to 'Source') diff --git a/Source/TableContent.m b/Source/TableContent.m index f5b0fa47..6537f574 100644 --- a/Source/TableContent.m +++ b/Source/TableContent.m @@ -1397,6 +1397,7 @@ id rowObject; NSMutableString *rowValue = [NSMutableString string]; NSString *currentTime = [[NSDate date] descriptionWithCalendarFormat:@"%H:%M:%S" timeZone:nil locale:nil]; + BOOL prefsLoadBlobsAsNeeded = [prefs boolForKey:@"LoadBlobsAsNeeded"]; int i; if ( !isEditingRow || currentlyEditingRow == -1) { @@ -1418,11 +1419,18 @@ for ( i = 0 ; i < [dataColumns count] ; i++ ) { rowObject = [NSArrayObjectAtIndex(tableValues, currentlyEditingRow) objectAtIndex:i]; + // Add (not loaded) placeholders directly for easy comparsion when added + if (prefsLoadBlobsAsNeeded && !isEditingNewRow + && [rowObject isEqualToString:NSLocalizedString(@"(not loaded)", @"value shown for hidden blob and text fields")]) + { + [fieldValues addObject:[NSString stringWithString:rowObject]]; + continue; + // Catch CURRENT_TIMESTAMP automatic updates - if the row is new and the cell value matches // the default value, or if the cell hasn't changed, update the current timestamp. - if ([[NSArrayObjectAtIndex(dataColumns, i) objectForKey:@"onupdatetimestamp"] intValue] - && ( (isEditingNewRow && [rowObject isEqualTo:[NSArrayObjectAtIndex(dataColumns, i) objectForKey:@"default"]]) - || (!isEditingNewRow && [rowObject isEqualTo:NSArrayObjectAtIndex(oldRow, i)]))) + } else if ([[NSArrayObjectAtIndex(dataColumns, i) objectForKey:@"onupdatetimestamp"] intValue] + && ( (isEditingNewRow && [rowObject isEqualTo:[NSArrayObjectAtIndex(dataColumns, i) objectForKey:@"default"]]) + || (!isEditingNewRow && [rowObject isEqualTo:NSArrayObjectAtIndex(oldRow, i)]))) { [rowValue setString:@"CURRENT_TIMESTAMP"]; @@ -1459,18 +1467,23 @@ [fieldValues addObject:[NSString stringWithString:rowValue]]; } - // Use INSERT syntax when creating new rows + // Use INSERT syntax when creating new rows - no need to do (not loaded) checking, as all values have been entered if ( isEditingNewRow ) { queryString = [NSString stringWithFormat:@"INSERT INTO %@ (%@) VALUES (%@)", [selectedTable backtickQuotedString], [[tableDataInstance columnNames] componentsJoinedAndBacktickQuoted], [fieldValues componentsJoinedByString:@","]]; // Use UPDATE syntax otherwise } else { + BOOL firstCellOutput = NO; queryString = [NSMutableString stringWithFormat:@"UPDATE %@ SET ", [selectedTable backtickQuotedString]]; for ( i = 0 ; i < [dataColumns count] ; i++ ) { - if ( i > 0 ) { - [queryString appendString:@", "]; - } + + // If data column loading is deferred and the value is the not loaded string, skip this cell + if (prefsLoadBlobsAsNeeded && [[fieldValues objectAtIndex:i] isEqualToString:NSLocalizedString(@"(not loaded)", @"value shown for hidden blob and text fields")]) continue; + + if (firstCellOutput) [queryString appendString:@", "]; + else firstCellOutput = YES; + [queryString appendString:[NSString stringWithFormat:@"%@=%@", [[NSArrayObjectAtIndex(dataColumns, i) objectForKey:@"name"] backtickQuotedString], [fieldValues objectAtIndex:i]]]; } -- cgit v1.2.3