kexiqueryview.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2004 Lucijan Busch <lucijan@kde.org>
3 Copyright (C) 2004, 2006 Jaroslaw Staniek <js@iidea.pl>
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this program; see the file COPYING. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20
21#include <kexiproject.h>
22#include <kexidb/connection.h>
23#include <kexidb/parser/parser.h>
24#include <kexidb/cursor.h>
25#include <keximainwindow.h>
26#include <kexiutils/utils.h>
27
28#include "kexiqueryview.h"
29#include "kexiquerydesignersql.h"
30#include "kexiquerydesignerguieditor.h"
31#include "kexiquerypart.h"
32#include <widget/tableview/kexitableview.h>
33#include <widget/kexiqueryparameters.h>
34
36class KexiQueryView::Private
37{
38 public:
39 Private()
40 : cursor(0)
41// , queryHasBeenChangedInViewMode( Kexi::NoViewMode )
42 {}
43 ~Private() {}
44 KexiDB::Cursor *cursor;
48// int queryHasBeenChangedInViewMode;
49};
50
51//---------------------------------------------------------------------------------
52
53KexiQueryView::KexiQueryView(KexiMainWindow *win, TQWidget *parent, const char *name)
54 : KexiDataTable(win, parent, name)
55 , d( new Private() )
56{
57 tableView()->setInsertingEnabled(false); //default
58}
59
60KexiQueryView::~KexiQueryView()
61{
62 if (d->cursor)
63 d->cursor->connection()->deleteCursor(d->cursor);
64 delete d;
65}
66
67tristate KexiQueryView::executeQuery(KexiDB::QuerySchema *query)
68{
69 if (!query)
70 return false;
72 KexiDB::Cursor *oldCursor = d->cursor;
73 KexiDB::debug( query->parameters() );
74 bool ok;
75 TQValueList<TQVariant> params;
76 {
79 *mainWin()->project()->dbConnection()->driver(), *query, ok);
80 }
81 if (!ok) {//input cancelled
82 return cancelled;
83 }
84 d->cursor = mainWin()->project()->dbConnection()->executeQuery(*query, params);
85 if (!d->cursor) {
86 parentDialog()->setStatus(parentDialog()->mainWin()->project()->dbConnection(),
87 i18n("Query executing failed."));
88 //todo: also provide server result and sql statement
89 return false;
90 }
91 setData(d->cursor);
92
94 d->cursor->close();
95
96 if (oldCursor)
97 oldCursor->connection()->deleteCursor(oldCursor);
98
100 tableView()->setReadOnly( true );
102 //set data model itself read-only too
103 tableView()->KexiDataAwareObjectInterface::data()->setReadOnly( true );
104 tableView()->setInsertingEnabled( false );
105 return true;
106}
107
108tristate KexiQueryView::afterSwitchFrom(int mode)
109{
110 if (mode==Kexi::NoViewMode) {
111 KexiDB::QuerySchema *querySchema = static_cast<KexiDB::QuerySchema *>(parentDialog()->schemaData());
112 const tristate result = executeQuery(querySchema);
113 if (true != result)
114 return result;
115 }
116 else if (mode==Kexi::DesignViewMode || Kexi::TextViewMode) {
117 KexiQueryPart::TempData * temp = static_cast<KexiQueryPart::TempData*>(parentDialog()->tempData());
118
119 //remember what view we should use to store data changes, if needed
120// if (temp->queryChangedInPreviousView)
121// d->queryHasBeenChangedInViewMode = mode;
122// else
123// d->queryHasBeenChangedInViewMode = Kexi::NoViewMode;
124
125 const tristate result = executeQuery(temp->query());
126 if (true != result)
127 return result;
128 }
129 return true;
130}
131
132KexiDB::SchemaData* KexiQueryView::storeNewData(const KexiDB::SchemaData& sdata, bool &cancel)
133{
134 KexiViewBase * view = parentDialog()->viewThatRecentlySetDirtyFlag();
135 if (dynamic_cast<KexiQueryDesignerGuiEditor*>(view))
136 return dynamic_cast<KexiQueryDesignerGuiEditor*>(view)->storeNewData(sdata, cancel);
137 if (dynamic_cast<KexiQueryDesignerSQLView*>(view))
138 return dynamic_cast<KexiQueryDesignerSQLView*>(view)->storeNewData(sdata, cancel);
139 return 0;
140}
141
142tristate KexiQueryView::storeData(bool dontAsk)
143{
144 KexiViewBase * view = parentDialog()->viewThatRecentlySetDirtyFlag();
145 if (dynamic_cast<KexiQueryDesignerGuiEditor*>(view))
146 return dynamic_cast<KexiQueryDesignerGuiEditor*>(view)->storeData(dontAsk);
147 if (dynamic_cast<KexiQueryDesignerSQLView*>(view))
148 return dynamic_cast<KexiQueryDesignerSQLView*>(view)->storeData(dontAsk);
149 return false;
150}
151
152
153#include "kexiqueryview.moc"
154
bool deleteCursor(Cursor *cursor)
Provides database cursor functionality.
Definition cursor.h:67
Connection * connection() const
Definition cursor.h:81
KexiDB::QuerySchema provides information about database query.
QuerySchemaParameterList parameters()
Provides a data-driven (record-based) tabular view.
Kexi's main window interface This interface is implemented by KexiMainWindowImpl class.
Design view of the Query Designer.
The KexiQueryDesignerSQLView class for editing Queries in text mode.
virtual KexiDB::SchemaData * storeNewData(const KexiDB::SchemaData &sdata, bool &cancel)
virtual tristate storeData(bool dontAsk=false)
static TQValueList< TQVariant > getParameters(TQWidget *parent, const KexiDB::Driver &driver, KexiDB::QuerySchema &querySchema, bool &ok)
Temporary data kept in memory while switching between Query Dialog's views.
KexiDB::QuerySchema * query() const
Base class for single view embeddable of in KexiDialogBase.
3-state logical type with three values: true, false and cancelled and convenient operators.
Definition tristate.h:100
KEXI_DB_EXPORT void debug(const QuerySchemaParameterList &list)
Shows debug information for list.
@ TextViewMode
Also known as SQL View Mode.
Definition kexi.h:48
@ NoViewMode
In KexiViewBase::afterSwitchFrom() and KexiViewBase::beforeSwitchTo() means that parent dialog of the...
Definition kexi.h:44