2004-04-26 15:13:27 +00:00
|
|
|
|
/* -*-objc-*- */
|
|
|
|
|
|
|
|
|
|
/** Implementation of SQLClient for GNUStep
|
|
|
|
|
Copyright (C) 2004 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
|
|
Written by: Richard Frith-Macdonald <rfm@gnu.org>
|
|
|
|
|
Date: April 2004
|
|
|
|
|
|
|
|
|
|
This file is part of the SQLClient Library.
|
|
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
2007-09-14 13:02:05 +00:00
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
2004-04-26 15:13:27 +00:00
|
|
|
|
License as published by the Free Software Foundation; either
|
2007-09-14 13:02:05 +00:00
|
|
|
|
version 3 of the License, or (at your option) any later version.
|
2004-04-26 15:13:27 +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
|
2007-09-14 13:02:05 +00:00
|
|
|
|
Lesser General Public License for more details.
|
2004-04-26 15:13:27 +00:00
|
|
|
|
|
2007-09-14 13:02:05 +00:00
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2004-04-26 15:13:27 +00:00
|
|
|
|
License along with this library; if not, write to the Free
|
|
|
|
|
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
|
|
|
|
|
|
|
|
|
|
$Date$ $Revision$
|
|
|
|
|
*/
|
|
|
|
|
|
2007-04-01 08:03:21 +00:00
|
|
|
|
#import <Foundation/NSArray.h>
|
|
|
|
|
#import <Foundation/NSAutoreleasePool.h>
|
|
|
|
|
#import <Foundation/NSBundle.h>
|
|
|
|
|
#import <Foundation/NSCalendarDate.h>
|
|
|
|
|
#import <Foundation/NSCharacterSet.h>
|
|
|
|
|
#import <Foundation/NSData.h>
|
|
|
|
|
#import <Foundation/NSDate.h>
|
|
|
|
|
#import <Foundation/NSDebug.h>
|
|
|
|
|
#import <Foundation/NSDictionary.h>
|
|
|
|
|
#import <Foundation/NSEnumerator.h>
|
|
|
|
|
#import <Foundation/NSException.h>
|
|
|
|
|
#import <Foundation/NSKeyValueCoding.h>
|
|
|
|
|
#import <Foundation/NSLock.h>
|
2014-06-19 21:26:25 +00:00
|
|
|
|
#import <Foundation/NSHashTable.h>
|
2007-04-01 08:03:21 +00:00
|
|
|
|
#import <Foundation/NSMapTable.h>
|
|
|
|
|
#import <Foundation/NSNotification.h>
|
|
|
|
|
#import <Foundation/NSNull.h>
|
|
|
|
|
#import <Foundation/NSPathUtilities.h>
|
|
|
|
|
#import <Foundation/NSProcessInfo.h>
|
2008-02-21 16:23:23 +00:00
|
|
|
|
#import <Foundation/NSRunLoop.h>
|
2007-04-01 08:03:21 +00:00
|
|
|
|
#import <Foundation/NSSet.h>
|
|
|
|
|
#import <Foundation/NSString.h>
|
2008-02-21 16:23:23 +00:00
|
|
|
|
#import <Foundation/NSThread.h>
|
2007-04-01 08:03:21 +00:00
|
|
|
|
#import <Foundation/NSTimer.h>
|
|
|
|
|
#import <Foundation/NSUserDefaults.h>
|
|
|
|
|
#import <Foundation/NSValue.h>
|
|
|
|
|
|
|
|
|
|
#import <Performance/GSCache.h>
|
|
|
|
|
#import <Performance/GSTicker.h>
|
2004-04-26 15:13:27 +00:00
|
|
|
|
|
2009-11-18 11:11:29 +00:00
|
|
|
|
#define SQLCLIENT_PRIVATE @public
|
|
|
|
|
|
|
|
|
|
#include <memory.h>
|
|
|
|
|
|
2004-04-26 15:13:27 +00:00
|
|
|
|
#include "SQLClient.h"
|
|
|
|
|
|
2007-04-01 08:03:21 +00:00
|
|
|
|
#if defined(GNUSTEP_BASE_LIBRARY)
|
|
|
|
|
#define SUBCLASS_RESPONSIBILITY [self subclassResponsibility: _cmd];
|
|
|
|
|
#else
|
|
|
|
|
#define SUBCLASS_RESPONSIBILITY
|
|
|
|
|
#endif
|
|
|
|
|
|
2007-08-01 15:40:12 +00:00
|
|
|
|
NSString * const SQLClientDidConnectNotification
|
2007-07-09 17:08:22 +00:00
|
|
|
|
= @"SQLClientDidConnectNotification";
|
|
|
|
|
|
2007-08-01 15:40:12 +00:00
|
|
|
|
NSString * const SQLClientDidDisconnectNotification
|
2007-07-09 17:08:22 +00:00
|
|
|
|
= @"SQLClientDidDisconnectNotification";
|
|
|
|
|
|
2015-04-28 11:47:23 +00:00
|
|
|
|
static unsigned int classDebugging = 0;
|
|
|
|
|
static NSTimeInterval classDuration = -1;
|
|
|
|
|
|
2006-08-26 12:49:59 +00:00
|
|
|
|
static NSNull *null = nil;
|
2008-02-21 16:23:23 +00:00
|
|
|
|
static NSArray *queryModes = nil;
|
|
|
|
|
static NSThread *mainThread = nil;
|
2014-06-20 05:15:24 +00:00
|
|
|
|
static Class NSStringClass = Nil;
|
|
|
|
|
static Class NSArrayClass = Nil;
|
|
|
|
|
static Class NSDateClass = Nil;
|
|
|
|
|
static Class NSSetClass = Nil;
|
|
|
|
|
static Class SQLClientClass = Nil;
|
2006-08-26 12:49:59 +00:00
|
|
|
|
|
2015-05-28 12:57:08 +00:00
|
|
|
|
@interface SQLClientPool (Swallow)
|
|
|
|
|
- (BOOL) _swallowClient: (SQLClient*)client withRetain: (BOOL)shouldRetain;
|
|
|
|
|
@end
|
|
|
|
|
|
2015-04-28 11:47:23 +00:00
|
|
|
|
@implementation SQLRecordKeys
|
|
|
|
|
|
|
|
|
|
- (NSUInteger) count
|
|
|
|
|
{
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
|
{
|
|
|
|
|
if (nil != order) [order release];
|
|
|
|
|
if (nil != map) [map release];
|
|
|
|
|
if (nil != low) [low release];
|
|
|
|
|
[super dealloc];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSUInteger) indexForKey: (NSString*)key
|
|
|
|
|
{
|
|
|
|
|
NSUInteger c;
|
|
|
|
|
|
|
|
|
|
c = (NSUInteger)NSMapGet(map, key);
|
|
|
|
|
if (c > 0)
|
|
|
|
|
{
|
|
|
|
|
return c - 1;
|
|
|
|
|
}
|
|
|
|
|
key = [key lowercaseString];
|
|
|
|
|
c = (NSUInteger)NSMapGet(low, key);
|
|
|
|
|
if (c > 0)
|
|
|
|
|
{
|
|
|
|
|
if (classDebugging > 0)
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"[SQLRecordKeys-indexForKey:] lowercase '%@'", key);
|
|
|
|
|
}
|
|
|
|
|
return c - 1;
|
|
|
|
|
}
|
|
|
|
|
return NSNotFound;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithKeys: (NSString**)keys count: (NSUInteger)c
|
|
|
|
|
{
|
|
|
|
|
if (nil != (self = [super init]))
|
|
|
|
|
{
|
|
|
|
|
count = c;
|
|
|
|
|
order = [[NSArray alloc] initWithObjects: keys count: c];
|
|
|
|
|
map = NSCreateMapTable(NSObjectMapKeyCallBacks,
|
|
|
|
|
NSIntegerMapValueCallBacks, count);
|
|
|
|
|
low = NSCreateMapTable(NSObjectMapKeyCallBacks,
|
|
|
|
|
NSIntegerMapValueCallBacks, count);
|
|
|
|
|
for (c = 1; c <= count; c++)
|
|
|
|
|
{
|
|
|
|
|
NSString *k = keys[c-1];
|
|
|
|
|
|
|
|
|
|
NSMapInsert(map, (void*)k, (void*)c);
|
|
|
|
|
k = [k lowercaseString];
|
|
|
|
|
NSMapInsert(low, (void*)k, (void*)c);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSArray*) order
|
|
|
|
|
{
|
|
|
|
|
return order;
|
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
2009-11-18 11:11:29 +00:00
|
|
|
|
@interface _ConcreteSQLRecord : SQLRecord
|
|
|
|
|
{
|
2015-04-28 11:47:23 +00:00
|
|
|
|
SQLRecordKeys *keys;
|
|
|
|
|
NSUInteger count; // Must be last
|
2009-11-18 11:11:29 +00:00
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
2008-02-21 16:23:23 +00:00
|
|
|
|
@interface CacheQuery : NSObject
|
|
|
|
|
{
|
|
|
|
|
@public
|
|
|
|
|
NSString *query;
|
2008-03-03 14:10:54 +00:00
|
|
|
|
id recordType;
|
|
|
|
|
id listType;
|
2008-02-21 16:23:23 +00:00
|
|
|
|
unsigned lifetime;
|
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation CacheQuery
|
|
|
|
|
- (void) dealloc
|
|
|
|
|
{
|
2009-11-18 11:11:29 +00:00
|
|
|
|
[query release];
|
2008-02-21 16:23:23 +00:00
|
|
|
|
[super dealloc];
|
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
2008-03-03 14:10:54 +00:00
|
|
|
|
static Class aClass = 0;
|
2007-03-08 17:12:55 +00:00
|
|
|
|
static Class rClass = 0;
|
|
|
|
|
|
2004-04-26 15:13:27 +00:00
|
|
|
|
@implementation SQLRecord
|
|
|
|
|
+ (id) allocWithZone: (NSZone*)aZone
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"Illegal attempt to allocate an SQLRecord");
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
2004-08-22 09:34:18 +00:00
|
|
|
|
+ (void) initialize
|
|
|
|
|
{
|
2005-11-14 20:37:33 +00:00
|
|
|
|
GSTickerTimeNow();
|
2004-08-22 09:34:18 +00:00
|
|
|
|
if (null == nil)
|
|
|
|
|
{
|
|
|
|
|
null = [NSNull new];
|
2008-03-03 14:10:54 +00:00
|
|
|
|
aClass = [NSMutableArray class];
|
2007-03-15 12:38:44 +00:00
|
|
|
|
rClass = [_ConcreteSQLRecord class];
|
2004-08-22 09:34:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-08 17:12:55 +00:00
|
|
|
|
+ (id) newWithValues: (id*)v keys: (NSString**)k count: (unsigned int)c
|
|
|
|
|
{
|
|
|
|
|
return [rClass newWithValues: v keys: k count: c];
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-28 11:47:23 +00:00
|
|
|
|
+ (id) newWithValues: (id*)v keys: (SQLRecordKeys*)k
|
|
|
|
|
{
|
|
|
|
|
return [rClass newWithValues: v keys: k];
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-08 17:12:55 +00:00
|
|
|
|
- (NSArray*) allKeys
|
|
|
|
|
{
|
2015-01-06 10:56:39 +00:00
|
|
|
|
NSUInteger count = [self count];
|
2007-03-08 17:12:55 +00:00
|
|
|
|
|
2015-01-06 10:56:39 +00:00
|
|
|
|
if (count > 0)
|
2007-03-08 17:12:55 +00:00
|
|
|
|
{
|
2015-01-06 10:56:39 +00:00
|
|
|
|
id buf[count];
|
|
|
|
|
|
|
|
|
|
while (count-- > 0)
|
|
|
|
|
{
|
|
|
|
|
buf[count] = [self keyAtIndex: count];
|
|
|
|
|
}
|
|
|
|
|
return [NSArray arrayWithObjects: buf count: count];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return [NSArray array];
|
2007-03-08 17:12:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) copyWithZone: (NSZone*)z
|
|
|
|
|
{
|
2009-11-18 11:11:29 +00:00
|
|
|
|
return [self retain];
|
2007-03-08 17:12:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-01-06 16:03:51 +00:00
|
|
|
|
- (NSUInteger) count
|
2007-03-08 17:12:55 +00:00
|
|
|
|
{
|
2007-04-01 08:03:21 +00:00
|
|
|
|
SUBCLASS_RESPONSIBILITY
|
2007-03-08 17:12:55 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSMutableDictionary*) dictionary
|
|
|
|
|
{
|
2015-01-06 10:56:39 +00:00
|
|
|
|
NSUInteger count = [self count];
|
2007-03-08 17:12:55 +00:00
|
|
|
|
|
2015-01-06 10:56:39 +00:00
|
|
|
|
if (count > 0)
|
|
|
|
|
{
|
|
|
|
|
id keys[count];
|
|
|
|
|
id vals[count];
|
|
|
|
|
|
|
|
|
|
[self getKeys: keys];
|
|
|
|
|
[self getObjects: vals];
|
|
|
|
|
return [NSMutableDictionary dictionaryWithObjects: vals
|
|
|
|
|
forKeys: keys
|
|
|
|
|
count: count];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return [NSMutableDictionary dictionary];
|
|
|
|
|
}
|
2007-03-08 17:12:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) getKeys: (id*)buf
|
|
|
|
|
{
|
2015-04-28 11:47:23 +00:00
|
|
|
|
NSUInteger i = [self count];
|
2007-03-08 17:12:55 +00:00
|
|
|
|
|
|
|
|
|
while (i-- > 0)
|
|
|
|
|
{
|
|
|
|
|
buf[i] = [self keyAtIndex: i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) getObjects: (id*)buf
|
|
|
|
|
{
|
2015-04-28 11:47:23 +00:00
|
|
|
|
NSUInteger i = [self count];
|
2007-03-08 17:12:55 +00:00
|
|
|
|
|
|
|
|
|
while (i-- > 0)
|
|
|
|
|
{
|
|
|
|
|
buf[i] = [self objectAtIndex: i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) init
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"Illegal attempt to -init an SQLRecord");
|
2009-11-18 11:11:29 +00:00
|
|
|
|
[self release];
|
|
|
|
|
return nil;
|
2007-03-08 17:12:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-01-06 16:03:51 +00:00
|
|
|
|
- (NSString*) keyAtIndex: (NSUInteger)index
|
2007-03-08 17:12:55 +00:00
|
|
|
|
{
|
2007-04-01 08:03:21 +00:00
|
|
|
|
SUBCLASS_RESPONSIBILITY
|
|
|
|
|
return nil;
|
2007-03-08 17:12:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-28 11:47:23 +00:00
|
|
|
|
- (SQLRecordKeys*) keys
|
|
|
|
|
{
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-06 15:39:11 +00:00
|
|
|
|
- (id) objectAtIndex: (NSUInteger)index
|
2007-03-08 17:12:55 +00:00
|
|
|
|
{
|
2007-04-01 08:03:21 +00:00
|
|
|
|
SUBCLASS_RESPONSIBILITY
|
|
|
|
|
return nil;
|
2007-03-08 17:12:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) objectForKey: (NSString*)key
|
|
|
|
|
{
|
2015-01-06 10:56:39 +00:00
|
|
|
|
NSUInteger count = [self count];
|
2007-03-08 17:12:55 +00:00
|
|
|
|
|
2015-01-06 10:56:39 +00:00
|
|
|
|
if (count > 0)
|
2007-03-08 17:12:55 +00:00
|
|
|
|
{
|
2015-01-06 10:56:39 +00:00
|
|
|
|
NSUInteger pos;
|
|
|
|
|
id keys[count];
|
|
|
|
|
|
|
|
|
|
[self getKeys: keys];
|
2007-03-08 17:12:55 +00:00
|
|
|
|
for (pos = 0; pos < count; pos++)
|
2015-01-06 10:56:39 +00:00
|
|
|
|
{
|
|
|
|
|
if ([key isEqualToString: keys[pos]] == YES)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (pos == count)
|
|
|
|
|
{
|
|
|
|
|
for (pos = 0; pos < count; pos++)
|
|
|
|
|
{
|
|
|
|
|
if ([key caseInsensitiveCompare: keys[pos]] == NSOrderedSame)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-03-08 17:12:55 +00:00
|
|
|
|
|
2015-01-06 10:56:39 +00:00
|
|
|
|
if (pos != count)
|
|
|
|
|
{
|
|
|
|
|
return [self objectAtIndex: pos];
|
|
|
|
|
}
|
2007-03-08 17:12:55 +00:00
|
|
|
|
}
|
2015-01-06 10:56:39 +00:00
|
|
|
|
return nil;
|
2007-03-08 17:12:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-01-06 15:39:11 +00:00
|
|
|
|
- (void) replaceObjectAtIndex: (NSUInteger)index withObject: (id)anObject
|
2007-03-08 17:12:55 +00:00
|
|
|
|
{
|
2007-04-01 08:03:21 +00:00
|
|
|
|
SUBCLASS_RESPONSIBILITY
|
2007-03-08 17:12:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) setObject: (id)anObject forKey: (NSString*)aKey
|
|
|
|
|
{
|
2015-01-06 10:56:39 +00:00
|
|
|
|
NSUInteger count = [self count];
|
2007-03-08 17:12:55 +00:00
|
|
|
|
|
2015-01-06 10:56:39 +00:00
|
|
|
|
if (count > 0)
|
2007-03-08 17:12:55 +00:00
|
|
|
|
{
|
2015-01-06 10:56:39 +00:00
|
|
|
|
NSUInteger pos;
|
|
|
|
|
id keys[count];
|
|
|
|
|
|
|
|
|
|
if (anObject == nil)
|
2007-03-08 17:12:55 +00:00
|
|
|
|
{
|
2015-01-06 10:56:39 +00:00
|
|
|
|
anObject = null;
|
|
|
|
|
}
|
|
|
|
|
[self getKeys: keys];
|
2007-03-08 17:12:55 +00:00
|
|
|
|
for (pos = 0; pos < count; pos++)
|
2015-01-06 10:56:39 +00:00
|
|
|
|
{
|
|
|
|
|
if ([aKey isEqualToString: keys[pos]] == YES)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (pos == count)
|
|
|
|
|
{
|
|
|
|
|
for (pos = 0; pos < count; pos++)
|
|
|
|
|
{
|
|
|
|
|
if ([aKey caseInsensitiveCompare: keys[pos]] == NSOrderedSame)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-03-08 17:12:55 +00:00
|
|
|
|
|
2015-01-06 10:56:39 +00:00
|
|
|
|
if (pos == count)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"Bad key (%@) in -setObject:forKey:", aKey];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
[self replaceObjectAtIndex: pos withObject: anObject];
|
|
|
|
|
}
|
2007-03-08 17:12:55 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2015-01-06 10:56:39 +00:00
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"Bad key (%@) in -setObject:forKey:", aKey];
|
2007-03-08 17:12:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-05 12:48:53 +00:00
|
|
|
|
- (NSUInteger) sizeInBytes: (NSMutableSet*)exclude
|
2007-03-08 17:12:55 +00:00
|
|
|
|
{
|
2013-09-05 12:48:53 +00:00
|
|
|
|
NSUInteger size = [super sizeInBytes: exclude];
|
2015-01-06 10:56:39 +00:00
|
|
|
|
NSUInteger count = [self count];
|
2007-04-01 07:03:43 +00:00
|
|
|
|
|
2015-01-06 10:56:39 +00:00
|
|
|
|
if (size > 0 && count > 0)
|
2007-03-08 17:12:55 +00:00
|
|
|
|
{
|
2013-09-05 12:48:53 +00:00
|
|
|
|
NSUInteger pos;
|
|
|
|
|
id vals[count];
|
2007-03-08 17:12:55 +00:00
|
|
|
|
|
|
|
|
|
[self getObjects: vals];
|
|
|
|
|
for (pos = 0; pos < count; pos++)
|
|
|
|
|
{
|
|
|
|
|
size += [vals[pos] sizeInBytes: exclude];
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-04-01 07:03:43 +00:00
|
|
|
|
return size;
|
2007-03-08 17:12:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation SQLRecord (KVC)
|
|
|
|
|
- (void) setValue: (id)aValue forKey: (NSString*)aKey
|
|
|
|
|
{
|
|
|
|
|
[self setObject: aValue forKey: aKey];
|
|
|
|
|
}
|
|
|
|
|
- (id) valueForKey: (NSString*)aKey
|
|
|
|
|
{
|
|
|
|
|
id v = [self objectForKey: aKey];
|
|
|
|
|
|
|
|
|
|
if (v == nil)
|
|
|
|
|
{
|
|
|
|
|
v = [super valueForKey: aKey];
|
|
|
|
|
}
|
|
|
|
|
return v;
|
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
2007-03-15 12:38:44 +00:00
|
|
|
|
@implementation _ConcreteSQLRecord
|
2007-03-08 17:12:55 +00:00
|
|
|
|
|
2015-04-28 11:47:23 +00:00
|
|
|
|
+ (id) newWithValues: (id*)v keys: (SQLRecordKeys*)k
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
2015-04-28 11:47:23 +00:00
|
|
|
|
id *ptr;
|
2007-03-15 12:38:44 +00:00
|
|
|
|
_ConcreteSQLRecord *r;
|
2015-04-28 11:47:23 +00:00
|
|
|
|
NSUInteger c;
|
2004-04-26 15:13:27 +00:00
|
|
|
|
|
2015-04-28 11:47:23 +00:00
|
|
|
|
c = [k count];
|
2007-03-15 12:38:44 +00:00
|
|
|
|
r = (_ConcreteSQLRecord*)NSAllocateObject(self,
|
2015-04-28 11:47:23 +00:00
|
|
|
|
c*sizeof(id), NSDefaultMallocZone());
|
2004-04-26 15:13:27 +00:00
|
|
|
|
r->count = c;
|
2015-04-28 11:47:23 +00:00
|
|
|
|
r->keys = [k retain];
|
|
|
|
|
ptr = (id*)(((void*)&(r->count)) + sizeof(r->count));
|
|
|
|
|
while (c-- > 0)
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
2015-04-28 11:47:23 +00:00
|
|
|
|
if (nil == v[c])
|
2004-08-22 09:34:18 +00:00
|
|
|
|
{
|
2015-04-28 11:47:23 +00:00
|
|
|
|
ptr[c] = [null retain];
|
2004-08-22 09:34:18 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2015-04-28 11:47:23 +00:00
|
|
|
|
ptr[c] = [v[c] retain];
|
2004-08-22 09:34:18 +00:00
|
|
|
|
}
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-28 11:47:23 +00:00
|
|
|
|
+ (id) newWithValues: (id*)v keys: (NSString**)k count: (unsigned int)c
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
2015-04-28 11:47:23 +00:00
|
|
|
|
SQLRecordKeys *o;
|
|
|
|
|
_ConcreteSQLRecord *r;
|
|
|
|
|
|
|
|
|
|
o = [[SQLRecordKeys alloc] initWithKeys: k count: c];
|
|
|
|
|
r = [self newWithValues: v keys: o];
|
|
|
|
|
[o release];
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2004-04-26 15:13:27 +00:00
|
|
|
|
|
2015-04-28 11:47:23 +00:00
|
|
|
|
- (NSArray*) allKeys
|
|
|
|
|
{
|
|
|
|
|
return [keys order];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) copyWithZone: (NSZone*)z
|
|
|
|
|
{
|
2009-11-18 11:11:29 +00:00
|
|
|
|
return [self retain];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-01-06 16:03:51 +00:00
|
|
|
|
- (NSUInteger) count
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
|
{
|
|
|
|
|
id *ptr;
|
2015-04-28 11:47:23 +00:00
|
|
|
|
NSUInteger pos;
|
2004-04-26 15:13:27 +00:00
|
|
|
|
|
2015-04-28 11:47:23 +00:00
|
|
|
|
[keys release];
|
|
|
|
|
ptr = (id*)(((void*)&count) + sizeof(count));
|
2004-04-26 15:13:27 +00:00
|
|
|
|
for (pos = 0; pos < count; pos++)
|
|
|
|
|
{
|
2009-11-18 11:11:29 +00:00
|
|
|
|
[ptr[pos] release]; ptr[pos] = nil;
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
2005-08-03 05:39:29 +00:00
|
|
|
|
[super dealloc];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2004-08-22 09:34:18 +00:00
|
|
|
|
- (NSMutableDictionary*) dictionary
|
|
|
|
|
{
|
|
|
|
|
NSMutableDictionary *d;
|
2015-04-28 11:47:23 +00:00
|
|
|
|
NSUInteger pos;
|
|
|
|
|
NSArray *k = [keys order];
|
2004-08-22 09:34:18 +00:00
|
|
|
|
id *ptr;
|
|
|
|
|
|
2015-04-28 11:47:23 +00:00
|
|
|
|
ptr = (id*)(((void*)&count) + sizeof(count));
|
2004-08-22 09:34:18 +00:00
|
|
|
|
d = [NSMutableDictionary dictionaryWithCapacity: count];
|
|
|
|
|
for (pos = 0; pos < count; pos++)
|
|
|
|
|
{
|
2015-04-28 11:47:23 +00:00
|
|
|
|
[d setObject: ptr[pos]
|
|
|
|
|
forKey: [[k objectAtIndex: pos] lowercaseString]];
|
2004-08-22 09:34:18 +00:00
|
|
|
|
}
|
|
|
|
|
return d;
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-08 17:12:55 +00:00
|
|
|
|
- (void) getKeys: (id*)buf
|
|
|
|
|
{
|
2015-04-28 11:47:23 +00:00
|
|
|
|
[[keys order] getObjects: buf];
|
2007-03-08 17:12:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-09-30 13:12:27 +00:00
|
|
|
|
- (void) getObjects: (id*)buf
|
|
|
|
|
{
|
|
|
|
|
id *ptr;
|
2015-04-28 11:47:23 +00:00
|
|
|
|
NSUInteger pos;
|
2005-09-30 13:12:27 +00:00
|
|
|
|
|
2015-04-28 11:47:23 +00:00
|
|
|
|
ptr = (id*)(((void*)&count) + sizeof(count));
|
2005-09-30 13:12:27 +00:00
|
|
|
|
for (pos = 0; pos < count; pos++)
|
|
|
|
|
{
|
|
|
|
|
buf[pos] = ptr[pos];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-04-26 15:13:27 +00:00
|
|
|
|
- (id) init
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"Illegal attempt to -init an SQLRecord");
|
2009-11-18 11:11:29 +00:00
|
|
|
|
[self release];
|
|
|
|
|
return nil;
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-01-06 16:03:51 +00:00
|
|
|
|
- (NSString*) keyAtIndex: (NSUInteger)pos
|
2007-03-08 17:12:55 +00:00
|
|
|
|
{
|
2015-04-28 11:47:23 +00:00
|
|
|
|
return [[keys order] objectAtIndex: pos];
|
|
|
|
|
}
|
2007-03-08 17:12:55 +00:00
|
|
|
|
|
2015-04-28 11:47:23 +00:00
|
|
|
|
- (SQLRecordKeys*) keys
|
|
|
|
|
{
|
|
|
|
|
return keys;
|
2007-03-08 17:12:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-01-06 15:39:11 +00:00
|
|
|
|
- (id) objectAtIndex: (NSUInteger)pos
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
|
|
|
|
id *ptr;
|
|
|
|
|
|
|
|
|
|
if (pos >= count)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSRangeException
|
|
|
|
|
format: @"Array index too large"];
|
|
|
|
|
}
|
2015-04-28 11:47:23 +00:00
|
|
|
|
ptr = (id*)(((void*)&count) + sizeof(count));
|
2004-04-26 15:13:27 +00:00
|
|
|
|
return ptr[pos];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) objectForKey: (NSString*)key
|
|
|
|
|
{
|
2015-04-28 11:47:23 +00:00
|
|
|
|
NSUInteger pos = [keys indexForKey: key];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
|
2015-04-28 11:47:23 +00:00
|
|
|
|
if (NSNotFound == pos)
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
2015-04-28 11:47:23 +00:00
|
|
|
|
return nil;
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
2015-04-28 11:47:23 +00:00
|
|
|
|
else
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
2015-04-28 11:47:23 +00:00
|
|
|
|
id *ptr;
|
|
|
|
|
|
|
|
|
|
ptr = (id*)(((void*)&count) + sizeof(count));
|
|
|
|
|
return ptr[pos];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2005-09-20 12:57:48 +00:00
|
|
|
|
|
2012-01-06 15:39:11 +00:00
|
|
|
|
- (void) replaceObjectAtIndex: (NSUInteger)index withObject: (id)anObject
|
2007-03-08 17:12:55 +00:00
|
|
|
|
{
|
|
|
|
|
id *ptr;
|
|
|
|
|
|
|
|
|
|
if (index >= count)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSRangeException
|
|
|
|
|
format: @"Array index too large"];
|
|
|
|
|
}
|
|
|
|
|
if (anObject == nil)
|
|
|
|
|
{
|
|
|
|
|
anObject = null;
|
|
|
|
|
}
|
2015-04-28 11:47:23 +00:00
|
|
|
|
ptr = (id*)(((void*)&count) + sizeof(count));
|
2007-03-08 17:12:55 +00:00
|
|
|
|
ptr += index;
|
2009-11-18 11:11:29 +00:00
|
|
|
|
[anObject retain];
|
|
|
|
|
[*ptr release];
|
|
|
|
|
*ptr = anObject;
|
2007-03-08 17:12:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-09-20 12:57:48 +00:00
|
|
|
|
- (void) setObject: (id)anObject forKey: (NSString*)aKey
|
|
|
|
|
{
|
|
|
|
|
id *ptr;
|
2015-04-28 11:47:23 +00:00
|
|
|
|
NSUInteger pos;
|
2005-09-20 12:57:48 +00:00
|
|
|
|
|
|
|
|
|
if (anObject == nil)
|
|
|
|
|
{
|
|
|
|
|
anObject = null;
|
|
|
|
|
}
|
2015-04-28 11:47:23 +00:00
|
|
|
|
ptr = (id*)(((void*)&count) + sizeof(count));
|
|
|
|
|
pos = [keys indexForKey: aKey];
|
|
|
|
|
if (NSNotFound == pos)
|
2005-09-20 12:57:48 +00:00
|
|
|
|
{
|
2015-04-28 11:47:23 +00:00
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"Bad key (%@) in -setObject:forKey:", aKey];
|
2005-09-20 12:57:48 +00:00
|
|
|
|
}
|
2015-04-28 11:47:23 +00:00
|
|
|
|
else
|
2005-09-20 12:57:48 +00:00
|
|
|
|
{
|
2015-04-28 11:47:23 +00:00
|
|
|
|
[anObject retain];
|
|
|
|
|
[ptr[pos] release];
|
|
|
|
|
ptr[pos] = anObject;
|
2005-09-20 12:57:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2005-09-22 08:42:37 +00:00
|
|
|
|
|
2013-09-05 12:48:53 +00:00
|
|
|
|
- (NSUInteger) sizeInBytes: (NSMutableSet*)exclude
|
2005-09-22 08:42:37 +00:00
|
|
|
|
{
|
|
|
|
|
if ([exclude member: self] != nil)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-09-05 12:48:53 +00:00
|
|
|
|
NSUInteger size = [super sizeInBytes: exclude];
|
|
|
|
|
NSUInteger pos;
|
2005-09-22 08:42:37 +00:00
|
|
|
|
id *ptr;
|
|
|
|
|
|
2015-04-28 11:47:23 +00:00
|
|
|
|
ptr = (id*)(((void*)&count) + sizeof(count));
|
2005-09-22 08:42:37 +00:00
|
|
|
|
for (pos = 0; pos < count; pos++)
|
|
|
|
|
{
|
|
|
|
|
size += [ptr[pos] sizeInBytes: exclude];
|
|
|
|
|
}
|
|
|
|
|
return size;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-04-26 15:13:27 +00:00
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Exception raised when an error with the remote database server occurs.
|
|
|
|
|
*/
|
|
|
|
|
NSString *SQLException = @"SQLException";
|
|
|
|
|
/**
|
|
|
|
|
* Exception for when a connection to the server is lost.
|
|
|
|
|
*/
|
|
|
|
|
NSString *SQLConnectionException = @"SQLConnectionException";
|
|
|
|
|
/**
|
|
|
|
|
* Exception for when a query is supposed to return data and doesn't.
|
|
|
|
|
*/
|
|
|
|
|
NSString *SQLEmptyException = @"SQLEmptyException";
|
|
|
|
|
/**
|
|
|
|
|
* Exception for when an insert/update would break the uniqueness of a
|
|
|
|
|
* field or index.
|
|
|
|
|
*/
|
|
|
|
|
NSString *SQLUniqueException = @"SQLUniqueException";
|
|
|
|
|
|
|
|
|
|
@implementation SQLClient (Logging)
|
|
|
|
|
|
|
|
|
|
+ (unsigned int) debugging
|
|
|
|
|
{
|
|
|
|
|
return classDebugging;
|
|
|
|
|
}
|
|
|
|
|
|
2004-05-07 08:16:16 +00:00
|
|
|
|
+ (NSTimeInterval) durationLogging
|
|
|
|
|
{
|
|
|
|
|
return classDuration;
|
|
|
|
|
}
|
|
|
|
|
|
2004-04-26 15:13:27 +00:00
|
|
|
|
+ (void) setDebugging: (unsigned int)level
|
|
|
|
|
{
|
|
|
|
|
classDebugging = level;
|
|
|
|
|
}
|
|
|
|
|
|
2004-05-07 08:16:16 +00:00
|
|
|
|
+ (void) setDurationLogging: (NSTimeInterval)threshold
|
|
|
|
|
{
|
|
|
|
|
classDuration = threshold;
|
|
|
|
|
}
|
|
|
|
|
|
2004-04-26 15:13:27 +00:00
|
|
|
|
- (void) debug: (NSString*)fmt, ...
|
|
|
|
|
{
|
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
|
NSLogv(fmt, ap);
|
|
|
|
|
va_end(ap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (unsigned int) debugging
|
|
|
|
|
{
|
2004-05-07 08:16:16 +00:00
|
|
|
|
return _debugging;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSTimeInterval) durationLogging
|
|
|
|
|
{
|
|
|
|
|
return _duration;
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) setDebugging: (unsigned int)level
|
|
|
|
|
{
|
2004-05-07 08:16:16 +00:00
|
|
|
|
_debugging = level;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) setDurationLogging: (NSTimeInterval)threshold
|
|
|
|
|
{
|
|
|
|
|
_duration = threshold;
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
2015-06-09 17:31:09 +00:00
|
|
|
|
/* Containers for all instances.
|
|
|
|
|
* Access to and update of these containers is protected by classLock.
|
|
|
|
|
* Most other operations involving an instance are protected by a lock
|
|
|
|
|
* in that instance.
|
|
|
|
|
* To avoid deadlocks, we always obtain the instance lock *before* we
|
|
|
|
|
* obtain the class lock, so we don't get the situation where one thread
|
|
|
|
|
* has locked the instance and another has locked the class lock, and
|
|
|
|
|
* each thread then tries to get the other lock.
|
2004-04-26 15:13:27 +00:00
|
|
|
|
*/
|
2014-06-19 21:26:25 +00:00
|
|
|
|
static NSHashTable *clientsHash = 0;
|
2005-09-27 06:35:05 +00:00
|
|
|
|
static NSMapTable *clientsMap = 0;
|
2014-06-19 21:26:25 +00:00
|
|
|
|
static NSRecursiveLock *clientsLock = nil;
|
2015-06-09 17:31:09 +00:00
|
|
|
|
|
|
|
|
|
/* Protect changes to the cache used for queries by any individual client.
|
|
|
|
|
*/
|
|
|
|
|
static NSRecursiveLock *cacheLock = nil;
|
|
|
|
|
|
2004-09-17 14:54:56 +00:00
|
|
|
|
static NSString *beginString = @"begin";
|
2004-04-26 15:13:27 +00:00
|
|
|
|
static NSArray *beginStatement = nil;
|
2004-09-17 14:54:56 +00:00
|
|
|
|
static NSString *commitString = @"commit";
|
2004-04-26 15:13:27 +00:00
|
|
|
|
static NSArray *commitStatement = nil;
|
2004-09-17 14:54:56 +00:00
|
|
|
|
static NSString *rollbackString = @"rollback";
|
2004-04-26 15:13:27 +00:00
|
|
|
|
static NSArray *rollbackStatement = nil;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@interface SQLClient (Private)
|
2014-10-07 09:15:16 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Internal method to handle configuration using the notification object.
|
|
|
|
|
* This object may be either a configuration front end or a user defaults
|
|
|
|
|
* object ... so we have to be careful that we work with both.
|
|
|
|
|
*/
|
2004-04-26 15:13:27 +00:00
|
|
|
|
- (void) _configure: (NSNotification*)n;
|
2014-10-07 09:15:16 +00:00
|
|
|
|
|
|
|
|
|
/** Internal method to make the client instance lock available to
|
|
|
|
|
* an associated SQLTransaction
|
|
|
|
|
*/
|
|
|
|
|
- (NSRecursiveLock*) _lock;
|
|
|
|
|
|
|
|
|
|
/** Internal method to populate the cache with the result of a query.
|
|
|
|
|
*/
|
2008-02-21 16:23:23 +00:00
|
|
|
|
- (void) _populateCache: (CacheQuery*)a;
|
2014-10-07 09:15:16 +00:00
|
|
|
|
|
|
|
|
|
/** Internal method called to record the 'main' thread in which automated
|
|
|
|
|
* cache updates are to be performed.
|
|
|
|
|
*/
|
2008-02-21 16:23:23 +00:00
|
|
|
|
- (void) _recordMainThread;
|
2014-10-07 09:15:16 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Internal method to substitute values from the dictionary into
|
|
|
|
|
* a string containing markup identifying where the values should
|
|
|
|
|
* appear by name. Non-string objects in the dictionary are quoted.<br />
|
|
|
|
|
* Returns an array containing the statement as the first object and
|
|
|
|
|
* any NSData objects following. The NSData objects appear in the
|
|
|
|
|
* statement strings as the marker sequence - <code>'?'''?'</code>
|
|
|
|
|
*/
|
2014-08-08 08:07:06 +00:00
|
|
|
|
- (NSMutableArray*) _substitute: (NSString*)str with: (NSDictionary*)vals;
|
2014-10-07 09:15:16 +00:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Called at one second intervals to ensure that our current timestamp
|
|
|
|
|
* is reasonably accurate.
|
|
|
|
|
*/
|
2005-09-27 06:35:05 +00:00
|
|
|
|
+ (void) _tick: (NSTimer*)t;
|
2014-10-07 09:15:16 +00:00
|
|
|
|
|
2004-04-26 15:13:27 +00:00
|
|
|
|
@end
|
|
|
|
|
|
2008-02-21 16:23:23 +00:00
|
|
|
|
@interface SQLClient (GSCacheDelegate)
|
|
|
|
|
- (BOOL) shouldKeepItem: (id)anObject
|
|
|
|
|
withKey: (id)aKey
|
|
|
|
|
lifetime: (unsigned)lifetime
|
|
|
|
|
after: (unsigned)delay;
|
|
|
|
|
@end
|
|
|
|
|
|
2004-04-26 15:13:27 +00:00
|
|
|
|
@implementation SQLClient
|
|
|
|
|
|
|
|
|
|
static unsigned int maxConnections = 8;
|
2014-10-13 10:47:06 +00:00
|
|
|
|
static int poolConnections = 0;
|
2004-04-26 15:13:27 +00:00
|
|
|
|
|
|
|
|
|
+ (NSArray*) allClients
|
|
|
|
|
{
|
2014-06-19 21:26:25 +00:00
|
|
|
|
NSMutableArray *a;
|
|
|
|
|
NSHashEnumerator e;
|
|
|
|
|
id o;
|
2004-04-26 15:13:27 +00:00
|
|
|
|
|
2014-06-19 21:26:25 +00:00
|
|
|
|
[clientsLock lock];
|
|
|
|
|
a = [NSMutableArray arrayWithCapacity: NSCountHashTable(clientsHash)];
|
|
|
|
|
e = NSEnumerateHashTable(clientsHash);
|
|
|
|
|
while (nil != (o = (id)NSNextHashEnumeratorItem(&e)))
|
|
|
|
|
{
|
|
|
|
|
[a addObject: o];
|
|
|
|
|
}
|
|
|
|
|
NSEndHashTableEnumeration(&e);
|
|
|
|
|
[clientsLock unlock];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
return a;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (SQLClient*) clientWithConfiguration: (NSDictionary*)config
|
|
|
|
|
name: (NSString*)reference
|
|
|
|
|
{
|
|
|
|
|
SQLClient *o;
|
|
|
|
|
|
2004-10-07 09:30:14 +00:00
|
|
|
|
if ([reference isKindOfClass: NSStringClass] == NO)
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
2004-06-29 17:11:10 +00:00
|
|
|
|
if (config == nil)
|
|
|
|
|
{
|
|
|
|
|
reference = [[NSUserDefaults standardUserDefaults] objectForKey:
|
|
|
|
|
@"SQLClientName"];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
reference = [config objectForKey: @"SQLClientName"];
|
|
|
|
|
}
|
2004-10-07 09:30:14 +00:00
|
|
|
|
if ([reference isKindOfClass: NSStringClass] == NO)
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
|
|
|
|
reference = @"Database";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
o = [self existingClient: reference];
|
2015-01-06 10:56:39 +00:00
|
|
|
|
if (nil == o)
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
2015-01-06 10:56:39 +00:00
|
|
|
|
o = [[[SQLClient alloc] initWithConfiguration: config name: reference]
|
|
|
|
|
autorelease];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (SQLClient*) existingClient: (NSString*)reference
|
|
|
|
|
{
|
|
|
|
|
SQLClient *existing;
|
|
|
|
|
|
2004-10-07 09:30:14 +00:00
|
|
|
|
if ([reference isKindOfClass: NSStringClass] == NO)
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
|
|
|
|
reference = [[NSUserDefaults standardUserDefaults] stringForKey:
|
|
|
|
|
@"SQLClientName"];
|
|
|
|
|
if (reference == nil)
|
|
|
|
|
{
|
|
|
|
|
reference = @"Database";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-19 21:26:25 +00:00
|
|
|
|
[clientsLock lock];
|
2005-09-27 06:35:05 +00:00
|
|
|
|
existing = (SQLClient*)NSMapGet(clientsMap, reference);
|
2009-11-18 11:11:29 +00:00
|
|
|
|
[[existing retain] autorelease];
|
2014-06-19 21:26:25 +00:00
|
|
|
|
[clientsLock unlock];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
return existing;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (void) initialize
|
|
|
|
|
{
|
2014-06-20 05:15:24 +00:00
|
|
|
|
if (Nil == SQLClientClass && [SQLClient class] == self)
|
|
|
|
|
{
|
|
|
|
|
static id modes[1];
|
|
|
|
|
|
|
|
|
|
SQLClientClass = self;
|
|
|
|
|
modes[0] = NSDefaultRunLoopMode;
|
|
|
|
|
queryModes = [[NSArray alloc] initWithObjects: modes count: 1];
|
|
|
|
|
GSTickerTimeNow();
|
2015-05-04 12:15:47 +00:00
|
|
|
|
[SQLRecord class]; // Force initialisatio
|
2014-06-20 05:15:24 +00:00
|
|
|
|
if (0 == clientsHash)
|
|
|
|
|
{
|
2015-05-04 12:15:47 +00:00
|
|
|
|
cacheLock = [NSRecursiveLock new];
|
2014-06-20 05:15:24 +00:00
|
|
|
|
clientsHash = NSCreateHashTable(NSNonOwnedPointerHashCallBacks, 0);
|
|
|
|
|
clientsMap = NSCreateMapTable(NSObjectMapKeyCallBacks,
|
|
|
|
|
NSNonRetainedObjectMapValueCallBacks, 0);
|
|
|
|
|
clientsLock = [NSRecursiveLock new];
|
|
|
|
|
beginStatement = [[NSArray arrayWithObject: beginString] retain];
|
|
|
|
|
commitStatement = [[NSArray arrayWithObject: commitString] retain];
|
|
|
|
|
rollbackStatement
|
|
|
|
|
= [[NSArray arrayWithObject: rollbackString] retain];
|
|
|
|
|
NSStringClass = [NSString class];
|
|
|
|
|
NSArrayClass = [NSArray class];
|
|
|
|
|
NSSetClass = [NSSet class];
|
|
|
|
|
[NSTimer scheduledTimerWithTimeInterval: 1.0
|
|
|
|
|
target: self
|
|
|
|
|
selector: @selector(_tick:)
|
|
|
|
|
userInfo: 0
|
|
|
|
|
repeats: YES];
|
|
|
|
|
}
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (unsigned int) maxConnections
|
|
|
|
|
{
|
|
|
|
|
return maxConnections;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (void) purgeConnections: (NSDate*)since
|
|
|
|
|
{
|
2014-06-19 21:26:25 +00:00
|
|
|
|
NSHashEnumerator e;
|
2015-06-09 16:47:36 +00:00
|
|
|
|
NSMutableArray *a = nil;
|
2004-10-07 09:30:14 +00:00
|
|
|
|
SQLClient *o;
|
2004-04-26 15:13:27 +00:00
|
|
|
|
unsigned int connectionCount = 0;
|
2015-06-09 16:47:36 +00:00
|
|
|
|
NSTimeInterval t;
|
2004-04-26 15:13:27 +00:00
|
|
|
|
|
2015-06-09 16:47:36 +00:00
|
|
|
|
t = (nil == since) ? 0.0 : [since timeIntervalSinceReferenceDate];
|
|
|
|
|
|
|
|
|
|
/* Find clients we may want to disconnect.
|
|
|
|
|
*/
|
2014-06-19 21:26:25 +00:00
|
|
|
|
[clientsLock lock];
|
|
|
|
|
e = NSEnumerateHashTable(clientsHash);
|
|
|
|
|
while (nil != (o = (SQLClient*)NSNextHashEnumeratorItem(&e)))
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
|
|
|
|
if (since != nil)
|
|
|
|
|
{
|
2004-10-07 09:30:14 +00:00
|
|
|
|
NSTimeInterval when = o->_lastOperation;
|
2004-04-26 15:13:27 +00:00
|
|
|
|
|
2015-06-09 16:47:36 +00:00
|
|
|
|
if (when < o->_lastStart)
|
|
|
|
|
{
|
|
|
|
|
when = o->_lastStart;
|
|
|
|
|
}
|
2014-09-10 11:49:40 +00:00
|
|
|
|
if (when < t && YES == o->connected)
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
2015-06-09 16:47:36 +00:00
|
|
|
|
if (nil == a)
|
|
|
|
|
{
|
|
|
|
|
a = [NSMutableArray array];
|
|
|
|
|
}
|
|
|
|
|
[a addObject: o];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-06-09 16:47:36 +00:00
|
|
|
|
else if ([o connected] == YES)
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
|
|
|
|
connectionCount++;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-06-19 21:26:25 +00:00
|
|
|
|
NSEndHashTableEnumeration(&e);
|
|
|
|
|
[clientsLock unlock];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
|
2015-06-09 16:47:36 +00:00
|
|
|
|
/* Disconnect any clients idle too long
|
|
|
|
|
*/
|
|
|
|
|
while ([a count] > 0)
|
|
|
|
|
{
|
|
|
|
|
o = [a lastObject];
|
|
|
|
|
if ([o->lock tryLock])
|
|
|
|
|
{
|
|
|
|
|
NSTimeInterval when = o->_lastOperation;
|
|
|
|
|
|
|
|
|
|
if (when < o->_lastStart)
|
|
|
|
|
{
|
|
|
|
|
when = o->_lastStart;
|
|
|
|
|
}
|
|
|
|
|
if (when < t && YES == o->connected)
|
|
|
|
|
{
|
|
|
|
|
NS_DURING
|
|
|
|
|
{
|
|
|
|
|
[o disconnect];
|
|
|
|
|
if ([o connected] == YES)
|
|
|
|
|
{
|
|
|
|
|
connectionCount++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
NS_HANDLER
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"Problem disconnecting: %@", localException);
|
|
|
|
|
}
|
|
|
|
|
NS_ENDHANDLER
|
|
|
|
|
}
|
|
|
|
|
[o->lock unlock];
|
|
|
|
|
}
|
|
|
|
|
[a removeLastObject];
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-13 10:47:06 +00:00
|
|
|
|
while (connectionCount >= (maxConnections + poolConnections))
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
|
|
|
|
SQLClient *other = nil;
|
2004-10-07 09:30:14 +00:00
|
|
|
|
NSTimeInterval oldest = 0.0;
|
2004-04-26 15:13:27 +00:00
|
|
|
|
|
|
|
|
|
connectionCount = 0;
|
2014-06-19 21:26:25 +00:00
|
|
|
|
[clientsLock lock];
|
|
|
|
|
e = NSEnumerateHashTable(clientsHash);
|
|
|
|
|
while (nil != (o = (SQLClient*)NSNextHashEnumeratorItem(&e)))
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
2014-09-10 11:49:40 +00:00
|
|
|
|
if (YES == o->connected)
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
2004-10-07 09:30:14 +00:00
|
|
|
|
NSTimeInterval when = o->_lastOperation;
|
2004-04-26 15:13:27 +00:00
|
|
|
|
|
2015-06-09 16:47:36 +00:00
|
|
|
|
if (when < o->_lastStart)
|
|
|
|
|
{
|
|
|
|
|
when = o->_lastStart;
|
|
|
|
|
}
|
2004-04-26 15:13:27 +00:00
|
|
|
|
connectionCount++;
|
2004-10-07 09:30:14 +00:00
|
|
|
|
if (oldest == 0.0 || when < oldest)
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
|
|
|
|
oldest = when;
|
2015-06-09 16:47:36 +00:00
|
|
|
|
ASSIGN(other, o);
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-06-19 21:26:25 +00:00
|
|
|
|
NSEndHashTableEnumeration(&e);
|
|
|
|
|
[clientsLock unlock];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
connectionCount--;
|
|
|
|
|
if ([other debugging] > 0)
|
|
|
|
|
{
|
|
|
|
|
[other debug:
|
2014-10-13 10:47:06 +00:00
|
|
|
|
@"Force disconnect of '%@' because max connections (%u) reached",
|
2004-04-26 15:13:27 +00:00
|
|
|
|
other, maxConnections];
|
|
|
|
|
}
|
2015-06-09 16:47:36 +00:00
|
|
|
|
[AUTORELEASE(other) disconnect];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (void) setMaxConnections: (unsigned int)c
|
|
|
|
|
{
|
|
|
|
|
if (c > 0)
|
|
|
|
|
{
|
|
|
|
|
maxConnections = c;
|
|
|
|
|
[self purgeConnections: nil];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) begin
|
|
|
|
|
{
|
|
|
|
|
[lock lock];
|
2004-05-07 08:16:16 +00:00
|
|
|
|
if (_inTransaction == NO)
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
2004-05-07 08:16:16 +00:00
|
|
|
|
_inTransaction = YES;
|
2004-04-26 15:13:27 +00:00
|
|
|
|
NS_DURING
|
|
|
|
|
{
|
2004-05-07 08:16:16 +00:00
|
|
|
|
[self simpleExecute: beginStatement];
|
2011-09-16 07:25:33 +00:00
|
|
|
|
/* NB. We leave the lock locked ... until a matching -commit
|
|
|
|
|
* or -rollback is called. This prevents other threads from
|
2014-05-07 16:36:06 +00:00
|
|
|
|
* interfering with this transaction.
|
2011-09-16 07:25:33 +00:00
|
|
|
|
*/
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
NS_HANDLER
|
|
|
|
|
{
|
2004-05-07 08:16:16 +00:00
|
|
|
|
_inTransaction = NO;
|
2014-05-13 10:26:48 +00:00
|
|
|
|
[lock unlock];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
[localException raise];
|
|
|
|
|
}
|
|
|
|
|
NS_ENDHANDLER
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
[lock unlock];
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"begin used inside transaction"];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-08 17:12:55 +00:00
|
|
|
|
- (NSString*) buildQuery: (NSString*)stmt, ...
|
|
|
|
|
{
|
|
|
|
|
va_list ap;
|
|
|
|
|
NSString *sql = nil;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* First check validity and concatenate parts of the query.
|
|
|
|
|
*/
|
|
|
|
|
va_start (ap, stmt);
|
2015-02-26 16:14:51 +00:00
|
|
|
|
sql = [[self prepare: stmt args: ap] objectAtIndex: 0];
|
2007-03-08 17:12:55 +00:00
|
|
|
|
va_end (ap);
|
|
|
|
|
|
|
|
|
|
return sql;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSString*) buildQuery: (NSString*)stmt with: (NSDictionary*)values
|
|
|
|
|
{
|
|
|
|
|
NSString *sql = nil;
|
|
|
|
|
|
|
|
|
|
sql = [[self _substitute: stmt with: values] objectAtIndex: 0];
|
|
|
|
|
|
|
|
|
|
return sql;
|
|
|
|
|
}
|
|
|
|
|
|
2004-04-26 15:13:27 +00:00
|
|
|
|
- (NSString*) clientName
|
|
|
|
|
{
|
2004-05-07 08:16:16 +00:00
|
|
|
|
return _client;
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) commit
|
|
|
|
|
{
|
|
|
|
|
[lock lock];
|
2004-05-07 08:16:16 +00:00
|
|
|
|
if (_inTransaction == NO)
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
|
|
|
|
[lock unlock];
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"commit used outside transaction"];
|
|
|
|
|
}
|
2011-09-16 07:25:33 +00:00
|
|
|
|
|
|
|
|
|
/* Since we are in a transaction we must be doubly locked right now,
|
|
|
|
|
* so we unlock once, and we still have the lock (which was locked
|
|
|
|
|
* in the earlier call to the -begin method).
|
|
|
|
|
*/
|
|
|
|
|
[lock unlock];
|
|
|
|
|
_inTransaction = NO;
|
2004-04-26 15:13:27 +00:00
|
|
|
|
NS_DURING
|
|
|
|
|
{
|
2004-05-07 08:16:16 +00:00
|
|
|
|
[self simpleExecute: commitStatement];
|
2007-07-07 07:23:40 +00:00
|
|
|
|
[_statements removeAllObjects];
|
2004-09-17 14:54:56 +00:00
|
|
|
|
[lock unlock]; // Locked by -begin
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
NS_HANDLER
|
|
|
|
|
{
|
2007-07-07 07:23:40 +00:00
|
|
|
|
[_statements removeAllObjects];
|
2004-09-17 14:54:56 +00:00
|
|
|
|
[lock unlock]; // Locked by -begin
|
2004-04-26 15:13:27 +00:00
|
|
|
|
[localException raise];
|
|
|
|
|
}
|
|
|
|
|
NS_ENDHANDLER
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL) connect
|
|
|
|
|
{
|
2014-05-13 11:01:50 +00:00
|
|
|
|
if (NO == connected)
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
|
|
|
|
[lock lock];
|
2014-05-13 11:01:50 +00:00
|
|
|
|
if (NO == connected)
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
|
|
|
|
NS_DURING
|
|
|
|
|
{
|
2012-11-29 11:40:04 +00:00
|
|
|
|
if (_connectFails > 1)
|
|
|
|
|
{
|
|
|
|
|
NSTimeInterval delay;
|
|
|
|
|
NSTimeInterval elapsed;
|
|
|
|
|
|
|
|
|
|
/* If we have repeated connection failures, we enforce a
|
|
|
|
|
* delay of up to 30 seconds between connection attempts
|
|
|
|
|
* to avoid overloading the system with too frequent
|
|
|
|
|
* connection attempts.
|
|
|
|
|
*/
|
|
|
|
|
delay = (_connectFails < 30) ? _connectFails : 30;
|
|
|
|
|
elapsed = GSTickerTimeNow() - _lastOperation;
|
|
|
|
|
if (elapsed < delay)
|
|
|
|
|
{
|
|
|
|
|
[NSThread sleepForTimeInterval: delay - elapsed];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-20 05:15:24 +00:00
|
|
|
|
_lastStart = GSTickerTimeNow();
|
2004-04-26 15:13:27 +00:00
|
|
|
|
[self backendConnect];
|
2012-10-22 15:57:56 +00:00
|
|
|
|
/* On establishng a new connection, we must restore any
|
|
|
|
|
* listen instructions in the backend.
|
|
|
|
|
*/
|
|
|
|
|
if (nil != _names)
|
|
|
|
|
{
|
|
|
|
|
NSEnumerator *e;
|
|
|
|
|
NSString *n;
|
|
|
|
|
|
|
|
|
|
e = [_names objectEnumerator];
|
|
|
|
|
while (nil != (n = [e nextObject]))
|
|
|
|
|
{
|
|
|
|
|
[self backendListen: n];
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-05-29 09:33:03 +00:00
|
|
|
|
_lastConnect = GSTickerTimeNow();
|
2008-11-12 05:52:38 +00:00
|
|
|
|
_connectFails = 0;
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
NS_HANDLER
|
|
|
|
|
{
|
2008-11-12 05:52:38 +00:00
|
|
|
|
_lastOperation = GSTickerTimeNow();
|
|
|
|
|
_connectFails++;
|
2014-05-13 11:01:50 +00:00
|
|
|
|
[lock unlock];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
[localException raise];
|
|
|
|
|
}
|
|
|
|
|
NS_ENDHANDLER
|
|
|
|
|
}
|
|
|
|
|
[lock unlock];
|
2012-10-22 15:57:56 +00:00
|
|
|
|
if (YES == connected)
|
2007-07-09 17:08:22 +00:00
|
|
|
|
{
|
2007-08-01 15:40:12 +00:00
|
|
|
|
NSNotificationCenter *nc;
|
|
|
|
|
|
|
|
|
|
nc = [NSNotificationCenter defaultCenter];
|
|
|
|
|
[nc postNotificationName: SQLClientDidConnectNotification
|
|
|
|
|
object: self];
|
2007-07-09 17:08:22 +00:00
|
|
|
|
}
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
return connected;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL) connected
|
|
|
|
|
{
|
|
|
|
|
return connected;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSString*) database
|
|
|
|
|
{
|
2004-05-07 08:16:16 +00:00
|
|
|
|
return _database;
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
|
{
|
|
|
|
|
NSNotificationCenter *nc;
|
|
|
|
|
|
2014-06-19 21:26:25 +00:00
|
|
|
|
[clientsLock lock];
|
|
|
|
|
NSHashRemove(clientsHash, (void*)self);
|
2014-07-04 13:11:47 +00:00
|
|
|
|
if (_name != nil
|
|
|
|
|
&& (SQLClient*)NSMapGet(clientsMap, (void*)_name) == self)
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
2005-09-27 06:35:05 +00:00
|
|
|
|
NSMapRemove(clientsMap, (void*)_name);
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
2014-06-19 21:26:25 +00:00
|
|
|
|
[clientsLock unlock];
|
2006-09-10 13:37:23 +00:00
|
|
|
|
nc = [NSNotificationCenter defaultCenter];
|
|
|
|
|
[nc removeObserver: self];
|
2014-09-10 11:49:40 +00:00
|
|
|
|
if (YES == connected) [self disconnect];
|
2009-11-18 11:11:29 +00:00
|
|
|
|
[lock release]; lock = nil;
|
|
|
|
|
[_client release]; _client = nil;
|
|
|
|
|
[_database release]; _database = nil;
|
|
|
|
|
[_password release]; _password = nil;
|
|
|
|
|
[_user release]; _user = nil;
|
|
|
|
|
[_name release]; _name = nil;
|
|
|
|
|
[_statements release]; _statements = nil;
|
|
|
|
|
[_cache release]; _cache = nil;
|
|
|
|
|
[_cacheThread release]; _cacheThread = nil;
|
2012-10-22 15:57:56 +00:00
|
|
|
|
if (0 != _observers)
|
|
|
|
|
{
|
|
|
|
|
NSNotificationCenter *nc;
|
|
|
|
|
NSMapEnumerator e;
|
|
|
|
|
NSMutableSet *n;
|
|
|
|
|
id o;
|
|
|
|
|
|
|
|
|
|
nc = [NSNotificationCenter defaultCenter];
|
|
|
|
|
e = NSEnumerateMapTable(_observers);
|
|
|
|
|
while (NSNextMapEnumeratorPair(&e, (void**)&o, (void**)&n) != 0)
|
|
|
|
|
{
|
|
|
|
|
NSEnumerator *ne = [n objectEnumerator];
|
|
|
|
|
NSString *name;
|
|
|
|
|
|
|
|
|
|
while (nil != (name = [ne nextObject]))
|
|
|
|
|
{
|
|
|
|
|
[nc removeObserver: o name: name object: nil];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
NSEndMapTableEnumeration(&e);
|
|
|
|
|
NSFreeMapTable(_observers);
|
|
|
|
|
_observers = 0;
|
|
|
|
|
}
|
|
|
|
|
[_names release]; _names = 0;
|
2004-04-26 15:13:27 +00:00
|
|
|
|
[super dealloc];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSString*) description
|
|
|
|
|
{
|
2009-11-18 11:11:29 +00:00
|
|
|
|
NSMutableString *s = [[NSMutableString new] autorelease];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
|
2008-02-21 16:23:23 +00:00
|
|
|
|
[lock lock];
|
2012-11-29 11:40:04 +00:00
|
|
|
|
NS_DURING
|
2006-03-29 06:46:37 +00:00
|
|
|
|
{
|
2012-11-29 11:40:04 +00:00
|
|
|
|
[s appendFormat: @"Database - %@\n", [self clientName]];
|
|
|
|
|
[s appendFormat: @" Name - %@\n", [self name]];
|
|
|
|
|
[s appendFormat: @" DBase - %@\n", [self database]];
|
|
|
|
|
[s appendFormat: @" DB User - %@\n", [self user]];
|
|
|
|
|
[s appendFormat: @" Password - %@\n",
|
|
|
|
|
[self password] == nil ? @"unknown" : @"known"];
|
|
|
|
|
[s appendFormat: @" Connected - %@\n", connected ? @"yes" : @"no"];
|
|
|
|
|
[s appendFormat: @" Transaction - %@\n",
|
|
|
|
|
_inTransaction ? @"yes" : @"no"];
|
|
|
|
|
if (_cache == nil)
|
|
|
|
|
{
|
|
|
|
|
[s appendString: @"\n"];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
[s appendFormat: @" Cache - %@\n", _cache];
|
|
|
|
|
}
|
2006-03-29 06:46:37 +00:00
|
|
|
|
}
|
2012-11-29 11:40:04 +00:00
|
|
|
|
NS_HANDLER
|
2006-03-29 06:46:37 +00:00
|
|
|
|
{
|
2012-11-29 11:40:04 +00:00
|
|
|
|
[lock unlock];
|
|
|
|
|
[localException raise];
|
2006-03-29 06:46:37 +00:00
|
|
|
|
}
|
2012-11-29 11:40:04 +00:00
|
|
|
|
NS_ENDHANDLER
|
2008-02-21 16:23:23 +00:00
|
|
|
|
[lock unlock];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) disconnect
|
|
|
|
|
{
|
2014-05-13 11:01:50 +00:00
|
|
|
|
if (YES == connected)
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
2007-08-01 15:40:12 +00:00
|
|
|
|
NSNotificationCenter *nc;
|
|
|
|
|
|
2004-04-26 15:13:27 +00:00
|
|
|
|
[lock lock];
|
2013-01-31 09:04:35 +00:00
|
|
|
|
if (YES == _inTransaction)
|
|
|
|
|
{
|
|
|
|
|
/* If we are inside a transaction we must be doubly locked,
|
|
|
|
|
* so we do an unlock corresponding to the -begin before we
|
|
|
|
|
* disconnect (the disconnect implicitly rolls back the
|
|
|
|
|
* transaction).
|
|
|
|
|
*/
|
|
|
|
|
_inTransaction = NO;
|
|
|
|
|
[lock unlock];
|
|
|
|
|
}
|
2014-05-13 11:01:50 +00:00
|
|
|
|
if (YES == connected)
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
|
|
|
|
NS_DURING
|
|
|
|
|
{
|
|
|
|
|
[self backendDisconnect];
|
|
|
|
|
}
|
|
|
|
|
NS_HANDLER
|
|
|
|
|
{
|
|
|
|
|
[lock unlock];
|
|
|
|
|
[localException raise];
|
|
|
|
|
}
|
|
|
|
|
NS_ENDHANDLER
|
|
|
|
|
}
|
|
|
|
|
[lock unlock];
|
2007-08-01 15:40:12 +00:00
|
|
|
|
nc = [NSNotificationCenter defaultCenter];
|
|
|
|
|
[nc postNotificationName: SQLClientDidDisconnectNotification
|
|
|
|
|
object: self];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-19 18:41:36 +00:00
|
|
|
|
- (NSInteger) execute: (NSString*)stmt, ...
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
|
|
|
|
NSArray *info;
|
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
|
|
va_start (ap, stmt);
|
2015-02-26 16:14:51 +00:00
|
|
|
|
info = [self prepare: stmt args: ap];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
va_end (ap);
|
2012-10-19 18:41:36 +00:00
|
|
|
|
return [self simpleExecute: info];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-10-19 18:41:36 +00:00
|
|
|
|
- (NSInteger) execute: (NSString*)stmt with: (NSDictionary*)values
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
|
|
|
|
NSArray *info;
|
|
|
|
|
|
|
|
|
|
info = [self _substitute: stmt with: values];
|
2012-10-19 18:41:36 +00:00
|
|
|
|
return [self simpleExecute: info];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) init
|
|
|
|
|
{
|
|
|
|
|
return [self initWithConfiguration: nil name: nil];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithConfiguration: (NSDictionary*)config
|
|
|
|
|
{
|
|
|
|
|
return [self initWithConfiguration: config name: nil];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithConfiguration: (NSDictionary*)config
|
|
|
|
|
name: (NSString*)reference
|
2014-06-19 21:26:25 +00:00
|
|
|
|
{
|
2014-06-20 15:39:25 +00:00
|
|
|
|
return [self initWithConfiguration: config name: reference pool: nil];
|
2014-06-19 21:26:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- (id) initWithConfiguration: (NSDictionary*)config
|
|
|
|
|
name: (NSString*)reference
|
2014-06-20 15:39:25 +00:00
|
|
|
|
pool: (SQLClientPool*)pool
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
|
|
|
|
NSNotification *n;
|
2005-07-07 21:11:04 +00:00
|
|
|
|
NSDictionary *conf = config;
|
2004-04-26 15:13:27 +00:00
|
|
|
|
id existing;
|
|
|
|
|
|
2004-06-29 17:11:10 +00:00
|
|
|
|
if (conf == nil)
|
|
|
|
|
{
|
2005-07-07 21:11:04 +00:00
|
|
|
|
// Pretend the defaults object is a dictionary.
|
|
|
|
|
conf = (NSDictionary*)[NSUserDefaults standardUserDefaults];
|
2004-06-29 17:11:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2004-10-07 09:30:14 +00:00
|
|
|
|
if ([reference isKindOfClass: NSStringClass] == NO)
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
2004-06-29 17:11:10 +00:00
|
|
|
|
reference = [conf objectForKey: @"SQLClientName"];
|
2004-10-07 09:30:14 +00:00
|
|
|
|
if ([reference isKindOfClass: NSStringClass] == NO)
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
2004-06-29 17:11:10 +00:00
|
|
|
|
reference = [conf objectForKey: @"Database"];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-19 21:26:25 +00:00
|
|
|
|
[clientsLock lock];
|
2014-06-20 15:39:25 +00:00
|
|
|
|
_pool = pool;
|
2014-07-04 13:31:30 +00:00
|
|
|
|
if (nil != _pool)
|
2014-06-19 21:26:25 +00:00
|
|
|
|
{
|
2014-07-04 13:31:30 +00:00
|
|
|
|
existing = nil; // Pool, object ... can't already exist
|
2014-06-19 21:26:25 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-06-20 15:39:25 +00:00
|
|
|
|
existing = (SQLClient*)NSMapGet(clientsMap, reference);
|
2014-06-19 21:26:25 +00:00
|
|
|
|
}
|
2014-05-17 09:47:58 +00:00
|
|
|
|
if (nil == existing)
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
2014-05-07 16:36:06 +00:00
|
|
|
|
lock = [NSRecursiveLock new]; // Ensure thread-safety.
|
2004-04-26 15:13:27 +00:00
|
|
|
|
[self setDebugging: [[self class] debugging]];
|
2004-05-07 08:16:16 +00:00
|
|
|
|
[self setDurationLogging: [[self class] durationLogging]];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
[self setName: reference]; // Set name and store in cache.
|
2004-09-17 14:54:56 +00:00
|
|
|
|
_statements = [NSMutableArray new];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
|
2004-06-29 17:11:10 +00:00
|
|
|
|
if ([conf isKindOfClass: [NSUserDefaults class]] == YES)
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
|
|
|
|
NSNotificationCenter *nc;
|
|
|
|
|
|
|
|
|
|
nc = [NSNotificationCenter defaultCenter];
|
|
|
|
|
[nc addObserver: self
|
|
|
|
|
selector: @selector(_configure:)
|
|
|
|
|
name: NSUserDefaultsDidChangeNotification
|
2004-06-29 17:11:10 +00:00
|
|
|
|
object: conf];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
2004-06-29 17:11:10 +00:00
|
|
|
|
n = [NSNotification
|
|
|
|
|
notificationWithName: NSUserDefaultsDidChangeNotification
|
|
|
|
|
object: conf
|
|
|
|
|
userInfo: nil];
|
|
|
|
|
|
2014-06-19 21:26:25 +00:00
|
|
|
|
NSHashInsert(clientsHash, (void*)self);
|
2004-04-26 15:13:27 +00:00
|
|
|
|
[self _configure: n]; // Actually set up the configuration.
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2009-11-18 11:11:29 +00:00
|
|
|
|
[self release];
|
|
|
|
|
self = [existing retain];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
2014-06-19 21:26:25 +00:00
|
|
|
|
[clientsLock unlock];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-17 14:57:48 +00:00
|
|
|
|
- (NSUInteger) hash
|
|
|
|
|
{
|
|
|
|
|
return [[self database] hash] + [[self user] hash];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL) isEqual: (id)other
|
|
|
|
|
{
|
|
|
|
|
if (self == other)
|
|
|
|
|
{
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
|
|
|
|
if ([self class] != [other class])
|
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
if (NO == [[self database] isEqual: [other database]])
|
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
if (NO == [[self user] isEqual: [other user]])
|
|
|
|
|
{
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
|
|
|
|
|
2004-04-26 15:13:27 +00:00
|
|
|
|
- (BOOL) isInTransaction
|
|
|
|
|
{
|
2004-05-07 08:16:16 +00:00
|
|
|
|
return _inTransaction;
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-05-29 09:33:03 +00:00
|
|
|
|
- (NSDate*) lastConnect
|
|
|
|
|
{
|
|
|
|
|
if (_lastConnect > 0.0)
|
|
|
|
|
{
|
|
|
|
|
return [NSDate dateWithTimeIntervalSinceReferenceDate: _lastConnect];
|
|
|
|
|
}
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
2004-04-26 15:13:27 +00:00
|
|
|
|
- (NSDate*) lastOperation
|
|
|
|
|
{
|
2008-11-12 05:52:38 +00:00
|
|
|
|
if (_lastOperation > 0.0 && _connectFails == 0)
|
2004-10-07 09:30:14 +00:00
|
|
|
|
{
|
|
|
|
|
return [NSDate dateWithTimeIntervalSinceReferenceDate: _lastOperation];
|
|
|
|
|
}
|
|
|
|
|
return nil;
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-01 11:32:49 +00:00
|
|
|
|
- (BOOL) lockBeforeDate: (NSDate*)limit
|
|
|
|
|
{
|
|
|
|
|
if (nil == limit)
|
|
|
|
|
{
|
|
|
|
|
return [lock tryLock];
|
|
|
|
|
}
|
|
|
|
|
return [lock lockBeforeDate: limit];
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-20 05:15:24 +00:00
|
|
|
|
- (SQLClient*) longestIdle: (SQLClient*)other
|
|
|
|
|
{
|
|
|
|
|
NSTimeInterval t0;
|
|
|
|
|
NSTimeInterval t1;
|
|
|
|
|
|
|
|
|
|
t0 = _lastOperation;
|
|
|
|
|
if (t0 < _lastStart)
|
|
|
|
|
{
|
|
|
|
|
t0 = _lastStart;
|
|
|
|
|
}
|
|
|
|
|
if (NO == connected || 0 != _connectFails)
|
|
|
|
|
{
|
|
|
|
|
t0 = 0.0;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-27 06:02:43 +00:00
|
|
|
|
if (NO == [other isKindOfClass: SQLClientClass] || YES == [other isProxy])
|
2014-06-20 05:15:24 +00:00
|
|
|
|
{
|
|
|
|
|
t1 = 0.0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
t1 = other->_lastOperation;
|
|
|
|
|
if (t1 < other->_lastStart)
|
|
|
|
|
{
|
|
|
|
|
t1 = other->_lastStart;
|
|
|
|
|
}
|
2015-05-27 06:02:43 +00:00
|
|
|
|
if (NO == other->connected || 0 != other->_connectFails)
|
2014-06-20 05:15:24 +00:00
|
|
|
|
{
|
|
|
|
|
t1 = 0.0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-28 08:39:10 +00:00
|
|
|
|
if (t1 > 0.0 && (t1 <= t0 || 0.0 == t0))
|
2014-06-20 05:15:24 +00:00
|
|
|
|
{
|
2015-05-27 06:02:43 +00:00
|
|
|
|
return other;
|
2014-06-20 05:15:24 +00:00
|
|
|
|
}
|
2015-05-28 08:39:10 +00:00
|
|
|
|
if (t0 > 0.0 && (t0 <= t1 || 0.0 == t1))
|
2014-06-20 05:15:24 +00:00
|
|
|
|
{
|
2015-05-27 06:02:43 +00:00
|
|
|
|
return self;
|
2014-06-20 05:15:24 +00:00
|
|
|
|
}
|
2015-05-27 06:02:43 +00:00
|
|
|
|
return nil;
|
2014-06-20 05:15:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2004-04-26 15:13:27 +00:00
|
|
|
|
- (NSString*) name
|
|
|
|
|
{
|
2004-05-07 08:16:16 +00:00
|
|
|
|
return _name;
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSString*) password
|
|
|
|
|
{
|
2004-05-07 08:16:16 +00:00
|
|
|
|
return _password;
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-26 16:14:51 +00:00
|
|
|
|
- (NSMutableArray*) prepare: (NSString*)stmt args: (va_list)args
|
|
|
|
|
{
|
|
|
|
|
NSMutableArray *ma = [NSMutableArray arrayWithCapacity: 2];
|
|
|
|
|
NSString *tmp = va_arg(args, NSString*);
|
|
|
|
|
NSAutoreleasePool *arp = [NSAutoreleasePool new];
|
|
|
|
|
|
|
|
|
|
if (tmp != nil)
|
|
|
|
|
{
|
|
|
|
|
NSMutableString *s = [NSMutableString stringWithCapacity: 1024];
|
|
|
|
|
|
|
|
|
|
[s appendString: stmt];
|
|
|
|
|
/*
|
|
|
|
|
* Append any values from the nil terminated varargs
|
|
|
|
|
*/
|
|
|
|
|
while (tmp != nil)
|
|
|
|
|
{
|
|
|
|
|
if ([tmp isKindOfClass: NSStringClass] == NO)
|
|
|
|
|
{
|
|
|
|
|
if ([tmp isKindOfClass: [NSData class]] == YES)
|
|
|
|
|
{
|
|
|
|
|
[ma addObject: tmp];
|
|
|
|
|
[s appendString: @"'?'''?'"]; // Marker.
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
[s appendString: [self quote: tmp]];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
[s appendString: tmp];
|
|
|
|
|
}
|
|
|
|
|
tmp = va_arg(args, NSString*);
|
|
|
|
|
}
|
|
|
|
|
stmt = s;
|
|
|
|
|
}
|
|
|
|
|
[ma insertObject: stmt atIndex: 0];
|
|
|
|
|
[arp release];
|
|
|
|
|
return ma;
|
|
|
|
|
}
|
|
|
|
|
|
2004-04-26 15:13:27 +00:00
|
|
|
|
- (NSMutableArray*) query: (NSString*)stmt, ...
|
|
|
|
|
{
|
|
|
|
|
va_list ap;
|
|
|
|
|
NSMutableArray *result = nil;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* First check validity and concatenate parts of the query.
|
|
|
|
|
*/
|
|
|
|
|
va_start (ap, stmt);
|
2015-02-26 16:14:51 +00:00
|
|
|
|
stmt = [[self prepare: stmt args: ap] objectAtIndex: 0];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
va_end (ap);
|
|
|
|
|
|
|
|
|
|
result = [self simpleQuery: stmt];
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSMutableArray*) query: (NSString*)stmt with: (NSDictionary*)values
|
|
|
|
|
{
|
|
|
|
|
NSMutableArray *result = nil;
|
|
|
|
|
|
|
|
|
|
stmt = [[self _substitute: stmt with: values] objectAtIndex: 0];
|
|
|
|
|
|
|
|
|
|
result = [self simpleQuery: stmt];
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2004-10-08 09:29:00 +00:00
|
|
|
|
- (NSString*) quote: (id)obj
|
|
|
|
|
{
|
2004-04-26 15:13:27 +00:00
|
|
|
|
/**
|
2004-10-07 09:30:14 +00:00
|
|
|
|
* For a nil object, we return NULL.
|
2004-04-26 15:13:27 +00:00
|
|
|
|
*/
|
2005-09-28 06:20:43 +00:00
|
|
|
|
if (obj == nil || obj == null)
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
|
|
|
|
return @"NULL";
|
|
|
|
|
}
|
2004-10-07 09:30:14 +00:00
|
|
|
|
else if ([obj isKindOfClass: NSStringClass] == NO)
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
2004-10-07 09:30:14 +00:00
|
|
|
|
/**
|
|
|
|
|
* For a number, we simply convert directly to a string.
|
|
|
|
|
*/
|
|
|
|
|
if ([obj isKindOfClass: [NSNumber class]] == YES)
|
|
|
|
|
{
|
|
|
|
|
return [obj description];
|
|
|
|
|
}
|
2004-04-26 15:13:27 +00:00
|
|
|
|
|
2004-10-07 09:30:14 +00:00
|
|
|
|
/**
|
|
|
|
|
* For a date, we convert to the text format used by the database,
|
|
|
|
|
* and add leading and trailing quotes.
|
|
|
|
|
*/
|
|
|
|
|
if ([obj isKindOfClass: NSDateClass] == YES)
|
|
|
|
|
{
|
|
|
|
|
return [obj descriptionWithCalendarFormat:
|
|
|
|
|
@"'%Y-%m-%d %H:%M:%S.%F %z'" timeZone: nil locale: nil];
|
|
|
|
|
}
|
2004-04-26 15:13:27 +00:00
|
|
|
|
|
2004-10-07 09:30:14 +00:00
|
|
|
|
/**
|
|
|
|
|
* For a data object, we don't quote ... the other parts of the code
|
|
|
|
|
* need to know they have an NSData object and pass it on unchanged
|
|
|
|
|
* to the -backendExecute: method.
|
|
|
|
|
*/
|
|
|
|
|
if ([obj isKindOfClass: [NSData class]] == YES)
|
|
|
|
|
{
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
|
2005-09-28 06:20:43 +00:00
|
|
|
|
/**
|
|
|
|
|
* Just in case an NSNull subclass has been created by someone.
|
|
|
|
|
* The normal NSNull instance should have been handled earlier.
|
|
|
|
|
*/
|
|
|
|
|
if ([obj isKindOfClass: [NSNull class]] == YES)
|
|
|
|
|
{
|
|
|
|
|
return @"NULL";
|
|
|
|
|
}
|
|
|
|
|
|
2006-02-18 17:10:56 +00:00
|
|
|
|
/**
|
2006-02-22 11:15:16 +00:00
|
|
|
|
* For an NSArray or NSSet, we produce a bracketed list of the
|
2006-02-18 17:10:56 +00:00
|
|
|
|
* (quoted) objects in the array.
|
|
|
|
|
*/
|
2006-02-22 11:15:16 +00:00
|
|
|
|
if ([obj isKindOfClass: NSArrayClass] == YES ||
|
|
|
|
|
[obj isKindOfClass: NSSetClass] == YES)
|
2006-02-18 17:10:56 +00:00
|
|
|
|
{
|
|
|
|
|
NSMutableString *ms = [NSMutableString stringWithCapacity: 100];
|
2006-02-22 11:15:16 +00:00
|
|
|
|
NSEnumerator *enumerator = [obj objectEnumerator];
|
|
|
|
|
id value = [enumerator nextObject];
|
2006-02-18 17:10:56 +00:00
|
|
|
|
|
|
|
|
|
[ms appendString: @"("];
|
2006-02-22 11:15:16 +00:00
|
|
|
|
if (value != nil)
|
2006-02-18 17:10:56 +00:00
|
|
|
|
{
|
2006-02-22 11:15:16 +00:00
|
|
|
|
[ms appendString: [self quote: value]];
|
|
|
|
|
}
|
|
|
|
|
while ((value = [enumerator nextObject]) != nil)
|
|
|
|
|
{
|
|
|
|
|
[ms appendString: @","];
|
|
|
|
|
[ms appendString: [self quote: value]];
|
2006-02-18 17:10:56 +00:00
|
|
|
|
}
|
|
|
|
|
[ms appendString: @")"];
|
|
|
|
|
return ms;
|
|
|
|
|
}
|
|
|
|
|
|
2004-10-07 09:30:14 +00:00
|
|
|
|
/**
|
|
|
|
|
* For any other type of data, we just produce a quoted string
|
|
|
|
|
* representation of the objects description.
|
|
|
|
|
*/
|
|
|
|
|
obj = [obj description];
|
|
|
|
|
}
|
2004-04-26 15:13:27 +00:00
|
|
|
|
|
|
|
|
|
/* Get a string description of the object. */
|
2006-05-25 11:34:03 +00:00
|
|
|
|
obj = [self quoteString: obj];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
|
2004-10-08 09:29:00 +00:00
|
|
|
|
- (NSString*) quotef: (NSString*)fmt, ...
|
|
|
|
|
{
|
2006-05-25 11:34:03 +00:00
|
|
|
|
va_list ap;
|
|
|
|
|
NSString *str;
|
|
|
|
|
NSString *quoted;
|
2004-10-08 09:29:00 +00:00
|
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2006-05-25 11:34:03 +00:00
|
|
|
|
str = [[NSString allocWithZone: NSDefaultMallocZone()]
|
2004-10-08 09:29:00 +00:00
|
|
|
|
initWithFormat: fmt arguments: ap];
|
|
|
|
|
va_end(ap);
|
|
|
|
|
|
2006-05-25 11:34:03 +00:00
|
|
|
|
quoted = [self quoteString: str];
|
2009-11-18 11:11:29 +00:00
|
|
|
|
[str release];
|
2006-05-25 11:34:03 +00:00
|
|
|
|
return quoted;
|
2004-10-08 09:29:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-11 17:16:14 +00:00
|
|
|
|
- (NSMutableString*) quoteArray: (NSArray *)a
|
|
|
|
|
toString: (NSMutableString *)s
|
|
|
|
|
quotingStrings: (BOOL)q
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSGenericException
|
|
|
|
|
format: @"%@ not supported for this database", NSStringFromSelector(_cmd)];
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
2008-11-12 05:52:38 +00:00
|
|
|
|
- (NSString*) quoteBigInteger: (int64_t)i
|
|
|
|
|
{
|
2013-03-05 15:16:07 +00:00
|
|
|
|
return [NSString stringWithFormat: @"%"PRId64, i];
|
2008-11-12 05:52:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2004-04-26 15:13:27 +00:00
|
|
|
|
- (NSString*) quoteCString: (const char *)s
|
|
|
|
|
{
|
2006-05-25 11:34:03 +00:00
|
|
|
|
NSString *str;
|
|
|
|
|
NSString *quoted;
|
2004-04-26 15:13:27 +00:00
|
|
|
|
|
2006-05-25 11:34:03 +00:00
|
|
|
|
if (s == 0)
|
|
|
|
|
{
|
|
|
|
|
s = "";
|
|
|
|
|
}
|
|
|
|
|
str = [[NSString alloc] initWithCString: s];
|
|
|
|
|
quoted = [self quoteString: str];
|
2009-11-18 11:11:29 +00:00
|
|
|
|
[str release];
|
2006-05-25 11:34:03 +00:00
|
|
|
|
return quoted;
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSString*) quoteChar: (char)c
|
|
|
|
|
{
|
2006-05-25 11:34:03 +00:00
|
|
|
|
NSString *str;
|
|
|
|
|
NSString *quoted;
|
|
|
|
|
|
2004-04-26 15:13:27 +00:00
|
|
|
|
if (c == 0)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"Attempt to quote a nul character in -quoteChar:"];
|
|
|
|
|
}
|
2006-05-25 11:34:03 +00:00
|
|
|
|
str = [[NSString alloc] initWithFormat: @"%c", c];
|
|
|
|
|
quoted = [self quoteString: str];
|
2009-11-18 11:11:29 +00:00
|
|
|
|
[str release];
|
2006-05-25 11:34:03 +00:00
|
|
|
|
return quoted;
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSString*) quoteFloat: (float)f
|
|
|
|
|
{
|
|
|
|
|
return [NSString stringWithFormat: @"%f", f];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSString*) quoteInteger: (int)i
|
|
|
|
|
{
|
|
|
|
|
return [NSString stringWithFormat: @"%d", i];
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-25 11:34:03 +00:00
|
|
|
|
- (NSString*) quoteString: (NSString *)s
|
|
|
|
|
{
|
|
|
|
|
static NSCharacterSet *special = nil;
|
|
|
|
|
NSMutableString *m;
|
|
|
|
|
NSRange r;
|
|
|
|
|
unsigned l;
|
|
|
|
|
|
|
|
|
|
if (special == nil)
|
|
|
|
|
{
|
2006-08-03 00:14:22 +00:00
|
|
|
|
NSString *stemp;
|
2006-05-25 11:34:03 +00:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* NB. length of C string is 2, so we include a nul character as a
|
|
|
|
|
* special.
|
|
|
|
|
*/
|
2006-08-03 00:14:22 +00:00
|
|
|
|
stemp = [[NSString alloc] initWithBytes: "'"
|
|
|
|
|
length: 2
|
|
|
|
|
encoding: NSASCIIStringEncoding];
|
|
|
|
|
special = [NSCharacterSet characterSetWithCharactersInString: stemp];
|
2009-11-18 11:11:29 +00:00
|
|
|
|
[stemp release];
|
|
|
|
|
[special retain];
|
2006-05-25 11:34:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Step through string removing nul characters
|
|
|
|
|
* and escaping quote characters as required.
|
|
|
|
|
*/
|
2009-11-18 11:11:29 +00:00
|
|
|
|
m = [[s mutableCopy] autorelease];
|
2006-05-25 11:34:03 +00:00
|
|
|
|
l = [m length];
|
|
|
|
|
r = NSMakeRange(0, l);
|
|
|
|
|
r = [m rangeOfCharacterFromSet: special options: NSLiteralSearch range: r];
|
|
|
|
|
while (r.length > 0)
|
|
|
|
|
{
|
|
|
|
|
unichar c = [m characterAtIndex: r.location];
|
|
|
|
|
|
|
|
|
|
if (c == 0)
|
|
|
|
|
{
|
|
|
|
|
r.length = 1;
|
|
|
|
|
[m replaceCharactersInRange: r withString: @""];
|
|
|
|
|
l--;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
r.length = 0;
|
|
|
|
|
[m replaceCharactersInRange: r withString: @"'"];
|
|
|
|
|
l++;
|
|
|
|
|
r.location += 2;
|
|
|
|
|
}
|
|
|
|
|
r = NSMakeRange(r.location, l - r.location);
|
|
|
|
|
r = [m rangeOfCharacterFromSet: special
|
|
|
|
|
options: NSLiteralSearch
|
|
|
|
|
range: r];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Add quoting around it. */
|
|
|
|
|
[m replaceCharactersInRange: NSMakeRange(0, 0) withString: @"'"];
|
|
|
|
|
[m appendString: @"'"];
|
|
|
|
|
return m;
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-06 15:39:11 +00:00
|
|
|
|
- (oneway void) release
|
2006-09-10 13:37:23 +00:00
|
|
|
|
{
|
|
|
|
|
/* We lock the table while checking, to prevent
|
|
|
|
|
* another thread from grabbing this object while we are
|
|
|
|
|
* checking it.
|
|
|
|
|
* If we are going to deallocate the object, we first remove
|
|
|
|
|
* it from the table so that no other thread will find it
|
|
|
|
|
* and try to use it while it is being deallocated.
|
|
|
|
|
*/
|
2014-06-19 21:26:25 +00:00
|
|
|
|
[clientsLock lock];
|
2006-09-10 13:37:23 +00:00
|
|
|
|
if (NSDecrementExtraRefCountWasZero(self))
|
|
|
|
|
{
|
2014-06-20 15:39:25 +00:00
|
|
|
|
if (nil != _pool)
|
|
|
|
|
{
|
2015-05-28 12:57:08 +00:00
|
|
|
|
[_pool _swallowClient: self withRetain: NO];
|
2014-06-20 15:39:25 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
[self dealloc];
|
|
|
|
|
}
|
2006-09-10 13:37:23 +00:00
|
|
|
|
}
|
2014-06-19 21:26:25 +00:00
|
|
|
|
[clientsLock unlock];
|
2006-09-10 13:37:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-06 10:56:39 +00:00
|
|
|
|
- (id) retain
|
|
|
|
|
{
|
|
|
|
|
NSIncrementExtraRefCount(self);
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
2004-04-26 15:13:27 +00:00
|
|
|
|
- (void) rollback
|
|
|
|
|
{
|
|
|
|
|
[lock lock];
|
2011-09-16 07:25:33 +00:00
|
|
|
|
if (NO == _inTransaction)
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
2011-09-16 07:25:33 +00:00
|
|
|
|
[lock unlock]; // Not in a transaction ... nothing to do.
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Since we are in a transaction we must be doubly locked right now,
|
|
|
|
|
* so we unlock once, and we still have the lock (which was locked
|
|
|
|
|
* in the earlier call to the -begin method).
|
|
|
|
|
*/
|
|
|
|
|
[lock unlock];
|
|
|
|
|
_inTransaction = NO;
|
|
|
|
|
NS_DURING
|
|
|
|
|
{
|
|
|
|
|
[self simpleExecute: rollbackStatement];
|
2007-07-07 07:23:40 +00:00
|
|
|
|
[_statements removeAllObjects];
|
2011-09-16 07:25:33 +00:00
|
|
|
|
[lock unlock]; // Locked by -begin
|
|
|
|
|
}
|
|
|
|
|
NS_HANDLER
|
|
|
|
|
{
|
|
|
|
|
[_statements removeAllObjects];
|
|
|
|
|
[lock unlock]; // Locked by -begin
|
|
|
|
|
[localException raise];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
2011-09-16 07:25:33 +00:00
|
|
|
|
NS_ENDHANDLER
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) setDatabase: (NSString*)s
|
|
|
|
|
{
|
2014-05-13 11:01:50 +00:00
|
|
|
|
[lock lock];
|
|
|
|
|
NS_DURING
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
2014-05-13 11:01:50 +00:00
|
|
|
|
if ([s isEqual: _database] == NO)
|
|
|
|
|
{
|
2014-09-10 11:49:40 +00:00
|
|
|
|
if (YES == connected)
|
2014-05-13 11:01:50 +00:00
|
|
|
|
{
|
|
|
|
|
[self disconnect];
|
|
|
|
|
}
|
|
|
|
|
s = [s copy];
|
|
|
|
|
[_database release];
|
|
|
|
|
_database = s;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
NS_HANDLER
|
|
|
|
|
{
|
|
|
|
|
[lock unlock];
|
|
|
|
|
[localException raise];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
2014-05-13 11:01:50 +00:00
|
|
|
|
NS_ENDHANDLER
|
|
|
|
|
[lock unlock];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) setName: (NSString*)s
|
|
|
|
|
{
|
2014-05-13 11:01:50 +00:00
|
|
|
|
[lock lock];
|
|
|
|
|
NS_DURING
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
2014-05-13 11:01:50 +00:00
|
|
|
|
if ([s isEqual: _name] == NO)
|
|
|
|
|
{
|
2014-06-19 21:26:25 +00:00
|
|
|
|
[clientsLock lock];
|
2014-06-20 15:39:25 +00:00
|
|
|
|
if (nil == _pool)
|
2014-05-13 11:01:50 +00:00
|
|
|
|
{
|
2014-06-19 21:26:25 +00:00
|
|
|
|
if (NSMapGet(clientsMap, s) != 0)
|
2014-05-13 11:01:50 +00:00
|
|
|
|
{
|
2014-06-19 21:26:25 +00:00
|
|
|
|
[clientsLock unlock];
|
|
|
|
|
[lock unlock];
|
|
|
|
|
if ([self debugging] > 0)
|
|
|
|
|
{
|
|
|
|
|
[self
|
|
|
|
|
debug: @"Error attempt to re-use client name %@", s];
|
|
|
|
|
}
|
|
|
|
|
NS_VOIDRETURN;
|
2014-05-13 11:01:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-09-10 11:49:40 +00:00
|
|
|
|
if (YES == connected)
|
2014-05-13 11:01:50 +00:00
|
|
|
|
{
|
|
|
|
|
[self disconnect];
|
|
|
|
|
}
|
2014-07-04 13:11:47 +00:00
|
|
|
|
if (_name != nil
|
|
|
|
|
&& (SQLClient*)NSMapGet(clientsMap, (void*)_name) == self)
|
2014-05-13 11:01:50 +00:00
|
|
|
|
{
|
|
|
|
|
NSMapRemove(clientsMap, (void*)_name);
|
|
|
|
|
}
|
|
|
|
|
s = [s copy];
|
|
|
|
|
[_name release];
|
|
|
|
|
_name = s;
|
|
|
|
|
[_client release];
|
|
|
|
|
_client = [[[NSProcessInfo processInfo] globallyUniqueString] retain];
|
2014-06-20 15:39:25 +00:00
|
|
|
|
if (nil == _pool && _name != nil)
|
2014-06-19 21:26:25 +00:00
|
|
|
|
{
|
|
|
|
|
NSMapInsert(clientsMap, (void*)_name, (void*)self);
|
|
|
|
|
}
|
|
|
|
|
[clientsLock unlock];
|
2005-08-03 07:03:28 +00:00
|
|
|
|
}
|
2014-05-13 11:01:50 +00:00
|
|
|
|
}
|
|
|
|
|
NS_HANDLER
|
|
|
|
|
{
|
2004-04-26 15:13:27 +00:00
|
|
|
|
[lock unlock];
|
2014-05-13 11:01:50 +00:00
|
|
|
|
[localException raise];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
2014-05-13 11:01:50 +00:00
|
|
|
|
NS_ENDHANDLER
|
|
|
|
|
[lock unlock];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) setPassword: (NSString*)s
|
|
|
|
|
{
|
2014-05-13 11:01:50 +00:00
|
|
|
|
[lock lock];
|
|
|
|
|
NS_DURING
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
2014-05-13 11:01:50 +00:00
|
|
|
|
if ([s isEqual: _password] == NO)
|
|
|
|
|
{
|
2014-09-10 11:49:40 +00:00
|
|
|
|
if (YES == connected)
|
2014-05-13 11:01:50 +00:00
|
|
|
|
{
|
|
|
|
|
[self disconnect];
|
|
|
|
|
}
|
|
|
|
|
s = [s copy];
|
|
|
|
|
[_password release];
|
|
|
|
|
_password = s;
|
|
|
|
|
}
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
2014-05-13 11:01:50 +00:00
|
|
|
|
NS_HANDLER
|
|
|
|
|
{
|
|
|
|
|
[lock unlock];
|
|
|
|
|
[localException raise];
|
|
|
|
|
}
|
|
|
|
|
NS_ENDHANDLER
|
|
|
|
|
[lock unlock];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-04-10 15:03:55 +00:00
|
|
|
|
- (void) setShouldTrim: (BOOL)aFlag
|
|
|
|
|
{
|
|
|
|
|
_shouldTrim = (YES == aFlag) ? YES : NO;
|
|
|
|
|
}
|
|
|
|
|
|
2004-04-26 15:13:27 +00:00
|
|
|
|
- (void) setUser: (NSString*)s
|
|
|
|
|
{
|
2014-05-13 11:01:50 +00:00
|
|
|
|
[lock lock];
|
|
|
|
|
NS_DURING
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
2014-09-10 11:36:20 +00:00
|
|
|
|
if ([s isEqual: _user] == NO)
|
2014-05-13 11:01:50 +00:00
|
|
|
|
{
|
2014-09-10 11:49:40 +00:00
|
|
|
|
if (YES == connected)
|
2014-05-13 11:01:50 +00:00
|
|
|
|
{
|
|
|
|
|
[self disconnect];
|
|
|
|
|
}
|
|
|
|
|
s = [s copy];
|
|
|
|
|
[_user release];
|
|
|
|
|
_user = s;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
NS_HANDLER
|
|
|
|
|
{
|
|
|
|
|
[lock unlock];
|
|
|
|
|
[localException raise];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
2014-05-13 11:01:50 +00:00
|
|
|
|
NS_ENDHANDLER
|
|
|
|
|
[lock unlock];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-10-19 18:41:36 +00:00
|
|
|
|
- (NSInteger) simpleExecute: (NSArray*)info
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
2012-10-19 18:41:36 +00:00
|
|
|
|
NSInteger result;
|
2013-01-31 09:04:35 +00:00
|
|
|
|
NSString *debug = nil;
|
2004-09-17 14:54:56 +00:00
|
|
|
|
|
2004-10-07 09:30:14 +00:00
|
|
|
|
[lock lock];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
NS_DURING
|
|
|
|
|
{
|
2012-11-29 11:40:04 +00:00
|
|
|
|
NSString *statement;
|
|
|
|
|
BOOL isCommit = NO;
|
|
|
|
|
BOOL isRollback = NO;
|
|
|
|
|
|
|
|
|
|
statement = [info objectAtIndex: 0];
|
|
|
|
|
|
|
|
|
|
if ([statement isEqualToString: commitString])
|
|
|
|
|
{
|
|
|
|
|
isCommit = YES;
|
|
|
|
|
}
|
|
|
|
|
if ([statement isEqualToString: rollbackString])
|
|
|
|
|
{
|
|
|
|
|
isRollback = YES;
|
|
|
|
|
}
|
2004-05-07 08:16:16 +00:00
|
|
|
|
|
2014-06-20 05:15:24 +00:00
|
|
|
|
_lastStart = GSTickerTimeNow();
|
2012-10-19 18:41:36 +00:00
|
|
|
|
result = [self backendExecute: info];
|
2005-11-14 20:37:33 +00:00
|
|
|
|
_lastOperation = GSTickerTimeNow();
|
2004-09-17 14:54:56 +00:00
|
|
|
|
[_statements addObject: statement];
|
2004-05-07 08:16:16 +00:00
|
|
|
|
if (_duration >= 0)
|
|
|
|
|
{
|
|
|
|
|
NSTimeInterval d;
|
|
|
|
|
|
2014-06-20 05:15:24 +00:00
|
|
|
|
d = _lastOperation - _lastStart;
|
2004-05-07 08:16:16 +00:00
|
|
|
|
if (d >= _duration)
|
|
|
|
|
{
|
2004-10-09 12:39:36 +00:00
|
|
|
|
if (isCommit || isRollback)
|
2004-09-17 14:54:56 +00:00
|
|
|
|
{
|
2013-01-31 09:04:35 +00:00
|
|
|
|
NSEnumerator *e = [_statements objectEnumerator];
|
|
|
|
|
NSMutableString *m;
|
|
|
|
|
|
2004-10-09 12:39:36 +00:00
|
|
|
|
if (isCommit)
|
2004-09-17 14:54:56 +00:00
|
|
|
|
{
|
2013-01-31 09:04:35 +00:00
|
|
|
|
m = [NSMutableString stringWithFormat:
|
|
|
|
|
@"Duration %g for transaction commit ...\n", d];
|
2004-09-17 14:54:56 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-01-31 09:04:35 +00:00
|
|
|
|
m = [NSMutableString stringWithFormat:
|
|
|
|
|
@"Duration %g for transaction rollback ...\n", d];
|
2004-09-17 14:54:56 +00:00
|
|
|
|
}
|
|
|
|
|
while ((statement = [e nextObject]) != nil)
|
|
|
|
|
{
|
2013-01-31 09:04:35 +00:00
|
|
|
|
[m appendFormat: @" %@;\n", statement];
|
2004-09-17 14:54:56 +00:00
|
|
|
|
}
|
2013-01-31 09:04:35 +00:00
|
|
|
|
debug = m;
|
2004-09-17 14:54:56 +00:00
|
|
|
|
}
|
|
|
|
|
else if ([self debugging] > 1)
|
2004-05-07 08:16:16 +00:00
|
|
|
|
{
|
2004-09-17 14:54:56 +00:00
|
|
|
|
/*
|
|
|
|
|
* For higher debug levels, we log data objects as well
|
|
|
|
|
* as the query string, otherwise we omit them.
|
|
|
|
|
*/
|
2013-01-31 09:04:35 +00:00
|
|
|
|
debug = [NSString stringWithFormat:
|
|
|
|
|
@"Duration %g for statement %@", d, info];
|
2004-05-07 08:16:16 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-01-31 09:04:35 +00:00
|
|
|
|
debug = [NSString stringWithFormat:
|
|
|
|
|
@"Duration %g for statement %@", d, statement];
|
2004-05-07 08:16:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-07-07 07:23:40 +00:00
|
|
|
|
if (_inTransaction == NO)
|
2004-09-17 14:54:56 +00:00
|
|
|
|
{
|
|
|
|
|
[_statements removeAllObjects];
|
|
|
|
|
}
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
NS_HANDLER
|
|
|
|
|
{
|
2012-10-19 18:41:36 +00:00
|
|
|
|
result = -1;
|
2007-07-07 07:23:40 +00:00
|
|
|
|
if (_inTransaction == NO)
|
2004-09-17 14:54:56 +00:00
|
|
|
|
{
|
|
|
|
|
[_statements removeAllObjects];
|
|
|
|
|
}
|
2004-04-26 15:13:27 +00:00
|
|
|
|
[lock unlock];
|
|
|
|
|
[localException raise];
|
|
|
|
|
}
|
|
|
|
|
NS_ENDHANDLER
|
|
|
|
|
[lock unlock];
|
2013-01-31 09:04:35 +00:00
|
|
|
|
if (nil != debug)
|
|
|
|
|
{
|
|
|
|
|
[self debug: @"%@", debug];
|
|
|
|
|
}
|
2012-10-19 18:41:36 +00:00
|
|
|
|
return result;
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSMutableArray*) simpleQuery: (NSString*)stmt
|
2007-03-08 17:12:55 +00:00
|
|
|
|
{
|
2008-03-03 14:10:54 +00:00
|
|
|
|
return [self simpleQuery: stmt recordType: rClass listType: aClass];
|
2007-03-08 17:12:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-03-03 14:10:54 +00:00
|
|
|
|
- (NSMutableArray*) simpleQuery: (NSString*)stmt
|
|
|
|
|
recordType: (id)rtype
|
|
|
|
|
listType: (id)ltype
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
|
|
|
|
NSMutableArray *result = nil;
|
2013-01-31 09:04:35 +00:00
|
|
|
|
NSString *debug = nil;
|
2004-04-26 15:13:27 +00:00
|
|
|
|
|
2008-03-03 14:10:54 +00:00
|
|
|
|
if (rtype == 0) rtype = rClass;
|
|
|
|
|
if (ltype == 0) ltype = aClass;
|
2004-04-26 15:13:27 +00:00
|
|
|
|
[lock lock];
|
|
|
|
|
NS_DURING
|
|
|
|
|
{
|
2014-06-20 05:15:24 +00:00
|
|
|
|
_lastStart = GSTickerTimeNow();
|
2008-03-03 14:10:54 +00:00
|
|
|
|
result = [self backendQuery: stmt recordType: rtype listType: ltype];
|
2005-11-14 20:37:33 +00:00
|
|
|
|
_lastOperation = GSTickerTimeNow();
|
2004-05-07 08:16:16 +00:00
|
|
|
|
if (_duration >= 0)
|
|
|
|
|
{
|
|
|
|
|
NSTimeInterval d;
|
|
|
|
|
|
2014-06-20 05:15:24 +00:00
|
|
|
|
d = _lastOperation - _lastStart;
|
2004-05-07 08:16:16 +00:00
|
|
|
|
if (d >= _duration)
|
|
|
|
|
{
|
2013-01-31 09:04:35 +00:00
|
|
|
|
debug = [NSString stringWithFormat:
|
|
|
|
|
@"Duration %g for query %@", d, stmt];
|
2004-05-07 08:16:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
NS_HANDLER
|
|
|
|
|
{
|
|
|
|
|
[lock unlock];
|
|
|
|
|
[localException raise];
|
|
|
|
|
}
|
|
|
|
|
NS_ENDHANDLER
|
|
|
|
|
[lock unlock];
|
2013-01-31 09:04:35 +00:00
|
|
|
|
if (nil != debug)
|
|
|
|
|
{
|
|
|
|
|
[self debug: @"%@", debug];
|
|
|
|
|
}
|
2004-04-26 15:13:27 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-01 11:32:49 +00:00
|
|
|
|
- (void) unlock
|
|
|
|
|
{
|
|
|
|
|
[lock unlock];
|
|
|
|
|
}
|
|
|
|
|
|
2004-04-26 15:13:27 +00:00
|
|
|
|
- (NSString*) user
|
|
|
|
|
{
|
2004-05-07 08:16:16 +00:00
|
|
|
|
return _user;
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation SQLClient (Subclass)
|
|
|
|
|
|
|
|
|
|
- (BOOL) backendConnect
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"Called -%@ without backend bundle loaded",
|
|
|
|
|
NSStringFromSelector(_cmd)];
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) backendDisconnect
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"Called -%@ without backend bundle loaded",
|
|
|
|
|
NSStringFromSelector(_cmd)];
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-19 18:41:36 +00:00
|
|
|
|
- (NSInteger) backendExecute: (NSArray*)info
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"Called -%@ without backend bundle loaded",
|
|
|
|
|
NSStringFromSelector(_cmd)];
|
2012-10-19 18:41:36 +00:00
|
|
|
|
return -1;
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-10-22 15:57:56 +00:00
|
|
|
|
- (void) backendListen: (NSString*)name
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) backendNotify: (NSString*)name payload: (NSString*)more
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"Called -%@ without backend bundle implementation",
|
|
|
|
|
NSStringFromSelector(_cmd)];
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2004-04-26 15:13:27 +00:00
|
|
|
|
- (NSMutableArray*) backendQuery: (NSString*)stmt
|
2007-03-08 17:12:55 +00:00
|
|
|
|
{
|
2008-03-03 14:10:54 +00:00
|
|
|
|
return [self backendQuery: stmt recordType: rClass listType: aClass];
|
2007-03-08 17:12:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-03-03 14:10:54 +00:00
|
|
|
|
- (NSMutableArray*) backendQuery: (NSString*)stmt
|
|
|
|
|
recordType: (id)rtype
|
|
|
|
|
listType: (id)ltype
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"Called -%@ without backend bundle loaded",
|
|
|
|
|
NSStringFromSelector(_cmd)];
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-22 15:57:56 +00:00
|
|
|
|
- (void) backendUnlisten: (NSString*)name
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2004-04-26 15:13:27 +00:00
|
|
|
|
- (unsigned) copyEscapedBLOB: (NSData*)blob into: (void*)buf
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"Called -%@ without backend bundle loaded",
|
|
|
|
|
NSStringFromSelector(_cmd)];
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (const void*) insertBLOBs: (NSArray*)blobs
|
|
|
|
|
intoStatement: (const void*)statement
|
|
|
|
|
length: (unsigned)sLength
|
|
|
|
|
withMarker: (const void*)marker
|
|
|
|
|
length: (unsigned)mLength
|
|
|
|
|
giving: (unsigned*)result
|
|
|
|
|
{
|
|
|
|
|
unsigned count = [blobs count];
|
|
|
|
|
unsigned length = sLength;
|
|
|
|
|
|
|
|
|
|
if (count > 1)
|
|
|
|
|
{
|
|
|
|
|
unsigned i;
|
|
|
|
|
unsigned char *buf;
|
|
|
|
|
unsigned char *ptr;
|
|
|
|
|
const unsigned char *from = (const unsigned char*)statement;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Calculate length of buffer needed.
|
|
|
|
|
*/
|
|
|
|
|
for (i = 1; i < count; i++)
|
|
|
|
|
{
|
|
|
|
|
length += [self lengthOfEscapedBLOB: [blobs objectAtIndex: i]];
|
|
|
|
|
length -= mLength;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buf = NSZoneMalloc(NSDefaultMallocZone(), length + 1);
|
|
|
|
|
[NSData dataWithBytesNoCopy: buf length: length + 1]; // autoreleased
|
|
|
|
|
ptr = buf;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Merge quoted data objects into statement.
|
|
|
|
|
*/
|
|
|
|
|
i = 1;
|
|
|
|
|
from = (unsigned char*)statement;
|
|
|
|
|
while (*from != 0)
|
|
|
|
|
{
|
|
|
|
|
if (*from == *(unsigned char*)marker
|
|
|
|
|
&& memcmp(from, marker, mLength) == 0)
|
|
|
|
|
{
|
|
|
|
|
NSData *d = [blobs objectAtIndex: i++];
|
|
|
|
|
|
|
|
|
|
from += mLength;
|
|
|
|
|
ptr += [self copyEscapedBLOB: d into: ptr];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
*ptr++ = *from++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
*ptr = '\0';
|
|
|
|
|
statement = buf;
|
|
|
|
|
}
|
|
|
|
|
*result = length;
|
|
|
|
|
return statement;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (unsigned) lengthOfEscapedBLOB: (NSData*)blob
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"Called -%@ without backend bundle loaded",
|
|
|
|
|
NSStringFromSelector(_cmd)];
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation SQLClient (Private)
|
|
|
|
|
|
|
|
|
|
- (void) _configure: (NSNotification*)n
|
|
|
|
|
{
|
2014-05-17 09:47:58 +00:00
|
|
|
|
NSDictionary *o;
|
2004-04-26 15:13:27 +00:00
|
|
|
|
NSDictionary *d;
|
|
|
|
|
NSString *s;
|
|
|
|
|
Class c;
|
|
|
|
|
|
2014-05-17 09:47:58 +00:00
|
|
|
|
[lock lock];
|
|
|
|
|
NS_DURING
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
2014-05-17 09:47:58 +00:00
|
|
|
|
o = [n object];
|
|
|
|
|
/*
|
|
|
|
|
* get dictionary containing config info for this client by name.
|
|
|
|
|
*/
|
|
|
|
|
d = [o objectForKey: @"SQLClientReferences"];
|
|
|
|
|
if ([d isKindOfClass: [NSDictionary class]] == NO)
|
|
|
|
|
{
|
|
|
|
|
[self debug: @"Unable to find SQLClientReferences config dictionary"];
|
|
|
|
|
d = nil;
|
|
|
|
|
}
|
|
|
|
|
d = [d objectForKey: _name];
|
|
|
|
|
if ([d isKindOfClass: [NSDictionary class]] == NO)
|
|
|
|
|
{
|
|
|
|
|
[self debug: @"Unable to find config for client '%@'", _name];
|
|
|
|
|
d = nil;
|
|
|
|
|
}
|
2004-04-26 15:13:27 +00:00
|
|
|
|
|
2014-05-17 09:47:58 +00:00
|
|
|
|
s = [d objectForKey: @"ServerType"];
|
|
|
|
|
if ([s isKindOfClass: NSStringClass] == NO)
|
|
|
|
|
{
|
|
|
|
|
s = @"Postgres";
|
|
|
|
|
}
|
2004-04-26 15:13:27 +00:00
|
|
|
|
|
2014-05-17 09:47:58 +00:00
|
|
|
|
c = NSClassFromString([@"SQLClient" stringByAppendingString: s]);
|
2005-02-19 04:20:13 +00:00
|
|
|
|
if (c == nil)
|
2014-05-17 09:47:58 +00:00
|
|
|
|
{
|
|
|
|
|
NSString *path;
|
|
|
|
|
NSBundle *bundle;
|
|
|
|
|
NSArray *paths;
|
|
|
|
|
NSMutableArray *tried;
|
|
|
|
|
unsigned count;
|
|
|
|
|
|
|
|
|
|
paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
|
|
|
|
|
NSAllDomainsMask, YES);
|
|
|
|
|
count = [paths count];
|
|
|
|
|
tried = [NSMutableArray arrayWithCapacity: count];
|
|
|
|
|
while (count-- > 0)
|
|
|
|
|
{
|
|
|
|
|
path = [paths objectAtIndex: count];
|
|
|
|
|
path = [path stringByAppendingPathComponent: @"Bundles"];
|
2014-07-17 08:51:33 +00:00
|
|
|
|
path = [path stringByAppendingPathComponent:
|
|
|
|
|
@"SQLClient"SOVERSION""];
|
2014-05-17 09:47:58 +00:00
|
|
|
|
path = [path stringByAppendingPathComponent: s];
|
|
|
|
|
path = [path stringByAppendingPathExtension: @"bundle"];
|
|
|
|
|
bundle = [NSBundle bundleWithPath: path];
|
|
|
|
|
if (bundle != nil)
|
|
|
|
|
{
|
|
|
|
|
[tried addObject: path];
|
|
|
|
|
if ((c = [bundle principalClass]) != nil)
|
|
|
|
|
{
|
|
|
|
|
break; // Found it.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* Try alternative version with more libraries linked in.
|
|
|
|
|
* In some systems and situations the dynamic linker needs
|
|
|
|
|
* to haved the SQLClient, gnustep-base, and objc libraries
|
|
|
|
|
* explicitly linked into the bundle, but in others it
|
|
|
|
|
* requires them to not be linked. To handle that, we create
|
|
|
|
|
* two versions of each bundle, the seond version has _libs
|
|
|
|
|
* appended to the bundle name, and has the extra libraries linked.
|
|
|
|
|
*/
|
|
|
|
|
path = [path stringByDeletingPathExtension];
|
|
|
|
|
path = [path stringByAppendingString: @"_libs"];
|
|
|
|
|
path = [path stringByAppendingPathExtension: @"bundle"];
|
|
|
|
|
bundle = [NSBundle bundleWithPath: path];
|
|
|
|
|
if (bundle != nil)
|
|
|
|
|
{
|
|
|
|
|
[tried addObject: path];
|
|
|
|
|
if ((c = [bundle principalClass]) != nil)
|
|
|
|
|
{
|
|
|
|
|
break; // Found it.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (c == nil)
|
|
|
|
|
{
|
|
|
|
|
if ([tried count] == 0)
|
|
|
|
|
{
|
|
|
|
|
[self debug: @"unable to load bundle for '%@' server type"
|
|
|
|
|
@" ... failed to locate bundle in %@", s, paths];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-05-19 09:31:02 +00:00
|
|
|
|
[self debug: @"unable to load backend class for '%@' server"
|
|
|
|
|
@" type ... dynamic library load failed in %@", s, tried];
|
2014-05-17 09:47:58 +00:00
|
|
|
|
}
|
2014-05-19 09:31:02 +00:00
|
|
|
|
[lock unlock];
|
|
|
|
|
NS_VOIDRETURN;
|
2014-05-17 09:47:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (c != [self class])
|
|
|
|
|
{
|
2014-09-10 11:49:40 +00:00
|
|
|
|
if (YES == connected)
|
|
|
|
|
{
|
|
|
|
|
[self disconnect];
|
|
|
|
|
}
|
2005-08-03 05:39:29 +00:00
|
|
|
|
#ifdef GNUSTEP
|
2014-05-17 09:47:58 +00:00
|
|
|
|
GSDebugAllocationRemove(object_getClass(self), self);
|
2005-08-03 05:39:29 +00:00
|
|
|
|
#endif
|
2014-05-17 09:47:58 +00:00
|
|
|
|
object_setClass(self, c);
|
2005-08-03 05:39:29 +00:00
|
|
|
|
#ifdef GNUSTEP
|
2014-05-17 09:47:58 +00:00
|
|
|
|
GSDebugAllocationAdd(object_getClass(self), self);
|
2005-08-03 05:39:29 +00:00
|
|
|
|
#endif
|
2014-05-17 09:47:58 +00:00
|
|
|
|
}
|
2004-04-26 15:13:27 +00:00
|
|
|
|
|
2014-05-17 09:47:58 +00:00
|
|
|
|
s = [d objectForKey: @"Database"];
|
2004-10-07 09:30:14 +00:00
|
|
|
|
if ([s isKindOfClass: NSStringClass] == NO)
|
2014-05-17 09:47:58 +00:00
|
|
|
|
{
|
|
|
|
|
s = [o objectForKey: @"Database"];
|
|
|
|
|
if ([s isKindOfClass: NSStringClass] == NO)
|
|
|
|
|
{
|
|
|
|
|
s = nil;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
[self setDatabase: s];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
|
2014-05-17 09:47:58 +00:00
|
|
|
|
s = [d objectForKey: @"User"];
|
2004-10-07 09:30:14 +00:00
|
|
|
|
if ([s isKindOfClass: NSStringClass] == NO)
|
2014-05-17 09:47:58 +00:00
|
|
|
|
{
|
|
|
|
|
s = [o objectForKey: @"User"];
|
|
|
|
|
if ([s isKindOfClass: NSStringClass] == NO)
|
|
|
|
|
{
|
|
|
|
|
s = @"";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
[self setUser: s];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
|
2014-05-17 09:47:58 +00:00
|
|
|
|
s = [d objectForKey: @"Password"];
|
2004-10-07 09:30:14 +00:00
|
|
|
|
if ([s isKindOfClass: NSStringClass] == NO)
|
2014-05-17 09:47:58 +00:00
|
|
|
|
{
|
|
|
|
|
s = [o objectForKey: @"Password"];
|
|
|
|
|
if ([s isKindOfClass: NSStringClass] == NO)
|
|
|
|
|
{
|
|
|
|
|
s = @"";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
[self setPassword: s];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
2014-05-17 09:47:58 +00:00
|
|
|
|
NS_HANDLER
|
|
|
|
|
{
|
|
|
|
|
[lock unlock];
|
|
|
|
|
[localException raise];
|
|
|
|
|
}
|
|
|
|
|
NS_ENDHANDLER
|
|
|
|
|
[lock unlock];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-07 09:15:16 +00:00
|
|
|
|
- (NSRecursiveLock*) _lock
|
|
|
|
|
{
|
|
|
|
|
return lock;
|
|
|
|
|
}
|
|
|
|
|
|
2008-02-21 16:23:23 +00:00
|
|
|
|
- (void) _populateCache: (CacheQuery*)a
|
|
|
|
|
{
|
|
|
|
|
GSCache *cache;
|
|
|
|
|
id result;
|
|
|
|
|
|
2015-05-29 09:33:03 +00:00
|
|
|
|
result = [self simpleQuery: a->query
|
|
|
|
|
recordType: a->recordType
|
|
|
|
|
listType: a->listType];
|
2008-02-21 16:23:23 +00:00
|
|
|
|
cache = [self cache];
|
|
|
|
|
[cache setObject: result
|
|
|
|
|
forKey: a->query
|
|
|
|
|
lifetime: a->lifetime];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) _recordMainThread
|
|
|
|
|
{
|
|
|
|
|
mainThread = [NSThread currentThread];
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-08 08:07:06 +00:00
|
|
|
|
- (NSMutableArray*) _substitute: (NSString*)str with: (NSDictionary*)vals
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
|
|
|
|
unsigned int l = [str length];
|
|
|
|
|
NSRange r;
|
|
|
|
|
NSMutableArray *ma = [NSMutableArray arrayWithCapacity: 2];
|
2009-11-18 11:11:29 +00:00
|
|
|
|
NSAutoreleasePool *arp = [NSAutoreleasePool new];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
|
|
|
|
|
if (l < 2)
|
|
|
|
|
{
|
|
|
|
|
[ma addObject: str]; // Can't contain a {...} sequence
|
|
|
|
|
}
|
|
|
|
|
else if ((r = [str rangeOfString: @"{"]).length == 0)
|
|
|
|
|
{
|
|
|
|
|
[ma addObject: str]; // No '{' markup
|
|
|
|
|
}
|
|
|
|
|
else if (l - r.location < 2)
|
|
|
|
|
{
|
|
|
|
|
[ma addObject: str]; // Can't contain a {...} sequence
|
|
|
|
|
}
|
|
|
|
|
else if ([str rangeOfString: @"}" options: NSLiteralSearch
|
|
|
|
|
range: NSMakeRange(r.location, l - r.location)].length == 0
|
|
|
|
|
&& [str rangeOfString: @"{{" options: NSLiteralSearch
|
|
|
|
|
range: NSMakeRange(0, l)].length == 0)
|
|
|
|
|
{
|
|
|
|
|
[ma addObject: str]; // No closing '}' or repeated '{{'
|
|
|
|
|
}
|
|
|
|
|
else if (r.length == 0)
|
|
|
|
|
{
|
|
|
|
|
[ma addObject: str]; // Nothing to do.
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2009-11-18 11:11:29 +00:00
|
|
|
|
NSMutableString *mtext = [[str mutableCopy] autorelease];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Replace {FieldName} with the value of the field
|
|
|
|
|
*/
|
|
|
|
|
while (r.length > 0)
|
|
|
|
|
{
|
|
|
|
|
unsigned pos = r.location;
|
|
|
|
|
unsigned nxt;
|
|
|
|
|
unsigned vLength;
|
|
|
|
|
NSArray *a;
|
|
|
|
|
NSRange s;
|
|
|
|
|
NSString *v;
|
|
|
|
|
NSString *alt;
|
2005-07-07 21:11:04 +00:00
|
|
|
|
id o;
|
2004-04-26 15:13:27 +00:00
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
|
|
r.length = l - pos;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* If the length of the string from the '{' onwards is less than two,
|
|
|
|
|
* there is nothing to do and we can end processing.
|
|
|
|
|
*/
|
|
|
|
|
if (r.length < 2)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ([mtext characterAtIndex: r.location + 1] == '{')
|
|
|
|
|
{
|
|
|
|
|
// Got '{{' ... remove one of them.
|
|
|
|
|
r.length = 1;
|
|
|
|
|
[mtext replaceCharactersInRange: r withString: @""];
|
|
|
|
|
l--;
|
|
|
|
|
r.location++;
|
|
|
|
|
r.length = l - r.location;
|
|
|
|
|
r = [mtext rangeOfString: @"{"
|
|
|
|
|
options: NSLiteralSearch
|
|
|
|
|
range: r];
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r = [mtext rangeOfString: @"}"
|
|
|
|
|
options: NSLiteralSearch
|
|
|
|
|
range: r];
|
|
|
|
|
if (r.length == 0)
|
|
|
|
|
{
|
|
|
|
|
break; // No closing bracket
|
|
|
|
|
}
|
|
|
|
|
nxt = NSMaxRange(r);
|
|
|
|
|
r = NSMakeRange(pos, nxt - pos);
|
|
|
|
|
s.location = r.location + 1;
|
|
|
|
|
s.length = r.length - 2;
|
|
|
|
|
v = [mtext substringWithRange: s];
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* If the value contains a '?', it is actually in two parts,
|
|
|
|
|
* the first part is the field name, and the second part is
|
|
|
|
|
* an alternative text to be used if the value from the
|
|
|
|
|
* dictionary is empty.
|
|
|
|
|
*/
|
|
|
|
|
s = [v rangeOfString: @"?"];
|
|
|
|
|
if (s.length == 0)
|
|
|
|
|
{
|
|
|
|
|
alt = @""; // No alternative value.
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
alt = [v substringFromIndex: NSMaxRange(s)];
|
|
|
|
|
v = [v substringToIndex: s.location];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* If the value we are substituting contains dots, we split it apart.
|
|
|
|
|
* We use the value to make a reference into the dictionary we are
|
|
|
|
|
* given.
|
|
|
|
|
*/
|
|
|
|
|
a = [v componentsSeparatedByString: @"."];
|
|
|
|
|
o = vals;
|
|
|
|
|
for (i = 0; i < [a count]; i++)
|
|
|
|
|
{
|
|
|
|
|
NSString *k = [a objectAtIndex: i];
|
|
|
|
|
|
|
|
|
|
if ([k length] > 0)
|
|
|
|
|
{
|
2005-07-07 21:11:04 +00:00
|
|
|
|
o = [(NSDictionary*)o objectForKey: k];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (o == vals)
|
|
|
|
|
{
|
|
|
|
|
v = nil; // Mo match found.
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2004-10-07 09:30:14 +00:00
|
|
|
|
if ([o isKindOfClass: NSStringClass] == YES)
|
2004-04-26 15:13:27 +00:00
|
|
|
|
{
|
2005-07-07 21:11:04 +00:00
|
|
|
|
v = (NSString*)o;
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if ([o isKindOfClass: [NSData class]] == YES)
|
|
|
|
|
{
|
|
|
|
|
[ma addObject: o];
|
2006-05-25 11:34:03 +00:00
|
|
|
|
v = @"'?'''?'";
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
v = [self quote: o];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ([v length] == 0)
|
|
|
|
|
{
|
|
|
|
|
v = alt;
|
2006-05-12 15:37:03 +00:00
|
|
|
|
if (v == nil)
|
|
|
|
|
{
|
|
|
|
|
v = @"";
|
|
|
|
|
}
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
vLength = [v length];
|
|
|
|
|
|
|
|
|
|
[mtext replaceCharactersInRange: r withString: v];
|
2006-05-12 15:37:03 +00:00
|
|
|
|
l += vLength; // Add length of string inserted
|
2004-04-26 15:13:27 +00:00
|
|
|
|
l -= r.length; // Remove length of string replaced
|
|
|
|
|
r.location += vLength;
|
|
|
|
|
|
|
|
|
|
if (r.location >= l)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
r.length = l - r.location;
|
|
|
|
|
r = [mtext rangeOfString: @"{"
|
|
|
|
|
options: NSLiteralSearch
|
|
|
|
|
range: r];
|
|
|
|
|
}
|
|
|
|
|
[ma insertObject: mtext atIndex: 0];
|
|
|
|
|
}
|
2009-11-18 11:11:29 +00:00
|
|
|
|
[arp release];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
return ma;
|
|
|
|
|
}
|
2005-09-26 11:22:35 +00:00
|
|
|
|
|
2005-09-27 06:35:05 +00:00
|
|
|
|
+ (void) _tick: (NSTimer*)t
|
2005-09-26 11:22:35 +00:00
|
|
|
|
{
|
2005-11-15 14:16:37 +00:00
|
|
|
|
(void) GSTickerTimeNow();
|
2005-09-26 11:22:35 +00:00
|
|
|
|
}
|
2004-04-26 15:13:27 +00:00
|
|
|
|
@end
|
|
|
|
|
|
2008-02-21 16:23:23 +00:00
|
|
|
|
@implementation SQLClient (GSCacheDelegate)
|
|
|
|
|
- (BOOL) shouldKeepItem: (id)anObject
|
|
|
|
|
withKey: (id)aKey
|
|
|
|
|
lifetime: (unsigned)lifetime
|
|
|
|
|
after: (unsigned)delay
|
|
|
|
|
{
|
|
|
|
|
CacheQuery *a;
|
2008-03-03 14:10:54 +00:00
|
|
|
|
NSDictionary *d;
|
2008-02-21 16:23:23 +00:00
|
|
|
|
|
|
|
|
|
a = [CacheQuery new];
|
2009-11-18 11:11:29 +00:00
|
|
|
|
aKey = [aKey copy];
|
|
|
|
|
[a->query release];
|
|
|
|
|
a->query = aKey;
|
2008-03-03 14:10:54 +00:00
|
|
|
|
d = [[NSThread currentThread] threadDictionary];
|
|
|
|
|
a->recordType = [d objectForKey: @"SQLClientRecordType"];
|
|
|
|
|
a->listType = [d objectForKey: @"SQLClientListType"];
|
2008-02-21 16:23:23 +00:00
|
|
|
|
a->lifetime = lifetime;
|
2009-11-18 11:11:29 +00:00
|
|
|
|
[a autorelease];
|
2008-02-21 16:23:23 +00:00
|
|
|
|
if (_cacheThread == nil)
|
|
|
|
|
{
|
|
|
|
|
[self _populateCache: a];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* We schedule an asynchronous update if the item is not too old,
|
|
|
|
|
* otherwise (more than lifetime seconds past its expiry) we wait
|
|
|
|
|
* for the update to complete.
|
|
|
|
|
*/
|
|
|
|
|
[self performSelectorOnMainThread: @selector(_populateCache:)
|
|
|
|
|
withObject: a
|
|
|
|
|
waitUntilDone: (delay > lifetime) ? YES : NO
|
|
|
|
|
modes: queryModes];
|
|
|
|
|
}
|
|
|
|
|
return YES; // Always keep items ...
|
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
2004-04-26 15:13:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@implementation SQLClient(Convenience)
|
|
|
|
|
|
2014-12-11 17:26:32 +00:00
|
|
|
|
+ (NSMutableArray*) columns: (NSMutableArray*)records
|
2009-09-16 08:59:59 +00:00
|
|
|
|
{
|
|
|
|
|
SQLRecord *r = [records lastObject];
|
|
|
|
|
unsigned rowCount = [records count];
|
|
|
|
|
unsigned colCount = [r count];
|
|
|
|
|
NSMutableArray *m;
|
|
|
|
|
|
|
|
|
|
if (rowCount == 0 || colCount == 0)
|
|
|
|
|
{
|
|
|
|
|
m = [NSMutableArray array];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
NSMutableArray *cols[colCount];
|
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
|
|
m = [NSMutableArray arrayWithCapacity: colCount];
|
|
|
|
|
for (i = 0; i < colCount; i++)
|
|
|
|
|
{
|
|
|
|
|
cols[i] = [[NSMutableArray alloc] initWithCapacity: rowCount];
|
|
|
|
|
[m addObject: cols[i]];
|
|
|
|
|
[cols[i] release];
|
|
|
|
|
}
|
|
|
|
|
for (i = 0; i < rowCount; i++)
|
|
|
|
|
{
|
|
|
|
|
unsigned j;
|
|
|
|
|
|
|
|
|
|
r = [records objectAtIndex: i];
|
|
|
|
|
for (j = 0; j < colCount; j++)
|
|
|
|
|
{
|
|
|
|
|
[cols[j] addObject: [r objectAtIndex: j]];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return m;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-11 17:26:32 +00:00
|
|
|
|
+ (void) singletons: (NSMutableArray*)records
|
|
|
|
|
{
|
|
|
|
|
unsigned c = [records count];
|
|
|
|
|
|
|
|
|
|
while (c-- > 0)
|
|
|
|
|
{
|
|
|
|
|
[records replaceObjectAtIndex: c
|
|
|
|
|
withObject: [[records objectAtIndex: c] lastObject]];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (SQLTransaction*) batch: (BOOL)stopOnFailure
|
|
|
|
|
{
|
|
|
|
|
SQLTransaction *transaction;
|
|
|
|
|
|
|
|
|
|
transaction = (SQLTransaction*)NSAllocateObject([SQLTransaction class], 0,
|
|
|
|
|
NSDefaultMallocZone());
|
|
|
|
|
|
|
|
|
|
transaction->_db = [self retain];
|
|
|
|
|
transaction->_info = [NSMutableArray new];
|
|
|
|
|
transaction->_batch = YES;
|
|
|
|
|
transaction->_stop = stopOnFailure;
|
|
|
|
|
return [(SQLTransaction*)transaction autorelease];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSMutableArray*) columns: (NSMutableArray*)records
|
|
|
|
|
{
|
|
|
|
|
return [SQLClient columns: records];
|
|
|
|
|
}
|
|
|
|
|
|
2004-04-26 15:13:27 +00:00
|
|
|
|
- (SQLRecord*) queryRecord: (NSString*)stmt, ...
|
|
|
|
|
{
|
|
|
|
|
va_list ap;
|
|
|
|
|
NSArray *result = nil;
|
|
|
|
|
SQLRecord *record;
|
|
|
|
|
|
|
|
|
|
va_start (ap, stmt);
|
2015-02-26 16:14:51 +00:00
|
|
|
|
stmt = [[self prepare: stmt args: ap] objectAtIndex: 0];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
va_end (ap);
|
|
|
|
|
|
|
|
|
|
result = [self simpleQuery: stmt];
|
|
|
|
|
|
|
|
|
|
if ([result count] > 1)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"Query returns more than one record -\n%@\n", stmt];
|
|
|
|
|
}
|
|
|
|
|
record = [result lastObject];
|
|
|
|
|
if (record == nil)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: SQLEmptyException
|
|
|
|
|
format: @"Query returns no data -\n%@\n", stmt];
|
|
|
|
|
}
|
|
|
|
|
return record;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSString*) queryString: (NSString*)stmt, ...
|
|
|
|
|
{
|
|
|
|
|
va_list ap;
|
|
|
|
|
NSArray *result = nil;
|
|
|
|
|
SQLRecord *record;
|
|
|
|
|
|
|
|
|
|
va_start (ap, stmt);
|
2015-02-26 16:14:51 +00:00
|
|
|
|
stmt = [[self prepare: stmt args: ap] objectAtIndex: 0];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
va_end (ap);
|
|
|
|
|
|
|
|
|
|
result = [self simpleQuery: stmt];
|
|
|
|
|
|
|
|
|
|
if ([result count] > 1)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"Query returns more than one record -\n%@\n", stmt];
|
|
|
|
|
}
|
|
|
|
|
record = [result lastObject];
|
|
|
|
|
if (record == nil)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: SQLEmptyException
|
|
|
|
|
format: @"Query returns no data -\n%@\n", stmt];
|
|
|
|
|
}
|
|
|
|
|
if ([record count] > 1)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"Query returns multiple fields -\n%@\n", stmt];
|
|
|
|
|
}
|
|
|
|
|
return [[record lastObject] description];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) singletons: (NSMutableArray*)records
|
|
|
|
|
{
|
2014-12-11 17:26:32 +00:00
|
|
|
|
[SQLClient singletons: records];
|
2004-04-26 15:13:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2004-07-26 08:56:26 +00:00
|
|
|
|
- (SQLTransaction*) transaction
|
|
|
|
|
{
|
2009-11-18 11:11:29 +00:00
|
|
|
|
SQLTransaction *transaction;
|
2004-07-26 08:56:26 +00:00
|
|
|
|
|
2009-11-18 11:11:29 +00:00
|
|
|
|
transaction = (SQLTransaction*)NSAllocateObject([SQLTransaction class], 0,
|
2004-07-26 08:56:26 +00:00
|
|
|
|
NSDefaultMallocZone());
|
|
|
|
|
|
2009-11-18 11:11:29 +00:00
|
|
|
|
transaction->_db = [self retain];
|
2004-07-26 08:56:26 +00:00
|
|
|
|
transaction->_info = [NSMutableArray new];
|
2009-11-18 11:11:29 +00:00
|
|
|
|
return [(SQLTransaction*)transaction autorelease];
|
2004-07-26 08:56:26 +00:00
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
2005-03-02 10:00:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@interface SQLClientCacheInfo : NSObject
|
|
|
|
|
{
|
|
|
|
|
@public
|
|
|
|
|
NSString *query;
|
|
|
|
|
NSMutableArray *result;
|
|
|
|
|
NSTimeInterval expires;
|
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation SQLClientCacheInfo
|
|
|
|
|
- (void) dealloc
|
|
|
|
|
{
|
2009-11-18 11:11:29 +00:00
|
|
|
|
[query release]; query = nil;
|
|
|
|
|
[result release]; result = nil;
|
2005-03-02 10:00:24 +00:00
|
|
|
|
[super dealloc];
|
|
|
|
|
}
|
2009-11-18 11:11:29 +00:00
|
|
|
|
- (NSUInteger) hash
|
2005-03-02 10:00:24 +00:00
|
|
|
|
{
|
|
|
|
|
return [query hash];
|
|
|
|
|
}
|
2005-07-07 21:11:04 +00:00
|
|
|
|
- (BOOL) isEqual: (id)other
|
2005-03-02 10:00:24 +00:00
|
|
|
|
{
|
2005-07-07 21:11:04 +00:00
|
|
|
|
return [query isEqual: ((SQLClientCacheInfo*)other)->query];
|
2005-03-02 10:00:24 +00:00
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation SQLClient (Caching)
|
2005-09-22 08:42:37 +00:00
|
|
|
|
|
2005-11-14 20:37:33 +00:00
|
|
|
|
- (GSCache*) cache
|
2005-09-22 08:42:37 +00:00
|
|
|
|
{
|
2008-02-21 16:23:23 +00:00
|
|
|
|
GSCache *c;
|
|
|
|
|
|
2015-05-04 12:15:47 +00:00
|
|
|
|
/* NB we use a different lock to protect the cache from the lock
|
|
|
|
|
* used to protect the database query. That allows multiple
|
|
|
|
|
* connections (a connection pool) to share the same cache.
|
|
|
|
|
*/
|
|
|
|
|
[cacheLock lock];
|
2014-05-13 11:01:50 +00:00
|
|
|
|
if (nil == _cache)
|
2005-09-22 08:42:37 +00:00
|
|
|
|
{
|
2005-11-14 20:37:33 +00:00
|
|
|
|
_cache = [GSCache new];
|
2008-02-21 16:23:23 +00:00
|
|
|
|
if (_cacheThread != nil)
|
|
|
|
|
{
|
|
|
|
|
[_cache setDelegate: self];
|
|
|
|
|
}
|
2005-09-22 08:42:37 +00:00
|
|
|
|
}
|
2009-11-18 11:11:29 +00:00
|
|
|
|
c = [_cache retain];
|
2015-05-04 12:15:47 +00:00
|
|
|
|
[cacheLock unlock];
|
2009-11-18 11:11:29 +00:00
|
|
|
|
return [c autorelease];
|
2005-09-22 08:42:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-26 16:14:51 +00:00
|
|
|
|
- (NSMutableArray*) cacheCheckSimpleQuery: (NSString*)stmt
|
|
|
|
|
{
|
|
|
|
|
NSMutableArray *result = [[self cache] objectForKey: stmt];
|
|
|
|
|
|
|
|
|
|
if (result != nil)
|
|
|
|
|
{
|
|
|
|
|
result = [[result mutableCopy] autorelease];
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2005-03-02 10:00:24 +00:00
|
|
|
|
- (NSMutableArray*) cache: (int)seconds
|
|
|
|
|
query: (NSString*)stmt,...
|
|
|
|
|
{
|
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
|
|
va_start (ap, stmt);
|
2015-02-26 16:14:51 +00:00
|
|
|
|
stmt = [[self prepare: stmt args: ap] objectAtIndex: 0];
|
2005-03-02 10:00:24 +00:00
|
|
|
|
va_end (ap);
|
|
|
|
|
|
|
|
|
|
return [self cache: seconds simpleQuery: stmt];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSMutableArray*) cache: (int)seconds
|
|
|
|
|
query: (NSString*)stmt
|
|
|
|
|
with: (NSDictionary*)values
|
|
|
|
|
{
|
|
|
|
|
stmt = [[self _substitute: stmt with: values] objectAtIndex: 0];
|
|
|
|
|
return [self cache: seconds simpleQuery: stmt];
|
|
|
|
|
}
|
|
|
|
|
|
2007-03-08 17:12:55 +00:00
|
|
|
|
- (NSMutableArray*) cache: (int)seconds
|
|
|
|
|
simpleQuery: (NSString*)stmt
|
|
|
|
|
{
|
2008-03-03 14:10:54 +00:00
|
|
|
|
return [self cache: seconds
|
|
|
|
|
simpleQuery: stmt
|
|
|
|
|
recordType: nil
|
|
|
|
|
listType: nil];
|
2007-03-08 17:12:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSMutableArray*) cache: (int)seconds
|
|
|
|
|
simpleQuery: (NSString*)stmt
|
2008-03-03 14:10:54 +00:00
|
|
|
|
recordType: (id)rtype
|
|
|
|
|
listType: (id)ltype
|
2005-03-02 10:00:24 +00:00
|
|
|
|
{
|
2008-02-21 16:23:23 +00:00
|
|
|
|
NSMutableArray *result;
|
2008-03-03 14:10:54 +00:00
|
|
|
|
NSMutableDictionary *md;
|
2008-02-21 16:23:23 +00:00
|
|
|
|
GSCache *c;
|
|
|
|
|
id toCache;
|
2005-03-02 10:00:24 +00:00
|
|
|
|
|
2008-03-03 14:10:54 +00:00
|
|
|
|
if (rtype == 0) rtype = rClass;
|
|
|
|
|
if (ltype == 0) ltype = aClass;
|
2008-02-21 16:23:23 +00:00
|
|
|
|
|
2008-03-03 14:10:54 +00:00
|
|
|
|
md = [[NSThread currentThread] threadDictionary];
|
|
|
|
|
[md setObject: rtype forKey: @"SQLClientRecordType"];
|
|
|
|
|
[md setObject: ltype forKey: @"SQLClientListType"];
|
2014-06-20 05:15:24 +00:00
|
|
|
|
_lastStart = GSTickerTimeNow();
|
2008-02-21 16:23:23 +00:00
|
|
|
|
c = [self cache];
|
|
|
|
|
toCache = nil;
|
|
|
|
|
|
|
|
|
|
if (seconds < 0)
|
|
|
|
|
{
|
|
|
|
|
seconds = -seconds;
|
|
|
|
|
result = nil;
|
|
|
|
|
}
|
|
|
|
|
else
|
2005-03-02 10:00:24 +00:00
|
|
|
|
{
|
2008-02-21 16:23:23 +00:00
|
|
|
|
result = [c objectForKey: stmt];
|
|
|
|
|
}
|
2005-03-02 10:00:24 +00:00
|
|
|
|
|
2008-02-21 16:23:23 +00:00
|
|
|
|
if (result == nil)
|
|
|
|
|
{
|
|
|
|
|
CacheQuery *a;
|
|
|
|
|
|
|
|
|
|
a = [CacheQuery new];
|
2009-11-18 11:11:29 +00:00
|
|
|
|
a->query = [stmt copy];
|
2008-03-03 14:10:54 +00:00
|
|
|
|
a->recordType = rtype;
|
|
|
|
|
a->listType = ltype;
|
2008-02-21 16:23:23 +00:00
|
|
|
|
a->lifetime = seconds;
|
2009-11-18 11:11:29 +00:00
|
|
|
|
[a autorelease];
|
2008-02-21 16:23:23 +00:00
|
|
|
|
|
|
|
|
|
if (_cacheThread == nil)
|
|
|
|
|
{
|
|
|
|
|
[self _populateCache: a];
|
2005-03-02 10:00:24 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2008-02-21 16:23:23 +00:00
|
|
|
|
/* Not really an asynchronous query becuse we wait until it's
|
|
|
|
|
* done in order to have a result we can return.
|
|
|
|
|
*/
|
|
|
|
|
[self performSelectorOnMainThread: @selector(_populateCache:)
|
|
|
|
|
withObject: a
|
|
|
|
|
waitUntilDone: YES
|
|
|
|
|
modes: queryModes];
|
|
|
|
|
}
|
|
|
|
|
result = [c objectForKey: stmt];
|
|
|
|
|
_lastOperation = GSTickerTimeNow();
|
|
|
|
|
if (_duration >= 0)
|
2005-09-22 08:42:37 +00:00
|
|
|
|
{
|
2008-02-21 16:23:23 +00:00
|
|
|
|
NSTimeInterval d;
|
2005-03-02 10:00:24 +00:00
|
|
|
|
|
2014-06-20 05:15:24 +00:00
|
|
|
|
d = _lastOperation - _lastStart;
|
2008-02-21 16:23:23 +00:00
|
|
|
|
if (d >= _duration)
|
|
|
|
|
{
|
|
|
|
|
[self debug: @"Duration %g for query %@", d, stmt];
|
2005-03-02 10:00:24 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2008-02-21 16:23:23 +00:00
|
|
|
|
}
|
2005-09-27 14:07:04 +00:00
|
|
|
|
|
2008-02-21 16:23:23 +00:00
|
|
|
|
if (seconds == 0)
|
|
|
|
|
{
|
|
|
|
|
// We have been told to remove the existing cached item.
|
|
|
|
|
[c setObject: nil forKey: stmt lifetime: seconds];
|
|
|
|
|
toCache = nil;
|
|
|
|
|
}
|
2005-09-22 08:42:37 +00:00
|
|
|
|
|
2008-02-21 16:23:23 +00:00
|
|
|
|
if (toCache != nil)
|
|
|
|
|
{
|
|
|
|
|
// We have a newly retrieved object ... cache it.
|
|
|
|
|
[c setObject: toCache forKey: stmt lifetime: seconds];
|
2005-03-02 10:00:24 +00:00
|
|
|
|
}
|
2008-02-21 16:23:23 +00:00
|
|
|
|
|
|
|
|
|
if (result != nil)
|
2005-03-02 10:00:24 +00:00
|
|
|
|
{
|
2008-02-21 16:23:23 +00:00
|
|
|
|
/*
|
|
|
|
|
* Return an autoreleased copy ... not the original cached data.
|
|
|
|
|
*/
|
2008-03-03 14:10:54 +00:00
|
|
|
|
result = [[result mutableCopy] autorelease];
|
2005-03-02 10:00:24 +00:00
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2005-11-14 20:37:33 +00:00
|
|
|
|
- (void) setCache: (GSCache*)aCache
|
2005-09-27 06:35:05 +00:00
|
|
|
|
{
|
2015-05-04 12:15:47 +00:00
|
|
|
|
/* NB we use a different lock to protect the cache from the lock
|
|
|
|
|
* used to protect the database query. That allows multiple
|
|
|
|
|
* connections (a connection pool) to share the same cache.
|
|
|
|
|
*/
|
|
|
|
|
[cacheLock lock];
|
2012-11-29 11:40:04 +00:00
|
|
|
|
NS_DURING
|
2008-02-21 16:23:23 +00:00
|
|
|
|
{
|
2012-11-29 11:40:04 +00:00
|
|
|
|
if (_cacheThread != nil)
|
|
|
|
|
{
|
|
|
|
|
[_cache setDelegate: nil];
|
|
|
|
|
}
|
|
|
|
|
[aCache retain];
|
|
|
|
|
[_cache release];
|
|
|
|
|
_cache = aCache;
|
|
|
|
|
if (_cacheThread != nil)
|
|
|
|
|
{
|
|
|
|
|
[_cache setDelegate: self];
|
|
|
|
|
}
|
2008-02-21 16:23:23 +00:00
|
|
|
|
}
|
2012-11-29 11:40:04 +00:00
|
|
|
|
NS_HANDLER
|
2008-02-21 16:23:23 +00:00
|
|
|
|
{
|
2015-05-04 12:15:47 +00:00
|
|
|
|
[cacheLock unlock];
|
2012-11-29 11:40:04 +00:00
|
|
|
|
[localException raise];
|
2008-02-21 16:23:23 +00:00
|
|
|
|
}
|
2012-11-29 11:40:04 +00:00
|
|
|
|
NS_ENDHANDLER
|
2015-05-04 12:15:47 +00:00
|
|
|
|
[cacheLock unlock];
|
2008-02-21 16:23:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) setCacheThread: (NSThread*)aThread
|
|
|
|
|
{
|
|
|
|
|
if (mainThread == nil)
|
|
|
|
|
{
|
|
|
|
|
[self performSelectorOnMainThread: @selector(_recordMainThread)
|
|
|
|
|
withObject: nil
|
|
|
|
|
waitUntilDone: NO
|
|
|
|
|
modes: queryModes];
|
|
|
|
|
}
|
|
|
|
|
if (aThread != nil && aThread != mainThread)
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"SQLClient: only the main thread is usable as cache thread");
|
|
|
|
|
aThread = mainThread;
|
|
|
|
|
}
|
|
|
|
|
[lock lock];
|
2012-11-29 11:40:04 +00:00
|
|
|
|
NS_DURING
|
2008-02-21 16:23:23 +00:00
|
|
|
|
{
|
2012-11-29 11:40:04 +00:00
|
|
|
|
if (_cacheThread != nil)
|
|
|
|
|
{
|
|
|
|
|
[_cache setDelegate: nil];
|
|
|
|
|
}
|
|
|
|
|
[aThread retain];
|
|
|
|
|
[_cacheThread release];
|
|
|
|
|
_cacheThread = aThread;
|
|
|
|
|
if (_cacheThread != nil)
|
|
|
|
|
{
|
|
|
|
|
[_cache setDelegate: self];
|
|
|
|
|
}
|
2008-02-21 16:23:23 +00:00
|
|
|
|
}
|
2012-11-29 11:40:04 +00:00
|
|
|
|
NS_HANDLER
|
2008-02-21 16:23:23 +00:00
|
|
|
|
{
|
2012-11-29 11:40:04 +00:00
|
|
|
|
[lock unlock];
|
|
|
|
|
[localException raise];
|
2008-02-21 16:23:23 +00:00
|
|
|
|
}
|
2012-11-29 11:40:04 +00:00
|
|
|
|
NS_ENDHANDLER
|
2008-02-21 16:23:23 +00:00
|
|
|
|
[lock unlock];
|
2005-09-27 06:35:05 +00:00
|
|
|
|
}
|
2005-03-02 10:00:24 +00:00
|
|
|
|
@end
|
|
|
|
|
|
2004-07-26 08:56:26 +00:00
|
|
|
|
@implementation SQLTransaction
|
2005-09-28 06:35:03 +00:00
|
|
|
|
|
2007-07-09 17:08:22 +00:00
|
|
|
|
- (void) _addSQL: (NSMutableString*)sql andArgs: (NSMutableArray*)args
|
|
|
|
|
{
|
|
|
|
|
unsigned count = [_info count];
|
|
|
|
|
unsigned index;
|
|
|
|
|
|
|
|
|
|
for (index = 0; index < count; index++)
|
|
|
|
|
{
|
|
|
|
|
id o = [_info objectAtIndex: index];
|
|
|
|
|
|
|
|
|
|
if ([o isKindOfClass: NSArrayClass] == YES)
|
|
|
|
|
{
|
|
|
|
|
unsigned c = [(NSArray*)o count];
|
|
|
|
|
|
|
|
|
|
if (c > 0)
|
|
|
|
|
{
|
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
|
|
[sql appendString: [(NSArray*)o objectAtIndex: 0]];
|
|
|
|
|
[sql appendString: @";"];
|
|
|
|
|
for (i = 1; i < c; i++)
|
|
|
|
|
{
|
|
|
|
|
[args addObject: [(NSArray*)o objectAtIndex: i]];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
[(SQLTransaction*)o _addSQL: sql andArgs: args];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-12 09:50:50 +00:00
|
|
|
|
- (void) addPrepared: (NSArray*)statement
|
2009-09-08 09:59:05 +00:00
|
|
|
|
{
|
|
|
|
|
[_info addObject: statement];
|
|
|
|
|
_count++;
|
|
|
|
|
}
|
|
|
|
|
|
2007-07-09 17:08:22 +00:00
|
|
|
|
- (void) _countLength: (unsigned*)length andArgs: (unsigned*)args
|
|
|
|
|
{
|
|
|
|
|
unsigned count = [_info count];
|
|
|
|
|
unsigned index;
|
|
|
|
|
|
|
|
|
|
for (index = 0; index < count; index++)
|
|
|
|
|
{
|
|
|
|
|
id o = [_info objectAtIndex: index];
|
|
|
|
|
|
|
|
|
|
if ([o isKindOfClass: NSArrayClass] == YES)
|
|
|
|
|
{
|
|
|
|
|
unsigned c = [(NSArray*)o count];
|
|
|
|
|
|
|
|
|
|
if (c > 0)
|
|
|
|
|
{
|
|
|
|
|
length += [[(NSArray*)o objectAtIndex: 0] length] + 1;
|
|
|
|
|
args += c - 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
[(SQLTransaction*)o _countLength: length andArgs: args];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-06-23 19:36:52 +00:00
|
|
|
|
|
2014-08-09 14:02:11 +00:00
|
|
|
|
/* Try to merge the prepared statement p with an earlier statement in the
|
|
|
|
|
* transaction. We search up to 5 earlier statements and we merge if we can.
|
|
|
|
|
*/
|
|
|
|
|
- (void) _merge: (NSMutableArray*)p
|
|
|
|
|
{
|
|
|
|
|
if (_count > 0 && _merge > 0)
|
|
|
|
|
{
|
|
|
|
|
static NSCharacterSet *w = nil;
|
|
|
|
|
NSString *s;
|
|
|
|
|
NSRange r;
|
|
|
|
|
|
|
|
|
|
s = [p objectAtIndex: 0]; // Get SQL part of array
|
|
|
|
|
|
|
|
|
|
if (nil == w)
|
|
|
|
|
{
|
|
|
|
|
w = [[NSCharacterSet whitespaceAndNewlineCharacterSet] retain];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r = [s rangeOfString: @"INSERT" options: NSCaseInsensitiveSearch];
|
|
|
|
|
if (r.length > 0 && 0 == r.location)
|
|
|
|
|
{
|
|
|
|
|
r = [s rangeOfString: @"VALUES" options: NSCaseInsensitiveSearch];
|
|
|
|
|
if (r.length > 0)
|
|
|
|
|
{
|
|
|
|
|
NSUInteger l = [s length];
|
|
|
|
|
NSUInteger pos = NSMaxRange(r);
|
|
|
|
|
|
|
|
|
|
while (pos < l
|
|
|
|
|
&& [w characterIsMember: [s characterAtIndex: pos]])
|
|
|
|
|
{
|
|
|
|
|
pos++;
|
|
|
|
|
}
|
|
|
|
|
if (pos < l && [s characterAtIndex: pos] == '(')
|
|
|
|
|
{
|
|
|
|
|
NSString *t = [s substringToIndex: pos];
|
|
|
|
|
NSUInteger index = _count;
|
|
|
|
|
NSUInteger attempts = 0;
|
|
|
|
|
|
|
|
|
|
s = [s substringFromIndex: pos];
|
|
|
|
|
while (index-- > 0 && attempts++ < _merge)
|
|
|
|
|
{
|
|
|
|
|
NSMutableArray *o;
|
|
|
|
|
NSString *os;
|
|
|
|
|
|
|
|
|
|
o = [_info objectAtIndex: index];
|
|
|
|
|
os = [o objectAtIndex: 0];
|
|
|
|
|
if ([os hasPrefix: t])
|
|
|
|
|
{
|
|
|
|
|
NSMutableString *m;
|
|
|
|
|
|
|
|
|
|
if ([os isKindOfClass: [NSMutableString class]])
|
|
|
|
|
{
|
|
|
|
|
m = (NSMutableString*)os;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
m = [NSMutableString
|
|
|
|
|
stringWithCapacity: [os length] * 100];
|
|
|
|
|
[m appendString: os];
|
|
|
|
|
}
|
|
|
|
|
[m appendString: @","];
|
|
|
|
|
[m appendString: s];
|
|
|
|
|
[o replaceObjectAtIndex: 0 withObject: m];
|
|
|
|
|
for (index = 1; index < [p count]; index++)
|
|
|
|
|
{
|
|
|
|
|
[o addObject: [p objectAtIndex: index]];
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r = [s rangeOfString: @"UPDATE" options: NSCaseInsensitiveSearch];
|
|
|
|
|
if (0 == r.length)
|
|
|
|
|
{
|
|
|
|
|
r = [s rangeOfString: @"DELETE" options: NSCaseInsensitiveSearch];
|
|
|
|
|
}
|
|
|
|
|
if (r.length > 0 && 0 == r.location)
|
|
|
|
|
{
|
|
|
|
|
r = [s rangeOfString: @"WHERE" options: NSCaseInsensitiveSearch];
|
|
|
|
|
if (r.length > 0)
|
|
|
|
|
{
|
|
|
|
|
NSUInteger l = [s length];
|
|
|
|
|
NSUInteger pos = NSMaxRange(r);
|
|
|
|
|
|
|
|
|
|
while (pos < l
|
|
|
|
|
&& [w characterIsMember: [s characterAtIndex: pos]])
|
|
|
|
|
{
|
|
|
|
|
pos++;
|
|
|
|
|
}
|
|
|
|
|
if (pos < l && [s characterAtIndex: pos] == '(')
|
|
|
|
|
{
|
|
|
|
|
NSString *t = [s substringToIndex: pos];
|
|
|
|
|
NSUInteger index = _count;
|
|
|
|
|
NSUInteger attempts = 0;
|
|
|
|
|
|
|
|
|
|
/* Get the condition after the WHERE and if it's not
|
|
|
|
|
* in brackets, add them so the merge can work.
|
|
|
|
|
*/
|
|
|
|
|
s = [s substringFromIndex: pos];
|
|
|
|
|
if ([s characterAtIndex: 0] != '(')
|
|
|
|
|
{
|
|
|
|
|
s = [NSString stringWithFormat: @"(%@)", s];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (index-- > 0 && attempts++ < _merge)
|
|
|
|
|
{
|
|
|
|
|
NSMutableArray *o;
|
|
|
|
|
NSString *os;
|
|
|
|
|
|
|
|
|
|
o = [_info objectAtIndex: index];
|
|
|
|
|
os = [o objectAtIndex: 0];
|
|
|
|
|
if ([os hasPrefix: t])
|
|
|
|
|
{
|
|
|
|
|
NSMutableString *m;
|
|
|
|
|
|
|
|
|
|
l = [os length];
|
|
|
|
|
if ([os characterAtIndex: l - 1] == ')')
|
|
|
|
|
{
|
|
|
|
|
if ([os isKindOfClass: [NSMutableString class]])
|
|
|
|
|
{
|
|
|
|
|
m = (NSMutableString*)os;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
m = [NSMutableString
|
|
|
|
|
stringWithCapacity: l * 100];
|
|
|
|
|
[m appendString: os];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* The condition of the WHERE clause was not
|
|
|
|
|
* bracketed, so we extract it and build a
|
|
|
|
|
* new statement in which it is bracketed.
|
|
|
|
|
*/
|
|
|
|
|
os = [os substringFromIndex: pos];
|
|
|
|
|
m = [NSMutableString
|
|
|
|
|
stringWithCapacity: l * 100];
|
|
|
|
|
[m appendFormat: @"%@(%@)", t, os];
|
|
|
|
|
}
|
|
|
|
|
[m appendString: @" OR "];
|
|
|
|
|
[m appendString: s];
|
|
|
|
|
[o replaceObjectAtIndex: 0 withObject: m];
|
|
|
|
|
for (index = 1; index < [p count]; index++)
|
|
|
|
|
{
|
|
|
|
|
[o addObject: [p objectAtIndex: index]];
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
[_info addObject: p];
|
|
|
|
|
_count++;
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-08 08:05:31 +00:00
|
|
|
|
- (void) add: (NSString*)stmt,...
|
|
|
|
|
{
|
2014-08-09 14:02:11 +00:00
|
|
|
|
va_list ap;
|
|
|
|
|
NSMutableArray *p;
|
2009-09-08 08:05:31 +00:00
|
|
|
|
|
|
|
|
|
va_start (ap, stmt);
|
2015-02-26 16:14:51 +00:00
|
|
|
|
p = [_db prepare: stmt args: ap];
|
2009-09-08 08:05:31 +00:00
|
|
|
|
va_end (ap);
|
2014-08-09 14:02:11 +00:00
|
|
|
|
[self _merge: p];
|
2009-09-08 08:05:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) add: (NSString*)stmt with: (NSDictionary*)values
|
|
|
|
|
{
|
2014-08-09 14:02:11 +00:00
|
|
|
|
NSMutableArray *p;
|
|
|
|
|
|
|
|
|
|
p = [_db _substitute: stmt with: values];
|
|
|
|
|
[self _merge: p];
|
2009-09-08 08:05:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) append: (SQLTransaction*)other
|
|
|
|
|
{
|
|
|
|
|
if (other != nil && other->_count > 0)
|
|
|
|
|
{
|
2012-06-17 14:57:48 +00:00
|
|
|
|
if (NO == [_db isEqual: other->_db])
|
2009-09-08 08:05:31 +00:00
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"[%@-%@] database client missmatch",
|
|
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
|
|
|
|
|
}
|
2014-08-09 14:02:11 +00:00
|
|
|
|
if (_merge > 0)
|
|
|
|
|
{
|
|
|
|
|
unsigned index;
|
|
|
|
|
|
|
|
|
|
/* Merging of statements is turned on ... try to merge statements
|
|
|
|
|
* from other transaction rather than simply appending a copy of it.
|
|
|
|
|
*/
|
|
|
|
|
for (index = 0; index < other->_count; index++)
|
|
|
|
|
{
|
|
|
|
|
[self _merge: [other->_info objectAtIndex: index]];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
other = [other copy];
|
|
|
|
|
[_info addObject: other];
|
|
|
|
|
_count += other->_count;
|
|
|
|
|
[other release];
|
|
|
|
|
}
|
2009-09-08 08:05:31 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) copyWithZone: (NSZone*)z
|
|
|
|
|
{
|
|
|
|
|
SQLTransaction *c;
|
|
|
|
|
|
|
|
|
|
c = (SQLTransaction*)NSCopyObject(self, 0, z);
|
2009-11-18 11:11:29 +00:00
|
|
|
|
c->_db = [c->_db retain];
|
2009-09-08 08:05:31 +00:00
|
|
|
|
c->_info = [c->_info mutableCopy];
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-06 16:03:51 +00:00
|
|
|
|
- (NSUInteger) count
|
2009-09-08 08:05:31 +00:00
|
|
|
|
{
|
|
|
|
|
return [_info count];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (SQLClient*) db
|
|
|
|
|
{
|
|
|
|
|
return _db;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
|
{
|
2009-11-18 11:11:29 +00:00
|
|
|
|
[_db release]; _db = nil;
|
|
|
|
|
[_info release]; _info = nil;
|
2009-09-08 08:05:31 +00:00
|
|
|
|
[super dealloc];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSString*) description
|
|
|
|
|
{
|
|
|
|
|
return [NSString stringWithFormat: @"%@ with SQL '%@' for %@",
|
|
|
|
|
[super description],
|
|
|
|
|
(_count == 0 ? (id)@"" : (id)_info), _db];
|
|
|
|
|
}
|
|
|
|
|
|
2004-07-26 08:56:26 +00:00
|
|
|
|
- (void) execute
|
|
|
|
|
{
|
|
|
|
|
if (_count > 0)
|
|
|
|
|
{
|
2007-07-09 17:08:22 +00:00
|
|
|
|
NSMutableArray *info = nil;
|
2014-10-07 09:15:16 +00:00
|
|
|
|
NSRecursiveLock *dbLock = [_db _lock];
|
|
|
|
|
BOOL wrap;
|
2006-06-23 19:36:52 +00:00
|
|
|
|
|
2014-10-07 09:15:16 +00:00
|
|
|
|
[dbLock lock];
|
|
|
|
|
wrap = [_db isInTransaction] ? NO : YES;
|
2004-07-26 08:56:26 +00:00
|
|
|
|
NS_DURING
|
|
|
|
|
{
|
2007-07-09 17:08:22 +00:00
|
|
|
|
NSMutableString *sql;
|
|
|
|
|
unsigned sqlSize = 0;
|
|
|
|
|
unsigned argCount = 0;
|
|
|
|
|
|
|
|
|
|
[self _countLength: &sqlSize andArgs: &argCount];
|
|
|
|
|
|
|
|
|
|
/* Allocate and initialise the transaction statement.
|
|
|
|
|
*/
|
|
|
|
|
info = [[NSMutableArray alloc] initWithCapacity: argCount + 1];
|
|
|
|
|
sql = [[NSMutableString alloc] initWithCapacity: sqlSize + 13];
|
|
|
|
|
[info addObject: sql];
|
2009-11-18 11:11:29 +00:00
|
|
|
|
[sql release];
|
2014-10-07 07:09:38 +00:00
|
|
|
|
if (YES == wrap)
|
2007-07-09 17:08:22 +00:00
|
|
|
|
{
|
|
|
|
|
[sql appendString: @"begin;"];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[self _addSQL: sql andArgs: info];
|
|
|
|
|
|
2014-10-07 07:09:38 +00:00
|
|
|
|
if (YES == wrap)
|
2007-07-09 17:08:22 +00:00
|
|
|
|
{
|
|
|
|
|
[sql appendString: @"commit;"];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[_db simpleExecute: info];
|
2009-11-18 11:11:29 +00:00
|
|
|
|
[info release]; info = nil;
|
2014-10-07 09:15:16 +00:00
|
|
|
|
[dbLock unlock];
|
2004-07-26 08:56:26 +00:00
|
|
|
|
}
|
|
|
|
|
NS_HANDLER
|
|
|
|
|
{
|
2014-10-07 07:09:38 +00:00
|
|
|
|
NSException *e = localException;
|
|
|
|
|
|
2009-11-18 11:11:29 +00:00
|
|
|
|
[info release];
|
2014-10-07 07:09:38 +00:00
|
|
|
|
if (YES == wrap)
|
2014-10-02 20:30:22 +00:00
|
|
|
|
{
|
|
|
|
|
NS_DURING
|
|
|
|
|
{
|
|
|
|
|
[_db simpleExecute: rollbackStatement];
|
|
|
|
|
}
|
|
|
|
|
NS_HANDLER
|
|
|
|
|
{
|
2014-10-07 07:09:38 +00:00
|
|
|
|
[_db disconnect];
|
|
|
|
|
NSLog(@"Disconnected due to failed rollback after %@", e);
|
2014-10-02 20:30:22 +00:00
|
|
|
|
}
|
|
|
|
|
NS_ENDHANDLER
|
|
|
|
|
}
|
2014-10-07 09:15:16 +00:00
|
|
|
|
[dbLock unlock];
|
2014-10-07 07:09:38 +00:00
|
|
|
|
[e raise];
|
2004-07-26 08:56:26 +00:00
|
|
|
|
}
|
|
|
|
|
NS_ENDHANDLER
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-07-09 17:08:22 +00:00
|
|
|
|
|
|
|
|
|
- (unsigned) executeBatch
|
2009-09-08 08:05:31 +00:00
|
|
|
|
{
|
2009-09-08 08:17:09 +00:00
|
|
|
|
return [self executeBatchReturningFailures: nil logExceptions: NO];
|
2009-09-08 08:05:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (unsigned) executeBatchReturningFailures: (SQLTransaction*)failures
|
2009-09-08 08:17:09 +00:00
|
|
|
|
logExceptions: (BOOL)log
|
2007-07-09 17:08:22 +00:00
|
|
|
|
{
|
|
|
|
|
unsigned executed = 0;
|
|
|
|
|
|
|
|
|
|
if (_count > 0)
|
|
|
|
|
{
|
2014-10-07 09:15:16 +00:00
|
|
|
|
NSRecursiveLock *dbLock = [_db _lock];
|
|
|
|
|
|
|
|
|
|
[dbLock lock];
|
2007-07-09 17:08:22 +00:00
|
|
|
|
NS_DURING
|
|
|
|
|
{
|
|
|
|
|
[self execute];
|
|
|
|
|
executed = _count;
|
|
|
|
|
}
|
|
|
|
|
NS_HANDLER
|
|
|
|
|
{
|
2009-09-08 08:17:09 +00:00
|
|
|
|
if (log == YES || [_db debugging] > 0)
|
2009-06-02 17:20:00 +00:00
|
|
|
|
{
|
|
|
|
|
[_db debug: @"Initial failure executing batch %@: %@",
|
|
|
|
|
self, localException];
|
|
|
|
|
}
|
2007-07-09 17:08:22 +00:00
|
|
|
|
if (_batch == YES)
|
|
|
|
|
{
|
2009-09-08 09:59:05 +00:00
|
|
|
|
SQLTransaction *wrapper = nil;
|
|
|
|
|
unsigned count = [_info count];
|
|
|
|
|
unsigned i;
|
2007-07-09 17:08:22 +00:00
|
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
|
{
|
|
|
|
|
BOOL success = NO;
|
2009-09-08 08:05:31 +00:00
|
|
|
|
id o = [_info objectAtIndex: i];
|
2007-07-09 17:08:22 +00:00
|
|
|
|
|
2009-09-08 08:05:31 +00:00
|
|
|
|
if ([o isKindOfClass: NSArrayClass] == YES)
|
|
|
|
|
{
|
|
|
|
|
NS_DURING
|
|
|
|
|
{
|
2009-09-08 09:59:05 +00:00
|
|
|
|
/* Wrap the statement inside a transaction so
|
|
|
|
|
* its context will still be that of a statement
|
|
|
|
|
* in a transaction rather than a standalone
|
|
|
|
|
* statement. This might be important if the
|
|
|
|
|
* statement is actually a call to a stored
|
|
|
|
|
* procedure whose code must all be executed
|
|
|
|
|
* with the visibility rules of a single
|
|
|
|
|
* transaction.
|
|
|
|
|
*/
|
|
|
|
|
if (wrapper == nil)
|
|
|
|
|
{
|
|
|
|
|
wrapper = [_db transaction];
|
|
|
|
|
}
|
|
|
|
|
[wrapper reset];
|
2015-04-12 09:50:50 +00:00
|
|
|
|
[wrapper addPrepared: o];
|
2009-09-08 09:59:05 +00:00
|
|
|
|
[wrapper execute];
|
2007-07-09 17:08:22 +00:00
|
|
|
|
executed++;
|
|
|
|
|
success = YES;
|
|
|
|
|
}
|
2009-09-08 08:05:31 +00:00
|
|
|
|
NS_HANDLER
|
2009-06-02 17:20:00 +00:00
|
|
|
|
{
|
2009-09-08 08:05:31 +00:00
|
|
|
|
if (failures != nil)
|
|
|
|
|
{
|
2015-04-12 09:50:50 +00:00
|
|
|
|
[failures addPrepared: o];
|
2009-09-08 08:05:31 +00:00
|
|
|
|
}
|
2009-09-08 08:17:09 +00:00
|
|
|
|
if (log == YES || [_db debugging] > 0)
|
2009-09-08 08:05:31 +00:00
|
|
|
|
{
|
|
|
|
|
[_db debug:
|
|
|
|
|
@"Failure of %d executing batch %@: %@",
|
|
|
|
|
i, self, localException];
|
|
|
|
|
}
|
|
|
|
|
success = NO;
|
2009-06-02 17:20:00 +00:00
|
|
|
|
}
|
2009-09-08 08:05:31 +00:00
|
|
|
|
NS_ENDHANDLER
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
unsigned result;
|
|
|
|
|
|
|
|
|
|
result = [(SQLTransaction*)o
|
2009-09-08 08:17:09 +00:00
|
|
|
|
executeBatchReturningFailures: failures
|
|
|
|
|
logExceptions: log];
|
2009-09-08 08:05:31 +00:00
|
|
|
|
executed += result;
|
|
|
|
|
if (result == [(SQLTransaction*)o totalCount])
|
|
|
|
|
{
|
|
|
|
|
success = YES;
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-07-09 17:08:22 +00:00
|
|
|
|
if (success == NO && _stop == YES)
|
|
|
|
|
{
|
2009-09-08 08:05:31 +00:00
|
|
|
|
/* We are configured to stop after a failure,
|
|
|
|
|
* so we need to add all the subsequent statements
|
|
|
|
|
* or transactions to the list of those which have
|
|
|
|
|
* not been done.
|
|
|
|
|
*/
|
|
|
|
|
i++;
|
|
|
|
|
while (i < count)
|
|
|
|
|
{
|
|
|
|
|
id o = [_info objectAtIndex: i++];
|
|
|
|
|
|
|
|
|
|
if ([o isKindOfClass: NSArrayClass] == YES)
|
|
|
|
|
{
|
2015-04-12 09:50:50 +00:00
|
|
|
|
[failures addPrepared: o];
|
2009-09-08 08:05:31 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
[failures append: (SQLTransaction*)o];
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-07-09 17:08:22 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
NS_ENDHANDLER
|
2014-10-07 09:15:16 +00:00
|
|
|
|
[dbLock unlock];
|
2007-07-09 17:08:22 +00:00
|
|
|
|
}
|
|
|
|
|
return executed;
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-08 08:05:31 +00:00
|
|
|
|
- (void) insertTransaction: (SQLTransaction*)trn atIndex: (unsigned)index
|
|
|
|
|
{
|
|
|
|
|
if (index > [_info count])
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSRangeException
|
|
|
|
|
format: @"[%@-%@] index too large",
|
|
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
|
|
|
|
|
}
|
|
|
|
|
if (trn == nil || trn->_count == 0)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"[%@-%@] attempt to insert nil/empty transaction",
|
|
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
|
|
|
|
|
}
|
2012-06-17 14:57:48 +00:00
|
|
|
|
if (NO == [_db isEqual: trn->_db])
|
2009-09-08 08:05:31 +00:00
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"[%@-%@] database client missmatch",
|
|
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
|
|
|
|
|
}
|
|
|
|
|
trn = [trn copy];
|
|
|
|
|
[_info addObject: trn];
|
|
|
|
|
_count += trn->_count;
|
2009-11-18 11:11:29 +00:00
|
|
|
|
[trn release];
|
2009-09-08 08:05:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) removeTransactionAtIndex: (unsigned)index
|
|
|
|
|
{
|
|
|
|
|
id o;
|
|
|
|
|
|
|
|
|
|
if (index >= [_info count])
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSRangeException
|
|
|
|
|
format: @"[%@-%@] index too large",
|
|
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
|
|
|
|
|
}
|
|
|
|
|
o = [_info objectAtIndex: index];
|
|
|
|
|
if ([o isKindOfClass: NSArrayClass] == YES)
|
|
|
|
|
{
|
|
|
|
|
_count--;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_count -= [(SQLTransaction*)o totalCount];
|
|
|
|
|
}
|
|
|
|
|
[_info removeObjectAtIndex: index];
|
|
|
|
|
}
|
|
|
|
|
|
2004-07-26 08:56:26 +00:00
|
|
|
|
- (void) reset
|
|
|
|
|
{
|
|
|
|
|
[_info removeAllObjects];
|
|
|
|
|
_count = 0;
|
|
|
|
|
}
|
2009-09-08 08:05:31 +00:00
|
|
|
|
|
2014-08-09 14:02:11 +00:00
|
|
|
|
- (uint8_t) setMerge: (uint8_t)history
|
|
|
|
|
{
|
|
|
|
|
uint8_t old = _merge;
|
|
|
|
|
|
|
|
|
|
_merge = history;
|
|
|
|
|
return old;
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-08 08:05:31 +00:00
|
|
|
|
- (unsigned) totalCount
|
|
|
|
|
{
|
|
|
|
|
return _count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (SQLTransaction*) transactionAtIndex: (unsigned)index
|
|
|
|
|
{
|
|
|
|
|
id o;
|
|
|
|
|
|
|
|
|
|
if (index >= [_info count])
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSRangeException
|
|
|
|
|
format: @"[%@-%@] index too large",
|
|
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
|
|
|
|
|
}
|
|
|
|
|
o = [_info objectAtIndex: index];
|
|
|
|
|
if ([o isKindOfClass: NSArrayClass] == YES)
|
|
|
|
|
{
|
|
|
|
|
SQLTransaction *t = [[self db] transaction];
|
|
|
|
|
|
2015-04-12 09:50:50 +00:00
|
|
|
|
[t addPrepared: o];
|
2009-09-08 08:05:31 +00:00
|
|
|
|
return t;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
o = [o copy];
|
2009-11-18 11:11:29 +00:00
|
|
|
|
return [o autorelease];
|
2009-09-08 08:05:31 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2004-04-26 15:13:27 +00:00
|
|
|
|
@end
|
|
|
|
|
|
2012-10-22 15:57:56 +00:00
|
|
|
|
|
|
|
|
|
@implementation SQLClient (Notifications)
|
|
|
|
|
|
|
|
|
|
static NSString *
|
|
|
|
|
validName(NSString *name)
|
|
|
|
|
{
|
|
|
|
|
const char *ptr;
|
|
|
|
|
|
|
|
|
|
if (NO == [name isKindOfClass: [NSString class]])
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"Notification name must be a string"];
|
|
|
|
|
}
|
|
|
|
|
ptr = [name UTF8String];
|
|
|
|
|
if (!isalpha(*ptr))
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"Notification name must begin with letter"];
|
|
|
|
|
}
|
|
|
|
|
ptr++;
|
|
|
|
|
while (0 != *ptr)
|
|
|
|
|
{
|
|
|
|
|
if (!isdigit(*ptr) && !isalpha(*ptr) && *ptr != '_')
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"Notification name must contain only letters,"
|
|
|
|
|
@" digits, and underscores"];
|
|
|
|
|
}
|
|
|
|
|
ptr++;
|
|
|
|
|
}
|
|
|
|
|
return [name lowercaseString];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) addObserver: (id)anObserver
|
|
|
|
|
selector: (SEL)aSelector
|
|
|
|
|
name: (NSString*)name
|
|
|
|
|
{
|
|
|
|
|
NSMutableSet *set;
|
|
|
|
|
|
|
|
|
|
name = validName(name);
|
|
|
|
|
[lock lock];
|
2012-11-29 11:40:04 +00:00
|
|
|
|
NS_DURING
|
2012-10-22 15:57:56 +00:00
|
|
|
|
{
|
2012-11-29 11:40:04 +00:00
|
|
|
|
if (nil == _observers)
|
2012-10-22 15:57:56 +00:00
|
|
|
|
{
|
2012-11-29 11:40:04 +00:00
|
|
|
|
_observers = NSCreateMapTable(NSNonRetainedObjectMapKeyCallBacks,
|
|
|
|
|
NSObjectMapValueCallBacks, 0);
|
|
|
|
|
_names = [NSCountedSet new];
|
2012-10-22 15:57:56 +00:00
|
|
|
|
}
|
2012-11-29 11:40:04 +00:00
|
|
|
|
set = (NSMutableSet*)NSMapGet(_observers, (void*)anObserver);
|
|
|
|
|
if (nil == set)
|
|
|
|
|
{
|
|
|
|
|
set = [NSMutableSet new];
|
|
|
|
|
NSMapInsert(_observers, anObserver, set);
|
|
|
|
|
[set release];
|
|
|
|
|
}
|
|
|
|
|
if (nil == [set member: name])
|
|
|
|
|
{
|
|
|
|
|
NSUInteger count = [_names countForObject: name];
|
|
|
|
|
|
|
|
|
|
[set addObject: name];
|
|
|
|
|
[_names addObject: name];
|
|
|
|
|
if (0 == count)
|
|
|
|
|
{
|
|
|
|
|
[self backendListen: name];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver: anObserver
|
|
|
|
|
selector: aSelector
|
|
|
|
|
name: name
|
|
|
|
|
object: self];
|
2012-10-22 15:57:56 +00:00
|
|
|
|
}
|
2012-11-29 11:40:04 +00:00
|
|
|
|
NS_HANDLER
|
|
|
|
|
{
|
|
|
|
|
[lock unlock];
|
|
|
|
|
[localException raise];
|
|
|
|
|
}
|
|
|
|
|
NS_ENDHANDLER
|
2012-10-22 15:57:56 +00:00
|
|
|
|
[lock unlock];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) postNotificationName: (NSString*)name payload: (NSString*)more
|
|
|
|
|
{
|
|
|
|
|
name = validName(name);
|
|
|
|
|
if (nil != more)
|
|
|
|
|
{
|
|
|
|
|
if (NO == [more isKindOfClass: [NSString class]])
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"Notification payload is not a string"];
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-05-16 10:03:51 +00:00
|
|
|
|
[lock lock];
|
|
|
|
|
NS_DURING
|
|
|
|
|
{
|
|
|
|
|
[self backendNotify: name payload: more];
|
|
|
|
|
}
|
|
|
|
|
NS_HANDLER
|
|
|
|
|
{
|
|
|
|
|
[lock unlock];
|
|
|
|
|
[localException raise];
|
|
|
|
|
}
|
|
|
|
|
NS_ENDHANDLER
|
|
|
|
|
[lock unlock];
|
2012-10-22 15:57:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) removeObserver: (id)anObserver name: (NSString*)name
|
|
|
|
|
{
|
|
|
|
|
if (nil != name)
|
|
|
|
|
{
|
|
|
|
|
name = validName(name);
|
|
|
|
|
}
|
|
|
|
|
[lock lock];
|
2012-11-29 11:40:04 +00:00
|
|
|
|
NS_DURING
|
2012-10-22 15:57:56 +00:00
|
|
|
|
{
|
2012-11-29 11:40:04 +00:00
|
|
|
|
if (_observers != nil)
|
2012-10-22 15:57:56 +00:00
|
|
|
|
{
|
2012-11-29 11:40:04 +00:00
|
|
|
|
NSNotificationCenter *nc;
|
|
|
|
|
NSMutableSet *set;
|
|
|
|
|
NSEnumerator *e;
|
|
|
|
|
|
|
|
|
|
nc = [NSNotificationCenter defaultCenter];
|
|
|
|
|
set = (NSMutableSet*)NSMapGet(_observers, (void*)anObserver);
|
|
|
|
|
if (nil == name)
|
2012-10-22 15:57:56 +00:00
|
|
|
|
{
|
2012-11-29 11:40:04 +00:00
|
|
|
|
e = [[set allObjects] objectEnumerator];
|
|
|
|
|
name = [e nextObject];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-03-17 12:19:55 +00:00
|
|
|
|
e = nil;
|
2012-11-29 11:40:04 +00:00
|
|
|
|
name = [[name retain] autorelease];
|
|
|
|
|
}
|
|
|
|
|
while (nil != name)
|
|
|
|
|
{
|
|
|
|
|
if (nil != [set member: name])
|
2012-10-22 15:57:56 +00:00
|
|
|
|
{
|
2012-11-29 11:40:04 +00:00
|
|
|
|
[nc removeObserver: anObserver
|
|
|
|
|
name: name
|
|
|
|
|
object: self];
|
|
|
|
|
[[name retain] autorelease];
|
|
|
|
|
[set removeObject: name];
|
|
|
|
|
[_names removeObject: name];
|
|
|
|
|
if (0 == [_names countForObject: name])
|
|
|
|
|
{
|
|
|
|
|
[self backendUnlisten: name];
|
|
|
|
|
}
|
2012-10-22 15:57:56 +00:00
|
|
|
|
}
|
2012-11-29 11:40:04 +00:00
|
|
|
|
name = [e nextObject];
|
2012-10-22 15:57:56 +00:00
|
|
|
|
}
|
2014-04-12 07:24:31 +00:00
|
|
|
|
if ([set count] == 0)
|
|
|
|
|
{
|
|
|
|
|
NSMapRemove(_observers, (void*)anObserver);
|
|
|
|
|
}
|
2012-10-22 15:57:56 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2012-11-29 11:40:04 +00:00
|
|
|
|
NS_HANDLER
|
|
|
|
|
{
|
|
|
|
|
[lock unlock];
|
|
|
|
|
[localException raise];
|
|
|
|
|
}
|
|
|
|
|
NS_ENDHANDLER
|
2012-10-22 15:57:56 +00:00
|
|
|
|
[lock unlock];
|
|
|
|
|
}
|
|
|
|
|
@end
|
2013-03-04 14:47:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@implementation SQLDictionaryBuilder
|
|
|
|
|
- (void) addObject: (id)anObject
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) alloc
|
|
|
|
|
{
|
|
|
|
|
return [self retain];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSMutableDictionary*) content
|
|
|
|
|
{
|
|
|
|
|
return content;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
|
{
|
|
|
|
|
[content release];
|
|
|
|
|
[super dealloc];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithCapacity: (NSUInteger)capacity
|
|
|
|
|
{
|
2015-01-06 10:56:39 +00:00
|
|
|
|
if (nil != (self = [super init]))
|
|
|
|
|
{
|
|
|
|
|
DESTROY(content);
|
|
|
|
|
content = [[NSMutableDictionary alloc] initWithCapacity: capacity];
|
|
|
|
|
}
|
2013-03-04 14:47:29 +00:00
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-21 09:56:09 +00:00
|
|
|
|
- (id) mutableCopyWithZone: (NSZone*)aZone
|
|
|
|
|
{
|
|
|
|
|
return [content mutableCopyWithZone: aZone];
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-06 22:46:15 +00:00
|
|
|
|
- (id) newWithValues: (id*)values
|
|
|
|
|
keys: (NSString**)keys
|
|
|
|
|
count: (unsigned int)count
|
2013-03-04 14:47:29 +00:00
|
|
|
|
{
|
|
|
|
|
if (count != 2)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"Query did not return key/value pairs"];
|
|
|
|
|
}
|
|
|
|
|
[content setObject: values[1] forKey: values[0]];
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
2014-02-15 07:16:26 +00:00
|
|
|
|
@implementation SQLSetBuilder
|
|
|
|
|
- (NSUInteger) added
|
|
|
|
|
{
|
|
|
|
|
return added;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) addObject: (id)anObject
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) alloc
|
|
|
|
|
{
|
|
|
|
|
return [self retain];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSCountedSet*) content
|
|
|
|
|
{
|
|
|
|
|
return content;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
|
{
|
|
|
|
|
[content release];
|
|
|
|
|
[super dealloc];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithCapacity: (NSUInteger)capacity
|
|
|
|
|
{
|
2015-01-06 10:56:39 +00:00
|
|
|
|
if (nil != (self = [super init]))
|
|
|
|
|
{
|
|
|
|
|
DESTROY(content);
|
|
|
|
|
content = [[NSCountedSet alloc] initWithCapacity: capacity];
|
|
|
|
|
added = 0;
|
|
|
|
|
}
|
2014-02-15 07:16:26 +00:00
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-21 09:56:09 +00:00
|
|
|
|
- (id) mutableCopyWithZone: (NSZone*)aZone
|
|
|
|
|
{
|
|
|
|
|
return [content mutableCopyWithZone: aZone];
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-15 07:16:26 +00:00
|
|
|
|
- (id) newWithValues: (id*)values
|
|
|
|
|
keys: (NSString**)keys
|
|
|
|
|
count: (unsigned int)count
|
|
|
|
|
{
|
|
|
|
|
if (count != 1)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"Query did not return a single value"];
|
|
|
|
|
}
|
|
|
|
|
added++;
|
|
|
|
|
[content addObject: values[0]];
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
2013-03-04 14:47:29 +00:00
|
|
|
|
@implementation SQLSingletonBuilder
|
2013-03-06 22:46:15 +00:00
|
|
|
|
- (id) newWithValues: (id*)values
|
|
|
|
|
keys: (NSString**)keys
|
|
|
|
|
count: (unsigned int)count
|
2013-03-04 14:47:29 +00:00
|
|
|
|
{
|
|
|
|
|
/* Instead of creating an object to hold the supplied record,
|
|
|
|
|
* we use the field from the record as the value to be used.
|
|
|
|
|
*/
|
|
|
|
|
if (count != 1)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"Query did not return singleton values"];
|
|
|
|
|
}
|
|
|
|
|
return [values[0] retain];
|
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
2014-10-13 10:47:06 +00:00
|
|
|
|
@implementation SQLClientPool (Adjust)
|
|
|
|
|
|
|
|
|
|
+ (void) _adjustPoolConnections: (int)n
|
|
|
|
|
{
|
|
|
|
|
unsigned err = 0;
|
|
|
|
|
|
|
|
|
|
[clientsLock lock];
|
|
|
|
|
poolConnections += n;
|
|
|
|
|
if (poolConnections < 0)
|
|
|
|
|
{
|
|
|
|
|
err -= poolConnections;
|
|
|
|
|
poolConnections = 0;
|
|
|
|
|
}
|
|
|
|
|
[clientsLock unlock];
|
|
|
|
|
NSAssert(0 == err, NSInvalidArgumentException);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|