summaryrefslogtreecommitdiffstats
path: root/kexi/plugins/queries/kexiquerydesignersql.cpp
blob: c9f9e1c5f1336c345eb018936641be88385b6bfa (plain)
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
/* This file is part of the KDE project
   Copyright (C) 2003 Lucijan Busch <lucijan@kde.org>
   Copyright (C) 2004-2006 Jaroslaw Staniek <js@iidea.pl>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include <tqsplitter.h>
#include <tqlayout.h>
#include <tqhbox.h>
#include <tqvbox.h>
#include <tqtimer.h>

#include <tdeapplication.h>
#include <kdebug.h>
#include <tdemessagebox.h>
#include <kiconloader.h>

#include <kexiutils/utils.h>
#include <kexidb/driver.h>
#include <kexidb/connection.h>
#include <kexidb/parser/parser.h>

#include <kexiproject.h>
#include <keximainwindow.h>

#include "kexiquerydesignersqleditor.h"
#include "kexiquerydesignersqlhistory.h"
#include "kexiquerydesignersql.h"
#include "kexiquerypart.h"

#include "kexisectionheader.h"


static bool compareSQL(const TQString& sql1, const TQString& sql2)
{
	//TODO: use reformatting functions here
	return sql1.stripWhiteSpace()==sql2.stripWhiteSpace();
}

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

//! @internal
class KexiQueryDesignerSQLView::Private
{
	public:
		Private() :
		   history(0)
		 , historyHead(0)
		 , statusPixmapOk( DesktopIcon("button_ok") )
		 , statusPixmapErr( DesktopIcon("button_cancel") )
		 , statusPixmapInfo( DesktopIcon("messagebox_info") )
		 , parsedQuery(0)
		 , heightForStatusMode(-1)
		 , heightForHistoryMode(-1)
		 , eventFilterForSplitterEnabled(true)
		 , justSwitchedFromNoViewMode(false)
		 , slotTextChangedEnabled(true)
		{
		}
		KexiQueryDesignerSQLEditor *editor;
		KexiQueryDesignerSQLHistory *history;
		TQLabel *pixmapStatus, *lblStatus;
		TQHBox *status_hbox;
		TQVBox *history_section;
		KexiSectionHeader *head, *historyHead;
		TQPixmap statusPixmapOk, statusPixmapErr, statusPixmapInfo;
		TQSplitter *splitter;
		TDEToggleAction *action_toggle_history;
		//! For internal use, this pointer is usually copied to TempData structure, 
		//! when switching out of this view (then it's cleared).
		KexiDB::QuerySchema *parsedQuery;
		//! For internal use, statement passed in switching to this view
		TQString origStatement;
		//! needed to remember height for both modes, between switching
		int heightForStatusMode, heightForHistoryMode;
		//! helper for slotUpdateMode()
		bool action_toggle_history_was_checked : 1;
		//! helper for eventFilter()
		bool eventFilterForSplitterEnabled : 1;
		//! helper for beforeSwitchTo()
		bool justSwitchedFromNoViewMode : 1;
		//! helper for slotTextChanged()
		bool slotTextChangedEnabled : 1;
};

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

KexiQueryDesignerSQLView::KexiQueryDesignerSQLView(KexiMainWindow *mainWin, TQWidget *parent, const char *name)
 : KexiViewBase(mainWin, parent, name)
 , d( new Private() )
{
	d->splitter = new TQSplitter(this);
	d->splitter->setOrientation(Qt::Vertical);
	d->head = new KexiSectionHeader(i18n("SQL Query Text"),Qt::Vertical, d->splitter);
	d->editor = new KexiQueryDesignerSQLEditor(mainWin, d->head, "sqle");
//	d->editor->installEventFilter(this);//for keys
	connect(d->editor, TQT_SIGNAL(textChanged()), TQT_TQOBJECT(this), TQT_SLOT(slotTextChanged()));
	addChildView(d->editor);
	setViewWidget(d->editor);
	d->splitter->setFocusProxy(d->editor);
	setFocusProxy(d->editor);

	d->history_section = new TQVBox(d->splitter);

	d->status_hbox = new TQHBox(d->history_section);
	d->status_hbox->installEventFilter(this);
	d->splitter->setResizeMode(d->history_section, TQSplitter::KeepSize);
	d->status_hbox->setSpacing(0);
	d->pixmapStatus = new TQLabel(d->status_hbox);
	d->pixmapStatus->setFixedWidth(d->statusPixmapOk.width()*3/2);
	d->pixmapStatus->setAlignment(AlignHCenter | AlignTop);
	d->pixmapStatus->setMargin(d->statusPixmapOk.width()/4);
	d->pixmapStatus->setPaletteBackgroundColor( palette().active().color(TQColorGroup::Base) );

	d->lblStatus = new TQLabel(d->status_hbox);
	d->lblStatus->setAlignment(AlignLeft | AlignTop | WordBreak);
	d->lblStatus->setMargin(d->statusPixmapOk.width()/4);
	d->lblStatus->setSizePolicy( TQSizePolicy::Preferred, TQSizePolicy::Expanding );
	d->lblStatus->resize(d->lblStatus->width(),d->statusPixmapOk.width()*3);
	d->lblStatus->setPaletteBackgroundColor( palette().active().color(TQColorGroup::Base) );

	TQHBoxLayout *b = new TQHBoxLayout(this);
	b->addWidget(d->splitter);

	plugSharedAction("querypart_check_query", TQT_TQOBJECT(this), TQT_SLOT(slotCheckQuery())); 
	plugSharedAction("querypart_view_toggle_history", TQT_TQOBJECT(this), TQT_SLOT(slotUpdateMode()));
	d->action_toggle_history = static_cast<TDEToggleAction*>( sharedAction( "querypart_view_toggle_history" ) );

	d->historyHead = new KexiSectionHeader(i18n("SQL Query History"),Qt::Vertical, d->history_section);
	d->historyHead->installEventFilter(this);
	d->history = new KexiQueryDesignerSQLHistory(d->historyHead, "sql_history");

	static const TQString msg_back = i18n("Back to Selected Query");
	static const TQString msg_clear = i18n("Clear History");
	d->historyHead->addButton("select_item", msg_back, TQT_TQOBJECT(this), TQT_SLOT(slotSelectQuery()));
	d->historyHead->addButton("edit-clear", msg_clear, TQT_TQOBJECT(d->history), TQT_SLOT(clear()));
	d->history->popupMenu()->insertItem(SmallIcon("select_item"), msg_back, TQT_TQOBJECT(this), TQT_SLOT(slotSelectQuery()));
	d->history->popupMenu()->insertItem(SmallIcon("edit-clear"), msg_clear, d->history, TQT_SLOT(clear()));
	connect(d->history, TQT_SIGNAL(currentItemDoubleClicked()), TQT_TQOBJECT(this), TQT_SLOT(slotSelectQuery()));

	d->heightForHistoryMode = -1; //height() / 2;
	//d->historyHead->hide();
	d->action_toggle_history_was_checked = !d->action_toggle_history->isChecked(); //to force update
	slotUpdateMode();
	slotCheckQuery();
}

KexiQueryDesignerSQLView::~KexiQueryDesignerSQLView()
{
	delete d;
}

KexiQueryDesignerSQLEditor *KexiQueryDesignerSQLView::editor() const
{
	return d->editor;
}

void KexiQueryDesignerSQLView::setStatusOk()
{
	d->pixmapStatus->setPixmap(d->statusPixmapOk);
	setStatusText("<h2>"+i18n("The query is correct")+"</h2>");
	d->history->addEvent(d->editor->text().stripWhiteSpace(), true, TQString());
}

void KexiQueryDesignerSQLView::setStatusError(const TQString& msg)
{
	d->pixmapStatus->setPixmap(d->statusPixmapErr);
	setStatusText("<h2>"+i18n("The query is incorrect")+"</h2><p>"+msg+"</p>");
	d->history->addEvent(d->editor->text().stripWhiteSpace(), false, msg);
}

void KexiQueryDesignerSQLView::setStatusEmpty()
{
	d->pixmapStatus->setPixmap(d->statusPixmapInfo);
	setStatusText(i18n("Please enter your query and execute \"Check query\" function to verify it."));
}

void KexiQueryDesignerSQLView::setStatusText(const TQString& text)
{
	if (!d->action_toggle_history->isChecked()) {
		TQSimpleRichText rt(text, d->lblStatus->font());
		rt.setWidth(d->lblStatus->width());
		TQValueList<int> sz = d->splitter->sizes();
		const int newHeight = rt.height()+d->lblStatus->margin()*2;
		if (sz[1]<newHeight) {
			sz[1] = newHeight;
			d->splitter->setSizes(sz);
		}
		d->lblStatus->setText(text);
	}
}

tristate
KexiQueryDesignerSQLView::beforeSwitchTo(int mode, bool &dontStore)
{
//TODO
	dontStore = true;
	if (mode==Kexi::DesignViewMode || mode==Kexi::DataViewMode) {
		TQString sqlText = d->editor->text().stripWhiteSpace();
		KexiQueryPart::TempData * temp = tempData();
		if (sqlText.isEmpty()) {
			//special case: empty SQL text
			if (temp->query()) {
				temp->queryChangedInPreviousView = true; //query changed
				temp->setQuery(0);
//				delete temp->query; //safe?
//				temp->query = 0;
			}
		}
		else {
			const bool designViewWasVisible = parentDialog()->viewForMode(mode)!=0;
			//should we check SQL text?
			if (designViewWasVisible 
				&& !d->justSwitchedFromNoViewMode //unchanged, but we should check SQL text
				&& compareSQL(d->origStatement, d->editor->text())) {
				//statement unchanged! - nothing to do
				temp->queryChangedInPreviousView = false;
			}
			else {
				//yes: parse SQL text
				if (!slotCheckQuery()) {
					if (KMessageBox::No==KMessageBox::warningYesNo(this, "<p>"+i18n("The query you entered is incorrect.")
						+"</p><p>"+i18n("Do you want to cancel any changes made to this SQL text?")+"</p>"
						+"</p><p>"+i18n("Answering \"No\" allows you to make corrections.")+"</p>"))
					{
						return cancelled;
					}
					//do not change original query - it's invalid
					temp->queryChangedInPreviousView = false;
					//this view is no longer _just_ switched from "NoViewMode"
					d->justSwitchedFromNoViewMode = false;
					return true;
				}
				//this view is no longer _just_ switched from "NoViewMode"
				d->justSwitchedFromNoViewMode = false;
				//replace old query schema with new one
				temp->setQuery( d->parsedQuery ); //this will also delete temp->query()
//				delete temp->query; //safe?
//				temp->query = d->parsedQuery;
				d->parsedQuery = 0;
				temp->queryChangedInPreviousView = true;
			}
		}
	}

	//TODO
	/*
	if (d->doc) {
		KexiDB::Parser *parser = new KexiDB::Parser(mainWin()->project()->dbConnection());
		parser->parse(getQuery());
		d->doc->setSchema(parser->select());

		if(parser->operation() == KexiDB::Parser::OP_Error)
		{
			d->history->addEvent(getQuery(), false, parser->error().error());
			kdDebug() << "KexiQueryDesignerSQLView::beforeSwitchTo(): syntax error!" << endl;
			return false;
		}
		delete parser;
	}

	setDirty(true);*/
