Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RDBCscroll.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file RDBCscroll.C
1 // $Id: RDBCscroll.C,v 1.1.1.1 2004/02/18 20:58:02 dave Exp $
2 //
3 // This file is part of the RDBC
4 // Author: Valeriy Onuchin <onuchin@sirius.ihep.su>
6 //
7 // This example based on libodbc++/tests/scroll.cpp
8 //
10 // This should work with almost any almost-compliant database out there,
11 // providing it supports scrollable cursors.
12 // */
13 //
15 //
16 // Usage:
17 //
18 // root[] gSystem->Load("libRDBC.so"); // load library
19 // root[] .L RDBCscroll.C // load macro
20 // root[] RDBCscroll(dsn,usr,psw); // execute the function from macro
21 //
22 //
24 //
25 // REQUIREMENTS
26 //
27 // o You must have create table/drop table rights
28 //
29 
30 #ifndef __CINT__
31 // g++ -c -W RDBCscroll.C -I$ROOTSYS/include
32 
33 #include <TError.h>
34 #include <TString.h>
35 #include <TSQLDriverManager.h>
36 #include <TSQLConnection.h>
37 #include <TSQLDatabaseMetaData.h>
38 #include <TSQLResultSet.h>
39 #include <TSQLResultSetMetaData.h>
40 #include <TSQLPreparedStatement.h>
41 #include <TROOT.h>
42 #include <TInterpreter.h>
43 #endif // __CINT__
44 
45 const TString tableName = "odbcxx_test";
46 const Int_t tableRows = 1000;
47 
48 //__________________________________________________________________
50 {
51  // create table
52 
53  TSQLStatement* stmt = con->CreateStatement();
54  TString str = "create table ";
55  str += tableName;
56  str += "( id integer not null primary key, ";
57  str += "name varchar(40) not null)";
58  stmt->ExecuteUpdate(str.Data());
59 
60  str = "Table ";
61  str += tableName;
62  str += " created.";
63  printf("%s \n",str.Data());
64 
65  delete stmt;
66 }
67 
68 //___________________________________________________________________
70 {
71  // Drops the database objects.
72 
73  TSQLStatement* stmt=con->CreateStatement();
74 
75  TString str = "drop table ";
76  str += tableName;
77  stmt->ExecuteUpdate(str.Data());
78 
79  str = "Dropped table ";
80  str += tableName;
81  printf("%s\n",str.Data());
82 
83  delete stmt;
84 }
85 
86 //__________________________________________________________________
87 TString MakeName(Int_t n)
88 {
89  TString str;
90  char number[10];
91 
92  sprintf(number,"%d",n);
93  str = "This is row number ";
94  str += number;
95  return str;
96 }
97 
98 //__________________________________________________________________
100 {
101  //
102 
103  TString str = "insert into ";
104  str += tableName;
105  str += " (id,name) values(?,?)";
106 
107  TSQLPreparedStatement* pstmt = con->PrepareStatement(str.Data());
108 
109  for(int i=0; i<tableRows; i++) {
110  pstmt->SetInt(1,i);
111  pstmt->SetString(2,MakeName(i));
112  pstmt->ExecuteUpdate();
113  }
114 
115  delete pstmt;
116  con->Commit();
117 
118  printf("Inserted %d rows\n",tableRows);
119 }
120 
121 //__________________________________________________________________
123 {
124  // decide whether we should use a scroll insensitive
125  // or a scroll sensitive cursor
126 
127  int rstype;
128  int rsconc;
129  TString str;
130  TString name;
131 
132  TSQLDatabaseMetaData* md = con->GetMetaData();
133 
135  rstype = kTYPE_SCROLL_INSENSITIVE;
137  rstype = kTYPE_SCROLL_SENSITIVE;
138  } else {
139  printf("Skipping compare, data source does not support scrollable cursors\n");
140  return;
141  }
142 
144  // this is all we need
145  rsconc = kCONCUR_READ_ONLY;
146  } else {
147  rsconc = kCONCUR_UPDATABLE;
148  }
149 
150  TSQLStatement* stmt = con->CreateStatement(rstype,rsconc);
151  str = "select id,name from ";
152  str += tableName;
153  TSQLResultSet* rs = stmt->ExecuteQuery( str );
154  if(!rs) return;
155 
156  Assert(rs->IsBeforeFirst());
157  Assert(rs->First());
158  Assert(!rs->IsBeforeFirst());
159  Assert(rs->IsFirst());
160 
161  Assert(rs->Last());
162  Assert(rs->IsLast());
163  Assert(!rs->IsAfterLast());
164  rs->AfterLast();
165  Assert(rs->IsAfterLast());
166 
167  Assert(rs->Previous());
168  Assert(rs->IsLast());
169 
170  printf("Positioned on the last row (%d)\n", rs->GetRow());
171 
172  int i = tableRows;
173 
174  do {
175  i--;
176  name = MakeName(i);
177  Assert(rs->GetInt(1) == i);
178  Assert(rs->GetString(2) == name);
179  } while(rs->Previous());
180 
181  Assert(i==0);
182  Assert(rs->IsBeforeFirst());
183 
184  printf("%d rows checked with expected values.\n",tableRows);;
185 
186  delete stmt; //will kill rs
187 }
188 
189 //__________________________________________________________________
190 Int_t RDBCscroll( const Text_t* dsn,
191  const Text_t* usr,
192  const Text_t* pwd )
193 {
194  //
195 
197  TString str;
198 
199  // connect to error handler
200  TSQL::SetHandler("Throw(TSQLException*)");
201 
202  str = "Connecting to dsn="; str += dsn;
203  str += ", uid="; str += usr;
204  str += ", pwd="; str += pwd;
205  str += " ...";
206  printf("%s\n",str.Data());
207 
208  con = gSQLDriverManager->GetConnection(dsn,usr,pwd);
209 
210  if(!con) return -1; // failed to connect
211 
212  printf("\t\t\t DONE.\n");
213 
214  // we don't want autocommit
215  if( con->GetMetaData()->SupportsTransactions() ) {
216  con->SetAutoCommit(kFALSE);
217  }
218 
219  DropStuff(con);
220  CreateStuff(con);
221  Populate(con);
222  Compare(con);
223  con->Commit();
224 
225  DropStuff(con);
226  con->Commit();
227  con->Close();
228  return 0;
229 }
230 
231 //___________________________________________________________________
233 {
234  // handle exceptions
235 
236  TString str = e->GetMessage();
237  printf("SQL Error: %s\n",str.Data());
238 }