File: MQScriptExecutionHelper.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 (419 lines) | stat: -rw-r--r-- 10,513 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
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
//
//  MQScriptExecutionHelper.m
//  MySQL QueryBrowser
//
//  Created by Alfredo Kojima on 5/30/05.
//  Copyright 2005 MySQL AB. All rights reserved.
//

#import "MQScriptExecutionHelper.h"
#import <MySQLToolsCommon/MSourceTextEditor.h>
#import <MySQLToolsCommon/MSourceTextView.h>
#import <MySQLToolsCommon/MTextGutterView.h>
#import <MySQLToolsCommon/MSyntaxColoring.h>

#define WORKER_GO 1
#define WORKER_STOP 0

@implementation MQScriptExecutionHelper

- (id)initWithText:(MSourceTextEditor*)text
{
  self= [super init];
  if (self)
  {
    _text= text;
    _sqlText= myx_init_mysql_text();
    _sqlTextLock= [[NSLock alloc] init];
    
    _workerLock= [[NSConditionLock alloc] initWithCondition:WORKER_STOP];
  }
  return self;
}

- (void)dealloc
{
  [_workerLock release];
  [_sqlTextLock release];
  myx_free_sql_text(_sqlText);
  [super dealloc];
}

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

- (unsigned int)lineForStatement:(unsigned int)statement
{
  if (_sqlText && statement < _sqlText->stmts_num)
    return _sqlText->stmts[statement].stmt_begin_line;
  return (unsigned int)-1;
}

- (unsigned int)statementAtLine:(unsigned int)line
{
  unsigned int i;
  for (i=0; i < _sqlText->stmts_num; i++)
  {
    if (_sqlText->stmts[i].stmt_begin_line <= (int)line &&
        (int)line <= _sqlText->stmts[i].stmt_end_line)
      return i;
  }
  return (unsigned int)-1;
}

- (void)setPCPosition:(unsigned int)index
{
  MSourceTextView *textView= [_text textView];
  MSyntaxColoring *colorer= [textView syntaxColorer];
  
  [colorer removeMarker:@"pc" atLine:[self lineForStatement:_currentStatement]];
  
  if (index != (unsigned int)-1)
  {
    unsigned int line;
    NSPoint point;
    
    _currentStatement= index;
    line= [self lineForStatement:_currentStatement];
    [colorer addMarker:@"pc" atLine:line];
    
    point.x= 0;
    point.y= line * [[textView layoutManager] defaultLineHeightForFont:[textView font]] - NSHeight([(NSClipView*)[textView superview] documentVisibleRect])/2;
    point.y= MAX(0, point.y);
    [[textView superview] scrollPoint:point];
  }
  [_text setNeedsDisplay:YES];
}

- (void)reparseNearLines:(NSRange)lineRange
{
  if (!_reparseScheduled)
  {
    [self performSelector:@selector(doReparse:)
               withObject:nil
               afterDelay:0.3];
    _reparseScheduled= YES;
  }
#if 0
  int i;
  MSyntaxColoring *colorer= [[_text textView] syntaxColorer];
  int stmtBefore, stmtAfter;
    
  // find the last statement before the one edited
  stmtBefore= 0;
  for (i= 0; i < _sqlText->stmts_num; i++)
  {
    if (lineRange.location <= _sqlText->stmts[i].stmt_end_line)
    {
      stmtBefore= i > 0 ? i-1 : 0;
      break;
    }
  }
  
  // find the 1st statement after the one edited
  stmtAfter= _sqlText->stmts_num;
  for (i= _sqlText->stmts_num-1; i >= stmtBefore; --i)
  {
    if (NSMaxRange(lineRange) >= _sqlText->stmts[i].stmt_begin_line)
    {
      stmtAfter= i < stmtAfter ? i+1 : stmtAfter;
      break;
    }
  }
  
  NSLog(@"reanalyze from stmt %i to %i",  stmtBefore, stmtAfter);
#endif
}

 
- (void)doReparse:(id)obj
{
  unsigned int i;
  MSyntaxColoring *colorer= [[_text textView] syntaxColorer];
  _reparseScheduled= NO;

  [_sqlTextLock lock];
  myx_analyze_text(_sqlText, (char*)[[[[_text textView] textStorage] string] UTF8String]);
  [_sqlTextLock unlock];

  [colorer removeMarkerFromAllLines:@"statement"];
  for (i= 0; i < _sqlText->stmts_num; i++)
    [colorer addMarker:@"statement" atLine:_sqlText->stmts[i].stmt_begin_line];
  [[_text gutterView] setNeedsDisplay:YES];
}


