From c3eb7de88f2baf7774749f4311a84b75ef833eb7 Mon Sep 17 00:00:00 2001 From: Bibiko Date: Fri, 16 Oct 2009 22:18:04 +0000 Subject: =?UTF-8?q?=E2=80=A2=20fixed=20NSString=20pointer=20reassignment?= =?UTF-8?q?=20in=20SPArrayAddition's=20method=20'componentsJoinedAndBackti?= =?UTF-8?q?ckQuoted'=20=E2=80=A2=20improved=20deletion=20of=20rows=20in=20?= =?UTF-8?q?tables=20which=20have=20no=20primary=20keys=20-=20check=20for?= =?UTF-8?q?=20duplicated=20rows=20first;=20if=20no=20duplicates=20are=20fo?= =?UTF-8?q?und=20use=20all=20columns=20as=20'primary=20keys'=20and=20delet?= =?UTF-8?q?e=20the=20rows=20via=20=E2=80=A6=20WHERE=20(a=3D'=E2=80=A6'=20A?= =?UTF-8?q?ND=20b=3D'=E2=80=A6')=20OR=20(a=3D'=E2=80=A6'=20AND=20b=3D'?= =?UTF-8?q?=E2=80=A6')=20OR=20=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/TableContent.m | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'Source/TableContent.m') 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) { -- cgit v1.2.3