diff options
-rw-r--r-- | CMMCPConnection.h | 6 | ||||
-rw-r--r-- | CMMCPConnection.m | 124 | ||||
-rw-r--r-- | CMMCPResult.h | 2 | ||||
-rw-r--r-- | CMMCPResult.m | 166 |
4 files changed, 144 insertions, 154 deletions
diff --git a/CMMCPConnection.h b/CMMCPConnection.h index 3e6212ed..d564d96b 100644 --- a/CMMCPConnection.h +++ b/CMMCPConnection.h @@ -27,11 +27,11 @@ #import "CMMCPResult.h" @interface CMMCPConnection : MCPConnection { - id delegate; + id delegate; } -- (CMMCPResult *) queryString:(NSString *) query; +- (CMMCPResult *)queryString:(NSString *) query; - (void)setDelegate:(id)object; -- (NSTimeZone *) timeZone; +- (NSTimeZone *)timeZone; @end diff --git a/CMMCPConnection.m b/CMMCPConnection.m index a8ae8dac..8d4e2673 100644 --- a/CMMCPConnection.m +++ b/CMMCPConnection.m @@ -28,22 +28,23 @@ @implementation CMMCPConnection /* +Gets a proper NSStringEncoding according to the given MySQL charset. + +MySQL 4.0 offers this charsets: +big5 cp1251 cp1257 croat czech danish dec8 dos estonia euc_kr gb2312 gbk german1 greek hebrew hp8 hungarian koi8_ru koi8_ukr latin1 latin1_de latin2 latin5 sjis swe7 tis620 ujis usa7 win1250 win1251ukr + +WARNING : incomplete implementation. Please, send your fixes. + + (NSStringEncoding) encodingForMySQLEncoding:(const char *) mysqlEncoding - Gets a proper NSStringEncoding according to the given MySQL charset. - - MySQL 4.0 offers this charsets: - big5 cp1251 cp1257 croat czech danish dec8 dos estonia euc_kr gb2312 gbk german1 greek hebrew hp8 hungarian koi8_ru koi8_ukr latin1 latin1_de latin2 latin5 sjis swe7 tis620 ujis usa7 win1250 win1251ukr - - WARNING : incomplete implementation. Please, send your fixes. { -// unicode + // unicode if (!strncmp(mysqlEncoding, "utf8", 4)) { return NSUTF8StringEncoding; } if (!strncmp(mysqlEncoding, "ucs2", 4)) { return NSUnicodeStringEncoding; } -// west european + // west european if (!strncmp(mysqlEncoding, "ascii", 5)) { return NSASCIIStringEncoding; } @@ -53,14 +54,14 @@ if (!strncmp(mysqlEncoding, "macroman", 8)) { return NSMacOSRomanStringEncoding; } -// central european + // central european if (!strncmp(mysqlEncoding, "cp1250", 6)) { return NSWindowsCP1250StringEncoding; } if (!strncmp(mysqlEncoding, "latin2", 6)) { return NSISOLatin2StringEncoding; } -// south european and middle east + // south european and middle east if (!strncmp(mysqlEncoding, "cp1256", 6)) { return CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingWindowsArabic); } @@ -73,15 +74,15 @@ if (!strncmp(mysqlEncoding, "latin5", 6)) { return CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingISOLatin5); } -// baltic + // baltic if (!strncmp(mysqlEncoding, "cp1257", 6)) { return CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingWindowsBalticRim); } -// cyrillic + // cyrillic if (!strncmp(mysqlEncoding, "cp1251", 6)) { return NSWindowsCP1251StringEncoding; } -// asian + // asian if (!strncmp(mysqlEncoding, "big5", 4)) { return CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingBig5); } @@ -92,120 +93,109 @@ return NSShiftJISStringEncoding; } -// default to iso latin 1, even if it is not exact (throw an exception?) + // default to iso latin 1, even if it is not exact (throw an exception?) NSLog(@"warning: unknown encoding %s! falling back to latin1.", mysqlEncoding); return NSISOLatin1StringEncoding; } */ -- (CMMCPResult *) queryString:(NSString *) query + /* -modified version of queryString to be used in sequel-pro -*/ + modified version of queryString to be used in sequel-pro + */ +- (CMMCPResult *)queryString:(NSString *) query { - CMMCPResult *theResult; + CMMCPResult *theResult; const char *theCQuery = [self cStringFromString:query]; - int theQueryCode; + int theQueryCode; -//[DIFF]: check connection - if ( ![self checkConnection] ) { + // check connection + if (![self checkConnection]) { NSLog(@"Connection was gone, but should be reestablished now!"); } -//end [DIFF] -//[DIFF]: inform the delegate about the query - if ( delegate && [delegate respondsToSelector:@selector(willQueryString:)] ) + // inform the delegate about the query + if (delegate && [delegate respondsToSelector:@selector(willQueryString:)]) { [delegate willQueryString:query]; -//end [DIFF] + } if (0 == (theQueryCode = mysql_query(mConnection, theCQuery))) { if (mysql_field_count(mConnection) != 0) { -//[DIFF]: use CMMCPResult instad of MCPResult + // use CMMCPResult instad of MCPResult theResult = [[CMMCPResult alloc] initWithMySQLPtr:mConnection encoding:mEncoding timeZone:mTimeZone]; -//end [DIFF] - } - else { + } else { return nil; } - } - else { -// NSLog (@"Problem in queryString error code is : %d, query is : %s -in ObjC : %@-\n", theQueryCode, theCQuery, query); -// NSLog(@"Error message is : %@\n", [self getLastErrorMessage]); + } else { +// NSLog (@"Problem in queryString error code is : %d, query is : %s -in ObjC : %@-\n", theQueryCode, theCQuery, query); +// NSLog(@"Error message is : %@\n", [self getLastErrorMessage]); // theResult = [theResult init]; // Old version... -// theResult = nil; -//[DIFF]: inform the delegate about errors - if ( delegate && [delegate respondsToSelector:@selector(queryGaveError:)] ) +// theResult = nil; + + // inform the delegate about errors + if (delegate && [delegate respondsToSelector:@selector(queryGaveError:)]) { [delegate queryGaveError:[self getLastErrorMessage]]; -//end [DIFF] + } + return nil; } return [theResult autorelease]; } - (void)setDelegate:(id)object -/* -sets the delegate -*/ { delegate = object; } - - - - -- (NSTimeZone *) timeZone -/*" Getting the currently used time zone (in communication with the DB server). "*/ +/* Getting the currently used time zone (in communication with the DB server). */ /* fixes mysql 4.1.14 problem, can be deleted as soon as fixed in the framework */ +- (NSTimeZone *)timeZone { if ([self checkConnection]) { - MCPResult *theSessionTZ = [self queryString:@"SHOW VARIABLES LIKE '%time_zone'"]; - NSArray *theRow; -// diff + MCPResult *theSessionTZ = [self queryString:@"SHOW VARIABLES LIKE '%time_zone'"]; + NSArray *theRow; id theTZName; -// end diff - NSTimeZone *theTZ; + NSTimeZone *theTZ; [theSessionTZ dataSeek:1ULL]; theRow = [theSessionTZ fetchRowAsArray]; theTZName = [theRow objectAtIndex:1]; -// diff + if ( [theTZName isKindOfClass:[NSData class]] ) { - // MySQL 4.1.14 returns the mysql variables as nsdata + // MySQL 4.1.14 returns the mysql variables as NSData theTZName = [self stringWithText:theTZName]; } -// end diff + if ([theTZName isEqualToString:@"SYSTEM"]) { [theSessionTZ dataSeek:0ULL]; theRow = [theSessionTZ fetchRowAsArray]; theTZName = [theRow objectAtIndex:1]; -// diff + if ( [theTZName isKindOfClass:[NSData class]] ) { - // MySQL 4.1.14 returns the mysql variables as nsdata + // MySQL 4.1.14 returns the mysql variables as NSData theTZName = [self stringWithText:theTZName]; } -// end diff } + if (theTZName) { // Old versions of the server does not support there own time zone ? theTZ = [NSTimeZone timeZoneWithName:theTZName]; - } - else { // By default set the time zone to the local one.. + } else { + // By default set the time zone to the local one.. // Try to get the name using the previously available variable: -// NSLog(@"Fecthing time-zone on 'old' DB server : variable name is : timezone"); theSessionTZ = [self queryString:@"SHOW VARIABLES LIKE 'timezone'"]; [theSessionTZ dataSeek:0ULL]; theRow = [theSessionTZ fetchRowAsArray]; theTZName = [theRow objectAtIndex:1]; - if (theTZName) { // Finally we found one ... -// NSLog(@"Result is : %@", theTZName); + if (theTZName) { + // Finally we found one ... theTZ = [NSTimeZone timeZoneWithName:theTZName]; - } - else { + } else { theTZ = [NSTimeZone defaultTimeZone]; -// theTZ = [NSTimeZone systemTimeZone]; + //theTZ = [NSTimeZone systemTimeZone]; NSLog(@"The time zone is not defined on the server, set it to the default one : %@", theTZ); } } + if (theTZ != mTimeZone) { [mTimeZone release]; mTimeZone = [theTZ retain]; @@ -214,8 +204,4 @@ sets the delegate return mTimeZone; } - - - - @end diff --git a/CMMCPResult.h b/CMMCPResult.h index c5ab06cf..7f527949 100644 --- a/CMMCPResult.h +++ b/CMMCPResult.h @@ -30,4 +30,6 @@ } +- (id)fetchRowAsType:(MCPReturnType)aType; + @end diff --git a/CMMCPResult.m b/CMMCPResult.m index a41ce84d..f80ff5c3 100644 --- a/CMMCPResult.m +++ b/CMMCPResult.m @@ -27,16 +27,16 @@ @implementation CMMCPResult -- (id) fetchRowAsType:(MCPReturnType) aType -/*" +/* modified version for use with sequel-pro -"*/ +*/ +- (id)fetchRowAsType:(MCPReturnType)aType { - MYSQL_ROW theRow; - unsigned long *theLengths; - MYSQL_FIELD *theField; + MYSQL_ROW theRow; + unsigned long *theLengths; + MYSQL_FIELD *theField; int i; - id theReturn; + id theReturn; if (mResult == NULL) { // If there is no results, returns nil, as after the last row... @@ -48,98 +48,100 @@ modified version for use with sequel-pro return nil; } - switch (aType) { - case MCPTypeArray: - theReturn = [NSMutableArray arrayWithCapacity:mNumOfFields]; - break; - case MCPTypeDictionary: - if (mNames == nil) { - [self fetchFieldNames]; - } - theReturn = [NSMutableDictionary dictionaryWithCapacity:mNumOfFields]; - break; - default : - NSLog (@"Unknown type : %d, will return an Array!\n", aType); - theReturn = [NSMutableArray arrayWithCapacity:mNumOfFields]; - break; - } + switch (aType) { + case MCPTypeArray: + theReturn = [NSMutableArray arrayWithCapacity:mNumOfFields]; + break; + case MCPTypeDictionary: + if (mNames == nil) { + [self fetchFieldNames]; + } + theReturn = [NSMutableDictionary dictionaryWithCapacity:mNumOfFields]; + break; + default : + NSLog (@"Unknown type : %d, will return an Array!\n", aType); + theReturn = [NSMutableArray arrayWithCapacity:mNumOfFields]; + break; + } - theLengths = mysql_fetch_lengths(mResult); - theField = mysql_fetch_fields(mResult); + theLengths = mysql_fetch_lengths(mResult); + theField = mysql_fetch_fields(mResult); - for (i=0; i<mNumOfFields; i++) { - id theCurrentObj; + for (i=0; i<mNumOfFields; i++) { + id theCurrentObj; if (theRow[i] == NULL) { theCurrentObj = [NSNull null]; - } - else { - char *theData = calloc(sizeof(char),theLengths[i]+1); - // char *theUselLess; - memcpy(theData, theRow[i],theLengths[i]); - theData[theLengths[i]] = '\0'; + } else { + char *theData = calloc(sizeof(char),theLengths[i]+1); + //char *theUselLess; + memcpy(theData, theRow[i],theLengths[i]); + theData[theLengths[i]] = '\0'; - switch (theField[i].type) { - case FIELD_TYPE_TINY: - case FIELD_TYPE_SHORT: - case FIELD_TYPE_INT24: - case FIELD_TYPE_LONG: - case FIELD_TYPE_LONGLONG: - case FIELD_TYPE_DECIMAL: - case FIELD_TYPE_FLOAT: - case FIELD_TYPE_DOUBLE: - case FIELD_TYPE_NEW_DECIMAL: - case FIELD_TYPE_TIMESTAMP: - case FIELD_TYPE_DATE: - case FIELD_TYPE_TIME: - case FIELD_TYPE_DATETIME: - case FIELD_TYPE_YEAR: - case FIELD_TYPE_VAR_STRING: - case FIELD_TYPE_STRING: - case FIELD_TYPE_SET: - case FIELD_TYPE_ENUM: - case FIELD_TYPE_NEWDATE: // Don't know what the format for this type is... - theCurrentObj = [self stringWithCString:theData]; - break; - case FIELD_TYPE_TINY_BLOB: - case FIELD_TYPE_BLOB: - case FIELD_TYPE_MEDIUM_BLOB: - case FIELD_TYPE_LONG_BLOB: - theCurrentObj = [NSData dataWithBytes:theData length:theLengths[i]]; + switch (theField[i].type) { + case FIELD_TYPE_TINY: + case FIELD_TYPE_SHORT: + case FIELD_TYPE_INT24: + case FIELD_TYPE_LONG: + case FIELD_TYPE_LONGLONG: + case FIELD_TYPE_DECIMAL: + case FIELD_TYPE_FLOAT: + case FIELD_TYPE_DOUBLE: + case FIELD_TYPE_NEW_DECIMAL: + case FIELD_TYPE_TIMESTAMP: + case FIELD_TYPE_DATE: + case FIELD_TYPE_TIME: + case FIELD_TYPE_DATETIME: + case FIELD_TYPE_YEAR: + case FIELD_TYPE_VAR_STRING: + case FIELD_TYPE_STRING: + case FIELD_TYPE_SET: + case FIELD_TYPE_ENUM: + case FIELD_TYPE_NEWDATE: // Don't know what the format for this type is... + theCurrentObj = [self stringWithCString:theData]; + break; + + case FIELD_TYPE_TINY_BLOB: + case FIELD_TYPE_BLOB: + case FIELD_TYPE_MEDIUM_BLOB: + case FIELD_TYPE_LONG_BLOB: + theCurrentObj = [NSData dataWithBytes:theData length:theLengths[i]]; if (!(theField[i].flags & BINARY_FLAG)) { // It is TEXT and NOT BLOB... theCurrentObj = [self stringWithText:theCurrentObj]; - } //#warning Should check for TEXT (using theField[i].flag BINARY_FLAG) + } // #warning Should check for TEXT (using theField[i].flag BINARY_FLAG) break; - case FIELD_TYPE_NULL: + case FIELD_TYPE_NULL: theCurrentObj = [NSNull null]; break; - - default: - NSLog (@"in fetchRowAsType : Unknown type : %d for column %d, send back a NSData object", (int)theField[i].type, (int)i); - theCurrentObj = [NSData dataWithBytes:theData length:theLengths[i]]; - break; - } + + default: + NSLog (@"in fetchRowAsType : Unknown type : %d for column %d, send back a NSData object", (int)theField[i].type, (int)i); + theCurrentObj = [NSData dataWithBytes:theData length:theLengths[i]]; + break; + } + + free(theData); - free(theData); // Some of the creators return nil object... - if (theCurrentObj == nil) { - theCurrentObj = [NSNull null]; - } - } + if (theCurrentObj == nil) { + theCurrentObj = [NSNull null]; + } + } - switch (aType) { - case MCPTypeDictionary : - [theReturn setObject:theCurrentObj forKey:[mNames objectAtIndex:i]]; - break; + switch (aType) { + case MCPTypeDictionary : + [theReturn setObject:theCurrentObj forKey:[mNames objectAtIndex:i]]; + break; + case MCPTypeArray : - default : - [theReturn addObject:theCurrentObj]; - break; - } - } + default : + [theReturn addObject:theCurrentObj]; + break; + } + } - return theReturn; + return theReturn; } @end |