| 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 <qsqlcursor.h>
|
|---|
| 16 | #include <qsqlselectcursor.h>
|
|---|
| 17 | #include <qdatatable.h>
|
|---|
| 18 | #include "connect.h"
|
|---|
| 19 |
|
|---|
| 20 | static void showError( const QSqlError& err, QWidget* parent = 0 )
|
|---|
| 21 | {
|
|---|
| 22 | QString errStr ( "The database reported an error\n" );
|
|---|
| 23 | if ( !err.databaseText().isEmpty() )
|
|---|
| 24 | errStr += err.databaseText();
|
|---|
| 25 | if ( !err.driverText().isEmpty() )
|
|---|
| 26 | errStr += err.driverText();
|
|---|
| 27 | QMessageBox::warning( parent, "Error", errStr );
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | ConnectDialog* conDiag = 0;
|
|---|
| 31 |
|
|---|
| 32 | void SqlEx::init()
|
|---|
| 33 | {
|
|---|
| 34 | hsplit->setResizeMode( lv, QSplitter::KeepSize );
|
|---|
| 35 | vsplit->setResizeMode( gb, QSplitter::KeepSize );
|
|---|
| 36 | submitBtn->setEnabled( FALSE );
|
|---|
| 37 | conDiag = new ConnectDialog( this, "Connection Dialog", TRUE );
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | void SqlEx::dbConnect()
|
|---|
| 41 | {
|
|---|
| 42 | if ( conDiag->exec() != QDialog::Accepted )
|
|---|
| 43 | return;
|
|---|
| 44 | if ( dt->sqlCursor() ) {
|
|---|
| 45 | dt->setSqlCursor( 0 );
|
|---|
| 46 | }
|
|---|
| 47 | // close old connection (if any)
|
|---|
| 48 | if ( QSqlDatabase::contains( "SqlEx" ) ) {
|
|---|
| 49 | QSqlDatabase* oldDb = QSqlDatabase::database( "SqlEx" );
|
|---|
| 50 | oldDb->close();
|
|---|
| 51 | QSqlDatabase::removeDatabase( "SqlEx" );
|
|---|
| 52 | }
|
|---|
| 53 | // open the new connection
|
|---|
| 54 | QSqlDatabase* db = QSqlDatabase::addDatabase( conDiag->comboDriver->currentText(), "SqlEx" );
|
|---|
| 55 | if ( !db ) {
|
|---|
| 56 | QMessageBox::warning( this, "Error", "Could not open database" );
|
|---|
| 57 | return;
|
|---|
| 58 | }
|
|---|
| 59 | db->setHostName( conDiag->editHostname->text() );
|
|---|
| 60 | db->setDatabaseName( conDiag->editDatabase->text() );
|
|---|
| 61 | db->setPort( conDiag->portSpinBox->value() );
|
|---|
| 62 | if ( !db->open( conDiag->editUsername->text(), conDiag->editPassword->text() ) ) {
|
|---|
| 63 | showError( db->lastError(), this );
|
|---|
| 64 | return;
|
|---|
| 65 | }
|
|---|
| 66 | lbl->setText( "Double-Click on a table-name to view the contents" );
|
|---|
| 67 | lv->clear();
|
|---|
| 68 |
|
|---|
| 69 | QStringList tables = db->tables();
|
|---|
| 70 | for ( QStringList::Iterator it = tables.begin(); it != tables.end(); ++it ) {
|
|---|
| 71 | QListViewItem* lvi = new QListViewItem( lv, *it );
|
|---|
| 72 | QSqlRecordInfo ri = db->recordInfo ( *it );
|
|---|
| 73 | for ( QSqlRecordInfo::Iterator it = ri.begin(); it != ri.end(); ++it ) {
|
|---|
| 74 | QString req;
|
|---|
| 75 | if ( (*it).isRequired() > 0 ) {
|
|---|
| 76 | req = "Yes";
|
|---|
| 77 | } else if ( (*it).isRequired() == 0 ) {
|
|---|
| 78 | req = "No";
|
|---|
| 79 | } else {
|
|---|
| 80 | req = "?";
|
|---|
| 81 | }
|
|---|
| 82 | QListViewItem* fi = new QListViewItem( lvi, (*it).name(), + QVariant::typeToName( (*it).type() ), req );
|
|---|
| 83 | lvi->insertItem( fi );
|
|---|
| 84 | }
|
|---|
| 85 | lv->insertItem( lvi );
|
|---|
| 86 | }
|
|---|
| 87 | submitBtn->setEnabled( TRUE );
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | void SqlEx::execQuery()
|
|---|
| 91 | {
|
|---|
| 92 | // use a custom cursor to populate the data table
|
|---|
| 93 | QSqlSelectCursor* cursor = new QSqlSelectCursor( te->text(), QSqlDatabase::database( "SqlEx", TRUE ) );
|
|---|
| 94 | if ( cursor->isSelect() ) {
|
|---|
| 95 | dt->setSqlCursor( cursor, TRUE, TRUE );
|
|---|
| 96 | dt->setSort( QStringList() );
|
|---|
| 97 | dt->refresh( QDataTable::RefreshAll );
|
|---|
| 98 | QString txt( "Query OK" );
|
|---|
| 99 | if ( cursor->size() >= 0 )
|
|---|
| 100 | txt += ", returned rows: " + QString::number( cursor->size() );
|
|---|
| 101 | lbl->setText( txt );
|
|---|
| 102 | } else {
|
|---|
| 103 | // an error occured if the cursor is not active
|
|---|
| 104 | if ( !cursor->isActive() ) {
|
|---|
| 105 | showError( cursor->lastError(), this );
|
|---|
| 106 | } else {
|
|---|
| 107 | lbl->setText( QString("Query OK, affected rows: %1").arg( cursor->numRowsAffected() ) );
|
|---|
| 108 | }
|
|---|
| 109 | }
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | void SqlEx::showTable( QListViewItem * item )
|
|---|
| 113 | {
|
|---|
| 114 | // get the table name
|
|---|
| 115 | QListViewItem* i = item->parent();
|
|---|
| 116 | if ( !i ) {
|
|---|
| 117 | i = item;
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | // populate the data table
|
|---|
| 121 | QSqlCursor* cursor = new QSqlCursor( i->text( 0 ), TRUE, QSqlDatabase::database( "SqlEx", TRUE ) );
|
|---|
| 122 | dt->setSqlCursor( cursor, TRUE, TRUE );
|
|---|
| 123 | dt->setSort( cursor->primaryIndex() );
|
|---|
| 124 | dt->refresh( QDataTable::RefreshAll );
|
|---|
| 125 | lbl->setText( "Displaying table " + i->text( 0 ) );
|
|---|
| 126 | }
|
|---|