From 2c558dccc2235bfbd9c8f2114bbffa99c78c4af5 Mon Sep 17 00:00:00 2001 From: stuconnolly Date: Fri, 26 Mar 2010 13:14:54 +0000 Subject: Minor MCPKit tidy up, including the resolution of potential issues raised during static analysis. --- Frameworks/MCPKit/MCPFoundationKit/MCPConnection.h | 70 +++++++++++++--------- Frameworks/MCPKit/MCPFoundationKit/MCPConnection.m | 34 ++++++----- Frameworks/MCPKit/MCPFoundationKit/MCPConstants.h | 13 +++- Frameworks/MCPKit/MCPFoundationKit/MCPResult.h | 2 +- .../MCPKit/MCPFoundationKit/MCPStreamingResult.h | 9 ++- .../MCPKit/MCPFoundationKit/MCPStreamingResult.m | 4 +- .../MCPKit/MCPFoundationKit/MCPStringAdditions.h | 43 +++++++++++++ sequel-pro.xcodeproj/project.pbxproj | 6 +- 8 files changed, 133 insertions(+), 48 deletions(-) create mode 100644 Frameworks/MCPKit/MCPFoundationKit/MCPStringAdditions.h diff --git a/Frameworks/MCPKit/MCPFoundationKit/MCPConnection.h b/Frameworks/MCPKit/MCPFoundationKit/MCPConnection.h index c6d81bd6..89739e23 100644 --- a/Frameworks/MCPKit/MCPFoundationKit/MCPConnection.h +++ b/Frameworks/MCPKit/MCPFoundationKit/MCPConnection.h @@ -27,47 +27,62 @@ // More info at #import +#import #import "MCPConstants.h" - #import "mysql.h" -#include - -enum -{ - MCP_NO_STREAMING = 0, - MCP_FAST_STREAMING = 1, - MCP_LOWMEM_STREAMING = 2 -}; -typedef NSUInteger mcp_query_streaming_types; -@class MCPResult, MCPStreamingResult; @protocol MCPConnectionProxy; -/** - * NSStringDataUsingLossyEncoding(aStr, enc, lossy) := [aStr dataUsingEncoding:enc allowLossyConversion:lossy] - */ -static inline NSData* NSStringDataUsingLossyEncoding(NSString* self, NSInteger encoding, NSInteger lossy) -{ - typedef NSData* (*SPStringDataUsingLossyEncodingMethodPtr)(NSString*, SEL, NSInteger, NSInteger); - static SPStringDataUsingLossyEncodingMethodPtr SPNSStringDataUsingLossyEncoding; - if (!SPNSStringDataUsingLossyEncoding) SPNSStringDataUsingLossyEncoding = (SPStringDataUsingLossyEncodingMethodPtr)[self methodForSelector:@selector(dataUsingEncoding:allowLossyConversion:)]; - NSData* to_return = SPNSStringDataUsingLossyEncoding(self, @selector(dataUsingEncoding:allowLossyConversion:), encoding, lossy); - return to_return; -} +@class MCPResult, MCPStreamingResult; -// Connection delegate interface @interface NSObject (MCPConnectionDelegate) +/** + * + */ - (void)willQueryString:(NSString *)query connection:(id)connection; + +/** + * + */ - (void)queryGaveError:(NSString *)error connection:(id)connection; + +/** + * + */ - (BOOL)connectionEncodingViaLatin1:(id)connection; + +/** + * + */ - (NSString *)keychainPasswordForConnection:(id)connection; + +/** + * + */ - (NSString *)onReconnectShouldSelectDatabase:(id)connection; + +/** + * + */ - (NSString *)onReconnectShouldUseEncoding:(id)connection; + +/** + * + */ - (void)noConnectionAvailable:(id)connection; + +/** + * + */ - (MCPConnectionCheck)connectionLost:(id)connection; -- (NSString*)connectionID; + +/** + * + */ +- (NSString *)connectionID; + @end @interface MCPConnection : NSObject @@ -77,9 +92,10 @@ static inline NSData* NSStringDataUsingLossyEncoding(NSString* self, NSInteger e NSStringEncoding mEncoding; /* The encoding used by MySQL server, to ISO-1 default. */ NSTimeZone *mTimeZone; /* The time zone of the session. */ NSUInteger mConnectionFlags; /* The flags to be used for the connection to the database. */ - id delegate; /* Connection delegate */ - NSLock *queryLock; /* Anything that performs a mysql_net_read is not thread-safe: mysql queries, pings */ + id delegate; /* Connection delegate */ + + NSLock *queryLock; /* Anything that performs a mysql_net_read is not thread-safe: mysql queries, pings */ BOOL useKeepAlive; BOOL isDisconnecting; @@ -212,7 +228,7 @@ void performThreadedKeepAlive(void *ptr); - (MCPResult *)queryString:(NSString *)query; - (MCPStreamingResult *)streamingQueryString:(NSString *)query; - (MCPStreamingResult *)streamingQueryString:(NSString *)query useLowMemoryBlockingStreaming:(BOOL)fullStream; -- (id)queryString:(NSString *) query usingEncoding:(NSStringEncoding) encoding streamingResult:(NSInteger) streamResult; +- (id)queryString:(NSString *)query usingEncoding:(NSStringEncoding)encoding streamingResult:(NSInteger)streamResult; - (my_ulonglong)affectedRows; - (my_ulonglong)insertId; - (void)cancelCurrentQuery; diff --git a/Frameworks/MCPKit/MCPFoundationKit/MCPConnection.m b/Frameworks/MCPKit/MCPFoundationKit/MCPConnection.m index 6f516157..af3bae08 100644 --- a/Frameworks/MCPKit/MCPFoundationKit/MCPConnection.m +++ b/Frameworks/MCPKit/MCPFoundationKit/MCPConnection.m @@ -28,10 +28,11 @@ #import "MCPConnection.h" #import "MCPResult.h" -#import "MCPStreamingResult.h" #import "MCPNumber.h" #import "MCPNull.h" +#import "MCPStreamingResult.h" #import "MCPConnectionProxy.h" +#import "MCPStringAdditions.h" #include #include @@ -1285,7 +1286,7 @@ void performThreadedKeepAlive(void *ptr) */ - (MCPResult *)queryString:(NSString *)query { - return [self queryString:query usingEncoding:mEncoding streamingResult:MCP_NO_STREAMING]; + return [self queryString:query usingEncoding:mEncoding streamingResult:MCPStreamingNone]; } /** @@ -1295,7 +1296,7 @@ void performThreadedKeepAlive(void *ptr) */ - (MCPStreamingResult *)streamingQueryString:(NSString *)query { - return [self queryString:query usingEncoding:mEncoding streamingResult:MCP_FAST_STREAMING]; + return [self queryString:query usingEncoding:mEncoding streamingResult:MCPStreamingFast]; } /** @@ -1307,7 +1308,7 @@ void performThreadedKeepAlive(void *ptr) */ - (MCPStreamingResult *)streamingQueryString:(NSString *)query useLowMemoryBlockingStreaming:(BOOL)fullStream { - return [self queryString:query usingEncoding:mEncoding streamingResult:(fullStream?MCP_LOWMEM_STREAMING:MCP_FAST_STREAMING)]; + return [self queryString:query usingEncoding:mEncoding streamingResult:(fullStream?MCPStreamingLowMem:MCPStreamingFast)]; } /** @@ -1413,7 +1414,7 @@ void performThreadedKeepAlive(void *ptr) // Lock the connection - on this thread for normal result sets (avoiding blocking issues // when the app is in modal mode), or ensuring a lock on the main thread for streaming queries. - if (streamResultType == MCP_NO_STREAMING) [queryLock lock]; + if (streamResultType == MCPStreamingNone) [queryLock lock]; else [self lockConnection]; // Run (or re-run) the query, timing the execution time of the query - note @@ -1431,14 +1432,14 @@ void performThreadedKeepAlive(void *ptr) if (mysql_field_count(mConnection) != 0) { // For normal result sets, fetch the results and unlock the connection - if (streamResultType == MCP_NO_STREAMING) { + if (streamResultType == MCPStreamingNone) { theResult = [[MCPResult alloc] initWithMySQLPtr:mConnection encoding:mEncoding timeZone:mTimeZone]; if (!queryCancelled || !queryCancelUsedReconnect) [queryLock unlock]; // For streaming result sets, fetch the result pointer and leave the connection locked - } else if (streamResultType == MCP_FAST_STREAMING) { + } else if (streamResultType == MCPStreamingFast) { theResult = [[MCPStreamingResult alloc] initWithMySQLPtr:mConnection encoding:mEncoding timeZone:mTimeZone connection:self withFullStreaming:NO]; - } else if (streamResultType == MCP_LOWMEM_STREAMING) { + } else if (streamResultType == MCPStreamingLowMem) { theResult = [[MCPStreamingResult alloc] initWithMySQLPtr:mConnection encoding:mEncoding timeZone:mTimeZone connection:self withFullStreaming:YES]; } @@ -1449,20 +1450,20 @@ void performThreadedKeepAlive(void *ptr) break; } } else { - if (streamResultType == MCP_NO_STREAMING) [queryLock unlock]; + if (streamResultType == MCPStreamingNone) [queryLock unlock]; else [self unlockConnection]; } queryErrorMessage = [[NSString alloc] initWithString:@""]; queryErrorId = 0; - if (streamResultType == MCP_NO_STREAMING && queryAffectedRows == -1) { + if (streamResultType == MCPStreamingNone && queryAffectedRows == -1) { queryAffectedRows = mysql_affected_rows(mConnection); } // On failure, set the error messages and IDs } else { if (!queryCancelled || !queryCancelUsedReconnect) { - if (streamResultType == MCP_NO_STREAMING) [queryLock unlock]; + if (streamResultType == MCPStreamingNone) [queryLock unlock]; else [self unlockConnection]; } @@ -1486,7 +1487,7 @@ void performThreadedKeepAlive(void *ptr) break; } - if (streamResultType == MCP_NO_STREAMING) { + if (streamResultType == MCPStreamingNone) { // If the mysql thread id has changed as a result of a connection error, // ensure connection details are still correct @@ -1499,8 +1500,13 @@ void performThreadedKeepAlive(void *ptr) // Update error strings and IDs lastQueryErrorId = queryErrorId; - [self setLastErrorMessage:queryErrorMessage?queryErrorMessage:@""]; - if (queryErrorMessage) [queryErrorMessage release]; + + if (queryErrorMessage) { + [self setLastErrorMessage:queryErrorMessage]; + + [queryErrorMessage release]; + } + lastQueryAffectedRows = queryAffectedRows; lastQueryExecutionTime = queryExecutionTime; diff --git a/Frameworks/MCPKit/MCPFoundationKit/MCPConstants.h b/Frameworks/MCPKit/MCPFoundationKit/MCPConstants.h index 816d68ca..acdeee16 100644 --- a/Frameworks/MCPKit/MCPFoundationKit/MCPConstants.h +++ b/Frameworks/MCPKit/MCPFoundationKit/MCPConstants.h @@ -43,12 +43,21 @@ enum { }; typedef NSUInteger MCPConnectionCheck; +// Streaming result set constants +enum +{ + MCPStreamingNone = 0, + MCPStreamingFast = 1, + MCPStreamingLowMem = 2 +}; +typedef NSUInteger MCPQueryStreamingType; + // Charcater set mapping constants typedef struct _OUR_CHARSET { NSUInteger nr; - const char *name; - const char *collation; + const char *name; + const char *collation; NSUInteger char_minlen; NSUInteger char_maxlen; } OUR_CHARSET; diff --git a/Frameworks/MCPKit/MCPFoundationKit/MCPResult.h b/Frameworks/MCPKit/MCPFoundationKit/MCPResult.h index ac0217de..ad50af75 100644 --- a/Frameworks/MCPKit/MCPFoundationKit/MCPResult.h +++ b/Frameworks/MCPKit/MCPFoundationKit/MCPResult.h @@ -27,8 +27,8 @@ // More info at #import -#import "MCPConstants.h" +#import "MCPConstants.h" #import "mysql.h" #define MAGIC_BINARY_CHARSET_NR 63 diff --git a/Frameworks/MCPKit/MCPFoundationKit/MCPStreamingResult.h b/Frameworks/MCPKit/MCPFoundationKit/MCPStreamingResult.h index 65ee6423..b17f8584 100644 --- a/Frameworks/MCPKit/MCPFoundationKit/MCPStreamingResult.h +++ b/Frameworks/MCPKit/MCPFoundationKit/MCPStreamingResult.h @@ -23,8 +23,10 @@ // // More info at -#import +#import + #import "MCPResult.h" +#import "mysql.h" @class MCPConnection; @@ -39,20 +41,25 @@ typedef struct SP_MYSQL_ROWS { MCPConnection *parentConnection; MYSQL_FIELD *fieldDefinitions; + BOOL fullyStreaming; BOOL connectionUnlocked; BOOL dataDownloaded; BOOL dataFreed; + LOCAL_ROW_DATA *localDataStore; LOCAL_ROW_DATA *currentDataStoreEntry; LOCAL_ROW_DATA *localDataStoreLastEntry; + unsigned long localDataRows; unsigned long localDataAllocated; unsigned long downloadedRowCount; unsigned long processedRowCount; unsigned long freedRowCount; + pthread_mutex_t dataCreationLock; pthread_mutex_t dataFreeLock; + IMP isConnectedPtr; SEL isConnectedSEL; } diff --git a/Frameworks/MCPKit/MCPFoundationKit/MCPStreamingResult.m b/Frameworks/MCPKit/MCPFoundationKit/MCPStreamingResult.m index bf8a2599..f6371b03 100644 --- a/Frameworks/MCPKit/MCPFoundationKit/MCPStreamingResult.m +++ b/Frameworks/MCPKit/MCPFoundationKit/MCPStreamingResult.m @@ -208,7 +208,7 @@ returnArray = [NSMutableArray arrayWithCapacity:mNumOfFields]; for (i = 0; i < mNumOfFields; i++) { id cellData = nil; - char *theData; + char *theData = NULL; // In fully streaming mode, copy across the data for the MYSQL_ROW if (fullyStreaming) { @@ -270,7 +270,7 @@ break; case FIELD_TYPE_BIT: - cellData = [NSString stringWithFormat:@"%u", theData[0]]; + cellData = (theData != NULL) ? [NSString stringWithFormat:@"%u", theData[0]] : @""; break; case FIELD_TYPE_TINY_BLOB: diff --git a/Frameworks/MCPKit/MCPFoundationKit/MCPStringAdditions.h b/Frameworks/MCPKit/MCPFoundationKit/MCPStringAdditions.h new file mode 100644 index 00000000..36693703 --- /dev/null +++ b/Frameworks/MCPKit/MCPFoundationKit/MCPStringAdditions.h @@ -0,0 +1,43 @@ +// +// $Id$ +// +// MCPStringAdditions.h +// sequel-pro +// +// Created by Stuart Connolly (stuconnolly.com) on March 25, 2010 +// Copyright (c) 2010 Stuart Connolly. All rights reserved. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// More info at + +#import + +/** + * NSStringDataUsingLossyEncoding(aStr, enc, lossy) := [aStr dataUsingEncoding:enc allowLossyConversion:lossy] + */ +static inline NSData *NSStringDataUsingLossyEncoding(NSString *self, NSInteger encoding, NSInteger lossy) +{ + typedef NSData *(*SPStringDataUsingLossyEncodingMethodPtr)(NSString*, SEL, NSInteger, NSInteger); + static SPStringDataUsingLossyEncodingMethodPtr SPNSStringDataUsingLossyEncoding; + + if (!SPNSStringDataUsingLossyEncoding) SPNSStringDataUsingLossyEncoding = (SPStringDataUsingLossyEncodingMethodPtr)[self methodForSelector:@selector(dataUsingEncoding:allowLossyConversion:)]; + + return SPNSStringDataUsingLossyEncoding(self, @selector(dataUsingEncoding:allowLossyConversion:), encoding, lossy); +} + +@interface NSString (MCPStringAdditions) + +@end diff --git a/sequel-pro.xcodeproj/project.pbxproj b/sequel-pro.xcodeproj/project.pbxproj index f3dce8ee..2fc06466 100644 --- a/sequel-pro.xcodeproj/project.pbxproj +++ b/sequel-pro.xcodeproj/project.pbxproj @@ -60,6 +60,7 @@ 17CC97F310B4ABE90034CD7A /* SPAboutController.m in Sources */ = {isa = PBXBuildFile; fileRef = 17CC97F210B4ABE90034CD7A /* SPAboutController.m */; }; 17CC97F710B4AC6C0034CD7A /* AboutPanel.xib in Resources */ = {isa = PBXBuildFile; fileRef = 17CC97F510B4AC6C0034CD7A /* AboutPanel.xib */; }; 17CC993B10B4C9C80034CD7A /* License.rtf in Resources */ = {isa = PBXBuildFile; fileRef = 17CC993A10B4C9C80034CD7A /* License.rtf */; }; + 17DCC5C7115C202700F89A00 /* MCPStringAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = 17DCC5C5115C202700F89A00 /* MCPStringAdditions.h */; }; 17DD52B7115071D0007D8950 /* sequel-pro-print-template.html in Resources */ = {isa = PBXBuildFile; fileRef = 17DD52B6115071D0007D8950 /* sequel-pro-print-template.html */; }; 17DD52B911507217007D8950 /* Credits.rtf in Resources */ = {isa = PBXBuildFile; fileRef = 17DD52B811507217007D8950 /* Credits.rtf */; }; 17DD52C3115074B3007D8950 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 17DD52C1115074B3007D8950 /* InfoPlist.strings */; }; @@ -403,6 +404,7 @@ 17CC97F610B4AC6C0034CD7A /* English */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = English; path = Interfaces/English.lproj/AboutPanel.xib; sourceTree = ""; }; 17CC993A10B4C9C80034CD7A /* License.rtf */ = {isa = PBXFileReference; lastKnownFileType = text.rtf; path = License.rtf; sourceTree = ""; }; 17DA04EA0FC1A7DA00D66140 /* Unit Tests-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "Unit Tests-Info.plist"; sourceTree = ""; }; + 17DCC5C5115C202700F89A00 /* MCPStringAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MCPStringAdditions.h; sourceTree = ""; }; 17DD52B6115071D0007D8950 /* sequel-pro-print-template.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = "sequel-pro-print-template.html"; sourceTree = ""; }; 17DD52B811507217007D8950 /* Credits.rtf */ = {isa = PBXFileReference; lastKnownFileType = text.rtf; path = Credits.rtf; sourceTree = ""; }; 17DD52C2115074B3007D8950 /* English */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.strings; name = English; path = English.lproj/InfoPlist.strings; sourceTree = ""; }; @@ -975,7 +977,6 @@ children = ( 17B7B5C4101603B200F057DE /* MCPConnection.h */, 17B7B5C5101603B200F057DE /* MCPConnection.m */, - 17B7B5C6101603B200F057DE /* MCPConnectionProxy.h */, 17B7B5C7101603B200F057DE /* MCPConstants.h */, 17B7B5C8101603B200F057DE /* MCPFastQueries.h */, 17B7B5C9101603B200F057DE /* MCPFastQueries.m */, @@ -990,6 +991,8 @@ 17B7B5D2101603B200F057DE /* MCPResultPlus.m */, 583B779710386B0200B21F7E /* MCPStreamingResult.h */, 583B779810386B0200B21F7E /* MCPStreamingResult.m */, + 17DCC5C5115C202700F89A00 /* MCPStringAdditions.h */, + 17B7B5C6101603B200F057DE /* MCPConnectionProxy.h */, ); path = MCPFoundationKit; sourceTree = ""; @@ -1506,6 +1509,7 @@ 17B7B5EF101603D200F057DE /* mysql_time.h in Headers */, 17B7B5F0101603D200F057DE /* mysql_version.h in Headers */, 17B7B5F1101603D200F057DE /* typelib.h in Headers */, + 17DCC5C7115C202700F89A00 /* MCPStringAdditions.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; -- cgit v1.2.3