Skip to content

Commit fb54e89

Browse files
committed
Setting large auto_increment values (> 1000) could be misinterpreted as decimal values in some cases (fixes #2130)
1 parent be4532c commit fb54e89

File tree

7 files changed

+31
-22
lines changed

7 files changed

+31
-22
lines changed

Interfaces/English.lproj/DBView.xib

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15857,7 +15857,7 @@ AAEAAQAAAT0AAwAAAAEAAgAAAVIAAwAAAAEAAQAAAVMAAwAAAAIAAQABAAAAAA</bytes>
1585715857
</object>
1585815858
<object class="IBConnectionRecord">
1585915859
<object class="IBActionConnection" key="connection">
15860-
<string key="label">resetAutoIncrementValueWasEdited:</string>
15860+
<string key="label">tableRowAutoIncrementWasEdited:</string>
1586115861
<reference key="source" ref="269913528"/>
1586215862
<reference key="destination" ref="735808692"/>
1586315863
</object>
@@ -29569,7 +29569,7 @@ AAEAAQAAAT0AAwAAAAEAAgAAAVIAAwAAAAEAAQAAAVMAAwAAAAIAAQABAAAAAA</bytes>
2956929569
<dictionary class="NSMutableDictionary" key="actions">
2957029570
<string key="reloadTable:">id</string>
2957129571
<string key="resetAutoIncrement:">id</string>
29572-
<string key="resetAutoIncrementValueWasEdited:">id</string>
29572+
<string key="tableRowAutoIncrementWasEdited:">id</string>
2957329573
<string key="updateTableCollation:">id</string>
2957429574
<string key="updateTableEncoding:">id</string>
2957529575
<string key="updateTableType:">id</string>
@@ -29583,8 +29583,8 @@ AAEAAQAAAT0AAwAAAAEAAgAAAVIAAwAAAAEAAQAAAVMAAwAAAAIAAQABAAAAAA</bytes>
2958329583
<string key="name">resetAutoIncrement:</string>
2958429584
<string key="candidateClassName">id</string>
2958529585
</object>
29586-
<object class="IBActionInfo" key="resetAutoIncrementValueWasEdited:">
29587-
<string key="name">resetAutoIncrementValueWasEdited:</string>
29586+
<object class="IBActionInfo" key="tableRowAutoIncrementWasEdited:">
29587+
<string key="name">tableRowAutoIncrementWasEdited:</string>
2958829588
<string key="candidateClassName">id</string>
2958929589
</object>
2959029590
<object class="IBActionInfo" key="updateTableCollation:">
@@ -29719,7 +29719,7 @@ AAEAAQAAAT0AAwAAAAEAAgAAAVIAAwAAAAEAAQAAAVMAAwAAAAIAAQABAAAAAA</bytes>
2971929719
<dictionary class="NSMutableDictionary" key="actions">
2972029720
<string key="reloadTable:">id</string>
2972129721
<string key="resetAutoIncrement:">id</string>
29722-
<string key="resetAutoIncrementValueWasEdited:">id</string>
29722+
<string key="tableRowAutoIncrementWasEdited:">id</string>
2972329723
<string key="updateTableCollation:">id</string>
2972429724
<string key="updateTableEncoding:">id</string>
2972529725
<string key="updateTableType:">id</string>
@@ -29733,8 +29733,8 @@ AAEAAQAAAT0AAwAAAAEAAgAAAVIAAwAAAAEAAQAAAVMAAwAAAAIAAQABAAAAAA</bytes>
2973329733
<string key="name">resetAutoIncrement:</string>
2973429734
<string key="candidateClassName">id</string>
2973529735
</object>
29736-
<object class="IBActionInfo" key="resetAutoIncrementValueWasEdited:">
29737-
<string key="name">resetAutoIncrementValueWasEdited:</string>
29736+
<object class="IBActionInfo" key="tableRowAutoIncrementWasEdited:">
29737+
<string key="name">tableRowAutoIncrementWasEdited:</string>
2973829738
<string key="candidateClassName">id</string>
2973929739
</object>
2974029740
<object class="IBActionInfo" key="updateTableCollation:">

Source/SPExtendedTableInfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
- (IBAction)updateTableEncoding:(id)sender;
7676
- (IBAction)updateTableCollation:(id)sender;
7777
- (IBAction)resetAutoIncrement:(id)sender;
78-
- (IBAction)resetAutoIncrementValueWasEdited:(id)sender;
78+
- (IBAction)tableRowAutoIncrementWasEdited:(id)sender;
7979

8080
// Others
8181
- (void)loadTable:(NSString *)table;

Source/SPExtendedTableInfo.m

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,11 @@ - (IBAction)resetAutoIncrement:(id)sender
216216
}
217217
}
218218