- (void)workerLoop:(id)arg
{
  NSString *text= [[[_text textView] textStorage] string];

  mysql_thread_init();
  
  _workerState= WorkerWaiting;
  for (;;)
  {
    NSMutableDictionary *info= nil;
    MYX_MYSQL_ERROR_MSGS *msgs;
    
    // wait for main thread to give us the go ahead
    [_workerLock lockWhenCondition:WORKER_GO];
    _workerState= WorkerExecuting;
    
    [_workerLock unlockWithCondition:WORKER_STOP];
  
    if (_workerEnd)
      break;
    
    if (_stepping)
      [self performSelectorOnMainThread:@selector(notifyWorkerBusy:)
                             withObject:nil
                          waitUntilDone:NO];
  
    [_sqlTextLock lock];
    NSRange range= NSMakeRange(_sqlText->stmts[_currentStatement].stmt_begin_char, 
                               _sqlText->stmts[_currentStatement].stmt_end_char-_sqlText->stmts[_currentStatement].stmt_begin_char);
    char *query= g_strndup([text UTF8String]+range.location, range.length);
    [_sqlTextLock unlock];

    if (!g_str_has_prefix(query, "DELIMITER"))
    {
      if (myx_mysql_query(_mysql, query) != 0)
      {      
        info= [[NSMutableDictionary alloc] initWithObjectsAndKeys:
          [NSNumber numberWithInt:myx_mysql_errno(_mysql)], @"errno",
          [NSString stringWithUTF8String:mysql_error(_mysql)], @"error",
          [NSNumber numberWithInt:_sqlText->stmts[_currentStatement].stmt_begin_line], @"line",
          nil];
      }
      msgs= myx_mysql_error_msgs_fetch(_mysql);
      if (msgs)
      {
        if (!info)
          info= [[NSMutableDictionary alloc] initWithObjectsAndKeys:
            [NSNumber numberWithInt:_sqlText->stmts[_currentStatement].stmt_begin_line], @"line",
            nil];
        [info setObject:[NSValue valueWithPointer:msgs] forKey:@"msgs"];
      }
    }
    g_free(query);

    _workerState= WorkerWaiting;
    // tell the main thread that we're ready
    [self performSelectorOnMainThread:@selector(notifyWorkerDone:)
                           withObject:info
                        waitUntilDone:NO];
    
    if (info)
    {
      [info release];
      info= nil;
    }
  }
  _workerState= WorkerExiting;
  [self performSelectorOnMainThread:@selector(notifyWorkerDone:)
                         withObject:nil
                      waitUntilDone:NO];  
  
  mysql_thread_end();
}


- (void)changeState:(MQScriptState)newState
{
  _state= newState;
  [_delegate scriptChangedState:self state:_state];
}

- (void)notifyWorkerBusy:(NSDictionary*)userData
{
  [self changeState:ScriptExecuting];
}

- (void)reset
{
  [self setPCPosition:-1];
  [[[_text textView] syntaxColorer] removeMarkerFromAllLines:@"error"];
  _currentStatement= 0;
  _stepping= NO;
  [_errorHandler performSelector:_errorHandlerSelector
                      withObject:self
                      withObject:nil];  
}

