File: MSchemaEditHelper.m

package info (click to toggle)
mysql-admin 1.2.5rc-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 80,944 kB
  • ctags: 43,103
  • sloc: sql: 295,916; pascal: 256,535; cpp: 74,487; ansic: 68,881; objc: 26,417; sh: 16,867; yacc: 10,755; java: 9,917; xml: 8,453; php: 2,806; python: 2,068; makefile: 1,252; perl: 3
file content (555 lines) | stat: -rw-r--r-- 16,563 bytes parent folder | download | duplicates (2)
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
//
//  MSchemaEditHelper.m
//  MySQLGUICommon
//
//  Created by Alfredo Kojima on 3/23/05.
//  Copyright 2005 MySQL AB. All rights reserved.
//

#import "MSchemaEditHelper.h"
#import "MMySQLDispatcher.h"
#import "MSchemaDataSource.h"
#import "MDialogs.h"

#import "MTableEditor.h"
#include "myxutil.h"
#import "mxUtils.h"

@implementation MSchemaEditHelper


- (id)init
{
  self= [super init];
  if (self)
  {
    _editors= [[NSMutableArray alloc] init];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(tableEditorChange:)
                                                 name:MTableEditorTableDidChange
                                               object:nil];
  }
  return self;
}

- (void)dealloc
{
  [_editors makeObjectsPerformSelector:@selector(close:) withObject:nil];
  [_editors release];
  
  [[NSNotificationCenter defaultCenter] removeObserver:self];
  [super dealloc];
}

- (void)tableEditorChange:(NSNotification*)notif
{
  [delegate performSelector:@selector(schemaHelperChangedSchemata:) withObject:self];
}

- (void)tableEditorClose:(NSNotification*)notif
{
  id window= [notif object];
  unsigned int i, c= [_editors count];
  for (i= 0; i < c; i++)
  {
    id obj= [_editors objectAtIndex:i];
    if ([obj window] == window)
    {
      [_editors removeObject:obj];
      break;
    }
  }
}


- (void)setConnection:(MYSQL*)mysql
{
  _mysql= mysql;
}

- (void)setDelegate:(id)deleg
{
  delegate= deleg;
}

- (IBAction)createSchema:(id)sender
{
  MStringRequestSheet *sheet= [MStringRequestSheet sheetWithTitle:@"Create Schema"
														   labels:[NSArray arrayWithObject:@"New Schema Name:"]];
  NSArray *name;
  
  if ((name= [sheet runModal:parentWindow]))
  {
	char *query;
	MYX_LIB_ERROR error;
    
	query= g_strdup_printf("create database `%s`", [[name objectAtIndex:0] UTF8String]);
    myx_query_execute_direct(_mysql,query,&error);
    g_free(query);
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(tableEditorClose:)
                                                 name:NSWindowWillCloseNotification
                                               object:nil];
  
    [delegate performSelector:@selector(schemaHelperChangedSchemata:) withObject:self];
  }
}

- (void)editSchema:(MSchemaItem*)item
{
  MStringRequestSheet *sheet= [MStringRequestSheet sheetWithTitle:@"Edit Schema"
														   labels:[NSArray arrayWithObject:@"Schema Name:"]];
  NSArray *name;

  if ((name= [sheet runModal:parentWindow]))
  {
	char *query;
	MYX_LIB_ERROR error;

	query= g_strdup_printf("create database `%s`", [[name objectAtIndex:0] UTF8String]);
    myx_query_execute_direct(_mysql, query, &error);
    g_free(query);

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(tableEditorClose:)
                                                 name:NSWindowWillCloseNotification
                                               object:nil];

    [delegate performSelector:@selector(schemaHelperChangedSchemata:) withObject:self];
  }  
}

- (IBAction)createTable:(id)sender
{
  MSchemaItem *item= [outline itemAtRow:[outline selectedRow]];
  MYX_SCHEMA *schema= [item schema];
  MTableEditor *editor;
  
  if (schema)
  {
    editor= [[MTableEditor alloc] init];
    
    [editor setConnection:_mysql];
    [editor setCatalogs:(MYX_CATALOGS*)[[outline dataSource] catalogs]];
    [editor showNewTableForCatalog:NSStr(schema->catalog_name)
                            schema:NSStr(schema->schema_name)];
    
    [_editors addObject:editor];
    [editor setReleaseOnClose:YES];
  }
}

