Skip to content
Permalink
Browse files

Change the way max_allowed_packet detection works

Sequel Pro will now simply go with whatever query returns a valid result instead of trying to guess the correct query from the server version number
  • Loading branch information
dmoagx committed Jan 25, 2017
1 parent ccd802d commit 4eb2987410586f87821002dfc69b1d7aa352a430
@@ -71,6 +71,7 @@

@interface SPMySQLConnection (Max_Packet_Size_Private_API)

- (NSInteger)_queryMaxAllowedPacketWithSQL:(NSString *)query resultInColumn:(NSUInteger)colIdx;
- (void)_updateMaxQuerySize;
- (void)_updateMaxQuerySizeEditability;
- (BOOL)_attemptMaxQuerySizeIncreaseTo:(NSUInteger)targetSize;
@@ -93,40 +93,58 @@ - (NSUInteger)setGlobalMaxQuerySize:(NSUInteger)newMaxSize
@implementation SPMySQLConnection (Max_Packet_Size_Private_API)

/**
* Update the max_allowed_packet size - the largest supported query size - from the server.
* Executes a passed query for max_allowed_packet and returns the resulting number in bytes
*
* @return -1 => if the query failed
* 0 => if the query did not fail, but also did not contain a (valid) result
* * => if the query succeeded and the value is a valid integer (NOTE: this may also include -1 and 0)
*/
- (void)_updateMaxQuerySize
- (NSInteger)_queryMaxAllowedPacketWithSQL:(NSString *)query resultInColumn:(NSUInteger)colIdx
{
// Determine which query to run based on server version
NSString *packetQueryString;
NSUInteger resultCol;
if ([self serverMajorVersion] == 3) {
packetQueryString = @"SHOW VARIABLES LIKE 'max_allowed_packet'";
resultCol = 1;
} else {
packetQueryString = @"SELECT @@global.max_allowed_packet";
resultCol = 0;
}

// Make a standard query to the server to retrieve the information
SPMySQLResult *result = [self queryString:packetQueryString];
if(!result) { // query fails on sphinxql
NSLog(@"Query for max_allowed_packet failed: %@ (%lu) (on %@)", [self lastErrorMessage], [self lastErrorID], [self serverVersionString]);
return;
SPMySQLResult *result = [self queryString:query];
if(!result) {
NSLog(@"Query (%@) for max_allowed_packet failed: %@ (%lu) (on %@)", query, [self lastErrorMessage], [self lastErrorID], [self serverVersionString]);
return -1;
}
[result setReturnDataAsStrings:YES];

// Get the maximum size string
NSString *maxQuerySizeString = [[result getRowAsArray] objectAtIndex:resultCol];
NSString *maxQuerySizeString = [[result getRowAsArray] objectAtIndex:colIdx];

NSInteger _maxQuerySize = maxQuerySizeString ? [maxQuerySizeString integerValue] : 0;
//see #2653
if(_maxQuerySize < 34) { // the max_allowed_packet query above has at least 34 bytes and succeeded, so any value less than that would be nonsense
NSLog(@"Query for max_allowed_packet returned invalid or implausible value: %ld (raw value: %@) (on %@)", _maxQuerySize, maxQuerySizeString, [self serverVersionString]);
return;

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

return _maxQuerySize;
}

/**
* Update the max_allowed_packet size - the largest supported query size - from the server.
*/
- (void)_updateMaxQuerySize
{
struct {
NSString *sql;
NSUInteger col;
} queryVariants[] = {
{ .sql = @"SELECT @@global.max_allowed_packet", .col = 0 }, //works on mysql 4+
{ .sql = @"SHOW VARIABLES LIKE 'max_allowed_packet'", .col = 1 }, //works on mysql 3, sphinx
{ .sql = nil, .col = 0 }, //terminator element
};

int i = 0;
while(queryVariants[i].sql) {
NSInteger _maxQuerySize = [self _queryMaxAllowedPacketWithSQL:queryVariants[i].sql resultInColumn:queryVariants[i].col];
//see #2653
if(_maxQuerySize >= 34) { // the max_allowed_packet query above has at least 34 bytes, so any value less than that would be nonsense
// If a valid size was returned, update the instance variable
maxQuerySize = (NSUInteger)_maxQuerySize;
return;
}
i++;
}
// If a valid size was returned, update the instance variable
maxQuerySize = (NSUInteger)_maxQuerySize;
}

/**

0 comments on commit 4eb2987

Please sign in to comment.
You can’t perform that action at this time.