#include "sqlsupport_part.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "kdevcore.h" #include "kdevmainwindow.h" #include "kdevlanguagesupport.h" #include "kdevpartcontroller.h" #include "kdevproject.h" #include "codemodel.h" #include "kdevplugininfo.h" #include "sqlconfigwidget.h" #include "sqlactions.h" #include "sqloutputwidget.h" #include "domutil.h" typedef KDevGenericFactory STQLSupportFactory; static const KDevPluginInfo data("kdevsqlsupport"); K_EXPORT_COMPONENT_FACTORY( libkdevsqlsupport, STQLSupportFactory( data ) ) STQLSupportPart::STQLSupportPart( TQObject *tqparent, const char *name, const TQStringList& ) : KDevLanguageSupport ( &data, tqparent, name ? name : "STQLSupportPart" ) { setInstance( STQLSupportFactory::instance() ); setXMLFile( "kdevsqlsupport.rc" ); KAction *action; action = new KAction( i18n( "&Run" ), "exec", Key_F9, this, TQT_SLOT( slotRun() ), actionCollection(), "build_execute" ); action->setToolTip(i18n("Run")); action->setWhatsThis(i18n("Run

Executes a SQL script.")); dbAction = new SqlListAction( this, i18n( "&Database Connections" ), 0, this, TQT_SLOT(activeConnectionChanged()), actionCollection(), "connection_combo" ); kdDebug( 9000 ) << "Creating STQLSupportPart" << endl; connect( core(), TQT_SIGNAL( projectConfigWidget( KDialogBase* ) ), this, TQT_SLOT( projectConfigWidget( KDialogBase* ) ) ); connect( core(), TQT_SIGNAL(projectOpened()), this, TQT_SLOT(projectOpened()) ); connect( core(), TQT_SIGNAL(projectClosed()), this, TQT_SLOT(projectClosed()) ); connect( core(), TQT_SIGNAL(languageChanged()), this, TQT_SLOT(projectOpened()) ); connect( partController(), TQT_SIGNAL( savedFile( const KURL& ) ), this, TQT_SLOT( savedFile( const KURL& ) ) ); m_widget = new SqlOutputWidget(); mainWindow()->embedOutputView( m_widget, i18n( "SQL" ), i18n( "Output of SQL commands" ) ); TQWhatsThis::add(m_widget, i18n("Output of SQL commands

