From 7b31872d90d55b2ace37ad9556d631ed6aaa1c1f Mon Sep 17 00:00:00 2001 From: helge Date: Tue, 12 Jul 2005 15:19:26 +0000 Subject: [PATCH] added profile creation git-svn-id: http://svn.opengroupware.org/SOGo/trunk@729 d1b88da0-ebda-0310-925b-ed51d893ca5b --- SOGo/SoObjects/SOGo/AgenorUserDefaults.h | 3 +- SOGo/SoObjects/SOGo/AgenorUserDefaults.m | 90 ++++++++++++++++++------ SOGo/SoObjects/SOGo/ChangeLog | 2 + SOGo/SoObjects/SOGo/Version | 2 +- 4 files changed, 73 insertions(+), 24 deletions(-) diff --git a/SOGo/SoObjects/SOGo/AgenorUserDefaults.h b/SOGo/SoObjects/SOGo/AgenorUserDefaults.h index d2751389..f984fc91 100644 --- a/SOGo/SoObjects/SOGo/AgenorUserDefaults.h +++ b/SOGo/SoObjects/SOGo/AgenorUserDefaults.h @@ -47,7 +47,8 @@ struct { int modified:1; - int reserved:31; + int isNew:1; + int reserved:30; } defFlags; } diff --git a/SOGo/SoObjects/SOGo/AgenorUserDefaults.m b/SOGo/SoObjects/SOGo/AgenorUserDefaults.m index dcab5b00..9b97cc65 100644 --- a/SOGo/SoObjects/SOGo/AgenorUserDefaults.m +++ b/SOGo/SoObjects/SOGo/AgenorUserDefaults.m @@ -136,15 +136,15 @@ static NSString *uidColumnName = @"uid"; /* fetch values */ row = [channel fetchAttributes:attrs withZone:NULL]; + self->defFlags.isNew = (row != nil) ? 0 : 1; [channel cancelFetch]; /* remember values */ [self->values release]; self->values = nil; - if (row != nil) - self->values = [row mutableCopy]; - else - self->values = [[NSMutableDictionary alloc] initWithCapacity:8]; + self->values = (row != nil) + ? [row mutableCopy] + : [[NSMutableDictionary alloc] initWithCapacity:8]; [self->values removeObjectForKey:uidColumnName]; ASSIGN(self->lastFetch, [NSCalendarDate date]); @@ -154,6 +154,63 @@ static NSString *uidColumnName = @"uid"; return YES; } +- (NSString *)formatValue:(id)_value forAttribute:(EOAttribute *)_attribute { + NSString *s; + + if (![_value isNotNull]) + return @"NULL"; + + if ([[_attribute externalType] hasPrefix:@"int"]) + return [_value stringValue]; + + s = [_value stringValue]; + s = [s stringByReplacingString:@"'" withString:@"''"]; + s = [[@"'" stringByAppendingString:s] stringByAppendingString:@"'"]; + return s; +} + +- (NSString *)generateSQLForInsert { + NSMutableString *sql; + unsigned i, count; + + if ([self->values count] == 0) + return nil; + + sql = [NSMutableString stringWithCapacity:2048]; + + [sql appendString:@"INSERT INTO "]; + [sql appendString:[[self tableURL] gcsTableName]]; + [sql appendString:@" ( uid"]; + + for (i = 0, count = [self->fieldNames count]; i < count; i++) { + EOAttribute *attr; + + attr = [self->attributes objectForKey:[self->fieldNames objectAtIndex:i]]; + [sql appendString:@", "]; + [sql appendString:[attr columnName]]; + } + + [sql appendString:@") VALUES ("]; + + [sql appendString:@"'"]; + [sql appendString:[self uid]]; // TODO: escaping necessary? + [sql appendString:@"'"]; + + for (i = 0, count = [self->fieldNames count]; i < count; i++) { + EOAttribute *attr; + id value; + + attr = [self->attributes objectForKey:[self->fieldNames objectAtIndex:i]]; + value = [self->values objectForKey:[self->fieldNames objectAtIndex:i]]; + + [sql appendString:@", "]; + [sql appendString:[self formatValue:value forAttribute:attr]]; + } + + [sql appendString:@")"]; + return sql; +} + - (NSString *)generateSQLForUpdate { NSMutableString *sql; unsigned i, count; @@ -178,23 +235,8 @@ static NSString *uidColumnName = @"uid"; if (i != 0) [sql appendString:@", "]; [sql appendString:[attr columnName]]; - - if ([value isNotNull]) { - /* a rather limited set of supported value types */ - - if ([[attr externalType] hasPrefix:@"int"]) - [sql appendFormat:@" = %i", [value intValue]]; - else { - // TODO: any other escaping?! move to adaptor? - value = [value stringValue]; - value = [value stringByReplacingString:@"'" withString:@"''"]; - [sql appendString:@" = '"]; - [sql appendString:value]; - [sql appendString:@"'"]; - } - } - else - [sql appendString:@" = NULL"]; + [sql appendString:@" = "]; + [sql appendString:[self formatValue:value forAttribute:attr]]; } [sql appendString:@" WHERE "]; @@ -220,7 +262,9 @@ static NSString *uidColumnName = @"uid"; /* run SQL */ - sql = [self generateSQLForUpdate]; + sql = self->defFlags.isNew + ? [self generateSQLForInsert] + : [self generateSQLForUpdate]; if ((ex = [channel evaluateExpressionX:sql]) != nil) { [self errorWithFormat:@"could not run SQL '%@': %@", sql, ex]; [cm releaseChannel:channel]; @@ -241,6 +285,7 @@ static NSString *uidColumnName = @"uid"; } self->defFlags.modified = 0; + self->defFlags.isNew = 0; return YES; } @@ -335,6 +380,7 @@ static NSString *uidColumnName = @"uid"; [self->attributes release]; self->attributes = nil; [self->lastFetch release]; self->lastFetch = nil; self->defFlags.modified = 0; + self->defFlags.isNew = 0; } /* typed accessors */ diff --git a/SOGo/SoObjects/SOGo/ChangeLog b/SOGo/SoObjects/SOGo/ChangeLog index 2b5c2d6e..e13e503e 100644 --- a/SOGo/SoObjects/SOGo/ChangeLog +++ b/SOGo/SoObjects/SOGo/ChangeLog @@ -1,5 +1,7 @@ 2005-07-12 Helge Hess + * AgenorUserDefaults.m: added automagic profile row creation (v0.9.48) + * AgenorUserDefaults.m: implemented saving of changed profiles (v0.9.47) diff --git a/SOGo/SoObjects/SOGo/Version b/SOGo/SoObjects/SOGo/Version index cd83313c..e2cd11d2 100644 --- a/SOGo/SoObjects/SOGo/Version +++ b/SOGo/SoObjects/SOGo/Version @@ -1,6 +1,6 @@ # version file -SUBMINOR_VERSION:=47 +SUBMINOR_VERSION:=48 # v0.9.34 requires libGDLContentStore v4.5.26 # v0.9.26 requires libOGoContentStore v0.9.13 -- 2.39.5