File indexing completed on 2025-08-03 08:22:02
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029 #ifndef __CINT__
0030
0031
0032 #include <TError.h>
0033 #include <TH1.h>
0034 #include <TH2.h>
0035 #include <TProfile.h>
0036 #include <TRandom.h>
0037 #include <TROOT.h>
0038 #include <TSQLDriverManager.h>
0039 #include <TSQLConnection.h>
0040 #include <TSQLResultSet.h>
0041 #include <TSQLResultSetMetaData.h>
0042 #include <TSQLPreparedStatement.h>
0043
0044 #endif
0045
0046 const TString tableName = "object_test";
0047
0048
0049 TH1F* hpx;
0050 TH2F* hpxpy;
0051 TProfile* hprof;
0052
0053
0054 void CreateStuff(TSQLConnection* con)
0055 {
0056
0057
0058
0059 TSQLStatement* stmt = con->CreateStatement();
0060
0061 stmt->ExecuteUpdate( "create table " +tableName+ "("
0062 "name varchar(22) not null, histos lo not null)" );
0063
0064 printf("Table %s created.\n",tableName.Data());
0065 delete stmt;
0066
0067 TSQLPreparedStatement* pstmt = con->PrepareStatement(
0068 "insert into " +tableName+
0069 "(name,histos) values(?,?)" );
0070
0071 hpx = new TH1F("hpx","This is the px distribution",100,-4,4);
0072 hpxpy = new TH2F("hpxpy","py vs px",40,-4,4,40,-4,4);
0073 hprof = new TProfile("hprof","Profile of pz versus px",100,-4,4,0,20);
0074
0075
0076 hpx->SetFillColor(48);
0077
0078 pstmt->SetString(1,"hpx");
0079 pstmt->SetObject(2,hpx);
0080 pstmt->ExecuteUpdate();
0081
0082 pstmt->SetString(1,"hpxpy");
0083 pstmt->SetObject(2,hpxpy);
0084 pstmt->ExecuteUpdate();
0085
0086 pstmt->SetString(1,"hprof");
0087 pstmt->SetObject(2,hprof);
0088 pstmt->ExecuteUpdate();
0089
0090 con->Commit();
0091 delete pstmt;
0092 }
0093
0094
0095 void DropStuff(TSQLConnection* con)
0096 {
0097
0098
0099 TSQLStatement* stmt = con->CreateStatement();
0100 stmt->ExecuteUpdate("drop table " + tableName);
0101 printf("Dropped table %s\n",tableName.Data());
0102 delete stmt;
0103 }
0104
0105
0106 void Fill(TSQLConnection* con)
0107 {
0108
0109
0110 TString str;
0111
0112 TSQLStatement* stmt =
0113 con->CreateStatement();
0114 if (!stmt) return;
0115
0116 TSQLResultSet* rs;
0117
0118 Float_t px, py, pz;
0119 int ii = 0;
0120
0121
0122 while (1)
0123 {
0124 gRandom->Rannor(px,py);
0125 pz = px*px + py*py;
0126 hpx->Fill(px);
0127 hpxpy->Fill(px,py);
0128 hprof->Fill(px,pz);
0129
0130 if ((ii % 1000) == 0)
0131 {
0132 rs = stmt->ExecuteQuery("select name, histos from "
0133 + tableName);
0134
0135 if (!rs) break;
0136
0137 while(rs->Next())
0138 {
0139 str = rs->GetString(1);
0140 if(str==hpx->GetName()) rs->UpdateObject(2,hpx);
0141 else if(str==hpxpy->GetName()) rs->UpdateObject(2,hpxpy);
0142 else if(str==hprof->GetName()) rs->UpdateObject(2,hprof);
0143 rs->UpdateRow();
0144 }
0145 con->Commit();
0146 delete rs;
0147 }
0148 ii++;
0149 }
0150 delete stmt;
0151 }
0152
0153
0154 Int_t oproducer( const Text_t* dsn,
0155 const Text_t* usr,
0156 const Text_t* pwd )
0157 {
0158
0159
0160 TSQLConnection* con;
0161 TString str;
0162
0163
0164 TSQL::SetHandler("Catch(TSQLException*)");
0165
0166 str = "dsn="; str += dsn;
0167 str += "; uid="; str += usr;
0168
0169
0170 con = gSQLDriverManager->GetConnection(str);
0171
0172 if(!con) return -1;
0173
0174 printf("\t\t\t DONE.\n");
0175
0176 if( con->GetMetaData()->SupportsTransactions() ) {
0177 con->SetAutoCommit(kFALSE);
0178 }
0179
0180 TSQL::UnsetHandler();
0181
0182 TSQL::SetHandler("Catch(TSQLException*)");
0183 CreateStuff(con);
0184
0185 TSQL::UnsetHandler();
0186
0187 con->Commit();
0188 con->Close();
0189 return 0;
0190 }
0191
0192
0193 void Catch(TSQLException* e)
0194 {
0195
0196
0197 TString str = e->GetMessage();
0198 printf("%s\n",str.Data());
0199 }