This window shows the output of SQL commands being executed. It can display results of SQL \"select\" commands in a table.")); } STQLSupportPart::~STQLSupportPart() { mainWindow()->removeView(m_widget); delete m_widget; } TQString STQLSupportPart::cryptStr(const TQString& aStr) { TQString result; for (unsigned int i = 0; i < aStr.length(); i++) result += (aStr[i].tqunicode() < 0x20) ? aStr[i] : TQChar(0x1001F - aStr[i].tqunicode()); return result; } void STQLSupportPart::activeConnectionChanged() { updateCatalog(); } void STQLSupportPart::clearConfig() { for ( TQStringList::Iterator it = conNames.begin(); it != conNames.end(); ++it ) { if ( TQSqlDatabase::tqcontains( *it ) ) { TQSqlDatabase::database( *it, false )->close(); TQSqlDatabase::removeDatabase( *it ); } else { kdDebug( 9000 ) << "Could not find connection named " << (*it) << endl; } } conNames.clear(); dbAction->refresh(); } void STQLSupportPart::loadConfig() { clearConfig(); TQDomDocument* doc = projectDom(); TQStringList db; int i = 0; TQString conName; while ( true ) { TQStringList sdb = DomUtil::readListEntry( *doc, "kdevsqlsupport/servers/server" + TQString::number( i ), "el" ); if ( (int)sdb.size() < 6 ) break; conName = "KDEVSTQLSUPPORT_"; conName += TQString::number( i ); conNames << conName; TQSqlDatabase* db = TQSqlDatabase::addDatabase( sdb[0], TQString( "KDEVSTQLSUPPORT_%1" ).tqarg( i ) ); db->setDatabaseName( sdb[1] ); db->setHostName( sdb[2] ); bool ok; int port = sdb[3].toInt( &ok ); if ( ok ) db->setPort( port ); db->setUserName( sdb[4] ); db->setPassword( cryptStr( sdb[5] ) ); db->open(); i++; } dbAction->refresh(); } void STQLSupportPart::projectConfigWidget( KDialogBase *dlg ) { TQVBox *vbox = dlg->addVBoxPage( TQString( "SQL" ), i18n( "Specify Your Database Connections" ), BarIcon("source", KIcon::SizeMedium) ); SqlConfigWidget *w = new SqlConfigWidget( (TQWidget*)vbox, "SQL config widget" ); w->setProjectDom( projectDom() ); w->loadConfig(); connect( dlg, TQT_SIGNAL(okClicked()), w, TQT_SLOT(accept()) ); connect( w, TQT_SIGNAL(newConfigSaved()), this, TQT_SLOT(loadConfig()) ); } void STQLSupportPart::projectOpened() { connect( project(), TQT_SIGNAL( addedFilesToProject( const TQStringList & ) ), this, TQT_SLOT( addedFilesToProject( const TQStringList & ) ) ); connect( project(), TQT_SIGNAL( removedFilesFromProject( const TQStringList & ) ), this, TQT_SLOT( removedFilesFromProject( const TQStringList & ) ) ); loadConfig(); // We want to parse only after all components have been // properly initialized TQTimer::singleShot( 0, this, TQT_SLOT( parse() ) ); } void STQLSupportPart::projectClosed() { clearConfig(); } void STQLSupportPart::slotRun () { TQString cName = dbAction->currentConnectionName(); if ( cName.isEmpty() ) { KMessageBox::sorry( 0, i18n("Please select a valid database connection.") ); return; } KTextEditor::EditInterface* doc = dynamic_cast(partController()->activePart()); if ( !doc ) return; // show error message? mainWindow()->raiseView( m_widget ); m_widget->showQuery( cName, doc->text() ); } #if 0 static TQString dbCaption(const TQSqlDatabase* db) { TQString res; if (!db) return res; res = db->driverName(); res += TQString::tqfromLatin1("@"); res += db->hostName(); if (db->port() >= 0) res += TQString::tqfromLatin1(":") + TQString::number(db->port()); return res; } #endif void STQLSupportPart::parse() { // @todo } void STQLSupportPart::updateCatalog() { if (!project() || !dbAction) return; codeModel()->wipeout(); TQString curConnection = dbAction->currentConnectionName(); if (curConnection.isEmpty()) { emit updatedSourceInfo(); return; } FileDom dbf = codeModel()->create(); dbf->setName(dbAction->currentConnectionName()); TQSqlDatabase *db = TQSqlDatabase::database(dbAction->currentConnectionName(), true); // tables are classes and fields are methods if (db->isOpen()) { TQSqlRecord inf; TQStringList tables = db->tables(); for (TQStringList::Iterator it = tables.begin(); it != tables.end(); ++it) { ClassDom dbc = codeModel()->create(); dbc->setName(*it); inf = db->record(*it); for (int i = 0; i < (int)inf.count(); ++i) { FunctionDom dbv = codeModel()->create(); dbv->setName(inf.fieldName(i)); dbv->setResultType(TQVariant::typeToName(inf.field(i)->type())); dbc->addFunction(dbv); } dbf->addClass(dbc); } } codeModel()->addFile(dbf); emit updatedSourceInfo(); } void STQLSupportPart::addedFilesToProject( const TQStringList &fileList ) { TQStringList::ConstIterator it; for ( it = fileList.begin(); it != fileList.end(); ++it ) { // parse( project() ->projectDirectory() + "/" + ( *it ) ); } emit updatedSourceInfo(); } void STQLSupportPart::removedFilesFromProject( const TQStringList &fileList ) { TQStringList::ConstIterator it; for ( it = fileList.begin(); it != fileList.end(); ++it ) { // classStore() ->removeWithReferences( project() ->projectDirectory() + "/" + ( *it ) ); } emit updatedSourceInfo(); } void STQLSupportPart::savedFile( const KURL &fileName ) { if ( project() ->allFiles().tqcontains( fileName.path().mid ( project() ->projectDirectory().length() + 1 ) ) ) { // parse( fileName ); // emit updatedSourceInfo(); } } KDevLanguageSupport::Features STQLSupportPart::features() { return Features( Classes | Functions ); } KMimeType::List STQLSupportPart::mimeTypes( ) { KMimeType::List list; KMimeType::Ptr mime = KMimeType::mimeType( "text/plain" ); if( mime ) list << mime; return list; } #include "sqlsupport_part.moc"