aboutsummaryrefslogtreecommitdiffstats
path: root/Source
diff options
context:
space:
mode:
authorBibiko <bibiko@eva.mpg.de>2009-10-16 22:18:04 +0000
committerBibiko <bibiko@eva.mpg.de>2009-10-16 22:18:04 +0000
commitc3eb7de88f2baf7774749f4311a84b75ef833eb7 (patch)
treea301a76611096d6718468b0d85fe6a4ae38e6d30 /Source
parent974049dd4fbe6b42c1a8349f72ea355cf6a38634 (diff)
downloadsequelpro-c3eb7de88f2baf7774749f4311a84b75ef833eb7.tar.gz
sequelpro-c3eb7de88f2baf7774749f4311a84b75ef833eb7.tar.bz2
sequelpro-c3eb7de88f2baf7774749f4311a84b75ef833eb7.zip
• fixed NSString pointer reassignment in SPArrayAddition's method 'componentsJoinedAndBacktickQuoted'
• improved deletion of rows in tables which have no primary keys - check for duplicated rows first; if no duplicates are found use all columns as 'primary keys' and delete the rows via … WHERE (a='…' AND b='…') OR (a='…' AND b='…') OR …
Diffstat (limited to 'Source')
-rw-r--r--Source/SPArrayAdditions.m10
-rw-r--r--Source/TableContent.m26
2 files changed, 32 insertions, 4 deletions
diff --git a/Source/SPArrayAdditions.m b/Source/SPArrayAdditions.m
index bbc732af..c16616a5 100644
--- a/Source/SPArrayAdditions.m
+++ b/Source/SPArrayAdditions.m
@@ -33,11 +33,15 @@
*/
- (NSString *)componentsJoinedAndBacktickQuoted;
{
- NSString *result = [NSString string];
+ NSMutableString *result = [NSMutableString string];
+ [result setString:@""];
+
for (NSString *component in self)
{
- if ([result length]) result = [result stringByAppendingString: @","];
- result = [result stringByAppendingString: [component backtickQuotedString] ];
+ if ([result length])
+ [result appendString: @","];
+
+ [result appendString:[component backtickQuotedString]];
}
return result;
}
diff --git a/Source/TableContent.m b/Source/TableContent.m
index 19f77db4..1c5551c6 100644
--- a/Source/TableContent.m
+++ b/Source/TableContent.m
@@ -1787,7 +1787,31 @@
NSArray *primaryKeyFieldNames = [tableDataInstance primaryKeyColumnNames];
- // TODO tentative → || [primaryKeyFields count] != 1
+ // If no PRIMARY KEY is found and numberOfSelectedRows > 3 then
+ // check for uniqueness of rows via combining all column values;
+ // if unique then use the all columns as 'primary keys'
+ if([selectedRows count] >3 && primaryKeyFieldNames == nil) {
+ primaryKeyFieldNames = [tableDataInstance columnNames];
+
+ NSInteger numberOfRows = 0;
+ // Get the number of rows in the table
+ MCPResult *r;
+ r = [mySQLConnection queryString:[NSString stringWithFormat:@"SELECT COUNT(*) FROM %@", [selectedTable backtickQuotedString]]];
+ if ([[mySQLConnection getLastErrorMessage] isEqualToString:@""]) {
+ NSArray *a = [r fetchRowAsArray];
+ if([a count])
+ numberOfRows = [[a objectAtIndex:0] integerValue];
+ }
+ // Check for uniqueness via LIMIT numberOfRows-1,numberOfRows for speed
+ if(numberOfRows > 0) {
+ [mySQLConnection queryString:[NSString stringWithFormat:@"SELECT * FROM %@ GROUP BY %@ LIMIT %d,%d", [selectedTable backtickQuotedString], [primaryKeyFieldNames componentsJoinedAndBacktickQuoted], numberOfRows-1, numberOfRows]];
+ if([mySQLConnection affectedRows] == 0)
+ primaryKeyFieldNames = nil;
+ } else {
+ primaryKeyFieldNames = nil;
+ }
+ }
+
if(primaryKeyFieldNames == nil) {
// delete row by row
while (index != NSNotFound) {