File: MSchemaDataSource.m

package info (click to toggle)
mysql-query-browser 1.1.6-1sarge0
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 36,320 kB
  • ctags: 24,680
  • sloc: pascal: 203,479; xml: 136,561; ansic: 47,502; cpp: 28,926; sh: 12,433; objc: 4,823; java: 1,849; php: 1,485; python: 1,225; sql: 1,128; makefile: 872
file content (519 lines) | stat: -rw-r--r-- 10,647 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
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
//
//  MSchemaDataSource.m
//  MySQLGUICommon
//
//  Created by Alfredo Kojima on Sun Jun 27 2004.
//  Copyright (c) 2004 MySQL AB. All rights reserved.
//

#import "MSchemaDataSource.h"


@implementation MSchemaItem

- (id)initWithCatalog: (MYX_CATALOG*)catalog
                 icon: (NSImage*)aicon
{
  if (!(self= [super init]))
    return nil;
  
  type= MCatalogItemType;
  _object.catalog= catalog;
  _repr= nil;
  icon= [aicon retain];

  return self;
}

- (id)initWithSchema: (MYX_SCHEMA*)schema
	   parentCatalog: (MSchemaItem*)parentItem
                icon: (NSImage*)aicon
{
  if (!(self= [super init]))
    return nil;
  
  type= MSchemaItemType;
  _object.schema= schema;
  parent= parentItem;
  _repr= nil;
  icon= [aicon retain];

  return self;
}

- (id)initWithTable: (MYX_SCHEMA_TABLE*)table
	   parentSchema: (MSchemaItem*)parentItem
               icon: (NSImage*)aicon
{
  if (!(self= [super init]))
    return nil;
  
  type= MTableItemType;
  _object.table= table;
  parent= parentItem;
  _repr= nil;
  icon= [aicon retain];

  return self;
}

- (id)initWithColumn: (MYX_SCHEMA_TABLE_COLUMN*)column
		 parentTable: (MSchemaItem*)parentItem
                icon: (NSImage*)aicon
{
  if (!(self= [super init]))
    return nil;
  
  type= MColumnItemType;
  _object.column= column;
  _repr= nil;
  parent= parentItem;
  icon= [aicon retain];

  return self;
}

- (void)dealloc
{
  [_identifier release];
  [_repr release];
  [icon release];
  [super dealloc];
}

- (NSImage*)icon
{
  return icon;
}


- (id)identifier
{
  if (!_identifier)
	_identifier= [[NSString stringWithFormat:@"%p",self] retain];
  return _identifier;
}

- (NSString*)repr
{
  if (!_repr)
  {
    switch (type)
    {
      case MCatalogItemType:
        _repr= [NSString stringWithUTF8String: _object.catalog->catalog_name?:"?"];
        break;
      case MSchemaItemType:
        _repr= [NSString stringWithUTF8String: _object.schema->schema_name?:""];
        break;
      case MTableItemType:
        _repr= [NSString stringWithUTF8String: _object.table->table_name?:""];
        break;
      case MColumnItemType:
        _repr= [NSString stringWithUTF8String: _object.column->column_name?:""];
        break;
    }
    [_repr retain];
  }
  return _repr;
}

- (MYX_CATALOG*)catalog
{
  MSchemaItem *item= nil;
  switch (type)
  {
    case MCatalogItemType:
      item= self;
      break;
    case MSchemaItemType:
      item= self->parent;
      break;
    case MTableItemType:
      item= self->parent->parent;
      break;
    case MColumnItemType:
      item= self->parent->parent->parent;
      break;
  }
  return item ? item->_object.catalog : NULL;
}

- (MYX_SCHEMA*)schema
{
  MSchemaItem *item= nil;
  switch (type)
  {
    case MSchemaItemType:
      item= self;
      break;
    case MTableItemType:
      item= self->parent;
      break;
    case MColumnItemType:
      item= self->parent->parent;
      break;
    default:
      NSAssert(0,@"invalid type (table)");
      break;      
  }
  return item ? item->_object.schema : NULL;
}

- (MYX_SCHEMA_TABLE*)table
{
  MSchemaItem *item= nil;
  switch (type)
  {
    case MTableItemType:
      item= self;
      break;
    case MColumnItemType:
      item= self->parent;
      break;
    default:
      NSAssert(0,@"invalid type (table)");
      break;
  }
  return item ? item->_object.table : NULL;  
}

