Analysis Software
Documentation for
sPHENIX
simulation software
Home page
Related Pages
Modules
Namespaces
Classes
Files
Examples
External Links
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Groups
Pages
TSQLConnection.cxx
Go to the documentation of this file.
Or view
the newest version in sPHENIX GitHub for file TSQLConnection.cxx
1
// $Id: TSQLConnection.cxx,v 1.1.1.1 2004/02/18 20:58:02 dave Exp $
2
//*-- Author : Valeriy Onuchin 14/02/2000
3
//
4
6
//
7
//
8
// A connection (session) with a specific database. Within the context
9
// of a TSQLConnection, SQL statements are executed and results are
10
// returned. A TSQLConnection's database is able to provide information
11
// describing its tables, its supported SQL grammar, its stored procedures,
12
// the capabilities of this connection, and so on. This information is
13
// obtained with the GetMetaData() method.
14
//
15
// See also:
16
// TSQLDriverManager::GetConnection(TString)
17
// TSQLStatement TSQLPreparedStatement TSQLCallableStatement
18
// TSQLResultSet TSQLDatabaseMetaData
19
//
21
//
22
// A transaction is a recoverable sequence of SQL operations grouped
23
// as a single unit. The initiation and termination of transaction
24
// define the following points of data consistency within an
25
// application process; either all SQL operations within a transaction
26
// are applied to the data source (committed), or the effects of all
27
// SQL operations within a transaction are completely "undone"
28
// (rolled back).
29
// RDBC provides the following modes of transaction processing
30
// that determine haw and when transactions are to be committed (or,
31
// if possible rolled back):
32
//
33
// - Auto-commit mode
34
// - Manual-commit mode
35
//
36
// defined by TSQLConnection::SetAutoCommit( Bool_t autoCommit )
37
//
39
//
40
// In multi-user database system, transactions can occur
41
// simultaneously, and each transaction has potential to interfere
42
// with another one. When transactions are not isolated from each
43
// other in multi-user enviroments, the following three types of
44
// events can occur:
45
//___________________________________________________________________
46
//
47
// Dirty Read:
48
//
49
// Transaction 1 changes a row. Transaction 2 reads
50
// the changed row before Transacion 1 commits the change. If
51
// Transaction 1 rolls back the change, Transacion 2 will have
52
// read a row that is considered to have never existed
53
//
54
//___________________________________________________________________
55
//
56
// Nonrepeatable Read:
57
//
58
// Transaction 1 reads a row. Transaction 2 updates or deletes
59
// that row and commits the change. If Transaction 1 attempts
60
// to reread the row, it will receive different row values or
61
// discover that the row has been deleted.
62
//
63
//___________________________________________________________________
64
//
65
// Phantom:
66
//
67
// Transaction 1 reads a set of rows that satisfy some search
68
// criteria. Transaction 2 generates one or more rows ( either
69
// through inserts or updates ) that match the search criteria
70
// If transacion 1 re-executes the statement that reads the rows,
71
// it receives a different set of rows.
72
//
74
//
75
// ODBC defines four levels of transaction isolation that can,
76
// prevent all, some, or none of these events from occurring.
77
// They are:
78
//___________________________________________________________________
79
//
80
// kTRANSACTION_NONE
81
//
82
// Indicates that transactions are not supported.
83
//
84
//___________________________________________________________________
85
//
86
// kTRANSACTION_READ_UNCOMMITTED
87
//
88
// Dirty reads, non-repeatable reads and phantom reads can occur.
89
// This level allows a row changed by one transaction to be read
90
// by another transaction before any changes in that row have been
91
// committed (a "dirty read"). If any of the changes are rolled back,
92
// the second transaction will have retrieved an invalid row.
93
//
94
//___________________________________________________________________
95
//
96
// kTRANSACTION_READ_COMMITTED
97
//
98
//
99
// Dirty reads are prevented; non-repeatable reads and phantom reads
100
// can occur. This level only prohibits a transaction from reading a
101
// row with uncommitted changes in it.
102
//
103
//___________________________________________________________________
104
//
105
// kTRANSACTION_REPEATABLE_READ
106
//
107
// Dirty reads and non-repeatable reads are prevented; phantom reads
108
// can occur. This level prohibits a transaction from reading a row
109
// with uncommitted changes in it, and it also prohibits the situation
110
// where one transaction reads a row, a second transaction alters the
111
// row, and the first transaction rereads the row, getting different
112
// values the second time (a "non-repeatable read").
113
//
114
//___________________________________________________________________
115
//
116
// kTRANSACTION_SERIALIZABLE
117
//
118
// Dirty reads, non-repeatable reads and phantom reads are prevented.
119
// This level includes the prohibitions in kTRANSACTION_REPEATABLE_READ
120
// and further prohibits the situation where one transaction reads
121
// all rows that satisfy a WHERE condition, a second transaction
122
// inserts a row that satisfies that WHERE condition, and the first
123
// transaction rereads for the same condition, retrieving the
124
// additional "phantom" row in the second read.
125
//
126
//
128
129
#include <
RDBC/TSQLConnection.h
>
130
#include <
RDBC/TSQLResultSet.h
>
131
#include <
RDBC/TSQLDatabaseMetaData.h
>
132
#include <
RDBC/TSQLDriverManager.h
>
133
#include <TList.h>
134
135
ClassImpQ
(
TSQLConnection
)
136
137
138
//___________________________________________________________________
139
TSQLConnection
::
TSQLConnection
( const TString&
/* connectString */
):
140
TObject(),
TSQL
(),TRefCnt()
141
{
142
// Attempts to establish a connection to the given database
143
// by specified connection string. This string is simply
144
// a series of keyword/value pairs, searated by semicolons,
145
// that contains information used to establish the connection.
146
// The TSQLDriverManager attempts to select an appropriate driver
147
// from the set of registered drivers.
148
//
149
// Parameters:
150
// connectString usually something like:
151
// "dsn=minos;uid=scott;pwd=tiger"
152
//
153
// Throws:
154
// TSQLException - if a database access error occurs
155
156
fImp
= 0;
157
fMetaData
= 0;
158
fListOfStatements =
new
TList();
159
AddReference();
160
}
161
162
//___________________________________________________________________
163
TSQLConnection::TSQLConnection
(
const
TString&
/* dsn */
,
164
const
TString&
/* username */
,
165
const
TString&
/* password */
):
166
TObject(),
TSQL
(),TRefCnt()
167
{
168
// Attempts to establish a connection to the given Data Source Name (DSN).
169
// The TSQLDriverManager attempts to select an appropriate driver from
170
// the set of registered drivers.
171
//
172
// Parameters:
173
// dsn - DataSourceName string
174
// username - the database user on whose behalf the TSQLConnection
175
// is being made
176
// password - the user's password
177
//
178
// Throws:
179
// TSQLException - if a database access error occurs
180
//
181
182
fImp
= 0;
183
fMetaData
= 0;
184
fListOfStatements =
new
TList();
185
AddReference();
186
}
187
188
//___________________________________________________________________
189
TSQLConnection::~TSQLConnection
()
190
{
191
// dtor
192
193
if
(
IsClosed
())
return
;
194
TList* li =
gSQLDriverManager
->
GetConnections
();
195
li->Remove(
this
);
196
}
197
198
//___________________________________________________________________
199
void
TSQLConnection::Close
()
200
{
201
// Releases a TSQLConnection's database and resources immediately
202
// instead of waiting for them to be automatically released.
203
//
204
// Throws:
205
// TSQLException - if a database access error occurs
206
207
if
(
IsClosed
()) {
208
Throw
(
new
TSQLException
(
"Connection is closed"
,
""
,0) );
209
return
;
210
}
211
212
if
(RemoveReference()>0)
return
;
213
214
TList* li =
gSQLDriverManager
->
GetConnections
();
215
li->Remove(
this
);
216
217
if
(
fListOfStatements
) {
// deallocate all statements
218
fListOfStatements
->Delete();
219
delete
fListOfStatements
;
220
}
221
222
fListOfStatements
= 0;
223
Destroyed();
224
}
225
226
//___________________________________________________________________
227
Bool_t
TSQLConnection::IsClosed
()
228
{
229
// Tests to see if a TSQLConnection is closed.
230
//
231
// Returns:
232
// kTRUE if the connection is closed;
233
// kFALSE if it's still open
234
// Throws:
235
// TSQLException - if a database access error occurs
236
237
return
References()<=0;
238
}
239
240
//___________________________________________________________________
241
void
TSQLConnection::SetURL
(
const
TString&
url
)
242
{
243
// sets URL string
244
245
fMetaData
->
SetURL
(url);
246
}
247
249
//___________________________________________________________________
250
const
char
*
s
(Bool_t
b
)
251
{
252
//
253
254
return
b?
"Yes"
:
"No"
;
255
}
256
257
//___________________________________________________________________
258
TString
maybeQuote
(
const
TString&
str
,
const
TString& qc)
259
{
260
//
261
262
TString res;
263
if
(qc!=
" "
) res = qc + str + qc;
264
else
res =
str
;
265
return
res;
266
}
267
268
//___________________________________________________________________
269
void
rsInfo
(
TSQLDatabaseMetaData
* md,
270
Int_t rsType,
const
char
*
name
)
271
{
272
// prints result set info
273
274
if
(md->
SupportsResultSetType
(rsType)) {
275
printf
(
"%s\n"
,name);
276
277
if
(md->
SupportsResultSetConcurrency
(rsType,
kCONCUR_READ_ONLY
)) {
278
printf
(
" + kCONCUR_READ_ONLY\n"
);
279
}
280
281
if
(md->
SupportsResultSetConcurrency
(rsType,
kCONCUR_UPDATABLE
)) {
282
printf
(
" + kCONCUR_UPDATABLE\n"
);
283
}
284
285
if
(md->
OwnInsertsAreVisible
(rsType)) {
286
printf
(
" Own inserts are visible\n"
);
287
}
288
289
if
(md->
OwnUpdatesAreVisible
(rsType)) {
290
printf
(
" Own updates are visible\n"
);
291
}
292
if
(md->
OwnDeletesAreVisible
(rsType)) {
293
printf
(
" Own deletes are visible\n"
);
294
}
295
}
296
}
297
298
//___________________________________________________________________
299
struct
{
300
int
id
;
301
const
char
*
name
;
302
}
levels
[] = {
303
{
kTRANSACTION_READ_UNCOMMITTED
,
"kTRANSACTION_READ_UNCOMMITTED"
},
304
{
kTRANSACTION_READ_COMMITTED
,
"kTRANSACTION_READ_COMMITTED"
},
305
{
kTRANSACTION_REPEATABLE_READ
,
"kTRANSACTION_REPEATABLE_READ"
},
306
{
kTRANSACTION_SERIALIZABLE
,
"kTRANSACTION_SERIALIZABLE"
},
307
{ 0,NULL }
308
};
309
310
//___________________________________________________________________
311
void
transactionInfo
(
TSQLDatabaseMetaData
* md)
312
{
313
//prints out transaction info
314
315
TString
str
;
316
317
str =
s
(md->
SupportsTransactions
());
318
printf
(
"Supports transactions : %s\n"
,str.Data());
319
320
printf
(
"\nTransaction support \n"
);
321
printf
(
"---------------------------------------------------\n"
);
322
323
if
(!md->
SupportsTransactions
()) {
324
printf
(
"This datasource does not support transactions.\n\n"
);
325
return
;
326
}
327
328
int
defIsolation=md->
GetDefaultTransactionIsolation
();
329
330
for
(
int
i
=0;
levels
[
i
].name!=NULL;
i
++) {
331
if
(md->
SupportsTransactionIsolationLevel
(
levels
[
i
].id)) {
332
str =
" +"
;
333
str +=
levels
[
i
].name;
334
str += (
levels
[
i
].id==defIsolation) ?
" (default)"
:
""
;
335
printf
(
"%s\n"
,str.Data());
336
}
337
}
338
339
// the longest method name I've ever seen!
340
if
(md->
SupportsDataDefinitionAndDataManipulationTransactions
()) {
341
printf
(
" Both DML and DDL can be used within a transaction\n"
);
342
}
else
if
(md->
SupportsDataManipulationTransactionsOnly
()) {
343
printf
(
" Only DML can be used within a transaction\n"
);
344
}
else
if
(md->
DataDefinitionCausesTransactionCommit
()) {
345
printf
(
" DDL causes commit\n"
);
346
}
else
if
(md->
DataDefinitionIgnoredInTransactions
()) {
347
printf
(
" DDL is ignored in transactions\n"
);
348
}
349
}
350
351
//___________________________________________________________________
352
void
catalogInfo
(
TSQLDatabaseMetaData
* md)
353
{
354
//
355
356
Bool_t cdml = md->
SupportsCatalogsInDataManipulation
();
357
Bool_t cproc = md->
SupportsCatalogsInProcedureCalls
();
358
Bool_t ctd = md->
SupportsCatalogsInTableDefinitions
();
359
Bool_t
cid
= md->
SupportsCatalogsInIndexDefinitions
();
360
Bool_t cpd = md->
SupportsCatalogsInPrivilegeDefinitions
();
361
Bool_t hasCatalogs = cdml || cproc || ctd || cid || cpd;
362
363
if
(hasCatalogs) {
364
printf
(
"Supported catalog uses\n"
);
365
printf
(
"--------------------------------------------------- \n"
);
366
printf
(
"Data manipulation : %s\n"
,
s
(cdml) );
367
printf
(
"Procedure calls : %s\n"
,
s
(cproc) );
368
printf
(
"Table definitions : %s\n"
,
s
(ctd) );
369
printf
(
"Index definitions : %s\n"
,
s
(cid) );
370
printf
(
"Privilege definitions: %s\n"
,
s
(cpd) );
371
}
else
{
372
printf
(
"This datasource does not support catalogs\n"
);
373
}
374
printf
(
"\n"
);
375
376
TString
id
= md->
GetTableTerm
();
377
if
(hasCatalogs) {
378
TString catSep;
379
TString catTerm;
380
catSep = md->
GetCatalogSeparator
();
381
catTerm = md->
GetCatalogTerm
();
382
if
(md->
IsCatalogAtStart
()) {
383
id
= catTerm + catSep +
id
;
384
}
else
{
385
id
=
id
+ catSep + catTerm;
386
}
387
}
388
printf
(
"Preferred table identifier format: %s\n"
,
id
.
Data
());
389
}
390
391
//___________________________________________________________________
392
void
schemaInfo
(
TSQLDatabaseMetaData
* md)
393
{
394
//
395
396
Bool_t sdml = md->
SupportsSchemasInDataManipulation
();
397
Bool_t sproc = md->
SupportsSchemasInProcedureCalls
();
398
Bool_t std = md->
SupportsSchemasInTableDefinitions
();
399
Bool_t
sid
= md->
SupportsSchemasInIndexDefinitions
();
400
Bool_t spd = md->
SupportsSchemasInPrivilegeDefinitions
();
401
Bool_t hasSchemas=sdml || sproc || std || sid || spd;
402
403
if
(hasSchemas) {
404
printf
(
"Supported schema uses\n"
);
405
printf
(
"--------------------------------------------------- \n"
);
406
printf
(
"Data manipulation : %s\n"
,
s
(sdml) );
407
printf
(
"Procedure calls : %s\n"
,
s
(sproc) );
408
printf
(
"Table definitions : %s\n"
,
s
(std) );
409
printf
(
"Index definitions : %s\n"
,
s
(sid) );
410
printf
(
"Privilege definitions: %s\n"
,
s
(spd) );
411
}
else
{
412
printf
(
"This datasource does not support schemas\n"
);
413
}
414
printf
(
"\n"
);
415
416
TString idq = md->
GetIdentifierQuoteString
();
417
// space means no quoting supported
418
419
TString
id
= md->
GetTableTerm
();
420
}
421
422
//___________________________________________________________________
423
void
productInfo
(
TSQLDatabaseMetaData
* md)
424
{
425
//
426
427
TString
str
;
428
429
str = md->
GetDatabaseProductName
();
430
printf
(
"Product name : %s\n"
,str.Data());
431
432
str = md->
GetDatabaseProductVersion
();
433
printf
(
"Product version : %s\n"
,str.Data());
434
}
435
436
//___________________________________________________________________
437
void
driverInfo
(
TSQLDatabaseMetaData
* md)
438
{
439
//
440
441
TString
str
;
442
443
str = md->
GetDriverName
();
444
printf
(
"Driver name : %s\n"
,str.Data());
445
446
str = md->
GetDriverVersion
();
447
printf
(
"Driver version : %s\n"
,str.Data());
448
}
449
450
//___________________________________________________________________
451
void
funcInfo
(
TSQLDatabaseMetaData
* md)
452
{
453
//
454
455
TString
str
;
456
457
str = md->
GetSystemFunctions
();
458
printf
(
"Supported system functions \n"
);
459
printf
(
"--------------------------------------------------- \n"
);
460
printf
(
"%s\n\n"
,str.Data());
461
462
printf
(
"Supported string functions\n"
);
463
printf
(
"--------------------------------------------------- \n"
);
464
str = md->
GetStringFunctions
();
465
printf
(
"%s\n\n"
,str.Data());
466
467
printf
(
"Supported time/date functions\n"
);
468
printf
(
"--------------------------------------------------- \n"
);
469
str = md->
GetTimeDateFunctions
();
470
printf
(
"%s\n\n"
,str.Data());
471
472
printf
(
"Supported numeric functions\n"
);
473
printf
(
"---------------------------------------------------\n"
);
474
str = md->
GetNumericFunctions
();
475
printf
(
"%s\n\n"
,str.Data());
476
}
477
478
//___________________________________________________________________
479
void
keywordInfo
(
TSQLDatabaseMetaData
* md)
480
{
481
//
482
483
TString
str
;
484
485
printf
(
"Non-ODBC SQL keywords\n"
);
486
printf
(
"--------------------------------------------------- \n"
);
487
str = md->
GetSQLKeywords
();
488
printf
(
"%s\n\n"
,str.Data());
489
}
490
491
//___________________________________________________________________
492
void
TSQLConnection::Print
(Option_t *option)
const
493
{
494
// Prints out information about this connection
495
//
496
// If option contains:
497
//
498
// 'r' - prints result set info
499
// 't' - prints out transaction info
500
// 'p' - prints out product info
501
// 'd' - prints out driver info
502
// 'f' - prints out supported functions
503
// 'k' - prints out Non-ODBC SQL keywords
504
// 'c' - prints out catalog info
505
// 's' - prints out shema info
506
// 'a' - prints out everything
507
508
TSQLConnection
*
con
= (
TSQLConnection
*)
this
;
509
if
(con->
IsClosed
())
return
;
510
511
TString opt = option;
512
opt.ToLower();
513
514
printf
(
"Connection to :\t%s\n"
,
fMetaData
->
GetURL
().Data());
515
printf
(
"=================================================== \n\n"
);
516
517
if
(opt.Contains(
"p"
) || opt.Contains(
"a"
)) {
518
productInfo
(
fMetaData
);
519
printf
(
"=================================================== \n\n"
);
520
}
521
if
(opt.Contains(
"d"
) || opt.Contains(
"a"
)) {
522
driverInfo
(
fMetaData
);
523
printf
(
"=================================================== \n\n"
);
524
}
525
if
(opt.Contains(
"t"
) || opt.Contains(
"a"
)) {
526
transactionInfo
(
fMetaData
);
527
printf
(
"=================================================== \n\n"
);
528
}
529
if
(opt.Contains(
"f"
) || opt.Contains(
"a"
)) {
530
funcInfo
(
fMetaData
);
531
printf
(
"=================================================== \n\n"
);
532
}
533
if
(opt.Contains(
"k"
) || opt.Contains(
"a"
)) {
534
keywordInfo
(
fMetaData
);
535
printf
(
"=================================================== \n\n"
);
536
}
537
538
if
(opt.Contains(
"r"
) || opt.Contains(
"a"
)) {
539
printf
(
"Supported TSQLResultSet types\n"
);
540
printf
(
"--------------------------------------------------- \n"
);
541
rsInfo
(
fMetaData
,
kTYPE_FORWARD_ONLY
,
"kTYPE_FORWARD_ONLY"
);
542
rsInfo
(
fMetaData
,
kTYPE_SCROLL_INSENSITIVE
,
"kTYPE_SCROLL_INSENSITIVE"
);
543
rsInfo
(
fMetaData
,
kTYPE_SCROLL_SENSITIVE
,
"kTYPE_SCROLL_SENSITIVE"
);
544
printf
(
"\n=================================================== \n"
);
545
}
546
if
(opt.Contains(
"c"
) || opt.Contains(
"a"
)) {
547
catalogInfo
(
fMetaData
);
548
printf
(
"=================================================== \n\n"
);
549
}
550
if
(opt.Contains(
"s"
) || opt.Contains(
"a"
)) {
551
schemaInfo
(
fMetaData
);
552
printf
(
"=================================================== \n\n"
);
553
}
554
}
555
556
//___________________________________________________________________
557
const
char
*
TSQLConnection::GetName
()
const
558
{
559
// returns dsn/url
560
561
TSQLConnection
* con = (
TSQLConnection
*)
this
;
562
if
(con->
IsClosed
())
return
0;
563
564
return
fMetaData
->
GetURL
().Data();
565
}
566
567
//___________________________________________________________________
568
const
char
*
TSQLConnection::GetTitle
()
const
569
{
570
// returns nothing
571
572
TSQLConnection
* con = (
TSQLConnection
*)
this
;
573
if
(con->
IsClosed
())
return
0;
574
return
""
;
575
}
RDBC
blob
master
rdbc
TSQLConnection.cxx
Built by
Jin Huang
. updated:
Sat Feb 17 2024 22:18:49
using
1.8.2 with
sPHENIX GitHub integration