void ComputerRepository::add(Computer &comp) { auto query = SQLConnection::getInstance()->getQuery(); query->prepare("INSERT INTO computers (name, type, build_year, built) VALUES (?,?,?,?)"); query->addBindValue(QString::fromStdString(comp.getName())); query->addBindValue(QString::fromStdString(comp.getType())); query->addBindValue(comp.getBuildYear()); query->addBindValue(comp.getBuilt()); if(!query->exec()) throw std::runtime_error(query->lastError().text().toStdString()); }
void ComputerRepository::update(Computer &comp, Computer &replace) { auto query = SQLConnection::getInstance()->getQuery(); query->prepare("UPDATE computers SET name = ?, type = ?, build_year = ?, built = ? WHERE id = ?"); query->addBindValue(QString::fromStdString(replace.getName())); query->addBindValue(QString::fromStdString(replace.getType())); query->addBindValue(replace.getBuildYear()); query->addBindValue(replace.getBuilt()); query->addBindValue(comp.getID()); if(!query->exec()) throw std::runtime_error(query->lastError().text().toStdString()); }
void ComputerRepository::remove(Computer &comp) { auto query = SQLConnection::getInstance()->getQuery(); query->prepare("DELETE FROM computers WHERE id = ?"); query->addBindValue(comp.getID()); if(!query->exec()) throw std::runtime_error(query->lastError().text().toStdString()); query->clear(); // Also clean all relations to other computers query->prepare("DELETE FROM scientist_computer WHERE computer_id = ?"); query->addBindValue(comp.getID()); if(!query->exec()) throw std::runtime_error(query->lastError().text().toStdString()); }
int QSqlQuery_addBindValue(lua_State* const state) { auto self = lua::get<QSqlQuery*>(state, 1); if (lua_gettop(state) == 2) { // void addBindValue(const QVariant & val) self->addBindValue( lua::get<const QVariant&>(state, 2) ); } else { // void addBindValue(const QVariant & val, QSql::ParamType paramType = QSql::In) self->addBindValue( lua::get<const QVariant&>(state, 2), lua::get<QSql::ParamType>(state, 3) ); } return 0; }
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()); }
//Searches for Computers after the parameters selected vector<Computer> ComputerRepository::search(ComputerFields::Field field, size_t rows, string search) { vector<Computer> ret; auto query = SQLConnection::getInstance()->getQuery(); QString search_field = ComputerFields::toField(field); query->prepare("SELECT * FROM computers WHERE " + search_field + " LIKE '%'|| ? ||'%' LIMIT " + QString::fromStdString(to_string(rows))); query->addBindValue(QString::fromStdString(search)); if(!query->exec()) throw std::runtime_error(query->lastError().text().toStdString()); while(query->next()) { ret.push_back(getComputer(query)); } return ret; }
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; }
/*! \overload Binds the placeholder with type \c QSql::In. */ void QSqlQuery::addBindValue( const QVariant& val ) { addBindValue( val, QSql::In ); }