Skip to content

Commit

Permalink
Setting large auto_increment values (> 1000) could be misinterpreted …
Browse files Browse the repository at this point in the history
…as decimal values in some cases (fixes #2130)
  • Loading branch information
dmoagx committed Jun 8, 2015
1 parent be4532c commit fb54e89
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 22 deletions.
14 changes: 7 additions & 7 deletions Interfaces/English.lproj/DBView.xib
Expand Up @@ -15857,7 +15857,7 @@ AAEAAQAAAT0AAwAAAAEAAgAAAVIAAwAAAAEAAQAAAVMAAwAAAAIAAQABAAAAAA</bytes>
</object>
<object class="IBConnectionRecord">
<object class="IBActionConnection" key="connection">
<string key="label">resetAutoIncrementValueWasEdited:</string>
<string key="label">tableRowAutoIncrementWasEdited:</string>
<reference key="source" ref="269913528"/>
<reference key="destination" ref="735808692"/>
</object>
Expand Down Expand Up @@ -29569,7 +29569,7 @@ AAEAAQAAAT0AAwAAAAEAAgAAAVIAAwAAAAEAAQAAAVMAAwAAAAIAAQABAAAAAA</bytes>
<dictionary class="NSMutableDictionary" key="actions">
<string key="reloadTable:">id</string>
<string key="resetAutoIncrement:">id</string>
<string key="resetAutoIncrementValueWasEdited:">id</string>
<string key="tableRowAutoIncrementWasEdited:">id</string>
<string key="updateTableCollation:">id</string>
<string key="updateTableEncoding:">id</string>
<string key="updateTableType:">id</string>
Expand All @@ -29583,8 +29583,8 @@ AAEAAQAAAT0AAwAAAAEAAgAAAVIAAwAAAAEAAQAAAVMAAwAAAAIAAQABAAAAAA</bytes>
<string key="name">resetAutoIncrement:</string>
<string key="candidateClassName">id</string>
</object>
<object class="IBActionInfo" key="resetAutoIncrementValueWasEdited:">
<string key="name">resetAutoIncrementValueWasEdited:</string>
<object class="IBActionInfo" key="tableRowAutoIncrementWasEdited:">
<string key="name">tableRowAutoIncrementWasEdited:</string>
<string key="candidateClassName">id</string>
</object>
<object class="IBActionInfo" key="updateTableCollation:">
Expand Down Expand Up @@ -29719,7 +29719,7 @@ AAEAAQAAAT0AAwAAAAEAAgAAAVIAAwAAAAEAAQAAAVMAAwAAAAIAAQABAAAAAA</bytes>
<dictionary class="NSMutableDictionary" key="actions">
<string key="reloadTable:">id</string>
<string key="resetAutoIncrement:">id</string>
<string key="resetAutoIncrementValueWasEdited:">id</string>
<string key="tableRowAutoIncrementWasEdited:">id</string>
<string key="updateTableCollation:">id</string>
<string key="updateTableEncoding:">id</string>
<string key="updateTableType:">id</string>
Expand All @@ -29733,8 +29733,8 @@ AAEAAQAAAT0AAwAAAAEAAgAAAVIAAwAAAAEAAQAAAVMAAwAAAAIAAQABAAAAAA</bytes>
<string key="name">resetAutoIncrement:</string>
<string key="candidateClassName">id</string>
</object>
<object class="IBActionInfo" key="resetAutoIncrementValueWasEdited:">
<string key="name">resetAutoIncrementValueWasEdited:</string>
<object class="IBActionInfo" key="tableRowAutoIncrementWasEdited:">
<string key="name">tableRowAutoIncrementWasEdited:</string>
<string key="candidateClassName">id</string>
</object>
<object class="IBActionInfo" key="updateTableCollation:">
Expand Down
2 changes: 1 addition & 1 deletion Source/SPExtendedTableInfo.h
Expand Up @@ -75,7 +75,7 @@
- (IBAction)updateTableEncoding:(id)sender;
- (IBAction)updateTableCollation:(id)sender;
- (IBAction)resetAutoIncrement:(id)sender;
- (IBAction)resetAutoIncrementValueWasEdited:(id)sender;
- (IBAction)tableRowAutoIncrementWasEdited:(id)sender;

// Others
- (void)loadTable:(NSString *)table;
Expand Down
5 changes: 3 additions & 2 deletions Source/SPExtendedTableInfo.m
Expand Up @@ -216,10 +216,11 @@ - (IBAction)resetAutoIncrement:(id)sender
}
}

