From 57afca1c7474785281df80d672eb2b77b011faee Mon Sep 17 00:00:00 2001 From: Wolfgang Sourdeau Date: Mon, 13 Aug 2012 17:48:18 +0000 Subject: [PATCH] Monotone-Parent: 54ac2dbf42dee286f64da8b9b3c4567570b00258 Monotone-Revision: dbc58efbc938f11a813baa01021a534ed90f70b8 Monotone-Author: wsourdeau@inverse.ca Monotone-Date: 2012-08-13T17:48:18 Monotone-Branch: ca.inverse.sogo --- ChangeLog | 5 +++ OpenChange/SOGoMAPIDBObject.h | 4 ++- OpenChange/SOGoMAPIDBObject.m | 59 +++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) 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 {