- (MYX_SCHEMA_TABLE_COLUMN*)column
{
  MSchemaItem *item= nil;
  switch (type)
  {
    case MColumnItemType:
      item= self;
      break;
    default:
      NSAssert(0,@"invalid type (table)");
      break;
  }
  return item ? item->_object.column : NULL;  
}

@end


//==============================================================================

@implementation MSchemaDataSource

- (MSchemaItem*)findItem: (NSString*)name
{
  /*
  int i, c= [_rootList count];
  for (i= 0; i < c; i++)
  {
    if ([[[_rootList objectAtIndex:i] repr] isEqualToString:name])
      return [_rootList objectAtIndex:i];
  }
   */
  return nil;
}


- (id)initWithRoot: (MSchemaDSItemType)root
              leaf: (MSchemaDSItemType)leaf
{
  self= [super init];
  if (self)
  {
    _rootType= root;
    _leafType= leaf;
	_children= [[NSMutableDictionary alloc] init];
	_items= [[NSMutableDictionary alloc] init];  
  }
  return self;
}


- (void)dealloc
{
  [_children release];
  [_items release];

  [super dealloc];
}


- (void)setTableFetcher:(id)target selector:(SEL)sel
{
  _tableFetcher= target;
  _tableFetcherAction= sel;
}


- (void)resetTree
{
  [_children removeAllObjects];
  [_items removeAllObjects];
}

- (void)setChildren: (NSMutableArray*)array
			forItem: (MSchemaItem*)item
{
  unsigned int i, c;
  NSMutableArray *children;

  children= [NSMutableArray arrayWithCapacity:[array count]];
  c= [array count];
  for (i= 0; i < c; i++)
  {
	MSchemaItem *obj= [array objectAtIndex:i];
	[_items setObject:obj forKey:[obj identifier]];
	[children addObject:[obj identifier]];
  }
  [_children setObject:children forKey:item ? [item identifier] : @"root"];
}


- (id)outlineView:(NSOutlineView *)outlineView 
            child:(int)index 
           ofItem:(id)item
{
  if (item == nil)
    return [_items objectForKey:[[_children objectForKey:@"root"] objectAtIndex: index]];
  else
  {
    id tmp= [_children objectForKey:[item identifier]];
    if (!tmp)
    {
	  [_tableFetcher performSelector:_tableFetcherAction
						  withObject:self
						  withObject:item];
	  return @"Loading...";
    }
    else
      return [_items objectForKey:[tmp objectAtIndex:index]];
  }
}

- (BOOL)outlineView:(NSOutlineView *)outlineView 
   isItemExpandable:(id)item
{
  if ([item isMemberOfClass:[MSchemaItem class]] && ((MSchemaItem*)item)->type < _leafType)
    return YES;
  else
    return NO;
}

- (int)outlineView:(NSOutlineView *)outlineView 
numberOfChildrenOfItem:(id)item
{
  if (item == nil)
    return [[_children objectForKey:@"root"] count];
  else if ([item isKindOfClass: [NSString class]]) // the Loading... string
    return 0;
  else
  {
	id tmp= [_children objectForKey:[item identifier]];
    if (!tmp)
	  return 1; // for Loading...
	else
	  return [tmp count];
  }
}

- (id)outlineView:(NSOutlineView *)outlineView 
objectValueForTableColumn:(NSTableColumn *)tableColumn 
           byItem:(id)item
{
  if ([item isMemberOfClass: [MSchemaItem class]])
    return [item repr];
  else
    return item; // NSString for Loading...
}

@end


//==============================================================================

@implementation MFilteredSchemaDataSource

- (id)initWithDataSource:(MSchemaDataSource*)parent
{
  self= [super initWithRoot:parent->_rootType leaf:parent->_leafType];
  if (self)
  {
	_parentDS= parent;
  }
  return self;
}


- (void)setRootList: (NSMutableArray*)array
{
  NSLog(@"NSFilteredSchemaDataSource should not have its rootList changed");
}


- (id)outlineView:(NSOutlineView *)outlineView 
            child:(int)index 
           ofItem:(id)item
{
  if (!_filter)
	return [_parentDS outlineView:outlineView child:index ofItem:item];
  else
	return [super outlineView:outlineView child:index ofItem:item];
  
}

