Re: [Mysql-cocoa-users] numOfRows
Brought to you by:
sergecohen
From: John G. <jo...@jl...> - 2002-05-27 18:36:33
|
I'd like to add my comments as support to those of Bertrand, His proposal to make a single piece of code seems sensible, HOWEVER I would add that I think it may make sense to keep the original methods and wrap them around the new calls, this would be a benefit to a novice user, but would keep compatability with existing C API's. I know it is quite simple to add a fetchmode command, but it does tend to make code slightly less readable, also in addition I prefer to use wrapper classes that return a specified object (NSArray, OR NSDictionary etc.) so that the compiler can issue warnings that makes debugging easier than relying on (id) types - ALTHOUGH I would support this wholeheartedly for the generic function. I also like the quick queries proposed, and have actually implemented something similar within my code to make programming quicker - ie. simpleQuery() which queries, grabs all and returns. I have a possible new request to throw into the pot following some work I did last week. I am working on a lightweight content management system and have been producing queries that in turn produce additional queries. What would be useful for me is to return all my results as the strings that are returned in the SQL. For example: "SELECT PAGEID, TITLE FROM PAGES" where PAGEID is an INT and TITLE is a string. What would be useful is if I can return an NSDictionary object that has string objects for both PAGEID and TITLE, since I am simply passing the PAGEID back into and SQL query at some point in the future. I was browsing the library code and this looks like a case of simply skipping the typing process. So maybe this is something else that could be built into a generic version of the query code with an extra Fetch mode flag (maybe RETURN_MODE) anyway just some feedback JOhn On Friday, May 24, 2002, at 04:19 PM, Bertrand Mansion wrote: > 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 > > > > > > > _______________________________________________________________ > > Don't miss the 2002 Sprint PCS Application Developer's Conference > August 25-28 in Las Vegas -- http://devcon.sprintpcs.com/adp/index.cfm > > _______________________________________________ > Mysql-cocoa-users mailing list > Mys...@li... > https://lists.sourceforge.net/lists/listinfo/mysql-cocoa-users > |