BRAT 2.4.5
Class index
Full class index
brahmlib
BRAHMS
ROOT page
// 
// BrDBQuery
//
// A SQL query object for use with the ROOT interface to a Relational
// DataBase Manager (RDBM). The main use of this class, is to convert
// an array of arbitery type into a C string for use in a
// TSQLServer::Query() call. This, the 'backslashes' in the equivelent
// C string must be escaped. 
//

//
// $Id: BrDbQuery.cxx,v 1.1.1.1 2001/06/21 14:55:16 hagel Exp $
// $Author: hagel $
// $Date: 2001/06/21 14:55:16 $
// $Copyright: 2000 Brahms Collabortion 
//

#include <TString.h>
#include <BrDbQuery.h>
#ifndef BRAT_BrDb
#include "BrDb.h"
#endif
#ifndef BRAT_BrException
#include "BrException.h"
#endif

//____________________________________________________________________
ClassImp(BrDbQuery); 

BrDbQuery   gSQLInsert("INSERT INTO ");
BrDbQuery   gSQLSelect("SELECT * FROM ");

//____________________________________________________________________
 BrDbQuery::BrDbQuery(void) 
  : TString()
{}

//____________________________________________________________________
 BrDbQuery::BrDbQuery(const Char_t* str) 
  : TString(str) 
{}

//____________________________________________________________________
BrDbQuery&
 BrDbQuery::Append(Float_t* arr, Int_t entries) 
{
  const int size = entries * sizeof(Float_t);
  TString::Append(BrDb::Addr2String(arr,size));
  return *this;
}

//____________________________________________________________________
BrDbQuery& 
 BrDbQuery::Append(Double_t* array, Int_t entries)
{
  const int size = entries * sizeof(Double_t);
  TString::Append(BrDb::Addr2String(array,size));
  return *this;
}

//____________________________________________________________________

BrDbQuery& 
 BrDbQuery::Append(Int_t* array, Int_t entries) 
{
  const int size = entries * sizeof(Int_t);
  TString::Append(BrDb::Addr2String(array,size));
  return *this;
}

//____________________________________________________________________

BrDbQuery& 
 BrDbQuery::Append(Long_t*  array, Int_t entries) 
{
  const int size = entries * sizeof(Long_t);
  TString::Append(BrDb::Addr2String(array,size));
  return *this;
}

//____________________________________________________________________

BrDbQuery& 
 BrDbQuery::Append(Byte_t* array, Int_t entries) 
{
  const int size = entries * sizeof(Byte_t);
  TString::Append(BrDb::Addr2String(array,size));
  return *this;
}

//____________________________________________________________________

//  BrDbQuery& 
//  BrDbQuery::Append(Bool_t* array, Int_t entries) 
//  {
//    const int size = entries * sizeof(Bool_t);
//    TString::Append(BrDb::Addr2String(array,size));
//    return *this;
//  }

//____________________________________________________________________

BrDbQuery& 
 BrDbQuery::Append(Char_t* str, Int_t entries)
{
  TString::Append(str);
  return *this;
}
  

//____________________________________________________________________
BrDbQuery&
 BrDbQuery::Append(const Char_t* str, Int_t entries) 
{
  TString::Append(str);
  return *this;
}

//____________________________________________________________________
BrDbQuery&
 BrDbQuery::Append(TString* str, Int_t entries) 
{
  TString::Append(*str);
  return *this;
}

//____________________________________________________________________
BrDbQuery*
 BrDbQuery::Create(const Char_t* tableName, const Char_t* format) 
{
  // Returns a pointer to BrDbQuery object, suitable for creating a
  // table in a database. 
  BrDbQuery* out = new BrDbQuery("CREATE TABLE ");
  out->Append(tableName);
  out->Append(" (id INT NOT NULL, ");
  out->Append(format);
  out->Append(", UNIQUE(id))");
  
  return out;
}

