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 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 <keximainwindow.h>
00025 #include <kexiutils/utils.h>
00026 
00027 #include "kexiqueryview.h"
00028 #include "kexiquerypart.h"
00029 #include "kexitableview.h"
00030 
00031 KexiQueryView::KexiQueryView(KexiMainWindow *win, QWidget *parent, const char *name)
00032  : KexiDataTable(win, parent, name)
00033 {
00034     tableView()->setInsertingEnabled(false); //default
00035 }
00036 
00037 KexiQueryView::~KexiQueryView()
00038 {
00039 }
00040 
00041 bool KexiQueryView::executeQuery(KexiDB::QuerySchema *query)
00042 {
00043     if (!query)
00044         return false;
00045     KexiUtils::WaitCursor wait;
00046     KexiDB::Cursor *rec = mainWin()->project()->dbConnection()->executeQuery(*query);
00047     if (!rec) {
00048         parentDialog()->setStatus(parentDialog()->mainWin()->project()->dbConnection(), i18n("Query executing failed."));
00049         //todo: also provide server result and sql statement
00050         return false;
00051     }
00052     setData(rec);
00053     //TODO: maybe allow writing and inserting for single-table relations?
00054     tableView()->setReadOnly( true );
00055     tableView()->setInsertingEnabled( false );
00056     return true;
00057 }
00058 
00059 tristate
00060 KexiQueryView::afterSwitchFrom(int mode)
00061 {
00062     if (mode==Kexi::NoViewMode) {
00063         KexiDB::QuerySchema *querySchema = static_cast<KexiDB::QuerySchema *>(parentDialog()->schemaData());
00064         if (!executeQuery(querySchema)) {
00065             return false;
00066         }
00067     }
00068     else if (mode==Kexi::DesignViewMode || Kexi::TextViewMode) {
00069         KexiQueryPart::TempData * temp = static_cast<KexiQueryPart::TempData*>(parentDialog()->tempData());
00070         if (!executeQuery(temp->query())) {
00071             return false;
00072         }
00073     }
00074 
00075 #if 0
00076     if (m_doc && m_doc->schema())
00077     {
00078         KexiDB::Cursor *rec = mainWin()->project()->dbConnection()->executeQuery(*m_doc->schema());
00079         QString statement = mainWin()->project()->dbConnection()->selectStatement(*m_doc->schema());
00080         if(!rec && !statement.isEmpty())
00081         {
00082             KexiDB::Parser *parser = new KexiDB::Parser(mainWin()->project()->dbConnection());
00083             parser->parse(statement);
00084             m_doc->setSchema(parser->select());
00085 
00086             if(parser->operation() == KexiDB::Parser::OP_Error)
00087                 m_doc->addHistoryItem(statement, parser->error().error());
00088 //          else
00089                 //m_doc->addHistoryItem(statement, mainWin()->project()->dbConnection()->serverErrorMsg());
00090                 //m_doc->addHistoryItem(statement, "The user is stupid");
00091 
00092 //          delete parser;
00093             return true;
00094         }
00095         setData(rec);
00096 
00097         m_doc->addHistoryItem(statement, "");
00098     }
00099 #endif
00100     return true;
00101 }
00102 
00103 
00104 #include "kexiqueryview.moc"
00105 
KDE Home | KDE Accessibility Home | Description of Access Keys