/*
- Copyright (C) 2004 SKYRIX Software AG
+ Copyright (C) 2004-2005 SKYRIX Software AG
This file is part of OpenGroupware.org.
Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
*/
-// $Id$
#include "OCSFolder.h"
#include "OCSFolderManager.h"
location:(NSURL *)_loc quickLocation:(NSURL *)_qloc
folderManager:(OCSFolderManager *)_fm
{
+ if (![_loc isNotNull]) {
+ [self logWithFormat:@"ERROR(%s): missing quicktable parameter!",
+ __PRETTY_FUNCTION__];
+ [self release];
+ return nil;
+ }
+
if ((self = [super init])) {
self->folderManager = [_fm retain];
self->folderInfo = [_ftype retain];
self->folderName = [[_path lastPathComponent] copy];
self->path = [_path copy];
self->location = [_loc retain];
- self->quickLocation = [_qloc retain];
+ self->quickLocation = _qloc ? [_qloc retain] : [_loc retain];
self->folderTypeName = [_ftname copy];
+
+ self->ofFlags.requiresFolderSelect = 0;
+ self->ofFlags.sameTableForQuick =
+ [self->location isEqualTo:self->quickLocation] ? 1 : 0;
}
return self;
}
return [[self quickLocation] ocsTableName];
}
+- (BOOL)isQuickInfoStoredInContentTable {
+ return self->ofFlags.sameTableForQuick ? YES : NO;
+}
+
/* channels */
- (EOAdaptorChannel *)acquireStoreChannel {
/* make content row */
contentRow = [NSMutableDictionary dictionaryWithCapacity:16];
+
+ if (self->ofFlags.sameTableForQuick)
+ [contentRow addEntriesFromDictionary:quickRow];
+
[contentRow setObject:_name forKey:@"c_name"];
if (isNewRecord) [contentRow setObject:now forKey:@"c_creationdate"];
[contentRow setObject:now forKey:@"c_lastmodified"];
if (isNewRecord)
[contentRow setObject:[NSNumber numberWithInt:0] forKey:@"c_version"];
else {
+ // TODO: increase version?
[contentRow setObject:[NSNumber numberWithInt:[storedVersion intValue]]
forKey:@"c_version"];
}
[self logWithFormat:@"ERROR(%s): could not open storage channel!"];
return nil;
}
- if ((quickChannel = [self acquireQuickChannel]) == nil) {
- [self logWithFormat:@"ERROR(%s): could not open quick channel!"];
- [self releaseChannel:storeChannel];
- return nil;
+ if (!self->ofFlags.sameTableForQuick) {
+ if ((quickChannel = [self acquireQuickChannel]) == nil) {
+ [self logWithFormat:@"ERROR(%s): could not open quick channel!"];
+ [self releaseChannel:storeChannel];
+ return nil;
+ }
}
+
+ /* generate SQL */
- // TODO: gen SQL, execute in transactions
+ qsql = nil;
if (isNewRecord) { /* insert */
- qsql = [self _generateInsertStatementForRow:quickRow
- tableName:[self quickTableName]];
- bsql = [self _generateInsertStatementForRow:contentRow
- tableName:[self storeTableName]];
-
- if ((error = [storeChannel evaluateExpressionX:bsql]) != nil) {
- [self logWithFormat:@"ERROR(%s): cannot insert content '%@': %@",
- __PRETTY_FUNCTION__, bsql, error];
- }
- else if ((error = [quickChannel evaluateExpressionX:qsql]) != nil) {
- NSString *delsql;
- NSException *delErr;
-
- [self logWithFormat:@"ERROR(%s): cannot insert quick '%@': %@",
- __PRETTY_FUNCTION__, qsql, error];
-
- delsql = [@"DELETE FROM " stringByAppendingString:[self storeTableName]];
- delsql = [delsql stringByAppendingString:@" WHERE c_name="];
- delsql = [delsql stringByAppendingString:[self _formatRowValue:_name]];
- if ((delErr = [storeChannel evaluateExpressionX:delsql]) != nil) {
- [self logWithFormat:
- @"ERROR(%s): cannot delete content '%@' after quick-fail: %@",
- __PRETTY_FUNCTION__, delsql, error];
- }
+ if (!self->ofFlags.sameTableForQuick) {
+ qsql = [self _generateInsertStatementForRow:quickRow
+ tableName:[self quickTableName]];
}
+ bsql = [self _generateInsertStatementForRow:contentRow
+ tableName:[self storeTableName]];
}
- else { /* update */
- qsql = [self _generateUpdateStatementForRow:quickRow
- tableName:[self quickTableName]
- whereColumn:@"c_name" isEqualTo:_name];
+ else {
+ if (!self->ofFlags.sameTableForQuick) {
+ qsql = [self _generateUpdateStatementForRow:quickRow
+ tableName:[self quickTableName]
+ whereColumn:@"c_name" isEqualTo:_name];
+ }
bsql = [self _generateUpdateStatementForRow:contentRow
tableName:[self storeTableName]
whereColumn:@"c_name" isEqualTo:_name];
- if ((error = [storeChannel evaluateExpressionX:bsql]) != nil) {
- [self logWithFormat:@"ERROR(%s): cannot update content '%@': %@",
- __PRETTY_FUNCTION__, bsql, error];
- }
- else if ((error = [quickChannel evaluateExpressionX:qsql]) != nil) {
- [self logWithFormat:@"ERROR(%s): cannot update quick '%@': %@",
- __PRETTY_FUNCTION__, qsql, error];
+ }
+
+ /* execute */
+ // TODO: execute in transactions
+
+ if ((error = [storeChannel evaluateExpressionX:bsql]) != nil) {
+ [self logWithFormat:@"ERROR(%s): cannot %s content '%@': %@",
+ __PRETTY_FUNCTION__, isNewRecord ? "insert" : "update", bsql, error];
+ }
+
+ if (error == nil && qsql != nil) {
+ if ((error = [quickChannel evaluateExpressionX:qsql]) != nil) {
+ NSString *delsql;
+ NSException *delErr;
+
+ [self logWithFormat:@"ERROR(%s): cannot %s quick '%@': %@",
+ __PRETTY_FUNCTION__, isNewRecord ? "insert" : "update",
+ qsql, error];
+
+ if (isNewRecord) {
+ /* insert in quick failed, so delete in content table */
+
+ delsql = [@"DELETE FROM " stringByAppendingString:
+ [self storeTableName]];
+ delsql = [delsql stringByAppendingString:@" WHERE c_name="];
+ delsql = [delsql stringByAppendingString:[self _formatRowValue:_name]];
+ if ((delErr = [storeChannel evaluateExpressionX:delsql]) != nil) {
+ [self logWithFormat:
+ @"ERROR(%s): could not delete content '%@' after quick-fail:"
+ @" %@", __PRETTY_FUNCTION__, delsql, error];
+ }
+ }
}
}
[self releaseChannel:storeChannel];
- [self releaseChannel:quickChannel];
+ if (!self->ofFlags.sameTableForQuick) [self releaseChannel:quickChannel];
return error;
}
[self logWithFormat:@"ERROR(%s): could not open storage channel!"];
return nil;
}
- if ((quickChannel = [self acquireQuickChannel]) == nil) {
- [self logWithFormat:@"ERROR(%s): could not open quick channel!"];
- [self releaseChannel:storeChannel];
- return nil;
+ if (!self->ofFlags.sameTableForQuick) {
+ if ((quickChannel = [self acquireQuickChannel]) == nil) {
+ [self logWithFormat:@"ERROR(%s): could not open quick channel!"];
+ [self releaseChannel:storeChannel];
+ return nil;
+ }
}
-
+
/* delete rows */
delsql = [@"DELETE FROM " stringByAppendingString:[self storeTableName]];
@"ERROR(%s): cannot delete content '%@': %@",
__PRETTY_FUNCTION__, delsql, error];
}
- else {
+ else if (!self->ofFlags.sameTableForQuick) {
/* content row deleted, now delete the quick row */
delsql = [@"DELETE FROM " stringByAppendingString:[self quickTableName]];
delsql = [delsql stringByAppendingString:@" WHERE c_name="];
/* release channels and return */
[self releaseChannel:storeChannel];
- [self releaseChannel:quickChannel];
+ if (!self->ofFlags.sameTableForQuick)
+ [self releaseChannel:quickChannel];
return error;
}