[Mysql-cocoa-commits] CVS: SMySQL MCPConnection.h,NONE,1.1.2.1 MCPResult.h,NONE,1.1.2.1 MCPResult.m,
Brought to you by:
sergecohen
From: Serge C. <ser...@us...> - 2002-05-30 13:52:05
|
Update of /cvsroot/mysql-cocoa/SMySQL In directory usw-pr-cvs1:/tmp/cvs-serv13517 Added Files: Tag: version-2 MCPConnection.h MCPResult.h MCPResult.m Removed Files: Tag: version-2 SMySQLConnection.h SMySQLResult.h SMySQLResult.m Log Message: Reananed SMySQLConnection.h, SMySQLResult.h, SMySQLResult.m to respectivly MCPConnection.h, MCPResult.h, MCPResult.m. The new first letters for the framework class will be MCP (MySQL Cocoa Project), but the framework will keep his name of SMySQL. Serge Cohen; MySQL Cocoa project, May 30th 2002. --- NEW FILE: MCPConnection.h --- // // SMySQLConnection.h // SMySQL // // Created by serge cohen (ser...@m4...) on Sat Dec 08 2001. // Copyright (c) 2001 MySQL Cocoa project. // // This code is free software; you can redistribute it and/or modify it under // the terms of the GNU General Public License as published by the Free // Software Foundation; either version 2 of the License, or any later version. // // This code is distributed in the hope that it will be useful, but WITHOUT ANY // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more // details. // // For a copy of the GNU General Public License, visit <http://www.gnu.org/> or // write to the Free Software Foundation, Inc., 59 Temple Place--Suite 330, // Boston, MA 02111-1307, USA. // // More info at <http://mysql-cocoa.sourceforge.net/> // // $Id: MCPConnection.h,v 1.1.2.1 2002/05/30 13:52:01 sergecohen Exp $ #import <Foundation/Foundation.h> #import "mysql.h" //#import <SMySQL/mysql.h> //#import <SMySQL/SMySQL.h> @class SMySQLResult; #define kSMySQL_default_option CLIENT_COMPRESS #define kSMySQLConnectionDefaultSocket MYSQL_UNIX_ADDR #define kSMySQLConnection_error_not_inited 1000 @interface SMySQLConnection : NSObject { MYSQL *mConnection; /*"The inited MySQL connection"*/ BOOL mConnected; /*"Reflect the fact that the connection is already in place or not"*/ NSStringEncoding mEncoding; /*"The encoding used by MySQL server, to ISO-1 default"*/ } /*" Getting default of MySQL "*/ + (NSDictionary *) getMySQLLocales; + (NSStringEncoding) encodingForMySQLEncoding:(const char *) mysqlEncoding; + (NSStringEncoding) defaultMySQLEncoding; /*" Initialisation "*/ - (id) init; // Port to 0 to use the default port - (id) initToHost:(NSString *) host withLogin:(NSString *) login password:(NSString *) pass usingPort:(int) port; - (id) initToSocket:(NSString *) socket withLogin:(NSString *) login password:(NSString *) pass; - (BOOL) setConnectionOption:(int) option withArgument:(id) arg; // Port to 0 to use the default port - (BOOL) connectWithLogin:(NSString *) login password:(NSString *) pass host:(NSString *) host port:(int) port socket:(NSString *) socket; - (BOOL) selectDB:(NSString *) dbName; /*" Errors information "*/ - (NSString *) getLastErrorMessage; - (unsigned int) getLastErrorID; - (BOOL) isConnected; - (BOOL) checkConnection; /*" Queries "*/ - (NSString *) prepareBinaryData:(NSData *) theData; - (NSString *) prepareString:(NSString *) theString; - (SMySQLResult *) queryString:(NSString *) query; - (my_ulonglong) affectedRows; - (my_ulonglong) insertId; /*" Getting description of the database structure "*/ - (SMySQLResult *) listDBs; - (SMySQLResult *) listDBsLike:(NSString *) dbsName; - (SMySQLResult *) listTables; - (SMySQLResult *) listTablesLike:(NSString *) tablesName; // Next method uses SHOW TABLES FROM db to be sure that the db is not changed during this call. - (SMySQLResult *) listTablesFromDB:(NSString *) dbName like:(NSString *) tablesName; - (SMySQLResult *) listFieldsFromTable:(NSString *) tableName; - (SMySQLResult *) listFieldsFromTable:(NSString *) tableName like:(NSString *) fieldsName; /*" Server information and control "*/ - (NSString *) clientInfo; - (NSString *) hostInfo; - (NSString *) serverInfo; - (NSNumber *) protoInfo; - (SMySQLResult *) listProcesses; - (BOOL) killProcess:(unsigned long) pid; //- (BOOL)createDBWithName:(NSString *)dbName; //- (BOOL)dropDBWithName:(NSString *)dbName; /*" Disconnection "*/ - (void) disconnect; - (void) dealloc; /*" String encoding concerns (c string type to NSString). It's unlikely that users of the framework needs to use these methods which are used internally "*/ - (void) setEncoding:(NSStringEncoding) theEncoding; - (NSStringEncoding) encoding; - (const char *) cStringFromString:(NSString *) theString; - (NSString *) stringWithCString:(const char *) theCString; @end --- NEW FILE: MCPResult.h --- // // SMySQLResult.h // SMySQL // // Created by serge cohen (ser...@m4...) on Sat Dec 08 2001. // Copyright (c) 2001 MySQL Cocoa project. // // This code is free software; you can redistribute it and/or modify it under // the terms of the GNU General Public License as published by the Free // Software Foundation; either version 2 of the License, or any later version. // // This code is distributed in the hope that it will be useful, but WITHOUT ANY // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more // details. // // For a copy of the GNU General Public License, visit <http://www.gnu.org/> or // write to the Free Software Foundation, Inc., 59 Temple Place--Suite 330, // Boston, MA 02111-1307, USA. // // More info at <http://mysql-cocoa.sourceforge.net/> // // $Id: MCPResult.h,v 1.1.2.1 2002/05/30 13:52:01 sergecohen Exp $ #import <Foundation/Foundation.h> #import "mysql.h" //#import <SMySQL/mysql.h> //#import <SMySQL/SMySQL.h> //@class SMySQLRow; @interface SMySQLResult : NSObject { MYSQL_RES *mResult; /*"The MYSQL_RES structure of the C API"*/ NSArray *mNames; /*"An NSArray holding the name of the columns"*/ NSDictionary *mMySQLLocales; /*"A Locales dictionary to define the locales of MySQL"*/ NSStringEncoding mEncoding; /*"The encoding used by MySQL server, to ISO-1 default"*/ } /*" Init used #{only} by #{SMySQLConnection} "*/ - (id)initWithMySQLPtr:(MYSQL *)mySQLPtr encoding:(NSStringEncoding)theEncoding; - (id)initWithResPtr:(MYSQL_RES *)mySQLResPtr encoding:(NSStringEncoding)theEncoding; - (id)init; /*" General info on the result "*/ - (my_ulonglong)numOfRows; - (unsigned int)numOfFields; /*" Getting the rows "*/ - (void)dataSeek:(my_ulonglong)row; - (NSDictionary *)fetchRowAsDictionary; - (NSArray *)fetchRowAsArray; /*" Getting information on columns "*/ - (NSArray *)fetchFieldsName; - (NSArray *)fetchTypesAsArray; - (NSDictionary *)fetchTypesAsDictionary; - (BOOL)isBlobAtIndex:(unsigned int)index; - (BOOL)isBlobForKey:(NSString *)key; /*" Utility method "*/ - (NSString *)description; /*" End of the scope... "*/ - (void)dealloc; /*" Private methods, internam use only "*/ - (const char *)cStringFromString:(NSString *)theString; - (NSString *)stringWithCString:(const char *)theCString; @end --- NEW FILE: MCPResult.m --- // // SMySQLResult.m // SMySQL // // Created by serge cohen (ser...@m4...) on Sat Dec 08 2001. // Copyright (c) 2001 MySQL Cocoa project. // // This code is free software; you can redistribute it and/or modify it under // the terms of the GNU General Public License as published by the Free // Software Foundation; either version 2 of the License, or any later version. // // This code is distributed in the hope that it will be useful, but WITHOUT ANY // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more // details. // // For a copy of the GNU General Public License, visit <http://www.gnu.org/> or // write to the Free Software Foundation, Inc., 59 Temple Place--Suite 330, // Boston, MA 02111-1307, USA. // // More info at <http://mysql-cocoa.sourceforge.net/> // // $Id: MCPResult.m,v 1.1.2.1 2002/05/30 13:52:01 sergecohen Exp $ #import "SMySQLConnection.h" #import "SMySQLResult.h" @implementation SMySQLResult /*" Hold the results of a query to a MySQL database server. It correspond to the MYSQL_RES structure of the C API, and to the statement handle of the PERL DBI/DBD. Uses the #{mysql_store_result()} function from the C API. This object is generated only by a SMySQLConnection object, in this way (see #{SMySQLConnection} documentation): !{ SMySQLConnection *theConnec = [SMySQLConnection alloc]; SMySQLResult *theRes; NSDictionnary *theDict; NSArray *theColNames; int i, j; theConnec = [theConnec initToHost::albert.com withLogin:@"toto" password:@"albert" port:0]; [theConnec selectDB:@"db1"]; theRes = [theConnec queryString:@"select * from table1"]; theColNames = [theRes fetchFiedlsName]; i = 0; while (theDict = [theRes fetchRowAsDictionary]){ NSLog(@"Row : %d\n", i); for (j=0; j<[theColNames count]; j++) { NSLog(@" Field : %@, contain : %@\n", [theColNames objectAtInex:j], [theDict objectForKey:[theColNames objectAtIndex:j]]); } i++; } } "*/ - (id)initWithMySQLPtr:(MYSQL *)mySQLPtr encoding:(NSStringEncoding)theEncoding /*" initialise a SMySQLResult, it is used internally by SMySQLConnection #{queryString:} method: the only proper way to get a running SMySQLResult object. "*/ { self = [super init]; mEncoding = theEncoding; if (mResult) { mysql_free_result(mResult); mResult = NULL; } if (mNames) { [mNames release]; mNames = NULL; } mResult = mysql_store_result(mySQLPtr); /* if (mResult == NULL) { [self autorelease]; return nil; } */ if (mMySQLLocales == NULL) { mMySQLLocales = [[SMySQLConnection getMySQLLocales] retain]; } return self; } - (id)initWithResPtr:(MYSQL_RES *)mySQLResPtr encoding:(NSStringEncoding)theEncoding /*" This metod it is used internally by SMySQLConnection object when it have already a MYSQL_RES object to initialise SMySQLResult object. Initialise a SMySQLResult with the MYSQL_RES pointer (returned by such a function as mysql_list_dbs). NB: SMySQLResult should be made by using one of the method of SMySQLConnection. "*/ { self = [super init]; mEncoding = theEncoding; if (mResult) { mysql_free_result(mResult); mResult = NULL; } if (mNames) { [mNames release]; mNames = NULL; } mResult = mySQLResPtr; /* if (mResult == NULL) { [self autorelease]; return nil; } */ if (mMySQLLocales == NULL) { mMySQLLocales = [[SMySQLConnection getMySQLLocales] retain]; } return self; } - (id)init /*" Empty init, normaly of NO use to the user, again, SMySQLResult should be made through calls to SMySQLConnection "*/ { self = [super init]; mEncoding = [SMySQLConnection defaultMySQLEncoding]; if (mResult) { mysql_free_result(mResult); mResult = NULL; } if (mNames) { [mNames release]; mNames = NULL; } if (mMySQLLocales == NULL) { mMySQLLocales = [[SMySQLConnection getMySQLLocales] retain]; } return self; } - (my_ulonglong)numOfRows /*" Return the number of rows selected by the query. "*/ { if (mResult) { return mysql_num_rows(mResult); } return 0; } - (unsigned int)numOfFields /*" Return the number of fields selected by the query. "*/ { if (mResult) { return mysql_num_fields(mResult); } return 0; } - (void)dataSeek:(my_ulonglong)row /*" Go to a precise row in the selected result. 0 is the very first row "*/ { my_ulonglong theRow = (row < 0)? 0 : row; theRow = (theRow < [self numOfRows])? theRow : ([self numOfRows]-1); mysql_data_seek(mResult,theRow); return; } - (NSDictionary *)fetchRowAsDictionary /*" Return the next row of the result as a dictionary, the key being the field name, the object a proper object for handling the information in the field (NSString, NSNumber ...). "*/ { MYSQL_ROW theRow; unsigned long *theLengths; MYSQL_FIELD *theField; int i; unsigned int theNumFields; NSMutableDictionary *theDict; if (mResult == NULL) { // If there is no results, returns nil, as after the last row... return nil; } if (mNames == NULL) { [self fetchFieldsName]; } theRow = mysql_fetch_row(mResult); if (theRow == NULL) { return nil; } // NSMutableDictionary *theDict = [NSMutableDictionary dictionaryWithCapacity:theNumFields]; theNumFields = [self numOfFields]; theDict = [NSMutableDictionary dictionaryWithCapacity:theNumFields]; theLengths = mysql_fetch_lengths(mResult); theField = mysql_fetch_fields(mResult); for (i=0; i<theNumFields; i++) { id theCurrentObj,theDataObj; char *theData = calloc(sizeof(char),theLengths[i]+1); memcpy(theData, theRow[i],theLengths[i]); theData[theLengths[i]] = '\0'; // NSLog (@"in fetchRowAsDictionary : field type is %d, for column %d", (int)theField[i].type, (int)i); switch (theField[i].type) { case FIELD_TYPE_TINY: case FIELD_TYPE_SHORT: case FIELD_TYPE_LONG: case FIELD_TYPE_INT24: case FIELD_TYPE_LONGLONG: theCurrentObj = [NSNumber numberWithLong:atol(theData)]; break; case FIELD_TYPE_DECIMAL: case FIELD_TYPE_FLOAT: case FIELD_TYPE_DOUBLE: theCurrentObj = [NSNumber numberWithDouble:atof(theData)]; break; case FIELD_TYPE_TIMESTAMP: // Indeed one should check which format it is (14,12...2) and get the corresponding format string // a switch on theLength[i] would do that... // Here it will crash if it's not default presentation : TIMESTAMP(14) theCurrentObj = [NSCalendarDate dateWithString:[NSString stringWithUTF8String:theData] calendarFormat:@"%Y%m%d%H%M%S"]; break; case FIELD_TYPE_DATE: theCurrentObj = [NSCalendarDate dateWithString:[NSString stringWithUTF8String:theData] calendarFormat:@"%Y-%m-%d"]; break; case FIELD_TYPE_TIME: // Pass them back as string for the moment... not TIME object in Cocoa (so far) theCurrentObj = [NSString stringWithUTF8String:theData]; case FIELD_TYPE_DATETIME: theCurrentObj = [NSCalendarDate dateWithString:[NSString stringWithUTF8String:theData] calendarFormat:@"%Y-%m-%d %H:%M:%S"]; break; case FIELD_TYPE_YEAR: theCurrentObj = [NSCalendarDate dateWithString:[NSString stringWithUTF8String:theData] calendarFormat:@"%Y"]; break; case FIELD_TYPE_VAR_STRING: case FIELD_TYPE_STRING: theDataObj = [NSData dataWithBytes:theData length:theLengths[i]]; theCurrentObj = [[NSString alloc] initWithData:theDataObj encoding:mEncoding]; [theCurrentObj autorelease]; 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]]; break; case FIELD_TYPE_SET: theDataObj = [NSData dataWithBytes:theData length:theLengths[i]]; theCurrentObj = [[NSString alloc] initWithData:theDataObj encoding:mEncoding]; [theCurrentObj autorelease]; break; case FIELD_TYPE_ENUM: theDataObj = [NSData dataWithBytes:theData length:theLengths[i]]; theCurrentObj = [[NSString alloc] initWithData:theDataObj encoding:mEncoding]; [theCurrentObj autorelease]; break; case FIELD_TYPE_NULL: theCurrentObj = nil; break; 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; default: NSLog (@"in fetchRowAsDictionary : 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; } [theDict setObject:theCurrentObj forKey:[mNames objectAtIndex:i]]; free(theData); } return [NSDictionary dictionaryWithDictionary:theDict]; } - (NSArray *)fetchRowAsArray /*" Return the next row of the result as an array, the index in select field order, the object a proper object for handling the information in the field (NSString, NSNumber ...). "*/ { MYSQL_ROW theRow; unsigned long *theLengths; MYSQL_FIELD *theField; int i; unsigned int theNumFields; NSMutableArray* theArray; if (mResult == NULL) { // If there is no results, returns nil, as after the last row... return nil; } theRow = mysql_fetch_row(mResult); if (theRow == NULL) { return nil; } // A row have been retrieved succesfully, put in in the dictionary theNumFields = [self numOfFields]; theArray = [NSMutableArray arrayWithCapacity:theNumFields]; theLengths = mysql_fetch_lengths(mResult); theField = mysql_fetch_fields(mResult); for (i=0; i<theNumFields; i++) { id theCurrentObj,theDataObj; char *theData = calloc(sizeof(char),theLengths[i]+1); memcpy(theData, theRow[i],theLengths[i]); theData[theLengths[i]] = '\0'; // NSLog (@"in fetchRowAsArray : field type is %d, for column %d", (int)theField[i].type, (int)i); switch (theField[i].type) { case FIELD_TYPE_TINY: case FIELD_TYPE_SHORT: case FIELD_TYPE_LONG: case FIELD_TYPE_INT24: case FIELD_TYPE_LONGLONG: theCurrentObj = [NSNumber numberWithLong:atol(theData)]; break; case FIELD_TYPE_DECIMAL: case FIELD_TYPE_FLOAT: case FIELD_TYPE_DOUBLE: theCurrentObj = [NSNumber numberWithDouble:atof(theData)]; break; case FIELD_TYPE_TIMESTAMP: // Indeed one should check which format it is (14,12...2) and get the corresponding format string // a switch on theLength[i] would do that... // Here it will crash if it's not default presentation : TIMESTAMP(14) theCurrentObj = [NSCalendarDate dateWithString:[NSString stringWithUTF8String:theData] calendarFormat:@"%Y%m%d%H%M%S"]; break; case FIELD_TYPE_DATE: theCurrentObj = [NSCalendarDate dateWithString:[NSString stringWithUTF8String:theData] calendarFormat:@"%Y-%m-%d"]; break; case FIELD_TYPE_TIME: // Pass them back as string for the moment... not TIME object in Cocoa (so far) theCurrentObj = [NSString stringWithUTF8String:theData]; case FIELD_TYPE_DATETIME: theCurrentObj = [NSCalendarDate dateWithString:[NSString stringWithUTF8String:theData] calendarFormat:@"%Y-%m-%d %H:%M:%S"]; break; case FIELD_TYPE_YEAR: theCurrentObj = [NSCalendarDate dateWithString:[NSString stringWithUTF8String:theData] calendarFormat:@"%Y"]; break; case FIELD_TYPE_VAR_STRING: case FIELD_TYPE_STRING: theDataObj = [NSData dataWithBytes:theData length:theLengths[i]]; theCurrentObj = [[NSString alloc] initWithData:theDataObj encoding:mEncoding]; [theCurrentObj autorelease]; 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]]; break; case FIELD_TYPE_SET: theDataObj = [NSData dataWithBytes:theData length:theLengths[i]]; theCurrentObj = [[NSString alloc] initWithData:theDataObj encoding:mEncoding]; [theCurrentObj autorelease]; break; case FIELD_TYPE_ENUM: theDataObj = [NSData dataWithBytes:theData length:theLengths[i]]; theCurrentObj = [[NSString alloc] initWithData:theDataObj encoding:mEncoding]; [theCurrentObj autorelease]; break; case FIELD_TYPE_NULL: theCurrentObj = nil; break; 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; default: NSLog (@"in fetchRowAsArray : 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; } [theArray addObject:theCurrentObj]; free(theData); } return [NSArray arrayWithArray:theArray]; } - (NSArray *)fetchFieldsName /*" Generate the mNames if not already generated, and return it. mNames is a NSArray holding the names of the fields(columns) of the results "*/ { unsigned int theNumFields; int i; NSMutableArray *theNamesArray; MYSQL_FIELD *theField; if (mNames) { return mNames; } if (mResult == NULL) { // If no results, give an empty array. Maybe it's better to give a nil pointer? return (mNames = [[NSArray array] retain]); } theNumFields = [self numOfFields]; theNamesArray = [NSMutableArray arrayWithCapacity: theNumFields]; theField = mysql_fetch_fields(mResult); for (i=0; i<theNumFields; i++) { NSString *theName = [self stringWithCString:theField[i].name]; if ((theName) && (![theName isEqualToString:@""])) { [theNamesArray addObject:theName]; } else { [theNamesArray addObject:[NSString stringWithFormat:@"Column %d", i]]; } } return (mNames = [[NSArray arrayWithArray:theNamesArray] retain]); } - (NSArray *)fetchTypesAsArray /*" Return an array of the fields' types. "*/ { unsigned int theNumFields; int i; NSMutableArray *theTypesArray; MYSQL_FIELD *theField; if (mResult == NULL) { // If no results, give an empty array. Maybe it's better to give a nil pointer? return ([NSArray array]); } theNumFields = [self numOfFields]; theTypesArray = [NSMutableArray arrayWithCapacity: theNumFields]; theField = mysql_fetch_fields(mResult); for (i=0; i<theNumFields; i++) { NSString *theType; switch (theField[i].type) { case FIELD_TYPE_TINY: theType = @"tiny"; break; case FIELD_TYPE_SHORT: theType = @"short"; break; case FIELD_TYPE_LONG: theType = @"long"; break; case FIELD_TYPE_INT24: theType = @"int24"; break; case FIELD_TYPE_LONGLONG: theType = @"longlong"; break; case FIELD_TYPE_DECIMAL: theType = @"decimal"; break; case FIELD_TYPE_FLOAT: theType = @"float"; break; case FIELD_TYPE_DOUBLE: theType = @"double"; break; case FIELD_TYPE_TIMESTAMP: theType = @"timestamp"; break; case FIELD_TYPE_DATE: theType = @"date"; break; case FIELD_TYPE_TIME: theType = @"time"; break; case FIELD_TYPE_DATETIME: theType = @"datetime"; break; case FIELD_TYPE_YEAR: theType = @"year"; break; case FIELD_TYPE_VAR_STRING: theType = @"varstring"; break; case FIELD_TYPE_STRING: theType = @"string"; break; case FIELD_TYPE_TINY_BLOB: theType = @"tinyblob"; break; case FIELD_TYPE_BLOB: theType = @"blob"; break; case FIELD_TYPE_MEDIUM_BLOB: theType = @"mediumblob"; break; case FIELD_TYPE_LONG_BLOB: theType = @"longblob"; break; case FIELD_TYPE_SET: theType = @"set"; break; case FIELD_TYPE_ENUM: theType = @"enum"; break; case FIELD_TYPE_NULL: theType = @"null"; break; case FIELD_TYPE_NEWDATE: theType = @"newdate"; break; default: theType = @"unknown"; NSLog (@"in fetchTypesAsArray : Unknown type for column %d of the SMySQLResult, type = %d", (int)i, (int)theField[i].type); break; } [theTypesArray addObject:theType]; } return ([NSArray arrayWithArray:theTypesArray]); } - (NSDictionary*)fetchTypesAsDictionary /*" Return a dictionnary of the fields' types (keys are the fields' names). "*/ { unsigned int theNumFields; int i; NSMutableDictionary *theTypesDict; MYSQL_FIELD *theField; if (mResult == NULL) { // If no results, give an empty array. Maybe it's better to give a nil pointer? return ([NSDictionary dictionary]); } if (mNames == NULL) { [self fetchFieldsName]; } theNumFields = [self numOfFields]; theTypesDict = [NSMutableDictionary dictionaryWithCapacity: theNumFields]; theField = mysql_fetch_fields(mResult); for (i=0; i<theNumFields; i++) { NSString *theType; switch (theField[i].type) { case FIELD_TYPE_TINY: theType = @"tiny"; break; case FIELD_TYPE_SHORT: theType = @"short"; break; case FIELD_TYPE_LONG: theType = @"long"; break; case FIELD_TYPE_INT24: theType = @"int24"; break; case FIELD_TYPE_LONGLONG: theType = @"longlong"; break; case FIELD_TYPE_DECIMAL: theType = @"decimal"; break; case FIELD_TYPE_FLOAT: theType = @"float"; break; case FIELD_TYPE_DOUBLE: theType = @"double"; break; case FIELD_TYPE_TIMESTAMP: theType = @"timestamp"; break; case FIELD_TYPE_DATE: theType = @"date"; break; case FIELD_TYPE_TIME: theType = @"time"; break; case FIELD_TYPE_DATETIME: theType = @"datetime"; break; case FIELD_TYPE_YEAR: theType = @"year"; break; case FIELD_TYPE_VAR_STRING: theType = @"varstring"; break; case FIELD_TYPE_STRING: theType = @"string"; break; case FIELD_TYPE_TINY_BLOB: theType = @"tinyblob"; break; case FIELD_TYPE_BLOB: theType = @"blob"; break; case FIELD_TYPE_MEDIUM_BLOB: theType = @"mediumblob"; break; case FIELD_TYPE_LONG_BLOB: theType = @"longblob"; break; case FIELD_TYPE_SET: theType = @"set"; break; case FIELD_TYPE_ENUM: theType = @"enum"; break; case FIELD_TYPE_NULL: theType = @"null"; break; case FIELD_TYPE_NEWDATE: theType = @"newdate"; break; default: theType = @"unknown"; NSLog (@"in fetchTypesAsDictionary : Unknown type for column %d of the SMySQLResult, type = %d", (int)i, (int)theField[i].type); break; } [theTypesDict setObject:theType forKey:[mNames objectAtIndex:i]]; } return ([NSDictionary dictionaryWithDictionary:theTypesDict]); } - (BOOL)isBlobAtIndex:(unsigned int)index /*" Return YES if the field with the given index is a BLOB. It should be used to discriminates between BLOBs and TEXTs. #{NOT YET IMPLEMENTED}, return YES for both BLOBs and TEXTs... "*/ { BOOL theRet; unsigned int theNumFields; MYSQL_FIELD *theField; if (mResult == NULL) { // If no results, give an empty array. Maybe it's better to give a nil pointer? return (NO); } theNumFields = [self numOfFields]; theField = mysql_fetch_fields(mResult); if (index >= theNumFields) { // Out of range... should raise an exception theRet = NO; } else { switch(theField[index].type) { case FIELD_TYPE_TINY_BLOB: case FIELD_TYPE_BLOB: case FIELD_TYPE_MEDIUM_BLOB: case FIELD_TYPE_LONG_BLOB: theRet = YES; break; default: theRet = NO; break; } } return theRet; } - (BOOL)isBlobForKey:(NSString *)key /*" Return YES if the field (by name) with the given index is a BLOB. It should be used to discriminates between BLOBs and TEXTs. #{NOT YET IMPLEMENTED}, return YES for both BLOBs and TEXTs... "*/ { BOOL theRet; unsigned int theNumFields, index; MYSQL_FIELD *theField; if (mResult == NULL) { // If no results, give an empty array. Maybe it's better to give a nil pointer? return (NO); } if (mNames == NULL) { [self fetchFieldsName]; } theNumFields = [self numOfFields]; theField = mysql_fetch_fields(mResult); if ((index = [mNames indexOfObject:key]) == NSNotFound) { // Non existent key... should raise an exception theRet = NO; } else { switch(theField[index].type) { case FIELD_TYPE_TINY_BLOB: case FIELD_TYPE_BLOB: case FIELD_TYPE_MEDIUM_BLOB: case FIELD_TYPE_LONG_BLOB: theRet = YES; break; default: theRet = NO; break; } } return theRet; } - (NSString *)description /*" Return a (long) string containing the table of results, first line being the fields name, next line(s) the row(s). Useful to have NSLog logging a SMySQLResult (example). "*/ { if (mResult == NULL) { return @"This is an empty SMySQLResult\n"; } else { NSMutableString *theString = [NSMutableString stringWithCapacity:0]; int i, numFields; NSArray *theRow; MYSQL_ROW_OFFSET thePosition; // First line, saying we are displaying a SMySQLResult [theString appendString:@"SMySQLResult:\n"]; // Second line: the field names, tab separated [self fetchFieldsName]; for (i=0; i<((numFields = [mNames count])-1); i++) { [theString appendFormat:@"%@\t", [mNames objectAtIndex:i]]; } [theString appendFormat:@"%@\n", [mNames objectAtIndex:i]]; // Next lines, the records (saving current position to put it back after the full display) thePosition = mysql_row_tell(mResult); [self dataSeek:0]; while (theRow = [self fetchRowAsArray]) { for (i=0; i<(numFields - 1); i++) { [theString appendFormat:@"%@\t", [theRow objectAtIndex:i]]; } [theString appendFormat:@"%@\n", [theRow objectAtIndex:i]]; } // Returning to the proper row mysql_row_seek(mResult,thePosition); return theString; } } - (void)dealloc /* Do one really needs an explanation for this method? Which by the way you should not use... */ { if (mResult) { mysql_free_result(mResult); } if (mNames) { [mNames autorelease]; } if (mMySQLLocales) { [mMySQLLocales autorelease]; } [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; if (! theString) { return (const char *)NULL; } 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; NSString * theString; if (theCString == NULL) { return @""; } theData = [NSData dataWithBytes:theCString length:(strlen(theCString))]; theString = [[NSString alloc] initWithData:theData encoding:mEncoding]; if (theString) { [theString autorelease]; } return theString; } @end --- SMySQLConnection.h DELETED --- --- SMySQLResult.h DELETED --- --- SMySQLResult.m DELETED --- |