home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / qt3_emx.zip / examples / demo / sql / sqlex.ui.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-09-17  |  4.7 KB  |  143 lines

  1. /****************************************************************************
  2. ** ui.h extension file, included from the uic-generated form implementation.
  3. **
  4. ** If you wish to add, delete or rename slots use Qt Designer which will
  5. ** update this file, preserving your code. Create an init() slot in place of
  6. ** a constructor, and a destroy() slot in place of a destructor.
  7. *****************************************************************************/
  8. #include <qsqldriver.h>
  9. #include <qmessagebox.h>
  10. #include <qsqldatabase.h>
  11. #include <qlineedit.h>
  12. #include <qcombobox.h>
  13. #include <qspinbox.h>
  14. #include <qsqlerror.h>
  15. #include "connect.h"
  16.  
  17. class QCustomSqlCursor: public QSqlCursor
  18. {
  19. public:
  20.     QCustomSqlCursor( const QString & query = QString::null, bool autopopulate = TRUE, QSqlDatabase* db = 0 ) :
  21.         QSqlCursor( QString::null, autopopulate, db )
  22.     {
  23.     exec( query );
  24.     if ( isSelect() && autopopulate ) {
  25.         QSqlRecordInfo inf = ((QSqlQuery*)this)->driver()->recordInfo( *(QSqlQuery*)this );
  26.         for ( QSqlRecordInfo::iterator it = inf.begin(); it != inf.end(); ++it ) {
  27.         append( *it );
  28.         }        
  29.     }
  30.     setMode( QSqlCursor::ReadOnly );
  31.     }
  32.     QCustomSqlCursor( const QCustomSqlCursor & other ): QSqlCursor( other ) {}
  33.     bool select( const QString & /*filter*/, const QSqlIndex & /*sort*/ = QSqlIndex() )
  34.     { return exec( lastQuery() ); }
  35.     QSqlIndex primaryIndex( bool /*prime*/ = TRUE ) const
  36.     { return QSqlIndex(); }
  37.     int insert( bool /*invalidate*/ = TRUE )
  38.     { return FALSE; }
  39.     int update( bool /*invalidate*/ = TRUE )
  40.     { return FALSE; }
  41.     int del( bool /*invalidate*/ = TRUE )
  42.     { return FALSE; }
  43.     void setName( const QString& /*name*/, bool /*autopopulate*/ = TRUE ) {}
  44. };
  45.  
  46. static void showError( const QSqlError& err, QWidget* parent = 0 )
  47. {
  48.    QString errStr ( "The database reported an error\n" );
  49.     if ( !err.databaseText().isEmpty() )
  50.     errStr += err.databaseText();
  51.     if ( !err.driverText().isEmpty() )
  52.     errStr += err.driverText();
  53.     QMessageBox::warning( parent, "Error", errStr );
  54. }
  55. void SqlEx::init()
  56. {
  57.     hsplit->setResizeMode( lv, QSplitter::KeepSize );
  58.     vsplit->setResizeMode( gb, QSplitter::KeepSize );
  59.     submitBtn->setEnabled( FALSE );
  60. }
  61.  
  62. void SqlEx::dbConnect()
  63.     ConnectDialog* conDiag = new ConnectDialog( this, "Connection Dialog", TRUE );
  64.     if ( conDiag->exec() != QDialog::Accepted )
  65.     return;
  66.     if ( dt->sqlCursor() ) {
  67.     dt->setSqlCursor( 0 );
  68.     }
  69.     QSqlDatabase* db = QSqlDatabase::addDatabase( conDiag->comboDriver->currentText(), "SqlEx" );
  70.     if ( !db ) {
  71.     QMessageBox::warning( this, "Error", "Could not open database" );    
  72.     return;
  73.     }
  74.     db->setHostName( conDiag->editHostname->text() );
  75.     db->setDatabaseName( conDiag->editDatabase->text() );
  76.     db->setPort( conDiag->portSpinBox->value() );
  77.     if ( !db->open( conDiag->editUsername->text(), conDiag->editPassword->text() ) ) {
  78.     showError( db->lastError(), this );
  79.     return;
  80.     }
  81.     lbl->setText( "Double-Click on a table-name to view the contents" );
  82.     lv->clear();
  83.     
  84.     QStringList tables = db->tables();
  85.     for ( QStringList::Iterator it = tables.begin(); it != tables.end(); ++it ) {
  86.     QListViewItem* lvi = new QListViewItem( lv, *it );
  87.     QSqlRecordInfo ri = db->recordInfo ( *it );
  88.     for ( QSqlRecordInfo::Iterator it = ri.begin(); it != ri.end(); ++it ) {
  89.         QString req;
  90.         if ( (*it).isRequired() > 0 ) {
  91.         req = "Yes";
  92.         } else if ( (*it).isRequired() == 0 ) {
  93.         req = "No";
  94.         } else {
  95.         req = "?";
  96.         }
  97.         QListViewItem* fi = new QListViewItem( lvi, (*it).name(),  + QVariant::typeToName( (*it).type() ), req );
  98.         lvi->insertItem( fi );
  99.     }
  100.     lv->insertItem( lvi );    
  101.     }
  102.     submitBtn->setEnabled( TRUE );
  103. }
  104.  
  105. void SqlEx::execQuery()
  106. {
  107.     // use a custom cursor to populate the data table
  108.     QCustomSqlCursor* cursor = new QCustomSqlCursor( te->text(), TRUE, QSqlDatabase::database( "SqlEx", TRUE ) );
  109.     if ( cursor->isSelect() ) {
  110.     dt->setSqlCursor( cursor, TRUE, TRUE );
  111.     dt->refresh( QDataTable::RefreshAll );
  112.     QString txt( "Query OK" );
  113.     if ( cursor->size() >= 0 )
  114.         txt += ", returned rows: " + QString::number( cursor->size() );
  115.     lbl->setText( txt );
  116.     } else {
  117.     // an error occured if the cursor is not active
  118.     if ( !cursor->isActive() ) {
  119.         showError( cursor->lastError(), this );
  120.     } else {
  121.         lbl->setText( QString("Query OK, affected rows: %1").arg( cursor->numRowsAffected() ) );
  122.     }
  123.     }
  124. }
  125.  
  126. void SqlEx::showTable( QListViewItem * item )
  127. {
  128.     // get the table name
  129.     QListViewItem* i = item->parent();
  130.     if ( !i ) {
  131.     i = item;
  132.     }
  133.  
  134.     // populate the data table
  135.     QSqlCursor* cursor = new QSqlCursor( i->text( 0 ), TRUE, QSqlDatabase::database( "SqlEx", TRUE ) );
  136.     dt->setSqlCursor( cursor, TRUE, TRUE );
  137.     dt->setSort( cursor->primaryIndex() );
  138.     dt->refresh( QDataTable::RefreshAll );
  139.     lbl->setText( "Displaying table " + i->text( 0 ) );
  140. }
  141.  
  142.