baseten / Sources / BXDatabaseObjectModel.m

//
// BXDatabaseObjectModel.m
// BaseTen
//
// Copyright (C) 2009 Marko Karppinen & Co. LLC.
//
// Before using this software, please review the available licensing options
// by visiting http://basetenframework.org/licensing/ or by contacting
// us at sales@karppinen.fi. Without an additional license, this software
// may be distributed only in compliance with the GNU General Public License.
//
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License, version 2.0,
// as published by the Free Software Foundation.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
//
// $Id$
//

#import "BXDatabaseObjectModel.h"
#import "BXDatabaseObjectModelStorage.h"
#import "BXEnumerate.h"
#import "BXEntityDescriptionPrivate.h"
#import "BXInterface.h"
#import "BXLogger.h"


@implementation BXDatabaseObjectModel
- (id) initWithStorage: (BXDatabaseObjectModelStorage *) storage key: (NSURL *) key
{
	if ((self = [super init]))
	{
		mStorage = [storage retain];
		mStorageKey = [key retain];
		mEntitiesBySchemaAndName = [[NSMutableDictionary alloc] init];
		mCanCreateEntities = YES;
	}
	return self;
}

- (id) init
{
	[self doesNotRecognizeSelector: _cmd];
	return nil;
}

- (void) dealloc
{	
	[mEntitiesBySchemaAndName release];
	[mStorage objectModelWillDeallocate: mStorageKey];
	[super dealloc];
}

- (BXEntityDescription *) entityForTable: (NSString *) name error: (NSError **) outError
{
	return [self entityForTable: name inSchema: @"public" error: outError];
}

- (BXEntityDescription *) entityForTable: (NSString *) name inSchema: (NSString *) schemaName error: (NSError **) outError
{
	NSMutableDictionary* schemaDict = nil;
	BXEntityDescription* retval = nil;
	BOOL canCreateEntityDesc = NO;
	@synchronized (mEntitiesBySchemaAndName)
	{
		//We need the lock for this.
		canCreateEntityDesc = mCanCreateEntities;
		
		schemaDict = [[[mEntitiesBySchemaAndName objectForKey: schemaName] retain] autorelease];
		if (! schemaDict)
		{
			schemaDict = [NSMutableDictionary dictionary];
			[mEntitiesBySchemaAndName setObject: schemaDict forKey: schemaName];
		}
	}
	
	@synchronized (schemaDict)
	{
		retval = [[[schemaDict objectForKey: name] retain] autorelease];
		if (! retval && canCreateEntityDesc)
		{
			retval = [[[BXEntityDescription alloc] initWithDatabaseURI: mStorageKey table: name inSchema: schemaName] autorelease];
			[schemaDict setObject: retval forKey: name];
		}
	}
	
	return retval;
}

- (NSArray *) entities: (NSError **) outError
{
	NSMutableArray* retval = [NSMutableArray array];
	NSDictionary* schemas = nil;
	@synchronized (mEntitiesBySchemaAndName)
	{
		schemas = [[mEntitiesBySchemaAndName copy] autorelease];
	}
	
	BXEnumerate (currentSchema, e, [schemas objectEnumerator])
	{
		@synchronized (currentSchema)
		{
			[retval addObjectsFromArray: currentSchema];
		}
	}
	
	return retval;
}

- (NSDictionary *) entitiesBySchemaAndName: (id <BXInterface>) interface reload: (BOOL) shouldReload error: (NSError **) outError
{
	id retval = nil;
	if (shouldReload)
	{
		[interface reloadDatabaseMetadata];
		@synchronized (mEntitiesBySchemaAndName)
		{
			mCanCreateEntities = YES;
			[interface prepareForEntityValidation];
			NSArray* entities = [self entities: outError];
			if (entities && [interface validateEntities: entities error: outError])
				retval = [[mEntitiesBySchemaAndName copy] autorelease];
		}
	}
	else
	{
		@synchronized (mEntitiesBySchemaAndName)
		{
			retval = [[mEntitiesBySchemaAndName copy] autorelease];
		}
	}
	return retval;
}

- (BOOL) contextConnectedUsingDatabaseInterface: (id <BXInterface>) interface error: (NSError **) outError
{
	ExpectR (outError, NO);
	
	BOOL retval = NO;
	[interface prepareForEntityValidation];
	
	NSArray* entities = [self entities: outError];
	if (entities)
		retval = [interface validateEntities: entities error: outError];
	return retval;
}

- (void) setCanCreateEntityDescriptions: (BOOL) aBool
{
	@synchronized (mEntitiesBySchemaAndName)
	{
		mCanCreateEntities = aBool;
	}
}
@end
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.