- (IBAction)resetAutoIncrementValueWasEdited:(id)sender
- (IBAction)tableRowAutoIncrementWasEdited:(id)sender
{
[tableRowAutoIncrement setEditable:NO];
[tableSourceInstance setAutoIncrementTo:[[tableRowAutoIncrement stringValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];

[tableSourceInstance takeAutoIncrementFrom:tableRowAutoIncrement];
}

- (BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)command
Expand Down
2 changes: 1 addition & 1 deletion Source/SPTableContent.m
Expand Up @@ -2038,7 +2038,7 @@ - (void)removeRowSheetDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode c

// Reset auto increment if suppression button was ticked
if([[alert suppressionButton] state] == NSOnState) {
[tableSourceInstance setAutoIncrementTo:@"1"];
[tableSourceInstance setAutoIncrementTo:@1];
#ifndef SP_CODA
[prefs setBool:YES forKey:SPResetAutoIncrementAfterDeletionOfAllRows];
#endif
Expand Down
1 change: 1 addition & 0 deletions Source/SPTableData.m
Expand Up @@ -575,6 +575,7 @@ - (NSDictionary *) informationForTable:(NSString *)tableName
returningInclusively: NO
ignoringQuotedStrings: NO];
if(fieldName == nil || [fieldName length] == 0) {
#warning NSAlert from background thread! (This whole function needs improvement)
NSBeep();
NSAlert *alert = [[NSAlert alloc] init];
[alert addButtonWithTitle:NSLocalizedString(@"OK", @"OK button")];
Expand Down
3 changes: 2 additions & 1 deletion Source/SPTableStructure.h
Expand Up @@ -129,6 +129,7 @@
- (void)removeFieldSheetDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo;
- (IBAction)resetAutoIncrement:(id)sender;
- (void)resetAutoincrementSheetDidEnd:(NSWindow *)theSheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo;
- (void)takeAutoIncrementFrom:(NSTextField *)field;
- (IBAction)showOptimizedFieldType:(id)sender;
- (IBAction)toggleColumnView:(NSMenuItem *)sender;
- (BOOL)cancelRowEditing;
Expand All @@ -142,7 +143,7 @@
- (BOOL)saveRowOnDeselect;
- (BOOL)addRowToDB;
- (void)addRowErrorSheetDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo;
- (void)setAutoIncrementTo:(NSString*)valueAsString;
- (void)setAutoIncrementTo:(NSNumber *)value;

// Accessors
- (NSString *)defaultValueForField:(NSString *)field;
Expand Down
26 changes: 16 additions & 10 deletions Source/SPTableStructure.m
Expand Up @@ -490,7 +490,7 @@ - (IBAction)resetAutoIncrement:(id)sender
[resetAutoIncrementValue setStringValue:@"1"];
}
else if ([sender tag] == 2) {
[self setAutoIncrementTo:@"1"];
[self setAutoIncrementTo:@1];
}
#endif
}
Expand All @@ -505,11 +505,21 @@ - (void)resetAutoincrementSheetDidEnd:(NSWindow *)theSheet returnCode:(NSInteger
[theSheet orderOut:nil];

if (returnCode == NSAlertDefaultReturn) {
[self setAutoIncrementTo:[[resetAutoIncrementValue stringValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
[self takeAutoIncrementFrom:resetAutoIncrementValue];
}
#endif
}

- (void)takeAutoIncrementFrom:(NSTextField *)field
{
id obj = [field objectValue];
//nil is handled by -setAutoIncrementTo:
if(obj && ![obj isKindOfClass:[NSNumber class]]) {
[NSException raise:NSInternalInconsistencyException format:@"[$field objectValue] should return NSNumber *, but was %@",[obj class]];
}
[self setAutoIncrementTo:(NSNumber *)obj];
}

/**
* Process the remove field sheet closing, performing the delete if the user
* confirmed the action.
Expand Down Expand Up @@ -600,13 +610,13 @@ - (IBAction)closeSheet:(id)sender
*
* @param valueAsString The new auto_increment integer as NSString
*/
- (void)setAutoIncrementTo:(NSString*)valueAsString
- (void)setAutoIncrementTo:(NSNumber *)value
{
NSString *selTable = [tablesListInstance tableName];

if (selTable == nil || ![selTable length]) return;

if (valueAsString == nil || ![valueAsString length]) {
if (value == nil) {
// reload data and bail
[tableDataInstance resetAllData];
#ifndef SP_CODA
Expand All @@ -616,12 +626,8 @@ - (void)setAutoIncrementTo:(NSString*)valueAsString
return;
}

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber *autoIncValue = [formatter numberFromString:valueAsString];
[formatter release];

[mySQLConnection queryString:[NSString stringWithFormat:@"ALTER TABLE %@ AUTO_INCREMENT = %@", [selTable backtickQuotedString], [autoIncValue stringValue]]];
// only int and float types can be AUTO_INCREMENT and right now BIGINT = 64 Bit (<= long long) is the largest type mysql supports
[mySQLConnection queryString:[NSString stringWithFormat:@"ALTER TABLE %@ AUTO_INCREMENT = %llu", [selTable backtickQuotedString], [value unsignedLongLongValue]]];

if ([mySQLConnection queryErrored]) {
SPBeginAlertSheet(NSLocalizedString(@"Error", @"error"),
Expand Down

0 comments on commit fb54e89

Please sign in to comment.