kexi
kexiqueryview.cpp
00001 /* This file is part of the KDE project 00002 Copyright (C) 2004 Lucijan Busch <lucijan@kde.org> 00003 Copyright (C) 2004, 2006 Jaroslaw Staniek <js@iidea.pl> 00004 00005 This program is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 This program is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public License 00016 along with this program; see the file COPYING. If not, write to 00017 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 * Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #include <kexiproject.h> 00022 #include <kexidb/connection.h> 00023 #include <kexidb/parser/parser.h> 00024 #include <kexidb/cursor.h> 00025 #include <keximainwindow.h> 00026 #include <kexiutils/utils.h> 00027 00028 #include "kexiqueryview.h" 00029 #include "kexiquerydesignersql.h" 00030 #include "kexiquerydesignerguieditor.h" 00031 #include "kexiquerypart.h" 00032 #include "kexitableview.h" 00033 00035 class KexiQueryView::Private 00036 { 00037 public: 00038 Private() 00039 : cursor(0) 00040 // , queryHasBeenChangedInViewMode( Kexi::NoViewMode ) 00041 {} 00042 ~Private() {} 00043 KexiDB::Cursor *cursor; 00047 // int queryHasBeenChangedInViewMode; 00048 }; 00049 00050 //--------------------------------------------------------------------------------- 00051 00052 KexiQueryView::KexiQueryView(KexiMainWindow *win, QWidget *parent, const char *name) 00053 : KexiDataTable(win, parent, name) 00054 , d( new Private() ) 00055 { 00056 tableView()->setInsertingEnabled(false); //default 00057 } 00058 00059 KexiQueryView::~KexiQueryView() 00060 { 00061 if (d->cursor) 00062 d->cursor->connection()->deleteCursor(d->cursor); 00063 delete d; 00064 } 00065 00066 bool KexiQueryView::executeQuery(KexiDB::QuerySchema *query) 00067 { 00068 if (!query) 00069 return false; 00070 KexiUtils::WaitCursor wait; 00071 KexiDB::Cursor *oldCursor = d->cursor; 00072 d->cursor = mainWin()->project()->dbConnection()->executeQuery(*query); 00073 if (!d->cursor) { 00074 parentDialog()->setStatus(parentDialog()->mainWin()->project()->dbConnection(), i18n("Query executing failed.")); 00075 //todo: also provide server result and sql statement 00076 return false; 00077 } 00078 setData(d->cursor); 00079 00081 d->cursor->close(); 00082 00083 if (oldCursor) 00084 oldCursor->connection()->deleteCursor(oldCursor); 00085 00086 //TODO: maybe allow writing and inserting for single-table relations? 00087 tableView()->setReadOnly( true ); 00088 tableView()->setInsertingEnabled( false ); 00089 return true; 00090 } 00091 00092 tristate KexiQueryView::afterSwitchFrom(int mode) 00093 { 00094 if (mode==Kexi::NoViewMode) { 00095 KexiDB::QuerySchema *querySchema = static_cast<KexiDB::QuerySchema *>(parentDialog()->schemaData()); 00096 if (!executeQuery(querySchema)) { 00097 return false; 00098 } 00099 } 00100 else if (mode==Kexi::DesignViewMode || Kexi::TextViewMode) { 00101 KexiQueryPart::TempData * temp = static_cast<KexiQueryPart::TempData*>(parentDialog()->tempData()); 00102 00103 //remember what view we should use to store data changes, if needed 00104 // if (temp->queryChangedInPreviousView) 00105 // d->queryHasBeenChangedInViewMode = mode; 00106 // else 00107 // d->queryHasBeenChangedInViewMode = Kexi::NoViewMode; 00108 00109 if (!executeQuery(temp->query())) { 00110 return false; 00111 } 00112 } 00113 return true; 00114 } 00115 00116 KexiDB::SchemaData* KexiQueryView::storeNewData(const KexiDB::SchemaData& sdata, bool &cancel) 00117 { 00118 KexiViewBase * view = parentDialog()->viewThatRecentlySetDirtyFlag(); 00119 if (dynamic_cast<KexiQueryDesignerGuiEditor*>(view)) 00120 return dynamic_cast<KexiQueryDesignerGuiEditor*>(view)->storeNewData(sdata, cancel); 00121 if (dynamic_cast<KexiQueryDesignerSQLView*>(view)) 00122 return dynamic_cast<KexiQueryDesignerSQLView*>(view)->storeNewData(sdata, cancel); 00123 return 0; 00124 } 00125 00126 tristate KexiQueryView::storeData(bool dontAsk) 00127 { 00128 KexiViewBase * view = parentDialog()->viewThatRecentlySetDirtyFlag(); 00129 if (dynamic_cast<KexiQueryDesignerGuiEditor*>(view)) 00130 return dynamic_cast<KexiQueryDesignerGuiEditor*>(view)->storeData(dontAsk); 00131 if (dynamic_cast<KexiQueryDesignerSQLView*>(view)) 00132 return dynamic_cast<KexiQueryDesignerSQLView*>(view)->storeData(dontAsk); 00133 return false; 00134 } 00135 00136 00137 #include "kexiqueryview.moc" 00138