//____________________________________________________________________
BrDbQuery*
 BrDbQuery::Insert(const Char_t* tableName, const Char_t* values) 
{
  // Returns a pointer to BrDbQuery object, suitable for inserting an
  // entriy into an existing database table. 
  BrDbQuery* out = new BrDbQuery("INSERT INTO ");
  out->Append(tableName);
  out->Append(" VALUES(");
  out->Append(values);
  out->Append(")");
  return out;
}

//____________________________________________________________________
BrDbQuery*
 BrDbQuery::Select(const Char_t* tableName, 
		  const Char_t* fields, 
		  const Char_t* condition) 
{
  BrDbQuery* query = new BrDbQuery("SELECT ");
  if (!fields || strlen(fields) <= 0)
    query->Append("*");
  else 
    query->Append(fields);
  query->Append(" FROM ");
  query->Append(tableName);
  if (condition && strlen(condition) > 0) {
    query->Append(" WHERE ");
    query->Append(condition);
  }
  
  return query;
}
  
//____________________________________________________________________
BrDbQuery*
 BrDbQuery::Update(const Char_t* tableName, 
		  const Char_t* values, 
		  const Char_t* condition) 
{
  BrDbQuery* query = new BrDbQuery("UPDATE ");
  if (!values || strlen(values) <= 0)
    throw new BrWarning("BrDbQuery::Update", 
			"Empty update query");

  query->Append(tableName);
  query->Append(" SET ");
  query->Append(values);

  if (condition && strlen(condition) > 0) {
    query->Append(" WHERE ");
    query->Append(condition);
  }
  
  return query;
}

#ifdef BRATDB_USE_VARIADIC
//____________________________________________________________________
BrDbQuery*
 BrDbQuery::Create(const Char_t* table, 
		  const Char_t* format ...) 
{
  va_list ap;
  va_start(ap,format);
  Char_t buf[1024];
  vsprintf(buf, format, ap);
  va_end(ap);
  return BrDbQuery::Create(table, buf);
}
//
//____________________________________________________________________
BrDbQuery*
 BrDbQuery::Insert(const Char_t* table, const Char_t* format ...) 
{
  va_list ap;
  va_start(ap,format);
  Char_t buf[1024];
  vsprintf(buf, format, ap);
  va_end(ap);
  return BrDbQuery::Insert(table, buf);
}
//
//____________________________________________________________________
BrDbQuery*
 BrDbQuery::Select(const Char_t* tableName, 
		  const Char_t* fields, 
		  const Char_t* format, ...) 
{
  va_list ap;
  va_start(ap,format);
  Char_t buf[1024];
  vsprintf(buf, format, ap);
  va_end(ap);
  return BrDbQuery::Select(tablename,fields,buf);
}
//
//____________________________________________________________________
BrDbQuery*
 BrDbQuery::Update(const Char_t* tableName, 
		  const Char_t* values, 
		  const Char_t* format, ...) 
{
  va_list ap;
  va_start(ap,format);
  Char_t buf[1024];
  vsprintf(buf, format, ap);
  va_end(ap);
  return BrDbQuery::Select(tablename,values,buf);
}
//
#endif   

//____________________________________________________________________
BrDbQuery 
BrDbQuery::operator+(BrDbQuery& rhs) 
{
  // Add two BrDbQuery's
  BrDbQuery out;
  out.Append(this->Data());
  out.Append(" ");
  out.Append(rhs.Data());
  return out;
}

//____________________________________________________________________
BrDbQuery& 
BrDbQuery::operator+=(BrDbQuery& rhs) 
{
  // Append another query. 
  Append(" ");
  Append(rhs.Data());
  return *this;
}




//
// $Log: BrDbQuery.cxx,v $
// Revision 1.1.1.1  2001/06/21 14:55:16  hagel
// Initial revision of brat2
//
// Revision 1.5  2001/06/05 18:41:03  cholm
// Removed BrDbInc.h an all references to it
//
// Revision 1.4  2000/11/23 01:31:56  brahmlib
// Changed the returned object from operator+.
//
// Revision 1.3  2000/05/10 15:56:13  nbi
// Added the classes BrRunsDb, BrGeometriesDb, changed some code, bug
// corrections , and so on. It's almost there ;-)
//
//

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