From fab8a7ddf951ed3697760093b43a612fa51a78c6 Mon Sep 17 00:00:00 2001 From: Bibiko Date: Mon, 22 Jun 2009 07:28:47 +0000 Subject: =?UTF-8?q?=E2=80=A2=20first=20steps=20to=20synchronize=20CustomQu?= =?UTF-8?q?ery=20table=20and=20ContentTable=20-=20CustomQuery=20table=20no?= =?UTF-8?q?w=20allows=20to=20copy=20selected=20rows=20as=20SQL=20INSERT=20?= =?UTF-8?q?(table=20name=20is=20set=20to=20""=20-=20if=20a=20field?= =?UTF-8?q?=20is=20a=20blob=20display=20only=20the=20first=20255=20bytes/c?= =?UTF-8?q?hars=20in=20the=20CQ=20table=20for=20speed=20reasons=20-=20blob?= =?UTF-8?q?=20fields=20in=20CQ=20does=20not=20listen=20to=20the=20pref=20s?= =?UTF-8?q?etting=20for=20"LoadBlobsAsNeeded"=20because=20it=20could=20hap?= =?UTF-8?q?pen=20that=20no=20one-to-one=20relation=20to=20their=20original?= =?UTF-8?q?=20data=20will=20be=20found?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/CMCopyTable.m | 5 ++-- Source/CMMCPResult.m | 12 ++++++-- Source/CustomQuery.h | 4 +++ Source/CustomQuery.m | 77 +++++++++++++++++++++++++++++++++++++++++++++++++--- 4 files changed, 90 insertions(+), 8 deletions(-) (limited to 'Source') diff --git a/Source/CMCopyTable.m b/Source/CMCopyTable.m index 85f8a20e..1ed22248 100644 --- a/Source/CMCopyTable.m +++ b/Source/CMCopyTable.m @@ -26,6 +26,7 @@ #import "SPArrayAdditions.h" #import "CMMCPConnection.h" #import "TableContent.h" +#import "CustomQuery.h" int MENU_EDIT_COPY_WITH_COLUMN = 2001; int MENU_EDIT_COPY_AS_SQL = 2002; @@ -240,7 +241,7 @@ int MENU_EDIT_COPY_AS_SQL = 2002; [mySQLConnection prepareString:[rowData description]]]]; break; case 2: // blob - if ([prefs boolForKey:@"LoadBlobsAsNeeded"]) { + if (![[self delegate] isKindOfClass:[CustomQuery class]] && [prefs boolForKey:@"LoadBlobsAsNeeded"]) { // Abort if there are no indices on this table or if there's no table name given. if (![[tableInstance argumentForRow:row] length] || selectedTable == nil) @@ -260,7 +261,7 @@ int MENU_EDIT_COPY_AS_SQL = 2002; } break; case 3: // long text data - if ([prefs boolForKey:@"LoadBlobsAsNeeded"]) { + if (![[self delegate] isKindOfClass:[CustomQuery class]] && [prefs boolForKey:@"LoadBlobsAsNeeded"]) { // Abort if there are no indices on this table or if there's no table name given. if (![[tableInstance argumentForRow:row] length] || selectedTable == nil) diff --git a/Source/CMMCPResult.m b/Source/CMMCPResult.m index ebc5c6ab..afc7c2ed 100644 --- a/Source/CMMCPResult.m +++ b/Source/CMMCPResult.m @@ -370,7 +370,7 @@ modified version for use with sequel-pro /* Div flags */ [fieldStructure setObject:[NSNumber numberWithUnsignedInt:theField[i].flags] forKey:@"flags"]; - [fieldStructure setObject:[NSNumber numberWithBool:(theField[i].flags & NOT_NULL_FLAG) ? YES : NO] forKey:@"NOT_NULL_FLAG"]; + [fieldStructure setObject:[NSNumber numberWithBool:(theField[i].flags & NOT_NULL_FLAG) ? YES : NO] forKey:@"null"]; [fieldStructure setObject:[NSNumber numberWithBool:(theField[i].flags & PRI_KEY_FLAG) ? YES : NO] forKey:@"PRI_KEY_FLAG"]; [fieldStructure setObject:[NSNumber numberWithBool:(theField[i].flags & UNIQUE_KEY_FLAG) ? YES : NO] forKey:@"UNIQUE_KEY_FLAG"]; [fieldStructure setObject:[NSNumber numberWithBool:(theField[i].flags & MULTIPLE_KEY_FLAG) ? YES : NO] forKey:@"MULTIPLE_KEY_FLAG"]; @@ -423,6 +423,7 @@ modified version for use with sequel-pro { // BOOL isUnsigned = (flags & UNSIGNED_FLAG) != 0; // BOOL isZerofill = (flags & ZEROFILL_FLAG) != 0; + switch (type) { case FIELD_TYPE_BIT: return @"BIT"; @@ -473,7 +474,14 @@ modified version for use with sequel-pro case 16777215: return isBlob? @"MEDIUMBLOB":@"MEDIUMTEXT"; case 4294967295: return isBlob? @"LONGBLOB":@"LONGTEXT"; default: - return @"UNKNOWN"; + switch (length) { + case 255: return isBlob? @"TINYBLOB":@"TINYTEXT"; + case 65535: return isBlob? @"BLOB":@"TEXT"; + case 16777215: return isBlob? @"MEDIUMBLOB":@"MEDIUMTEXT"; + case 4294967295: return isBlob? @"LONGBLOB":@"LONGTEXT"; + default: + return @"UNKNOWN"; + } } } case MYSQL_TYPE_VAR_STRING: diff --git a/Source/CustomQuery.h b/Source/CustomQuery.h index 0be5dc0d..6c33a375 100644 --- a/Source/CustomQuery.h +++ b/Source/CustomQuery.h @@ -88,6 +88,9 @@ int helpTarget; WebHistory *helpHistory; NSString *helpHTMLTemplate; + + NSMutableArray *fullResult; + NSArray *columnDefinition; } @@ -123,6 +126,7 @@ // Accessors - (NSArray *)currentResult; +- (NSArray *)fetchResultAsArray:(CMMCPResult *)theResult; // MySQL Help - (NSString *)getHTMLformattedMySQLHelpFor:(NSString *)aString; diff --git a/Source/CustomQuery.m b/Source/CustomQuery.m index c07f4d24..d3739ef3 100644 --- a/Source/CustomQuery.m +++ b/Source/CustomQuery.m @@ -637,6 +637,10 @@ } + columnDefinition = [theResult fetchResultFieldsStructure]; + [fullResult removeAllObjects]; + [fullResult setArray:[self fetchResultAsArray:theResult]]; + // Otherwise add columns corresponding to the query result theColumns = [theResult fetchFieldNames]; for ( i = 0 ; i < [theResult numOfFields] ; i++) { @@ -656,7 +660,7 @@ [customQueryView addTableColumn:theCol]; [theCol release]; } - + [customQueryView sizeLastColumnToFit]; //tries to fix problem with last row (otherwise to small) //sets last column to width of the first if smaller than 30 @@ -666,6 +670,9 @@ setWidth:[[customQueryView tableColumnWithIdentifier:[NSNumber numberWithInt:0]] width]]; [customQueryView reloadData]; + // Init copyTable with necessary information for copying selected rows as SQL INSERT + [customQueryView setTableInstance:self withTableData:fullResult withColumns:columnDefinition withTableName:nil withConnection:mySQLConnection]; + //query finished [[NSNotificationCenter defaultCenter] postNotificationName:@"SMySQLQueryHasBeenPerformed" object:self]; @@ -675,6 +682,57 @@ notificationName:@"Query Finished"]; } +/* + * Fetches the result as an array with a dictionary for each row in it + */ +- (NSArray *)fetchResultAsArray:(CMMCPResult *)theResult +{ + // NSArray *columns; + unsigned long numOfRows = [theResult numOfRows]; + NSMutableArray *tempResult = [NSMutableArray arrayWithCapacity:numOfRows]; + + NSDictionary *tempRow; + NSMutableDictionary *modifiedRow = [NSMutableDictionary dictionary]; + NSEnumerator *enumerator; + id key; + int i, j; + Class nullClass = [NSNull class]; + id prefsNullValue = [prefs objectForKey:@"NullValue"]; + BOOL prefsLoadBlobsAsNeeded = [prefs boolForKey:@"LoadBlobsAsNeeded"]; + + // columns = [customQueryView columns]; + long columnsCount = [columnDefinition count]; + + if (numOfRows) [theResult dataSeek:0]; + for ( i = 0 ; i < numOfRows ; i++ ) { + tempRow = [theResult fetchRowAsDictionary]; + enumerator = [tempRow keyEnumerator]; + + while ( key = [enumerator nextObject] ) { + if ( [[tempRow objectForKey:key] isMemberOfClass:nullClass] ) { + [modifiedRow setObject:prefsNullValue forKey:key]; + } else { + [modifiedRow setObject:[tempRow objectForKey:key] forKey:key]; + } + } + + // Add values for hidden blob and text fields if appropriate + // if ( prefsLoadBlobsAsNeeded ) { + // for ( j = 0 ; j < columnsCount ; j++ ) { + // if ( [[NSArrayObjectAtIndex(columnDefinition, j) objectForKey:@"typegrouping"] isEqualToString:@"blobdata"] || + // [[NSArrayObjectAtIndex(columnDefinition, j) objectForKey:@"typegrouping"] isEqualToString:@"textdata"]) { + // [modifiedRow setObject:NSLocalizedString(@"(not loaded)", @"value shown for hidden blob and text fields") forKey:[NSArrayObjectAtIndex(columnDefinition, j) objectForKey:@"name"]]; + // } + // } + // } + + [tempResult addObject:[NSMutableDictionary dictionaryWithDictionary:modifiedRow]]; + } + + return tempResult; +} + + /* * Retrieve the range of the query at a position specified * within the custom query text view. @@ -1030,15 +1088,23 @@ tmp = [[NSString alloc] initWithData:[theRow objectAtIndex:[theIdentifier intValue]] encoding:NSASCIIStringEncoding]; } - return [tmp autorelease]; + // If field contains binary data show only the first 255 bytes for speed + if([tmp length] > 255) { + return [[tmp autorelease] substringToIndex:255]; + } else + return [tmp autorelease]; } if ( [[theRow objectAtIndex:[theIdentifier intValue]] isMemberOfClass:[NSNull class]] ) return [prefs objectForKey:@"NullValue"]; return [theRow objectAtIndex:[theIdentifier intValue]]; - } else if ( aTableView == queryFavoritesView ) { + } + + else if ( aTableView == queryFavoritesView ) { return [queryFavorites objectAtIndex:rowIndex]; - } else { + } + + else { return @""; } } @@ -1914,6 +1980,8 @@ // init search history [helpWebView setMaintainsBackForwardList:YES]; [[helpWebView backForwardList] setCapacity:20]; + + fullResult = [[NSMutableArray alloc] init]; } return self; @@ -1930,6 +1998,7 @@ [queryResult release]; [queryFavorites release]; [usedQuery release]; + [fullResult release]; [super dealloc]; } -- cgit v1.2.3