Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-03 08:22:07

0001 
0002 #include <TError.h>
0003 #include <TString.h>
0004 #include <TFile.h>
0005 #include <TF1.h>
0006 #include <RDBC/TSQLDriverManager.h>
0007 #include <RDBC/TSQLConnection.h>
0008 #include <RDBC/TSQLDatabaseMetaData.h>
0009 #include <RDBC/TSQLResultSet.h>
0010 #include <RDBC/TSQLResultSetMetaData.h>
0011 #include <RDBC/TSQLPreparedStatement.h>
0012 #include <RDBC/TSQLCallableStatement.h>
0013 #include <RDBC/TSQLTypes.h>
0014 extern "C" {
0015 #include <stdlib.h>
0016 };
0017 //___________________________________________________________________
0018 Int_t RDBCTestInt(const Text_t* dsn,
0019                const Text_t* usr="",
0020                const Text_t* pwd="")
0021 {
0022    //    Open a connection...
0023    TSQLConnection* myConnection = NULL;
0024    if(usr!="" && pwd !=""){
0025      if(getenv("VERBOSE"))
0026        printf( "connecting with: dsn= %s usr=%s pwd=%s\n",dsn,usr,pwd);
0027      myConnection = TSQLDriverManager::GetConnection( dsn, usr,  pwd );
0028    } else{
0029      if(getenv("VERBOSE"))
0030        printf( "connecting with: dsn= %s \n",dsn);
0031      myConnection = TSQLDriverManager::GetConnection( dsn );
0032    }
0033 
0034   if(!myConnection) {
0035      printf( "failed to connect: dsn= %s usr=%s pwd=%s\n",dsn,usr,pwd);
0036      printf("exiting...\n");
0037      return -1;  // return on error
0038    }else
0039      printf("connected!!!\n");
0040    TSQLStatement* stmt = myConnection->CreateStatement();
0041 
0042    stmt->ExecuteUpdate( "drop table string_table" );
0043    stmt->ExecuteUpdate( "create table string_table (a_string varchar(255) not null)" );
0044    TSQLPreparedStatement* pstmt = 
0045      myConnection->PrepareStatement("insert into string_table (a_string) values(?)");
0046    
0047    Text_t *  some_string = "a short string";    
0048    pstmt->SetString(1,some_string);
0049    pstmt->ExecuteUpdate(""); 
0050    
0051    some_string = "a much much longer string that may overrun internal buffers if the odbc drivers are not written correctly";
0052    //             123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_12345  less than 255 chars so its legal!
0053    pstmt->SetString(1,some_string);
0054    pstmt->ExecuteUpdate(""); 
0055    
0056    TSQLResultSet* rs = stmt->ExecuteQuery("select a_string from string_table order by a_string");
0057    rs->Next(); // goto the first row
0058    TString  my_string = rs->GetString(1); 
0059    printf ("retrieved: %s\n",my_string.Data() );    
0060    rs->Next(); // goto the next row
0061    my_string = rs->GetString(1); 
0062    printf ("retrieved: %s\n",my_string.Data()); 
0063 
0064 
0065    myConnection->Close();
0066    return 0;
0067 }
0068 
0069 //___________________________________________________________________
0070 void Catch(TSQLException* e)
0071 { 
0072    // handle exceptions
0073    
0074    TString str = e->GetMessage();
0075    printf("SQL Error: %s\n",str.Data()); 
0076 }
0077 
0078 
0079 //////////////////////////// Main program ////////////////////////////////////
0080 #ifdef STANDALONE
0081 
0082 #include <TROOT.h>
0083 #include <TSystem.h>
0084 #include <iostream>
0085 
0086 //---- Main program ------------------------------------------------------------
0087 
0088 TROOT root("RDBCTestInt","Test RDBC TSQLDriverManager and TSQLConnection");
0089 
0090 int main(int argc, char **argv)
0091 {
0092 
0093    gSystem->Load("libRDBC");
0094    Int_t ret = -1;
0095 
0096    if(argc < 2 || argc > 4){
0097      printf ("usage: RDBCTestInt dsn [usr] [password]\n");
0098      return ret;
0099    }
0100 
0101    if(argc==2) 
0102      ret=RDBCTestInt(argv[1]);
0103    if(argc==3) 
0104      ret=RDBCTestInt(argv[1],argv[2]);
0105    if(argc==4)
0106      ret=RDBCTestInt(argv[1],argv[2],argv[3]);
0107 
0108    return ret;
0109 }
0110 #endif