- (void)editTable:(MSchemaItem*)item
{
  MTableEditor *editor= [[MTableEditor alloc] init];
  MYX_SCHEMA_TABLE *table= [item table];
  MYX_SCHEMA *schema= [item schema];
  
  [editor setConnection:_mysql];
  [editor setCatalogs:(MYX_CATALOGS*)[[outline dataSource] catalogs]];
  [editor showTable:NSStr((char*)table->table_name) catalog:NSStr(schema->catalog_name)
             schema:NSStr(schema->schema_name)];
  
  [_editors addObject:editor];
  [editor setReleaseOnClose:YES];
}


- (void)editView:(MSchemaItem*)item
{
  MYX_SCHEMA_TABLE *view= [item table];
  MYX_SCHEMA *schema= [item schema];

  MYX_DBM_VIEW_DATA *view_data= myx_dbm_get_view_data(_mysql, schema->catalog_name,
                                                      schema->schema_name,
                                                      view->table_name,
                                                      MYX_DEFAULT_QUOTE_CHAR);

  if (view_data)
    [delegate schemaHelper:self editScript:
      [NSString stringWithFormat:@"DROP VIEW IF EXISTS `%s`;\n%@;\n",
        view->table_name,
        [NSString stringWithUTF8String:view_data->definition]]];
  else
    MXRunAlertPanelWithMySQLError(@"Error", @"Could not retrieve View definition.",
                                  _mysql);
}


- (void)editSP:(MSchemaItem*)item
{
  MYX_SCHEMA_STORED_PROCEDURE *sp= [item sp];
  MYX_SCHEMA *schema= [item schema];
  MYX_DBM_STORED_PROCEDURE_DATA *sp_data= myx_dbm_get_sp_data(_mysql,schema->catalog_name,
                                                              schema->schema_name,
                                                              sp->name, sp->sp_type,
                                                              MYX_DEFAULT_QUOTE_CHAR);
  if (sp_data)
    [delegate schemaHelper:self editScript:
      [NSString stringWithFormat:@"DROP PROCEDURE IF EXISTS `%s`;\nDELIMITER $$\n%@\n$$",
        sp->name,
        [NSString stringWithUTF8String:sp_data->definition]]];
  else
  {
    MXRunAlertPanelWithMySQLError(@"Error", @"Could not retrieve Stored Procedure definition.",
                                  _mysql);
  }
}


- (void)dropObjectAlertDidEnd:(NSAlert *)sheet 
                   returnCode:(int)returnCode
                  contextInfo:(void *)contextInfo
{
  MSchemaItem *item= (MSchemaItem*)contextInfo;
  char *query= NULL;
  MYX_LIB_ERROR error;
  
  [[sheet window] orderOut:self];
  
  if (returnCode != NSAlertAlternateReturn)
    return;

  switch ([item type])
  {
    case MTableItemType:
      query= g_strdup_printf("drop table `%s`.`%s`", [item schema]->schema_name, [item table]->table_name);
      break;
    case MViewItemType:
      query= g_strdup_printf("drop view `%s`.`%s`", [item schema]->schema_name, [item table]->table_name);
      break;
    case MSPItemType:
      query= g_strdup_printf("drop procedure `%s`.`%s`", [item schema]->schema_name, [item sp]->name);
      break;
    default:
      break;
  }
  myx_query_execute_direct(_mysql,query,&error);
  g_free(query);

  [delegate performSelector:@selector(schemaHelperChangedSchemata:) withObject:self];
}



- (void)dropTable:(MSchemaItem*)item
{
  NSAlert *alert = [NSAlert alertWithMessageText:@"Drop Table?"
                                   defaultButton:@"Cancel" 
                                 alternateButton:@"Drop Table"
                                     otherButton:nil
                       informativeTextWithFormat:@"The table '%s' will be dropped and all data contained in it will be permanently erased.",
    [item table]->table_name];

  [alert beginSheetModalForWindow:parentWindow modalDelegate:self 
                   didEndSelector:@selector(dropObjectAlertDidEnd:returnCode:contextInfo:) 
                      contextInfo:item];
}


- (void)dropSP:(MSchemaItem*)item
{
  NSAlert *alert = [NSAlert alertWithMessageText:@"Drop Stored Procedure?"
                                   defaultButton:@"Cancel" 
                                 alternateButton:@"Drop SP"
                                     otherButton:nil
                       informativeTextWithFormat:@"The Stored Procedure '%s' will be dropped.",
    [item sp]->name];
  
  [alert beginSheetModalForWindow:parentWindow modalDelegate:self 
                   didEndSelector:@selector(dropObjectAlertDidEnd:returnCode:contextInfo:) 
                      contextInfo:item];
}


