From bfbb45ce2addd0555fea58922d35b737486ed332 Mon Sep 17 00:00:00 2001 From: helge Date: Fri, 4 Mar 2005 22:20:01 +0000 Subject: [PATCH] started work on SQL generation git-svn-id: http://svn.opengroupware.org/SOGo/trunk@626 d1b88da0-ebda-0310-925b-ed51d893ca5b --- OGoContentStore/ChangeLog | 9 +++ OGoContentStore/GNUmakefile | 9 ++- OGoContentStore/GNUmakefile.preamble | 3 + OGoContentStore/OCSFieldInfo.h | 11 ++- OGoContentStore/OCSFieldInfo.m | 28 +++++-- OGoContentStore/OCSFolderType.h | 5 +- OGoContentStore/OCSFolderType.m | 44 ++++++----- OGoContentStore/Version | 2 +- OGoContentStore/ocs_gensql.m | 114 +++++++++++++++++++++++++++ OGoContentStore/ocs_ls.m | 2 +- 10 files changed, 192 insertions(+), 35 deletions(-) create mode 100644 OGoContentStore/ocs_gensql.m diff --git a/OGoContentStore/ChangeLog b/OGoContentStore/ChangeLog index 48e7c952..34cdd493 100644 --- a/OGoContentStore/ChangeLog +++ b/OGoContentStore/ChangeLog @@ -1,3 +1,12 @@ +2005-03-04 Helge Hess + + * v0.9.24 + + * ocs_gensql.m: started tool to create SQL CREATE from ocs model file + + * OCSFolderType.m: small change to the factory API, changed to use + NGResourceLocator + 2005-03-03 Helge Hess * OCSFolderManager.m: fixed a bug in subfolder listing (v0.9.23) diff --git a/OGoContentStore/GNUmakefile b/OGoContentStore/GNUmakefile index 2d07b7a1..05d83669 100644 --- a/OGoContentStore/GNUmakefile +++ b/OGoContentStore/GNUmakefile @@ -4,7 +4,7 @@ include $(GNUSTEP_MAKEFILES)/common.make include ./Version LIBRARY_NAME = libOGoContentStore -TOOL_NAME = ocs_ls ocs_mkdir ocs_cat ocs_recreatequick +TOOL_NAME = ocs_ls ocs_mkdir ocs_cat ocs_recreatequick ocs_gensql TYPEMODELS_DIR = $(GNUSTEP_USER_ROOT)/Library/OCSTypeModels/ @@ -47,9 +47,10 @@ libOGoContentStore_TYPEMODELS += \ appointment.ocs \ contact.ocs \ -ocs_ls_OBJC_FILES += ocs_ls.m -ocs_mkdir_OBJC_FILES += ocs_mkdir.m -ocs_cat_OBJC_FILES += ocs_cat.m +ocs_ls_OBJC_FILES += ocs_ls.m +ocs_mkdir_OBJC_FILES += ocs_mkdir.m +ocs_cat_OBJC_FILES += ocs_cat.m +ocs_gensql_OBJC_FILES += ocs_gensql.m ocs_recreatequick_OBJC_FILES += ocs_recreatequick.m -include GNUmakefile.preamble diff --git a/OGoContentStore/GNUmakefile.preamble b/OGoContentStore/GNUmakefile.preamble index f94b0797..46e9d225 100644 --- a/OGoContentStore/GNUmakefile.preamble +++ b/OGoContentStore/GNUmakefile.preamble @@ -2,6 +2,8 @@ libOGoContentStore_LIBRARIES_DEPEND_UPON += \ -lGDLAccess \ + -lNGiCal \ + -lNGExtensions \ -lEOControl \ -lSaxObjC @@ -16,6 +18,7 @@ ocs_ls_TOOL_LIBS += $(OCS_TOOL_LIBS) ocs_mkdir_TOOL_LIBS += $(OCS_TOOL_LIBS) ocs_cat_TOOL_LIBS += $(OCS_TOOL_LIBS) ocs_recreatequick_TOOL_LIBS += $(OCS_TOOL_LIBS) +ocs_gensql_TOOL_LIBS += $(OCS_TOOL_LIBS) ADDITIONAL_INCLUDE_DIRS += -I. -I.. diff --git a/OGoContentStore/OCSFieldInfo.h b/OGoContentStore/OCSFieldInfo.h index 49c79a18..a61e17d9 100644 --- a/OGoContentStore/OCSFieldInfo.h +++ b/OGoContentStore/OCSFieldInfo.h @@ -1,5 +1,5 @@ /* - Copyright (C) 2004 SKYRIX Software AG + Copyright (C) 2004-2005 SKYRIX Software AG This file is part of OpenGroupware.org. @@ -18,13 +18,20 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -// $Id$ #ifndef __OGoContentStore_OCSFieldInfo_H__ #define __OGoContentStore_OCSFieldInfo_H__ #import +/* + OCSFieldInfo + + The field info inside an .ocs schema file. + + The field info objects are stored in an OCSFolderType. +*/ + @class NSString, NSArray; @interface OCSFieldInfo : NSObject diff --git a/OGoContentStore/OCSFieldInfo.m b/OGoContentStore/OCSFieldInfo.m index 11cd6513..f9763a6b 100644 --- a/OGoContentStore/OCSFieldInfo.m +++ b/OGoContentStore/OCSFieldInfo.m @@ -39,7 +39,7 @@ field = [[OCSFieldInfo alloc] initWithPropertyList: [_plist objectAtIndex:i]]; - if (field) [fields addObject:field]; + if (field != nil) [fields addObject:field]; [field release]; } return fields; @@ -47,6 +47,16 @@ - (id)initWithPropertyList:(id)_plist { if ((self = [super init])) { + self->columnName = [[_plist objectForKey:@"columnName"] copy]; + self->sqlType = [[_plist objectForKey:@"sqlType"] copy]; + + self->allowsNull = [[_plist objectForKey:@"allowsNull"] boolValue]; + self->isPrimaryKey = [[_plist objectForKey:@"isPrimaryKey"] boolValue]; + + if (![self->columnName isNotNull] || ![self->sqlType isNotNull]) { + [self release]; + return nil; + } } return self; } @@ -93,18 +103,22 @@ /* description */ -- (NSString *)description { - NSMutableString *ms; +- (void)appendAttributesToDescription:(NSMutableString *)ms { id tmp; - ms = [NSMutableString stringWithCapacity:256]; - [ms appendFormat:@"<0x%08X[%@]:", self, NSStringFromClass([self class])]; + if ((tmp = [self columnName]) != nil) [ms appendFormat:@" column=%@", tmp]; + if ((tmp = [self sqlType]) != nil) [ms appendFormat:@" sql=%@", tmp]; - 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"]; +} + +- (NSString *)description { + NSMutableString *ms; + ms = [NSMutableString stringWithCapacity:256]; + [ms appendFormat:@"<0x%08X[%@]:", self, NSStringFromClass([self class])]; + [self appendAttributesToDescription:ms]; [ms appendString:@">"]; return ms; } diff --git a/OGoContentStore/OCSFolderType.h b/OGoContentStore/OCSFolderType.h index c07474b5..ed4fedfa 100644 --- a/OGoContentStore/OCSFolderType.h +++ b/OGoContentStore/OCSFolderType.h @@ -1,5 +1,5 @@ /* - Copyright (C) 2004 SKYRIX Software AG + Copyright (C) 2004-2005 SKYRIX Software AG This file is part of OpenGroupware.org. @@ -18,7 +18,6 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -// $Id$ #ifndef __OGoContentStore_OCSFolderType_H__ #define __OGoContentStore_OCSFolderType_H__ @@ -54,6 +53,8 @@ OCSFieldExtractor *extractor; } ++ (id)folderTypeWithName:(NSString *)_type; + - (id)initWithPropertyList:(id)_plist; - (id)initWithContentsOfFile:(NSString *)_path; - (id)initWithFolderTypeName:(NSString *)_typeName; diff --git a/OGoContentStore/OCSFolderType.m b/OGoContentStore/OCSFolderType.m index 8b2fa5e3..4fad4335 100644 --- a/OGoContentStore/OCSFolderType.m +++ b/OGoContentStore/OCSFolderType.m @@ -25,6 +25,7 @@ #include "OCSiCalFieldExtractor.h" #include "common.h" #include +#include @implementation OCSFolderType @@ -56,29 +57,35 @@ return [self initWithPropertyList:plist]; } -- (id)initWithFolderTypeName:(NSString *)_typeName { - // TODO: use GNUSTEP_PATHPREFIX_LIST - NSDictionary *env; - NSFileManager *fm; - NSString *filename, *path; ++ (NGResourceLocator *)resourceLocator { + NGResourceLocator *loc; - env = [[NSProcessInfo processInfo] environment]; - fm = [NSFileManager defaultManager]; - filename = [_typeName stringByAppendingPathExtension:@"ocs"]; + loc = [NGResourceLocator resourceLocatorForGNUstepPath: + @"Library/OCSTypeModels" + fhsPath:@"share/ocs"]; + return loc; +} + ++ (id)folderTypeWithName:(NSString *)_typeName { + NSString *filename, *path; - 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]; + filename = [_typeName stringByAppendingPathExtension:@"ocs"]; + path = [[self resourceLocator] lookupFileWithName:filename]; + if (path != nil) + return [[self alloc] initWithContentsOfFile:path]; + NSLog(@"ERROR(%s): did not find model for type: '%@'", __PRETTY_FUNCTION__, _typeName); - [self release]; return nil; } +- (id)initWithFolderTypeName:(NSString *)_typeName { + // DEPRECATED + [self release]; + return [[OCSFolderType folderTypeWithName:_typeName] retain]; +} + - (void)dealloc { [self->extractor release]; [self->extractorClassName release]; @@ -123,16 +130,17 @@ sql = [NSMutableString stringWithCapacity:512]; [sql appendString:@"CREATE TABLE "]; [sql appendString:_tabName]; - [sql appendString:@" ( "]; + [sql appendString:@" (\n"]; count = [self->fields count]; for (i = 0; i < count; i++) { - if (i > 0) [sql appendString:@", "]; + if (i > 0) [sql appendString:@",\n"]; + [sql appendString:@" "]; [sql appendString:[[self->fields objectAtIndex:i] sqlCreateSection]]; } - [sql appendString:@")"]; + [sql appendString:@"\n)"]; return sql; } diff --git a/OGoContentStore/Version b/OGoContentStore/Version index 55399d44..fdd7e4c1 100644 --- a/OGoContentStore/Version +++ b/OGoContentStore/Version @@ -2,7 +2,7 @@ MAJOR_VERSION=0 MINOR_VERSION=9 -SUBMINOR_VERSION:=23 +SUBMINOR_VERSION:=24 # v0.9.19 requires libNGiCal v4.5.40 # v0.9.18 requires libNGiCal v4.5.38 diff --git a/OGoContentStore/ocs_gensql.m b/OGoContentStore/ocs_gensql.m new file mode 100644 index 00000000..a4c37868 --- /dev/null +++ b/OGoContentStore/ocs_gensql.m @@ -0,0 +1,114 @@ +/* + Copyright (C) 2005 SKYRIX Software AG + + This file is part of OpenGroupware.org. + + OGo is free software; you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + OGo is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with OGo; see the file COPYING. If not, write to the + Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA + 02111-1307, USA. +*/ + +#import + +@class NSUserDefaults, NSArray; +@class OCSFolderManager; + +@interface Tool : NSObject +{ + NSUserDefaults *ud; +} + ++ (int)runWithArgs:(NSArray *)_args; +- (int)run; + +@end + +#include +#include "common.h" + +@implementation Tool + +- (id)init { + if ((self = [super init])) { + self->ud = [[NSUserDefaults standardUserDefaults] retain]; + } + return self; +} +- (void)dealloc { + [self->ud release]; + [super dealloc]; +} + +/* operation */ + +- (int)runOnTable:(NSString *)_tableName typeName:(NSString *)_typeName { + OCSFolderType *folderType; + + if ((folderType = [OCSFolderType folderTypeWithName:_typeName]) != nil) { + NSString *s; + + s = [folderType sqlQuickCreateWithTableName:_tableName]; + + fwrite([s cString], 1, [s cStringLength], stdout); + printf("\n"); + } + else { + fprintf(stderr, "ERROR: did not find OCS type: '%s'\n", + [_typeName cString]); + } + + return 0; +} + +- (int)run { + NSEnumerator *e; + NSString *tableName, *typeName; + + e = [[[NSProcessInfo processInfo] argumentsWithoutDefaults] + objectEnumerator]; + [e nextObject]; // skip tool name + + while ((tableName = [e nextObject]) != nil) { + typeName = [e nextObject]; + if (typeName == nil) { + [self logWithFormat:@"got tablename '%@' but no type?!", tableName]; + break; + } + + [self runOnTable:tableName typeName:typeName]; + } + + return 0; +} ++ (int)runWithArgs:(NSArray *)_args { + return [(Tool *)[[[self alloc] init] autorelease] run]; +} + +@end /* Tool */ + +int main(int argc, char **argv, char **env) { + NSAutoreleasePool *pool; + int rc; + + pool = [[NSAutoreleasePool alloc] init]; +#if LIB_FOUNDATION_LIBRARY + [NSProcessInfo initializeWithArguments:argv count:argc environment:env]; +#endif + + rc = [Tool runWithArgs: + [[NSProcessInfo processInfo] argumentsWithoutDefaults]]; + + [pool release]; + return rc; +} diff --git a/OGoContentStore/ocs_ls.m b/OGoContentStore/ocs_ls.m index c3ead10a..73c6fe72 100644 --- a/OGoContentStore/ocs_ls.m +++ b/OGoContentStore/ocs_ls.m @@ -112,7 +112,7 @@ objectEnumerator]; [e nextObject]; // skip tool name - while ((path = [e nextObject])) + while ((path = [e nextObject]) != nil) [self runOnPath:path]; return 0; -- 2.39.5