void RaceSet::init(){ qty=races.size(); next=0; rounds=0; if(qty==0){ std::cout<<"RaceSet construction failed: empty set of races"<<std::endl; throw 1; } genXML(); }
int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); //Command line arguments TCLAP::CmdLine cmd("GOBLET (c) 2012, International Livestock Research Institute (ILRI) \n Developed by Carlos Quiros ([email protected])", ' ', "1.0 (Beta 1)"); //Required arguments TCLAP::ValueArg<std::string> databaseArg("d","database","Database name",true,"","string"); TCLAP::ValueArg<std::string> calculationArg("c","calculation","Calculation to perform. For example: 'sum(DatasetA),sum(DatasetB)' ",true,"","string"); TCLAP::ValueArg<std::string> shapeArg("s","shapefile","Shapefile to use",true,"","string"); //Non required arguments TCLAP::ValueArg<std::string> pathArg("a","path","Path to database. Default .",false,".","string"); TCLAP::ValueArg<std::string> hostArg("H","host","Connect to host. Default localhost",false,"localhost","string"); TCLAP::ValueArg<std::string> portArg("P","port","Port number to use. Default 3306",false,"3306","string"); TCLAP::ValueArg<std::string> userArg("u","user","User. Default empty",false,"","string"); TCLAP::ValueArg<std::string> passArg("p","password","Passwork. Default no password",false,"","string"); TCLAP::ValueArg<std::string> descArg("S","descriptions","Descriptions for the calculations separated by coma. Default value is the calculation string",false,"","string"); TCLAP::ValueArg<std::string> fieldArg("f","fields","Field of shapefile to include in result separared by coma. Default value all",false,"all","string"); TCLAP::ValueArg<std::string> whereArg("w","where","WHERE statement for shapefile",false,"","string"); TCLAP::ValueArg<std::string> groupArg("g","group","GROUP fields statements for shapefile",false,"","string"); TCLAP::ValueArg<std::string> ouputArg("t","outputType","Output type: (h)uman readable or (c)omputer readable",false,"h","string"); TCLAP::ValueArg<std::string> outputFileArg("o","outputFile","If output type is computer, output xml file. Default ./executesql_out.xml",false,"./executesql_out.xml","string"); //Switches TCLAP::SwitchArg remoteSwitch("r","remote","Connect to remote host", cmd, false); cmd.add(databaseArg); cmd.add(calculationArg); cmd.add(ouputArg); cmd.add(shapeArg); cmd.add(whereArg); cmd.add(groupArg); cmd.add(pathArg); cmd.add(hostArg); cmd.add(portArg); cmd.add(userArg); cmd.add(passArg); cmd.add(descArg); cmd.add(fieldArg); cmd.add(outputFileArg); //Parsing the command lines cmd.parse( argc, argv ); //Getting the variables from the command bool remote = remoteSwitch.getValue(); QString path = QString::fromUtf8(pathArg.getValue().c_str()); QString dbName = QString::fromUtf8(databaseArg.getValue().c_str()); QString host = QString::fromUtf8(hostArg.getValue().c_str()); QString port = QString::fromUtf8(portArg.getValue().c_str()); QString userName = QString::fromUtf8(userArg.getValue().c_str()); QString password = QString::fromUtf8(passArg.getValue().c_str()); QString calculation = QString::fromUtf8(calculationArg.getValue().c_str()); QString format = QString::fromUtf8(ouputArg.getValue().c_str()); QString description = QString::fromUtf8(descArg.getValue().c_str()); QString shape = QString::fromUtf8(shapeArg.getValue().c_str()); QString cmdWhere = QString::fromUtf8(whereArg.getValue().c_str()); QString cmdgroup = QString::fromUtf8(groupArg.getValue().c_str()); QString strfields = QString::fromUtf8(fieldArg.getValue().c_str()); QString outfile = QString::fromUtf8(outputFileArg.getValue().c_str()); myDBConn con; QSqlDatabase mydb; if (!remote) { QDir dir; dir.setPath(path); if (con.connectToDB(dir.absolutePath()) == 1) { if (!dir.cd(dbName)) { gbtLog(QObject::tr("The database does not exists")); con.closeConnection(); return 1; } mydb = QSqlDatabase::addDatabase(con.getDriver(),"connection1"); } } else { mydb = QSqlDatabase::addDatabase("QMYSQL","connection1"); mydb.setHostName(host); mydb.setPort(port.toInt()); if (!userName.isEmpty()) mydb.setUserName(userName); if (!password.isEmpty()) mydb.setPassword(password); } mydb.setDatabaseName(dbName); if (!mydb.open()) { gbtLog(QObject::tr("Cannot open database")); con.closeConnection(); return 1; } else { QTime procTime; procTime.start(); QString sql; QSqlQuery qry(mydb); if (!shapeFileExists(shape,mydb)) { gbtLog(QObject::tr("The shapefile does not exists")); mydb.close(); con.closeConnection(); return 1; } QStringList sfields; sfields = getFields(strfields,shape,mydb); if (constructSQL(calculation,sql,mydb,shape,sfields,cmdWhere,cmdgroup,description)) { gbtLog(QObject::tr("Error in calculation.")); mydb.close(); con.closeConnection(); return 1; } //gbtLog(sql); if (!qry.exec(sql)) { gbtLog(QObject::tr("Error in calculation.")); gbtLog(qry.lastError().databaseText()); mydb.close(); con.closeConnection(); return 1; } if (format == "h") printResult(qry); else genXML(outfile,qry); //---------------Print or save result------------------------------- int Hours; int Minutes; int Seconds; int Milliseconds; Milliseconds = procTime.elapsed(); Hours = Milliseconds / (1000*60*60); Minutes = (Milliseconds % (1000*60*60)) / (1000*60); Seconds = ((Milliseconds % (1000*60*60)) % (1000*60)) / 1000; if (format == "h") gbtLog("Finished in " + QString::number(Hours) + " Hours," + QString::number(Minutes) + " Minutes and " + QString::number(Seconds) + " Seconds."); mydb.close(); con.closeConnection(); return 0; } return 0; }