- (void)dropView:(MSchemaItem*)item
{
  NSAlert *alert = [NSAlert alertWithMessageText:@"Drop View?"
                                   defaultButton:@"Cancel" 
                                 alternateButton:@"Drop View"
                                     otherButton:nil
                       informativeTextWithFormat:@"The view '%s' will be dropped.",
    [item table]->table_name];
  
  [alert beginSheetModalForWindow:parentWindow modalDelegate:self 
                   didEndSelector:@selector(dropObjectAlertDidEnd:returnCode:contextInfo:) 
                      contextInfo:item];
}


- (void)dropSchemaAlertDidEnd:(NSAlert *)sheet 
                   returnCode:(int)returnCode
                  contextInfo:(void *)contextInfo
{
  MSchemaItem *item= (MSchemaItem*)contextInfo;
  char *query;
  MYX_LIB_ERROR error;
  
  [[sheet window] orderOut:self];
  
  if (returnCode != NSAlertAlternateReturn)
    return;
  
  if (strcmp([item schema]->schema_name, "mysql")==0)
  {
    NSRunAlertPanel(@"Error",@"MySQL schema cannot be dropped.",
                    @"OK",nil,nil);
    return;
  }

  myx_query_execute_direct(_mysql,"/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;",&error);  
  query= g_strdup_printf("drop database `%s`", [item schema]->schema_name);
  myx_query_execute_direct(_mysql,query,&error);
  g_free(query);
  myx_query_execute_direct(_mysql,"/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;",&error);
  
  [delegate performSelector:@selector(schemaHelperChangedSchemata:) withObject:self];
}


- (void)dropSchema:(MSchemaItem*)item
{
  NSAlert *alert = [NSAlert alertWithMessageText:@"Drop Schema?"
                                   defaultButton:@"Cancel" 
                                 alternateButton:@"Drop Schema"
                                     otherButton:nil
                       informativeTextWithFormat:@"The schema (database) '%s' will be dropped and all its tables will be permanently erased.",
    [item schema]->schema_name];

  [alert beginSheetModalForWindow:parentWindow modalDelegate:self 
                   didEndSelector:@selector(dropSchemaAlertDidEnd:returnCode:contextInfo:) 
                      contextInfo:item];
}


- (IBAction)createSP:(id)sender
{
  int row= [outline selectedRow];
  MSchemaItem *item= [outline itemAtRow:row];
  MYX_SCHEMA *schema= [item schema];
  MStringRequestSheet *sheet;
  NSArray *name;
  NSString *template;
  
  if (!schema)
    return;
  
  sheet= [[MStringRequestSheet alloc] initWithTitle:@"Give a name for the new Stored Procedure or Function"
                                             labels:[NSArray arrayWithObject:@"Routine Name:"]
                                            buttons:[NSArray arrayWithObjects:@"Create Procedure",@"Create Function",@"Cancel",nil]];
  
  name= [sheet runModal:parentWindow];
  if (name && [sheet clickedButton]!=2)
  {
    const char *type;
    const char *rtype;
    
    if ([sheet clickedButton]==0)
    {
      type= "PROCEDURE";
      rtype= "";
    }
    else
    {
      type= "FUNCTION";
      rtype= " RETURNS INT";
    }
    
    template= [NSString stringWithFormat:
      @"DELIMITER $$\n\n"
      "DROP %s IF EXISTS `%s`.`%@`$$\n"
      "CREATE %s `%s`.`%@` ()%s\n"
      "BEGIN\n\n"
      "END$$\n\n"
      "DELIMITER ;\n",
      type, schema->schema_name, [name objectAtIndex:0],
      type, schema->schema_name, [name objectAtIndex:0], rtype];
    
    [delegate schemaHelper:self editScript:template];    
  }
  [sheet release];
}

- (IBAction)createView:(id)sender
{
  NSString *template;
  int row= [outline selectedRow];
  MSchemaItem *item= [outline itemAtRow:row];
  MYX_SCHEMA *schema= [item schema];
  const char *table_name= "";
  NSArray *name;
  MStringRequestSheet *sheet;
  
  sheet= [[MStringRequestSheet alloc] initWithTitle:@"Give a name for the new View"
                                             labels:[NSArray arrayWithObject:@"View Name:"]
                                            buttons:[NSArray arrayWithObjects:@"Create View",@"Cancel",nil]];
  
  name= [sheet runModal:parentWindow];
  if ([sheet clickedButton]==0)
  {
    if ([item type] == MTableItemType)
      table_name= [item table]->table_name;
  
    template= [NSString stringWithFormat:
      @"CREATE VIEW `%s`.`%@` AS\n    SELECT * FROM `%s`",
      schema->schema_name, [name objectAtIndex:0],
      table_name];
    
    [delegate schemaHelper:self editScript:template];
  }
  [sheet release];
}


