diff --git a/ChangeLog b/ChangeLog index 36869115b..d215a538c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,10 @@ 2012-08-13 Wolfgang Sourdeau + * OpenChange/SOGoMAPIDBObject.m (-performBatchSQLQueries:) new + method to perform void queries under a transaction. + (-changePathTo:) new method that updates the references for the + object record in the dbfs table. + * SoObjects/SOGo/NSString+Utilities.m (-stringByReplacingPrefix:withPrefix:): new self-explicit method. diff --git a/OpenChange/SOGoMAPIDBObject.h b/OpenChange/SOGoMAPIDBObject.h index ea0a7a58a..6bf1750bb 100644 --- a/OpenChange/SOGoMAPIDBObject.h +++ b/OpenChange/SOGoMAPIDBObject.h @@ -73,10 +73,12 @@ typedef enum { /* automatically set from actions */ - (BOOL) deleted; +- (void) changePathTo: (NSString *) newPath; + /* db helpers */ - (EOAdaptor *) tableChannelAdaptor; - (NSArray *) performSQLQuery: (NSString *) sql; - +- (BOOL) performBatchSQLQueries: (NSArray *) queries; @end diff --git a/OpenChange/SOGoMAPIDBObject.m b/OpenChange/SOGoMAPIDBObject.m index e085440a3..deb760383 100644 --- a/OpenChange/SOGoMAPIDBObject.m +++ b/OpenChange/SOGoMAPIDBObject.m @@ -260,6 +260,33 @@ static EOAttribute *textColumn = nil; } /* actions */ +- (void) changePathTo: (NSString *) newPath +{ + NSMutableString *sql; + NSString *oldPath, *newParentPath; + NSRange slashRange; + + oldPath = [self path]; + + slashRange = [newPath rangeOfString: @"/" + options: NSBackwardsSearch]; + if (slashRange.location != NSNotFound) + newParentPath = [newPath substringToIndex: slashRange.location]; + else + newParentPath = NULL; + + sql = [NSMutableString stringWithFormat: @"UPDATE %@" + @" SET c_path = '%@'", + [self tableName], + newPath]; + if (newParentPath) + [sql appendFormat: @", c_parent_path = '%@'", newParentPath]; + else + [sql appendString: @", c_parent_path = NULL"]; + [sql appendFormat: @" WHERE c_path = '%@'", oldPath]; + [self performBatchSQLQueries: [NSArray arrayWithObject: sql]]; +} + - (EOAdaptor *) tableChannelAdaptor { GCSChannelManager *cm; @@ -307,6 +334,38 @@ static EOAttribute *textColumn = nil; return records; } +- (BOOL) performBatchSQLQueries: (NSArray *) queries +{ + GCSChannelManager *cm; + EOAdaptorChannel *channel; + EOAdaptorContext *dbContext; + NSException *error; + NSUInteger count, max; + NSString *sql; + + cm = [GCSChannelManager defaultChannelManager]; + channel = [cm acquireOpenChannelForURL: [self tableUrl]]; + dbContext = [channel adaptorContext]; + + [dbContext beginTransaction]; + + error = nil; + + max = [queries count]; + for (count = 0; error == nil && count < max; count++) + { + sql = [queries objectAtIndex: count]; + error = [channel evaluateExpressionX: sql]; + if (error) + [dbContext rollbackTransaction]; + } + if (!error) + [dbContext commitTransaction]; + [cm releaseChannel: channel]; + + return (error == nil); +} + - (NSDictionary *) lookupRecord: (NSString *) path newerThanVersion: (NSInteger) startVersion {