Subversion Repositories Programming Utils

Compare Revisions

Ignore whitespace Rev 87 → Rev 88

/trunk/TrainDatabase/traintablemodel.cpp
1,6 → 1,8
#include <QDebug>
#include <QPushButton>
 
#include "traintablemodel.h"
#include "dbexception.h"
 
TrainTableModel::TrainTableModel( DBAccess* access, QObject *parent) :
QAbstractTableModel(parent)
32,6 → 34,7
case 2: return record->getCost();
case 3: return QString( record->getStockType().c_str() );
case 4: return QString( record->getNotes().c_str() );
case 5: return QString( "Delete" );
}
}
return QVariant();
46,6 → 49,7
case 2: return QString( "Cost" );
case 3: return QString( "Type" );
case 4: return QString( "Notes" );
case 5: return QString( "Delete" );
}
}
}
74,7 → 78,14
case 4: record->setNotes( value.toString().toStdString() );
break;
}
try{
access->updateRecord( record );
}catch( DBException ex ){
qDebug() << ex.what();
}
}
 
emit dataChanged( index, index );
return true;
}
 
84,3 → 95,21
 
emit dataChanged( topIndex, bottomIndex );
}
 
void TrainTableModel::updateLastRecord(){
beginInsertRows( QModelIndex(), access->getNumberOfRecords(), access->getNumberOfRecords() );
endInsertRows();
 
emit dataChanged( QModelIndex(), QModelIndex() );
}
 
void TrainTableModel::deleteRow( int row ){
TrainRecord* rec = access->getRecord( row);
 
beginRemoveRows( QModelIndex(), rec->getKey(), rec->getKey() );
endRemoveRows();
 
access->deleteRecord( rec );
 
emit dataChanged( QModelIndex(), QModelIndex() );
}
/trunk/TrainDatabase/dbaccess.cpp
19,8 → 19,8
 
error = sqlite3_open_v2( dbLocation, &m_sql, flags, NULL );
if( error ){
QString exc = QString( "Can't open database: %1" ).arg( sqlite3_errmsg( m_sql ) );
throw DBException( exc.toStdString() );
QString exc = QString( "Can't open database: %1" ).arg( sqlite3_errmsg( m_sql ) );
throw DBException( exc.toStdString() );
}
 
if( create ){
33,8 → 33,8
Notes TEXT )",
NULL, NULL, NULL );
if( error ){
QString exc = QString( "Can't create database: %1" ).arg( sqlite3_errmsg( m_sql ) );
throw DBException( exc.toStdString() );
QString exc = QString( "Can't create database: %1" ).arg( sqlite3_errmsg( m_sql ) );
throw DBException( exc.toStdString() );
}
}
 
41,7 → 41,7
records = getNumberOfRecords();
allRecords.resize( records );
 
