- Inherits From:
- NSObject
- Declared In:
- MCPResult.h
$Id: MCPResult.m,v 1.5 2004/08/04 23:24:06 sergecohen Exp $
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 MCPConnection object, in this way (see MCPConnection documentation):
MCPConnection *theConnec = [MCPConnection alloc];
MCPResult *theRes;
NSDictionary *theDict;
NSArray *theColNames;
int i, j;
theConnec = [theConnec initToHost:@"albert.com" withLogin:@"toto" password:@"albert" usingPort: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 objectAtIndex:j], [theDict objectForKey:[theColNames objectAtIndex:j]]);
}
i++;
}
MYSQL_RES *mResult;
NSArray *mNames;
NSDictionary *mMySQLLocales;
NSStringEncoding mEncoding;
unsigned int mNumOfFields;
mResult The MYSQL_RES structure of the C API mNames An NSArray holding the name of the columns mMySQLLocales A Locales dictionary to define the locales of MySQL mEncoding The encoding used by MySQL server, to ISO-1 default mNumOfFields The number of fields in the result
Class maintenanceInit used only by MCPConnection
- + initialize
General info on the result
- - initWithMySQLPtr:encoding:
- - initWithResPtr:encoding:
- - init
Getting the rows
- - numOfRows
- - numOfFields
Getting information on columns
- - dataSeek:
- - fetchRowAsType:
- - fetchRowAsArray
- - fetchRowAsDictionary
Text data convertion to string
- - fetchFieldsName
- - fetchTypesAsType:
- - fetchTypesAsArray
- - fetchTypesAsDictionary
- - fetchFlagsAtIndex:
- - fetchFlagsForKey:
- - isBlobAtIndex:
- - isBlobForKey:
Utility method
- - stringWithText:
End of the scope...
- - description
Private methods, internal use only
- - dealloc
- - cStringFromString:
- - stringWithCString:
+ (void)initialize
Initialize the class version to 2.3.1
- (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 MCPConnection. Lossy conversions are enabled.
- (void)dataSeek:(my_ulonglong)row
Go to a precise row in the selected result. 0 is the very first row
- (void)dealloc
No method description.
- (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 MCPResult (example).
- (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)fetchFlagsAtIndex:(unsigned int)index
Return the MySQL flags of the column at the given index... Can be used to check if a number is signed or not...
- (unsigned int)fetchFlagsForKey:(NSString *)key
No method description.
- (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 ...).
Just a typed wrapper for method fetchRosAsType:
(with arg MCPTypeArray).
NB: Returned object is immutable.
- (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 ...).
Just a typed wrapper for method fetchRosAsType:
(with arg MCPTypeDictionary).
NB: Returned object is immutable.
- (id)fetchRowAsType:(MCPReturnType)aType
Return the next row of the result as a collection of type defined by aType (namely MCPTypeArray or MCPTypeDictionary). Each field of the row is made into a proper object to hold the info (NSNumber -indeed MCPNumber, to keep signedness-, NSString...).
This method returned directly the mutable object generated while going through all the columns
- (NSArray *)fetchTypesAsArray
Return an array of the fields' types.
NB: Returned object is immutable.
- (NSDictionary *)fetchTypesAsDictionary
Return a dictionnary of the fields' types (keys are the fields' names).
NB: Returned object is immutable.
- (id)fetchTypesAsType:(MCPReturnType)aType
Return a collection of the fields's type. The type of collection is choosen by the aType variable (MCPTypeArray or MCPTypeDictionary).
This method returned directly the mutable object generated while going through all the columns
- (id)init
Empty init, normaly of NO use to the user, again, MCPResult should be made through calls to MCPConnection
- (id)initWithMySQLPtr:(MYSQL *)mySQLPtr encoding:(NSStringEncoding)theEncoding
initialise a MCPResult, it is used internally by MCPConnection queryString:
method: the only proper way to get a running MCPResult object.
- (id)initWithResPtr:(MYSQL_RES *)mySQLResPtr encoding:(NSStringEncoding)theEncoding
This metod it is used internally by MCPConnection object when it have already a MYSQL_RES object to initialise MCPResult object. Initialise a MCPResult with the MYSQL_RES pointer (returned by such a function as mysql_list_dbs). NB: MCPResult should be made by using one of the method of MCPConnection.
- (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.
DEPRECATED, This method is not consistent with the C API which is supposed to return YES for BOTH text and blob (and BTW is also deprecated)...
NOTE That the current version handles properly TEXT, and returns those as NSString (and not NSData as it used to be).
- (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.
DEPRECATED, This method is not consistent with the C API which is supposed to return YES for BOTH text and blob (and BTW is also deprecated)...
NOTE That the current version handles properly TEXT, and returns those as NSString (and not NSData as it used to be).
- (unsigned int)numOfFields
Return the number of fields selected by the query. As a side effect it forces an update of the number of fields.
- (my_ulonglong)numOfRows
Return the number of rows selected by the query.
- (NSString *)stringWithCString:(const char *)theCString
Return a NSString from a C style string encoded with the character set of theMCPConnection.
- (NSString *)stringWithText:(NSData *)theTextData
Use the string encoding to convert the returned NSData to a string (for a TEXT field)