- (BOOL)outlineView:(NSOutlineView *)outlineView 
   isItemExpandable:(id)item
{
  if (!_filter)
	return [_parentDS outlineView:outlineView isItemExpandable:item];
  else
	return [super outlineView:outlineView isItemExpandable:item];
}

- (int)outlineView:(NSOutlineView *)outlineView 
numberOfChildrenOfItem:(id)item
{
  if (!_filter)
	return [_parentDS outlineView:outlineView numberOfChildrenOfItem:item];
  else
	return [super outlineView:outlineView numberOfChildrenOfItem:item];
}

- (id)outlineView:(NSOutlineView *)outlineView 
objectValueForTableColumn:(NSTableColumn *)tableColumn 
           byItem:(id)item
{
  if (!_filter)
	return [_parentDS outlineView:outlineView
	objectValueForTableColumn:tableColumn
					   byItem:item];
  else
	return [super outlineView:outlineView
	objectValueForTableColumn:tableColumn
					   byItem:item];
}


- (BOOL)filterChildren:(NSArray*)children
				ofItem:(MSchemaItem*)item
			  matching:(NSString*)filter
{
  unsigned int i, c= [children count];
  NSMutableArray *array= [NSMutableArray arrayWithCapacity:1];

  for (i= 0; i < c; i++)
  {
	id ident= [children objectAtIndex:i];
	id obj= [_parentDS->_items objectForKey:ident];
	NSArray *ch= [_parentDS->_children objectForKey:ident];
	BOOL ok= NO;

	if ([[obj repr] rangeOfString:filter].location!=NSNotFound)
	  ok= YES,
		NSLog(@"%@ matche", [obj repr]);
		
	if (ch && [self filterChildren:ch ofItem:obj matching:filter])
	  ok= YES;

	if (ok)
	  [array addObject:ident];
  }
  
  [_children setObject:array forKey:[item identifier]];
  
  return [array count]>0;
}


- (BOOL)removeChildren:(NSMutableArray*)children
				ofItem:(MSchemaItem*)item
		   notMatching:(NSString*)filter
{
  int i, c= [children count];
  int matches= 0;

  for (i= c-1; i >= 0; i--)
  {
	id ident= [children objectAtIndex:i];
	id obj= [_parentDS->_items objectForKey:ident];
	NSMutableArray *ch= [_parentDS->_children objectForKey:ident];
	
	if (ch && [self removeChildren:ch ofItem:obj notMatching:filter])
	{
	  matches++;
	}
	else if ([[obj repr] rangeOfString:filter].location!=NSNotFound)
	{
	  matches++;
	}
	else
	{
	  [children removeObject:ident];
	}
  }
  
  return matches>0;
}


- (void)performSearch:(NSString*)filter
{
  int i, c;

//  NSLog(@"filter by %@", filter);
  if (!filter || [filter length] == 0)
  {
	[_filter release];
	_filter= nil;
  }
  else if (!_filter || [filter rangeOfString:_filter].location == NSNotFound)
  {
	NSArray *array;
	NSMutableArray *rootArray= nil;
	
	[_filter release];
	_filter= [filter retain];

	[_children removeAllObjects];
	array= [_parentDS->_children objectForKey:@"root"];
	c= [array count];
	for (i= 0; i < c; i++)
	{
	  if ([self filterChildren:[_parentDS->_children objectForKey:[array objectAtIndex:i]]
						ofItem:[_parentDS->_items objectForKey:[array objectAtIndex:i]]
					  matching:filter])
	  {
		if (!rootArray)
		  rootArray= [NSMutableArray arrayWithCapacity:1];

		[rootArray addObject:[array objectAtIndex:i]];
	  }
	}
	if (rootArray)
	{
	  [_children setObject:rootArray forKey:@"root"];
	  _items= _parentDS->_items;
	}
  }
  else // incremental search
  {
	NSMutableArray *array;

	[_filter release];
	_filter= [filter retain];

	array= [_children objectForKey:@"root"];
	c= [array count];
	for (i= c-1; i >= 0; i--)
	{
	  if (![self removeChildren:[_children objectForKey:[array objectAtIndex:i]]
						 ofItem:[_parentDS->_items objectForKey:[array objectAtIndex:i]]
					notMatching:filter])
	  {
		[array removeObject:[array objectAtIndex:i]];
	  }
	}
  }
}

@end