//	if (parentDialog()->hasFocus())
	d->editor->setFocus();
	return true;
}

tristate
KexiQueryDesignerSQLView::afterSwitchFrom(int mode)
{
	kdDebug() << "KexiQueryDesignerSQLView::afterSwitchFrom()" << endl;
//	if (mode==Kexi::DesignViewMode || mode==Kexi::DataViewMode) {
	if (mode==Kexi::NoViewMode) {
		//User opened text view _directly_. 
		//This flag is set to indicate for beforeSwitchTo() that even if text has not been changed,
		//SQL text should be invalidated.
		d->justSwitchedFromNoViewMode = true;
	}
	KexiQueryPart::TempData * temp = tempData();
	KexiDB::QuerySchema *query = temp->query();
	if (!query) {//try to just get saved schema, instead of temporary one
		query = dynamic_cast<KexiDB::QuerySchema *>(parentDialog()->schemaData());
	}

	if (mode!=0/*failure only if it is switching from prev. view*/ && !query) {
		//TODO msg
		return false;
	}

	if (!query) {
		//no valid query schema delivered: just load sql text, no matter if it's valid
		if (!loadDataBlock( d->origStatement, "sql", true /*canBeEmpty*/ ))
			return false;
	}
	else {
	// Use query with Kexi keywords (but not driver-specific keywords) escaped.
		temp->setQuery( query );
//		temp->query = query;
		KexiDB::Connection* conn = mainWin()->project()->dbConnection();
		KexiDB::Connection::SelectStatementOptions options;
		options.identifierEscaping = KexiDB::Driver::EscapeKexi;
		options.addVisibleLookupColumns = false;
		d->origStatement = conn->selectStatement(*query, options).stripWhiteSpace();
	}

	d->slotTextChangedEnabled = false;
	 d->editor->setText( d->origStatement );
	d->slotTextChangedEnabled = true;
	TQTimer::singleShot(100, d->editor, TQT_SLOT(setFocus()));
	return true;
}

