From: helge Date: Sun, 20 Feb 2005 21:39:52 +0000 (+0000) Subject: added +adaptorForURL: factory method X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0636e33848aa667fc20ad27c16a030337801aacd;p=sope added +adaptorForURL: factory method git-svn-id: http://svn.opengroupware.org/SOPE/trunk@579 e4a50df8-12e2-0310-a44c-efbce7f8a7e3 --- diff --git a/sope-gdl1/GDLAccess/ChangeLog b/sope-gdl1/GDLAccess/ChangeLog index 7bee603f..f388d203 100644 --- a/sope-gdl1/GDLAccess/ChangeLog +++ b/sope-gdl1/GDLAccess/ChangeLog @@ -1,3 +1,9 @@ +2005-02-20 Helge Hess + + * EOAdaptor.m: added +adaptorForURL: method to create EOAdaptor + objects from JDBC style URLs (eg PostgreSQL://OGo:OGo@localhost/OGo") + (v4.5.47) + 2005-01-14 Helge Hess * EOAdaptorDataSource.m: fixed a bug in the sorting code, the "AS" was diff --git a/sope-gdl1/GDLAccess/EOAdaptor.h b/sope-gdl1/GDLAccess/EOAdaptor.h index a4e27187..cae78044 100644 --- a/sope-gdl1/GDLAccess/EOAdaptor.h +++ b/sope-gdl1/GDLAccess/EOAdaptor.h @@ -29,9 +29,7 @@ #import -@class NSMutableArray, NSArray; -@class NSDictionary; -@class NSString; +@class NSMutableArray, NSArray, NSDictionary, NSString, NSURL; @class EOModel; @class EOAttribute; @@ -63,6 +61,7 @@ /* Creating an EOAdaptor */ + (id)adaptorWithModel:(EOModel *)aModel; + (id)adaptorWithName:(NSString *)aName; ++ (id)adaptorForURL:(id)_url; - (id)initWithName:(NSString *)aName; /* Getting an adaptor's name */ diff --git a/sope-gdl1/GDLAccess/EOAdaptor.m b/sope-gdl1/GDLAccess/EOAdaptor.m index bed518e9..7f343ffe 100644 --- a/sope-gdl1/GDLAccess/EOAdaptor.m +++ b/sope-gdl1/GDLAccess/EOAdaptor.m @@ -184,6 +184,87 @@ return AUTORELEASE([[adaptorClass alloc] initWithName:adaptorName]); } ++ (NSString *)adaptorNameForURLScheme:(NSString *)_scheme { + // TODO: map scheme to adaptors (eg 'postgresql://' to PostgreSQL + + // TODO: hack in some known names + if ([_scheme isEqualToString:@"postgresql"]) return @"PostgreSQL"; + if ([_scheme isEqualToString:@"sybase"]) return @"Sybase10"; + if ([_scheme isEqualToString:@"sqlite"]) return @"SQLite3"; + return _scheme; +} + +- (NSDictionary *)connectionDictionaryForNSURL:(NSURL *)_url { + /* + "Database URLs" + + We use the schema: + postgresql://[user]:[password]@[host]:[port]/[dbname]/[tablename] + */ + NSMutableDictionary *md; + id tmp; + + md = [NSMutableDictionary dictionaryWithCapacity:8]; + + if ((tmp = [_url host]) != nil) + [md setObject:tmp forKey:@"hostName"]; + if ((tmp = [_url port]) != nil) + [md setObject:tmp forKey:@"port"]; + if ((tmp = [_url user]) != nil) + [md setObject:tmp forKey:@"userName"]; + if ((tmp = [_url password]) != nil) + [md setObject:tmp forKey:@"password"]; + + /* extract dbname */ + + tmp = [_url path]; + if ([tmp hasPrefix:@"/"]) tmp = [tmp substringFromIndex:1]; + if ([tmp length] > 0) { + NSRange r; + + r = [tmp rangeOfString:@"/"]; + if (r.length > 0) tmp = [tmp substringToIndex:r.location]; + + if ([tmp length] > 0) + [md setObject:tmp forKey:@"databaseName"]; + } + + return md; +} + ++ (id)adaptorForNSURL:(NSURL *)_url { + EOAdaptor *adaptor; + NSString *adaptorName; + NSDictionary *condict; + + if (_url == nil) + return nil; + + adaptorName = [self adaptorNameForURLScheme:[_url scheme]]; + adaptor = [self adaptorWithName:adaptorName]; + + if ((condict = [adaptor connectionDictionaryForNSURL:_url]) != nil) + [adaptor setConnectionDictionary:condict]; + + return adaptor; +} + ++ (id)adaptorForURL:(id)_url { + NSURL *url; + + if (_url == nil) + return nil; + + if ([_url isKindOfClass:[NSURL class]]) + url = _url; + else if ((url = [NSURL URLWithString:[_url stringValue]]) == nil) { + NSLog(@"ERROR(%s): could not parse URL: '%@'", __PRETTY_FUNCTION__, _url); + return nil; + } + + return [self adaptorForNSURL:url]; +} + - (id)initWithName:(NSString*)_name { ASSIGN(self->name, _name); self->contexts = [[NSMutableArray allocWithZone:[self zone]] init]; diff --git a/sope-gdl1/GDLAccess/Version b/sope-gdl1/GDLAccess/Version index 3e648f95..25b5b300 100644 --- a/sope-gdl1/GDLAccess/Version +++ b/sope-gdl1/GDLAccess/Version @@ -1,3 +1,3 @@ # version file -SUBMINOR_VERSION:=46 +SUBMINOR_VERSION:=47