Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RDBCmysql.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file RDBCmysql.C
1 // $Id: RDBCmysql.C,v 1.1.1.1 2004/02/18 20:58:02 dave Exp $
2 //
3 // !!! not working yet !!!
4 // This is a simple test to be run against a mysql database
5 // Assure you have create table permissions before you run.
6 //
7 
8 #ifndef __CINT__
9 // g++ -c -Wall RDBCmysql.C -I$ROOTSYS/include -I../include
10 
11 #include <RDBC/TSQLDriverManager.h>
12 #include <RDBC/TSQLConnection.h>
13 #include <RDBC/TSQLResultSet.h>
16 
17 #endif // __CINT__
18 
19 
20 const char* testchars="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
21 
22 const int odbctestRows=5000;
23 
24 const int longtestRows=30;
25 const int longtestSize=123456;
26 
28 
29 //___________________________________________________________________
30 Bool_t feq(const double& a, const double& b)
31 {
32  //
33  return (fabs(a-b)<=a*0.0000001);
34 }
35 
37 class ValueGen1
38 {
39 private:
40  Int_t fCnt;
41  Int_t fMax;
42 
43 public:
44  ValueGen1(Int_t max) : fCnt(0), fMax(max) {}
46 
47  Bool_t Next() { return ((++fCnt) <= fMax); }
48  Int_t cnt() const {return fCnt; }
49 
50  signed char i1() const { return (signed char)(fCnt%256 - 128); }
51  short i2() const { return (short)(fCnt%65536 - 32768); }
52  int i3() const { return (int)(fCnt%16777215-8388608); }
53  int i4() const { return (int)fCnt; }
54  Long_t i5() const { return ((Long_t)fCnt)*1000000L; }
55  float f1() const { return ((float)fCnt); }
56  float f2() const { return ((float)fCnt/10); }
57  double f3() const { return ((double)fCnt/20);}
58  Long_t d1() const { return (((Long_t)fCnt)-fMax/2)*4567;}
59  TString s1() const { return TString(&testchars[fCnt%strlen(testchars)],1); }
60  TString s3() const { return this->s1(); }
61  TString s2() const { char buf[41];
62  snprintf(buf,41,"This is row number %d",fCnt);
63  return TString_C(buf); }
64  TString s4() const { return this->s2(); }
65 
66  Date dt() {
67  return Date((time_t)fCnt*10000);
68  }
69 
70  Time t() {
71  return Time((time_t)fCnt);
72  }
73 
74  Timestamp ts() {
75  return Timestamp((time_t)fCnt*10000);
76  }
77 //this generates the values for odbctest
78 };
79 
80 
82 class ValueGen2 {
83 private:
84  Int_t fCnt;
85  Int_t rows_;
86 
89 
90 public:
91  ValueGen2(Int_t rows)
92  :fCnt(0), rows_(rows) {}
94 
95  Bool_t Next() {
96  return ((++fCnt) <= rows_);
97  }
98 
99  int id() {
100  return fCnt;
101  }
102 
103  TBuffer* c() {
104  int len=strlen(testchars);
105  for(int i=0; i<longtestSize; i++) {
106  currentC[i]=testchars[(i+fCnt)%len];
107  }
109 #if !defined(ODBCXX_QT)
110  isstream* s=new isstream(currentC,longtestSize);
111 #else
112  QBuffer* s=new QBuffer();
113  s->open(IO_WriteOnly);
114  s->writeBlock(currentC,longtestSize);
115  s->close();
116  s->open(IO_ReadOnly);
117 #endif
118  return s;
119  }
120 
121  TBuffer* b() {
122  for(int i=0; i<longtestSize; i++) {
123  currentB[i]=((i+fCnt)%256);
124  }
125 #if !defined(ODBCXX_QT)
126  isstream* s=new isstream(currentB,longtestSize);
127 #else
128  QBuffer* s=new QBuffer();
129  s->open(IO_WriteOnly);
130  s->writeBlock(currentB,longtestSize);
131  s->close();
132  s->open(IO_ReadOnly);
133 #endif
134  return s;
135  }
136 
137 //this is for the BLOB/CLOB test
138 };
139 
140 //___________________________________________________________________
141 Bool_t compareStreams(TBuffer* s1, TBuffer* s2)
142 {
143  Int_t cnt=0;
144 #if !defined(ODBCXX_QT)
145  char c1, c2;
146  while(s1->get(c1) && s2->get(c2)) {
147  cnt++;
148  if(c1!=c2)
149  return false;
150  }
151 #else
152  char buf1[1024];
153  char buf2[1024];
154  int r1, r2;
155  while((r1=s1->readBlock(buf1,1024))!=-1 &&
156  (r2=s2->readBlock(buf2,1024))!=-1) {
157  if(r1!=r2) return false;
158 
159  for(int i=0; i<r1; i++) {
160  cnt++;
161  if(buf1[i]!=buf2[i]) return false;
162  }
163  }
164 #endif
165  return (cnt==longtestSize);
166 }
167 
168 //___________________________________________________________________
170 {
171  cout << "Creating tables:" << flush;
173  ("create table odbctest ("
174  "i1 tinyint not null, "
175  "i2 smallint not null, "
176  "i3 mediumint not null, "
177  "i4 int not null, "
178  "i5 bigint not null, "
179  "f1 float(4) not null, "
180  "f2 float(8) not null, "
181  "f3 double(10,3) not null, "
182  "d1 decimal(20,5) not null, "
183  "s1 char(1) not null, "
184  "s2 char(40) not null, "
185  "s3 varchar(1) not null, "
186  "s4 varchar(40) not null, "
187  "dt date not null, "
188  "t time not null, "
189  "ts datetime not null"
190  ")");
191  pstmt->executeUpdate();
192  cout << " odbctest" << flush;
193  delete pstmt;
194 
195  pstmt=con->PrepareStatement
196  ("create table odbctest2 ("
197  "id int not null, "
198  "c mediumtext, "
199  "b mediumblob)");
200  pstmt->executeUpdate();
201  cout << " odbctest2" << flush;
202 
203  delete pstmt;
204 
205  cout << endl;
206 }
207 
208 //___________________________________________________________________
210 {
211 
212  cout << "Populating:" << flush;
213 
214  PreparedStatement* pstmt=con->PrepareStatement
215  ("insert into odbctest(i1,i2,i3,i4,i5,f1,f2,f3,d1,s1,s2,s3,s4,dt,t,ts) "
216  "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
217 
219  while(vg.Next()) {
220  pstmt->setByte(1,vg.i1());
221  pstmt->setShort(2,vg.i2());
222  pstmt->setInt(3,vg.i3());
223  pstmt->setInt(4,vg.i4());
224  pstmt->setLong(5,vg.i5());
225  pstmt->setFloat(6,vg.f1());
226  pstmt->setFloat(7,vg.f2());
227  pstmt->setDouble(8,vg.f3());
228  pstmt->setLong(9,vg.d1());
229  pstmt->setString(10,vg.s1());
230  pstmt->setString(11,vg.s2());
231  pstmt->setString(12,vg.s3());
232  pstmt->setString(13,vg.s4());
233  pstmt->setDate(14,vg.dt());
234  pstmt->setTime(15,vg.t());
235  pstmt->setTimestamp(16,vg.ts());
236  pstmt->executeUpdate();
237  }
238 
239  delete pstmt;
240  cout << " odbctest (" << odbctestRows << ")" << flush;
241 
242  pstmt=con->PrepareStatement
243  ("insert into odbctest2(id,c,b) values(?,?,?)");
244 
245  ValueGen2 vg2(longtestRows);
246  while(vg2.Next()) {
247  pstmt->setInt(1,vg2.id());
248  TBuffer* cs=vg2.c();
249  pstmt->setAsciiStream(2,cs,longtestSize);
250  TBuffer* bs=vg2.b();
251  pstmt->setBinaryStream(3,bs,longtestSize);
252 
253  pstmt->executeUpdate();
254 
255  delete cs;
256  delete bs;
257  }
258 
259  // insert an extra row containing NULL for c and b
260  pstmt->setInt(1,vg2.id()+1);
261  pstmt->setNull(2,Types::LONGVARCHAR);
262  pstmt->setNull(3,Types::LONGVARBINARY);
263  pstmt->executeUpdate();
264 
265  delete pstmt;
266 
267  cout << " odbctest2 (" << longtestRows << ")" << flush;
268 
269  cout << endl;
270 }
271 
272 //___________________________________________________________________
274 {
275  cout << "Checking:" << flush;
276 
277  Statement* stmt=con->createStatement();
278  stmt->setFetchSize(37); // some odd number
279  ResultSet* rs=stmt->executeQuery
280  ("select i1,i2,i3,i4,i5,f1,f2,f3,d1,s1,s2,s3,s4,dt,t,ts from odbctest");
281 
282  cout << " odbctest" << flush;
283 
285  Int_t cnt=0;
286  while(rs->Next() && vg.Next()) {
287  cnt++;
288 
289  Assert(vg.cnt()==rs->GetRow());
290 
291  Assert(vg.i1()==rs->GetByte(1) &&
292  vg.i1()==rs->GetByte("i1"));
293  Assert(vg.i2()==rs->GetShort(2) &&
294  vg.i2()==rs->GetShort("i2"));
295  Assert(vg.i3()==rs->GetInt(3) &&
296  vg.i3()==rs->GetInt("i3"));
297  Assert(vg.i4()==rs->GetInt(4) &&
298  vg.i4()==rs->GetInt("i4"));
299  Assert(vg.i5()==rs->GetLong(5) &&
300  vg.i5()==rs->GetLong("i5"));
301 
302  Assert(feq(vg.f1(),rs->GetFloat(6)) &&
303  feq(vg.f1(),rs->GetFloat("f1")));
304  Assert(feq(vg.f2(),rs->GetFloat(7)) &&
305  feq(vg.f2(),rs->GetFloat("f2")));
306  Assert(feq(vg.f3(),rs->GetDouble(8)) &&
307  feq(vg.f3(),rs->GetDouble("f3")));
308 
309  Assert(vg.d1()==rs->GetLong(9) &&
310  vg.d1()==rs->GetLong("d1"));
311 
312  Assert(vg.s1()==rs->GetString(10)
313  && vg.s1()==rs->GetString("s1"));
314  Assert(vg.s2()==rs->GetString(11) &&
315  vg.s2()==rs->GetString("s2"));
316  Assert(vg.s3()==rs->GetString(12) &&
317  vg.s3()==rs->GetString("s3"));
318  Assert(vg.s4()==rs->GetString(13) &&
319  vg.s4()==rs->GetString("s4"));
320 
321  Assert(vg.dt().toString()==rs->GetString(14));
322  Assert(vg.t().toString()==rs->GetString(15));
323  Assert(vg.ts().toString()==rs->GetString(16));
324  }
325 
326  delete rs;
327  delete stmt;
328 
329  Assert(cnt==odbctestRows);
330  cout << "(" << cnt << ")" << flush;
331 
332  stmt=con->createStatement();
333  stmt->setFetchSize(10);
334  rs=stmt->executeQuery("select id,c,b from odbctest2");
335  // since we have LONGVARwhatevers in the result set
336  // this should fall down to 1
337  Assert(rs->GetFetchSize()==1);
338 
339  ValueGen2 vg2(longtestRows);
340  cnt=0;
341  cout << " odbctest2" << flush;
342  while(rs->Next() && vg2.Next()) {
343  cnt++;
344  Assert(vg2.id()==rs->GetInt("id"));
345  TBuffer* cs=vg2.c();
346  TBuffer* bs=vg2.b();
347  Assert(compareStreams(cs,rs->GetAsciiStream("c")));
348  Assert(compareStreams(bs,rs->GetBinaryStream("b")));
349  delete cs;
350  delete bs;
351  }
352 
353  // here, rs->next has been called an extra time above
354  // (we're at the row containing NULL values)
355  TBuffer* tmp=rs->GetAsciiStream("c");
356  Assert(rs->WasNull());
357  tmp=rs->GetBinaryStream("b");
358  Assert(rs->WasNull());
359 
360  Assert(cnt==longtestRows);
361  cout << "(" << cnt << ")" << flush;
362 
363  delete rs;
364  delete stmt;
365 
366  cout << endl;
367 }
368 
369 //___________________________________________________________________
370 Bool_t dropTable(TSQLConnection* con, const TString& tableName)
371 {
372  Bool_t r=true;
374  ("drop table "+tableName);
375  try {
376  pstmt->executeUpdate();
377  } catch(SQLException& e) { r=false; }
378 
379  delete pstmt;
380  return r;
381 }
382 
383 //___________________________________________________________________
385 {
386  cout << "Dropping tables:" << flush;
387  if(dropTable(con,"odbctest")) {
388  cout << " odbctest" << flush;
389  }
390 
391  if(dropTable(con," odbctest2")) {
392  cout << " odbctest2" << flush;
393  }
394  cout << endl;
395 }
396 
397 
398 #include "../src/dtconv.h"
399 
401 int main(int argc, char** argv)
402 {
403  if(argc!=2 && argc!=4) {
404  cerr << "Usage: " << argv[0] << " connect-string" << endl
405  << "or " << argv[0] << " dsn username password" << endl;
406  return 0;
407  }
408 
409 
410  try {
411  Connection* con;
412  if(argc==2) {
413  cout << "Connecting to " << argv[1] << "..." << flush;
415  } else {
416  cout << "Connecting to dsn=" << argv[1]
417  << ", uid=" << argv[2]
418  << ", pwd=" << argv[3] << "..." << flush;
419  con=TSQLDriverManager::GetConnection(argv[1],argv[2],argv[3]);
420  }
421  cout << " done." << endl;
422 
423  dropTables(con);
424  createTables(con);
425 
426  populateTables(con);
427 
428  checkTables(con);
429 
430  dropTables(con);
431 
432  delete con;
433 
434  DriverManager::shutdown();
435 
436  if(assertionsFailed) {
437  cerr << assertionsFailed << " assertions failed" << endl;
438  return 1;
439  } else {
440  cout << "Apparently, this worked!" << endl;
441  }
442 
443 
444  } catch(SQLException& e) {
445  cerr << endl << e.getMessage() << endl;
446  return 2;
447  }
448 
449  return 0;
450 }