From 2a2a81c70c8dfe065f43bb79bf2d1c8b30446063 Mon Sep 17 00:00:00 2001 From: helge Date: Tue, 29 Jun 2004 19:13:17 +0000 Subject: [PATCH] git-svn-id: http://svn.opengroupware.org/SOGo/trunk@80 d1b88da0-ebda-0310-925b-ed51d893ca5b --- OGoContentStore/OCSFieldInfo.h | 20 ++++++ OGoContentStore/OCSFieldInfo.m | 80 +++++++++++++++++++++ OGoContentStore/OCSFolderManager.m | 18 ++++- OGoContentStore/OCSFolderType.h | 13 +++- OGoContentStore/OCSFolderType.m | 77 ++++++++++++++++---- OGoContentStore/appointment.ocs | 33 +++++++++ OGoContentStore/sql/appointment-create.psql | 9 +++ 7 files changed, 232 insertions(+), 18 deletions(-) create mode 100644 OGoContentStore/appointment.ocs create mode 100644 OGoContentStore/sql/appointment-create.psql diff --git a/OGoContentStore/OCSFieldInfo.h b/OGoContentStore/OCSFieldInfo.h index da59b882..49c79a18 100644 --- a/OGoContentStore/OCSFieldInfo.h +++ b/OGoContentStore/OCSFieldInfo.h @@ -25,10 +25,30 @@ #import +@class NSString, NSArray; + @interface OCSFieldInfo : NSObject { + NSString *columnName; + NSString *sqlType; + BOOL allowsNull; + BOOL isPrimaryKey; } ++ (NSArray *)fieldsForPropertyList:(NSArray *)_plist; +- (id)initWithPropertyList:(id)_plist; + +/* accessors */ + +- (NSString *)columnName; +- (NSString *)sqlType; +- (BOOL)doesAllowNull; +- (BOOL)isPrimaryKey; + +/* generating SQL */ + +- (NSString *)sqlCreateSection; + @end #endif /* __OGoContentStore_OCSFieldInfo_H__ */ diff --git a/OGoContentStore/OCSFieldInfo.m b/OGoContentStore/OCSFieldInfo.m index b7c2bd0c..11cd6513 100644 --- a/OGoContentStore/OCSFieldInfo.m +++ b/OGoContentStore/OCSFieldInfo.m @@ -25,8 +25,88 @@ @implementation OCSFieldInfo ++ (NSArray *)fieldsForPropertyList:(NSArray *)_plist { + NSMutableArray *fields; + unsigned i, count; + + if (_plist == nil) + return nil; + + count = [_plist count]; + fields = [NSMutableArray arrayWithCapacity:count]; + for (i = 0; i < count; i++) { + OCSFieldInfo *field; + + field = [[OCSFieldInfo alloc] initWithPropertyList: + [_plist objectAtIndex:i]]; + if (field) [fields addObject:field]; + [field release]; + } + return fields; +} + +- (id)initWithPropertyList:(id)_plist { + if ((self = [super init])) { + } + return self; +} + - (void)dealloc { + [self->columnName release]; + [self->sqlType release]; [super dealloc]; } +/* accessors */ + +- (NSString *)columnName { + return self->columnName; +} +- (NSString *)sqlType { + return self->sqlType; +} + +- (BOOL)doesAllowNull { + return self->allowsNull; +} +- (BOOL)isPrimaryKey { + return self->isPrimaryKey; +} + +/* generating SQL */ + +- (NSString *)sqlCreateSection { + NSMutableString *ms; + + ms = [NSMutableString stringWithCapacity:32]; + [ms appendString:[self columnName]]; + [ms appendString:@" "]; + [ms appendString:[self sqlType]]; + + [ms appendString:@" "]; + if (![self doesAllowNull]) [ms appendString:@"NOT "]; + [ms appendString:@"NULL"]; + + if ([self isPrimaryKey]) [ms appendString:@" PRIMARY KEY"]; + return ms; +} + +/* description */ + +- (NSString *)description { + NSMutableString *ms; + id tmp; + + ms = [NSMutableString stringWithCapacity:256]; + [ms appendFormat:@"<0x%08X[%@]:", self, NSStringFromClass([self class])]; + + if ((tmp = [self columnName])) [ms appendFormat:@" column=%@", tmp]; + if ((tmp = [self sqlType])) [ms appendFormat:@" sql=%@", tmp]; + if ([self doesAllowNull]) [ms appendString:@" allows-null"]; + if ([self isPrimaryKey]) [ms appendString:@" pkey"]; + + [ms appendString:@">"]; + return ms; +} + @end /* OCSFieldInfo */ diff --git a/OGoContentStore/OCSFolderManager.m b/OGoContentStore/OCSFolderManager.m index bbdef2a0..c2f4b8a8 100644 --- a/OGoContentStore/OCSFolderManager.m +++ b/OGoContentStore/OCSFolderManager.m @@ -98,13 +98,15 @@ static const char *OCSPathColumnPattern = "c_path%i"; return nil; } if ((self = [super init])) { + OCSFolderType *cal; + self->channelManager = [[OCSChannelManager defaultChannelManager] retain]; self->folderInfoLocation = [_url retain]; + cal = [[OCSFolderType alloc] initWithFolderTypeName:@"appointment"]; self->nameToType = - [[NSDictionary alloc] initWithObjectsAndKeys: - [OCSFolderType defaultCalendarFolderType], - @"appointment", nil]; + [[NSDictionary alloc] initWithObjectsAndKeys:cal, @"appointment", nil]; + [cal release]; cal = nil; } return self; } @@ -538,6 +540,16 @@ static const char *OCSPathColumnPattern = "c_path%i"; - (NSException *)createFolderOfType:(NSString *)_type atPath:(NSString *)_path{ // TODO: implement folder create + OCSFolderType *ftype; + + if ((ftype = [self folderTypeWithName:_type]) == nil) { + return [NSException exceptionWithName:@"OCSMissingFolderType" + reason:@"missing folder type" + userInfo:nil]; + } + + [self logWithFormat:@"create folder of type: %@", _type]; + return [NSException exceptionWithName:@"NotYetImplemented" reason:@"no money, no time, ..." userInfo:nil]; diff --git a/OGoContentStore/OCSFolderType.h b/OGoContentStore/OCSFolderType.h index 097f280c..57eb7dc9 100644 --- a/OGoContentStore/OCSFolderType.h +++ b/OGoContentStore/OCSFolderType.h @@ -39,7 +39,7 @@ #import -@class NSString, NSDictionary; +@class NSString, NSArray, NSDictionary; @class EOQualifier; @class OCSFolder; @@ -47,17 +47,24 @@ { NSString *blobTablePattern; // eg 'SOGo_$folderId$_blob NSString *quickTablePattern; // eg 'SOGo_$folderId$_quick - NSDictionary *fields; // maps a name to OCSFieldInfo + NSArray *fields; // OCSFieldInfo objects + NSDictionary *fieldDict; // maps a name to OCSFieldInfo EOQualifier *folderQualifier; // to further limit the table set } -+ (id)defaultCalendarFolderType; +- (id)initWithPropertyList:(id)_plist; +- (id)initWithContentsOfFile:(NSString *)_path; +- (id)initWithFolderTypeName:(NSString *)_typeName; /* operations */ - (NSString *)blobTableNameForFolder:(OCSFolder *)_folder; - (NSString *)quickTableNameForFolder:(OCSFolder *)_folder; +/* generating SQL */ + +- (NSString *)sqlQuickCreateWithTableName:(NSString *)_tabName; + @end #endif /* __OGoContentStore_OCSFolderType_H__ */ diff --git a/OGoContentStore/OCSFolderType.m b/OGoContentStore/OCSFolderType.m index 358f920d..5f15e9dc 100644 --- a/OGoContentStore/OCSFolderType.m +++ b/OGoContentStore/OCSFolderType.m @@ -22,35 +22,65 @@ #include "OCSFolderType.h" #include "OCSFolder.h" +#include "OCSFieldInfo.h" #include "common.h" #include @implementation OCSFolderType -static OCSFolderType *calendarType = nil; - -- (id)initAsCalendarFolderType { - // TODO: should be moved to a plist file +- (id)initWithPropertyList:(id)_plist { if ((self = [super init])) { - self->blobTablePattern = @"SOGo_$folderId$_blob"; - self->quickTablePattern = @"SOGo_$folderId$_quick"; - self->folderQualifier = nil; // a table per folder + self->blobTablePattern = [[_plist objectForKey:@"blobTablePattern"] copy]; + self->quickTablePattern = [[_plist objectForKey:@"quickTablePattern"]copy]; + // TODO: qualifier; - // TODO: which fields? + self->fields = [[OCSFieldInfo fieldsForPropertyList: + [_plist objectForKey:@"fields"]] retain]; } return self; } -+ (id)defaultCalendarFolderType { - if (calendarType == nil) - calendarType = [[self alloc] init]; - return calendarType; +- (id)initWithContentsOfFile:(NSString *)_path { + NSDictionary *plist; + + plist = [NSDictionary dictionaryWithContentsOfFile:_path]; + if (plist == nil) { + NSLog(@"ERROR(%s): could not read dictionary at path %@", + __PRETTY_FUNCTION__, _path); + [self release]; + return nil; + } + return [self initWithPropertyList:plist]; +} + +- (id)initWithFolderTypeName:(NSString *)_typeName { + // TODO: use GNUSTEP_PATHPREFIX_LIST + NSDictionary *env; + NSFileManager *fm; + NSString *filename, *path; + + env = [[NSProcessInfo processInfo] environment]; + fm = [NSFileManager defaultManager]; + filename = [_typeName stringByAppendingPathExtension:@"ocs"]; + + path = [env objectForKey:@"GNUSTEP_USER_ROOT"]; + path = [path stringByAppendingPathComponent:@"Library"]; + path = [path stringByAppendingPathComponent:@"OCSTypeModels"]; + path = [path stringByAppendingPathComponent:filename]; + if ([fm fileExistsAtPath:path]) + return [self initWithContentsOfFile:path]; + + NSLog(@"ERROR(%s): did not find model for type: '%@'", + __PRETTY_FUNCTION__, _typeName); + [self release]; + return nil; } - (void)dealloc { [self->blobTablePattern release]; [self->quickTablePattern release]; [self->fields release]; + [self->fieldDict release]; [self->folderQualifier release]; [super dealloc]; } @@ -79,6 +109,29 @@ static OCSFolderType *calendarType = nil; requiresAllVariables:NO]; } +/* generating SQL */ + +- (NSString *)sqlQuickCreateWithTableName:(NSString *)_tabName { + NSMutableString *sql; + unsigned i, count; + + sql = [NSMutableString stringWithCapacity:512]; + [sql appendString:@"CREATE TABLE "]; + [sql appendString:_tabName]; + [sql appendString:@" ( "]; + + count = [self->fields count]; + for (i = 0; i < count; i++) { + if (i > 0) [sql appendString:@", "]; + + [sql appendString:[[self->fields objectAtIndex:i] sqlCreateSection]]; + } + + [sql appendString:@")"]; + + return sql; +} + /* description */ - (NSString *)description { diff --git a/OGoContentStore/appointment.ocs b/OGoContentStore/appointment.ocs new file mode 100644 index 00000000..081a4cb2 --- /dev/null +++ b/OGoContentStore/appointment.ocs @@ -0,0 +1,33 @@ +{ + blobTablePattern = "SOGo_$folderId$_blob"; + quickTablePattern = "SOGo_$folderId$_quick"; + + fields = ( + { + columnName = pkey; + sqlType = "INTEGER"; + allowsNull = NO; + isPrimaryKey = YES; + }, + { + columnName = startdate; + sqlType = "INT"; + allowsNull = NO; + }, + { + columnName = enddate; + sqlType = "INT"; + allowsNull = NO; + }, + { + columnName = title; + sqlType = "VARCHAR(1000)"; + allowsNull = NO; + }, + { + columnName = participants; + sqlType = "VARCHAR(1000000)"; + allowsNull = NO; + }, + ); +} diff --git a/OGoContentStore/sql/appointment-create.psql b/OGoContentStore/sql/appointment-create.psql new file mode 100644 index 00000000..0a0459dc --- /dev/null +++ b/OGoContentStore/sql/appointment-create.psql @@ -0,0 +1,9 @@ +CREATE TABLE %s ( + pkey INTEGER + NOT NULL + PRIMARY KEY, + startdate INT NOT NULL, + enddate INT NOT NULL, + title VARCHAR(1000) NOT NULL, + participants VARCHAR(100000) NOT NULL +); -- 2.39.5