TQString
KexiQueryDesignerSQLView::sqlText() const
{
	return d->editor->text();
}

bool KexiQueryDesignerSQLView::slotCheckQuery()
{
	TQString sqlText( d->editor->text().stripWhiteSpace() );
	if (sqlText.isEmpty()) {
		delete d->parsedQuery;
		d->parsedQuery = 0;
		setStatusEmpty();
		return true;
	}

	kdDebug() << "KexiQueryDesignerSQLView::slotCheckQuery()" << endl;
	//KexiQueryPart::TempData * temp = tempData();
	KexiDB::Parser *parser = mainWin()->project()->sqlParser();
	const bool ok = parser->parse( sqlText );
	delete d->parsedQuery;
	d->parsedQuery = parser->query();
	if (!d->parsedQuery || !ok || !parser->error().type().isEmpty()) {
		KexiDB::ParserError err = parser->error();
		setStatusError(err.error());
		d->editor->jump(err.at());
		delete d->parsedQuery;
		d->parsedQuery = 0;
		return false;
	}

	setStatusOk();
	return true;
}

void KexiQueryDesignerSQLView::slotUpdateMode()
{
	if (d->action_toggle_history->isChecked() == d->action_toggle_history_was_checked)
		return;

	d->eventFilterForSplitterEnabled = false;

	TQValueList<int> sz = d->splitter->sizes();
	d->action_toggle_history_was_checked = d->action_toggle_history->isChecked();
	int heightToSet = -1;
	if (d->action_toggle_history->isChecked()) {
		d->status_hbox->hide();
		d->historyHead->show();
		d->history->show();
		if (d->heightForHistoryMode==-1)
			d->heightForHistoryMode = m_dialog->height() / 2;
		heightToSet = d->heightForHistoryMode;
		d->heightForStatusMode = sz[1]; //remember
	}
	else {
		if (d->historyHead)
			d->historyHead->hide();
		d->status_hbox->show();
		if (d->heightForStatusMode>=0) {
			heightToSet = d->heightForStatusMode;
		} else {
			d->heightForStatusMode = d->status_hbox->height();
		}
		if (d->heightForHistoryMode>=0)
			d->heightForHistoryMode = sz[1];
	}
	
	if (heightToSet>=0) {
		sz[1] = heightToSet;
		d->splitter->setSizes(sz);
	}
	d->eventFilterForSplitterEnabled = true;
	slotCheckQuery();
}

