BRAT 2.4.5
Class index
Full class index
brahmlib
BRAHMS
ROOT page
// 
// Class BrDbRevision
//
// Representation of a Revision inside and outside of Database.
// 

// $Author: videbaek $
// $Date: 2001/11/26 21:42:21 $
// $Copyright: Brahms collaboration
// $Id: BrDbRevision.cxx,v 1.6 2001/11/26 21:42:21 videbaek Exp $

#include <BrDbRevision.h>
#ifndef BRAT_BrException
#include "BrException.h"
#endif
#ifndef BRAT_BrDbPerson
#include "BrDbPerson.h"
#endif
class BrDb;
#ifndef BRAT_BrDb
#include "BrDb.h"
#endif

#ifndef WIN32 
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <ctime>
#else 
#include <stdlib.h>
#include <iostream.h>
#include <time.h>
#endif
ClassImp(BrDbRevision); // Revision Class for BRAHMS database

//____________________________________________________________________
const Char_t* BrDbRevision::kTableName = REVISION_NAME;

//____________________________________________________________________
 BrDbRevision::BrDbRevision(Int_t  parameterId,
			   Int_t  validstart,
			   Int_t  validend,
			   Int_t  fromstart,
			   Int_t  fromend,
			   Int_t  typeId,
			   Int_t  date,
			   Int_t  authorId,
			   //Bool_t canonical,
			   const Char_t* comment,
			   EDataType datatype,
			   Int_t   entries,
			   Long_t  bytes,
			   void*  address)
{
 
 fParameterID = parameterId;
  fTypeID      = typeId;
  fValidStart  = validstart;
  fValidEnd    = validend;
  fFromStart   = fromstart;
  fFromEnd     = fromend;
  fAuthorID    = authorId;
  fDate        = date;
  if (comment) 
    SetComment(comment);
  fEntries     = entries;
  fBytes       = bytes;
  fDataType    = datatype;
  SetArray(address);
}

//____________________________________________________________________
 BrDbRevision::BrDbRevision(BrDbRevision* rev) 
{
  fParameterID = rev->GetParameterID();
  fTypeID      = rev->GetTypeID();
  fValidStart  = rev->GetValidStart();
  fValidEnd    = rev->GetValidEnd();
  fFromStart   = rev->GetFromStart();
  fFromEnd     = rev->GetFromEnd();
  fAuthorID    = rev->GetAuthorID();
  fDate        = rev->GetDate();
  SetComment(rev->GetComment());
  fDataType    = rev->GetDataType();
  fEntries     = rev->GetEntries();
  fBytes       = rev->GetBytes();
  SetArray(rev->GetArray());
}

//____________________________________________________________________
 BrDbRevision* BrDbRevision::SingleInstance(TSQLRow* row)
{
  // Returns an instance of a BrDbRevision. User needs to store this
  // object imidiatly. 
  if (!row) return 0;
  BrDbRevision::EDataType datatype = (EDataType) strtol(row->GetField(10),NULL,0);

  BrDbRevision* revision = 
    new BrDbRevision(strtol(row->GetField(1),NULL,0),   // Parameter ID
		     strtol(row->GetField(2),NULL,0),   // Valid Start
		     strtol(row->GetField(3),NULL,0),   // Valid End
		     strtol(row->GetField(4),NULL,0),   // From Start
		     strtol(row->GetField(5),NULL,0),   // From End
		     strtol(row->GetField(6),NULL,0),   // Type ID
		     strtol(row->GetField(7),NULL,0),   // Date
		     strtol(row->GetField(8),NULL,0),   // Author
		     row->GetField(9),                 // comment 
		     datatype,
		     strtol(row->GetField(11),NULL,0)); // entries
  
  revision->SetDBID(strtol(row->GetField(0),NULL,0)); // ID
  Int_t bytes = row->GetFieldLength(12);
  revision->SetBytes(bytes); 


  switch (datatype) {
  case kShort:
    revision->SetArray(BrDb::String2Short(row->GetField(12), bytes));
    break;
  case kInt:
    revision->SetArray(BrDb::String2Int(row->GetField(12),   bytes));
    break;
  case kLong:
    revision->SetArray(BrDb::String2Long(row->GetField(12),   bytes));
    break;
  case kFloat:
    revision->SetArray(BrDb::String2Float(row->GetField(12), bytes));
    break;
  case kDouble:
    revision->SetArray(BrDb::String2Double(row->GetField(12),bytes));
    break;
  default:
     ;
  }
  return revision;
}


