File: MQHistory.m

package info (click to toggle)
mysql-gui-tools 5.0r14%2BopenSUSE-2.1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 116,956 kB
  • ctags: 48,715
  • sloc: sql: 341,918; pascal: 276,698; ansic: 91,020; cpp: 90,451; objc: 33,236; sh: 29,481; yacc: 10,756; xml: 10,589; java: 10,079; php: 2,806; python: 2,092; makefile: 1,783; perl: 4
file content (390 lines) | stat: -rw-r--r-- 8,980 bytes parent folder | download | duplicates (3)
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
//
//  MQHistory.m
//  MySQL QueryBrowser
//
//  Created by Alfredo Kojima on 4/19/05.
//  Copyright 2005 MySQL AB. All rights reserved.
//

#include <time.h>
#import <Cocoa/Cocoa.h>
#import "MQHistory.h"
#include <MySQLToolsCommon/myxUtil.h>

NSString *MQHistoryDidChangeNotification= @"MQHistoryDidChangeNotification";

@interface MQHistoryGroup : NSObject
{
@public
  NSMutableArray *array;
  NSString *name;
}
@end


@implementation MQHistoryGroup
- (id)init
{
  self= [super init];
  if (self)
  {
	array= [[NSMutableArray alloc] init];
  }
  return self;
}

- (void)dealloc
{
  [name release];
  [array release];
  [super dealloc];
}
@end


@implementation MQHistoryItem

- (id)initWithItem:(MYX_HISTORY_ENTRY*)entry
{
  self= [super init];
  if (self)
  {
	char *p;
	struct tm tstamp;
	
	_query= [NSStr(entry->sql) retain];
	_catalog= [NSStr(entry->catalog) retain];
	_schema= [NSStr(entry->schema) retain];
	_queryType= entry->query_type;

	p= strchr(entry->date_last_access, 'T');
	if (p) *p= ' ';
	
	strptime(entry->date_last_access, "%F %T", &tstamp);
	
	_lastAccess= mktime(&tstamp);
  }
  return self;
}

- (NSString*)query
{
  return _query;
}
@end



@implementation MQHistory

- (id)init
{
  self= [super init];
  if (self)
  {
	_itemsByInterval= [[NSMutableDictionary alloc] init];
  }
  return self;
}


