Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TSQLImportClient.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file TSQLImportClient.cxx
1 // $Id: TSQLImportClient.cxx,v 1.1.1.1 2004/02/18 20:58:02 dave Exp $
2 //*-- Author : Valeriy Onuchin 21/03/2001
3 
5 //
6 // TSQLImportClient
7 //
9 
10 #include <RDBC/TSQLUrl.h>
11 #include <RDBC/TSQL.h>
12 #include <RDBC/TSQLImportClient.h>
13 #include <TString.h>
14 #include <TSystem.h>
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <TUrl.h>
18 #include <TSocket.h>
19 
21 //___________________________________________________________________
23 {
24  // ctor.
25 
26  fUrl = new TSQLUrl(url);
27  fException = 0;
28  fStatus = 0;
29  TString host(fUrl->GetHost().Data());
30  TString str(fUrl->GetFile().Data());
31  fLocalFile = str;
32 
33  if(host=="localhost") {
34  fMustDeleteLocalFile = kFALSE;
35  } else {
36  fMustDeleteLocalFile = kTRUE;
37  fLocalFile = Form("/tmp/%s%d",gSystem->BaseName(fUrl->GetFile().Data()),gSystem->GetPid());
38  GET(url); // download
39  }
40 
41  fSkipLines = 1; // default , first line is a header describes the columns
42 }
43 
44 //___________________________________________________________________
46 {
47  // dtor.
48 
49  Clean();
50 }
51 
52 //___________________________________________________________________
54 {
55  //
56 
58  gSystem->Unlink(fLocalFile.Data());
59  }
60  if(fException) delete fException;
61  if(fUrl) delete fUrl;
62 }
63 
64 //___________________________________________________________________
65 TString Validate(const TString& str)
66 {
67  // internal use static func.
68  //
69  // - Does validation of string as coulmn names/types, very primitive so far.
70  // - Returns corrected column string
71 
72  TString ret = str.Strip(TString::kBoth);
73  Int_t spidx = 0;
74  const char* s = ret.Data();
75 
76  if(s[0]=='\"' || s[strlen(s)-1]=='\"' ) {
77  TString quote('\"');
78  ret.ReplaceAll(quote,"");
79  goto exit;
80  }
81  if( ret.IsNull() ||
82  ((ret.Length()==1) && !isalpha(s[0]) ) ) return "wrong format";
83 
84  for (Ssiz_t i = 0; i < ret.Length(); i++) {
85  if( !isalnum(s[i]) && !isspace(s[i]) &&
86  s[i]!=')' && s[i]!='(' && s[i]!=',' &&
87  s[i]!='_' && s[i]!='-' ) {
88  return "wrong format";
89  }
90  if(isspace(s[i])) spidx = i;
91  }
92 
93 exit:
94  if(!spidx) ret += " TEXT NOT NULL";
95  return ret;
96 }
97 
98 //___________________________________________________________________
99 void TSQLImportClient::GET(const TString& url)
100 {
101  // Download url into local temporary file
102 
103  TString str;
104  const Int_t buflen=8192;
105  static char buf[buflen];
106 
107  TString filename = url;
108  filename.ReplaceAll(" ","");
109 
110  TUrl u(filename);
111 
112  TSocket s(u.GetHost(), u.GetPort());
113 
114  if (!s.IsValid()) {
116  return;
117  }
118 
119  TString msg = Form("GET %s HTTP/1.0\015\012\015\012", u.GetFile());
120  s.SendRaw(msg.Data(), msg.Length());
121 
122  while(s.RecvRaw(buf, buflen)>0) {
123  str += buf;
124  memset(buf,0,buflen);
125  }
126  s.Close();
127 
128  // cutoff HTTP header
129  Int_t idx;
130  idx = str.Index("\015\012\015\012");
131  if(idx!=kNPOS) str = str(idx+4,str.Length()-idx-4);
132 
133  FILE* fd = fopen(fLocalFile.Data(),"w");
134  if(!fd) fStatus = HTTP_FORBIDDEN;
135 
136  idx = fwrite(str.Data(),str.Length(),1,fd);
137  if(idx!=1) {
139  }
140  fclose(fd);
141  return;
142 }
143 
144 //___________________________________________________________________
146 {
147  // - read first line from local file
148  // - determine column names and types
149 
150  //usused: Bool_t isValid = kTRUE;
151  FILE* fd;
152  TString str;
153 
154  fTableName = TString(gSystem->BaseName(fLocalFile.Data()));
155  TString ext = strrchr(fTableName.Data(),'.');
156 
157  if(!ext.IsNull()) {
158  Int_t pidx = fTableName.Index(ext.Data());
159  if(pidx>1) {
160  fTableName = fTableName(0,pidx);
161  }
162 
163  fTableName.ReplaceAll(".","_");
164  }
165 
166  if(gSystem->AccessPathName(fLocalFile.Data())) {
168  str = "File ";
169  str += fLocalFile + " not found";
170  fException = new TSQLException(str,"",fStatus);
171  return fStatus;
172  }
173 
174  fd = fopen(fLocalFile.Data(),"r");
175 
176  if( !fd ) {
177  fclose(fd);
179  str = "You don't have read permission to ";
180  str += fLocalFile;
181  fException = new TSQLException(str,"",fStatus);
182  return fStatus;
183  }
184 
185  const Int_t buflen=8192;
186  char buf[buflen];
187 
188  fgets(buf,buflen,fd); // read first line
189  str = buf;
190 
191  if(str.IsNull()) {
192  fclose(fd); // empty file
194  str = "File ";
195  str += fLocalFile + " is empty";
196  fException = new TSQLException(str,"",fStatus);
197  return fStatus;
198  }
199  str.Chop(); // cut off \n
200 
201  TString tmp;
202  Int_t i,k;
203  Int_t ncols = 0;
204  Bool_t wrongFormat = kFALSE;
205 
206  for( i=k=0; (i=str.Index(",",i))>0; k=i++ ) {
207  ncols++;
208  tmp = Validate(str(!k?0:k+1,!k?i:i-k-1));
209  wrongFormat = wrongFormat || tmp.IsNull() || (tmp=="wrong format");
210  if(!wrongFormat) fColumns += tmp + ",";
211  }
212 
213  ncols++;
214  tmp = Validate(str(k+(ncols>1),str.Length())); // the rest of string
215 
216  wrongFormat = wrongFormat || (tmp=="wrong format");
217  if(!wrongFormat) fColumns += tmp;
218  else {
219  fColumns = "";
220  for(i=1; i<ncols; i++) fColumns += Form("C%d TEXT NOT NULL,",i);
221  fColumns += Form("C%d TEXT NOT NULL",ncols);
222  fSkipLines = 0;
223  }
224 
225  fclose(fd);
226  return fStatus = HTTP_OK;
227 }