void Database::add(Scientist scientist) { QSqlQuery query(connectDatabase()); query.prepare("INSERT INTO Scientists (Name, Gender, YearOfBirth, YearOfDeath) VALUES (:Name, :Gender, :YearOfBirth, :YearOfDeath)"); query.bindValue(":Name", QString::fromStdString(scientist.getName())); query.bindValue(":Gender", QString::fromStdString(scientist.getGender())); query.bindValue(":YearOfBirth", QString::number(scientist.getBirthYear())); query.bindValue(":YearOfDeath", QString::number(scientist.getDeathYear())); query.exec(); }
std::vector<Computer> ScientistRepository::queryComputersByScientist(Scientist scientist) { vector<Computer> computers; db.open(); if (!db.isOpen()) { return computers; } QSqlQuery query(db); stringstream sqlQuery; sqlQuery << "SELECT s.* FROM ScientistComputerConnections scc "; sqlQuery << "JOIN Computers c "; sqlQuery << "ON c.id = scc.computerId "; sqlQuery << "WHERE scc.scientistId = " << scientist.getId(); query.exec(QString::fromStdString(sqlQuery.str())); while (query.next()) { int id = query.value("id").toUInt(); string name = query.value("name").toString().toStdString(); enum computerType type = utils::intToComputerType(query.value("type").toInt()); int yearBuilt = query.value("yearBuilt").toInt(); computers.push_back(Computer(id, name, type, yearBuilt)); } return computers; }
void ComputerRepository::unlink(Computer &c, Scientist &s) { auto query = SQLConnection::getInstance()->getQuery(); query->prepare("DELETE FROM scientist_computer WHERE scientist_id = ? AND computer_id = ?"); query->addBindValue(s.getID()); query->addBindValue(c.getID()); if(!query->exec()) throw std::runtime_error(query->lastError().text().toStdString()); }
void ComputerRepository::link(Computer &c, Scientist &s) { auto query = SQLConnection::getInstance()->getQuery(); query->prepare("INSERT INTO scientist_computer (scientist_id, computer_id) VALUES (?,?)"); query->addBindValue(s.getID()); query->addBindValue(c.getID()); if(!query->exec()) throw std::runtime_error(query->lastError().text().toStdString()); }
vector<Scientist> ScientistRepository::queryScientists(QString sqlQuery) { vector<Scientist> scientists; db.open(); if (!db.isOpen()) { return scientists; } QSqlQuery query(db); if (!query.exec(sqlQuery)) { return scientists; } while (query.next()) { int id = query.value("id").toUInt(); string name = query.value("name").toString().toStdString(); int gender = query.value("Gender").toInt(); enum sexType sex; if (gender == 1) sex = sexType::male; else sex = sexType::female; int yearBorn = query.value("BirthYear").toInt(); int yearDied = query.value("DeathYear").toInt(); scientists.push_back(Scientist(id, name, sex, yearBorn, yearDied)); } db.close(); for (unsigned int i = 0; i < scientists.size(); i++) { Scientist currentScientist = scientists.at(i); currentScientist.setComputers(queryComputersByScientist(currentScientist)); } return scientists; }
void MainWindow::displayAllScientists(QVector<Scientist> scientists) { ui->listOfSci->setSortingEnabled(false); canEdit = false; ui->listOfSci->clearContents(); ui->listOfSci->setRowCount(scientists.size()); for (int row = 0; row < scientists.size(); row++) { Scientist currentScientist = scientists.at(row); QString id = currentScientist.returnID(); QString gender = currentScientist.returnSex(); QString name = currentScientist.returnName(); QString dateBirth = currentScientist.dateofBirth(); QString dateDeath = currentScientist.dateofDeath(); QString description = currentScientist.returnDescription(); ui->listOfSci->setItem(row, 0, new QTableWidgetItem(id)); ui->listOfSci->setItem(row, 1, new QTableWidgetItem(name)); ui->listOfSci->setItem(row, 2, new QTableWidgetItem(gender)); ui->listOfSci->setItem(row, 3, new QTableWidgetItem(dateBirth)); ui->listOfSci->setItem(row, 4, new QTableWidgetItem(dateDeath)); ui->listOfSci->setItem(row, 5, new QTableWidgetItem(description)); } canEdit = true; ui->listOfSci->setSortingEnabled(true); return; }
void View::populateScientist(Scientist& guy){ string name; bool gender; QString fact; askName(name); askGender(gender); QDate doB = askDateOfBirth(); QDate doD = askDateOfDeath(); askFact(fact); guy.setName(QString::fromStdString(name)); guy.setGender(gender); guy.setdoB(doB); guy.setdoD(doD); guy.setFact(fact); return; }
vector<Scientist> ScientistRepository::queryScientists(QString sqlQuery) { vector<Scientist> scientists; db.open(); if (!db.isOpen()) { return scientists; } QSqlQuery query(db); if (!query.exec(sqlQuery)) { return scientists; } while (query.next()) { int id = query.value("id").toUInt(); string name = query.value("name").toString().toStdString(); enum sexType sex = utils::intToSex(query.value("sex").toInt()); int yearBorn = query.value("yearBorn").toInt(); int yearDied = query.value("yearDied").toInt(); string info = query.value("info").toString().toStdString(); Scientist sci(id, name, sex, yearBorn, yearDied); sci.setInfo(info); scientists.push_back(sci); } db.close(); for (unsigned int i = 0; i < scientists.size(); i++) { Scientist currentScientist = scientists.at(i); currentScientist.setComputers(queryComputersByScientist(currentScientist)); } return scientists; }
bool ScientistRepository::removeScientist(Scientist scientist) { db.open(); QSqlQuery query(db); stringstream sqlQuery; sqlQuery << "DELETE FROM Scientists WHERE id = " << scientist.getId(); bool success = query.exec(QString::fromStdString(sqlQuery.str())); db.close(); return success; }
vector<Computer> ComputerRepository::byScientist(Scientist &s) { vector<Computer> ret; auto query = SQLConnection::getInstance()->getQuery(); // Select just the scientists.* to not get id conflicts. TODO: rename the id query->prepare("SELECT computers.* FROM scientist_computer " "INNER JOIN computers ON computers.id = scientist_computer.computer_id " "WHERE scientist_id = ?"); query->addBindValue(s.getID()); if(!query->exec()) throw std::runtime_error(query->lastError().text().toStdString()); while(query->next()) { ret.push_back(getComputer(query)); } return ret; }
// Nokkur föll sem gera samanburð á tveimur elementum, skila true ef í réttri röð bool name_order(Scientist n1, Scientist n2) { bool order = false; if (n1.get_last() < n2.get_last()) { order = true; } else if (n1.get_last() == n2.get_last() && n1.get_first() <= n2.get_first()) { order = true; } return order; }
void ScientistComputerConnectionsRepository::populateScientistList(std::list<Scientist> &scientistList, QSqlQuery query) { while(query.next()){ Scientist s = Scientist(); s.setId(query.value("ID").toInt()); s.setName(query.value("Name").toString().toStdString()); s.setDateOfBirth(query.value("DateOfBirth").toString().toStdString()); s.setDateOfDeath(query.value("DateOfDeath").toString().toStdString()); s.setGender(query.value("Gender").toInt()); scientistList.push_back(s); } }
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::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(); }
void MainWindow::displayScientist(vector<Scientist> scientists) { ui->table_scientists->clearContents(); ui->table_scientists->setRowCount(scientists.size()); for (unsigned int i = 0; i < scientists.size(); i++) { Scientist currentScientist = scientists.at(i); QString FirstName = QString::fromStdString(currentScientist.getFirstName()); QString LastName = QString::fromStdString(currentScientist.getLastName()); QString Sex = QString::fromStdString(currentScientist.getSex()); QString YearOfBirth = QString::fromStdString(currentScientist.getYearOfBirth()); QString YearOfDeath = QString::fromStdString(currentScientist.getYearOfDeath()); ui->table_scientists->setItem(i, 0, new QTableWidgetItem(FirstName)); ui->table_scientists->setItem(i, 1, new QTableWidgetItem(LastName)); ui->table_scientists->setItem(i, 2, new QTableWidgetItem(Sex)); ui->table_scientists->setItem(i, 3, new QTableWidgetItem(YearOfBirth)); ui->table_scientists->setItem(i, 4, new QTableWidgetItem(YearOfDeath)); } currentlyDisplayedScientists = scientists; }
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(); }
vector<Computer>Service::getAssociatedComputers(Scientist scientistDetails) { int idScientistToMatch = scientistDetails.getId(); return computerRepository.getAssociatedComputers(idScientistToMatch); }
void ConsoleUI::add_scientist() { Scientist s = Scientist(); string firstname_, lastname_, gender_, birth_, death_; clear_screen(); cout << readFileToString("../templates/commands.txt") << endl; cout << "Name of Scientist: [FirstName LastName]" << endl; cout << endl << " -> "; cin >> firstname_ >> lastname_; clear_screen(); cout << readFileToString("../templates/commands.txt") << endl; cout << "Gender of scientist: \n(1) male \n(2) female" << endl; cout << endl << " -> "; cin >> gender_; while ((gender_ != "1") && (gender_ != "2")) { clear_screen(); cout << readFileToString("../templates/commands.txt") << endl; cout << "Invalid input! Please try again:" << endl; cout << "Gender of scientist: \n(1) male \n(2) female" << endl; cout << endl << " -> "; cin >> gender_; } clear_screen(); cout << readFileToString("../templates/commands.txt") << endl; cout << "Birth year of scientist:" << endl; cout << endl << " -> "; cin >> birth_; while (!is_digits(birth_)) { clear_screen(); cout << readFileToString("../templates/commands.txt") << endl; cout << "Invalid input! Please try again:" << endl; cout << "Birth year of scientist:" << endl; cout << endl << " -> "; cin >> birth_; } clear_screen(); cout << readFileToString("../templates/commands.txt") << endl; cout << "Scientist's Year of death: " << endl; cout << endl << " -> "; cin >> death_; while (!is_digits(death_) || (atoi(death_.c_str()) < atoi(birth_.c_str()))) { clear_screen(); cout << readFileToString("../templates/commands.txt") << endl; if (!is_digits(death_)) { cout << "Invalid input! Please try again:" << endl; } else if (atoi(death_.c_str()) < atoi(birth_.c_str())) { cout << "Year of death should be AFTER year of birth! Please ry again:" << endl; } cout << "Scientist's Year of death:" << endl; cout << endl << " -> "; cin >> death_; } if (gender_ == "1") { s.set_gender("Male"); } else if (gender_ == "2") { s.set_gender("Female"); } fix_string(firstname_); fix_string(lastname_); s.set_firstname(firstname_); s.set_lastname(lastname_); s.set_birth(birth_); s.set_death(death_); dbService.addS(s); clear_screen(); cout << readFileToString("../templates/commands.txt") << endl; cout << "\t\t " << "Scientist has been added to the list!\n\n\n\n\n\n\n" << endl; }