Thread: [Mysql-cocoa-commits] CVS: SMySQL SMySQLConnection.h,1.5,1.6 SMySQLConnection.m,1.5,1.6 SMySQLResult
Brought to you by:
sergecohen
From: Serge C. <ser...@us...> - 2002-05-06 09:58:31
|
Update of /cvsroot/mysql-cocoa/SMySQL In directory usw-pr-cvs1:/tmp/cvs-serv11778 Modified Files: SMySQLConnection.h SMySQLConnection.m SMySQLResult.h SMySQLResult.m Log Message: Corrected the method listFields: fromTable:. Finished the proper character set encoding for all the method. This version should be as transaprent as possible with non ascii character encoding (as far as the charset used by mysql is known from Mac OS X). Incremented the version to 0.5.5 Serge Cohen; MySQL Cocoa project, May 6th 2002. Index: SMySQLConnection.h =================================================================== RCS file: /cvsroot/mysql-cocoa/SMySQL/SMySQLConnection.h,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** SMySQLConnection.h 3 May 2002 09:36:39 -0000 1.5 --- SMySQLConnection.h 6 May 2002 09:58:27 -0000 1.6 *************** *** 112,115 **** --- 112,116 ---- "*/ - (const char *)cStringFromString:(NSString *)theString; + - (NSString *)stringWithCString:(const char *)theCString; @end Index: SMySQLConnection.m =================================================================== RCS file: /cvsroot/mysql-cocoa/SMySQL/SMySQLConnection.m,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** SMySQLConnection.m 3 May 2002 09:36:39 -0000 1.5 --- SMySQLConnection.m 6 May 2002 09:58:27 -0000 1.6 *************** *** 215,218 **** --- 215,219 ---- /*" Select the encoding used by the server for data transfert. This is usefull for the output NSString (query result) to be Ok even for character which are not ASCII + NB: The character set (encoding) used by the db is passed to the SMySQLConnection object upon connection, so most likely the encoding (from -encoding) method is already the proper one. That is to say : It's unlikeley you need to use this method, and if ever you use it, do it at your own risks. "*/ { *************** *** 247,251 **** { #warning What to do if one of the string is a nil pointer? - #warning What about the encoding? const char *theHost = [host UTF8String]; const char *theLogin = [login UTF8String]; --- 248,251 ---- *************** *** 284,288 **** { #warning What to do if one of the string is a nil pointer? - #warning What about the encoding? const char *theSocket = [socket UTF8String]; const char *theLogin = [login UTF8String]; --- 284,287 ---- *************** *** 320,325 **** } if (mConnected) { ! #warning What about the encoding? ! const char *theDBName = [dbName UTF8String]; if (mysql_select_db(mConnection, theDBName) == NULL) { return YES; --- 319,323 ---- } if (mConnected) { ! const char *theDBName = [self cStringFromString:dbName]; if (mysql_select_db(mConnection, theDBName) == NULL) { return YES; *************** *** 336,341 **** { if (mConnection) { ! #warning What about the encoding? ! return [NSString stringWithUTF8String:mysql_error(mConnection)]; } else { --- 334,338 ---- { if (mConnection) { ! return [self stringWithCString:mysql_error(mConnection)]; } else { *************** *** 383,390 **** mysql_real_escape_string(mConnection, theCEscBuffer, theCDataBuffer, theLength); ! #warning What about the encoding? ! theReturn = [NSString stringWithUTF8String:theCEscBuffer]; free (theCEscBuffer); - [theReturn autorelease]; return theReturn; } --- 380,385 ---- mysql_real_escape_string(mConnection, theCEscBuffer, theCDataBuffer, theLength); ! theReturn = [self stringWithCString:theCEscBuffer]; free (theCEscBuffer); return theReturn; } *************** *** 397,420 **** { SMySQLResult *theResult = [SMySQLResult alloc]; - #warning We should get a proper encoding for the string (more likely iso latin-1) (done?) const char *theCQuery = [self cStringFromString:query]; int theQueryCode; - NSLog (@"in queryString query is : %s -in ObjC : %@-\n", theCQuery, query); if ((theQueryCode = mysql_query(mConnection, theCQuery)) == NULL) { theResult = [theResult initWithMySQLPtr:mConnection encoding:mEncoding]; - NSLog (@"the SMySQLResult is %@\n", theResult); - if (theResult) { - [theResult autorelease]; - } - return theResult; } else { NSLog (@"Problem in queryString error code is : %d, query is : %s -in ObjC : %@-\n", theQueryCode, theCQuery, query); } if (theResult) { ! [theResult release]; } ! return nil; } --- 392,409 ---- { SMySQLResult *theResult = [SMySQLResult alloc]; const char *theCQuery = [self cStringFromString:query]; int theQueryCode; if ((theQueryCode = mysql_query(mConnection, theCQuery)) == NULL) { theResult = [theResult initWithMySQLPtr:mConnection encoding:mEncoding]; } else { NSLog (@"Problem in queryString error code is : %d, query is : %s -in ObjC : %@-\n", theQueryCode, theCQuery, query); + theResult = [theResult init]; } if (theResult) { ! [theResult autorelease]; } ! return theResult; } *************** *** 524,530 **** theResult = [self queryString:theQuery]; } - if (theResult) { - [theResult autorelease]; - } return theResult; } --- 513,516 ---- *************** *** 537,544 **** { if (mConnected) { ! #warning Is the string is really encoded in UTF8 : NO, in iso latin1 ! return [NSString stringWithUTF8String: mysql_get_server_info(mConnection)]; } ! return [NSString stringWithUTF8String: ""]; } --- 523,529 ---- { if (mConnected) { ! return [self stringWithCString: mysql_get_server_info(mConnection)]; } ! return @""; } *************** *** 595,603 **** "*/ { ! NSMutableData *theData = [NSMutableData dataWithCapacity:0]; ! [theData appendData:[theString dataUsingEncoding: mEncoding allowLossyConversion: YES]]; [theData increaseLengthBy:1]; return (const char *)[theData bytes]; } --- 580,602 ---- "*/ { ! NSMutableData *theData = [NSMutableData dataWithData:[theString dataUsingEncoding:mEncoding allowLossyConversion:YES]]; [theData increaseLengthBy:1]; return (const char *)[theData bytes]; + } + + + - (NSString *)stringWithCString:(const char *)theCString + /*" + Return a NSString from a C style string encoded with the character set of theSMySQLConnection. + "*/ + { + NSData * theData = [NSData dataWithBytes:theCString length:(strlen(theCString))]; + NSString * theString = [[NSString alloc] initWithData:theData encoding:mEncoding]; + + if (theString) { + [theString autorelease]; + } + return theString; } Index: SMySQLResult.h =================================================================== RCS file: /cvsroot/mysql-cocoa/SMySQL/SMySQLResult.h,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** SMySQLResult.h 3 May 2002 09:36:39 -0000 1.3 --- SMySQLResult.h 6 May 2002 09:58:27 -0000 1.4 *************** *** 83,86 **** --- 83,91 ---- - (void)dealloc; + /*" + Private methods, internam use only + "*/ + - (const char *)cStringFromString:(NSString *)theString; + - (NSString *)stringWithCString:(const char *)theCString; @end Index: SMySQLResult.m =================================================================== RCS file: /cvsroot/mysql-cocoa/SMySQL/SMySQLResult.m,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** SMySQLResult.m 3 May 2002 09:36:39 -0000 1.3 --- SMySQLResult.m 6 May 2002 09:58:27 -0000 1.4 *************** *** 75,78 **** --- 75,84 ---- } mResult = mysql_store_result(mySQLPtr); + /* + if (mResult == NULL) { + [self autorelease]; + return nil; + } + */ if (mMySQLLocales == NULL) { mMySQLLocales = [[SMySQLConnection getMySQLLocales] retain]; *************** *** 100,107 **** } mResult = mySQLResPtr; ! if (mResult == NULL) { [self autorelease]; return nil; } if (mMySQLLocales == NULL) { mMySQLLocales = [[SMySQLConnection getMySQLLocales] retain]; --- 106,115 ---- } mResult = mySQLResPtr; ! /* ! if (mResult == NULL) { [self autorelease]; return nil; } + */ if (mMySQLLocales == NULL) { mMySQLLocales = [[SMySQLConnection getMySQLLocales] retain]; *************** *** 224,228 **** // a switch on theLength[i] would do that... // Here it will crash if it's not default presentation : TIMESTAMP(14) - #warning What about the encoding? For all the rest of the method theCurrentObj = [NSCalendarDate dateWithString:[NSString stringWithUTF8String:theData] calendarFormat:@"%Y%m%d%H%M%S"]; break; --- 232,235 ---- *************** *** 266,270 **** case FIELD_TYPE_NEWDATE: // Don't know what the format for this type is... ! theCurrentObj = [NSString stringWithUTF8String:theData]; break; } --- 273,279 ---- case FIELD_TYPE_NEWDATE: // Don't know what the format for this type is... ! theDataObj = [NSData dataWithBytes:theData length:theLengths[i]]; ! theCurrentObj = [[NSString alloc] initWithData:theDataObj encoding:mEncoding]; ! [theCurrentObj autorelease]; break; } *************** *** 328,332 **** // a switch on theLength[i] would do that... // Here it will crash if it's not default presentation : TIMESTAMP(14) - #warning What about the encoding? For all the rest of the method theCurrentObj = [NSCalendarDate dateWithString:[NSString stringWithUTF8String:theData] calendarFormat:@"%Y%m%d%H%M%S"]; break; --- 337,340 ---- *************** *** 370,374 **** case FIELD_TYPE_NEWDATE: // Don't know what the format for this type is... ! theCurrentObj = [NSString stringWithUTF8String:theData]; break; } --- 378,384 ---- case FIELD_TYPE_NEWDATE: // Don't know what the format for this type is... ! theDataObj = [NSData dataWithBytes:theData length:theLengths[i]]; ! theCurrentObj = [[NSString alloc] initWithData:theDataObj encoding:mEncoding]; ! [theCurrentObj autorelease]; break; } *************** *** 404,409 **** theField = mysql_fetch_fields(mResult); for (i=0; i<theNumFields; i++) { ! #warning What about the encoding? For all the rest of the method ! NSString *theName = [NSString stringWithUTF8String:theField[i].name]; [theNamesArray addObject:theName]; } --- 414,418 ---- theField = mysql_fetch_fields(mResult); for (i=0; i<theNumFields; i++) { ! NSString *theName = [self stringWithCString:theField[i].name]; [theNamesArray addObject:theName]; } *************** *** 747,750 **** --- 756,786 ---- [super dealloc]; return; + } + + - (const char *)cStringFromString:(NSString *)theString + /*" + For internal use only. Transform a NSString to a C type string (ended with \0) using ethe character set from the SMySQLConnection. + Lossy conversions are enabled. + "*/ + { + NSMutableData *theData = [NSMutableData dataWithData:[theString dataUsingEncoding:mEncoding allowLossyConversion:YES]]; + + [theData increaseLengthBy:1]; + return (const char *)[theData bytes]; + } + + + - (NSString *)stringWithCString:(const char *)theCString + /*" + Return a NSString from a C style string encoded with the character set of theSMySQLConnection. + "*/ + { + NSData * theData = [NSData dataWithBytes:theCString length:(strlen(theCString))]; + NSString * theString = [[NSString alloc] initWithData:theData encoding:mEncoding]; + + if (theString) { + [theString autorelease]; + } + return theString; } |