File indexing completed on 2025-08-03 08:22:06
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <RDBC/TSQLImporter.h>
0011 #include <RDBC/TSQLImportClient.h>
0012 #include <RDBC/TSQLConnection.h>
0013 #include <RDBC/TSQLUrl.h>
0014 #include <RDBC/TSQLStatement.h>
0015 #include <RDBC/TSQLResultSet.h>
0016 #include <RDBC/TSQLDriverManager.h>
0017
0018 ClassImpQ(TSQLImporter)
0019
0020
0021 TSQLImporter::TSQLImporter():TQObject()
0022 {
0023
0024
0025 fConnection = 0;
0026 fClient = 0;
0027 fStatus = 400;
0028 }
0029
0030
0031 void TSQLImporter::LoadTable(const TString& url)
0032 {
0033
0034
0035 fStatus = 400;
0036 TSQLStatement* stmt = 0;
0037
0038 fClient = new TSQLImportClient(url);
0039
0040
0041 fClient->Init();
0042
0043 if(!fClient->IsValid()) {
0044 fStatus = fClient->GetStatus();
0045 TSQLException* e = fClient->GetException();
0046 if(e) {
0047
0048 TSQLException* clientException = new TSQLException();
0049 *clientException = *e;
0050 Throw(clientException);
0051 }
0052 if(fClient) delete fClient;
0053 fClient = 0;
0054 return;
0055 }
0056
0057 TString cols = fClient->GetColumns();
0058 TString table = fClient->GetTableName();
0059 TString file = fClient->GetLocalFile();
0060
0061 TString query;
0062 TString query1;
0063 Int_t update_count;
0064
0065 stmt = fConnection->CreateStatement();
0066
0067 if(!stmt) {
0068 Throw(new TSQLException("Failed to create SQL statement","",HTTP_NOT_ACCEPTABLE));
0069 if(fClient) delete fClient;
0070 Destroyed();
0071 fClient = 0;
0072 fStatus = HTTP_NOT_ACCEPTABLE;
0073 return;
0074 }
0075
0076 query1 = "CREATE TEMPORARY TABLE ";
0077 query1 += table + "(" + cols + ")";
0078
0079 update_count = stmt->ExecuteUpdate(query1);
0080
0081 query = "LOAD DATA ";
0082 query += fClient->GetLocal() + " INFILE '";
0083 query += file;
0084 query += "' INTO TABLE ";
0085 query += table;
0086 query += " FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '";
0087 query += '\"';
0088 query += "'";
0089
0090
0091
0092 if(fClient->GetSkipLines()) {
0093 query += " IGNORE ";
0094 query += Form("%d LINES",fClient->GetSkipLines());
0095 }
0096
0097 update_count = stmt->ExecuteUpdate(query);
0098
0099 if(!update_count) {
0100 Throw(new TSQLException(Form("Wrong format:\n%s\n%s",query1.Data(),query.Data()),"",HTTP_NOT_ACCEPTABLE));
0101 Destroyed();
0102 SafeDelete(fClient);
0103 fStatus = HTTP_NOT_ACCEPTABLE;
0104 }
0105
0106 fStatus = HTTP_OK;
0107 if(stmt) delete stmt;
0108 return;
0109 }
0110
0111
0112 void TSQLImporter::LoadCatalog(const TString& url)
0113 {
0114
0115
0116 LoadTable(url);
0117
0118 if( (fStatus!=HTTP_OK) || !fClient ) {
0119 return;
0120 }
0121
0122 TSQLStatement* stmt = fConnection->CreateStatement();
0123 TString table = fClient->GetTableName();
0124 TString query;
0125
0126
0127
0128
0129 query = "SELECT * FROM " + table;
0130
0131 TSQLResultSet* rs = stmt->ExecuteQuery(query);
0132
0133 if(!rs) {
0134 fStatus = HTTP_NOT_ACCEPTABLE;
0135 return;
0136 }
0137
0138 if(fClient) {
0139 delete fClient;
0140 fClient = 0;
0141 }
0142
0143 while(rs->Next()) {
0144 table = rs->GetString(1);
0145 LoadTable(table);
0146 }
0147
0148 if(fClient) {
0149 delete fClient;
0150 fClient = 0;
0151 }
0152
0153 fStatus = HTTP_OK;
0154 if(stmt) delete stmt;
0155 return;
0156 }
0157
0158
0159 Int_t TSQLImporter::Import(const TString& url,TSQLConnection* con)
0160 {
0161
0162
0163 fStatus = 400;
0164
0165 if(!con) {
0166 fConnection = 0;
0167
0168 return fStatus = HTTP_FORBIDDEN;
0169 }
0170
0171 fConnection = con;
0172 TString ext = strrchr(url.Data(),'.');
0173
0174 if( (ext==".cat") || (ext==".db") ) LoadCatalog(url);
0175 else LoadTable(url);
0176
0177 return fStatus = HTTP_OK;
0178 }
0179
0180
0181 Bool_t TSQLImporter::IsValid() const
0182 {
0183
0184
0185 return (fStatus < 400);
0186 }
0187
0188
0189 TSQLImporter::~TSQLImporter()
0190 {
0191
0192
0193 SafeDelete(fClient);
0194 }
0195