From 114a878c64ce6f8223cfd22d76a20eb16d177e5e Mon Sep 17 00:00:00 2001 From: toma Date: Wed, 25 Nov 2009 17:56:58 +0000 Subject: Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features. BUG:215923 git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdevelop@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- languages/sql/sqloutputwidget.cpp | 127 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 languages/sql/sqloutputwidget.cpp (limited to 'languages/sql/sqloutputwidget.cpp') diff --git a/languages/sql/sqloutputwidget.cpp b/languages/sql/sqloutputwidget.cpp new file mode 100644 index 00000000..a0d7368d --- /dev/null +++ b/languages/sql/sqloutputwidget.cpp @@ -0,0 +1,127 @@ +/*************************************************************************** + * Copyright (C) 2003 by Harald Fernengel * + * harry@kdevelop.org * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "sqloutputwidget.h" + +class QCustomSqlCursor: public QSqlCursor +{ +public: + QCustomSqlCursor( const QString & query = QString::null, bool autopopulate = TRUE, QSqlDatabase* db = 0 ) : + QSqlCursor( QString::null, autopopulate, db ) + { + exec( query ); + if ( isSelect() && autopopulate ) { + QSqlRecordInfo inf = ((QSqlQuery*)this)->driver()->recordInfo( *(QSqlQuery*)this ); + for ( QSqlRecordInfo::iterator it = inf.begin(); it != inf.end(); ++it ) { + append( *it ); + } + } + setMode( QSqlCursor::ReadOnly ); + } + QCustomSqlCursor( const QCustomSqlCursor & other ): QSqlCursor( other ) {} + bool select( const QString & /*filter*/, const QSqlIndex & /*sort*/ = QSqlIndex() ) + { return exec( lastQuery() ); } + QSqlIndex primaryIndex( bool /*prime*/ = TRUE ) const + { return QSqlIndex(); } + int insert( bool /*invalidate*/ = TRUE ) + { return FALSE; } + int update( bool /*invalidate*/ = TRUE ) + { return FALSE; } + int del( bool /*invalidate*/ = TRUE ) + { return FALSE; } + void setName( const QString& /*name*/, bool /*autopopulate*/ = TRUE ) {} +}; + + +SqlOutputWidget::SqlOutputWidget ( QWidget* parent, const char* name ) : + QWidget( parent, name ) +{ + m_stack = new QWidgetStack( this ); + m_table = new QDataTable( this ); + m_textEdit = new QTextEdit( this ); + + m_textEdit->setTextFormat( QTextEdit::RichText ); + m_textEdit->setReadOnly( true ); + + m_stack->addWidget( m_textEdit ); + m_stack->addWidget( m_table ); + + QVBoxLayout* layout = new QVBoxLayout( this ); + layout->addWidget( m_stack ); +} + +SqlOutputWidget::~SqlOutputWidget() +{} + +void SqlOutputWidget::showQuery( const QString& connectionName, const QString& query ) +{ + QSqlDatabase* db = QSqlDatabase::database( connectionName, true ); + if ( !db ) { + showError( i18n("No such connection: %1").arg( connectionName ) ); + return; + } + if ( !db->isOpen() ) { + showError( db->lastError() ); + return; + } + + QSqlCursor* cur = new QCustomSqlCursor( query, true, db ); + if ( !cur->isActive() ) { + showError( cur->lastError() ); + } else if ( cur->isSelect() ) { + m_table->setSqlCursor( cur, true, true ); + m_table->refresh( QDataTable::RefreshAll ); + m_stack->raiseWidget( m_table ); + } else { + showSuccess( cur->numRowsAffected() ); + } +} + +void SqlOutputWidget::showSuccess( int rowsAffected ) +{ + m_textEdit->clear(); + m_textEdit->setText( i18n("Query successful, number of rows affected: %1").arg( rowsAffected ) ); + m_stack->raiseWidget( m_textEdit ); +} + +void SqlOutputWidget::showError( const QString& message ) +{ + m_textEdit->clear(); + m_textEdit->setText( "

" + i18n("An error occurred:") + "

\n" + message ); + m_stack->raiseWidget( m_textEdit ); +} + +void SqlOutputWidget::showError( const QSqlError& message ) +{ + m_textEdit->clear(); + m_textEdit->setText( "

" + i18n("An error occurred:") + + "

\n

" + i18n("Driver") + ": " + + QStyleSheet::escape( message.driverText() ) + + "
" + i18n("Database") + ":: " + + QStyleSheet::escape( message.databaseText() ) ); + m_stack->raiseWidget( m_textEdit ); +} + +#include "sqloutputwidget.moc" + -- cgit v1.2.3