void KexiQueryDesignerSQLView::slotTextChanged()
{
	if (!d->slotTextChangedEnabled)
		return;
	setDirty(true);
	setStatusEmpty();
}

bool KexiQueryDesignerSQLView::eventFilter( TQObject *o, TQEvent *e )
{
	if (d->eventFilterForSplitterEnabled) {
		if (e->type()==TQEvent::Resize && o && TQT_BASE_OBJECT(o)==TQT_BASE_OBJECT(d->historyHead) && d->historyHead->isVisible()) {
			d->heightForHistoryMode = d->historyHead->height();
		}
		else if (e->type()==TQEvent::Resize && o && TQT_BASE_OBJECT(o)==TQT_BASE_OBJECT(d->status_hbox) && d->status_hbox->isVisible()) {
			d->heightForStatusMode = d->status_hbox->height();
		}
	}
	return KexiViewBase::eventFilter(o, e);
}

void KexiQueryDesignerSQLView::updateActions(bool activated)
{
	if (activated) {
		slotUpdateMode();
	}
	setAvailable("querypart_check_query", true);
	setAvailable("querypart_view_toggle_history", true);
	KexiViewBase::updateActions(activated);
}

void KexiQueryDesignerSQLView::slotSelectQuery()
{
	TQString sql = d->history->selectedStatement();
	if (!sql.isEmpty()) {
		d->editor->setText( sql );
	}
}

