aboutsummaryrefslogtreecommitdiffstats
path: root/Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Server Info.m
diff options
context:
space:
mode:
authorrowanbeentje <rowan@beent.je>2012-02-20 00:51:47 +0000
committerrowanbeentje <rowan@beent.je>2012-02-20 00:51:47 +0000
commitcc3c436432e592d15c46a4a9e38c6cb689eca0ee (patch)
tree2b0c17791b8a62ff2ab707a8bb082e563bec8c80 /Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Server Info.m
parentbe5b96f6d79fecec53da93be6bb4d10df90e9ef5 (diff)
downloadsequelpro-cc3c436432e592d15c46a4a9e38c6cb689eca0ee.tar.gz
sequelpro-cc3c436432e592d15c46a4a9e38c6cb689eca0ee.tar.bz2
sequelpro-cc3c436432e592d15c46a4a9e38c6cb689eca0ee.zip
Initial commit of the new SPMySQL Framework, which is added to the project and ready for use but not yet integrated. This new framework should provide much of the functionality required from MCPKit and is based around its interface for relatively easy integration. The largest missing component is Hans' structure code which I believe is better placed outside the framework.
From the Readme file: The SPMySQL Framework is intended to provide a stable MySQL connection framework, with the ability to run text-based queries and rapidly retrieve result sets with conversion from MySQL data types to Cocoa objects. SPMySQL.framework has an interface loosely based around that provided by MCPKit by Serge Cohen and Bertrand Mansion (http://mysql-cocoa.sourceforge.net/), and in particular the heavily modified Sequel Pro version (http://www.sequelpro.com/). It is a full rewrite of the original framework, although it includes code from patches implementing the following Sequel Pro functionality, largely contributed by Hans-Jörg Bibiko, Stuart Connolly, Jakob Egger, and Rowan Beentje: - Connection locking (Jakob et al) - Ping & keepalive (Rowan et al) - Query cancellation (Rowan et al) - Delegate setup (Stuart et al) - SSL support (Rowan et al) - Connection checking (Rowan et al) - Version state (Stuart et al) - Maximum packet size control (Hans et al) - Result multithreading and streaming (Rowan et al) - Improved encoding support & switching (Rowan et al) - Database structure; moved to inside the app (Hans et al) - Query reattempts and error-handling approach (Rowan et al) - Geometry result class (Hans et al) - Connection proxy (Stuart et al)
Diffstat (limited to 'Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Server Info.m')
-rw-r--r--Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Server Info.m175
1 files changed, 175 insertions, 0 deletions
diff --git a/Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Server Info.m b/Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Server Info.m
new file mode 100644
index 00000000..f695d977
--- /dev/null
+++ b/Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Server Info.m
@@ -0,0 +1,175 @@
+//
+// $Id$
+//
+// Server Info.m
+// SPMySQLFramework
+//
+// Created by Rowan Beentje (rowan.beent.je) on January 14, 2012
+// Copyright (c) 2012 Rowan Beentje. All rights reserved.
+//
+// Permission is hereby granted, free of charge, to any person
+// obtaining a copy of this software and associated documentation
+// files (the "Software"), to deal in the Software without
+// restriction, including without limitation the rights to use,
+// copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the
+// Software is furnished to do so, subject to the following
+// conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+// OTHER DEALINGS IN THE SOFTWARE.
+//
+// More info at <http://code.google.com/p/sequel-pro/>
+
+
+#import "Server Info.h"
+#import "SPMySQL Private APIs.h"
+
+@implementation SPMySQLConnection (Server_Info)
+
+#pragma mark -
+#pragma mark Server version information
+
+/**
+ * Return the server version string, or nil on failure.
+ */
+- (NSString *)serverVersionString
+{
+ if (serverVersionString) {
+ return [NSString stringWithString:serverVersionString];
+ }
+
+ return nil;
+}
+
+/**
+ * Return the server major version or NSNotFound on failure
+ */
+- (NSUInteger)serverMajorVersion
+{
+
+ if (serverVersionString != nil) {
+ NSString *s = [[serverVersionString componentsSeparatedByString:@"."] objectAtIndex:0];
+ return (NSUInteger)[s integerValue];
+ }
+
+ return NSNotFound;
+}
+
+/**
+ * Return the server minor version or NSNotFound on failure
+ */
+- (NSUInteger)serverMinorVersion
+{
+ if (serverVersionString != nil) {
+ NSString *s = [[serverVersionString componentsSeparatedByString:@"."] objectAtIndex:1];
+ return (NSUInteger)[s integerValue];
+ }
+
+ return NSNotFound;
+}
+
+/**
+ * Return the server release version or NSNotFound on failure
+ */
+- (NSUInteger)serverReleaseVersion
+{
+ if (serverVersionString != nil) {
+ NSString *s = [[serverVersionString componentsSeparatedByString:@"."] objectAtIndex:2];
+ return (NSUInteger)[[[s componentsSeparatedByString:@"-"] objectAtIndex:0] integerValue];
+ }
+
+ return NSNotFound;
+}
+
+#pragma mark -
+#pragma mark Server version comparisons
+
+/**
+ * Returns whether the connected server version is greater than or equal to the
+ * supplied version number. Returns NO if no connection is active.
+ */
+- (BOOL)serverVersionIsGreaterThanOrEqualTo:(NSUInteger)aMajorVersion minorVersion:(NSUInteger)aMinorVersion releaseVersion:(NSUInteger)aReleaseVersion
+{
+ if (!serverVersionString) return NO;
+
+ NSArray *serverVersionParts = [serverVersionString componentsSeparatedByString:@"."];
+
+ NSUInteger serverMajorVersion = (NSUInteger)[[serverVersionParts objectAtIndex:0] integerValue];
+ if (serverMajorVersion < aMajorVersion) return NO;
+ if (serverMajorVersion > aMajorVersion) return YES;
+
+ NSUInteger serverMinorVersion = (NSUInteger)[[serverVersionParts objectAtIndex:1] integerValue];
+ if (serverMinorVersion < aMinorVersion) return NO;
+ if (serverMinorVersion > aMinorVersion) return YES;
+
+ NSString *serverReleasePart = [serverVersionParts objectAtIndex:2];
+ NSUInteger serverReleaseVersion = (NSUInteger)[[[serverReleasePart componentsSeparatedByString:@"-"] objectAtIndex:0] integerValue];
+ if (serverReleaseVersion < aReleaseVersion) return NO;
+ return YES;
+}
+
+#pragma mark -
+#pragma mark Server tasks & processes
+
+/**
+ * Returns a result set describing the current server threads and their tasks. Note that
+ * the resulting process list defaults to the short form; run a manual SHOW FULL PROCESSLIST
+ * to retrieve tasks in non-truncated form.
+ * Returns nil on error.
+ */
+- (SPMySQLResult *)listProcesses
+{
+ if (state != SPMySQLConnected) return nil;
+
+ // Check the connection if appropriate
+ if (![self _checkConnectionIfNecessary]) return nil;
+
+ // Lock the connection before using it
+ [self _lockConnection];
+
+ // Get the process list
+ MYSQL_RES *mysqlResult = mysql_list_processes(mySQLConnection);
+
+ // Convert to SPMySQLResult
+ SPMySQLResult *theResult = [[SPMySQLResult alloc] initWithMySQLResult:mysqlResult stringEncoding:stringEncoding];
+
+ // Unlock and return
+ [self _unlockConnection];
+ return [theResult autorelease];
+}
+
+/**
+ * Kill the process with the supplied thread ID. On MySQL version 5 or later, this kills
+ * the query; on older servers this kills the entire connection. Note that the SUPER
+ * privilege is required to kill queries and processes not belonging to the currently
+ * connected user, while only PROCESS is required to see other user's processes.
+ * Returns a boolean indicating success or failure.
+ */
+- (BOOL)killQueryOnThreadID:(unsigned long)theThreadID
+{
+
+ // Note that mysql_kill has been deprecated, so use a query to perform this task.
+ NSMutableString *killQuery = [NSMutableString stringWithString:@"KILL"];
+ if ([self serverVersionIsGreaterThanOrEqualTo:5 minorVersion:0 releaseVersion:0]) {
+ [killQuery appendString:@" QUERY"];
+ }
+ [killQuery appendFormat:@" %lu", theThreadID];
+
+ // Run the query
+ [self queryString:killQuery];
+
+ // Return a value based on whether the query errored or not
+ return ![self queryErrored];
+}
+
+@end \ No newline at end of file