- (BOOL)loadFromFile:(NSString*)file;
{
  MYX_HISTORY *history;
  MYX_HISTORY_TREE *tree;
  MYX_LIB_ERROR error;
  static NSString *names[]= {
	@"Today",
	@"Monday",
	@"Tuesday",
	@"Wednesday", 
	@"Thursday",
	@"Friday",
	@"Saturday",
	@"Sunday",
	@"Yesterday", 
	@"Last Week",
	@"Before Last Week"
  };
  static int order[]= {
    MYX_HIT_TODAY, 
    MYX_HIT_YESTERDAY,
    MYX_HIT_MONDAY,
    MYX_HIT_TUESDAY,
    MYX_HIT_WEDNESDAY,
    MYX_HIT_THURSDAY,
    MYX_HIT_FRIDAY,
    MYX_HIT_SATURDAY,
    MYX_HIT_SUNDAY,
    MYX_HIT_LAST_WEEK,
    MYX_HIT_BEFORE_LAST_WEEK
  };
  unsigned int o= 0;
  
  
  history= myx_history_load([file fileSystemRepresentation], &error);
  if (!history && error != MYX_ERROR_CANT_OPEN_FILE)
  {
	NSRunAlertPanel(nil, @"Could not load history data from file %@:\n%@",
					@"OK", nil, nil,
					file, MXGetErrorString(error));
	return NO;
  }
  
  // Always add Today group
  {
	MQHistoryGroup *group= [[[MQHistoryGroup alloc] init] autorelease];
	group->name= [names[MYX_HIT_TODAY] retain];
	[_itemsByInterval setObject:group
						 forKey:[NSNumber numberWithInt:MYX_HIT_TODAY]];
    _periodForIndex[o++]= MYX_HIT_TODAY;
  }  

  if (history)
  {
	unsigned int i, c, s, e;

	tree= myx_history_get_tree(history);

	for (i= 0; i < tree->history_intervals_num; i++)
	{
	  MQHistoryGroup *group;

      if (tree->history_intervals[i].interval_type > MYX_HIT_BEFORE_LAST_WEEK)
		tree->history_intervals[i].interval_type= MYX_HIT_BEFORE_LAST_WEEK;
      
      if (tree->history_intervals[i].interval_type == MYX_HIT_TODAY)
        group= [_itemsByInterval objectForKey:[NSNumber numberWithInt:MYX_HIT_TODAY]];
      else
      {
        group= [[[MQHistoryGroup alloc] init] autorelease];
        group->name= [names[tree->history_intervals[i].interval_type] retain];
        [_itemsByInterval setObject:group
                             forKey:[NSNumber numberWithInt:tree->history_intervals[i].interval_type]];
        _periodForIndex[o++]= tree->history_intervals[i].interval_type;
      }

	  for (c= 0; c < tree->history_intervals[i].catalogs_num; c++)
	  {
		for (s= 0; s < tree->history_intervals[i].catalogs[c].schemata_num; s++)
		{
		  for (e= 0; e < tree->history_intervals[i].catalogs[c].schemata[s].entries_num; e++)
		  {
			MQHistoryItem *item= [[MQHistoryItem alloc] initWithItem:tree->history_intervals[i].catalogs[c].schemata[s].entries[e]];
	
			[group->array addObject:item];
			[item release];
		  }
		}
	  }
	}
    myx_history_free_tree(tree);
	
	myx_history_free(history);
    
    // bubblesort the orders
    for (i= 0; i < o; i++)
    {
      int j;
      for (j= i; j < o; j++)
      {
        if (order[_periodForIndex[i]] > order[_periodForIndex[j]])
        {
          int tmp= _periodForIndex[j];
          _periodForIndex[j]= _periodForIndex[i];
          _periodForIndex[i]= tmp;
        }
      }
    }
  }  
  return YES;
}


static int compareEntry(const MYX_HISTORY_ENTRY *e1, const MYX_HISTORY_ENTRY *e2)
{
  return strcmp(e1->date_last_access, e2->date_last_access);
}


- (void)storeToFile:(NSString*)file
{
  MYX_HISTORY *history;
  NSEnumerator *enu;
  MQHistoryGroup *items;
  unsigned int j;
  int limit= [[NSUserDefaults standardUserDefaults] integerForKey:@"QueryHistoryLimit"];
  
  history= g_new0(MYX_HISTORY, 1);
  enu= [_itemsByInterval objectEnumerator];
  while ((items= [enu nextObject]))
    history->entries_num+= [items->array count];
  history->entries= g_new0(MYX_HISTORY_ENTRY, history->entries_num);
  
  j= 0;
  enu= [_itemsByInterval objectEnumerator];
  while ((items= [enu nextObject]))
  {
    unsigned int i, c= [items->array count];
    for (i= 0; i < c; i++)
    {
      MQHistoryItem *item= [items->array objectAtIndex:i];
      MYX_HISTORY_ENTRY *entry= history->entries+j++;
      char buffer[100];

      entry->sql= g_strdup([item->_query UTF8String]);
      entry->catalog= g_strdup([item->_catalog UTF8String]);
      entry->schema= g_strdup([item->_schema UTF8String]);

      entry->query_type= item->_queryType;

      strftime(buffer, sizeof(buffer), "%FT%T", gmtime(&item->_lastAccess));
      entry->date_last_access= g_strdup(buffer);
    }
  }
  
  qsort(history->entries, history->entries_num, sizeof(MYX_HISTORY_ENTRY), 
        (int(*)(const void*,const void*))compareEntry);
  
  j= history->entries_num;
  if (limit > 0)
    history->entries_num= limit;
  myx_history_store([file fileSystemRepresentation], history);
  history->entries_num= j;
  
  myx_history_free(history);
}


