New method to convert SQLRecord to a dictionary.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/sqlclient/trunk@19900 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
CaS 2004-08-22 09:34:18 +00:00
parent 8819cf4b9b
commit 6f2422ee32
5 changed files with 72 additions and 19 deletions

View file

@ -1,3 +1,8 @@
Sun Aug 22 10:35:00 2004 Richard Frith-Macdonald <rfm@gnu.org>
* SQLClient.[hm]: Add ([SQLRecord-dictionary]) and tidy/comment the
class a bit better.
Sat Aug 07 14:25:00 2004 Richard Frith-Macdonald <rfm@gnu.org>
* WebServer.m: Add session timeouts to kill off idle sessions.

View file

@ -299,7 +299,7 @@ static unsigned int trim(char *str)
int recordCount = mysql_num_rows(result);
int fieldCount = mysql_num_fields(result);
MYSQL_FIELD *fields = mysql_fetch_fields(result);
id keys[fieldCount];
NSString *keys[fieldCount];
int i;
for (i = 0; i < fieldCount; i++)

View file

@ -377,13 +377,13 @@ static unsigned int trim(char *str)
}
if (PQresultStatus(result) == PGRES_TUPLES_OK)
{
int recordCount = PQntuples(result);
int fieldCount = PQnfields(result);
id keys[fieldCount];
int types[fieldCount];
int modifiers[fieldCount];
int formats[fieldCount];
int i;
int recordCount = PQntuples(result);
int fieldCount = PQnfields(result);
NSString *keys[fieldCount];
int types[fieldCount];
int modifiers[fieldCount];
int formats[fieldCount];
int i;
for (i = 0; i < fieldCount; i++)
{

View file

@ -176,10 +176,34 @@
*/
@interface SQLRecord : NSArray
{
@private
unsigned int count;
}
+ (id) newWithValues: (id*)v keys: (id*)k count: (unsigned int)c;
/**
* Create a new SQLRecord containing the specified fields.<br />
* NB. The values and keys are <em>retained</em> by the record rather
* than being copied.<br />
* A nil value is represented by [NSNull null].<br />
* Keys must be unique string values (case insensitive comparison).
*/
+ (id) newWithValues: (id*)v keys: (NSString**)k count: (unsigned int)c;
/**
* Returns an array containing the names of all the fields in the record.
*/
- (NSArray*) allKeys;
/**
* Return the record as a mutable dictionary with the keys as the
* record field names standardised to be lowercase strings.
*/
- (NSMutableDictionary*) dictionary;
/**
* Returns the value of the named field.<br />
* The field name is case insensitive.
*/
- (id) objectForKey: (NSString*)key;
@end

View file

@ -51,6 +51,8 @@ typedef struct {
@defs(SQLTransaction);
} *TDefs;
static NSNull *null = nil;
@implementation SQLRecord
+ (id) allocWithZone: (NSZone*)aZone
{
@ -58,7 +60,15 @@ typedef struct {
return nil;
}
+ (id) newWithValues: (id*)v keys: (id*)k count: (unsigned int)c
+ (void) initialize
{
if (null == nil)
{
null = [NSNull new];
}
}
+ (id) newWithValues: (id*)v keys: (NSString**)k count: (unsigned int)c
{
id *ptr;
SQLRecord *r;
@ -69,16 +79,19 @@ typedef struct {
ptr = ((void*)&(r->count)) + sizeof(r->count);
for (pos = 0; pos < c; pos++)
{
ptr[pos] = RETAIN(v[pos]);
if (v[pos] == nil)
{
ptr[pos] = RETAIN(null);
}
else
{
ptr[pos] = RETAIN(v[pos]);
}
ptr[pos + c] = RETAIN(k[pos]);
}
return r;
}
/**
* Returns an array containing the names of all the fields in the
* record, in the order in which they occur in the record.
*/
- (NSArray*) allKeys
{
id *ptr;
@ -111,6 +124,21 @@ typedef struct {
NSDeallocateObject(self);
}
- (NSMutableDictionary*) dictionary
{
NSMutableDictionary *d;
unsigned pos;
id *ptr;
ptr = ((void*)&count) + sizeof(count);
d = [NSMutableDictionary dictionaryWithCapacity: count];
for (pos = 0; pos < count; pos++)
{
[d setObject: ptr[pos] forKey: [ptr[pos + count] lowercaseString]];
}
return d;
}
- (id) init
{
NSLog(@"Illegal attempt to -init an SQLRecord");
@ -131,10 +159,6 @@ typedef struct {
return ptr[pos];
}
/**
* Returns the first field in the record whose name matches the specified
* key. Uses an exact match in preference to a case-insensitive match.
*/
- (id) objectForKey: (NSString*)key
{
id *ptr;