Commits

Tuukka Norri committed e468cb6

Removed dependency from BXPGEntityConverter to BXDatabaseContext (fixes #185)

Comments (0)

Files changed (4)

Sources/BXPGEntityConverter.h

 // BXPGEntityConverter.h
 // BaseTen
 //
-// Copyright (C) 2006-2008 Marko Karppinen & Co. LLC.
+// Copyright (C) 2006-2010 Marko Karppinen & Co. LLC.
 //
 // Before using this software, please review the available licensing options
 // by visiting http://basetenframework.org/licensing/ or by contacting
 
 #import <Foundation/Foundation.h>
 
-@class BXDatabaseContext;
+@class BXPGEntityConverter;
+@class BXEntityDescription;
+@class NSEntityDescription;
+@class PGTSConnection;
+
+
+@protocol BXPGEntityConverterDelegate <NSObject>
+- (BXEntityDescription *) entityConverter: (BXPGEntityConverter *) converter 
+ shouldAddDropStatementFromEntityMatching: (NSEntityDescription *) importedEntity
+								 inSchema: (NSString *) schemaName
+									error: (NSError **) outError;
+- (BOOL) entityConverter: (BXPGEntityConverter *) converter shouldCreateSchema: (NSString *) schemaName;
+- (PGTSConnection *) connectionForEntityConverter: (BXPGEntityConverter *) converter;
+@end
 
 
 @interface BXPGEntityConverter : NSObject
 {
+	id <BXPGEntityConverterDelegate> mDelegate;
 }
+- (id <BXPGEntityConverterDelegate>) delegate;
+- (void) setDelegate: (id <BXPGEntityConverterDelegate>) delegate;
 - (NSArray *) statementsForEntities: (NSArray *) entityArray 
 						 schemaName: (NSString *) schemaName
-						 	context: (BXDatabaseContext *) context
 				   enabledRelations: (NSArray **) outArray
 							 errors: (NSArray **) outErrors;
 @end

Sources/BXPGEntityConverter.m

 // BXPGEntityConverter.m
 // BaseTen
 //
-// Copyright (C) 2006-2008 Marko Karppinen & Co. LLC.
+// Copyright (C) 2006-2010 Marko Karppinen & Co. LLC.
 //
 // Before using this software, please review the available licensing options
 // by visiting http://basetenframework.org/licensing/ or by contacting
 
 
 @implementation BXPGEntityConverter
+- (void) setDelegate: (id <BXPGEntityConverterDelegate>) delegate
+{
+	mDelegate = delegate;
+}
+
+
+- (id <BXPGEntityConverterDelegate>) delegate
+{
+	return mDelegate;
+}
+
+
 - (NSMutableArray *) add: (NSString *) aName fromUnsatisfied: (NSMutableDictionary *) unsatisfied
 {
     NSMutableArray* retval = [NSMutableArray array];
 
 - (NSArray *) statementsForEntities: (NSArray *) entityArray 
 						 schemaName: (NSString *) schemaName
-						 	context: (BXDatabaseContext *) context
 				   enabledRelations: (NSArray **) outArray
 							 errors: (NSArray **) outErrors
 {
+	Expect (mDelegate);
 	Expect (entityArray);
-	Expect (context);
 	
 	if (! [schemaName length])
 		schemaName = @"public";
 	NSMutableArray* errors = [NSMutableArray array];
 	NSMutableArray* retval = [NSMutableArray array];
 	NSMutableArray* enabledRelations = [NSMutableArray array];
-	PGTSConnection* connection = [[(BXPGInterface *) [context databaseInterface] transactionHandler] connection];
-	PGTSDatabaseDescription* database = [connection databaseDescription];
 	entityArray = [self sortedEntities: entityArray errors: errors];
 	
-	if (! [database schemaNamed: schemaName])
+	if ([mDelegate entityConverter: self shouldCreateSchema: schemaName])
 		[retval addObject: [NSString stringWithFormat: @"CREATE SCHEMA \"%@\";", schemaName]];
 	
 	BXEnumerate (currentEntity, e, [entityArray objectEnumerator])
 	{
         NSError* error = nil;
-		BXEntityDescription* match = [context matchingEntity: currentEntity inSchema: schemaName error: &error];
+		BXEntityDescription *match = [mDelegate entityConverter: self 
+					   shouldAddDropStatementFromEntityMatching: currentEntity 
+													   inSchema: schemaName
+														  error: &error];
 		if (match)
 		{
 			if ([match isView])
         if (error)
             [errors addObject: error];
 		
-		[retval addObject: [currentEntity BXPGCreateStatementWithIDColumn: YES inSchema: schemaName connection: connection errors: errors]];
+		[retval addObject: [currentEntity BXPGCreateStatementWithIDColumn: YES inSchema: schemaName connection: [mDelegate connectionForEntityConverter: self] errors: errors]];
 		[retval addObject: [currentEntity BXPGPrimaryKeyConstraintInSchema: schemaName]];
 		[enabledRelations addObject: [currentEntity name]];
 		
 			if ([currentAttr BXCanAddAttribute: &error])
 			{
 				[retval addObjectsFromArray: [currentAttr BXPGAttributeConstraintsInSchema: schemaName]];
-				[retval addObjectsFromArray: [currentAttr BXPGConstraintsForValidationPredicatesInSchema: schemaName connection: connection]];
+				[retval addObjectsFromArray: [currentAttr BXPGConstraintsForValidationPredicatesInSchema: schemaName connection: [mDelegate connectionForEntityConverter: self]]];
 			}
 
             if (error)

Sources/BXPGEntityImporter.h

 // BXPGEntityImporter.h
 // BaseTen
 //
-// Copyright (C) 2006-2008 Marko Karppinen & Co. LLC.
+// Copyright (C) 2006-2010 Marko Karppinen & Co. LLC.
 //
 // Before using this software, please review the available licensing options
 // by visiting http://basetenframework.org/licensing/ or by contacting
 //
 
 #import <Foundation/Foundation.h>
+#import <BaseTen/BXPGEntityConverter.h>
 @class BXDatabaseContext;
-@class BXPGEntityConverter;
 @class BXPGEntityImporter;
 
 
 - (BOOL) enableEntities: (NSError **) outError;
 - (BOOL) disableEntities: (NSArray *) entities error: (NSError **) outError;
 @end
+
+
+@interface BXPGEntityImporter (BXPGEntityConverterDelegate) <BXPGEntityConverterDelegate>
+@end

Sources/BXPGEntityImporter.m

 // BXPGEntityImporter.m
 // BaseTen
 //
-// Copyright (C) 2006-2008 Marko Karppinen & Co. LLC.
+// Copyright (C) 2006-2010 Marko Karppinen & Co. LLC.
 //
 // Before using this software, please review the available licensing options
 // by visiting http://basetenframework.org/licensing/ or by contacting
 #import "BXPGEntityConverter.h"
 #import "PGTSResultSet.h"
 #import "PGTSHOM.h"
+#import "PGTSDatabaseDescription.h"
 
 
 @implementation BXPGEntityImporter
 {
 	if (mEntityConverter != aConverter)
 	{
+		[mEntityConverter setDelegate: nil];
 		[mEntityConverter release];
 		mEntityConverter = [aConverter retain];
+		[mEntityConverter setDelegate: self];
 	}
 }
 
 		[self setSchemaName: @"public"];
 
 	if (! mEntityConverter)
+	{
 		mEntityConverter = [[BXPGEntityConverter alloc] init];
+		[mEntityConverter setDelegate: self];
+	}
 	
 	NSArray* enabledRelations = nil;
-	NSArray* statements = [mEntityConverter statementsForEntities: mEntities schemaName: mSchemaName
-														  context: mContext enabledRelations: &enabledRelations
+	NSArray* statements = [mEntityConverter statementsForEntities: mEntities 
+													   schemaName: mSchemaName
+												 enabledRelations: &enabledRelations
 														   errors: outErrors];
 	[self setStatements: statements];
 	[self setEnabledRelations: enabledRelations];
 	return retval;
 }
 @end
+
+
+
+@implementation BXPGEntityImporter (BXPGEntityConverterDelegate)
+- (BXEntityDescription *) entityConverter: (BXPGEntityConverter *) converter 
+ shouldAddDropStatementFromEntityMatching: (NSEntityDescription *) importedEntity
+								 inSchema: (NSString *) schemaName
+									error: (NSError **) outError
+{
+	return [mContext matchingEntity: importedEntity inSchema: schemaName error: outError];
+}
+
+
+- (BOOL) entityConverter: (BXPGEntityConverter *) converter shouldCreateSchema: (NSString *) schemaName
+{
+	ExpectR (mContext, NO);
+	PGTSConnection* connection = [[(BXPGInterface *) [mContext databaseInterface] transactionHandler] connection];
+	PGTSDatabaseDescription* database = [connection databaseDescription];
+	return ([database schemaNamed: schemaName] ? NO : YES);
+}
+
+
+- (PGTSConnection *) connectionForEntityConverter: (BXPGEntityConverter *) converter
+{
+	Expect (mContext);
+	return [[(BXPGInterface *) [mContext databaseInterface] transactionHandler] connection];
+}
+@end