summaryrefslogtreecommitdiffstats
path: root/kexi/plugins/queries/kexiqueryview.cpp
blob: 9d7cc542a0de0a2fcdcac325d2cc037a8a0d7570 (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
/* This file is part of the KDE project
   Copyright (C) 2004 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 <kexiproject.h>
#include <kexidb/connection.h>
#include <kexidb/parser/parser.h>
#include <kexidb/cursor.h>
#include <keximainwindow.h>
#include <kexiutils/utils.h>

#include "kexiqueryview.h"
#include "kexiquerydesignersql.h"
#include "kexiquerydesignerguieditor.h"
#include "kexiquerypart.h"
#include <widget/tableview/kexitableview.h>
#include <widget/kexiqueryparameters.h>

//! @internal
class KexiQueryView::Private
{
	public:
		Private()
			: cursor(0)
//			, queryHasBeenChangedInViewMode( Kexi::NoViewMode )
		{}
		~Private() {}
		KexiDB::Cursor *cursor;
		/*! Used in storeNewData(), storeData() to decide whether 
		 we should ask other view to save changes.
		 Stores information about view mode. */
//		int queryHasBeenChangedInViewMode;
};

//---------------------------------------------------------------------------------

KexiQueryView::KexiQueryView(KexiMainWindow *win, TQWidget *tqparent, const char *name)
 : KexiDataTable(win, tqparent, name)
 , d( new Private() )
{
	tableView()->setInsertingEnabled(false); //default
}

KexiQueryView::~KexiQueryView()
{
	if (d->cursor)
		d->cursor->connection()->deleteCursor(d->cursor);
	delete d;
}

tristate KexiQueryView::executeQuery(KexiDB::QuerySchema *query)
{
	if (!query)
		return false;
	KexiUtils::WaitCursor wait;
	KexiDB::Cursor *oldCursor = d->cursor;
	KexiDB::debug( query->parameters() );
	bool ok;
	TQValueList<TQVariant> params;
	{
		KexiUtils::WaitCursorRemover remover;
		params = KexiQueryParameters::getParameters(this, 
			*mainWin()->project()->dbConnection()->driver(), *query, ok);
	}
	if (!ok) {//input cancelled
		return cancelled;
	}
	d->cursor = mainWin()->project()->dbConnection()->executeQuery(*query, params);
	if (!d->cursor) {
		parentDialog()->settqStatus(parentDialog()->mainWin()->project()->dbConnection(), 
			i18n("Query executing failed."));
		//todo: also provide server result and sql statement
		return false;
	}
	setData(d->cursor);

//! @todo remove close() when dynamic cursors arrive
	d->cursor->close();

	if (oldCursor)
		oldCursor->connection()->deleteCursor(oldCursor);

//! @todo maybe allow writing and inserting for single-table relations?
	tableView()->setReadOnly( true );
//! @todo maybe allow writing and inserting for single-table relations?
	//set data model itself read-only too
	tableView()->KexiDataAwareObjectInterface::data()->setReadOnly( true );
	tableView()->setInsertingEnabled( false );
	return true;
}

tristate KexiQueryView::afterSwitchFrom(int mode)
{
	if (mode==Kexi::NoViewMode) {
		KexiDB::QuerySchema *querySchema = static_cast<KexiDB::QuerySchema *>(parentDialog()->schemaData());
		const tristate result = executeQuery(querySchema);
		if (true != result)
			return result;
	}
	else if (mode==Kexi::DesignViewMode || Kexi::TextViewMode) {
		KexiQueryPart::TempData * temp = static_cast<KexiQueryPart::TempData*>(parentDialog()->tempData());

		//remember what view we should use to store data changes, if needed
//		if (temp->queryChangedInPreviousView)
//			d->queryHasBeenChangedInViewMode = mode;
//		else
//			d->queryHasBeenChangedInViewMode = Kexi::NoViewMode;

		const tristate result = executeQuery(temp->query());
		if (true != result)
			return result;
	}
	return true;
}

KexiDB::SchemaData* KexiQueryView::storeNewData(const KexiDB::SchemaData& sdata, bool &cancel)
{
	KexiViewBase * view = parentDialog()->viewThatRecentlySetDirtyFlag();
	if (dynamic_cast<KexiQueryDesignerGuiEditor*>(view))
		return dynamic_cast<KexiQueryDesignerGuiEditor*>(view)->storeNewData(sdata, cancel);
	if (dynamic_cast<KexiQueryDesignerSQLView*>(view))
		return dynamic_cast<KexiQueryDesignerSQLView*>(view)->storeNewData(sdata, cancel);
	return 0;
}

tristate KexiQueryView::storeData(bool dontAsk)
{
	KexiViewBase * view = parentDialog()->viewThatRecentlySetDirtyFlag();
	if (dynamic_cast<KexiQueryDesignerGuiEditor*>(view))
		return dynamic_cast<KexiQueryDesignerGuiEditor*>(view)->storeData(dontAsk);
	if (dynamic_cast<KexiQueryDesignerSQLView*>(view))
		return dynamic_cast<KexiQueryDesignerSQLView*>(view)->storeData(dontAsk);
	return false;
}


#include "kexiqueryview.moc"