- (void)notifyWorkerDone:(NSDictionary*)userData
{
  if (userData)
    [_errorHandler performSelector:_errorHandlerSelector
                        withObject:self
                        withObject:userData];
  
  if ([userData objectForKey:@"error"])
  {
    _stepping= YES;
    [self setPCPosition:_currentStatement];
    [[[_text textView] syntaxColorer] addMarker:@"error"
                                         atLine:[self lineForStatement:_currentStatement]];
    [self changeState:ScriptError];
    return;
  }
  
  // normal execution
  if (!_stepping && _workerState == WorkerWaiting)
  {
#define HAS_NEXT_STATEMENT(cur, limit, last)\
    ((cur) < (last) && ((limit)<0 || ((cur) < (limit))))
    if (HAS_NEXT_STATEMENT(_currentStatement, (unsigned int)_stopAtStatement, _sqlText->stmts_num-1))
    {
      if ([[[_text textView] syntaxColorer] marker:@"breakpoint"
                                            atLine:[self lineForStatement:_currentStatement+1]])
      {
        _stepping= YES;
        [self setPCPosition:_currentStatement+1];
        [self changeState:ScriptBreakpoint];
        return;
      }
      else
        _currentStatement++;
    }
    else
    {
      _workerEnd= YES;
      [self setPCPosition:-1];
      [self changeState:ScriptFinished];
    }
    [_workerLock unlockWithCondition:WORKER_GO];
  }
  else
  {
    if (_workerState == WorkerExiting)
    {
      [self setPCPosition:-1];
      [self changeState:_currentStatement == _sqlText->stmts_num-1 ? ScriptFinished : ScriptStopped];
    }
    else if (_currentStatement < _sqlText->stmts_num && _workerState == WorkerWaiting)
    { // stepping
      [self setPCPosition:_currentStatement+1];
      [self changeState:ScriptWaiting];
    }
    else
    {
      _workerEnd= YES;
      [self changeState:ScriptFinished];
    }
  }
}


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


- (void)setErrorHandler:(id)handler
               selector:(SEL)selector
{
  _errorHandler= handler;
  _errorHandlerSelector= selector;
}


- (IBAction)executeScript:(id)sender
{
  MSourceTextView *sourceText= [_text textView];
  unsigned int start;
  
  switch ([sender tag])
  {
    case 24:
    case 0: // Execute
    case 1:
      _stopAtStatement= -1;
      start= 0;
      _stepping= NO;
      break;
    case 2: // Execute Selected
    {
      NSRange lineRange= [[sourceText syntaxColorer] lineRangeForCharacterRange:[sourceText selectedRange]];
      
      start= [self statementAtLine:lineRange.location];
      _stopAtStatement= [self statementAtLine:NSMaxRange(lineRange)];
      _stepping= NO;
      break;
    }
    case 3: // Execute Stepping
      _stopAtStatement= -1;
      start= 0;
      _stepping= YES;
      break;
  }

  [[sourceText syntaxColorer] removeMarkerFromAllLines:@"pc"];

  if (!_sqlText || start >= _sqlText->stmts_num)
    return;

  if (_stepping)
    [self setPCPosition:start];
  else
  {
    _currentStatement= start;
    [self changeState:ScriptExecuting];
  }

  _workerEnd= NO;
  _workerState= WorkerStarting;
  
  [NSApplication detachDrawingThread:@selector(workerLoop:)
                            toTarget:self
                          withObject:nil];

  if ([[[_text textView] syntaxColorer] marker:@"breakpoint"
                                        atLine:[self lineForStatement:_currentStatement]]
      || _stepping)
  {
    _stepping= YES;
    [self setPCPosition:_currentStatement];
    [self changeState:_stepping ? ScriptWaiting : ScriptBreakpoint];
  }
  else
    [_workerLock unlockWithCondition:WORKER_GO];
}


- (IBAction)stepScript:(id)sender
{
  if (_currentStatement < _sqlText->stmts_num)
  {
    [_workerLock unlockWithCondition:WORKER_GO];
  }
}

- (IBAction)stopScript:(id)sender
{
  [self reset];
  _workerEnd= YES;
  [_workerLock unlockWithCondition:WORKER_GO];
}

- (IBAction)pauseScript:(id)sender
{
  _stepping= YES;
}

- (IBAction)continueScript:(id)sender
{
  [self setPCPosition:-1];
  _stepping= NO;
  [self changeState:ScriptExecuting];
  [_workerLock unlockWithCondition:WORKER_GO];
}

- (MQScriptState)state
{
  return _state;
}

@end