Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RDBCTestString.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file RDBCTestString.C
1 
2 #include <TError.h>
3 #include <TString.h>
4 #include <TFile.h>
5 #include <TF1.h>
7 #include <RDBC/TSQLConnection.h>
9 #include <RDBC/TSQLResultSet.h>
13 #include <RDBC/TSQLTypes.h>
14 extern "C" {
15 #include <stdlib.h>
16 };
17 //___________________________________________________________________
18 Int_t RDBCTestInt(const Text_t* dsn,
19  const Text_t* usr="",
20  const Text_t* pwd="")
21 {
22  // Open a connection...
23  TSQLConnection* myConnection = NULL;
24  if(usr!="" && pwd !=""){
25  if(getenv("VERBOSE"))
26  printf( "connecting with: dsn= %s usr=%s pwd=%s\n",dsn,usr,pwd);
27  myConnection = TSQLDriverManager::GetConnection( dsn, usr, pwd );
28  } else{
29  if(getenv("VERBOSE"))
30  printf( "connecting with: dsn= %s \n",dsn);
31  myConnection = TSQLDriverManager::GetConnection( dsn );
32  }
33 
34  if(!myConnection) {
35  printf( "failed to connect: dsn= %s usr=%s pwd=%s\n",dsn,usr,pwd);
36  printf("exiting...\n");
37  return -1; // return on error
38  }else
39  printf("connected!!!\n");
40  TSQLStatement* stmt = myConnection->CreateStatement();
41 
42  stmt->ExecuteUpdate( "drop table string_table" );
43  stmt->ExecuteUpdate( "create table string_table (a_string varchar(255) not null)" );
44  TSQLPreparedStatement* pstmt =
45  myConnection->PrepareStatement("insert into string_table (a_string) values(?)");
46 
47  Text_t * some_string = "a short string";
48  pstmt->SetString(1,some_string);
49  pstmt->ExecuteUpdate("");
50 
51  some_string = "a much much longer string that may overrun internal buffers if the odbc drivers are not written correctly";
52  // 123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_12345 less than 255 chars so its legal!
53  pstmt->SetString(1,some_string);
54  pstmt->ExecuteUpdate("");
55 
56  TSQLResultSet* rs = stmt->ExecuteQuery("select a_string from string_table order by a_string");
57  rs->Next(); // goto the first row
58  TString my_string = rs->GetString(1);
59  printf ("retrieved: %s\n",my_string.Data() );
60  rs->Next(); // goto the next row
61  my_string = rs->GetString(1);
62  printf ("retrieved: %s\n",my_string.Data());
63 
64 
65  myConnection->Close();
66  return 0;
67 }
68 
69 //___________________________________________________________________
71 {
72  // handle exceptions
73 
74  TString str = e->GetMessage();
75  printf("SQL Error: %s\n",str.Data());
76 }
77 
78 
80 #ifdef STANDALONE
81 
82 #include <TROOT.h>
83 #include <TSystem.h>
84 #include <iostream>
85 
86 //---- Main program ------------------------------------------------------------
87 
88 TROOT root("RDBCTestInt","Test RDBC TSQLDriverManager and TSQLConnection");
89 
90 int main(int argc, char **argv)
91 {
92 
93  gSystem->Load("libRDBC");
94  Int_t ret = -1;
95 
96  if(argc < 2 || argc > 4){
97  printf ("usage: RDBCTestInt dsn [usr] [password]\n");
98  return ret;
99  }
100 
101  if(argc==2)
102  ret=RDBCTestInt(argv[1]);
103  if(argc==3)
104  ret=RDBCTestInt(argv[1],argv[2]);
105  if(argc==4)
106  ret=RDBCTestInt(argv[1],argv[2],argv[3]);
107 
108  return ret;
109 }
110 #endif