[go: up one dir, main page]

File: todebugtext.cpp

package info (click to toggle)
tora 1.3.4-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 8,632 kB
  • ctags: 7,487
  • sloc: cpp: 68,518; perl: 1,475; ansic: 291; sh: 173; makefile: 51
file content (434 lines) | stat: -rw-r--r-- 12,651 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
//***************************************************************************
/*
 * TOra - An Oracle Toolkit for DBA's and developers
 * Copyright (C) 2000-2001,2001 Underscore AB
 * 
 * This program 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;  only version 2 of
 * the License is valid for this program.
 * 
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 *      As a special exception, you have permission to link this program
 *      with the Oracle Client libraries and distribute executables, as long
 *      as you follow the requirements of the GNU GPL in regard to all of the
 *      software in the executable aside from Oracle client libraries.
 *
 *      Specifically you are not permitted to link this program with the
 *      Qt/UNIX, Qt/Windows or Qt Non Commercial products of TrollTech.
 *      And you are not permitted to distribute binaries compiled against
 *      these libraries without written consent from Underscore AB. Observe
 *      that this does not disallow linking to the Qt Free Edition.
 *
 * All trademarks belong to their respective owners.
 *
 ****************************************************************************/

#include "utils.h"

#include "toconnection.h"
#include "todebug.h"
#include "todebugtext.h"
#include "tosql.h"

#include <qapplication.h>
#include <qpainter.h>
#include <qpixmap.h>

#include "icons/breakpoint.xpm"
#include "icons/disbreakpoint.xpm"

#define TO_BREAK_COL 5

toBreakpointItem::toBreakpointItem(QListView *parent,QListViewItem *after,
				   const QString &schema,const QString &type,
				   const QString &object,int line)
: QListViewItem(parent,after)
{
  if (schema.isNull())
    setText(2,"");
  else
    setText(2,schema);
  if (object.isNull())
    setText(0,"");
  else
    setText(0,object);
  if (type.isNull())
    setText(3,"");
  else
    setText(3,type);
  setText(1,QString::number(line+1));
  if(type=="PACKAGE"||type=="PROCEDURE"||type=="FUNCTION"||type=="TYPE")
    Namespace=TO_NAME_TOPLEVEL;
  else if (type=="PACKAGE BODY"||type=="TYPE BODY")
    Namespace=TO_NAME_BODY;
  else
    Namespace=TO_NAME_NONE;
  Line=line;
  setText(4,"DEFERED");
}

static toSQL SQLBreakpoint("toDebug:SetBreakpoint",
			   "DECLARE\n"
			   "    proginf SYS.DBMS_DEBUG.PROGRAM_INFO;\n"
			   "    bnum BINARY_INTEGER;\n"
			   "    ret BINARY_INTEGER;\n"
			   "BEGIN\n"
			   "    proginf.Namespace:=:type<int,in>;\n"
			   "    proginf.Name:=:name<char[100],in>;\n"
			   "    proginf.Owner:=:schema<char[100],in>;\n"
			   "    proginf.DbLink:=NULL;\n"
			   "    proginf.LibUnitType:=SYS.DBMS_DEBUG.LibUnitType_Procedure;\n"
			   "    proginf.EntryPointName:=NULL;\n"
			   "    proginf.Line#:=:line<int,in>;\n"
			   "    ret:=SYS.DBMS_DEBUG.SET_BREAKPOINT(proginf,proginf.Line#,bnum,0,1);\n"
			   "    SELECT ret,bnum INTO :ret<int,out>,:bnum<int,out> FROM sys.DUAL;\n"
			   "END;",
			   "Set breakpoint, must have same bindings");
		       

void toBreakpointItem::setBreakpoint(void)
{
  bool ok=false;
  try {
    clearBreakpoint();
    toConnection &conn=toCurrentConnection(listView());
    toQList args;
    toPush(args,toQValue(Namespace));
    toPush(args,toQValue(text(0)));
    toPush(args,toQValue(text(2)));
    toPush(args,toQValue(Line+1));
    toQuery query(conn,SQLBreakpoint,args);
    int ret=query.readValue().toInt();
    if (ret==TO_SUCCESS) {
      setText(TO_BREAK_COL,query.readValue());
      setText(4,"ENABLED");
      ok=true;
    } else if (ret==TO_ERROR_ILLEGAL_LINE) {
      toStatusMessage("Can not enable breakpoint, not a valid line. Perhaps needs to recompile.");
    } else if (ret==TO_ERROR_BAD_HANDLE) {
      toStatusMessage("Can not enable breakpoint, not a valid object. Perhaps needs to compile.");
    }
  } TOCATCH
  if (!ok)
    setText(4,"NOT SET");
}

static toSQL SQLClearBreakpoint("toDebug:ClearBreakpoint",
				"DECLARE\n"
				"    bnum BINARY_INTEGER;\n"
				"    ret BINARY_INTEGER;\n"
				"BEGIN\n"
				"    bnum:=:bnum<int,in>;\n"
				"    ret:=SYS.DBMS_DEBUG.DELETE_BREAKPOINT(bnum);\n"
				"    SELECT ret INTO :ret<int,out> FROM sys.DUAL;\n"
				"END;",
				"Clear breakpoint, must have same bindings");

