Re: [Mysql-cocoa-users] numOfRows
Brought to you by:
sergecohen
From: Bertrand M. <bma...@wa...> - 2002-05-24 16:17:30
|
Hi serge, I've had a look at the implementation of fetchRowAsDictionary and fetchRowAsArray : they look quite similar and are both quite long due to the typing process. This means they are a pain to maintain. I believe there could be some code reuse. So I suggest a different approach. In PEAR DB, we use a generic method to do the fetch work, the result is set according to a specified fetch mode. For instance it could be: fetchRowWithMode:(unsigned int)mode with constants like: DB_FETCHMODE_ORDERED (for array) = 0 DB_FETCHMODE_ASSOC (for dictionary) = 1 DB_FETCHMODE_OBJECT (column data as object properties) = 2 DB_FETCHMODE_FLIPPED (multidimensional array: column name => all rows) = 3 ... which would give us: [result fetchRowWithMode:DB_FETCHMODE_ASSOC]; instead of [result fetchRowAsDictionary]; Tell me what you think about that. Next, I think we could implement a few methods that are very handy when it comes to quickly programming. They are: - (id) getOne:(NSString *) query; getOne takes a query string as parameter and returns the first field from the first row. It initialises and dealloc the SMySQLResult itself so the developer does not need to take care of that. Ex: [mConnection getOne:@"select password from users where username='user1'"]; It should return nil if no result is found. - (id) getRow:(NSString *) query withMode:(unsigned int) mode; getRow will return an NSArray or a NSDictionary containing the first row of the result according to what mode it is given. Ex: [mConnection getRow:@"select * from users where username='user1'" withMode:]; It should return nil if no result is found. - (id) getAll:(NSString *) query withMode:(unsigned int) mode; Same as getRow but will fill a multidimensional NSArray with all the rows. [mConnection getAll:@"select * from users" withMode:DB_FETCHMODE_ASSOC]; It should return nil if no result is found. - (id) getCol:(NSString *) query withColIndex:(unsigned int) col; Will fetch a column (by its colunm index starting from 0) from a result set and return it as an NSArray. Ex: [mConnection getCol:@"select * from users" withColIndex:0]; It should return nil if no result is found. Tell me what you think of that too. I don't think it will be too difficult to code. Take care, Bertrand Mansion Mamasam |