MCPResult


Inherits From:
NSObject
Declared In:
MCPResult.h


Class Description

$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++;
    }
    


Instance Variables

MYSQL_RES *mResult;
NSArray *mNames;
NSDictionary *mMySQLLocales;
NSStringEncoding mEncoding;
unsigned int mNumOfFields;

mResultThe MYSQL_RES structure of the C API
mNamesAn NSArray holding the name of the columns
mMySQLLocalesA Locales dictionary to define the locales of MySQL
mEncodingThe encoding used by MySQL server, to ISO-1 default
mNumOfFieldsThe number of fields in the result


Method Types

Class maintenance
+ initialize
Init used only by MCPConnection
- initWithMySQLPtr:encoding:
- initWithResPtr:encoding:
- init
General info on the result
- numOfRows
- numOfFields
Getting the rows
- dataSeek:
- fetchRowAsType:
- fetchRowAsArray
- fetchRowAsDictionary
Getting information on columns
- fetchFieldsName
- fetchTypesAsType:
- fetchTypesAsArray
- fetchTypesAsDictionary
- fetchFlagsAtIndex:
- fetchFlagsForKey:
- isBlobAtIndex:
- isBlobForKey:
Text data convertion to string
- stringWithText:
Utility method
- description
End of the scope...
- dealloc
Private methods, internal use only
- cStringFromString:
- stringWithCString:


Class Methods

initialize

+ (void)initialize

Initialize the class version to 2.3.1


Instance Methods

cStringFromString:

- (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.


dataSeek:

- (void)dataSeek:(my_ulonglong)row

Go to a precise row in the selected result. 0 is the very first row


dealloc

- (void)dealloc

No method description.


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).


fetchFieldsName

- (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


fetchFlagsAtIndex:

- (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...


fetchFlagsForKey:

- (unsigned int)fetchFlagsForKey:(NSString *)key

No method description.


fetchRowAsArray

- (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.


fetchRowAsDictionary

- (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.


fetchRowAsType:

- (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


fetchTypesAsArray

- (NSArray *)fetchTypesAsArray

Return an array of the fields' types.

NB: Returned object is immutable.


fetchTypesAsDictionary

- (NSDictionary *)fetchTypesAsDictionary

Return a dictionnary of the fields' types (keys are the fields' names).

NB: Returned object is immutable.


fetchTypesAsType:

- (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


init

- (id)init

Empty init, normaly of NO use to the user, again, MCPResult should be made through calls to MCPConnection


initWithMySQLPtr:encoding:

- (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.


initWithResPtr:encoding:

- (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.


isBlobAtIndex:

- (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).


isBlobForKey:

- (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).


numOfFields

- (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.


numOfRows

- (my_ulonglong)numOfRows

Return the number of rows selected by the query.


stringWithCString:

- (NSString *)stringWithCString:(const char *)theCString

Return a NSString from a C style string encoded with the character set of theMCPConnection.


stringWithText:

- (NSString *)stringWithText:(NSData *)theTextData

Use the string encoding to convert the returned NSData to a string (for a TEXT field)


Version 1.2 Copyright ©2004 by Serge Cohen. All Rights Reserved. Mon Aug 09 11:00:27 2004