sqloutputwidget.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <qsqldatabase.h>
00013 #include <qsqlerror.h>
00014 #include <qsqlcursor.h>
00015 #include <qsqldriver.h>
00016 #include <qsqlrecord.h>
00017 #include <qwidgetstack.h>
00018 #include <qdatatable.h>
00019 #include <qtextedit.h>
00020 #include <qlayout.h>
00021 #include <qstylesheet.h>
00022
00023 #include <klocale.h>
00024
00025 #include "sqloutputwidget.h"
00026
00027 class QCustomSqlCursor: public QSqlCursor
00028 {
00029 public:
00030 QCustomSqlCursor( const QString & query = QString::null, bool autopopulate = TRUE, QSqlDatabase* db = 0 ) :
00031 QSqlCursor( QString::null, autopopulate, db )
00032 {
00033 exec( query );
00034 if ( isSelect() && autopopulate ) {
00035 QSqlRecordInfo inf = ((QSqlQuery*)this)->driver()->recordInfo( *(QSqlQuery*)this );
00036 for ( QSqlRecordInfo::iterator it = inf.begin(); it != inf.end(); ++it ) {
00037 append( *it );
00038 }
00039 }
00040 setMode( QSqlCursor::ReadOnly );
00041 }
00042 QCustomSqlCursor( const QCustomSqlCursor & other ): QSqlCursor( other ) {}
00043 bool select( const QString & , const QSqlIndex & = QSqlIndex() )
00044 { return exec( lastQuery() ); }
00045 QSqlIndex primaryIndex( bool = TRUE ) const
00046 { return QSqlIndex(); }
00047 int insert( bool = TRUE )
00048 { return FALSE; }
00049 int update( bool = TRUE )
00050 { return FALSE; }
00051 int del( bool = TRUE )
00052 { return FALSE; }
00053 void setName( const QString& , bool = TRUE ) {}
00054 };
00055
00056
00057 SqlOutputWidget::SqlOutputWidget ( QWidget* parent, const char* name ) :
00058 QWidget( parent, name )
00059 {
00060 m_stack = new QWidgetStack( this );
00061 m_table = new QDataTable( this );
00062 m_textEdit = new QTextEdit( this );
00063
00064 m_textEdit->setTextFormat( QTextEdit::RichText );
00065 m_textEdit->setReadOnly( true );
00066
00067 m_stack->addWidget( m_textEdit );
00068 m_stack->addWidget( m_table );
00069
00070 QVBoxLayout* layout = new QVBoxLayout( this );
00071 layout->addWidget( m_stack );
00072 }
00073
00074 SqlOutputWidget::~SqlOutputWidget()
00075 {}
00076
00077 void SqlOutputWidget::showQuery( const QString& connectionName, const QString& query )
00078 {
00079 QSqlDatabase* db = QSqlDatabase::database( connectionName, true );
00080 if ( !db ) {
00081 showError( i18n("No such connection: %1").arg( connectionName ) );
00082 return;
00083 }
00084 if ( !db->isOpen() ) {
00085 showError( db->lastError() );
00086 return;
00087 }
00088
00089 QSqlCursor* cur = new QCustomSqlCursor( query, true, db );
00090 if ( !cur->isActive() ) {
00091 showError( cur->lastError() );
00092 } else if ( cur->isSelect() ) {
00093 m_table->setSqlCursor( cur, true, true );
00094 m_table->refresh( QDataTable::RefreshAll );
00095 m_stack->raiseWidget( m_table );
00096 } else {
00097 showSuccess( cur->numRowsAffected() );
00098 }
00099 }
00100
00101 void SqlOutputWidget::showSuccess( int rowsAffected )
00102 {
00103 m_textEdit->clear();
00104 m_textEdit->setText( i18n("Query successful, number of rows affected: %1").arg( rowsAffected ) );
00105 m_stack->raiseWidget( m_textEdit );
00106 }
00107
00108 void SqlOutputWidget::showError( const QString& message )
00109 {
00110 m_textEdit->clear();
00111 m_textEdit->setText( "<p><b>" + i18n("An error occurred:") + "</b></p>\n" + message );
00112 m_stack->raiseWidget( m_textEdit );
00113 }
00114
00115 void SqlOutputWidget::showError( const QSqlError& message )
00116 {
00117 m_textEdit->clear();
00118 m_textEdit->setText( "<p><b>" + i18n("An error occurred:") +
00119 "</b></p>\n<p><i>" + i18n("Driver") + "</i>: " +
00120 QStyleSheet::escape( message.driverText() ) +
00121 "<br><i>" + i18n("Database") + ":</i>: " +
00122 QStyleSheet::escape( message.databaseText() ) );
00123 m_stack->raiseWidget( m_textEdit );
00124 }
00125
00126 #include "sqloutputwidget.moc"
00127
This file is part of the documentation for KDevelop Version 3.1.2.