From 6f2422ee3294b7825772db33ed69f3a264faa041 Mon Sep 17 00:00:00 2001 From: CaS Date: Sun, 22 Aug 2004 09:34:18 +0000 Subject: [PATCH] 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 --- ChangeLog | 5 +++++ MySQL.m | 2 +- Postgres.m | 14 +++++++------- SQLClient.h | 26 +++++++++++++++++++++++++- SQLClient.m | 44 ++++++++++++++++++++++++++++++++++---------- 5 files changed, 72 insertions(+), 19 deletions(-) diff --git a/ChangeLog b/ChangeLog index afe024c..904c6a5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Sun Aug 22 10:35:00 2004 Richard Frith-Macdonald + + * SQLClient.[hm]: Add ([SQLRecord-dictionary]) and tidy/comment the + class a bit better. + Sat Aug 07 14:25:00 2004 Richard Frith-Macdonald * WebServer.m: Add session timeouts to kill off idle sessions. diff --git a/MySQL.m b/MySQL.m index a05ae89..0949471 100644 --- a/MySQL.m +++ b/MySQL.m @@ -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++) diff --git a/Postgres.m b/Postgres.m index 9319566..4f0aa0c 100644 --- a/Postgres.m +++ b/Postgres.m @@ -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++) { diff --git a/SQLClient.h b/SQLClient.h index a1cd56f..d7fc537 100644 --- a/SQLClient.h +++ b/SQLClient.h @@ -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.
+ * NB. The values and keys are retained by the record rather + * than being copied.
+ * A nil value is represented by [NSNull null].
+ * 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.
+ * The field name is case insensitive. + */ - (id) objectForKey: (NSString*)key; @end diff --git a/SQLClient.m b/SQLClient.m index 702319b..b94b461 100644 --- a/SQLClient.m +++ b/SQLClient.m @@ -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;