summaryrefslogtreecommitdiffstats
path: root/languages/sql/sqloutputwidget.cpp
blob: 0a3756d8f083d0cbd9338c83b4cee8cc7074d08a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/***************************************************************************
 *   Copyright (C) 2003 by Harald Fernengel                                *
 *   harry@tdevelop.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 <tqsqldatabase.h>
#include <tqsqlerror.h>
#include <tqsqlcursor.h>
#include <tqsqldriver.h>
#include <tqsqlrecord.h>
#include <tqwidgetstack.h>
#include <tqdatatable.h>
#include <tqtextedit.h>
#include <tqlayout.h>
#include <tqstylesheet.h>

#include <klocale.h>

#include "sqloutputwidget.h"

class TQCustomSqlCursor: public TQSqlCursor
{
public:
    TQCustomSqlCursor( const TQString & query = TQString(), bool autopopulate = TRUE, TQSqlDatabase* db = 0 ) :
                TQSqlCursor( TQString(), autopopulate, db )
    {
        exec( query );
        if ( isSelect() && autopopulate ) {
            TQSqlRecordInfo inf = ((TQSqlQuery*)this)->driver()->recordInfo( *(TQSqlQuery*)this );
            for ( TQSqlRecordInfo::iterator it = inf.begin(); it != inf.end(); ++it ) {
                append( *it );
            }
        }
        setMode( TQSqlCursor::ReadOnly );
    }
    TQCustomSqlCursor( const TQCustomSqlCursor & other ): TQSqlCursor( other ) {}
    bool select( const TQString & /*filter*/, const TQSqlIndex & /*sort*/ = TQSqlIndex() )
        { return exec( lastQuery() ); }
    TQSqlIndex primaryIndex( bool /*prime*/ = TRUE ) const
        { return TQSqlIndex(); }
    int insert( bool /*invalidate*/ = TRUE )
        { return FALSE; }
    int update( bool /*invalidate*/ = TRUE )
        { return FALSE; }
    int del( bool /*invalidate*/ = TRUE )
        { return FALSE; }
    void setName( const TQString& /*name*/, bool /*autopopulate*/ = TRUE ) {}
};


SqlOutputWidget::SqlOutputWidget ( TQWidget* parent, const char* name ) :
    TQWidget( parent, name )
{
    m_stack = new TQWidgetStack( this );
    m_table = new TQDataTable( this );
    m_textEdit = new TQTextEdit( this );

    m_textEdit->setTextFormat( TQTextEdit::RichText );
    m_textEdit->setReadOnly( true );
    
    m_stack->addWidget( m_textEdit );
    m_stack->addWidget( m_table );

    TQVBoxLayout* layout = new TQVBoxLayout( this );
    layout->addWidget( m_stack );
}

SqlOutputWidget::~SqlOutputWidget()
{}

void SqlOutputWidget::showQuery( const TQString& connectionName, const TQString& query )
{
    TQSqlDatabase* db = TQSqlDatabase::database( connectionName, true );
    if ( !db ) {
        showError( i18n("No such connection: %1").arg( connectionName ) );
        return;
    }
    if ( !db->isOpen() ) {
        showError( db->lastError() );
        return;
    }

    TQSqlCursor* cur = new TQCustomSqlCursor( query, true, db );
    if ( !cur->isActive() ) {
        showError( cur->lastError() );
    } else if ( cur->isSelect() ) {
        m_table->setSqlCursor( cur, true, true );
        m_table->refresh( TQDataTable::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 TQString& message )
{
    m_textEdit->clear();
    m_textEdit->setText( "<p><b>" + i18n("An error occurred:") + "</b></p>\n" + message );
    m_stack->raiseWidget( m_textEdit );
}

void SqlOutputWidget::showError( const TQSqlError& message )
{
    m_textEdit->clear();
    m_textEdit->setText( "<p><b>" + i18n("An error occurred:") + 
                         "</b></p>\n<p><i>" + i18n("Driver") + "</i>: " + 
                         TQStyleSheet::escape( message.driverText() ) + 
                         "<br><i>" + i18n("Database") + ":</i>: " +
                         TQStyleSheet::escape( message.databaseText() ) );
    m_stack->raiseWidget( m_textEdit );
}

#include "sqloutputwidget.moc"