void toBreakpointItem::clearBreakpoint()
{
  if (text(4)=="ENABLED"&&!text(TO_BREAK_COL).isEmpty()) {
    try {
      toConnection &conn=toCurrentConnection(listView());
      toQList args;
      toPush(args,toQValue(text(TO_BREAK_COL).toInt()));
      toQuery query(conn,SQLBreakpoint);
      int res=query.readValue().toInt();
      
      if (res!=TO_SUCCESS) {
	QString str("Failed to remove breakpoint (Reason ");
	str+=QString::number(res);
	str+=")";
	toStatusMessage(str);
      }
    } TOCATCH
    setText(TO_BREAK_COL,NULL);
  }
  setText(4,"DISABLED");
}

#define DEBUG_INDENT 10

static QPixmap *toBreakpointPixmap;
static QPixmap *toDisBreakpointPixmap;

static toSQL SQLReadSource("toDebug:ReadSource",
			   "SELECT Text FROM SYS.All_Source\n"
			   " WHERE OWNER = :f1<char[101]>\n"
			   "   AND NAME = :f2<char[101]>\n"
			   "   AND TYPE = :f3<char[101]>\n"
			   " ORDER BY Type,Line",
			   "Read sourcecode for object");
static toSQL SQLReadErrors("toDebug:ReadErrors",
			   "SELECT Line-1,Text FROM SYS.All_Errors\n"
			   " WHERE OWNER = :f1<char[101]>\n"
			   "   AND NAME = :f2<char[101]>\n"
			   "   AND TYPE = :f3<char[101]>\n"
			   " ORDER BY Type,Line",
			   "Get lines with errors in object (Observe first line 0)");

bool toDebugText::readErrors(toConnection &conn)
{
  try {
    toQuery errors(conn,SQLReadErrors,Schema,Object,Type);
    std::map<int,QString> Errors;

    while(!errors.eof()) {
      int line=errors.readValue().toInt();
      Errors[line]+=" ";
      Errors[line]+=errors.readValue();
    }
    setErrors(Errors);
    return true;
  } TOCATCH
  return false;
}
bool toDebugText::readData(toConnection &conn,QListView *Stack)
{
  QListViewItem *item=NULL;
  if (Stack&&Stack->firstChild())
    for(item=Stack->firstChild();item->firstChild();item=item->firstChild())
      ;
  try {
    toQuery lines(conn,SQLReadSource,Schema,Object,Type);

    QString str;
    while(!lines.eof())
      str+=lines.readValue();
    setText(str);
    setEdited(false);
    setCurrent(-1);

    if (str.isEmpty())
      return false;
    else {
      if (item&&
	  Schema==item->text(2)&&
	  Object==item->text(0)&&
	  Type==item->text(3))
	setCurrent(item->text(1).toInt()-1);

      return readErrors(conn);
    }
  } TOCATCH
  return false;
}

void toDebugText::setData(const QString &schema,const QString &type,const QString &object)
{
  Schema=schema;
  Type=type;
  Object=object;
  CurrentItem=FirstItem=NULL;
  NoBreakpoints=false;
  update();
}

toDebugText::toDebugText(QListView *breakpoints,
			 QWidget *parent,
			 toDebug *debugger,
			 const char *name)
  : toHighlightedText(parent,name),
    Debugger(debugger),
    Breakpoints(breakpoints)
{
  setLeftIgnore(DEBUG_INDENT);
  setHMargin(DEBUG_INDENT+hMargin());
  setMouseTracking(true);
  QRect view=viewRect();
  LastX=DEBUG_INDENT+view.left();
  CurrentItem=FirstItem=NULL;
  NoBreakpoints=false;
  if (!toBreakpointPixmap)
    toBreakpointPixmap=new QPixmap((const char **)breakpoint_xpm);
  if (!toDisBreakpointPixmap)
    toDisBreakpointPixmap=new QPixmap((const char **)disbreakpoint_xpm);
}

bool toDebugText::checkItem(toBreakpointItem *item)
{
  if (!item)
    return false;
  if (item->text(2)==Schema&&
      item->text(3)==Type&&
      item->text(0)==Object)
    return true;
  return false;
}

void toDebugText::clear(void)
{
  setData("","","");
  FirstItem=CurrentItem=NULL;
  NoBreakpoints=false;
  toHighlightedText::clear();
}

bool toDebugText::hasBreakpoint(int row) // This has to leave CurrentItem on the breakpoint
{
  if (!FirstItem&&!NoBreakpoints) {
    FirstItem=dynamic_cast<toBreakpointItem *>(Breakpoints->firstChild());
    while(!checkItem(FirstItem)&&FirstItem)
      FirstItem=dynamic_cast<toBreakpointItem *>(FirstItem->nextSibling());
    if (!FirstItem)
      NoBreakpoints=true;
    CurrentItem=FirstItem;
  }

  if (!NoBreakpoints) {
    toBreakpointItem *next=dynamic_cast<toBreakpointItem *>(CurrentItem->nextSibling());
    bool hasNext=checkItem(next);
    int nextLine=hasNext?next->line():row+1;

    if (CurrentItem->line()==row)
      return true;
    if (row==nextLine) {
      CurrentItem=next;
      return true;
    }
    if (!hasNext&&row>CurrentItem->line())
      return false;
    if (row<CurrentItem->line()) {
      if (CurrentItem==FirstItem)
	return false;
      CurrentItem=FirstItem;
      return hasBreakpoint(row);
    }
    if (row>nextLine) {
      CurrentItem=next;
      return hasBreakpoint(row);
    }
  }
  return false;
}