- (void)rememberQuery:(NSString*)query
			  catalog:(NSString*)catalog
			   schema:(NSString*)schema
{
  NSEnumerator *enumer= [_itemsByInterval objectEnumerator];
  MQHistoryGroup *group;
  MQHistoryItem *item= nil;
  BOOL found= NO;
  
  while (!found && (group= [enumer nextObject]))
  {
	unsigned int i, c;
	c= [group->array count];
	for (i= 0; i < c; i++)
	{
	  item= [group->array objectAtIndex:i];
	  
	  if ([[item query] isEqualToString:query])
	  {
		item->_lastAccess= time(NULL);
		[[item retain] autorelease];
		[group->array removeObject:item];
		found= YES; // break outer loop
		break;
	  }
	}
  }
  
  if (!found)
  {
	item= [[[MQHistoryItem alloc] init] autorelease];
	item->_query= [query retain];
	item->_catalog= [catalog retain];
	item->_schema= [schema retain];
	
	item->_lastAccess= time(NULL);
  }
  
  group= [_itemsByInterval objectForKey:[NSNumber numberWithInt:MYX_HIT_TODAY]];
  if (group)
    [group->array insertObject:item atIndex:0];
    
  [[NSNotificationCenter defaultCenter] postNotificationName:MQHistoryDidChangeNotification 
													  object:self];
}


- (void)reset
{ 
  [_itemsByInterval removeAllObjects];  

  MQHistoryGroup *group= [[[MQHistoryGroup alloc] init] autorelease];
  group->name= @"Today";
  [_itemsByInterval setObject:group
                       forKey:[NSNumber numberWithInt:MYX_HIT_TODAY]];
  _periodForIndex[0]= MYX_HIT_TODAY;
    
  [[NSNotificationCenter defaultCenter] postNotificationName:MQHistoryDidChangeNotification 
													  object:self];  
}


- (void)removeItem:(MQHistoryItem*)item
{
  NSEnumerator *en= [_itemsByInterval objectEnumerator];
  MQHistoryGroup *group;
  [item retain];
  while ((group= [en nextObject]))
  {
    [group->array removeObject:item];
  }
  [item release];
}


- (id)outlineView:(NSOutlineView *)outlineView 
            child:(int)index 
           ofItem:(id)item
{
  if (!item)
	return [_itemsByInterval objectForKey:[NSNumber numberWithInt:_periodForIndex[index]]];
  else
	return [((MQHistoryGroup*)item)->array objectAtIndex:index];
}


- (BOOL)outlineView:(NSOutlineView *)outlineView 
   isItemExpandable:(id)item
{
  if (!item || [item isKindOfClass:[MQHistoryGroup class]])
	return YES;
  else
	return NO;
}


- (int)outlineView:(NSOutlineView *)outlineView 
numberOfChildrenOfItem:(id)item
{
  int count; 
  if (!item)
	count= [_itemsByInterval count];
  else
	count= [((MQHistoryGroup*)item)->array count];
  return count;
}


- (id)outlineView:(NSOutlineView *)outlineView 
objectValueForTableColumn:(NSTableColumn *)tableColumn 
           byItem:(id)item
{
  if ([item isKindOfClass:[MQHistoryItem class]])
	return [[item query] substringToIndex:MIN(100, [[item query] length])];
  else
	return ((MQHistoryGroup*)item)->name;
}


- (BOOL)outlineView:(NSOutlineView *)outlineView
         writeItems:(NSArray*)items toPasteboard:(NSPasteboard*)pboard 
{
  if (![[items objectAtIndex:0] isKindOfClass:[MQHistoryItem class]])
    return NO;
  [pboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
  
  [pboard setString:[[items objectAtIndex:0] query] forType:NSStringPboardType];
  
  return YES;
}

@end