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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
|
/**************************************************
*
*
**************************************************
* This code was created by Peter Harvey @ CodeByDesign.
* Released under GPL 30.NOV.00
*
* Contributions from...
* -----------------------------------------------
* Peter Harvey - pharvey@codebydesign.com
**************************************************/
#include "classBrowse.h"
#include "classODBC.h"
#include <qheader.h>
#include <qwhatsthis.h>
#include <qfiledialog.h>
#include <qvaluelist.h>
#include <qlayout.h>
#include <qpixmap.h>
#include "LinuxODBC.xpm"
#define MAX_ROWS_IN_TABLE 25000 // give a resonable number back
// WhatsThis help strings
char *szHelpQuery = \
"Use this area to quickly specify a simple data filter.\n\n" \
"1. Leave the Expression cells blank to get all data in the table or enter filter Expression(s) to limit the amount of data returned. Expression examples; \n='MyText'\n>23\n" \
"2. Enter (Y)es in the Show cells to get data in that column.\n" \
"3. Enter (A)scending or (D)escending in the Sort cells to get sorted column data.\n"\
"4. Control-Click and drag column headers to rearrange the column order.\n" \
"5. Click the Run button to get your results.\n" \
"NOTE: The max number of rows returned is 25000 and \n" \
"the max column size is 1024." ;
char *szHelpData = \
"This area contains the results from your Query. You can:\n\n" \
"1. Browse the data.\n" \
"2. Export the data in HTML format\n" \
"3. Delete rows of existing data\n" \
"4. Insert new rows of data\n" \
"5. Add a blank row for inserting later\n" \
"6. Copy an existing row for inserting later\n\n" \
"NOTE: The insert and delete operatoins are performed with\n" \
"the current columns shown in the result table\n" ;
char *szHelpStatus2 = \
"This area contains status messages.\n" ;
classBrowse::classBrowse( SQLHDBC hDbc, const QString& qsTable, const QString& qsLibrary, QWidget *parent, const char *name )
: QWidget( parent, name, 0 ) , hDbc( hDbc ) , qsTable( qsTable ) , qsLibrary( qsLibrary ) , bAutoRefresh( true )
{
// VERTICAL LAYOUT
QVBoxLayout *layoutMain = new QVBoxLayout( this );
// FILTER LABEL
labelFilter = new QLabel( "FILTER" , this );
labelFilter->setPalette( QPalette( QColor(lightGray) ) );
labelFilter->setAlignment( AlignCenter );
// FILTER TABLE
tableQuery = new QTable( 3, 1, this );
tableQuery->setColumnMovingEnabled ( TRUE );
tableQuery->setMinimumSize( 78, 105 );
tableQuery->setLeftMargin( 84 ) ;
QHeader * headerQuery = tableQuery->verticalHeader();
headerQuery->setLabel( 0, QString( tr("Expression") ));
headerQuery->setLabel( 1, QString( tr("Show" ) ));
headerQuery->setLabel( 2, QString( tr("Sort" ) ));
// DATA LABEL
labelResults = new QLabel( "RESULTS", this );
labelResults->setPalette( QPalette( QColor(lightGray) ) );
labelResults->setAlignment( AlignCenter );
// DATA TABLE
tableData = new myQTable( 0, 0, this );
tableData->setSorting( TRUE ) ;
tableData->setLeftMargin( 60 ) ;
// STATUS LABEL
QLabel *labelStatus = new QLabel( "STATUS", this );
labelStatus->setAlignment( AlignCenter );
// STATUS LISTBOX
listStatus = new QListBox( this );
listStatus->setSelectionMode( QListBox::NoSelection );
// LAYOUT
layoutMain->addWidget( labelFilter );
layoutMain->addWidget( tableQuery );
layoutMain->addWidget( labelResults );
layoutMain->addWidget( tableData, 6 );
layoutMain->addWidget( labelStatus );
layoutMain->addWidget( listStatus, 2);
InitQuery();
setMinimumSize( 50, 50 );
setMaximumSize( 32767, 32767 );
QWhatsThis::add( tableQuery, szHelpQuery );
QWhatsThis::add( labelFilter, szHelpQuery );
QWhatsThis::add( tableData, szHelpData );
QWhatsThis::add( labelResults, szHelpData );
QWhatsThis::add( labelStatus, szHelpStatus2 );
QWhatsThis::add( listStatus, szHelpStatus2 );
}
void classBrowse::addStatus( const QString &statusMsg )
{
if ( listStatus->count() == 300 )
listStatus->removeItem( 0 ) ;
listStatus->insertItem( statusMsg ) ;
listStatus->setBottomItem( listStatus->count()-1 ) ;
}
int classBrowse::GetSelectedRows( QString & qsRowsSelected, ListRows & listRows )
{
listRows.clear() ;
qsRowsSelected = "" ;
int nRows = tableData->numRows();
// Build a nice string to confirm the delete
for (int r = 0 ; r < nRows ; r++ )
{
if ( tableData->isRowSelected( r, TRUE ) )
{
listRows += r ;
qsRowsSelected += QString::number(r+1) + "," ;
}
}
qsRowsSelected.remove( qsRowsSelected.length() - 1, 1 ) ; // cleanup last comma
return listRows.count() ;
}
void classBrowse::AutoRefresh( bool b )
{
bAutoRefresh = b ;
}
bool classBrowse::OkWithUser( const QString &shortDesc, const QString &qsQuestion )
{
QMessageBox qBox("DataManager - classBrowse - " + shortDesc, // caption
qsQuestion, // text
QMessageBox::Information, // icon
QMessageBox::Yes, // button 1
QMessageBox::No | QMessageBox::Escape | QMessageBox::Default, // button 2
QMessageBox::NoButton ) ; // button 3
qBox.setIconPixmap( QPixmap( LinuxODBC_xpm ) );
return QMessageBox::Yes == qBox.exec() ;
}
int classBrowse::RowAction( const QString &shortDesc, const QString &sql, const ListRows &listRows, SQLRETURN &nReturn )
{
CursorScoper s(this) ;
nReturn = SQL_SUCCESS ;
int nRowsAffected = 0 ;
int nCols = tableData->numCols();
addStatus( QString().sprintf("%s: SQL=%s", shortDesc.upper().data(), sql.data() ) ) ;
// LOOP THROUGH THE ROWS
ListRows::ConstIterator it ;
for (it = listRows.begin(); it != listRows.end() && SQL_SUCCEEDED(nReturn) ; it++ )
{
int nRow = *it ;
addStatus( QString().sprintf("%s: row %d in progress...", shortDesc.upper().data(), nRow+1) ) ;
// CREATE A STATEMENT
StatementScoper stmt( hDbc ) ; if ( !stmt() ) return 0 ;
// PREPARE
if (!SQL_SUCCEEDED(nReturn=SQLPrepare( stmt(), (SQLCHAR*)sql.data(), SQL_NTS ) ) )
my_msgBox( "classBrowse", "SQLPrepare", nReturn, NULL, NULL, stmt(), sql ) ;
// BIND PARAMETERS
// TODO might need to add call to SQLDescribeCols to get the host data type for the Bind call.
// most drivers ignore this unneeded field.
QString *parmValues = new QString[ nCols ];
for (int nCol = 0; nCol < nCols ; nCol++ )
{
parmValues[nCol] = tableData->text(nRow, nCol) ;
if (!SQL_SUCCEEDED(nReturn=SQLBindParameter( stmt(), nCol+1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, parmValues[nCol].length(), 0, (void*)parmValues[nCol].data(), 0, 0 ) ) )
my_msgBox( "classBrowse", "SQLBindParameter", nReturn, NULL, NULL, stmt(), sql ) ;
}
// EXECUTE
if (!SQL_SUCCEEDED(nReturn=SQLExecute( stmt() ) ) )
my_msgBox( "classBrowse", "SQLExecute", nReturn, NULL, NULL, stmt(), sql ) ;
// GET NUMBER OF ROWS AFFECTED
SQLLEN nRows = 0 ;
if (!SQL_SUCCEEDED(SQLRowCount( stmt(), &nRows ) ) )
nRows = 0 ;
// DO WE NEED TO FETCH THE DATA TO COUNT THE ROWS?
if ( nRows == SQL_NO_ROW_NUMBER )
for ( nRows = 0 ; SQL_SUCCEEDED(SQLFetch(stmt())); nRows++ ) ;
nRowsAffected += nRows;
}
if (SQL_SUCCEEDED(nReturn))
addStatus( QString().sprintf("%s: %d row(s) completed", shortDesc.upper().data(), nRowsAffected) ) ;
else
addStatus( QString().sprintf("%s: %d row(s), ERROR rc=%d, %d row(s) affected", shortDesc.upper().data(), listRows.count(), nReturn, nRowsAffected) ) ;
return nRowsAffected ;
}
bool classBrowse::CheckRowsAffected(const QString &shortDesc, const ListRows &listRows )
{
// Build SQL statement with parameter markers
// SELECT COL1, COL2 FROM LIBRARY.TABLE WHERE COL1=? AND COL2=?
QString sql("SELECT ");
int nCols = tableData->numCols();
for (int nCol = 0; nCol < nCols ; nCol++ ) sql += tableData->horizontalHeader()->label(nCol) + (nCol != nCols-1 ? ", " : " ") ;
sql += "FROM ";
if (!qsLibrary.isEmpty())
sql += qsLibrary + "." ;
sql += qsTable + " WHERE ";
for (int nCol = 0; nCol < nCols ; nCol++ ) sql += tableData->horizontalHeader()->label(nCol) + "=?" + (nCol != nCols-1 ? " AND " : " ") ;
// LOOP THROUGH THE ROWS
SQLRETURN nReturn ;
ListRows::ConstIterator it ;
for (it = listRows.begin(); it != listRows.end(); it++ )
{
ListRows singleRow ; singleRow += *it ;
int nRowsAffected = RowAction( "check", sql, singleRow, nReturn ) ;
// Did an error occur?
if (!SQL_SUCCEEDED(nReturn) )
return FALSE ;
// Found a possible dup row, give the user at least one warning
if ( nRowsAffected > 1 )
return OkWithUser( shortDesc, QString().sprintf("WARNING! For row %d, the affected row count (%d) is greated then 1.\n", *it+1, nRowsAffected ) +
"This usually means there are rows with duplicate data in them.\n"
"ALL DUPLICATE ROWS WILL BE AFFECTED.\n\n"
"Do you REALLY want to continue?" ) ;
}
return TRUE ;
}
void classBrowse::DeleteRows()
{
QString qsRowsSelected ;
ListRows listRows ;
// No work to do?
if (0 == GetSelectedRows( qsRowsSelected, listRows ))
return ;
// Confirm delete with user
if (!OkWithUser( "table row delete", QString().sprintf("Do you want to delete the following %d row(s)?\nROWS: ", listRows.count()) + qsRowsSelected ))
return ;
// Double check the action to be sure user knows about duplicates that might be affected
if (!CheckRowsAffected("table row delete", listRows ))
return ;
// Build SQL statement with parameter markers
// DELETE FROM LIBRARY.TABLE WHERE COL1=? AND COL2=?
QString sql("DELETE FROM ");
if (!qsLibrary.isEmpty())
sql += qsLibrary + "." ;
sql += qsTable + " WHERE ";
int nCols = tableData->numCols();
for (int nCol = 0; nCol < nCols ; nCol++ ) sql += tableData->horizontalHeader()->label(nCol) + "=?" + (nCol != nCols-1 ? " AND " : " ") ;
// Execute the statement
SQLRETURN nReturn ;
RowAction( "delete", sql, listRows, nReturn ) ;
if ( SQL_SUCCEEDED(nReturn) && bAutoRefresh )
Exec() ;
}
void classBrowse::InsertRows()
{
QString qsRowsSelected ;
ListRows listRows ;
// No work to do?
if (0 == GetSelectedRows( qsRowsSelected, listRows ))
return ;
// Confirm delete with user
if (!OkWithUser( "table row insert", QString().sprintf("Do you want to insert the following %d row(s)?\nROWS: ", listRows.count()) + qsRowsSelected ))
return ;
// Build SQL statement with parameter markers
// INSERT INTO LIBRARY.TABLE ( COL1, COL2 ) VALUES( ?, ? )
QString sql("INSERT INTO ");
if (!qsLibrary.isEmpty())
sql += qsLibrary + "." ;
sql += qsTable + " ( ";
int nCols = tableData->numCols();
for (int nCol = 0; nCol < nCols ; nCol++ ) sql += tableData->horizontalHeader()->label(nCol) + (nCol != nCols-1 ? ", " : " ") ;
sql += ") VALUES ( ";
for (int nCol = 0; nCol < nCols ; nCol++ ) sql += QString( "?" ) + (nCol != nCols-1 ? ", " : " ") ;
sql += ") ";
// Execute the statement
SQLRETURN nReturn ;
RowAction( "insert", sql, listRows, nReturn ) ;
if ( SQL_SUCCEEDED(nReturn) && bAutoRefresh )
Exec() ;
}
int classBrowse::AddRow()
{
// Add the Row
int nRows = tableData->numRows();
tableData->setNumRows(nRows+1);
tableData->verticalHeader()->setLabel( nRows, QString().sprintf("*%d", nRows+1) ) ;
// Scroll down to the new row
tableData->ensureCellVisible( nRows, 0 ) ;
addStatus( QString().sprintf("ADD: row %d added", nRows+1) ) ;
return nRows ;
}
int classBrowse::CopyRow()
{
QString qsRowsSelected ;
ListRows listRows ;
// No work to do?
if (1 != GetSelectedRows( qsRowsSelected, listRows )) return 0 ;
// Add a new row
int nRows = AddRow();
// Copy the contents of the selected row
int nCols = tableData->numCols();
for (int nCol = 0; nCol < nCols ; nCol++ ) tableData->setText( nRows, nCol, tableData->text( *listRows.begin(), nCol) ) ;
// Scroll down to the new copy
tableData->ensureCellVisible( nRows, 0 ) ;
addStatus( QString().sprintf("COPY: row %d copied to new row %d", *listRows.begin()+1, nRows+1) ) ;
return nRows ;
}
void classBrowse::resizeEvent( QResizeEvent *p )
{
resize( p->size() );
}
void classBrowse::Clear() // CLEAR QUERY AND DATA
{
ClearCells( tableData );
InitQuery();
}
bool classBrowse::Save() // DATA
{
labelResults->setText("RESULTS" );
if ( qsDataFileName.isEmpty() )
return SaveAs();
// TRY TO SAVE THE FILE
QFile hFile( qsDataFileName );
if ( !hFile.open( IO_WriteOnly ) )
{
my_msgBox( "classBrowse", "QFile.open", hFile.status(), NULL, NULL, NULL, qsDataFileName ) ;
return FALSE;
}
WriteHTML( &hFile, TRUE );
hFile.close();
labelResults->setText( QString().sprintf("RESULTS (%s)", qsDataFileName.data() ) );
addStatus( QString().sprintf("SAVE: results saved as %s", qsDataFileName.data() ) ) ;
return TRUE;
}
bool classBrowse::SaveAs() // DATA
{
// LET USER PICK A FILE
QString fileName = QFileDialog::getSaveFileName( qsDataFileName );
if ( fileName.isEmpty() )
return FALSE;
else
{
qsDataFileName = fileName ;
return Save();
}
}
bool classBrowse::SaveSQL() // SAVE QUERY
{
labelFilter->setText( "FILTER (%s)" );
QString fileName = QFileDialog::getSaveFileName( qsSQLFileName );
if (fileName.isEmpty())
return FALSE;
// TRY TO SAVE THE FILE
QFile hFile( fileName );
if ( !hFile.open( IO_WriteOnly ) )
{
my_msgBox( "classBrowse", "QFile.open", hFile.status(), NULL, NULL, NULL, fileName ) ;
return FALSE;
}
QString qsSQL;
CreateSQL( qsSQL ) ;
hFile.writeBlock( qsSQL.data(), qsSQL.length() ) ;
hFile.close();
qsSQLFileName = fileName ;
labelFilter->setText( QString().sprintf("FILTER (%s)", qsSQLFileName.data() ) );
addStatus( QString().sprintf("SAVE SQL: sql filter saved as %s", qsSQLFileName.data() ) ) ;
return TRUE;
}
void classBrowse::InitQuery()
{
SQLRETURN nReturn ;
SQLCHAR szColumnName[MAX_COLUMN_WIDTH];
SQLLEN nCols = 0;
int nCol = 0;
QHeader *headerQuery = tableQuery->horizontalHeader();
// CREATE A STATEMENT
StatementScoper stmt( hDbc ) ; if ( !stmt() ) return ;
// EXECUTE
if (!SQL_SUCCEEDED(nReturn=SQLColumns( stmt(), 0, 0, (SQLCHAR*)qsLibrary.data(), SQL_NTS, (SQLCHAR*)qsTable.data(), SQL_NTS, 0, 0 ) ) )
return my_msgBox( "classBrowse", "SQLColumns", nReturn, NULL, NULL, stmt() ) ;
// GET NUMBER OF COLUMNS AFFECTED
if (!SQL_SUCCEEDED(SQLRowCount( stmt(), &nCols ) ) )
nCols = 0;
// Default Value, since some drivers do not handle RowCount up front for performance reasons
if ( nCols == SQL_NO_ROW_NUMBER )
nCols = 1000; // give a resonable number back
tableQuery->setNumCols( nCols );
// GET RESULTS
while ( SQL_SUCCEEDED(SQLFetch( stmt() ) ) && nCol < nCols )
{
if (!SQL_SUCCEEDED(SQLGetData( stmt(), SQLColumns_COLUMN_NAME, SQL_C_CHAR, &szColumnName[0], sizeof(szColumnName), 0 ) ) )
strcpy( (char *)szColumnName, "Unknown" );
headerQuery->setLabel( nCol, QString((char*)szColumnName).stripWhiteSpace() );
nCol++;
}
// Show the correct number of columns to make the gui look correct
tableQuery->setNumCols( nCol );
SelectAllColumns();
}
void classBrowse::Exec() // RUN QUERY
{
CursorScoper s(this) ;
SQLLEN nRows;
SWORD nColumns;
SQLRETURN nReturn;
QString qsSQL ;
if (!CreateSQL( qsSQL ) )
return ;
ClearCells( tableData );
// CREATE A STATEMENT
StatementScoper stmt( hDbc ) ; if ( !stmt() ) return ;
// PREPATE
if (!SQL_SUCCEEDED(nReturn=SQLPrepare(stmt(), (SQLCHAR*)qsSQL.data(), SQL_NTS) ) )
return my_msgBox( "classBrowse", "SQLPrepare", nReturn, NULL, NULL, stmt(), qsSQL ) ;
// EXECUTE
addStatus( QString().sprintf( "RUN: filter sql=%s", qsSQL.data() ) ) ;
if(!SQL_SUCCEEDED(nReturn=SQLExecute(stmt()) ) )
return my_msgBox( "classBrowse", "SQLExecute", nReturn, NULL, NULL, stmt(), qsSQL ) ;
// GET NUMBER OF ROWS AFFECTED
if (!SQL_SUCCEEDED(SQLRowCount( stmt(), &nRows ) ) )
nRows = 0;
// Default Value, since some drivers do not handle RowCount up front for performance reasons
if ( nRows == SQL_NO_ROW_NUMBER )
nRows = MAX_ROWS_IN_TABLE; // give a resonable number back
tableData->setNumRows( nRows );
// GET NUMBER OF COLUMNS RETURNED
if (!SQL_SUCCEEDED(SQLNumResultCols( stmt(), &nColumns ) ) )
nColumns = 0;
tableData->setNumCols( nColumns );
// GET A RESULTS HEADER (column headers)
ExecHeader( stmt(), nColumns );
// GET A RESULTS BODY (data)
nRows = ExecBody( stmt(), nColumns );
// Show the correct number of rows to make the gui look correct
tableData->setNumRows( nRows );
// Update the status
addStatus( QString().sprintf( "RUN: %d rows and %d columns affected", nRows, nColumns ) ) ;
}
void classBrowse::ExecHeader( SQLHSTMT hStmt, SWORD nColumns )
{
for( int nCol = 0; nCol < nColumns; nCol++ )
{
SQLCHAR szColumnName[MAX_COLUMN_WIDTH]; szColumnName[0] = 0;
SQLColAttribute( hStmt, nCol+1, SQL_DESC_LABEL, szColumnName, sizeof(szColumnName), 0, 0 );
tableData->horizontalHeader()->setLabel( nCol, QString( (char*)szColumnName ).stripWhiteSpace() );
}
}
int classBrowse::ExecBody( SQLHSTMT hStmt, SWORD nColumns )
{
int nRow = 0;
// PROCESS ALL ROWS
while( SQL_SUCCEEDED(SQLFetch(hStmt) ) && nRow < MAX_ROWS_IN_TABLE )
{
nRow++;
// PROCESS ALL COLUMNS
for( int nCol = 1; nCol <= nColumns; nCol++ )
{
SQLLEN nIndicator;
char szData[MAX_COLUMN_WIDTH]; szData[0] = 0 ;
SQLRETURN nReturn = SQLGetData( hStmt, nCol, SQL_C_CHAR, (SQLPOINTER)szData, sizeof(szData), &nIndicator );
if ( SQL_SUCCEEDED(nReturn) && nIndicator != SQL_NULL_DATA )
tableData->setText( nRow-1, nCol-1, QString( szData ).stripWhiteSpace() );
else
tableData->setText( nRow-1, nCol-1, " " );
tableData->verticalHeader()->setLabel( nRow-1, QString::number( nRow ) ) ;
}
}
return nRow ; // return the actual number of rows in the result set
}
bool classBrowse::CreateSQL( QString& qsSQL )
{
int nCols = tableQuery->numCols();
QString qsColumns ;
QString qsWhereExpressions ;
QString qsAnd ;
QString qsComma1 ;
QString qsComma2 ;
QString qsSorts ;
for (int nCol=0; nCol<nCols; nCol++ )
{
QString qsColumn = tableQuery->horizontalHeader()->label( nCol );
qsColumn.prepend('"').append('"') ; // Quote column name in case it matches SQL keyword
QString qsExpression = tableQuery->text( QUERY_ROW_EXPRESSION, nCol );
QString qsShow = tableQuery->text( QUERY_ROW_SHOW, nCol ).upper();
QString qsSort = tableQuery->text( QUERY_ROW_SORT, nCol ).upper();
if ( qsShow.startsWith("Y") )
{
qsColumns += qsComma1;
qsColumns += qsColumn;
qsComma1 = ", ";
}
if ( !qsSort.isEmpty() )
{
qsSorts += qsComma2;
qsSorts += qsColumn;
qsSorts += qsSort.startsWith("D") ? " DESC " : " ASC " ;
qsComma2 = ", ";
}
if ( !qsExpression.isEmpty() )
{
qsWhereExpressions += qsAnd;
qsWhereExpressions += qsColumn;
qsWhereExpressions += " ";
qsWhereExpressions += qsExpression;
qsAnd = " AND ";
}
}
if ( qsColumns.isEmpty() )
{
QMessageBox::warning( (QWidget *)this, "Table browse", "No columns to show. Please specify which columns you want to show." );
return false;
}
qsSQL = "SELECT ";
qsSQL += qsColumns;
qsSQL += " FROM ";
if (!qsLibrary.isEmpty())
qsSQL += qsLibrary + "." ;
qsSQL += qsTable;
if (!qsWhereExpressions.isEmpty()) qsSQL += " WHERE " + qsWhereExpressions;
if (!qsSorts.isEmpty() ) qsSQL += " ORDER BY " + qsSorts;
return true;
}
void classBrowse::ChangeAllColumns( int row, const char * text )
{
int nCol = 0;
int nCols = tableQuery->numCols();
QHeader *header;
header = tableQuery->horizontalHeader();
for ( nCol=0; nCol<nCols; nCol++ )
tableQuery->setText( row, nCol, text );
}
void classBrowse::SelectAllColumns()
{
ChangeAllColumns( QUERY_ROW_SHOW, "YES" ) ;
}
void classBrowse::UnSelectAllColumns()
{
ChangeAllColumns( QUERY_ROW_SHOW, "" ) ;
}
void classBrowse::UnSortAllColumns()
{
ChangeAllColumns( QUERY_ROW_SORT, "" ) ;
}
void classBrowse::UnExprAllColumns()
{
ChangeAllColumns( QUERY_ROW_EXPRESSION, "" ) ;
}
void classBrowse::WriteHTML( QFile *hFile, bool bPage )
{
int nCol = 0;
int nCols = tableData->numCols();
int nRow = 0;
int nRows = tableData->numRows();
QHeader *header;
header = tableData->horizontalHeader();
if ( bPage )
hFile->writeBlock( "<HTML><BODY>", sizeof("<HTML><BODY>")-1 );
hFile->writeBlock( "<TABLE>", sizeof("<TABLE>")-1 );
// HEADER
hFile->writeBlock( "\n<TR>", sizeof("\n<TR>")-1 );
for ( nCol=0; nCol<nCols; nCol++ )
{
hFile->writeBlock( "<TH>", sizeof("<TH>")-1 );
hFile->writeBlock( header->label( nCol ), header->label( nCol ).length() );
hFile->writeBlock( "</TH>", sizeof("</TH>")-1 );
}
hFile->writeBlock( "</TR>", sizeof("</TR>")-1 );
// BODY
for ( nRow=0; nRow<nRows; nRow++ )
{
hFile->writeBlock( "\n<TR>", sizeof("\n<TR>")-1 );
for ( nCol=0; nCol<nCols; nCol++ )
{
hFile->writeBlock( "\n<TD>", sizeof("\n<TD>")-1 );
hFile->writeBlock( tableData->text( nRow, nCol ), tableData->text( nRow, nCol ).length() );
hFile->writeBlock( "</TD>", sizeof("</TD>")-1 );
}
hFile->writeBlock( "</TR>", sizeof("</TR>")-1 );
}
hFile->writeBlock( "\n</TABLE>", sizeof("\n</TABLE>")-1 );
if ( bPage )
hFile->writeBlock( "</BODY></HTML>", sizeof("</BODY></HTML>")-1 );
}
void classBrowse::ClearCells( QTable *table )
{
int nCol = 0;
int nCols = table->numCols();
int nRow = 0;
int nRows = table->numRows();
for ( nRow=0; nRow<nRows; nRow++ )
{
for ( nCol=0; nCol<nCols; nCol++ )
{
table->clearCell( nRow, nCol );
}
}
}
|