KexiQueryPart::TempData *
KexiQueryDesignerSQLView::tempData() const
{	
	return dynamic_cast<KexiQueryPart::TempData*>(parentDialog()->tempData());
}

KexiDB::SchemaData*
KexiQueryDesignerSQLView::storeNewData(const KexiDB::SchemaData& sdata, bool &cancel)
{
	Q_UNUSED( cancel );

	//here: we won't store query layout: it will be recreated 'by hand' in GUI Query Editor
	bool queryOK = slotCheckQuery();
	bool ok = true;
	KexiDB::SchemaData* query = 0;
	if (queryOK) {
		//query is ok
		if (d->parsedQuery) {
			query = d->parsedQuery; //will be returned, so: don't keep it
			d->parsedQuery = 0;
		}
		else {//empty query
			query = new KexiDB::SchemaData(); //just empty
		}

		(KexiDB::SchemaData&)*query = sdata; //copy main attributes
		ok = m_mainWin->project()->dbConnection()->storeObjectSchemaData( *query, true /*newObject*/ );
		if (ok) {
			m_dialog->setId( query->id() );
			ok = storeDataBlock( d->editor->text(), "sql" );
		}
	}
	else {
		//query is not ok
//#if 0
	//TODO: allow saving invalid queries
	//TODO: just ask this question:
		query = new KexiDB::SchemaData(); //just empty

		ok = (KMessageBox::questionYesNo(this, i18n("Do you want to save invalid query?"),
			0, KStdGuiItem::yes(), KStdGuiItem::no(), "askBeforeSavingInvalidQueries"/*config entry*/)==KMessageBox::Yes);
		if (ok) {
			(KexiDB::SchemaData&)*query = sdata; //copy main attributes
			ok = m_mainWin->project()->dbConnection()->storeObjectSchemaData( *query, true /*newObject*/ );
		}
		if (ok) {
			m_dialog->setId( query->id() );
			ok = storeDataBlock( d->editor->text(), "sql" );
		}
//#else
		//ok = false;
//#endif
	}
	if (!ok) {
		delete query;
		query = 0;
	}
	return query;
}

tristate KexiQueryDesignerSQLView::storeData(bool dontAsk)
{
	tristate res = KexiViewBase::storeData(dontAsk);
	if (~res)
		return res;
	if (res == true) {
		res = storeDataBlock( d->editor->text(), "sql" );
#if 0
		bool queryOK = slotCheckQuery();
		if (queryOK) {
			res = storeDataBlock( d->editor->text(), "sql" );
		}
		else {
			//query is not ok
			//TODO: allow saving invalid queries
			//TODO: just ask this question:
			res = false;
		}
#endif
	}
	if (res == true) {
		TQString empty_xml;
		res = storeDataBlock( empty_xml, "query_layout" ); //clear
	}
	if (!res)
		setDirty(true);
	return res;
}


/*void KexiQueryDesignerSQLView::slotHistoryHeaderButtonClicked(const TQString& buttonIdentifier)
{
	if (buttonIdentifier=="select_query") {
		slotSelectQuery();
	}
	else if (buttonIdentifier=="clear_history") {
		d->history->clear();
	}
}*/

#include "kexiquerydesignersql.moc"