libs-gdl2/DBModeler/SQLGenerator.m
ratmice e0c9a227ef * DBModeler/AdaptorsPanel.m (-init): Add parenthesis around
assignment.   Replace label with window title.
(-runAdaptorsPanel:): Remove unused variable.

* DBModeler/DefaultColumnProvider.m: Add missing braces, remove
unused ivars.
(-cellForColumnNamed:): Autorelease cells.

* DBModeler/EOAdditions.m: New EOAttribute KVC methods -allowNull and
 -setAllowNull:.

* DBModeler/GNUmakefile: Add new to project.

* DBModeler/KVDataSource.m (-createObject:): Return nil after
throwing exception.

* DBModeler/MainModelEditor.m
(-dragImageForRows:event:dragImageOffset:):: Enable drag and
drop for relationships.
(-initWithDocument:): Add parenthesis around assignment. Don't
release the document window on close.
(-ecStuff:): temporarily reload everything in the outline view when
something changes.
(-viewSelectedObject:): Remove NSLog.  Rewrite editor activation.
Fix leaks.

* DBModeler/Modeler.m (-applicationWillFinishLaunching:): Add new menu
items. Don't order our menu in.
(-new:,-open:): Move document initializition to _newDocumentWithModel:.
(-_newDocumentWithModel:,-newFromDatabase:): New methods.
(-validateMenuItem:,-generateSQL:): Ditto.

* DBModeler/ModelerAttributeEditor.m (-initWithParentEditor:):
Remove unused variables.
(-displayGroupDidChangeSelection:): return early if there is no
longer a selection.

* DBModeler/ModelerEntityEditor.m:
(-canSupportCurrentSelection): Remove NSLog.
(-displayGroupDidChangeSelection:): Ditto.
(-dealloc:): New method.
(-initWithParentEditor:): Remove unused variables.  Add parens around
assignment.  Release local variables.

* DBModeler/ModelerTableEmbedibleEditor:
(-addDefaultTableColumnsForTableView:displayGroup:): Release table
 columns.
(-addTableColumnForItem:tableView:): Ditto.

* DBModeler/Inspectors/RelationshipInspector.m:
(-selectedEntity, -selectedDestinationAttribute): New methods.
(-selectedSourceAttribute, -indexOfSourceAttribute:): Ditto.
(-indexOfDestinationAttribute:,joinWithSource:destination:): Ditto.
(-selectedJoin:,updateConnectButton,): Ditto.
(-refresh): Rewrite using new methods.
(-numberOfRowsInTableView:): Add fallback return value.
(-tableView:objectValueForTableColumn:row:): Ditto.
(-tableView:selectionDidChange:): If a source or destination
attribute is now selected, select its counterpart.
(-tableView:shouldSelectRow:): New method to disallow entity
selection if there is a destination entity.
(-tableView:willDisplayCell:forTableColumn:row:): New method,
set the cell text color to disabled text color, if we would disallow
 selection.
(-connectionChanged:): Implement disconnection.

* DBModeler/SQLGenerator.h/m: New files initial implementation.
* DBModeler/Resources/SQLGenerator.gorm: Ditto.
* DBModeler/ConsistencyChecker.h/m: Ditto.
* DBModeler/ConsistencyResults.h/m: Ditto.
* DBModeler/ConsistencyResults.gorm: Ditto.


git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gdl2/trunk@21438 72102866-910b-0410-8b05-ffd578937521
2005-07-09 02:07:42 +00:00

266 lines
7.9 KiB
Objective-C

