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()); }
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()); }
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; }