2002-11-15 22:57:05 +00:00
|
|
|
/**
|
2006-09-15 13:01:07 +00:00
|
|
|
PostgreSQLContext.m <title>PostgreSQLContext</title>
|
2002-11-15 22:57:05 +00:00
|
|
|
|
2005-08-17 08:07:57 +00:00
|
|
|
Copyright (C) 2000-2002,2003,2004,2005 Free Software Foundation, Inc.
|
2002-11-15 22:57:05 +00:00
|
|
|
|
2006-09-14 16:06:21 +00:00
|
|
|
Author: Mirko Viviani <mirko.viviani@gmail.com>
|
2002-11-15 22:57:05 +00:00
|
|
|
Date: February 2000
|
|
|
|
|
2006-09-15 13:01:07 +00:00
|
|
|
based on the PostgreSQL adaptor written by
|
2002-11-15 22:57:05 +00:00
|
|
|
Mircea Oancea <mircea@jupiter.elcom.pub.ro>
|
|
|
|
|
|
|
|
Author: Manuel Guesdon <mguesdon@orange-concept.com>
|
|
|
|
Date: October 2000
|
|
|
|
|
|
|
|
$Revision$
|
|
|
|
$Date$
|
|
|
|
|
|
|
|
<abstract></abstract>
|
|
|
|
|
|
|
|
This file is part of the GNUstep Database Library.
|
|
|
|
|
|
|
|
<license>
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
2007-07-12 06:39:22 +00:00
|
|
|
version 3 of the License, or (at your option) any later version.
|
2002-11-15 22:57:05 +00:00
|
|
|
|
|
|
|
This library 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
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public
|
|
|
|
License along with this library; see the file COPYING.LIB.
|
|
|
|
If not, write to the Free Software Foundation,
|
2005-08-17 08:07:57 +00:00
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2002-11-15 22:57:05 +00:00
|
|
|
</license>
|
|
|
|
**/
|
|
|
|
|
2002-12-31 16:25:21 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
RCS_ID("$Id$")
|
2002-11-15 22:57:05 +00:00
|
|
|
|
2004-03-19 16:41:21 +00:00
|
|
|
#ifdef GNUSTEP
|
2003-03-31 00:24:15 +00:00
|
|
|
#include <Foundation/NSString.h>
|
|
|
|
#include <Foundation/NSArray.h>
|
|
|
|
#include <Foundation/NSValue.h>
|
|
|
|
#include <Foundation/NSException.h>
|
|
|
|
#include <Foundation/NSDebug.h>
|
|
|
|
#else
|
|
|
|
#include <Foundation/Foundation.h>
|
|
|
|
#endif
|
|
|
|
|
2003-07-11 19:04:05 +00:00
|
|
|
#ifndef GNUSTEP
|
2003-08-25 20:01:59 +00:00
|
|
|
#include <GNUstepBase/GNUstep.h>
|
2010-04-26 20:48:27 +00:00
|
|
|
#include <GNUstepBase/NSDebug+GNUstepBase.h>
|
2003-07-11 19:04:05 +00:00
|
|
|
#endif
|
|
|
|
|
2003-03-31 00:24:15 +00:00
|
|
|
#include <EOControl/EODebug.h>
|
|
|
|
|
2006-09-15 13:42:00 +00:00
|
|
|
#include "PostgreSQLAdaptor.h"
|
|
|
|
#include "PostgreSQLContext.h"
|
|
|
|
#include "PostgreSQLChannel.h"
|
|
|
|
#include "PostgreSQLExpression.h"
|
2002-11-15 22:57:05 +00:00
|
|
|
|
|
|
|
|
2006-09-15 13:01:07 +00:00
|
|
|
@implementation PostgreSQLContext
|
2002-11-15 22:57:05 +00:00
|
|
|
|
|
|
|
- (id)initWithAdaptor: (EOAdaptor *)adaptor
|
|
|
|
{
|
|
|
|
if ((self = [super initWithAdaptor: adaptor]))
|
|
|
|
{
|
2010-06-09 12:48:33 +00:00
|
|
|
// if (adaptor)
|
|
|
|
// [self setPrimaryKeySequenceNameFormat:
|
|
|
|
// [(PostgreSQLAdaptor*)adaptor primaryKeySequenceNameFormat]];
|
2002-11-15 22:57:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)beginTransaction
|
|
|
|
{
|
2006-09-15 13:01:07 +00:00
|
|
|
PostgreSQLChannel *channel = nil;
|
2002-11-15 22:57:05 +00:00
|
|
|
|
|
|
|
if ([self transactionNestingLevel])
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
format: @"%@ -- %@ 0x%x: attempted to begin a transaction within a transaction",
|
|
|
|
NSStringFromSelector(_cmd),
|
|
|
|
NSStringFromClass([self class]),
|
|
|
|
self];
|
|
|
|
|
|
|
|
if (_delegateRespondsTo.shouldBegin)
|
|
|
|
{
|
|
|
|
if (![_delegate adaptorContextShouldBegin: self])
|
2006-09-15 13:01:07 +00:00
|
|
|
[NSException raise: PostgreSQLException
|
2002-11-15 22:57:05 +00:00
|
|
|
format: @"%@ -- %@ 0x%x: delegate refuses",
|
|
|
|
NSStringFromSelector(_cmd),
|
|
|
|
NSStringFromClass([self class]),
|
|
|
|
self];
|
|
|
|
}
|
|
|
|
|
2010-05-30 18:24:14 +00:00
|
|
|
if ((!_channels) || ([_channels count] < 1)) {
|
|
|
|
[NSException raise:NSInternalInconsistencyException
|
|
|
|
format:@"%s: No open channel found. CreateAdaptorChannel first!",
|
|
|
|
__PRETTY_FUNCTION__];
|
|
|
|
}
|
2002-11-15 22:57:05 +00:00
|
|
|
channel = [[_channels objectAtIndex: 0] nonretainedObjectValue];
|
2010-05-30 18:24:14 +00:00
|
|
|
|
2002-11-15 22:57:05 +00:00
|
|
|
if ([channel isOpen] == NO)
|
2006-09-15 13:01:07 +00:00
|
|
|
[NSException raise: PostgreSQLException
|
2002-11-15 22:57:05 +00:00
|
|
|
format: @"cannot execute SQL expression. Channel is not opened."];
|
|
|
|
|
|
|
|
_flags.didBegin = YES;
|
|
|
|
|
|
|
|
[channel _evaluateExpression: [EOSQLExpression
|
|
|
|
expressionForString: @"BEGIN TRANSACTION"]
|
|
|
|
withAttributes: nil];
|
|
|
|
|
|
|
|
[self transactionDidBegin];
|
|
|
|
|
|
|
|
if (_delegateRespondsTo.didBegin)
|
|
|
|
[_delegate adaptorContextDidBegin: self];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)commitTransaction
|
|
|
|
{
|
|
|
|
//channel conn
|
|
|
|
//self transactionNestingLevel
|
|
|
|
//self transactionDidCommit
|
|
|
|
EOFLOGObjectFnStart();
|
|
|
|
|
|
|
|
NSDebugMLLog(@"gsdb", @"_flags.didBegin=%s",
|
|
|
|
(_flags.didBegin ? "YES" : "NO"));
|
|
|
|
NSDebugMLLog(@"gsdb", @"_flags.didAutoBegin=%s",
|
|
|
|
(_flags.didAutoBegin ? "YES" : "NO"));
|
|
|
|
|
|
|
|
if ([self transactionNestingLevel] == 0)
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
format: @"%@ -- %@ 0x%x:illegal attempt to commit a transaction when there are none in progress",
|
|
|
|
NSStringFromSelector(_cmd),
|
|
|
|
NSStringFromClass([self class]),
|
|
|
|
self];
|
|
|
|
|
|
|
|
if (_delegateRespondsTo.shouldCommit)
|
|
|
|
{
|
|
|
|
if (![_delegate adaptorContextShouldCommit: self])
|
2006-09-15 13:01:07 +00:00
|
|
|
[NSException raise: PostgreSQLException
|
2002-11-15 22:57:05 +00:00
|
|
|
format: @"%@ -- %@ 0x%x: delegate refuses",
|
|
|
|
NSStringFromSelector(_cmd),
|
|
|
|
NSStringFromClass([self class]),
|
|
|
|
self];
|
|
|
|
}
|
|
|
|
//???
|
|
|
|
[[[_channels objectAtIndex: 0] nonretainedObjectValue]
|
|
|
|
_evaluateExpression: [EOSQLExpression
|
|
|
|
expressionForString: @"END TRANSACTION"]
|
|
|
|
withAttributes: nil];
|
|
|
|
|
|
|
|
_flags.didBegin = NO;
|
|
|
|
|
|
|
|
[self transactionDidCommit];
|
|
|
|
|
|
|
|
if (_delegateRespondsTo.didCommit)
|
|
|
|
[_delegate adaptorContextDidCommit: self];
|
|
|
|
|
|
|
|
NSDebugMLLog(@"gsdb", @"_flags.didBegin=%s",
|
|
|
|
(_flags.didBegin ? "YES" : "NO"));
|
|
|
|
NSDebugMLLog(@"gsdb", @"_flags.didAutoBegin=%s",
|
|
|
|
(_flags.didAutoBegin ? "YES" : "NO"));
|
|
|
|
|
|
|
|
EOFLOGObjectFnStop();
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)rollbackTransaction
|
|
|
|
{
|
|
|
|
EOFLOGObjectFnStart();
|
|
|
|
|
|
|
|
NSDebugMLLog(@"gsdb", @"_flags.didBegin=%s",
|
|
|
|
(_flags.didBegin ? "YES" : "NO"));
|
|
|
|
NSDebugMLLog(@"gsdb", @"_flags.didAutoBegin=%s",
|
|
|
|
(_flags.didAutoBegin ? "YES" : "NO"));
|
|
|
|
|
|
|
|
if (![self transactionNestingLevel])
|
|
|
|
{
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
format: @"%@ -- %@ 0x%x:illegal attempt to commit a transaction when there are none in progress",
|
|
|
|
NSStringFromSelector(_cmd),
|
|
|
|
NSStringFromClass([self class]),
|
|
|
|
self];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_delegateRespondsTo.shouldRollback)
|
|
|
|
{
|
|
|
|
if (![_delegate adaptorContextShouldRollback: self])
|
2006-09-15 13:01:07 +00:00
|
|
|
[NSException raise: PostgreSQLException
|
2002-11-15 22:57:05 +00:00
|
|
|
format: @"%@ -- %@ 0x%x: delegate refuses",
|
|
|
|
NSStringFromSelector(_cmd),
|
|
|
|
NSStringFromClass([self class]),
|
|
|
|
self];
|
|
|
|
}
|
|
|
|
|
|
|
|
[[[_channels objectAtIndex: 0] nonretainedObjectValue]
|
|
|
|
_evaluateExpression: [EOSQLExpression
|
|
|
|
expressionForString: @"ABORT TRANSACTION"]
|
|
|
|
withAttributes: nil];
|
|
|
|
|
|
|
|
_flags.didBegin = NO;
|
|
|
|
|
|
|
|
[self transactionDidRollback];
|
|
|
|
|
|
|
|
if (_delegateRespondsTo.didRollback)
|
|
|
|
[_delegate adaptorContextDidRollback: self];
|
|
|
|
|
|
|
|
NSDebugMLLog(@"gsdb", @"_flags.didBegin=%s",
|
|
|
|
(_flags.didBegin ? "YES" : "NO"));
|
|
|
|
NSDebugMLLog(@"gsdb", @"_flags.didAutoBegin=%s",
|
|
|
|
(_flags.didAutoBegin ? "YES" : "NO"));
|
|
|
|
|
|
|
|
EOFLOGObjectFnStop();
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)canNestTransactions
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (EOAdaptorChannel *)createAdaptorChannel
|
|
|
|
{
|
|
|
|
//OK
|
|
|
|
EOAdaptorChannel *adaptorChannel;
|
|
|
|
|
2006-09-15 13:01:07 +00:00
|
|
|
adaptorChannel = [PostgreSQLChannel adaptorChannelWithAdaptorContext: self];
|
2002-11-15 22:57:05 +00:00
|
|
|
|
|
|
|
return adaptorChannel;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)autoBeginTransaction: (BOOL)force
|
|
|
|
{
|
|
|
|
//seems OK
|
|
|
|
BOOL ok = NO;
|
|
|
|
|
|
|
|
EOFLOGObjectFnStart();
|
|
|
|
|
|
|
|
NSDebugMLLog(@"gsdb", @"force=%d _flags.didBegin=%s [self transactionNestingLevel]=%d",
|
|
|
|
force,
|
|
|
|
(_flags.didBegin ? "YES" : "NO"),
|
|
|
|
[self transactionNestingLevel]);
|
|
|
|
|
|
|
|
if (!_flags.didBegin && [self transactionNestingLevel] == 0)
|
|
|
|
{
|
|
|
|
if (force == YES)
|
|
|
|
[self beginTransaction];
|
|
|
|
|
|
|
|
_flags.didAutoBegin = YES;
|
|
|
|
_flags.forceTransaction = force;
|
|
|
|
|
|
|
|
ok = YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSDebugMLLog(@"gsdb", @"_flags.didBegin=%s",
|
|
|
|
(_flags.didBegin ? "YES" : "NO"));
|
|
|
|
NSDebugMLLog(@"gsdb", @"_flags.didAutoBegin=%s",
|
|
|
|
(_flags.didAutoBegin ? "YES" : "NO"));
|
|
|
|
|
|
|
|
EOFLOGObjectFnStop();
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)autoCommitTransaction
|
|
|
|
{
|
|
|
|
//seems ok
|
|
|
|
BOOL ok = NO;
|
|
|
|
|
|
|
|
EOFLOGObjectFnStart();
|
|
|
|
|
|
|
|
NSDebugMLLog(@"gsdb", @"_flags.didBegin=%s",
|
|
|
|
(_flags.didBegin ? "YES" : "NO"));
|
|
|
|
NSDebugMLLog(@"gsdb", @"_flags.didAutoBegin=%s",
|
|
|
|
(_flags.didAutoBegin ? "YES" : "NO"));
|
|
|
|
|
|
|
|
if (_flags.didAutoBegin)
|
|
|
|
{
|
|
|
|
NSDebugMLLog(@"gsdb", @"_flags.forceTransaction=%s",
|
|
|
|
(_flags.forceTransaction ? "YES" : "NO"));
|
|
|
|
|
|
|
|
if (_flags.forceTransaction == YES)
|
|
|
|
{
|
|
|
|
[self commitTransaction];
|
|
|
|
}
|
|
|
|
|
|
|
|
_flags.didAutoBegin = NO;
|
|
|
|
_flags.forceTransaction = NO;
|
|
|
|
|
|
|
|
ok = YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSDebugMLLog(@"gsdb", @"_flags.didBegin=%s",
|
|
|
|
(_flags.didBegin ? "YES" : "NO"));
|
|
|
|
NSDebugMLLog(@"gsdb", @"_flags.didAutoBegin=%s",
|
|
|
|
(_flags.didAutoBegin ? "YES" : "NO"));
|
|
|
|
|
|
|
|
EOFLOGObjectFnStop();
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** format is something like @"%@_SEQ" or @"EOSEQ_%@", "%@" is replaced by external table name **/
|
2010-06-09 12:48:33 +00:00
|
|
|
//- (void)setPrimaryKeySequenceNameFormat: (NSString*)format
|
|
|
|
//{
|
|
|
|
// ASSIGN(_primaryKeySequenceNameFormat, format);
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//- (NSString*)primaryKeySequenceNameFormat
|
|
|
|
//{
|
|
|
|
// return _primaryKeySequenceNameFormat;
|
|
|
|
//}
|
2002-11-15 22:57:05 +00:00
|
|
|
|
2006-09-15 13:01:07 +00:00
|
|
|
@end /* PostgreSQLContext */
|
2002-11-15 22:57:05 +00:00
|
|
|
/*
|
|
|
|
//TODO
|
|
|
|
autoCommitTransaction
|
|
|
|
{
|
|
|
|
self commitTransaction
|
|
|
|
};
|
|
|
|
*/
|