69 class TSQLUrlParser: public TQObject, public TRefCnt
76 kErrProtocol = BIT(0),
77 kErrSubProtocol= BIT(1),
82 kErrDatabase = BIT(5),
102 virtual TString DefaultProtocol() {
return "odbc"; }
103 virtual TString DefaultSubProtocol() {
return "odbc"; }
104 virtual TString DefaultDriver() {
return ""; }
105 virtual TString DefaultHost() {
return "localhost"; }
106 virtual TString DefaultDatabase() {
return ""; }
107 virtual TString DefaultPort() {
return ""; }
108 virtual TString DefaultOptions() {
return ""; }
109 virtual TString DefaultAnchor() {
return ""; }
110 virtual TString DefaultDescription() {
return ""; }
111 virtual TString DefaultDriverPath() {
return ""; }
113 virtual Bool_t ValidProtocol(
const TString&);
114 virtual Bool_t ValidSubProtocol(
const TString&);
115 virtual Bool_t ValidDriver(
const TString&);
116 virtual Bool_t ValidHost(
const TString&);
117 virtual Bool_t ValidDatabase(
const TString&);
118 virtual Bool_t ValidPort(
const TString&);
119 virtual Bool_t ValidOption(
const TString&);
120 virtual Bool_t ValidAnchor(
const TString&);
122 virtual void SetDefaults();
123 virtual void SetFullUrl();
125 TSQLUrlParser():TQObject(),TRefCnt() { }
126 TSQLUrlParser(
const TString&
url);
127 TSQLUrlParser&
operator=(
const TSQLUrlParser& url);
129 virtual Bool_t
IsValid() {
return fStatus==0; }
160 const char* strCopy = str.Data();
210 Int_t idx1 =
fString.First(term);
237 Int_t idx1 =
fString.Last(term);
239 if(idx1!=kNPOS && idx1<
fString.Length()) {
250 return str(idx+
fDL,str.Length());
259 TSQLUrlParser::TSQLUrlParser(
const TString&
url):TQObject(),TRefCnt()
271 fProtocol =
parser.Forw(
'/');
273 if(fProtocol.IsNull()) {
276 fProtocol = DefaultProtocol();
277 fDynamicDSN = kFALSE;
285 fSubProtocol =
parser.Forw(
'/');
286 fSubProtocol.ToLower();
289 fDriver=
parser.Forw(
'@');
291 if(fDriver.BeginsWith(
"//")) {
297 fOptions =
parser.Back(
':');
300 fAnchor =
parser.Back(
':');
302 if(fAnchor.IsNull()) fAnchor = DefaultAnchor();
308 if(
parser.GetString().Contains(
":")) {
313 fHost =
parser.GetString();
316 if(fProtocol==
"file") {
317 if( fHost !=
"localhost") {
319 const char* host = 0;
320 if ( fHost.BeginsWith(
"$") ) host = getenv(fHost.Data()+1);
325 else fFile =
"/" + fHost + fFile;
332 void TSQLUrlParser::SetFullUrl()
337 fFullUrl = fProtocol +
":" + fSubProtocol +
":";
338 fFullUrl += fDriver +
"://";
339 if(!(fHost==
"localhost" && fPort.IsNull())) fFullUrl += fHost;
340 fFullUrl += fPort.IsNull() ?
"" :
":";
342 if((fHost==
"localhost"&& fPort.IsNull())) fFullUrl +=
"/";
344 fFullUrl += fOptions.IsNull() ?
"" :
"?";
345 fFullUrl += fOptions;
350 void TSQLUrlParser::SetDefaults()
354 if(fSubProtocol.IsNull()) fSubProtocol = DefaultSubProtocol();
355 if(fDriver.IsNull()) fDriver = DefaultDriver();
356 if(fOptions.IsNull()) fOptions = DefaultOptions();
357 if(fDescription.IsNull()) fDescription = DefaultDescription();
358 if(fFile.IsNull()) fFile = DefaultDatabase();
359 if(fPort.IsNull()) fPort = DefaultPort();
360 if(fHost.IsNull()) fHost = DefaultHost();
370 if(!ValidProtocol(fProtocol)) {
371 str =
"Wrong format of protocol: ";
375 fStatus = fStatus | kErrProtocol;
378 if(!ValidSubProtocol(fSubProtocol)) {
379 str =
"Wrong format of sub-protocol: ";
383 fStatus = fStatus | kErrSubProtocol;
386 if(!ValidDriver(fDriver)) {
387 str =
"Wrong Driver : ";
391 fStatus = fStatus | kErrDriver;
394 if(!ValidHost(fHost)) {
395 str =
"Wrong format of host: ";
399 fStatus = fStatus | kErrHost;
402 if(!ValidDatabase(fFile)) {
403 str =
"Wrong format of file/database: ";
407 fStatus = fStatus | kErrFile;
410 if(!ValidPort(fPort)) {
411 str =
"Wrong port: ";
415 fStatus = fStatus | kErrPort;
418 if(!ValidOption(fOptions)) {
419 str =
"Wrong format of option: ";
423 fStatus = fStatus | kErrOption;
429 static Int_t nConnect = 0;
430 fDSN = fProtocol + Form(
"%d-%d",gSystem->GetPid(),++nConnect);
440 fDriver = url.fDriver;
441 fFullUrl = url.fFullUrl;
443 fProtocol = url.fProtocol;
444 fSubProtocol = url.fSubProtocol;
447 fAnchor = url.fAnchor;
448 fOptions = url.fOptions;
449 fDescription = url.fDescription;
451 fStatus = url.fStatus;
452 fDynamicDSN = url.fDynamicDSN;
458 Bool_t TSQLUrlParser::ValidProtocol(
const TString& prot)
462 const char* str = prot.Data();
464 for (Ssiz_t
i = 0;
i < prot.Length();
i++) {
465 if( !isalnum(str[
i]) && str[
i]!=
'-' )
return kFALSE;
471 Bool_t TSQLUrlParser::ValidSubProtocol(
const TString& prot)
474 const char* str = prot.Data();
476 for (Ssiz_t
i = 0;
i < prot.Length();
i++) {
477 if( !isalnum(str[
i]) && str[
i]!=
'-' )
return kFALSE;
483 Bool_t TSQLUrlParser::ValidDriver(
const TString& drvname)
494 TString so =
"."; so += gSystem->GetSoExt();
496 TString
name = chrStr = gSystem->ExpandPathName(drvname.Data());
504 if(str==name)
return kTRUE;
508 while((obj=(TNamed*)nexta())) {
510 str.ReplaceAll(
" ",
"");
511 str.ReplaceAll(
"\t",
"");
512 Int_t
i = str.Index(
"=");
514 value =
str(i+1,str.Length()-i-1);
516 if(key==
"driver" && value==name)
return kTRUE;
521 Bool_t af = gSystem->IsAbsoluteFileName(name.Data());
524 if(!name.BeginsWith(
"lib")) name =
"lib" + name;
525 if(!name.EndsWith(so.Data())) name += so;
527 str2 = chrStr = gSystem->ConcatFileName(DefaultDriverPath().
Data(),name.Data());
529 if(!gSystem->AccessPathName(str2.Data())) { fDriver = str2;
return kTRUE; }
531 str2 = chrStr = gSystem->ConcatFileName(
"/usr/lib",name.Data());
533 if(!gSystem->AccessPathName(str2.Data())) { fDriver = str2;
return kTRUE; }
535 str2 = chrStr = gSystem->ConcatFileName(
"/usr/local/lib",name.Data());
537 if(!gSystem->AccessPathName(str2.Data())) { fDriver = str2;
return kTRUE; }
539 TString rootsys = chrStr = gSystem->ExpandPathName(
"$ROOTSYS/lib");
542 str2 = chrStr = gSystem->ConcatFileName(rootsys.Data(),name.Data());
544 if(!gSystem->AccessPathName(str2.Data())) { fDriver = str2;
return kTRUE; }
547 TString ldlib = gSystem->Getenv(
"LD_LIBRARY_PATH");
549 if(!ldlib.IsNull()) {
555 sep = ldlib.Index(
":",prev);
556 if(sep==kNPOS)
break;
557 sublib = ldlib(prev,sep-prev);
560 if( (sublib==
"/usr/lib") ||
561 (sublib==
"/usr/local/lib") ||
562 (sublib==rootsys) ) {
563 prev += sublib.Length()+1;
568 str2 = chrStr = gSystem->ConcatFileName(sublib.Data(),name.Data());
570 if(!gSystem->AccessPathName(str2.Data())) { fDriver = str2;
return kTRUE; }
572 sublib = ldlib(prev,ldlib.Length()-prev);
573 str2 = chrStr = gSystem->ConcatFileName(sublib.Data(),name.Data());
575 if(!gSystem->AccessPathName(str2.Data())) { fDriver = str2;
return kTRUE; }
580 if(!gSystem->AccessPathName(name.Data())) {
592 Bool_t TSQLUrlParser::ValidHost(
const TString& host)
595 const char* str = host.Data();
598 for (Ssiz_t i = 0; i < host.Length(); i++) {
599 if( (!isalnum(str[i]) && str[i]!=
'.' && str[i]!=
'-') ||
600 (str[
i]==
'.' && prevch==
'.') )
return kFALSE;
607 Bool_t TSQLUrlParser::ValidDatabase(
const TString&
db)
611 const char* str = db.Data();
613 for (Ssiz_t i = 0; i < db.Length(); i++) {
614 if( !isascii(str[i]) )
return kFALSE;
620 Bool_t TSQLUrlParser::ValidPort(
const TString& port)
624 const char* str = port.Data();
629 Bool_t TSQLUrlParser::ValidOption(
const TString& )
636 Bool_t TSQLUrlParser::ValidAnchor(
const TString&)
655 virtual TString
DefaultDescription() {
return "Connection via OpenLink ODBC driver to Oracle database"; }
666 if(fFile.BeginsWith(
"@")) {
668 fFile = parser.
Skip(1);
669 if(fFile.Contains(
":")) {
672 fHost = parser.
Forw(0);
673 fPort = parser.
Forw(0);
689 if(fSubProtocol==
"odbc") {
701 if(fSubProtocol==
"odbc") {
702 return "ServerType=Oracle 8;"
705 "FetchBufferSize=100;"
736 if(fSubProtocol==
"odbc") {
754 virtual TString
DefaultDescription() {
return "Connection via psqlodbc driver to PostgreSQL database"; }
776 Int_t i1 = fullurl.Index(
"=");
777 Int_t i2 = fullurl.Index(
";",i1);
783 fDSN = fullurl(i1+1,i2-i1-1);
784 fOptions = fullurl(i2+1,fullurl.Length()-i2-1);
785 fDynamicDSN = kFALSE;
795 TString fullurl(url.Data());
796 fullurl.ReplaceAll(
" ",
"");
798 if (fullurl.BeginsWith(
"oracle:",TString::kIgnoreCase)) {
fParser =
new OracleParser(fullurl);
goto exit; }
799 if (fullurl.BeginsWith(
"mysql:",TString::kIgnoreCase)) {
fParser =
new MySQLParser(fullurl);
goto exit; }
800 if (fullurl.BeginsWith(
"postgresql:",TString::kIgnoreCase)) {
fParser =
new PostgreSQLParser(fullurl);
goto exit; }
804 if(fullurl.BeginsWith(
"/")) fullurl =
"file:" + fullurl;
807 fParser =
new TSQLUrlParser(fullurl);
819 }
else fParser->AddReference();
829 fParser =
new TSQLUrlParser(dsn);
948 const char* str = (
const char*)
fParser->fPort;
973 int l =
fParser->fOptions.Length();
975 if(l)
fParser->fFullUrl +=
"?";
977 if(!l || ( l && !
fParser->fOptions.Last(
';')))
fParser->fOptions +=
";";
978 fParser->fOptions += opt +
";";
979 fParser->fFullUrl += opt +
";";
1013 else fd = fopen(option,
"w");
1021 if(!
fParser->fDescription.IsNull())
fprintf(fd,
"Description\t\t= %s\n",
fParser->fDescription.Data());
1029 if(!
fParser->fOptions.IsNull()) {
1031 str.ReplaceAll(
";",
"\n");
1032 str.ReplaceAll(
"&",
"\n");
1033 str.ReplaceAll(
"=",
"\t\t= ");