void toDebugText::paintCell(QPainter *painter,int row,int col)
{
  toHighlightedText::paintCell(painter,row,col);
  if (col==0) {
    QPalette cp=qApp->palette();
    painter->fillRect(0,0,DEBUG_INDENT-2,cellHeight(),cp.active().background());
    painter->fillRect(DEBUG_INDENT-2,0,1,cellHeight(),cp.active().midlight());
    painter->fillRect(DEBUG_INDENT-1,0,1,cellHeight(),cp.active().dark());

    if (hasBreakpoint(row)) {
      int h=max((cellHeight()-toBreakpointPixmap->height())/2,0);
      if (CurrentItem->text(4)=="DISABLED")
	painter->drawPixmap(0,h,*toDisBreakpointPixmap);
      else
	painter->drawPixmap(0,h,*toBreakpointPixmap);
    }
  }
}

void toDebugText::paintEvent(QPaintEvent *pe)
{
  toHighlightedText::paintEvent(pe);
  int fromY=0;
  if (numRows()==0||rowYPos(numRows()-1,&fromY)) {
    if (numRows())
      fromY+=cellHeight();
    QRect view=viewRect();
    if (fromY+view.top()<view.height()) {
      QPainter painter(this);
      QPalette cp=qApp->palette();
      if (xOffset()<DEBUG_INDENT-3)
	painter.fillRect(view.left(),fromY,DEBUG_INDENT-2-xOffset(),view.bottom()-fromY,cp.active().background());
      if (xOffset()<DEBUG_INDENT-2)
	painter.fillRect(view.left()+DEBUG_INDENT-2-xOffset(),fromY,1,view.bottom()-fromY,cp.active().midlight());
      if (xOffset()<DEBUG_INDENT-1)
	painter.fillRect(view.left()+DEBUG_INDENT-1-xOffset(),fromY,1,view.bottom()-fromY,cp.active().dark());
    }
  }
}

void toDebugText::mouseMoveEvent(QMouseEvent *me)
{
  QRect view=viewRect();
  if (me->x()>DEBUG_INDENT+view.left()) {
    if (LastX<=DEBUG_INDENT+view.left())
      setCursor(Qt::ibeamCursor);
    if(me->state()!=0)
      toHighlightedText::mouseMoveEvent(me);
  } else {
    if (LastX>DEBUG_INDENT+view.left())
      setCursor(Qt::ibeamCursor);
    setCursor(Qt::arrowCursor);
  }
  LastX=me->x();
}

void toDebugText::toggleBreakpoint(int row,bool enable)
{
  if (Schema.isEmpty()||
      Type.isEmpty()||
      Object.isEmpty())
    return;

  int curcol;
  if (row<0)
    getCursorPosition (&row,&curcol);
  if (row>=0) {
    if (hasBreakpoint(row)) {
      if (enable) {
	if (CurrentItem->text(4)=="DISABLED")
	  CurrentItem->setText(4,"DEFERED");
	else
	  CurrentItem->clearBreakpoint();
      } else {
	CurrentItem->clearBreakpoint();
	delete CurrentItem;
	if (FirstItem==CurrentItem) {
	  NoBreakpoints=false;
	  CurrentItem=FirstItem=NULL;
	} else
	  CurrentItem=FirstItem;
      }
    } else if (!enable) {
      if (CurrentItem&&CurrentItem->line()>row)
	new toBreakpointItem(Breakpoints,NULL,
			     Schema,Type,Object,row);
      else
	new toBreakpointItem(Breakpoints,CurrentItem,
			     Schema,Type,Object,row);
      FirstItem=CurrentItem=NULL;
      NoBreakpoints=false;
    }
    updateCell(row,0,false);
  }
}

void toDebugText::mouseDoubleClickEvent(QMouseEvent *me)
{
  if (me->x()+xOffset()>DEBUG_INDENT)
    toHighlightedText::mouseDoubleClickEvent(me);
  else {
    int row=findRow(me->y());
    toggleBreakpoint(row);
  }
}

void toDebugText::exportData(std::map<QString,QString> &data,const QString &prefix)
{
  toHighlightedText::exportData(data,prefix);
  data[prefix+":Schema"]=Schema;
  data[prefix+":Object"]=Object;
  data[prefix+":Type"]=Type;
}

void toDebugText::importData(std::map<QString,QString> &data,const QString &prefix)
{
  toHighlightedText::importData(data,prefix);
  Schema=data[prefix+":Schema"];
  Object=data[prefix+":Object"];
  Type=data[prefix+":Type"];
  NoBreakpoints=false;
}