- (IBAction)editSelection:(id)sender
{
  int row= [outline selectedRow];
  
  if (row >= 0)
  {
    MSchemaItem *item= [outline itemAtRow:row];
    
    switch (item->type)
    {
      case MSchemaItemType:
        break;
      case MTableItemType:
        [self editTable:item];
        break;
      case MSPItemType:
        [self editSP:item];
        break;
      case MViewItemType:
        [self editView:item];
        break;
      default:
        break;
    }
  }
}

- (IBAction)dropSelection:(id)sender
{
  int row= [outline selectedRow];
  
  if (row >= 0)
  {
    MSchemaItem *item= [outline itemAtRow:row];
    
    switch (item->type)
    {
      case MSchemaItemType:
        [self dropSchema:item];
        break;
      case MTableItemType:
        [self dropTable:item];
        break;
      case MSPItemType:
        [self dropSP:item];
        break;
      case MViewItemType:
        [self dropView:item];
        break;        
      default:
        break;
    }
  }    
}


- (NSString*)getCreateCommandForItem:(MSchemaItem*)item
{
  NSString *s;
  char *sql;
  
  switch (item->type)
  {
    case MSchemaItemType:
      sql= myx_dbm_get_create_sql(_mysql,
                                  [item schema]->catalog_name,
                                  [item schema]->schema_name,
                                  [item schema]->schema_name,
                                  MYX_DBM_OT_SCHEMA,
                                  MYX_DEFAULT_QUOTE_CHAR, 0);
      break;
    case MTableItemType:
      sql= myx_dbm_get_create_sql(_mysql,
                                  [item schema]->catalog_name,
                                  [item schema]->schema_name,
                                  [item table]->table_name,
                                  MYX_DBM_OT_TABLE,
                                  MYX_DEFAULT_QUOTE_CHAR, 0);
      break;
    case MSPItemType:
      sql= myx_dbm_get_create_sql(_mysql,
                                  [item schema]->catalog_name,
                                  [item schema]->schema_name,
                                  [item sp]->name,
                                  [item sp]->sp_type==MSPT_PROCEDURE?MYX_DBM_OT_PROCEDURE:MYX_DBM_OT_FUNCTION,
                                  MYX_DEFAULT_QUOTE_CHAR, 0);
      break;
    case MViewItemType:
      sql= myx_dbm_get_create_sql(_mysql,
                                  [item schema]->catalog_name,
                                  [item schema]->schema_name,
                                  [item table]->table_name,
                                  MYX_DBM_OT_VIEW,
                                  MYX_DEFAULT_QUOTE_CHAR, 0);
      break;        
    default:
      sql= NULL;
      break;
  }  
  if (sql)
  {
    s= [NSString stringWithUTF8String:sql];
    g_free(sql);
  }
  else
    s= nil;
  return s;
}


- (IBAction)copySelectionSQL:(id)sender
{
  int row= [outline selectedRow];
  
  if (row >= 0)
  {
    MSchemaItem *item= [outline itemAtRow:row];
    NSString *s= [self getCreateCommandForItem:item];
    NSPasteboard *pasteboard= [NSPasteboard generalPasteboard];
    [pasteboard declareTypes:[NSArray arrayWithObject:NSStringPboardType]
                       owner:nil];
    [pasteboard setString:s forType:NSStringPboardType];
  }  
}


- (BOOL)editStoredProceduresInCatalog:(NSString*)catalog schema:(NSString*)schema 
{
  MYX_SCHEMA_STORED_PROCEDURES *sps;
  BOOL ok= NO;
  
  sps= myx_get_schema_sps(_mysql, [catalog UTF8String], [schema UTF8String]);
  if (sps)
  {
    char *script;
    
    script= myx_dbm_make_script_from_sps(_mysql, 
                                         [catalog UTF8String], [schema UTF8String],
                                         sps, MYX_DEFAULT_QUOTE_CHAR);
    if (script)
    {
      [delegate schemaHelper:self editScript:[NSString stringWithUTF8String:script]];
      g_free(script);
      ok= YES;
    }
    myx_free_schema_sps(sps);
  }
  return ok;
}

@end