219-
- (IBAction)resetAutoIncrementValueWasEdited:(id)sender
219+
- (IBAction)tableRowAutoIncrementWasEdited:(id)sender
220220
{
221221
[tableRowAutoIncrement setEditable:NO];
222-
[tableSourceInstance setAutoIncrementTo:[[tableRowAutoIncrement stringValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
222+
223+
[tableSourceInstance takeAutoIncrementFrom:tableRowAutoIncrement];
223224
}
224225

225226
- (BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)command

Source/SPTableContent.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2038,7 +2038,7 @@ - (void)removeRowSheetDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode c
20382038

20392039
// Reset auto increment if suppression button was ticked
20402040
if([[alert suppressionButton] state] == NSOnState) {
2041-
[tableSourceInstance setAutoIncrementTo:@"1"];
2041+
[tableSourceInstance setAutoIncrementTo:@1];
20422042
#ifndef SP_CODA
20432043
[prefs setBool:YES forKey:SPResetAutoIncrementAfterDeletionOfAllRows];
20442044
#endif

Source/SPTableData.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,7 @@ - (NSDictionary *) informationForTable:(NSString *)tableName
575575
returningInclusively: NO
576576
ignoringQuotedStrings: NO];
577577
if(fieldName == nil || [fieldName length] == 0) {
578+
#warning NSAlert from background thread! (This whole function needs improvement)
578579
NSBeep();
579580
NSAlert *alert = [[NSAlert alloc] init];
580581
[alert addButtonWithTitle:NSLocalizedString(@"OK", @"OK button")];

Source/SPTableStructure.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
- (void)removeFieldSheetDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo;
130130
- (IBAction)resetAutoIncrement:(id)sender;
131131
- (void)resetAutoincrementSheetDidEnd:(NSWindow *)theSheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo;
132+
- (void)takeAutoIncrementFrom:(NSTextField *)field;
132133
- (IBAction)showOptimizedFieldType:(id)sender;
133134
- (IBAction)toggleColumnView:(NSMenuItem *)sender;
134135
- (BOOL)cancelRowEditing;
@@ -142,7 +143,7 @@
142143
- (BOOL)saveRowOnDeselect;
143144
- (BOOL)addRowToDB;
144145
- (void)addRowErrorSheetDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo;
145-
- (void)setAutoIncrementTo:(NSString*)valueAsString;
146+
- (void)setAutoIncrementTo:(NSNumber *)value;
146147

147148
// Accessors
148149
- (NSString *)defaultValueForField:(NSString *)field;

Source/SPTableStructure.m

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ - (IBAction)resetAutoIncrement:(id)sender
490490
[resetAutoIncrementValue setStringValue:@"1"];
491491
}
492492
else if ([sender tag] == 2) {
493-
[self setAutoIncrementTo:@"1"];
493+
[self setAutoIncrementTo:@1];
494494
}
495495
#endif
496496
}
@@ -505,11 +505,21 @@ - (void)resetAutoincrementSheetDidEnd:(NSWindow *)theSheet returnCode:(NSInteger
505505
[theSheet orderOut:nil];
506506

507507
if (returnCode == NSAlertDefaultReturn) {
508-
[self setAutoIncrementTo:[[resetAutoIncrementValue stringValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
508+
[self takeAutoIncrementFrom:resetAutoIncrementValue];
509509
}
510510
#endif
511511
}
512512

513+
- (void)takeAutoIncrementFrom:(NSTextField *)field
514+
{
515+
id obj = [field objectValue];
516+
//nil is handled by -setAutoIncrementTo:
517+
if(obj && ![obj isKindOfClass:[NSNumber class]]) {
518+
[NSException raise:NSInternalInconsistencyException format:@"[$field objectValue] should return NSNumber *, but was %@",[obj class]];
519+
}
520+
[self setAutoIncrementTo:(NSNumber *)obj];
521+
}
522+
513523
/**
514524
* Process the remove field sheet closing, performing the delete if the user
515525
* confirmed the action.
@@ -600,13 +610,13 @@ - (IBAction)closeSheet:(id)sender
600610
*
601611
* @param valueAsString The new auto_increment integer as NSString
602612
*/
603-
- (void)setAutoIncrementTo:(NSString*)valueAsString
613+
- (void)setAutoIncrementTo:(NSNumber *)value
604614
{
605615
NSString *selTable = [tablesListInstance tableName];
606616

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

609-
if (valueAsString == nil || ![valueAsString length]) {
619+
if (value == nil) {
610620
// reload data and bail
611621
[tableDataInstance resetAllData];
612622
#ifndef SP_CODA
@@ -616,12 +626,8 @@ - (void)setAutoIncrementTo:(NSString*)valueAsString
616626
return;
617627
}
618628

619-
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
620-
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
621-
NSNumber *autoIncValue = [formatter numberFromString:valueAsString];
622-
[formatter release];
623-
624-
[mySQLConnection queryString:[NSString stringWithFormat:@"ALTER TABLE %@ AUTO_INCREMENT = %@", [selTable backtickQuotedString], [autoIncValue stringValue]]];
629+
// only int and float types can be AUTO_INCREMENT and right now BIGINT = 64 Bit (<= long long) is the largest type mysql supports
630+
[mySQLConnection queryString:[NSString stringWithFormat:@"ALTER TABLE %@ AUTO_INCREMENT = %llu", [selTable backtickQuotedString], [value unsignedLongLongValue]]];
625631

626632
if ([mySQLConnection queryErrored]) {
627633
SPBeginAlertSheet(NSLocalizedString(@"Error", @"error"),

0 commit comments

Comments
 (0)