kexiquerypart.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 "kexiquerypart.h"
22
23#include <kdebug.h>
24#include <kgenericfactory.h>
25
26#include <keximainwindow.h>
27#include <kexidialogbase.h>
28#include <kexiproject.h>
29#include <kexipartinfo.h>
30
31#include <kexidb/cursor.h>
32#include <kexidb/parser/parser.h>
33
34#include "kexiqueryview.h"
35#include "kexiquerydesignerguieditor.h"
36#include "kexiquerydesignersql.h"
37
38//------------------------------------------------
39
40KexiQueryPart::KexiQueryPart(TQObject *parent, const char *name, const TQStringList &l)
41 : KexiPart::Part(parent, name, l)
42{
43 // REGISTERED ID:
44 m_registeredPartID = (int)KexiPart::QueryObjectType;
45
46 m_names["instanceName"]
47 = i18n("Translate this word using only lowercase alphanumeric characters (a..z, 0..9). "
48 "Use '_' character instead of spaces. First character should be a..z character. "
49 "If you cannot use latin characters in your language, use english word.",
50 "query");
51 m_names["instanceCaption"] = i18n("Query");
52 m_supportedViewModes = Kexi::DataViewMode | Kexi::DesignViewMode | Kexi::TextViewMode;
53}
54
55KexiQueryPart::~KexiQueryPart()
56{
57}
58
61{
62 KexiQueryPart::TempData *data = new KexiQueryPart::TempData(dialog, dialog->mainWin()->project()->dbConnection());
63 data->listenerInfoString = dialog->part()->instanceCaption() + " \"" + dialog->partItem()->name() + "\"";
64 return data;
65}
66
68KexiQueryPart::createView(TQWidget *parent, KexiDialogBase* dialog, KexiPart::Item &item, int viewMode, TQMap<TQString,TQString>*)
69{
70 Q_UNUSED( item );
71
72 kdDebug() << "KexiQueryPart::createView()" << endl;
73
74 if (viewMode == Kexi::DataViewMode) {
75 return new KexiQueryView(dialog->mainWin(), parent, "dataview");
76 }
77 else if (viewMode == Kexi::DesignViewMode) {
79 dialog->mainWin(), parent, "guieditor");
80 //needed for updating tables combo box:
81 KexiProject *prj = dialog->mainWin()->project();
82 connect(prj, TQ_SIGNAL(newItemStored(KexiPart::Item&)),
83 view, TQ_SLOT(slotNewItemStored(KexiPart::Item&)));
84 connect(prj, TQ_SIGNAL(itemRemoved(const KexiPart::Item&)),
85 view, TQ_SLOT(slotItemRemoved(const KexiPart::Item&)));
86 connect(prj, TQ_SIGNAL(itemRenamed(const KexiPart::Item&, const TQCString&)),
87 view, TQ_SLOT(slotItemRenamed(const KexiPart::Item&, const TQCString&)));
88
89// connect(dialog->mainWin()->project(), TQ_SIGNAL(tableCreated(KexiDB::TableSchema&)),
90// view, TQ_SLOT(slotTableCreated(KexiDB::TableSchema&)));
91 return view;
92 }
93 else if (viewMode == Kexi::TextViewMode) {
94 return new KexiQueryDesignerSQLView(dialog->mainWin(), parent, "sqldesigner");
95 }
96
97 return 0;
98}
99
100bool
102{
103 if (!win || !win->project() || !win->project()->dbConnection())
104 return false;
105 KexiDB::Connection *conn = win->project()->dbConnection();
106 KexiDB::QuerySchema *sch = conn->querySchema(item.identifier());
107 if (sch)
108 return conn->dropQuery( sch );
109 //last chance: just remove item
110 return conn->removeObject( item.identifier() );
111}
112
113#if 0
115KexiQueryPart::dataSource()
116{
117 return new KexiQueryDataSource(this);
118}
119
120void KexiQueryPart::initPartActions( TDEActionCollection *col )
121{
122}
123
124void KexiQueryPart::initInstanceActions( int mode, TDEActionCollection *col )
125{
126 if (mode==Kexi::DataViewMode) {
127 }
128 else if (mode==Kexi::DesignViewMode) {
129 }
130 else if (mode==Kexi::TextViewMode) {
131// new TDEAction(i18n("Check Query"), "test_it", 0, this, TQ_SLOT(slotCheckQuery()), col, "querypart_check_query");
132
133//TODO new TDEAction(i18n("Execute Query"), "?????", 0, this, TQ_SLOT(checkQuery()), col, "querypart_execute_query");
134 }
135}
136#endif
137
138void KexiQueryPart::initPartActions()
139{
140}
141
142void KexiQueryPart::initInstanceActions()
143{
144// new TDEAction(i18n("Check Query"), "test_it", 0, this, TQ_SLOT(slotCheckQuery()),
145// m_instanceGuiClients[Kexi::DesignViewMode]->actionCollection(), "querypart_check_query");
146
147 TDEAction *a = createSharedAction(Kexi::TextViewMode, i18n("Check Query"), "test_it",
148 Key_F9, "querypart_check_query");
149 a->setToolTip(i18n("Check Query"));
150 a->setWhatsThis(i18n("Checks query for validity."));
151
153 Kexi::TextViewMode, i18n("Show SQL History"), "view_top_bottom"/*TODO other icon*/,
154 0, "querypart_view_toggle_history");
155 a->setWhatsThis(i18n("Shows or hides SQL editor's history."));
156
157// setActionAvailable("querypart_check_query", true);
158}
159
161KexiQueryPart::loadSchemaData(KexiDialogBase *dlg, const KexiDB::SchemaData& sdata, int viewMode)
162{
163 KexiQueryPart::TempData * temp = static_cast<KexiQueryPart::TempData*>(dlg->tempData());
164 TQString sqlText;
165 if (!loadDataBlock( dlg, sqlText, "sql" )) {
166 return 0;
167 }
168 KexiDB::Parser *parser = dlg->mainWin()->project()->sqlParser();
169 parser->parse( sqlText );
170 KexiDB::QuerySchema *query = parser->query();
171 //error?
172 if (!query) {
173 if (viewMode==Kexi::TextViewMode) {
174 //for SQL view, no parsing is initially needed:
175 //-just make a copy:
176 return KexiPart::Part::loadSchemaData(dlg, sdata, viewMode);
177 }
178 /* Set this to true on data loading loadSchemaData() to indicate that TextView mode
179 could be used instead of DataView or DesignView, because there are problems
180 with opening object. */
182 //todo
183 return 0;
184 }
185 query->debug();
186 (KexiDB::SchemaData&)*query = sdata; //copy main attributes
187
188 temp->registerTableSchemaChanges(query);
189
190 query->debug();
191 return query;
192}
193
194TQString KexiQueryPart::i18nMessage(const TQCString& englishMessage, KexiDialogBase* dlg) const
195{
196 Q_UNUSED(dlg);
197 if (englishMessage=="Design of object \"%1\" has been modified.")
198 return i18n("Design of query \"%1\" has been modified.");
199 if (englishMessage=="Object \"%1\" already exists.")
200 return i18n("Query \"%1\" already exists.");
201
202 return englishMessage;
203}
204
205tristate KexiQueryPart::rename(KexiMainWindow *win, KexiPart::Item &item, const TQString& newName)
206{
207 Q_UNUSED(newName);
208 if (!win->project()->dbConnection())
209 return false;
210 win->project()->dbConnection()->setQuerySchemaObsolete( item.name() );
211 return true;
212}
213
214//----------------
215
216KexiQueryPart::TempData::TempData(KexiDialogBase* parent, KexiDB::Connection *conn)
217 : KexiDialogTempData(parent)
218 , KexiDB::Connection::TableSchemaChangeListenerInterface()
219 , queryChangedInPreviousView(false)
220 , m_query(0)
221{
222 this->conn = conn;
223}
224
225KexiQueryPart::TempData::~TempData()
226{
227 conn->unregisterForTablesSchemaChanges(*this);
228}
229
230void KexiQueryPart::TempData::clearQuery()
231{
232 if (!m_query)
233 return;
234 unregisterForTablesSchemaChanges();
235 m_query->clear();
236}
237
238void KexiQueryPart::TempData::unregisterForTablesSchemaChanges()
239{
240 conn->unregisterForTablesSchemaChanges(*this);
241}
242
243void KexiQueryPart::TempData::registerTableSchemaChanges(KexiDB::QuerySchema *q)
244{
245 if (!q)
246 return;
248 it.current(); ++it)
249 {
250 conn->registerForTableSchemaChanges(*this, *it.current());
251 }
252}
253
254tristate KexiQueryPart::TempData::closeListener()
255{
256 KexiDialogBase* dlg = static_cast<KexiDialogBase*>(parent());
257 return dlg->mainWin()->closeDialog(dlg);
258}
259
261{
262 KexiDB::QuerySchema *query = m_query;
263 m_query = 0;
264 return query;
265}
266
268{
269 if (m_query && m_query == query)
270 return;
271 if (m_query
272 /* query not owned by dialog */
273 && (static_cast<KexiDialogBase*>(parent())->schemaData() != static_cast<KexiDB::SchemaData*>( m_query )))
274 {
275 delete m_query;
276 }
277 m_query = query;
278}
279
280//----------------
281
282#if 0
283KexiQueryDataSource::KexiQueryDataSource(KexiPart::Part *part)
284 : KexiPart::DataSource(part)
285{
286}
287
288KexiQueryDataSource::~KexiQueryDataSource()
289{
290}
291
293KexiQueryDataSource::fields(KexiProject *, const KexiPart::Item &)
294{
295 return 0;
296}
297
299KexiQueryDataSource::cursor(KexiProject *, const KexiPart::Item &, bool)
300{
301 return 0;
302}
303#endif
304
305//----------------
306
307K_EXPORT_COMPONENT_FACTORY( kexihandler_query, KGenericFactory<KexiQueryPart>("kexihandler_query") )
308
309#include "kexiquerypart.moc"
310
Provides database connection, allowing queries and data modification.
Definition connection.h:62
bool removeObject(uint objId)
bool setQuerySchemaObsolete(const TQString &queryName)
QuerySchema * querySchema(int queryId)
bool dropQuery(QuerySchema *querySchema)
Provides database cursor functionality.
Definition cursor.h:67
Parser for SQL statements.
Definition parser.h:113
KexiDB::QuerySchema provides information about database query.
TableSchema::List * tables() const
TQPtrListIterator< TableSchema > ListIterator
Iterator for tables list.
Definition tableschema.h:46
Base class for child window of Kexi's main application window.
KexiDialogTempData * tempData() const
KexiPart::Part * part() const
KexiPart::Item * partItem() const
Privides temporary data shared between KexiDialogBase's views (KexiView's)
bool proposeOpeningInTextViewModeBecauseOfProblems
Kexi's main window interface This interface is implemented by KexiMainWindowImpl class.
virtual tristate closeDialog(KexiDialogBase *dlg)=0
virtual KexiProject * project()=0
Project data of currently opened project or NULL if no project here yet.
this class provides a datasource framework for e.g.
Information about a single object that can be instantiated using Kexi Part.
The main class for kexi frontend parts like tables, queries, forms and reports.
Definition kexipart.h:66
TDEAction * createSharedAction(int mode, const TQString &text, const TQString &pix_name, const TDEShortcut &cut, const char *name, const char *subclassName=0)
Definition kexipart.cpp:170
TQString instanceCaption() const
Definition kexipart.cpp:415
TDEAction * createSharedToggleAction(int mode, const TQString &text, const TQString &pix_name, const TDEShortcut &cut, const char *name)
Definition kexipart.cpp:193
A project's main controller.
Definition kexiproject.h:68
KexiDB::Connection * dbConnection() const
Design view of the Query Designer.
The KexiQueryDesignerSQLView class for editing Queries in text mode.
Temporary data kept in memory while switching between Query Dialog's views.
KexiDB::QuerySchema * takeQuery()
Takes query associated with this data (without deleting) and returns it.
void setQuery(KexiDB::QuerySchema *query)
KexiDB::Connection * conn
Connection used for retrieving definition of the query.
virtual TQString i18nMessage(const TQCString &englishMessage, KexiDialogBase *dlg) const
virtual KexiViewBase * createView(TQWidget *parent, KexiDialogBase *dialog, KexiPart::Item &item, int viewMode=Kexi::DataViewMode, TQMap< TQString, TQString > *staticObjectArgs=0)
virtual bool remove(KexiMainWindow *win, KexiPart::Item &item)
virtual tristate rename(KexiMainWindow *win, KexiPart::Item &item, const TQString &newName)
virtual KexiDialogTempData * createTempData(KexiDialogBase *dialog)
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
High-level database connectivity library with database backend drivers.
@ TextViewMode
Also known as SQL View Mode.
Definition kexi.h:48