if( sqlite3_prepare_v2( m_sql, "SELECT Reporting_Mark, Road_Num, Type, Cost, Notes FROM Trains",
if( sqlite3_prepare_v2( m_sql, "SELECT train_id, Reporting_Mark, Road_Num, Type, Cost, Notes FROM Trains",
-1, &res, NULL ) != SQLITE_OK ){
QString exc = QString( "Can't create prepared statement: %1" ).arg( sqlite3_errmsg( m_sql ) );
throw DBException( exc.toStdString() );
51,11 → 51,12
count = 0;
while( result = sqlite3_step( res ), result == SQLITE_ROW ){
TrainRecord* newRecord = new TrainRecord(
(const char*)sqlite3_column_text( res, 0 ),
sqlite3_column_int( res, 1 ),
(const char*)sqlite3_column_text( res, 2 ),
sqlite3_column_int( res, 3 ),
(const char*)sqlite3_column_text( res, 4 )
sqlite3_column_int( res, 0 ),
(const char*)sqlite3_column_text( res, 1 ),
sqlite3_column_int( res, 2 ),
(const char*)sqlite3_column_text( res, 3 ),
sqlite3_column_int( res, 4 ),
(const char*)sqlite3_column_text( res, 5 )
);
allRecords[ count ] = newRecord;
count++;
116,3 → 117,156
}
return allRecords[ num ];
}
 
void DBAccess::updateRecord( TrainRecord * rec ){
sqlite3_stmt* res;
int result;
if( rec == NULL ){
return;
}
 
//set the reporting mark
if( sqlite3_prepare_v2( m_sql, "UPDATE Trains SET Reporting_Mark = ? WHERE train_id = ?", -1, &res, NULL ) != SQLITE_OK ){
QString exc = QString( "Can't create prepared statement: %1" ).arg( sqlite3_errmsg( m_sql ) );
throw DBException( exc.toStdString() );
}
sqlite3_bind_text( res, 1, rec->getReportingMark().c_str(), -1, NULL );
sqlite3_bind_int( res, 2, rec->getKey() );
 
result = sqlite3_step( res );
if( result != SQLITE_DONE ){
QString exc = QString( "Can't execute prepared statement: %1" ).arg( sqlite3_errmsg( m_sql ) );
throw DBException( exc.toStdString() );
}
 
//set the road number
if( sqlite3_prepare_v2( m_sql, "UPDATE Trains SET Road_Num = ? WHERE train_id = ?", -1, &res, NULL ) != SQLITE_OK ){
QString exc = QString( "Can't create prepared statement: %1" ).arg( sqlite3_errmsg( m_sql ) );
throw DBException( exc.toStdString() );
}
sqlite3_bind_int( res, 1, rec->getRoadNumber() );
sqlite3_bind_int( res, 2, rec->getKey() );
 
result = sqlite3_step( res );
if( result != SQLITE_DONE ){
QString exc = QString( "Can't execute prepared statement: %1" ).arg( sqlite3_errmsg( m_sql ) );
throw DBException( exc.toStdString() );
}
 
//set the type
if( sqlite3_prepare_v2( m_sql, "UPDATE Trains SET Type = ? WHERE train_id = ?", -1, &res, NULL ) != SQLITE_OK ){
QString exc = QString( "Can't create prepared statement: %1" ).arg( sqlite3_errmsg( m_sql ) );
throw DBException( exc.toStdString() );
}
sqlite3_bind_text( res, 1, rec->getStockType().c_str(), -1, NULL );
sqlite3_bind_int( res, 2, rec->getKey() );
 
result = sqlite3_step( res );
if( result != SQLITE_DONE ){
QString exc = QString( "Can't execute prepared statement: %1" ).arg( sqlite3_errmsg( m_sql ) );
throw DBException( exc.toStdString() );
}
 
//set the cost
if( sqlite3_prepare_v2( m_sql, "UPDATE Trains SET Cost = ? WHERE train_id = ?", -1, &res, NULL ) != SQLITE_OK ){
QString exc = QString( "Can't create prepared statement: %1" ).arg( sqlite3_errmsg( m_sql ) );
throw DBException( exc.toStdString() );
}
sqlite3_bind_int( res, 1, rec->getCost() );
sqlite3_bind_int( res, 2, rec->getKey() );
 
result = sqlite3_step( res );
if( result != SQLITE_DONE ){
QString exc = QString( "Can't execute prepared statement: %1" ).arg( sqlite3_errmsg( m_sql ) );
throw DBException( exc.toStdString() );
}
 
//set the notes
if( sqlite3_prepare_v2( m_sql, "UPDATE Trains SET Notes = ? WHERE train_id = ?", -1, &res, NULL ) != SQLITE_OK ){
QString exc = QString( "Can't create prepared statement: %1" ).arg( sqlite3_errmsg( m_sql ) );
throw DBException( exc.toStdString() );
}
sqlite3_bind_text( res, 1, rec->getNotes().c_str(), -1, NULL );
sqlite3_bind_int( res, 2, rec->getKey() );
 
result = sqlite3_step( res );
if( result != SQLITE_DONE ){
QString exc = QString( "Can't execute prepared statement: %1" ).arg( sqlite3_errmsg( m_sql ) );
throw DBException( exc.toStdString() );
}
}
 
TrainRecord* DBAccess::newRecord(){
sqlite3_stmt* res;
int result;
 
//set the reporting mark to be NEW
if( sqlite3_prepare_v2( m_sql, "INSERT INTO Trains ( Reporting_Mark ) VALUES ( 'NEW' )", -1, &res, NULL ) != SQLITE_OK ){
QString exc = QString( "Can't create prepared statement: %1" ).arg( sqlite3_errmsg( m_sql ) );
throw DBException( exc.toStdString() );
}
 
result = sqlite3_step( res );
if( result != SQLITE_DONE ){
QString exc = QString( "Can't execute prepared statement: %1" ).arg( sqlite3_errmsg( m_sql ) );
throw DBException( exc.toStdString() );
}
 
//get the new record and return a pointer to it.
if( sqlite3_prepare_v2( m_sql, "SELECT train_id, reporting_mark, road_num, type, cost FROM Trains ORDER BY train_id DESC LIMIT 1",
-1, &res, NULL ) != SQLITE_OK ){
QString exc = QString( "Can't create prepared statement: %1" ).arg( sqlite3_errmsg( m_sql ) );
throw DBException( exc.toStdString() );
}
 
result = sqlite3_step( res );
if( result != SQLITE_ROW){
QString exc = QString( "Can't execute prepared statement: %1" ).arg( sqlite3_errmsg( m_sql ) );
throw DBException( exc.toStdString() );
}
 
TrainRecord* newRecord = new TrainRecord(
sqlite3_column_int( res, 0 ),
(const char*)sqlite3_column_text( res, 1 ),
sqlite3_column_int( res, 2 ),
(const char*)sqlite3_column_text( res, 3 ),
sqlite3_column_int( res, 4 ),
(const char*)sqlite3_column_text( res, 5 )
);
allRecords.push_back( newRecord );
 
return newRecord;
}
 
void DBAccess::deleteRecord( TrainRecord * toDelete ){
int result;
if( toDelete == NULL ){
return;
}
 
sqlite3_stmt* res;
std::vector<TrainRecord*>::iterator it;
if( sqlite3_prepare_v2( m_sql, "DELETE FROM Trains WHERE train_id = ?", -1, &res, NULL ) != SQLITE_OK ){
QString exc = QString( "Can't create prepared statement: %1" ).arg( sqlite3_errmsg( m_sql ) );
throw DBException( exc.toStdString() );
}
 
sqlite3_bind_int( res, 1, toDelete->getKey() );
result = sqlite3_step( res );
if( result != SQLITE_DONE ){
QString exc = QString( "Can't execute prepared statement: %1" ).arg( sqlite3_errmsg( m_sql ) );
throw DBException( exc.toStdString() );
}
 
//find this in our local cache
for( it = allRecords.begin(); it != allRecords.end(); it++ ){
if( *it == toDelete ){
break;
}
}
 
if( it != allRecords.end() ){
allRecords.erase( it );
delete toDelete;
}
}
/trunk/TrainDatabase/traintablemodel.h
22,6 → 22,16
*/
void updateFull();
 
/**
* @brief updateLastRecord Update just the last record in the table.
*/
void updateLastRecord();
 
/**
* @brief deleteSelected Delete the given row.
*/
void deleteRow( int row );
 
signals:
 
public slots:
/trunk/TrainDatabase/dbaccess.h
36,6 → 36,17
*/
void updateRecord( TrainRecord* );
 
/**
* @brief newRecord Create a new record and return it
* @return
*/
TrainRecord* newRecord();
 
/**
* @brief deleteRecord Delete the given record from the database
*/
void deleteRecord( TrainRecord* );
 
private:
sqlite3* m_sql;
int records;
/trunk/TrainDatabase/trainrecord.cpp
3,7 → 3,7
 
#include "trainrecord.h"
 
TrainRecord::TrainRecord( const char* reporting_mark, uint16_t road_number,
TrainRecord::TrainRecord( int key, const char* reporting_mark, uint16_t road_number,
const char* stock_type, uint16_t costi, const char* notesi ){
if( reporting_mark != NULL ){
reporting.assign( reporting_mark );
16,6 → 16,7
if( notesi != NULL ){
notes.assign( notesi );
}
this->key = key;
}
 
std::string TrainRecord::getReportingMark(){
57,3 → 58,7
void TrainRecord::setNotes( std::string notesIn ){
notes.assign( notesIn );
}
 
int TrainRecord::getKey(){
return key;
}
/trunk/TrainDatabase/traindb.ui
49,6 → 49,20
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>New Record</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="deleteButton">
<property name="text">
<string>Delete Selected</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
/trunk/TrainDatabase/trainrecord.h
7,7 → 7,8
class TrainRecord
{
public:
TrainRecord( const char* reporting_mark,
TrainRecord( int key,
const char* reporting_mark,
uint16_t road_number,
const char* stock_type,
uint16_t costi,
28,6 → 29,8
std::string getNotes();
void setNotes( std::string );
 
int getKey();
 
private:
std::string reporting;
uint16_t road;
34,6 → 37,7
std::string stock;
uint16_t cost;
std::string notes;
int key;
};
 
#endif // TRAINRECORD_H
/trunk/TrainDatabase/traindb.cpp
1,6 → 1,7
#include <QFileDialog>
#include <QDebug>
#include <QMessageBox>
#include <QModelIndexList>
 
#include "traindb.h"
#include "ui_traindb.h"
100,4 → 101,41
}
model = new TrainTableModel( access );
ui->trainTable->setModel( model );
connect( model, SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)),
this, SLOT(db_updated(const QModelIndex&, const QModelIndex&)) );
ui->trainTable->setColumnWidth( 0, 120 );
ui->trainTable->setColumnWidth( 4, 400 );
}
 
void TrainDB::on_pushButton_clicked()
{
if( access == NULL ){
return;
}
 
access->newRecord();
model->updateLastRecord();
}
 
void TrainDB::db_updated(const QModelIndex &, const QModelIndex &){
QString formatted = QString( "%1" ).arg( access->getNumberOfRecords() );
ui->numberOfTrains->setText( formatted );
QString costformatted = QString( "$%1" ).arg( access->getTotalCost() );
ui->totalCost->setText( costformatted );
}
 
void TrainDB::on_deleteButton_clicked()
{
int response = QMessageBox::question( this, "Delete?", "Are you sure you want to delete this record?",
QMessageBox::Yes, QMessageBox::No );
if( response != QMessageBox::Yes ){
return;
}
 
QModelIndexList bb = ui->trainTable->selectionModel()->selection().indexes();
try{
model->deleteRow( bb[ 0 ].row() );
}catch( DBException ex ){
QMessageBox::critical( this, "ERROR", ex.what() );
}
}
/trunk/TrainDatabase/aboutdialog.ui
11,7 → 11,7
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
<string>About</string>
</property>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
81,14 → 81,14
<widget class="QLabel" name="label_4">
<property name="geometry">
<rect>
<x>110</x>
<x>80</x>
<y>180</y>
<width>181</width>
<width>251</width>
<height>21</height>
</rect>
</property>
<property name="text">
<string>http:www.rm5248.com</string>
<string>http://programming.rm5248.com</string>
</property>
<property name="openExternalLinks">
<bool>true</bool>
/trunk/TrainDatabase/traindb.h
27,6 → 27,12
 
void on_actionNew_Database_triggered();
 
void on_pushButton_clicked();
 
void db_updated( const QModelIndex & topLeft, const QModelIndex & bottomRigh );
 
void on_deleteButton_clicked();
 
private:
void updateGUILabelsAndModel();