Changeset faed520 in observatorio for simulacion/SimEscenariosEconomicos
- Timestamp:
- Jan 16, 2015, 2:36:56 PM (9 years ago)
- Branches:
- simulacion
- Children:
- d7bfb1f
- Parents:
- e481d13
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
simulacion/SimEscenariosEconomicos/queries.C
rbf76088 rfaed520 197 197 } 198 198 199 std::string getSupplierName(const std::string & rif) {200 201 AutoConnection conn;202 203 DBQuery query(conn);204 205 StrQuery strQuery;206 207 strQuery.addSelect(DB::NAME);208 strQuery.addFrom(DB::SUPPLIER_TABLE_NAME);209 strQuery.addWhere(op(DB::RIF, singleQuote(rif)));210 211 if (not query.exec(strQuery)) {212 213 std::string msg = "Cannot execute query: " + std::string(strQuery);214 215 throw std::logic_error(msg);216 }217 218 if (not query.next()) {219 220 std::stringstream msg;221 222 msg << "Supplier with R.I.F. = " << rif223 << " does not exists in database";224 225 throw std::domain_error(msg.str());226 }227 228 return query.getValue(0);229 }230 231 199 void listInputs(const size_t & inputId, const std::string & year, 232 200 List <InputInfo> & inputs) { … … 290 258 query.clear(); 291 259 292 std::string sum = "SUM(" + concat(DB::INPUT_SUPPLIER_TABLE_NAME, 293 DB::BOUGHT_QUANTITY) 294 + ") AS " + 295 concat(DB::INPUT_SUPPLIER_TABLE_NAME, 296 DB::BOUGHT_QUANTITY, '_'); 297 298 std::string strquery = 299 "SELECT " + 300 concatas(DB::SUPPLIER_TABLE_NAME, DB::RIF) + ',' + 301 concatas(DB::SUPPLIER_TABLE_NAME, DB::NATIONALITY) + ',' + sum + 302 " FROM " + 303 DB::INPUT_SUPPLIER_TABLE_NAME + ',' + 304 DB::SUPPLIER_TABLE_NAME + 305 " WHERE " + 306 op(concat(DB::INPUT_SUPPLIER_TABLE_NAME, DB::YEAR_R), year) + " AND " + 307 op(concat(DB::INPUT_SUPPLIER_TABLE_NAME, DB::INPUT_ID), 308 idToStr(inputId)) + " AND " + 309 op(concat(DB::INPUT_SUPPLIER_TABLE_NAME, DB::SUPPLIER_ID), 310 concat(DB::SUPPLIER_TABLE_NAME, DB::ID)) + 311 " GROUP BY " + 312 concat(DB::SUPPLIER_TABLE_NAME, DB::RIF, '_') + ',' + 313 concat(DB::SUPPLIER_TABLE_NAME, DB::NATIONALITY, '_'); 314 315 316 if (not query.exec(strquery)) { 317 318 std::string msg = "Cannot execute query: " + strquery; 319 320 throw std::logic_error(msg); 321 } 260 strQuery.addSelect(concatas(DB::SUPPLIER_TABLE_NAME, DB::RIF)); 261 strQuery.addSelect(concatas(DB::SUPPLIER_TABLE_NAME, DB::NAME)); 262 strQuery.addSelect(concatas(DB::SUPPLIER_TABLE_NAME, DB::NATIONALITY)); 263 strQuery.addSelect(concatas(DB::INPUT_SUPPLIER_TABLE_NAME, 264 DB::BOUGHT_QUANTITY)); 265 266 strQuery.addFrom(DB::INPUT_SUPPLIER_TABLE_NAME); 267 strQuery.addFrom(DB::SUPPLIER_TABLE_NAME); 268 269 strQuery.addWhere(op(concat(DB::INPUT_SUPPLIER_TABLE_NAME, DB::YEAR_R), 270 year)); 271 strQuery.addWhere(op(concat(DB::INPUT_SUPPLIER_TABLE_NAME, DB::INPUT_ID), 272 idToStr(inputId))); 273 strQuery.addWhere(op(concat(DB::INPUT_SUPPLIER_TABLE_NAME, DB::SUPPLIER_ID), 274 concat(DB::SUPPLIER_TABLE_NAME, DB::ID))); 275 276 strQuery.addOrderBy(concat(DB::SUPPLIER_TABLE_NAME, DB::RIF)); 277 278 279 if (not query.exec(strQuery)) { 280 281 std::string msg = "Cannot execute query: " + std::string(strQuery); 282 283 throw std::logic_error(msg); 284 } 285 286 std::string lastRif = ""; 287 288 inputInfo.quantity = 0.0; 322 289 323 290 while (query.next()) { 324 291 325 inputInfo.companyRif =292 std::string rif = 326 293 query.getValue(concat(DB::SUPPLIER_TABLE_NAME, DB::RIF, '_')); 327 inputInfo.companyName = getSupplierName(inputInfo.companyRif); 328 inputInfo.quantity = std::atof( 294 295 std::string nationality = query.getValue(concat(DB::SUPPLIER_TABLE_NAME, 296 DB::NATIONALITY, '_')); 297 298 inputInfo.quantity += std::atof( 329 299 query.getValue(concat(DB::INPUT_SUPPLIER_TABLE_NAME, 330 300 DB::BOUGHT_QUANTITY, '_'))); 331 301 332 char * nat = query.getValue(concat(DB::SUPPLIER_TABLE_NAME, 333 DB::NATIONALITY, '_')); 334 335 inputInfo.nationality = nat[0]; 302 if (nationality != "E" and rif == lastRif) { 303 continue; 304 } 305 306 lastRif = rif; 307 308 inputInfo.companyRif = rif; 309 310 inputInfo.companyName = 311 query.getValue(concat(DB::SUPPLIER_TABLE_NAME, DB::NAME, '_')); 312 313 inputInfo.nationality = nationality[0]; 336 314 337 315 long supplierId = std::atol(query.getValue(0));
Note: See TracChangeset
for help on using the changeset viewer.