bool ScientistRepository::removeScientist(Scientist scientist)
{
    db.open();

    QSqlQuery query(db);

    stringstream sqlQuery;

    //All scientists with the exact same information as the input scientist will be deleted from the database. The primary key is irrelevant.
    sqlQuery << "DELETE FROM scientists WHERE name = '" << scientist.getName() << "' AND sex = " << scientist.getSex() << " AND yearBorn = " << scientist.getYearBorn();

    if (scientist.getYearDied() == constants::YEAR_NOT_ENTERED_DEFAULT_VALUE)
    {   //No year of death entered, search empty fields
        sqlQuery << " AND yearDied IS NULL";
    }
    else
    {   //year of death entered, normal search
        sqlQuery << " AND yearDied = " << scientist.getYearDied();
    }

    bool success = query.exec(QString::fromStdString(sqlQuery.str()));

    db.close();

    return success;

}
bool ScientistRepository::addScientist(Scientist scientist)
{
    if(db.open())
    {
        QSqlQuery query(db);

        string tableCreation ="CREATE TABLE IF NOT EXISTS scientists(sci_id INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL , name VARCHAR, sex INTEGER, yearBorn INTEGER, yearDied INTEGER)";
        query.exec(QString(tableCreation.c_str()));

        string sSex = utils::intToString(scientist.getSex());
        string sYearBorn = utils::intToString(scientist.getYearBorn());
        string queryInsert = "INSERT INTO scientists(name,sex,yearBorn)VALUES('"+scientist.getName()+"','"+sSex+"','"+sYearBorn+"')";
        if(query.exec(QString(queryInsert.c_str())))    //exec returns true if it is successful
        {
            if(scientist.getYearDied() != constants::YEAR_NOT_ENTERED_DEFAULT_VALUE)
            {
                string sYearDied = utils::intToString(scientist.getYearDied());
                queryInsert = "UPDATE scientists SET yearDied = '" + sYearDied + "' WHERE sci_id = (SELECT MAX(sci_id) FROM scientists)";
                query.exec(QString(queryInsert.c_str()));
            }
        }
        else
        {   //first query failed
            db.close();
            return false;
        }
        db.close();
    }
    else    //file open failed
        return false;
    return true;
}
bool ScientistRepository::addScientist(Scientist scientist)
{
    ofstream file;

    file.open(fileName.c_str(), std::ios::app);

    if (file.is_open())
    {
        string name = scientist.getName();
        enum sexType sex = scientist.getSex();
        int yearBorn = scientist.getYearBorn();
        int yearDied = scientist.getYearDied();

        file << name << constants::FILE_DELIMETER
             << sex << constants::FILE_DELIMETER
             << yearBorn << constants::FILE_DELIMETER;

        if (yearDied != constants::YEAR_DIED_DEFAULT_VALUE)
        {
            file << yearDied;
        }

        file << '\n';
    }
    else
    {
        return false;
    }

    file.close();
    return true;
}
bool ScientistRepository::addScientist(Scientist scientist)
{
    db.open();

    if (!db.isOpen())
    {
        return false;
    }

    QSqlQuery query(db);

    stringstream sqlQuery;
    sqlQuery << "INSERT INTO Scientists (name, Gender, BirthYear, DeathYear) VALUES ("
             << "'" << scientist.getName() << "', "
             << scientist.getSex() << ", "
             << scientist.getYearBorn() << ", "
             << scientist.getYearDied()
             << ")";

    if (!query.exec(QString::fromStdString(sqlQuery.str())))
    {
        return false;
    }

    db.close();

    return true;
}
bool ScientistRepository::editScientist(Scientist scientist, int id)
{
    db.open();

    if (!db.isOpen())
    {
        return false;
    }

    QSqlQuery query(db);

    stringstream sqlQuery;
    sqlQuery << "UPDATE Scientists SET "
             << "name = " << "'" << scientist.getName() << "', "
             << "sex = " << scientist.getSex() << ", "
             << "yearBorn = " << scientist.getYearBorn() << ", "
             << "yearDied = " << scientist.getYearDied() << ", "
             << "info = " << "'" << scientist.getInfo() << "' "
             << "WHERE id = " << id;

    if (!query.exec(QString::fromStdString(sqlQuery.str())))
    {
        return false;
    }

    db.close();

    return true;
}
Exemple #6
0
bool Service::updateScientist(Scientist scientistUpdate) {
    string bufferFirstName = scientistUpdate.getF(),
           bufferLastName  = "";

    seperateFirstLast(bufferFirstName, bufferLastName);

    Scientist nameUpdated(scientistUpdate.getId(),
                          bufferFirstName, bufferLastName,
                          scientistUpdate.getGender(),
                          scientistUpdate.getYearBorn(),
                          scientistUpdate.getYearDied(),
                          scientistUpdate.getKnownFor());

    return scientistRepository.updateScientist(nameUpdated);
}
bool Scientistrepository::addToDatabase(Scientist newScientist) {
    QSqlQuery query;

    QString fName  = QString::fromStdString((newScientist.getF()));
    QString lName  = QString::fromStdString((newScientist.getL()));
    bool    gender = newScientist.getGender();
    int     born   = newScientist.getYearBorn();
    int     died   = newScientist.getYearDied();
    QString known  = QString::fromStdString((newScientist.getKnownFor()));


    query.prepare("INSERT INTO Scientists (FirstName, LastName, Gender, Born, Died, KnownFor) "
                  "VALUES (:FirstName, :LastName, :Gender, :Born, :Died, :KnownFor)");
    query.bindValue(":FirstName", fName);
    query.bindValue(":LastName",  lName);
    query.bindValue(":Gender",    gender);
    query.bindValue(":Born",      born);
    query.bindValue(":Died",      died);
    query.bindValue(":KnownFor",  known);

    return query.exec();
}
bool Scientistrepository::updateScientist(Scientist scientistUpdate) {
    QSqlQuery query;

    int id         = scientistUpdate.getId();
    QString fName  = QString::fromStdString((scientistUpdate.getF()));
    QString lName  = QString::fromStdString((scientistUpdate.getL()));
    bool    gender = scientistUpdate.getGender();
    int     born   = scientistUpdate.getYearBorn();
    int     died   = scientistUpdate.getYearDied();
    QString known  = QString::fromStdString((scientistUpdate.getKnownFor()));

    query.prepare("UPDATE Scientists SET FirstName=:FirstName, LastName=:LastName, "
                  "Gender=:Gender, Born=:Born, Died=:Died, KnownFor=:KnownFor WHERE id=:id");
    query.bindValue(":id",        id);
    query.bindValue(":FirstName", fName);
    query.bindValue(":LastName",  lName);
    query.bindValue(":Gender",    gender);
    query.bindValue(":Born",      born);
    query.bindValue(":Died",      died);
    query.bindValue(":KnownFor",  known);

    return query.exec();
}