00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "BDSGlobalConstants.hh"
00012
00013 #include <cstdlib>
00014 #include "BDSMaterials.hh"
00015 #include "BDSMySQLWrapper.hh"
00016 #include "BDSMySQLTable.hh"
00017
00018 using namespace std;
00019
00020 #include"globals.hh"
00021 #include<string>
00022 #include<vector>
00023
00024 extern BDSMaterials* theMaterials;
00025
00026 void Log(const G4String& tag, int depth, ostream& os)
00027 {
00028 static const char* tab = "----|";
00029 while(depth--) os<<tab;
00030 os<<' '<<tag<<endl;
00031 }
00032
00033 void StripHeader(istream& is)
00034 {
00035
00036 char buffer[256];
00037 for (int i=0; i<6; i++)
00038 is.getline(buffer,256);
00039 }
00040
00041 inline G4String StripQuotes(const G4String& text)
00042 {
00043 if(text=="") return text;
00044 return text.substr(1,text.length()-2);
00045 }
00046
00047 inline vector<G4String> StripComma(const G4String& text)
00048 {
00049 vector<G4String> strippedtext;
00050 G4String comma_text = text;
00051 if(text==',')
00052 {
00053 return strippedtext;
00054 }
00055
00056 G4int length = comma_text.length();
00057 G4int curr=0;
00058 while(comma_text.contains(','))
00059 {
00060 curr = comma_text.first(',');
00061
00062 if(curr!=0) strippedtext.push_back(comma_text.substr(0,curr));
00063
00064
00065 if(curr+1>=length)
00066 {
00067 comma_text="";
00068 break;
00069 }
00070 else comma_text = comma_text.substr(curr+1,length);
00071 }
00072 if(comma_text.length()>0) strippedtext.push_back(comma_text);
00073
00074 return strippedtext;
00075
00076 }
00077 inline G4String StripFirst(const G4String& text)
00078 {
00079 if(text=="") return text;
00080 return text.substr(1,text.length());
00081 }
00082 inline G4String StripEnd(const G4String& text)
00083 {
00084 if(text=="") return text;
00085 return text.substr(0,text.length()-2);
00086 }
00087
00088 BDSMySQLWrapper::BDSMySQLWrapper (const G4String& SQLFileName)
00089 : ifs(SQLFileName.c_str())
00090
00091 {
00092 if(ifs) G4cout<<"Loading SQL Filename="<<SQLFileName<<G4endl;
00093 else G4cout<<"Unable to load SQL file: "<<SQLFileName<<G4endl;
00094 }
00095
00096 vector<BDSMySQLTable*> BDSMySQLWrapper::ConstructTable ()
00097 {
00098
00099 ifs.seekg(0);
00100 ComponentN=0;
00101 tableN=-1;
00102 while(ifs) ComponentN+=ReadComponent();
00103 ifs.close();
00104 return table;
00105 }
00106
00107
00108 BDSMySQLWrapper::~BDSMySQLWrapper()
00109 {
00110 }
00111
00112 G4int BDSMySQLWrapper::ReadComponent()
00113 {
00114 #define _READ(value) if(!(ifs>>value)) return 0;
00115
00116 #define CMD_CREATE "CREATE"
00117 #define CMD_TABLE "TABLE"
00118 #define CMD_INSERT "INSERT"
00119 #define CMD_INTO "INTO"
00120 #define CMD_VALUES "VALUES"
00121 #define CMD_DROP "DROP"
00122 #define CMD_DATABASE "DATABASE"
00123 #define CMD_USE "USE"
00124 #define CMD_IF "IF"
00125 #define CMD_EXISTS "EXISTS"
00126
00127 G4String input;
00128 G4String varname;
00129 G4String vartype;
00130
00131 char buffer[255];
00132 _READ(input);
00133 if(input.contains("#")){
00134 ifs.getline(buffer,255);
00135 return 0;
00136 }
00137
00138
00139 if(input==CMD_DROP) return 0;
00140 else if(input==CMD_DATABASE) return 0;
00141 else if(input==CMD_IF) return 0;
00142 else if(input==CMD_EXISTS) return 0;
00143 else if(input==CMD_USE) return 0;
00144
00145 else if(input==CMD_CREATE)
00146 {
00147 _READ(input);
00148 if(input==CMD_DATABASE) return 0;
00149 else if(input==CMD_TABLE)
00150 {
00151 _READ(CurrentTableName);
00152 ifs.getline(buffer,255);
00153 table.push_back(new BDSMySQLTable(CurrentTableName));
00154 tableN++;
00155
00156 do
00157 {
00158 _READ(varname);
00159 if(!varname.contains(";"))
00160 {
00161 _READ(vartype);
00162 if(vartype.contains("DOUBLE")) vartype="DOUBLE";
00163 else if(vartype.contains("VARCHAR")) vartype="STRING";
00164 else if(vartype.contains("INTEGER")) vartype="INTEGER";
00165
00166 table[tableN]->AddVariable(varname,vartype);
00167 ifs.getline(buffer,255);
00168 }
00169 }
00170 while (!varname.contains(";"));
00171 }
00172 return 0;
00173 }
00174
00175 else if(input==CMD_INSERT)
00176 {
00177 _READ(input);
00178 if(input==CMD_INTO)
00179 {
00180 _READ(InsertTableName);
00181
00182 _READ(input);
00183 if(input==CMD_VALUES)
00184 {
00185
00186 for(G4int j=0; j<(G4int)table.size(); j++)
00187 {
00188 if(table[j]->GetName()==InsertTableName)
00189 {
00190 for(G4int k=0; k<table[j]->GetNVariables(); k++)
00191 {
00192 _READ(input);
00193
00194 if(input=="(") _READ(input);
00195 if(input.contains("(")) input = StripFirst(input);
00196 if(input.contains(");")) input = StripEnd(input);
00197
00198 if(input.contains(","))
00199 {
00200 vector<G4String> vctInput = StripComma(input);
00201 if(vctInput.size()==0) k--;
00202 for(G4int i=0; i<(G4int)vctInput.size(); i++)
00203 {
00204
00205 if(table[j]->GetVariable(k)->GetVarType()=="DOUBLE")
00206 table[j]->GetVariable(k)->AddValue(atof(vctInput[i])*mm);
00207 else if(table[j]->GetVariable(k)->GetVarType()=="STRING")
00208 table[j]->GetVariable(k)->AddValue(StripQuotes(vctInput[i]));
00209 else if(table[j]->GetVariable(k)->GetVarType()=="INTEGER")
00210 table[j]->GetVariable(k)->AddValue(atoi(vctInput[i]));
00211 if(i!=(G4int)vctInput.size()-1) k++;
00212 }
00213 }
00214 else
00215 {
00216
00217 if(table[j]->GetVariable(k)->GetVarType()=="DOUBLE")
00218 table[j]->GetVariable(k)->AddValue(atof(input)*mm);
00219 else if(table[j]->GetVariable(k)->GetVarType()=="STRING")
00220 table[j]->GetVariable(k)->AddValue(StripQuotes(input));
00221 else if(table[j]->GetVariable(k)->GetVarType()=="INTEGER")
00222 table[j]->GetVariable(k)->AddValue(atoi(input));
00223
00224 }
00225
00226 }
00227 }
00228
00229 }
00230 }
00231 }
00232 return 1;
00233 }
00234 else return 0;
00235 }