File: SQLGenerator.m

package info (click to toggle)
gnustep-dl2 0.12.0%2Bgit20250112-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 7,092 kB
  • sloc: objc: 68,223; makefile: 60; sh: 48
file content (341 lines) | stat: -rw-r--r-- 9,549 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/**
 SQLGenerator.m 
 
 Author: Matt Rice <ratmice@gmail.com>
 Date: 2005, 2006
 Author: David Wetzel <dave@turbocat.de>
 Date: 2010
 
 This file is part of EOModelEditor.
 
 <license>
 EOModelEditor 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 3 of the License, or
 (at your option) any later version.
 
 EOModelEditor 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 EOModelEditor; if not, write to the Free Software
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 </license>
 **/

#include "SQLGenerator.h"

#ifdef NeXT_Foundation_LIBRARY
#include <Foundation/Foundation.h>
#else
#include <Foundation/NSException.h>
#include <Foundation/NSDictionary.h>
#endif

#ifdef NeXT_GUI_LIBRARY
#include <AppKit/AppKit.h>
#else
#include <AppKit/NSNibLoading.h>

#include <AppKit/NSButton.h>
#include <AppKit/NSTextView.h>
#include <AppKit/NSSavePanel.h>
#include <AppKit/NSWindow.h>
#endif

#include <Renaissance/Renaissance.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>
#include <EOControl/EODebug.h>

#include <GNUstepBase/GNUstep.h>

#include "EOMEDocument.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 loadGSMarkupNamed: @"SQLGenerator" owner: self];
  return self;
}

- (void) dealloc
{
  DESTROY(_document);
  
  [super dealloc];
}


- (void) awakeFromGSMarkup
{
  [[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) openSQLGeneratorForDocument:(EOMEDocument*)sender
{
  NSString *adaptorName;
  ASSIGN(_document, sender);

  while (loadedNib && !goodToGo)
    {
      /* wait.. */ 
    }
  
  adaptorName = [[_document eomodel] adaptorName];
  if (!adaptorName)
    {
      [_document setAdaptor:self];
    }
  
  adaptorName = [[_document eomodel] adaptorName];

  if (adaptorName)
    [_window makeKeyAndOrderFront:self];
}

- (void)windowWillClose:(NSNotification *)notification
{
  DESTROY(_document);
}

- (IBAction) executeSQL:(id)sender
{
  EOModel   *eomodel = [_document eomodel];
  EOAdaptor *adaptor;
  EOAdaptorContext *context;
  EOAdaptorChannel *channel;
  
  adaptor = [EOAdaptor adaptorWithName: [eomodel adaptorName]];

  Class exprClass = [adaptor expressionClass];
  EOSQLExpression *expr;
  NSDictionary *connDict = [adaptor connectionDictionary];
  
  if ([[_sqlOutput string] length] == 0)
    return;
  
  if (!adaptor)
    {
      [_document setAdaptor:self];
      adaptor = [EOAdaptor adaptorWithName: [eomodel adaptorName]];
      connDict = [adaptor connectionDictionary];
    }
  else if ([[connDict allKeys] count] == 0)
    {
      connDict = [adaptor runLoginPanel];

      if (connDict)
        [adaptor setConnectionDictionary:connDict];
    }
 
  if (!adaptor || [[connDict allKeys] count] == 0)
    {
      NSRunAlertPanel(@"Error",
                      @"SQL generator requires a valid adaptor and connection dictionary",
                      @"Ok",
                      nil,
                      nil);
      return;
    }

  RETAIN(connDict); 
  context = [adaptor createAdaptorContext];
  channel = [context createAdaptorChannel];
 
  /* FIXME 
   * this is a hack for PostgreSQL because it requires you to connect to an
   * existing database to run 'CREATE DATABASE' statements, it should probably
   * not be in EOModelEditor.
   */
  if (_adminScript && [_adminScript length] 
      && [[connDict objectForKey:@"adaptorName"] isEqual:@"PostgreSQLEOAdaptor"])
    {
      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
{
  NSEmitTODO();
}

- (IBAction) saveAs:(id)sender
{
  id savePanel = [NSSavePanel savePanel];
  int result = [savePanel runModal];
  if (result == NSOKButton)
    {
      NSString *path;
      path = [savePanel filename];
      [[_sqlOutput string] writeToFile:path atomically:YES];
    }
}

- (void) generate
{
  EOModel * model = [_document eomodel];
  Class expr;
  EOAdaptor * adaptor;
  NSArray *arr;
  int i, c;
  NSButton *btn;
  
    
  adaptor = [EOAdaptor adaptorWithName: [model adaptorName]];
  expr = [adaptor expressionClass];
  
  if (!expr)
  {
    [_document setAdaptor:self];
    adaptor = [EOAdaptor adaptorWithName: [model adaptorName]];
    
    expr = [adaptor expressionClass];
    if (!expr) return;
  }
  
  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 = [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 = [model entities];
  _otherScript = RETAIN([expr schemaCreationScriptForEntities:arr
                                                      options:opts]);
  
  [_sqlOutput setString:[_adminScript stringByAppendingString:_otherScript]];
}



- (IBAction) switchChanged:(id)sender
{
  RELEASE(_adminScript);
  RELEASE(_otherScript);
  [self generate];
}

@end