kexi

kexiquerypart.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 "kexiquerypart.h"
00022 
00023 #include <kdebug.h>
00024 #include <kgenericfactory.h>
00025 
00026 #include <keximainwindow.h>
00027 #include <kexidialogbase.h>
00028 #include <kexiproject.h>
00029 #include <kexipartinfo.h>
00030 
00031 #include <kexidb/cursor.h>
00032 #include <kexidb/parser/parser.h>
00033 
00034 #include "kexiqueryview.h"
00035 #include "kexiquerydesignerguieditor.h"
00036 #include "kexiquerydesignersql.h"
00037 
00038 //------------------------------------------------
00039 
00040 KexiQueryPart::KexiQueryPart(QObject *parent, const char *name, const QStringList &l)
00041  : KexiPart::Part(parent, name, l)
00042 {
00043     // REGISTERED ID:
00044     m_registeredPartID = (int)KexiPart::QueryObjectType;
00045 
00046     m_names["instanceName"] 
00047         = i18n("Translate this word using only lowercase alphanumeric characters (a..z, 0..9). "
00048         "Use '_' character instead of spaces. First character should be a..z character. "
00049         "If you cannot use latin characters in your language, use english word.", 
00050         "query");
00051     m_names["instanceCaption"] = i18n("Query");
00052     m_supportedViewModes = Kexi::DataViewMode | Kexi::DesignViewMode | Kexi::TextViewMode;
00053 }
00054 
00055 KexiQueryPart::~KexiQueryPart()
00056 {
00057 }
00058 
00059 KexiDialogTempData* 
00060 KexiQueryPart::createTempData(KexiDialogBase* dialog)
00061 {
00062     KexiQueryPart::TempData *data = new KexiQueryPart::TempData(dialog, dialog->mainWin()->project()->dbConnection());
00063     data->listenerInfoString = dialog->part()->instanceCaption() + " \"" + dialog->partItem()->name() + "\"";
00064     return data;
00065 }
00066 
00067 KexiViewBase*
00068 KexiQueryPart::createView(QWidget *parent, KexiDialogBase* dialog, KexiPart::Item &item, int viewMode, QMap<QString,QString>*)
00069 {
00070     Q_UNUSED( item );
00071 
00072     kdDebug() << "KexiQueryPart::createView()" << endl;
00073 
00074     if (viewMode == Kexi::DataViewMode) {
00075         return new KexiQueryView(dialog->mainWin(), parent, "dataview");
00076     }
00077     else if (viewMode == Kexi::DesignViewMode) {
00078         KexiQueryDesignerGuiEditor* view = new KexiQueryDesignerGuiEditor(
00079             dialog->mainWin(), parent, "guieditor");
00080         //needed for updating tables combo box:
00081         KexiProject *prj = dialog->mainWin()->project();
00082         connect(prj, SIGNAL(newItemStored(KexiPart::Item&)),
00083             view, SLOT(slotNewItemStored(KexiPart::Item&)));
00084         connect(prj, SIGNAL(itemRemoved(const KexiPart::Item&)),
00085             view, SLOT(slotItemRemoved(const KexiPart::Item&)));
00086         connect(prj, SIGNAL(itemRenamed(const KexiPart::Item&, const QCString&)),
00087             view, SLOT(slotItemRenamed(const KexiPart::Item&, const QCString&)));
00088 
00089 //      connect(dialog->mainWin()->project(), SIGNAL(tableCreated(KexiDB::TableSchema&)),
00090 //          view, SLOT(slotTableCreated(KexiDB::TableSchema&)));
00091         return view;
00092     }
00093     else if (viewMode == Kexi::TextViewMode) {
00094         return new KexiQueryDesignerSQLView(dialog->mainWin(), parent, "sqldesigner");
00095     }
00096 
00097     return 0;
00098 }
00099 
00100 bool
00101 KexiQueryPart::remove(KexiMainWindow *win, KexiPart::Item &item)
00102 {
00103     if (!win || !win->project() || !win->project()->dbConnection())
00104         return false;
00105     KexiDB::Connection *conn = win->project()->dbConnection();
00106     KexiDB::QuerySchema *sch = conn->querySchema(item.identifier());
00107     if (sch)
00108         return conn->dropQuery( sch );
00109     //last chance: just remove item
00110     return conn->removeObject( item.identifier() );
00111 }
00112 
00113 #if 0
00114 KexiPart::DataSource *
00115 KexiQueryPart::dataSource()
00116 {
00117     return new KexiQueryDataSource(this);
00118 }
00119 
00120 void KexiQueryPart::initPartActions( KActionCollection *col )
00121 {
00122 }
00123 
00124 void KexiQueryPart::initInstanceActions( int mode, KActionCollection *col )
00125 {
00126     if (mode==Kexi::DataViewMode) {
00127     }
00128     else if (mode==Kexi::DesignViewMode) {
00129     }
00130     else if (mode==Kexi::TextViewMode) {
00131 //      new KAction(i18n("Check Query"), "test_it", 0, this, SLOT(slotCheckQuery()), col, "querypart_check_query");
00132 
00133 //TODO      new KAction(i18n("Execute Query"), "?????", 0, this, SLOT(checkQuery()), col, "querypart_execute_query");
00134     }
00135 }
00136 #endif
00137 
00138 void KexiQueryPart::initPartActions()
00139 {
00140 }
00141 
00142 void KexiQueryPart::initInstanceActions()
00143 {
00144 //  new KAction(i18n("Check Query"), "test_it", 0, this, SLOT(slotCheckQuery()), 
00145 //      m_instanceGuiClients[Kexi::DesignViewMode]->actionCollection(), "querypart_check_query");
00146 
00147     KAction *a = createSharedAction(Kexi::TextViewMode, i18n("Check Query"), "test_it", 
00148         Key_F9, "querypart_check_query");
00149     a->setToolTip(i18n("Check Query"));
00150     a->setWhatsThis(i18n("Checks query for validity."));
00151 
00152     a = createSharedToggleAction(
00153         Kexi::TextViewMode, i18n("Show SQL History"), "view_top_bottom"/*TODO other icon*/,
00154         0, "querypart_view_toggle_history");
00155     a->setWhatsThis(i18n("Shows or hides SQL editor's history."));
00156 
00157 //  setActionAvailable("querypart_check_query", true);
00158 }
00159 
00160 KexiDB::SchemaData*
00161 KexiQueryPart::loadSchemaData(KexiDialogBase *dlg, const KexiDB::SchemaData& sdata, int viewMode)
00162 {
00163     KexiQueryPart::TempData * temp = static_cast<KexiQueryPart::TempData*>(dlg->tempData());
00164     QString sqlText;
00165     if (!loadDataBlock( dlg, sqlText, "sql" )) {
00166         return 0;
00167     }
00168     KexiDB::Parser *parser = dlg->mainWin()->project()->sqlParser();
00169     parser->parse( sqlText );
00170     KexiDB::QuerySchema *query = parser->query();
00171     //error?
00172     if (!query) {
00173         if (viewMode==Kexi::TextViewMode) {
00174             //for SQL view, no parsing is initially needed:
00175             //-just make a copy:
00176             return KexiPart::Part::loadSchemaData(dlg, sdata, viewMode);
00177         }
00178         /* Set this to true on data loading loadSchemaData() to indicate that TextView mode 
00179          could be used instead of DataView or DesignView, because there are problems 
00180          with opening object. */
00181         temp->proposeOpeningInTextViewModeBecauseOfProblems = true;
00182         //todo
00183         return 0;
00184     }
00185     query->debug();
00186     (KexiDB::SchemaData&)*query = sdata; //copy main attributes
00187 
00188     temp->registerTableSchemaChanges(query);
00189 
00190     query->debug();
00191     return query;
00192 }
00193 
00194 QString KexiQueryPart::i18nMessage(const QCString& englishMessage, KexiDialogBase* dlg) const
00195 {
00196     Q_UNUSED(dlg);
00197     if (englishMessage=="Design of object \"%1\" has been modified.")
00198         return i18n("Design of query \"%1\" has been modified.");
00199     if (englishMessage=="Object \"%1\" already exists.")
00200         return i18n("Query \"%1\" already exists.");
00201 
00202     return englishMessage;
00203 }
00204 
00205 tristate KexiQueryPart::rename(KexiMainWindow *win, KexiPart::Item &item, const QString& newName)
00206 {
00207     Q_UNUSED(newName);
00208     if (!win->project()->dbConnection())
00209         return false;
00210     win->project()->dbConnection()->setQuerySchemaObsolete( item.name() );
00211     return true;
00212 }
00213 
00214 //----------------
00215 
00216 KexiQueryPart::TempData::TempData(KexiDialogBase* parent, KexiDB::Connection *conn)
00217  : KexiDialogTempData(parent)
00218  , KexiDB::Connection::TableSchemaChangeListenerInterface()
00219  , queryChangedInPreviousView(false)
00220  , m_query(0)
00221 {
00222     this->conn = conn;
00223 }
00224 
00225 KexiQueryPart::TempData::~TempData()
00226 {
00227     conn->unregisterForTablesSchemaChanges(*this);
00228 }
00229 
00230 void KexiQueryPart::TempData::clearQuery()
00231 {
00232     if (!m_query)
00233         return;
00234     unregisterForTablesSchemaChanges();
00235     m_query->clear();
00236 }
00237 
00238 void KexiQueryPart::TempData::unregisterForTablesSchemaChanges()
00239 {
00240     conn->unregisterForTablesSchemaChanges(*this);
00241 }
00242 
00243 void KexiQueryPart::TempData::registerTableSchemaChanges(KexiDB::QuerySchema *q)
00244 {
00245     if (!q)
00246         return;
00247     for (KexiDB::TableSchema::ListIterator it(*q->tables());
00248         it.current(); ++it)
00249     {
00250         conn->registerForTableSchemaChanges(*this, *it.current());
00251     }
00252 }
00253 
00254 tristate KexiQueryPart::TempData::closeListener()
00255 {
00256     KexiDialogBase* dlg = static_cast<KexiDialogBase*>(parent());
00257     return dlg->mainWin()->closeDialog(dlg);
00258 }
00259 
00260 KexiDB::QuerySchema *KexiQueryPart::TempData::takeQuery()
00261 {
00262     KexiDB::QuerySchema *query = m_query;
00263     m_query = 0;
00264     return query;
00265 }
00266 
00267 void KexiQueryPart::TempData::setQuery(KexiDB::QuerySchema *query)
00268 {
00269     if (m_query && m_query == query)
00270         return;
00271     if (m_query
00272         /* query not owned by dialog */
00273         && (static_cast<KexiDialogBase*>(parent())->schemaData() != static_cast<KexiDB::SchemaData*>( m_query )))
00274     {
00275         delete m_query;
00276     }
00277     m_query = query;
00278 }
00279 
00280 //----------------
00281 
00282 #if 0
00283 KexiQueryDataSource::KexiQueryDataSource(KexiPart::Part *part)
00284  : KexiPart::DataSource(part)
00285 {
00286 }
00287 
00288 KexiQueryDataSource::~KexiQueryDataSource()
00289 {
00290 }
00291 
00292 KexiDB::FieldList *
00293 KexiQueryDataSource::fields(KexiProject *, const KexiPart::Item &)
00294 {
00295     return 0;
00296 }
00297 
00298 KexiDB::Cursor *
00299 KexiQueryDataSource::cursor(KexiProject *, const KexiPart::Item &, bool)
00300 {
00301     return 0;
00302 }
00303 #endif
00304 
00305 //----------------
00306 
00307 K_EXPORT_COMPONENT_FACTORY( kexihandler_query, KGenericFactory<KexiQueryPart>("kexihandler_query") )
00308 
00309 #include "kexiquerypart.moc"
00310 
KDE Home | KDE Accessibility Home | Description of Access Keys