Skip to content

Commit 7f35608

Browse files
committed
* More formatting changes
* Fix an unintended switch/case fallthrough (the error got caught soon afterwards, so this never really caused problems) * Fix a typo in the mysql fallback charset mappings (119 should’ve been 199) * Fix a charset confusion issue (SPMySQL’s compiled-in C strings are in UTF-8, not the connection encoding of the mysql connection)
1 parent 5bd7cde commit 7f35608

23 files changed

+260
-327
lines changed

Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Conversion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Encoding.m
2+
// Conversion.h
33
// SPMySQLFramework
44
//
55
// Created by Rowan Beentje (rowan.beent.je) on January 22, 2012

Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Conversion.m

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Encoding.m
2+
// Conversion.m
33
// SPMySQLFramework
44
//
55
// Created by Rowan Beentje (rowan.beent.je) on January 22, 2012
@@ -47,7 +47,6 @@ @implementation SPMySQLConnection (Conversion)
4747
But the "string" can already contain NUL bytes, so it's not a valid c string anyway.
4848
+ (const char *)_cStringForString:(NSString *)aString usingEncoding:(NSStringEncoding)anEncoding returningLengthAs:(NSUInteger *)cStringLengthPointer
4949
{
50-
5150
// Don't try and convert nil strings
5251
if (!aString) return NULL;
5352

@@ -73,7 +72,6 @@ + (const char *)_cStringForString:(NSString *)aString usingEncoding:(NSStringEnc
7372
*/
7473
- (const char *)_cStringForString:(NSString *)aString
7574
{
76-
7775
// Use a cached reference to avoid dynamic method overhead
7876
return _cStringForStringWithEncoding(aString, stringEncoding, NULL);
7977
}

Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Databases & Tables.m

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ @implementation SPMySQLConnection (Databases_and_Tables)
4545
*/
4646
- (BOOL)selectDatabase:(NSString *)aDatabase
4747
{
48-
4948
// If no database was supplied, can't deselected - return NO.
5049
if (!aDatabase) return NO;
5150

@@ -96,7 +95,6 @@ - (BOOL)selectDatabase:(NSString *)aDatabase
9695
*/
9796
- (NSArray *)databases
9897
{
99-
10098
// Wrap the related databasesLike: function to avoid code duplication
10199
return [self databasesLike:nil];
102100
}
@@ -151,7 +149,6 @@ - (NSArray *)databasesLike:(NSString *)nameLikeString
151149
*/
152150
- (NSArray *)tables
153151
{
154-
155152
// Wrap the related tablesLike:fromDatabase: function to avoid code duplication
156153
return [self tablesLike:nil fromDatabase:nil];
157154
}
@@ -166,10 +163,8 @@ - (NSArray *)tables
166163
*/
167164
- (NSArray *)tablesLike:(NSString *)nameLikeString
168165
{
169-
170166
// Wrap the related tablesLike:fromDatabase: function to avoid code duplication
171167
return [self tablesLike:nameLikeString fromDatabase:nil];
172-
173168
}
174169

175170
/**
@@ -180,10 +175,8 @@ - (NSArray *)tablesLike:(NSString *)nameLikeString
180175
*/
181176
- (NSArray *)tablesFromDatabase:(NSString *)aDatabase
182177
{
183-
184178
// Wrap the related tablesLike:fromDatabase: function to avoid code duplication
185179
return [self tablesLike:nil fromDatabase:aDatabase];
186-
187180
}
188181

189182
/**
@@ -246,7 +239,6 @@ @implementation SPMySQLConnection (Databases_and_Tables_Private_API)
246239
*/
247240
- (BOOL)_storeAndAlterEncodingToUTF8IfRequired
248241
{
249-
250242
// If the encoding is already UTF8, no change is required.
251243
if ([encoding isEqualToString:@"utf8"] && !encodingUsesLatin1Transport) return NO;
252244

Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Encoding.m

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ - (BOOL)encodingUsesLatin1Transport
8282
*/
8383
- (BOOL)setEncoding:(NSString *)theEncoding
8484
{
85-
8685
// MySQL versions prior to 4.1 don't support encoding changes; return NO on those
8786
// versions.
8887
if (![self serverVersionIsGreaterThanOrEqualTo:4 minorVersion:1 releaseVersion:0]) {
@@ -116,14 +115,13 @@ - (BOOL)setEncoding:(NSString *)theEncoding
116115
* Latin1 transport is a compatibility mode in place for compatibility with older
117116
* incorrect setups, where databases and clients might both be set to use UTF8 (or
118117
* other encodings) for storing and retrieving data, but the MySQL link was never
119-
* set to UTF8 mode; as a result, multibyte characters where split by the connection
118+
* set to UTF8 mode; as a result, multibyte characters were split by the connection
120119
* into pairs of characters, resulting in malformed storage. The data works
121120
* correctly if written and read in the same way, so this mode allows correct display
122121
* of that data.
123122
*/
124123
- (BOOL)setEncodingUsesLatin1Transport:(BOOL)useLatin1
125124
{
126-
127125
// MySQL versions prior to 4.1 don't support encoding changes; return NO on those
128126
// versions.
129127
if (![self serverVersionIsGreaterThanOrEqualTo:4 minorVersion:1 releaseVersion:0]) {
@@ -208,7 +206,6 @@ - (void)restoreStoredEncoding
208206
*/
209207
+ (NSStringEncoding)stringEncodingForMySQLCharset:(const char *)mysqlCharset
210208
{
211-
212209
// Handle the most common cases first
213210
if (!strcmp(mysqlCharset, "utf8")) {
214211
return NSUTF8StringEncoding;
@@ -337,7 +334,6 @@ + (NSStringEncoding)stringEncodingForMySQLCharset:(const char *)mysqlCharset
337334
*/
338335
+ (NSString *)mySQLCharsetForStringEncoding:(NSStringEncoding)aStringEncoding
339336
{
340-
341337
// Switch through the list of NSStringEncodings from NSString, returning the most
342338
// appropriate encoding for each
343339
switch (aStringEncoding) {

Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Locking.m

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,11 @@ @implementation SPMySQLConnection (Locking)
4242
*/
4343
- (void)_lockConnection
4444
{
45-
46-
// We can only start a query when the condition is SPMySQLConnectionIdle
45+
// We can only start a query when the condition is SPMySQLConnectionIdle
4746
[connectionLock lockWhenCondition:SPMySQLConnectionIdle];
48-
49-
// Set the condition to SPMySQLConnectionBusy
50-
[connectionLock unlockWithCondition:SPMySQLConnectionBusy];
47+
48+
// Set the condition to SPMySQLConnectionBusy
49+
[connectionLock unlockWithCondition:SPMySQLConnectionBusy];
5150
}
5251

5352
/**
@@ -58,7 +57,6 @@ - (void)_lockConnection
5857
*/
5958
- (BOOL)_tryLockConnection
6059
{
61-
6260
// If the connection is already is use, return failure
6361
if (![connectionLock tryLockWhenCondition:SPMySQLConnectionIdle]) {
6462
return NO;
@@ -75,28 +73,31 @@ - (BOOL)_tryLockConnection
7573
*/
7674
- (void)_unlockConnection
7775
{
76+
// Always lock the conditional lock before proceeding
77+
[connectionLock lock];
78+
79+
// Check if the connection actually was busy. If it wasn't busy,
80+
// it means the connection may have been unlocked twice. This is
81+
// potentially dangerous, so we log this to the console
82+
if ([connectionLock condition] != SPMySQLConnectionBusy) {
83+
NSLog(@"SPMySQLConnection: Tried to unlock the connection, but it wasn't locked.");
84+
}
85+
86+
// Since we connected with CLIENT_MULTI_RESULT, we must make sure there are not more results!
87+
// This is still a bit of a dirty hack
88+
if (
89+
state == SPMySQLConnected &&
90+
mySQLConnection &&
91+
mySQLConnection->net.vio &&
92+
mySQLConnection->net.buff &&
93+
mysql_more_results(mySQLConnection)
94+
) {
95+
NSLog(@"SPMySQLConnection: Discarding unretrieved results. This is currently normal when using CALL.");
96+
[self _flushMultipleResultSets];
97+
}
7898

79-
// Always lock the conditional lock before proceeding
80-
[connectionLock lock];
81-
82-
// Check if the connection actually was busy. If it wasn't busy,
83-
// it means the connection may have been unlocked twice. This is
84-
// potentially dangerous, so we log this to the console
85-
if ([connectionLock condition] != SPMySQLConnectionBusy) {
86-
NSLog(@"SPMySQLConnection: Tried to unlock the connection, but it wasn't locked.");
87-
}
88-
89-
// Since we connected with CLIENT_MULTI_RESULT, we must make sure there are not more results!
90-
// This is still a bit of a dirty hack
91-
if (state == SPMySQLConnected
92-
&& mySQLConnection && mySQLConnection->net.vio && mySQLConnection->net.buff && mysql_more_results(mySQLConnection))
93-
{
94-
NSLog(@"SPMySQLConnection: Discarding unretrieved results. This is currently normal when using CALL.");
95-
[self _flushMultipleResultSets];
96-
}
97-
98-
// Tell everyone that the connection is available again
99-
[connectionLock unlockWithCondition:SPMySQLConnectionIdle];
99+
// Tell everyone that the connection is available again
100+
[connectionLock unlockWithCondition:SPMySQLConnectionIdle];
100101
}
101102

102103
@end

Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Max Packet Size.m

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ - (BOOL)isMaxQuerySizeEditable
6767
*/
6868
- (NSUInteger)setGlobalMaxQuerySize:(NSUInteger)newMaxSize
6969
{
70-
7170
// Perform basic validation. First, ensure the max query size is editable
7271
if (![self isMaxQuerySizeEditable]) return NSNotFound;
7372

@@ -114,8 +113,7 @@ - (NSInteger)_queryMaxAllowedPacketWithSQL:(NSString *)query resultInColumn:(NSU
114113

115114
NSInteger _maxQuerySize = maxQuerySizeString ? [maxQuerySizeString integerValue] : 0;
116115

117-
if(_maxQuerySize == 0)
118-
NSLog(@"Query (%@) for max_allowed_packet returned invalid value: %ld (raw value: %@) (on %@)", query, _maxQuerySize, maxQuerySizeString, [self serverVersionString]);
116+
if(_maxQuerySize == 0) NSLog(@"Query (%@) for max_allowed_packet returned invalid value: %ld (raw value: %@) (on %@)", query, _maxQuerySize, maxQuerySizeString, [self serverVersionString]);
119117

120118
return _maxQuerySize;
121119
}
@@ -164,12 +162,10 @@ - (void)_updateMaxQuerySizeEditability
164162
*/
165163
- (BOOL)_attemptMaxQuerySizeIncreaseTo:(NSUInteger)targetSize
166164
{
167-
168165
// If the query size is editable, attempt to increase the size
169166
if ([self isMaxQuerySizeEditable]) {
170167
NSUInteger newSize = [self setGlobalMaxQuerySize:targetSize];
171168
if (newSize != NSNotFound) {
172-
173169
// Successfully increased the global size - reconnect to use it, and return success
174170
[self _reconnectAllowingRetries:YES];
175171
return YES;
@@ -185,6 +181,7 @@ - (BOOL)_attemptMaxQuerySizeIncreaseTo:(NSUInteger)targetSize
185181
[delegate queryGaveError:errorMessage connection:self];
186182
}
187183

184+
//TODO Setting the last error above should be enough at the framework level. Also this is the only case where -showErrorWithTitle:message: is even used, so we should get rid of it.
188185
// Display an alert as this is a special failure
189186
if ([delegate respondsToSelector:@selector(showErrorWithTitle:message:)]) {
190187
[delegate showErrorWithTitle:NSLocalizedString(@"Error", @"error") message:errorMessage];
@@ -202,7 +199,6 @@ - (BOOL)_attemptMaxQuerySizeIncreaseTo:(NSUInteger)targetSize
202199
*/
203200
- (void)_restoreMaximumQuerySizeAfterQuery
204201
{
205-
206202
// Return if no action needs to be performed
207203
if (queryActionShouldRestoreMaxQuerySize == NSNotFound) return;
208204

Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Ping & KeepAlive.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,6 @@
3030

3131
// This class is private to the framework.
3232

33-
typedef struct {
34-
MYSQL *mySQLConnection;
35-
volatile BOOL *keepAlivePingThreadActivePointer;
36-
volatile BOOL *keepAliveLastPingSuccessPointer;
37-
void *parentId;
38-
} SPMySQLConnectionPingDetails;
39-
4033
@interface SPMySQLConnection (Ping_and_KeepAlive)
4134

4235
// Keepalive ping initialisation

Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Ping & KeepAlive.m

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@
3535
#import <pthread.h>
3636
#include <stdio.h>
3737

38+
typedef struct {
39+
MYSQL *mySQLConnection;
40+
volatile BOOL *keepAlivePingThreadActivePointer;
41+
volatile BOOL *keepAliveLastPingSuccessPointer;
42+
void *parentId;
43+
} SPMySQLConnectionPingDetails;
44+
3845
@implementation SPMySQLConnection (Ping_and_KeepAlive)
3946

4047
#pragma mark -
@@ -47,7 +54,6 @@ @implementation SPMySQLConnection (Ping_and_KeepAlive)
4754
*/
4855
- (void)_keepAlive
4956
{
50-
5157
// Do nothing if not connected, if keepalive is disabled, or a keepalive is in
5258
// progress.
5359
if (state != SPMySQLConnected || !useKeepAlive) return;
@@ -185,10 +191,11 @@ - (BOOL)_pingConnectionUsingLoopDelay:(NSUInteger)loopDelay
185191

186192
// If the ping timeout has been exceeded, or the ping thread has been
187193
// cancelled, force a timeout; double-check that the thread is still active.
188-
if (([[NSThread currentThread] isCancelled] || pingElapsedTime > pingTimeout)
194+
if (
195+
([[NSThread currentThread] isCancelled] || pingElapsedTime > pingTimeout)
189196
&& keepAlivePingThreadActive
190-
&& !threadCancelled)
191-
{
197+
&& !threadCancelled
198+
) {
192199
pthread_cancel(keepAlivePingThread_t);
193200
threadCancelled = YES;
194201

@@ -209,7 +216,7 @@ - (BOOL)_pingConnectionUsingLoopDelay:(NSUInteger)loopDelay
209216
keepAlivePingThread_t = NULL;
210217
pthread_attr_destroy(&attr);
211218

212-
// Unlock the connection
219+
// Unlock the connection
213220
[self _unlockConnection];
214221

215222
return keepAliveLastPingSuccess;
@@ -278,7 +285,6 @@ void _pingThreadCleanup(void *pingDetails)
278285
*/
279286
- (BOOL)_cancelKeepAlives
280287
{
281-
282288
// If no keepalive thread is active, return
283289
if (keepAliveThread) {
284290

Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Querying & Preparation.m

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ - (NSString *)escapeAndQuoteString:(NSString *)theString
6464
*/
6565
- (NSString *)escapeString:(NSString *)theString includingQuotes:(BOOL)includeQuotes
6666
{
67-
6867
// Return nil strings untouched
6968
if (!theString) return theString;
7069

@@ -137,7 +136,6 @@ - (NSString *)escapeAndQuoteData:(NSData *)theData
137136
*/
138137
- (NSString *)escapeData:(NSData *)theData includingQuotes:(BOOL)includeQuotes
139138
{
140-
141139
// Return nil datas as nil strings
142140
if (!theData) return nil;
143141

@@ -345,7 +343,7 @@ - (id)queryString:(NSString *)theQueryString usingEncoding:(NSStringEncoding)the
345343

346344
} while (--queryAttemptsAllowed > 0);
347345

348-
id theResult = nil;
346+
SPMySQLResult *theResult = nil;
349347

350348
// On success, if there is a query result, retrieve the result data type
351349
if (!queryStatus) {
@@ -549,7 +547,6 @@ + (BOOL)isErrorIDConnectionError:(NSUInteger)theErrorID
549547
*/
550548
- (void)cancelCurrentQuery
551549
{
552-
553550
// If not connected, no action is required
554551
if (state != SPMySQLConnected && state != SPMySQLDisconnecting) return;
555552

@@ -656,7 +653,6 @@ @implementation SPMySQLConnection (Querying_and_Preparation_Private_API)
656653
*/
657654
- (void)_flushMultipleResultSets
658655
{
659-
660656
// Repeat as long as there are results
661657
while (!mysql_next_result(mySQLConnection)) {
662658
MYSQL_RES *eachResult = mysql_use_result(mySQLConnection);
@@ -691,7 +687,6 @@ - (void)_updateLastErrorInfos
691687
*/
692688
- (void)_updateLastErrorMessage:(NSString *)theErrorMessage
693689
{
694-
695690
// If an error message wasn't supplied, select one from the connection
696691
if (!theErrorMessage) {
697692
theErrorMessage = [self _stringForCString:mysql_error(mySQLConnection)];
@@ -714,7 +709,6 @@ - (void)_updateLastErrorMessage:(NSString *)theErrorMessage
714709
*/
715710
- (void)_updateLastErrorID:(NSUInteger)theErrorID
716711
{
717-
718712
// If NSNotFound was supplied as the ID, ask the connection for the last error
719713
if (theErrorID == NSNotFound) {
720714
queryErrorID = mysql_errno(mySQLConnection);

Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Server Info.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ - (SPMySQLResult *)listProcesses
136136
*/
137137
- (BOOL)killQueryOnThreadID:(unsigned long)theThreadID
138138
{
139-
140139
// Note that mysql_kill has been deprecated, so use a query to perform this task.
141140
NSMutableString *killQuery = [NSMutableString stringWithString:@"KILL"];
142141
if ([self serverVersionIsGreaterThanOrEqualTo:5 minorVersion:0 releaseVersion:0]) {

0 commit comments

Comments
 (0)