#import <Foundation/NSObject.h>
-@class NSMutableArray, NSArray;
-@class NSDictionary;
-@class NSString;
+@class NSMutableArray, NSArray, NSDictionary, NSString, NSURL;
@class EOModel;
@class EOAttribute;
/* Creating an EOAdaptor */
+ (id)adaptorWithModel:(EOModel *)aModel;
+ (id)adaptorWithName:(NSString *)aName;
++ (id)adaptorForURL:(id)_url;
- (id)initWithName:(NSString *)aName;
/* Getting an adaptor's name */
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];