/** * Exécute une requête SQL (INSERT, UPDATE, ...) * @return Identifiant de l'entité insérée */ int Database::execute(Query &query, bool getInsertedId) { int id = 0; try { logger.log(Logger::DEBUG, "Database::execute " + query.get()); // Création de la requête préparée sql::PreparedStatement *preparedStatement; preparedStatement = this->connection->prepareStatement(query.get()); // Complétion de la requête for(map<int, int>::iterator it = query.intValues.begin(); it != query.intValues.end(); ++it) { preparedStatement->setInt(it->first, it->second); } for(map<int, string>::iterator it = query.stringValues.begin(); it != query.stringValues.end(); ++it) { preparedStatement->setString(it->first, it->second); } for(map<int, bool>::iterator it = query.booleanValues.begin(); it != query.booleanValues.end(); ++it) { preparedStatement->setBoolean(it->first, it->second); } // Exécution de la requête preparedStatement->execute(); delete preparedStatement; // Récupération de l'identifiant if(getInsertedId) { sql::Statement* statement = nullptr; statement = this->connection->createStatement(); sql::ResultSet* result = statement->executeQuery("SELECT LAST_INSERT_ID() AS id"); result->next(); id = result->getInt("id"); delete result; delete statement; logger.log(Logger::DEBUG, "last inserted id = " + to_string(id)); } } catch(sql::SQLException &e) { logger.log(Logger::INFO, "MySQL error code : " + to_string(e.getErrorCode())); logger.log(Logger::INFO, "SQLState : " + e.getSQLState()); logger.log(Logger::CRITICAL, e.what()); } return id; }
Row DbmsQueryLocal::get(Dir dir) { last_q = q; Row row(q->get(dir)); if (q->updateable() && row != Row::Eof) row.recadr = row.data[1].off(); // [1] to skip key verify(row.recadr >= 0); return row; }
/** * Exécute une requête SQL et renvoie le résultat * @return result Pointeur vers le résultat de la requête */ void Database::getResult(Query &query, sql::ResultSet* &result) { try { logger.log(Logger::DEBUG, "Database::getResult " + query.get()); sql::Statement *statement; statement = this->connection->createStatement(); result = statement->executeQuery(query.get()); delete statement; } catch(sql::SQLException &e) { logger.log(Logger::INFO, "MySQL error code : " + to_string(e.getErrorCode())); logger.log(Logger::INFO, "SQLState : " + e.getSQLState()); logger.log(Logger::CRITICAL, e.what()); } }
Query* Parser::E() { Query* par = new Query(SIGN_OR); par->add(T()); while (!token.compare("OR") || !token.compare("|")) { // T OR E if (!token.compare("OR")) match("OR"); else match("|"); par->add(T()); } if (par->size() == 1) { Query* ret = par->get(0); par->clear(); delete par; return ret; } return par; }
Query* Parser::T() { Query* par = new Query(SIGN_AND); par->add(S()); while (!token.compare("AND") || !token.compare("&")) { // S AND T if (!token.compare("AND")) match("AND"); else match("&"); par->add(S()); } if (par->size() == 1) { Query* ret = par->get(0); par->clear(); delete par; return ret; } return par; }
Handle<Value> Engine::runSync(Arguments const& args) { HandleScope scope; if (args.Length() < 1) { ThrowException(Exception::TypeError(String::New("first argument must be a osrm.Query"))); } if (!args[0]->IsObject()) { return ThrowException(Exception::TypeError(String::New("first argument must be a osrm.Query object"))); } Local<Object> obj = args[0]->ToObject(); if (obj->IsNull() || obj->IsUndefined() || !Query::constructor->HasInstance(obj)) { return ThrowException(Exception::TypeError(String::New("osrm.Query object expected for first argument"))); } Query *query = ObjectWrap::Unwrap<Query>(obj); Engine* machine = ObjectWrap::Unwrap<Engine>(args.This()); http::Reply osrm_reply; machine->this_->RunQuery(*query->get(), osrm_reply); return scope.Close(String::New(osrm_reply.content.c_str())); }