// // MGMMyConnection.m // MGMDB // // Created by Mr. Gecko on 1/20/10. // Copyright (c) 2011 Mr. Gecko's Media (James Coleman). http://mrgeckosmedia.com/ // // Permission to use, copy, modify, and/or distribute this software for any purpose // with or without fee is hereby granted, provided that the above copyright notice // and this permission notice appear in all copies. // // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND // FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, // OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, // DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS // ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. // #import "MGMMyConnection.h" #import "MGMMyResult.h" @implementation MGMMyConnection - (id)init { if (self = [super init]) { timeZone = [[NSTimeZone defaultTimeZone] retain]; isConnected = NO; } return self; } + (id)connectionWithHost:(NSString *)theHost port:(int)thePort username:(NSString *)theUsername password:(NSString *)thePassword { return [[[self alloc] initWithHost:theHost port:thePort username:theUsername password:thePassword] autorelease]; } - (id)initWithHost:(NSString *)theHost port:(int)thePort username:(NSString *)theUsername password:(NSString *)thePassword { if (self = [self init]) { logQuery = NO; [self connectWithHost:theHost port:thePort username:theUsername password:thePassword]; escapeSet = [[NSCharacterSet characterSetWithCharactersInString:@"'\"\\"] retain]; } return self; } + (id)connectionWithSocket:(NSString *)theSocket username:(NSString *)theUsername password:(NSString *)thePassword { return [[[self alloc] initWithSocket:theSocket username:theUsername password:thePassword] autorelease]; } - (id)initWithSocket:(NSString *)theSocket username:(NSString *)theUsername password:(NSString *)thePassword { if (self = [self init]) { logQuery = NO; [self connectWithSocket:theSocket username:theUsername password:thePassword]; escapeSet = [[NSCharacterSet characterSetWithCharactersInString:@"'\"\\"] retain]; } return self; } - (void)dealloc { if (isConnected) { mysql_close(MySQLConnection); MySQLConnection = NULL; } [timeZone release]; [super dealloc]; } - (BOOL)connectWithHost:(NSString *)theHost port:(int)thePort username:(NSString *)theUsername password:(NSString *)thePassword { if (isConnected) { isConnected = NO; mysql_close(MySQLConnection); MySQLConnection = NULL; } if (MySQLConnection==NULL) { MySQLConnection = mysql_init(NULL); } if (MySQLConnection!=NULL) { if (thePort==0) thePort = 3306; if (theHost==nil || [theHost isEqualToString:@""]) return NO; const char *username = NULL; if (theUsername!=nil || [theUsername isEqualToString:@""]) username = [theUsername cStringUsingEncoding:NSUTF8StringEncoding]; const char *password = NULL; if (thePassword!=nil || [thePassword isEqualToString:@""]) password = [thePassword cStringUsingEncoding:NSUTF8StringEncoding]; void *theRet = mysql_real_connect(MySQLConnection, [theHost cStringUsingEncoding:NSUTF8StringEncoding], username, password, NULL, thePort, MYSQL_UNIX_ADDR, CLIENT_COMPRESS); isConnected = (theRet==MySQLConnection); if (isConnected) [self getTimeZone]; } return isConnected; } - (BOOL)connectWithSocket:(NSString *)theSocket username:(NSString *)theUsername password:(NSString *)thePassword { if (isConnected) { isConnected = NO; mysql_close(MySQLConnection); MySQLConnection = NULL; } if (MySQLConnection==NULL) { MySQLConnection = mysql_init(NULL); } if (MySQLConnection!=NULL) { const char *socket = NULL; if (theSocket==nil || [theSocket isEqualToString:@""]) socket = MYSQL_UNIX_ADDR; else socket = [theSocket cStringUsingEncoding:NSUTF8StringEncoding]; const char *username = NULL; if (theUsername!=nil || [theUsername isEqualToString:@""]) username = [theUsername cStringUsingEncoding:NSUTF8StringEncoding]; const char *password = NULL; if (thePassword!=nil || [thePassword isEqualToString:@""]) password = [thePassword cStringUsingEncoding:NSUTF8StringEncoding]; void *theRet = mysql_real_connect(MySQLConnection, NULL, username, password, NULL, 0, socket, CLIENT_COMPRESS); isConnected = (theRet==MySQLConnection); if (isConnected) [self getTimeZone]; } return isConnected; } - (MYSQL *)MySQLConnection { return MySQLConnection; } - (NSStringEncoding)stringEncodingWithCharSet:(NSString *)theCharSet { if ([theCharSet isEqualToString:@"utf8"]) return NSUTF8StringEncoding; if ([theCharSet isEqualToString:@"ucs2"]) return NSUnicodeStringEncoding; if ([theCharSet isEqualToString:@"ascii"]) return NSASCIIStringEncoding; if ([theCharSet isEqualToString:@"latin1"]) return NSISOLatin1StringEncoding; if ([theCharSet isEqualToString:@"macroman"]) return NSMacOSRomanStringEncoding; if ([theCharSet isEqualToString:@"latin2"]) return NSISOLatin2StringEncoding; if ([theCharSet isEqualToString:@"cp1250"]) return NSWindowsCP1250StringEncoding; if ([theCharSet isEqualToString:@"win1250"]) return NSWindowsCP1250StringEncoding; if ([theCharSet isEqualToString:@"cp1257"]) return CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingWindowsBalticRim); if ([theCharSet isEqualToString:@"latin5"]) return NSWindowsCP1254StringEncoding; if ([theCharSet isEqualToString:@"greek"]) return NSWindowsCP1253StringEncoding; if ([theCharSet isEqualToString:@"win1251ukr"]) return NSWindowsCP1251StringEncoding; if ([theCharSet isEqualToString:@"cp1251"]) return NSWindowsCP1251StringEncoding; if ([theCharSet isEqualToString:@"koi8_ru"]) return CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingKOI8_R); if ([theCharSet isEqualToString:@"koi8_ukr"]) return CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingKOI8_R); if ([theCharSet isEqualToString:@"cp1256"]) return CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingWindowsArabic); if ([theCharSet isEqualToString:@"hebrew"]) return CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingISOLatinHebrew); if ([theCharSet isEqualToString:@"ujis"]) return NSJapaneseEUCStringEncoding; if ([theCharSet isEqualToString:@"sjis"]) return NSShiftJISStringEncoding; if ([theCharSet isEqualToString:@"big5"]) return CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingBig5); if ([theCharSet isEqualToString:@"euc_kr"]) return CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingEUC_KR); return NSISOLatin1StringEncoding; } - (BOOL)isConnected { return (isConnected ? mysql_ping(MySQLConnection)==0 : NO); } - (void)disconnect { if (isConnected) { mysql_close(MySQLConnection); MySQLConnection = NULL; isConnected = NO; } } - (NSString *)errorMessage { if (isConnected) return [NSString stringWithCString:mysql_error(MySQLConnection) encoding:NSUTF8StringEncoding]; return nil; } - (unsigned int)errorID { if (isConnected) return mysql_errno(MySQLConnection); return 0; } - (NSString *)escapeData:(NSData *)theData { static const char hexdigits[] = "0123456789ABCDEF"; const size_t length = [theData length]; const char *bytes = [theData bytes]; char *stringBuffer = (char *)malloc(length * 2 + 1); char *hexBuffer = stringBuffer; for (int i=0; i> 4) & 0xF]; *hexBuffer++ = hexdigits[*bytes & 0xF]; bytes++; } *hexBuffer = '\0'; NSString *hex = [NSString stringWithUTF8String:stringBuffer]; free(stringBuffer); return hex; } - (NSString *)escapeString:(NSString *)theString { if (theString==nil) return nil; NSRange range = [theString rangeOfCharacterFromSet:escapeSet]; NSMutableString *string = [NSMutableString string]; if (range.location==NSNotFound) { [string appendString:theString]; } else { unsigned long len = [theString length]; for (unsigned long i=0; i