From 5b6f8231ebfeea5d24fa6163325c0215d3829e7c Mon Sep 17 00:00:00 2001 From: rowanbeentje Date: Thu, 9 Apr 2009 22:50:38 +0000 Subject: - If a connection is reestablished automatically by the mysql libraries, ensure the connection encoding is similarly reset. This addresses Issue #79. --- Source/CMMCPConnection.h | 1 + Source/CMMCPConnection.m | 23 +++++++++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) (limited to 'Source') diff --git a/Source/CMMCPConnection.h b/Source/CMMCPConnection.h index eece0d3c..bdda78ec 100644 --- a/Source/CMMCPConnection.h +++ b/Source/CMMCPConnection.h @@ -30,6 +30,7 @@ - (void)willQueryString:(NSString *)query; - (void)queryGaveError:(NSString *)error; +- (BOOL)connectionEncodingViaLatin1; @end diff --git a/Source/CMMCPConnection.m b/Source/CMMCPConnection.m index 41a828e2..9f8c78fa 100644 --- a/Source/CMMCPConnection.m +++ b/Source/CMMCPConnection.m @@ -146,8 +146,8 @@ static void forcePingTimeout(int signalNumber); if (delegate && [delegate valueForKey:@"_encoding"]) { currentEncoding = [NSString stringWithString:[delegate valueForKey:@"_encoding"]]; } - if (delegate && [delegate boolForKey:@"_encodingViaLatin1"]) { - currentEncodingUsesLatin1Transport = [delegate boolForKey:@"_encodingViaLatin1"]; + if (delegate && [delegate respondsToSelector:@selector(connectionEncodingViaLatin1)]) { + currentEncodingUsesLatin1Transport = [delegate connectionEncodingViaLatin1]; } // Close the connection if it exists. @@ -178,10 +178,10 @@ static void forcePingTimeout(int signalNumber); [self selectDB:currentDatabase]; } if (currentEncoding) { - [self queryString:[NSString stringWithFormat:@"SET NAMES '%@'", currentEncoding]]; + [self queryString:[NSString stringWithFormat:@"/*!40101 SET NAMES '%@' */", currentEncoding]]; [self setEncoding:[CMMCPConnection encodingForMySQLEncoding:[currentEncoding UTF8String]]]; if (currentEncodingUsesLatin1Transport) { - [self queryString:@"SET CHARACTER_SET_RESULTS=latin1"]; + [self queryString:@"/*!40101 SET CHARACTER_SET_RESULTS=latin1 */"]; } } } else if (parentWindow) { @@ -444,10 +444,15 @@ static void forcePingTimeout(int signalNumber); */ - (BOOL)checkConnection { + unsigned long threadid; + if (!mConnected) return NO; BOOL connectionVerified = FALSE; + // Get the current thread ID for this connection + threadid = mConnection->thread_id; + // Check whether the connection is still operational via a wrapped version of MySQL ping. connectionVerified = [self pingConnection]; @@ -474,6 +479,16 @@ static void forcePingTimeout(int signalNumber); default: return [self checkConnection]; } + + // If a connection exists, check whether the thread id differs; if so, the connection has + // probably been reestablished and we need to reset the connection encoding + } else if (threadid != mConnection->thread_id) { + if (delegate && [delegate valueForKey:@"_encoding"]) { + [self queryString:[NSString stringWithFormat:@"/*!40101 SET NAMES '%@' */", [NSString stringWithString:[delegate valueForKey:@"_encoding"]]]]; + if (delegate && [delegate respondsToSelector:@selector(connectionEncodingViaLatin1)]) { + if ([delegate connectionEncodingViaLatin1]) [self queryString:@"/*!40101 SET CHARACTER_SET_RESULTS=latin1 */"]; + } + } } return connectionVerified; -- cgit v1.2.3