#include #include #include #include #include #include #include #include #include #include "../DB/DBQueryCreator.h" #include "../DB/FERODBFieldPositions.h" #include "../Config/TpcConfig.h" #include "../Utilities/Stopwatch.h" #include "../Utilities/ProgressBar.h" #include "ocilib.hpp" using namespace std; using namespace ocilib; struct arguments { bool pExit, pdbUserSet, pdbPasswordSet, pdbConnectIdSet; string pUpdateTableName; bool pFile, pTable, pTest, pConnected, pSelectSingleField; vector pSelectCruList, pSelectFecList, pSelectBranchList, pSelectPartitionList, pSelectSideList; vector pSelectSectorList, pSelectTagList; string pSelectField, pdbUser, pdbPassword, pdbConnectId; vector pFileNameList; int pUpdateValueInt; float pUpdateValueFloat; }; struct arguments arguments; #include "DBTool.h" #include "Misc.h" #include "UpdateFromFile.h" const char *argp_program_version = "dbupdatefromfile"; const char *argp_program_bug_address = ""; static const char doc[]= "Program to modify content of the TPC FERO Configuration Data Base.\ \v==============================================================================\ \nExample: dbupdatefromfile --file=pedestal_values.txt -t2,4,6 -TPEDESTAL\ \n dbupdatefromfile --file=threshold_values.txt -t0,1 -TZSTHR\ \n=============================================================================="; #define OPT_TEST 0 #define OPT_P2DB 1 #define OPT_TESTDB 2 // .. up to 31 static const struct argp_option options[]={ {"p2", OPT_P2DB, 0, 0, "Connect to P2 data base in DCS network." , 0}, {"testdb", OPT_TESTDB, 0, 0, "Connect to test data base in CERN GPN." , 0}, {"user", 'u', "USER", 0, "Data base user (default tpcfero)." , 0}, {"connect_id", 'z', "CONNECT_ID", 0, "Oracle DB connect identifier (default alice_fero)." , 0}, {"password", 'P', "PASSWORD", 0, "Password for the data base (mandatory)." , 0}, {"newvalue", 'n', "NEWVALUE", 0, "New value for update." , 1}, {"tag", 't', "TAG1[,TAG2...]", 0, "Specify configuration ID (Tag)." , 1}, {"id", 0, 0, OPTION_ALIAS }, {"test", OPT_TEST, 0, 0, "Test only. No DB activity. SQL Commands are written to files." , 2}, {"table", 'T', "TABLENAME", 0, "Specify table (PEDESTAL, ZSTHR, ONCHANNEL, CMCORR, ITCORR1, ITCORR2)." , 4}, {"column", 'C', "COLUMNNAME", 0, "Specify column name for select or update." , 2}, {"col", 0, "COLUMNNAME", OPTION_ALIAS }, {"field", 0, "COLUMNNAME", OPTION_ALIAS }, {"file", 'F', "FNAME1[,FNAME2...]", 0, "Update using data in these files. Specify also tag." , 3}, {"side", 's', "SIDE1[,SIDE2]", 0, "Specify side of TPC (0,1). More values separated by comma." , 4}, {"sector", 'r', "SEC1[,SEC2...]", 0, "Specify sector of TPC (0...17). More values separated by comma." , 4}, {"partition", 'p', "P1[,P2...]", 0, "Specify partition (0...4). More values separated by comma." , 4}, {"cru", 'c', "CRU1[,CRU2]", 0, "Specify CRU (0,1). 0=Master, 1=Slave. More values separated by comma." , 4}, {"branch", 'b', "BR1[,BR2]", 0, "Specify FEC branch (0,1). More values separated by comma." , 4}, {"fec", 'f', "FEC1[,FEC2...]", 0, "Specify fec (0...12). More values separated by comma." , 4}, { 0 } }; static error_t parse_opt(int32_t key, char *arg, struct argp_state *state) { struct arguments *arguments = (struct arguments*)state->input; char *arg_copy; char *token; switch (key) { case OPT_P2DB: arguments->pdbUserSet = true; arguments->pdbUser = "tpcfero"; arguments->pdbConnectIdSet = true; arguments->pdbConnectId = "alice_fero"; arguments->pdbPasswordSet = true; arguments->pdbPassword = "tpc1234"; break; case OPT_TESTDB: arguments->pdbUserSet = true; arguments->pdbUser = "tpcconfig"; arguments->pdbConnectIdSet = true; arguments->pdbConnectId = "intdb11"; arguments->pdbPasswordSet = true; arguments->pdbPassword = "bollocks_88"; break; case 'u': arguments->pdbUserSet = true; arguments->pdbUser = arg; break; case 'z': arguments->pdbConnectIdSet = true; arguments->pdbConnectId = arg; break; case 'P': arguments->pdbPasswordSet = true; arguments->pdbPassword = arg; break; case 'C': arguments->pSelectSingleField = true; arguments->pSelectField = arg; break; case 'n': arguments->pUpdateValueInt = atoi(arg); arguments->pUpdateValueFloat = atof(arg); break; case 'T': if ( strcmp(arg, "PEDESTAL" ) == 0) arguments->pUpdateTableName = "pedestal"; if ( strcmp(arg, "pedestal" ) == 0) arguments->pUpdateTableName = "pedestal"; else if ( strcmp(arg, "ZSTHR" ) == 0) arguments->pUpdateTableName = "zsthr"; else if ( strcmp(arg, "zsthr" ) == 0) arguments->pUpdateTableName = "zsthr"; else if ( strcmp(arg, "ONCHANNEL" ) == 0) arguments->pUpdateTableName = "on_channel"; else if ( strcmp(arg, "onchannel" ) == 0) arguments->pUpdateTableName = "on_channel"; else if ( strcmp(arg, "ON_CHANNEL" ) == 0) arguments->pUpdateTableName = "on_channel"; else if ( strcmp(arg, "on_channel" ) == 0) arguments->pUpdateTableName = "on_channel"; else if ( strcmp(arg, "CMCORR" ) == 0) arguments->pUpdateTableName = "cmcorr"; else if ( strcmp(arg, "cmcorr" ) == 0) arguments->pUpdateTableName = "cmcorr"; else if ( strcmp(arg, "ITCORR1" ) == 0) arguments->pUpdateTableName = "itcorr1"; else if ( strcmp(arg, "itcorr1" ) == 0) arguments->pUpdateTableName = "itcorr1"; else if ( strcmp(arg, "ITCORR2" ) == 0) arguments->pUpdateTableName = "itcorr2"; else if ( strcmp(arg, "itcorr2" ) == 0) arguments->pUpdateTableName = "itcorr2"; arguments->pTable = true; break; case 't': arguments->pSelectTagList.clear(); arg_copy=strdup(arg); token=strtok(arg_copy,","); do { int32_t tag = atoi(token); arguments->pSelectTagList.push_back(tag); } while ((token=strtok(NULL,","))); free(arg_copy); break; case OPT_TEST: arguments->pTest = true; cout << "*Test mode. No DB activity. Output written to local file." << endl; break; case 'F': arguments->pFile = true; arg_copy=strdup(arg); token=strtok(arg_copy,","); do { arguments->pFileNameList.push_back(token); } while ((token=strtok(NULL,","))); free(arg_copy); break; case 's': arg_copy=strdup(arg); token=strtok(arg_copy,","); do { uint32_t side = atoi(token); if ( side > 1 ) { cout << "*Strange side: " << side << endl; } else { arguments->pSelectSideList.push_back(side); } } while ((token=strtok(NULL,","))); free(arg_copy); break; case 'r': arg_copy=strdup(arg); token=strtok(arg_copy,","); do { uint32_t sector = atoi(token); if ( sector > 17 ) { cout << "*Strange sector: " << sector << endl; } else { arguments->pSelectSectorList.push_back(sector); } } while ((token=strtok(NULL,","))); free(arg_copy); break; case 'p': arg_copy=strdup(arg); token=strtok(arg_copy,","); do { uint32_t partition = atoi(token); if ( partition > 4 ) { cout << "*Strange partition: " << partition << endl; } else { arguments->pSelectPartitionList.push_back(partition); } } while ((token=strtok(NULL,","))); free(arg_copy); break; case 'c': arg_copy=strdup(arg); token=strtok(arg_copy,","); do { uint32_t cru = atoi(token); if ( cru > 1 ) { cout << "*Strange CRU position: " << cru << endl; } else { arguments->pSelectCruList.push_back(cru); } } while ((token=strtok(NULL,","))); free(arg_copy); break; case 'b': arg_copy=strdup(arg); token=strtok(arg_copy,","); do { uint32_t branch = atoi(token); if ( branch > 1 ) { cout << "*Strange branch: " << branch << endl; } else { arguments->pSelectBranchList.push_back(branch); } } while ((token=strtok(NULL,","))); free(arg_copy); break; case 'f': arg_copy=strdup(arg); token=strtok(arg_copy,","); do { uint32_t fec = atoi(token); if ( fec > 9 ) { cout << "*Strange fec: " << fec << endl; } else { arguments->pSelectFecList.push_back(fec); } } while ((token=strtok(NULL,","))); free(arg_copy); break; default: return ARGP_ERR_UNKNOWN; } return 0; } static struct argp argp={options,parse_opt,0,doc}; // ============================================================================= // ============================================================================= int32_t check_args(int32_t argc, char** argv ) { // // Check all arguments given // TpcConfig tpcconf; // Default arguments arguments.pExit = false; arguments.pdbUserSet = false; arguments.pdbPasswordSet = false; arguments.pdbConnectIdSet = false; arguments.pTest = false; arguments.pConnected = false; arguments.pFile = false; arguments.pTable = false; arguments.pSelectSingleField = false; arguments.pSelectField = ""; arguments.pUpdateTableName = ""; arguments.pUpdateValueInt = -1; arguments.pUpdateValueFloat = -1.; argp_parse(&argp, argc, argv, 0, 0, &arguments); if (arguments.pExit) return 1; // // Check Commandline Parameters // if ( arguments.pdbUserSet == false ) { arguments.pdbUser = "tpcfero"; arguments.pdbUserSet = true; if (arguments.pTest == false) cout << "*No db Username set. Using default: " << arguments.pdbUser << endl; } if ( (arguments.pdbPasswordSet == false) && (arguments.pTest == false) ) { cout << "*No db Password set. Specify using \"-dbp\" option!" << endl; return 0; } if ( arguments.pdbConnectIdSet == false ) { arguments.pdbConnectId = "alice_fero"; arguments.pdbConnectIdSet = true; if (arguments.pTest == false) cout << "*No db Connect-String set. Using default: " << arguments.pdbConnectId << endl; } if ( arguments.pFile == true ) { if ( arguments.pSelectTagList.size() == 0 ) { cout << "*No Tag (Configuration ID) specified!" << endl; return 0; } if ( arguments.pFileNameList.size() == 0 ) { cout << "*No input file specified!" << endl; return 0; } if ( !arguments.pTable ) { cout << "*No Table name specified!" << endl; return 0; } } if ( arguments.pTable == true ) { if ( !arguments.pFile ) { cout << "*No File with new values specified!" << endl; return 0; } } return 1; } // ============================================================================= // ============================================================================= int main(int argc, char** argv ) { // TODO: Use EXIT_SUCCESS and EXIT_FAILURE if (!check_args(argc, argv)) exit(EXIT_FAILURE); if (arguments.pExit) exit(EXIT_SUCCESS); // Connect if ( !arguments.pTest ) { try { Environment::Initialize(); con.Open(arguments.pdbConnectId, arguments.pdbUser, arguments.pdbPassword); } catch(std::exception &ex) { cout << ex.what() << endl; exit(EXIT_FAILURE); } if ( con.IsServerAlive() ) { cout << "================================================================" << endl; cout << "=== Connected to: " << endl << con.GetServerVersion() << endl; cout << "=== Data base: " << con.GetDatabase() << endl; cout << "=== Instance: " << con.GetInstance() << endl; cout << "=== Service: " << con.GetService() << endl; cout << "=== Server: " << con.GetServer() << endl; cout << "=== Domain: " << con.GetDomain() << endl; cout << "=== Oracle version used for the connection: " << con.GetVersion() << endl; cout << "================================================================" << endl; arguments.pConnected = true; } else { cout << "Server is dead. Doing nothing..." << endl; exit(EXIT_FAILURE); } } // Do it if ( arguments.pFile && !updateULfromfile(arguments.pUpdateTableName) ) cerr << "*Error: Could not update values in table " << arguments.pUpdateTableName << " from file!" << endl; // Disconnect if ( !arguments.pTest && arguments.pConnected ) { try { Environment::Cleanup(); } catch(std::exception &ex) { cout << ex.what() << endl; exit(EXIT_FAILURE); } } return EXIT_SUCCESS; } // EOF