Sohag Developer PostgreSQL Database Applications

Build Powerful Robust Database Applications

Simple Template Class Implementation .cpp

Tue, 07/17/2018 - 22:31 - amr

Simple Template Class Implementation .cpp


    
#include "employees.h"
#include "ui_employees.h"
#include <QSqlQuery>
#include <QSqlError>
#include <QMessageBox>


Employees::Employees(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Employees)
{
    ui->setupUi(this);
   
    db = QSqlDatabase::database();
   
    newRecord();
    connect(ui->saveButton,SIGNAL(pressed()),this,SLOT(save()));
    connect(ui->newButton,SIGNAL(pressed()),this,SLOT(newRecord()));
    connect(ui->deleteButton,SIGNAL(pressed()),this,SLOT(remove()));
    connect(ui->queryButton,SIGNAL(pressed()),this,SLOT(query()));
    connect(ui->closeButton,SIGNAL(pressed()),this,SLOT(close()));
   
    connect(ui->employee_id,SIGNAL(textEdited(QString)),this,SLOT(setStatusChanged()));
    connect(ui->employee_name,SIGNAL(textEdited(QString)),this,SLOT(setStatusChanged()));
    connect(ui->gender,SIGNAL(textEdited(QString)),this,SLOT(setStatusChanged()));
    connect(ui->birth_date,SIGNAL(textEdited(QString)),this,SLOT(setStatusChanged()));
    connect(ui->department_id,SIGNAL(textEdited(QString)),this,SLOT(setStatusChanged()));
    connect(ui->job_id,SIGNAL(textEdited(QString)),this,SLOT(setStatusChanged()));
   
   
}

Employees::~Employees()
{
    delete ui;
}




void Employees::newRecord()
{
    clear();
    mode=CreateRecord;
    status=UnChangedStatus;
    db.transaction();
}


void Employees::save()
{
   
    if(status==UnChangedStatus){
        // Unchanged Record Action
        return;
    }
    if(mode==CreateRecord){
        if(insert()){
            // Insert success Action
        }else{
            // Insert Fail Action
            return;
        }
    }else if(mode==EditRecord){
        if(update()){
            // Edit success Action
           
        }else{
            // Edit Fail Action
            return;
        }
    }
    if(db.commit()){
        if(mode==DeleteRecord){
            mode=CreateRecord;
        }else{
            mode=EditRecord;
        }
        status=UnChangedStatus;
        db.transaction();
        // Save success Action
    }else{
        // Save Fail Action
        error(db.lastError().databaseText(),"Save Failed\n"+db.lastError().text(),SaveOperation);
    }
}



void Employees::clear()
{
    ui->employee_id->clear();
    ui->employee_name->clear();
    ui->gender->clear();
    ui->birth_date->clear();
    ui->department_id->clear();
    ui->job_id->clear();
   
}


void Employees::setStatusChanged()
{
    status=ChangedStatus;
}


void Employees::error(QString code, QString errorText, int operation)
{
    QMessageBox::information(this,"error",code+"\n"+errorText+QString::number(operation));
}

QVariant Employees::toVarinat(QString string)
{
    if (string.isEmpty()){
        return QVariant();
    }
    return QVariant(string);
}


QString Employees::toString(QVariant value)
{
    int valueType=value.type();
    if(value.isNull()){
        return QString();
    }else if(valueType==QMetaType::Int){
        return QString::number(value.toInt());
    }else if(valueType==QMetaType::UInt){
        return QString::number(value.toUInt());
    }else if(valueType==QMetaType::Double){
        return QString::number(value.toDouble());
    }else if(valueType==QMetaType::Long){
        return QString::number(value.toLongLong());
    }else if(valueType==QMetaType::LongLong){
        return QString::number(value.toLongLong());
    }else if(valueType==QMetaType::Short){
        return QString::number(value.toInt());
    }else if(valueType==QMetaType::ULong){
        return QString::number(value.toULongLong());
    }else if(valueType==QMetaType::ULongLong){
        return QString::number(value.toULongLong());
    }else if(valueType==QMetaType::UShort){
        return QString::number(value.toUInt());
    }else if(valueType==QMetaType::Float){
        return QString::number(value.toFloat());
    }
    return value.toString();
}


bool Employees::insert()
{
    QSqlQuery query;
    query.prepare("insert into public.employees(employee_id,employee_name,gender,birth_date,department_id,job_id) values (?,?,?,?,?,?)");
    query.addBindValue(toVarinat(ui->employee_id->text()));
    query.addBindValue(toVarinat(ui->employee_name->text()));
    query.addBindValue(toVarinat(ui->gender->text()));
    query.addBindValue(toVarinat(ui->birth_date->text()));
    query.addBindValue(toVarinat(ui->department_id->text()));
    query.addBindValue(toVarinat(ui->job_id->text()));


    query.exec();
    if(query.lastError().isValid()){
        error(db.lastError().databaseText(),query.lastError().text(),InsertOperation);

        return false;
    }
    return true;
}


bool Employees::update()
{
    QSqlQuery query;
    query.prepare("UPDATE public.employees SET employee_id=?,employee_name=?,gender=?,birth_date=?,department_id=?,job_id=? WHERE employee_id=?");
    query.addBindValue(toVarinat(ui->employee_id->text()));
    query.addBindValue(toVarinat(ui->employee_name->text()));
    query.addBindValue(toVarinat(ui->gender->text()));
    query.addBindValue(toVarinat(ui->birth_date->text()));
    query.addBindValue(toVarinat(ui->department_id->text()));
    query.addBindValue(toVarinat(ui->job_id->text()));


    query.addBindValue(ui->employee_id->text());


    query.exec();
    if(query.lastError().isValid()){
        error(db.lastError().databaseText(),query.lastError().text(),UpdateOperation);

        return false;
    }
    return true;
}


bool Employees::remove()
{
    QSqlQuery query;
    query.prepare("delete from public.employees WHERE employee_id=?");

    query.addBindValue(ui->employee_id->text());


    query.exec();
    if(query.lastError().isValid()){
        error(db.lastError().databaseText(),query.lastError().text(),RemoveOperation);

        return false;
    }
    status=ChangedStatus;
    clear();
    mode=DeleteRecord;
    return true;
}


bool Employees::query()
{
    QSqlQuery query;
    query.prepare("select employee_id,employee_name,gender,birth_date,department_id,job_id from public.employees WHERE employee_id=?");

    query.addBindValue(ui->employee_id->text());


    query.exec();
    if(query.lastError().isValid()){
        error(db.lastError().databaseText(),query.lastError().text(),QueryOperation);

        return false;
    }
    query.first();
    ui->employee_id->setText(toString(query.value(0)));
    ui->employee_name->setText(toString(query.value(1)));
    ui->gender->setText(toString(query.value(2)));
    ui->birth_date->setText(toString(query.value(3)));
    ui->department_id->setText(toString(query.value(4)));
    ui->job_id->setText(toString(query.value(5)));
    mode=EditRecord;
    status=UnChangedStatus;
    return true;
}

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.