// insert_thresholds.C
// This is a support file. This was used to insert some standard threshold values
// in the standardtest_thresholds table. This particular file sets the threshold
// values of all the parameters to 0.5
// This performs only insertion and no updation. SO if this is run without deleting 
// the old stuff from the tables then it simply adds more stuff to the table???

#include <TSQLServer.h>
#include <TSQLRow.h>
#include <TSQLResult.h> 
#include <TCanvas.h>
#include <TFrame.h>
#include <TH1.h>
#include <string.h>
#include <iostream.h>
#include <stdio.h>

void insert_thresholds(){

  // This connects to the database
   char *connection="oracle://sage/SLAC_TCP";
   char *user="glast_data";
   char *pass="flight06";

  // Connect to oracle server
  TSQLServer *db=TSQLServer::Connect(connection, user, pass);
  
  // Structure to hold the systest_id and the info from the metadata table
  struct Aaa{
    int systest_id;
    int info;
    int standardtest_id;
  };

  struct Aaa *meta;

  // Before insertion into the thresholds table get the max threshold_id
  TSQLResult *r = db->Query("select max(threshold_id) from standard_test_thresholds");
  TSQLRow *rowr = r->Next();
  int threshold_id = std::atoi(rowr->GetField(0));
  delete r;
  delete rowr;

  // Get the number of entries in the metadata
  TSQLResult *s = db->Query("select count(meta_id) from metadata");    
  TSQLRow *rows = s->Next();
  int num_meta = std::atoi(rows->GetField(0));  
  delete s;
  delete rows;
  
  // Assign the array size
  meta = (struct Aaa *)malloc(num_meta*sizeof(struct Aaa));


  const char *meta_query = "select m.systest_id, m.info, s.standardtest_id from metadata m, system_tests s where m.systest_id = s.systest_id";

  // Now we begin inserting into the thresholds table
  
  // To get systest_id and info
  TSQLResult *res=db->Query(meta_query);   
  TSQLRow *row=res->Next();      
  int i =0;

  // Store these values in the array
  do{

    TString systestid = row->GetField(0);
    TString info = row->GetField(1);
    TString standardtestid = row->GetField(2);
    
    meta[i].systest_id = std::atoi(systestid.Data());
    meta[i].info = std::atoi(info.Data());
    meta[i].standardtest_id = std::atoi(standardtestid.Data());

    i++;

  }while(res->Next());

  for(i=0; i<num_meta; i++){

    std::cout<<"Systestid :"<<meta[i].systest_id<<endl;
    std::cout<<"Info :"<<meta[i].info<<endl;
    std::cout<<"Standardtest_id :"<<meta[i].standardtest_id<<endl<<endl;
    
  }// end for
  
  // Insert threshold_id, systest_id and info into the thresholds table
  const char *temp_insert_thresholds = "insert into standard_test_thresholds(threshold_id, systest_id, standardtest_id, info, info_value) values(%d, %d, %d, %d, %s)";
  
  
  for(i=0; i<num_meta; i++){
    
    threshold_id++;
    char *insert_thresholds;
    insert_thresholds = new char[4096];
    std::sprintf(insert_thresholds, temp_insert_thresholds, threshold_id, meta[i].systest_id, meta[i].standardtest_id, meta[i].info, "0.5");
    
    TSQLResult *insert_thresholds_res = db->Query(insert_thresholds);    
    TSQLResult *com = db->Query("commit");
    delete insert_thresholds_res;
    delete com;
    delete[] insert_thresholds;


  }// end for 

}// end of insert_thresholds
