//____________________________________________________________________
 TObjArray* BrDbRevision::MultipleInstance(TSQLResult* res)
{
  // Returns an (1D) array of BrDbRevisions matching Query that made the
  // TSQLResult. User need to store this immediately. 
  Int_t count = (res) ? res->GetRowCount() : 0;
  TObjArray* table = new TObjArray(count);

  for (Int_t i = 0; i < count; i++)
    table->Add(BrDbRevision::SingleInstance(res->Next()));
  
  return table;
}

//____________________________________________________________________
 BrDbQuery* BrDbRevision::Create(void) 
{
  return BrDbQuery::Create(BrDbRevision::kTableName,
			   "parameterId INT NOT NULL, "
			   "validStart  INT NOT NULL, " 
			   "validEnd    INT NOT NULL, "
			   "fromStart   INT NOT NULL, " 
			   "fromEnd     INT NOT NULL, "
			   "typeId      INT NOT NULL, " 
			   "date        INT NOT NULL, " 
			   "authorId    INT NOT NULL, " 
			   "comment     VARCHAR(255) NOT NULL, " 
			   "datatype    INT NOT NULL, " 
			   "entries     INT NOT NULL, " 
			   "array       BLOB NOT NULL, " 
			   "INDEX(parameterId), "
			   "INDEX(validStart,validEnd), "
			   "INDEX(typeId), "
			   "INDEX(authorId), "
			   "INDEX(date)");
}

//____________________________________________________________________
 BrDbQuery* BrDbRevision::Insert(void) 
{
  SetDate(-1);
  
  if (fAuthorID < 0)
    fAuthorID = gPerson->GetDBID();

  // This fix to allow long arrays 
  TString value(Form("%d, %d, %d, %d, %d, %d, %d, %d, %d, '%s', %d, %d, '", 
		     GetDBID(),               // 0
		     fParameterID,            // 1
		     fValidStart,             // 2
		     fValidEnd,               // 3
		     fFromStart,              // 4 
		     fFromEnd,                // 5
		     fTypeID,                 // 6
		     fDate,                   // 7    
		     fAuthorID,               // 8
		     fComment,                // 9
		     fDataType,               // 10
		     fEntries));              // 11
  
  switch(fDataType) {
  case kShort:
    value += BrDb::Short2String(fArray, fBytes);
    break;
  case kInt:
    value += BrDb::Int2String(fArray,   fBytes);
    break;
  case kLong:
    value += BrDb::Long2String(fArray,   fBytes);
    break;
  case kFloat:
    value += BrDb::Float2String(fArray, fBytes);
    break;
  case kDouble:
    value+= BrDb::Double2String(fArray, fBytes);
    break;
  default:
    Warning("Insert","Should not happen %d",fDataType);
    ;
  }
  value += "'";
  return BrDbQuery::Insert(BrDbRevision::kTableName, value.Data());
}
  
//____________________________________________________________________
 void BrDbRevision::SetArray(void* address) 
{
  if (address)
    fArray  = (Byte_t*)address;
  else
    fArray  = 0;
}
  
//____________________________________________________________________
 void BrDbRevision::SetDate(Int_t unixdate) 
{
  if (unixdate < 0) 
    unixdate = time(NULL);
  if (unixdate < 0)
    throw new BrFatal("BrDbRevision::SetDate",
		      "Couldn't get system time");
  fDate = unixdate;
}

//____________________________________________________________________
 void BrDbRevision::SetComment(const Char_t* comment) 
{
  if (strlen(comment) > 254) {
    strncpy(fComment, comment, 254);
    fComment[254] = '0';
  }
  strcpy(fComment,comment);
}

//____________________________________________________________________
 void   BrDbRevision::Print(Option_t* option) const
{
  // print method
  //
 // Options:  
  //   B         Basic information 
  TString opt(option);
  opt.ToLower(); 
  
  // Basic information 

  cout  << "BrDbRevision " << endl;
  cout  << " ParameterId = "    << setw(4)  << fParameterID << endl;
  cout  << " RevisionTypeId = " << setw(4)  << fTypeID   << endl;
  cout  << " ValidStart     = " << setw(12) << fValidStart << endl;
  cout  << " ValidEnd       = " << setw(12) << fValidEnd << endl;
  cout  << " DataType       = " << setw(4)  << fDataType << endl;
  cout  << " Entries        = " << setw(4)  << fEntries << endl;
  cout  << " Bytes          = " << setw(4)  << fBytes   << endl;
  cout  << " Address        = " << setw(8)  << (Int_t) fArray << endl; 
}

This page automatically generated by script docBrat by Christian Holm

Copyright ; 2002 BRAHMS Collaboration <brahmlib@rcf.rhic.bnl.gov>
Last Update on by

Validate HTML
Validate CSS