diff options
Diffstat (limited to 'Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories')
3 files changed, 40 insertions, 0 deletions
diff --git a/Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Databases & Tables.m b/Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Databases & Tables.m index dd84ff55..0b9651f6 100644 --- a/Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Databases & Tables.m +++ b/Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Databases & Tables.m @@ -63,11 +63,13 @@ if (encodingChangeRequired) { NSString *theErrorString = [self lastErrorMessage]; NSUInteger theErrorID = [self lastErrorID]; + NSString *theSqlstate = [self lastSqlstate]; [self restoreStoredEncoding]; [self _updateLastErrorMessage:theErrorString]; [self _updateLastErrorID:theErrorID]; + [self _updateLastSqlstate:theSqlstate]; } return NO; diff --git a/Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Querying & Preparation.h b/Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Querying & Preparation.h index 4509fdc9..a595cfb2 100644 --- a/Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Querying & Preparation.h +++ b/Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Querying & Preparation.h @@ -56,6 +56,7 @@ - (BOOL)queryErrored; - (NSString *)lastErrorMessage; - (NSUInteger)lastErrorID; +- (NSString *)lastSqlstate; + (BOOL)isErrorIDConnectionError:(NSUInteger)theErrorID; // Query cancellation diff --git a/Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Querying & Preparation.m b/Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Querying & Preparation.m index 80cc798d..6fce5361 100644 --- a/Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Querying & Preparation.m +++ b/Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Querying & Preparation.m @@ -227,6 +227,7 @@ double queryExecutionTime; NSString *theErrorMessage; NSUInteger theErrorID; + NSString *theSqlstate; lastQueryWasCancelled = NO; lastQueryWasCancelledUsingReconnect = NO; @@ -299,6 +300,7 @@ if (!queryStatus) { theErrorMessage = nil; theErrorID = 0; + theSqlstate = nil; break; // If the query failed, determine whether to reattempt the query @@ -307,6 +309,7 @@ // Store the error state theErrorMessage = [self _stringForCString:mysql_error(mySQLConnection)]; theErrorID = mysql_errno(mySQLConnection); + theSqlstate = [self _stringForCString:mysql_sqlstate(mySQLConnection)]; // Prevent retries if the query was cancelled or not a connection error if (lastQueryWasCancelled || ![SPMySQLConnection isErrorIDConnectionError:mysql_errno(mySQLConnection)]) { @@ -319,6 +322,7 @@ if (![self checkConnection]) { [self _updateLastErrorMessage:theErrorMessage]; [self _updateLastErrorID:theErrorID]; + [self _updateLastSqlstate:theSqlstate]; return nil; } [self _lockConnection]; @@ -365,6 +369,7 @@ // Update the error message, if appropriate, to reflect result store errors or overall success theErrorMessage = [self _stringForCString:mysql_error(mySQLConnection)]; theErrorID = mysql_errno(mySQLConnection); + theSqlstate = [self _stringForCString:mysql_sqlstate(mySQLConnection)]; } else { theResult = [[SPMySQLEmptyResult alloc] init]; } @@ -379,6 +384,7 @@ if (lastQueryWasCancelled) { theErrorMessage = NSLocalizedString(@"Query cancelled.", @"Query cancelled error"); theErrorID = 1317; + theSqlstate = @"70100"; // If the query was cancelled on a MySQL <5 server, check the connection to allow reconnects // after query kills. This is also handled within the class for internal cancellations, but @@ -403,6 +409,7 @@ // Update error string and ID, and the rows affected [self _updateLastErrorMessage:theErrorMessage]; [self _updateLastErrorID:theErrorID]; + [self _updateLastSqlstate:theSqlstate]; lastQueryAffectedRowCount = theAffectedRowCount; // Store the result time on the response object @@ -474,6 +481,12 @@ return [NSString stringWithString:queryErrorMessage]; } +- (NSString *)lastSqlstate +{ + if(!querySqlstate) return nil; + return [NSString stringWithString:querySqlstate]; +} + /** * If the last query (or connection) triggered an error, returns the error * ID; if the last query did not error, 0 is returned. @@ -687,4 +700,28 @@ } } +/** + * Update the MySQL SQLSTATE for this connection. + * @param thSqlstate If a SQLSTATE is supplied it will be stored and returned to + * anything asking the instance for the last SQLSTATE; + * if nil is supplied, the connection will be used to derive + * (or clear) the SQLSTATE string; + * if @"" is supplied the SQLSTATE will only be cleared. + */ +- (void)_updateLastSqlstate:(NSString *)theSqlstate +{ + // If a SQLSTATE wasn't supplied, select one from the connection + if(!theSqlstate) { + theSqlstate = [self _stringForCString:mysql_sqlstate(mySQLConnection)]; + } + + // Clear the last SQLSTATE stored on the instance + if(querySqlstate) [querySqlstate release], querySqlstate = nil; + + // If we have a SQLSTATE *with a length*, update the instance SQLSTATE + if(theSqlstate && [theSqlstate length]) { + querySqlstate = [[NSString alloc] initWithString:theSqlstate]; + } +} + @end |