/**
SQLGenerator.m
Author: Matt Rice <ratmice@yahoo.com>
Date: Jul 2005
This file is part of DBModeler.
<license>
DBModeler is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
DBModeler 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 DBModeler; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
</license>
**/
#include "SQLGenerator.h"
#include <Foundation/NSException.h>
#include <Foundation/NSDictionary.h>
#include <AppKit/NSNibLoading.h>
#include <AppKit/NSButton.h>
#include <AppKit/NSTextView.h>
#include <AppKit/NSSavePanel.h>
#include <AppKit/NSWindow.h>
#include <EOAccess/EOAdaptor.h>
#include <EOAccess/EOAdaptorChannel.h>
#include <EOAccess/EOAdaptorContext.h>
#include <EOAccess/EOSchemaGeneration.h>
#include <EOAccess/EOSQLExpression.h>
#include <EOAccess/EOModel.h>
#include <EOModeler/EOModelerApp.h>
#include <EOModeler/EOModelerDocument.h>
static SQLGenerator *sharedGenerator;
static BOOL loadedNib;
static BOOL goodToGo;
static NSMutableArray *adminSwitchButtons;
static NSMutableArray *otherSwitchButtons;
NSMutableDictionary *opts;
static NSString *_adminScript;
static NSString *_otherScript;
@implementation SQLGenerator : NSObject
+ (SQLGenerator *) sharedGenerator;
{
if (!sharedGenerator)
sharedGenerator = [[self allocWithZone:NSDefaultMallocZone()] init];
return sharedGenerator;
}
- (id) init
{
if (sharedGenerator)
{
[[NSException exceptionWithName:NSInternalInconsistencyException
reason: @"singleton initialized multiple times"
userInfo:nil] raise];
return nil;
}
self = [super init];
adminSwitchButtons = [[NSMutableArray alloc] initWithCapacity:3];
otherSwitchButtons = [[NSMutableArray alloc] initWithCapacity:7];
opts = [[NSMutableDictionary alloc] initWithCapacity:8];
loadedNib = [NSBundle loadNibNamed:@"SQLGenerator" owner:self];
return self;
}
- (void) awakeFromNib
{
[[dropDatabaseSwitch cell] setRepresentedObject: EODropDatabaseKey];
[[createDatabaseSwitch cell] setRepresentedObject: EOCreateDatabaseKey];
[[dropTablesSwitch cell] setRepresentedObject: EODropTablesKey];
[[createTablesSwitch cell] setRepresentedObject:EOCreateTablesKey];
[[dropPKSwitch cell] setRepresentedObject:EODropPrimaryKeySupportKey];
[[createPKSwitch cell] setRepresentedObject:EOCreatePrimaryKeySupportKey];
[[createPKConstraintsSwitch cell] setRepresentedObject:EOPrimaryKeyConstraintsKey];
[[createFKConstraintsSwitch cell] setRepresentedObject:EOForeignKeyConstraintsKey];
[adminSwitchButtons addObject:dropDatabaseSwitch];
[adminSwitchButtons addObject:createDatabaseSwitch];
[otherSwitchButtons addObject:dropTablesSwitch];
[otherSwitchButtons addObject:createTablesSwitch];
[otherSwitchButtons addObject:dropPKSwitch];
[otherSwitchButtons addObject:createPKSwitch];
[otherSwitchButtons addObject:createPKConstraintsSwitch];
[otherSwitchButtons addObject:createFKConstraintsSwitch];
goodToGo = YES;
}
- (void) openSQLGenerator:(id)sender;
{
while (loadedNib && !goodToGo)
{
/* wait.. */
}
[_window makeKeyAndOrderFront:self];
}
- (IBAction) executeSQL:(id)sender
{
EOAdaptor *adaptor = [[EOMApp activeDocument] adaptor];
EOAdaptorContext *context;
EOAdaptorChannel *channel;
Class exprClass = [[[EOMApp activeDocument] adaptor] expressionClass];
EOSQLExpression *expr;
NSDictionary *connDict = RETAIN([adaptor connectionDictionary]);
if ([[_sqlOutput string] length] == 0)
return;
if ([adaptor hasOpenChannels])
{
NSArray *contexts = [adaptor contexts];
int i,c;
for (i = 0, c = [contexts count]; i < c; i++)
{
EOAdaptorContext *ctxt = [contexts objectAtIndex:i];
if ([ctxt hasOpenChannels])
{
int j, d;
NSArray *channels = [ctxt channels];
for (j = 0, d = [channels count]; j < d; j++)
{
EOAdaptorChannel *channel = [channels objectAtIndex:j];
if ([channel isOpen])
[channel closeChannel];
}
}
}
}
context = [adaptor createAdaptorContext];
channel = [context createAdaptorChannel];
if (_adminScript && [_adminScript length]
&& [[connDict objectForKey:@"adaptorName"] isEqual:@"Postgres95EOAdaptor"])
{
NSMutableDictionary *tmp = RETAIN([NSMutableDictionary dictionaryWithDictionary:connDict]);
[tmp setObject:@"template1" forKey:@"databaseName"];
[adaptor setConnectionDictionary:tmp];
}
if (_adminScript && [_adminScript length])
{
NS_DURING
[channel openChannel];
NS_HANDLER
NSLog(@"admin exception%@ %@ %@", [localException name], [localException reason], [localException userInfo]);
NS_ENDHANDLER
expr = [exprClass expressionForString:_adminScript];
NS_DURING
[channel evaluateExpression:expr];
NS_HANDLER
NSLog(@"admin exception%@ %@ %@", [localException name], [localException reason], [localException userInfo]);
NS_ENDHANDLER
if ([channel isOpen])
[channel closeChannel];
}
if (_otherScript && [_otherScript length])
{
[adaptor setConnectionDictionary:connDict];
NS_DURING
[channel openChannel];
NS_HANDLER
NSLog(@"exception %@ %@ %@", [localException name], [localException reason], [localException userInfo]);
NS_ENDHANDLER
expr = [exprClass expressionForString:_otherScript];
NS_DURING
[channel evaluateExpression:expr];
NS_HANDLER
NSLog(@"exception %@ %@ %@", [localException name], [localException reason], [localException userInfo]);
NS_ENDHANDLER
if ([channel isOpen])
[channel closeChannel];
}
RELEASE(connDict);
}
- (IBAction) showTables:(id)sender
{
}
- (IBAction) saveAs:(id)sender
{
id savePanel = [NSSavePanel savePanel];
NSString *path;
int result = [savePanel runModal];
if (result == NSOKButton)
{
path = [savePanel filename];
}
[[_sqlOutput string] writeToFile:path atomically:YES];
}
- (IBAction) switchChanged:(id)sender
{
RELEASE(_adminScript);
RELEASE(_otherScript);
[self generate];
}
- (void) generate
{
Class expr = [[[EOMApp activeDocument] adaptor] expressionClass];
NSArray *arr;
int i, c;
NSButton *btn;
for (i = 0, c = [adminSwitchButtons count]; i < c; i++)
{
btn = [adminSwitchButtons objectAtIndex:i];
[opts setObject:([[btn objectValue] boolValue]) ? @"YES" : @"NO"
forKey: [[btn cell] representedObject]];
}
for (i = 0, c = [otherSwitchButtons count]; i < c; i++)
{
btn = [otherSwitchButtons objectAtIndex:i];
[opts setObject:@"NO" forKey:[[btn cell] representedObject]];
}
arr = [[[EOMApp activeDocument] model] entities];
_adminScript = RETAIN([expr schemaCreationScriptForEntities:arr
options:opts]);
for (i = 0, c = [adminSwitchButtons count]; i < c; i++)
{
btn = [adminSwitchButtons objectAtIndex:i];
[opts setObject:@"NO" forKey:[[btn cell] representedObject]];
}
for (i = 0, c = [otherSwitchButtons count]; i < c; i++)
{
btn = [otherSwitchButtons objectAtIndex:i];
[opts setObject:([[btn objectValue] boolValue]) ? @"YES" : @"NO"
forKey: [[btn cell] representedObject]];
}
arr = [[[EOMApp activeDocument] model] entities];
_otherScript = RETAIN([expr schemaCreationScriptForEntities:arr
options:opts]);
[_sqlOutput setString:[_adminScript stringByAppendingString:_otherScript]];
}
@end