00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "BDSGlobalConstants.hh"
00012 #include "BDSDebug.hh"
00013
00014 #include <cstdlib>
00015 #include "BDSMySQLWrapper.hh"
00016 #include "BDSMySQLTable.hh"
00017
00018 #include<iostream>
00019 #include<boost/tokenizer.hpp>
00020
00021 #include"globals.hh"
00022 #include<string>
00023 #include<vector>
00024 #include<sstream>
00025
00026 BDSMySQLWrapper::BDSMySQLWrapper (const G4String& SQLFileName)
00027 : ifs(SQLFileName.c_str()), ComponentN(0), tableN(-1)
00028
00029 {
00030 _startOfFile=true;
00031 BeginTokens();
00032
00033 if(ifs) {
00034 #ifdef BDSDEBUG
00035 G4cout<<"BDSMySQLWrapper contructor: Loading SQL Filename="<<SQLFileName<<G4endl;
00036 #endif
00037 }
00038 else {
00039 G4cout<<"BDSMySQLWrapper constructor: Unable to load SQL file: "<<SQLFileName<<G4endl;
00040 exit(1);
00041 }
00042 }
00043
00044 BDSMySQLWrapper::~BDSMySQLWrapper()
00045 {
00046 }
00047
00048 std::vector<BDSMySQLTable*> BDSMySQLWrapper::ConstructTable ()
00049 {
00050 ComponentN=0;
00051 tableN=-1;
00052 while(NextToken()){
00053 ParseComponent();
00054 }
00055 return table;
00056 }
00057
00058 void BDSMySQLWrapper::TokenizeLine(){
00059 std::string token;
00060 _tokens.clear();
00061 typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
00062
00063
00064 boost::char_separator<char> sep(", ",";()\n");
00065 tokenizer tok(_currentLine, sep);
00066 for(tokenizer::iterator tok_iter=tok.begin(); tok_iter != tok.end(); ++tok_iter){
00067 token = *tok_iter;
00068 RemoveQuotesFromLine(token);
00069 RemoveWhitespace(token);
00070 _tokens.push_back(token);
00071 #ifdef BDSDEBUG
00072 G4cout << __METHOD_NAME__ << " - token: = <" << token << ">" << G4endl;
00073 #endif
00074 }
00075 BeginTokens();
00076 }
00077
00078 bool BDSMySQLWrapper::NextToken(){
00079 std::string line;
00080 ++_tokens_iter;
00081 if(_startOfFile){
00082 --_tokens_iter;
00083 }
00084 if(_startOfFile || EndTokens()){
00085 if(ifs.good()){
00086 ReadLine();
00087 TokenizeLine();
00088 #ifdef BDSDEBUG
00089 G4cout << __METHOD_NAME__ << " - Token() = " << Token() << G4endl;
00090 #endif
00091 }else{
00092 return false;
00093 }
00094 } else {
00095 #ifdef BDSDEBUG
00096 G4cout << __METHOD_NAME__ << " - Token() = " << Token() << G4endl;
00097 #endif
00098 }
00099 return true;
00100 }
00101
00102
00103
00104 G4int BDSMySQLWrapper::ParseComponent()
00105 {
00106
00107 if(Token()==CMD_DROP) { return 0;}
00108 else if(Token()==CMD_DATABASE) { return 0;}
00109 else if(Token()==CMD_IF) { return 0;}
00110 else if(Token()==CMD_EXISTS) { return 0;}
00111 else if(Token()==CMD_USE) { return 0;}
00112
00113 else if(Token()==CMD_CREATE) {Create(); return 0;}
00114 else if(Token()==CMD_INSERT) {Insert(); ComponentN++; return 0;}
00115 return 0;
00116 }
00117
00118
00119 void BDSMySQLWrapper::CreateDatabase(){
00120 }
00121
00122 void BDSMySQLWrapper::CreateTable(){
00123 G4String varname;
00124 G4String vartype;
00125
00126 _NEXTINPUT
00127 CurrentTableName=Token();
00128 #ifdef BDSDEBUG
00129 G4cout << __METHOD_NAME__ << " reading CurrentTableName: " << CurrentTableName << G4endl;
00130 #endif
00131 ProceedToEndOfLine();
00132 table.push_back(new BDSMySQLTable(CurrentTableName));
00133 tableN++;
00134
00135 while((!varname.contains(";")) && (!vartype.contains(";"))){
00136
00137 do{
00138 _NEXTINPUT
00139 } while((EmptyToken()) || (EndOfLine()));
00140 varname=Token();
00141 #ifdef BDSDEBUG
00142 G4cout << __METHOD_NAME__ << " reading varname: " << varname << G4endl;
00143 #endif
00144 if (varname.contains(";")) return;
00145 _NEXTINPUT
00146 vartype=Token();
00147 #ifdef BDSDEBUG
00148 G4cout << __METHOD_NAME__ << " reading vartype: " << vartype << G4endl;
00149 #endif
00150 if (vartype.contains(";")) return;
00151 if(vartype.contains("DOUBLE")) vartype="DOUBLE";
00152 else if(vartype.contains("VARCHAR")) vartype="STRING";
00153 else if(vartype.contains("INTEGER")) vartype="INTEGER";
00154 table[tableN]->AddVariable(varname,vartype);
00155 ProceedToEndOfLine();
00156 }
00157 }
00158
00159
00160 void BDSMySQLWrapper::Create(){
00161 _NEXT
00162 #ifdef BDSDEBUG
00163 G4cout << __METHOD_NAME__ << " reading input: " << Token() << G4endl;
00164 #endif
00165 if(Token()==CMD_DATABASE) {CreateDatabase();}
00166 else if(Token()==CMD_TABLE) {CreateTable();}
00167 }
00168
00169 void BDSMySQLWrapper::Insert(){
00170 _NEXT
00171 #ifdef BDSDEBUG
00172 G4cout << __METHOD_NAME__ << " reading input: " << Token() << G4endl;
00173 #endif
00174 if(Token()==CMD_INTO){InsertInto();}
00175 }
00176
00177 void BDSMySQLWrapper::InsertInto() {
00178 _NEXT
00179 InsertTableName=Token();
00180 _NEXT
00181 if(Token()==CMD_VALUES){Values();}
00182 }
00183
00184 void BDSMySQLWrapper::Values() {
00185 for(G4int j=0; j<(G4int)table.size(); j++){
00186 if(table[j]->GetName()==InsertTableName){
00187 for(G4int k=0; k<table[j]->GetNVariables(); k++){
00188 _NEXTINPUT
00189
00190 while (Token()=="(") {
00191 _NEXTINPUT
00192 }
00193 if (Token()==")") {
00194
00195
00196
00197
00198 }
00199 #ifdef BDSDEBUG
00200 G4cout << __METHOD_NAME__ << " inserting value " << Token() << G4endl;
00201 #endif
00202 if(table[j]->GetVariable(k)->GetVarType()=="DOUBLE")
00203 table[j]->GetVariable(k)->AddValue(atof(Token().c_str())*CLHEP::mm);
00204 else if(table[j]->GetVariable(k)->GetVarType()=="STRING")
00205 table[j]->GetVariable(k)->AddValue(Token().c_str());
00206 else if(table[j]->GetVariable(k)->GetVarType()=="INTEGER")
00207 table[j]->GetVariable(k)->AddValue(atoi(Token().c_str()));
00208 }
00209 }
00210 }
00211 }
00212
00213 void BDSMySQLWrapper::BeginTokens(){
00214 _tokens_iter=_tokens.begin();
00215 }
00216
00217 bool BDSMySQLWrapper::NextInputToken(){
00218 if(ifs.good()){
00219 bool status;
00220 do{
00221 status=NextToken();
00222 } while (EndOfLine());
00223 return status;
00224 } else {
00225 return false;
00226 }
00227 }
00228
00229 bool BDSMySQLWrapper::EndTokens(){
00230 return(_tokens_iter==_tokens.end());
00231 }
00232
00233 bool BDSMySQLWrapper::EndOfLine(){
00234 return((*_tokens_iter)=="\n");
00235 }
00236
00237 bool BDSMySQLWrapper::EmptyToken(){
00238 return((*_tokens_iter)=="");
00239 }
00240
00241 std::string BDSMySQLWrapper::Token(){
00242 return *_tokens_iter;
00243 }
00244
00245 void BDSMySQLWrapper::ProceedToEndOfLine(){
00246 #ifdef BDSDEBUG
00247 G4cout << __METHOD_NAME__ << G4endl;
00248 #endif
00249 while(!EndOfLine()){
00250 _NEXT
00251 #ifdef BDSDEBUG
00252 G4cout << __METHOD_NAME__ << " " << Token() << G4endl;
00253 #endif
00254 }
00255 #ifdef BDSDEBUG
00256 G4cout << __METHOD_NAME__ << " finished." << G4endl;
00257 #endif
00258 }
00259
00260 void BDSMySQLWrapper::ReadLine(){
00261 _currentLine.clear();
00262 _startOfFile=false;
00263
00264 char buffer[512];
00265 ifs.getline(buffer,512);
00266 _currentLine=buffer;
00267 RemoveCommentsFromLine(_currentLine);
00268
00269 _currentLine += ' ';
00270 _currentLine += '\n';
00271 #ifdef BDSDEBUG
00272 G4cout << __METHOD_NAME__ << " - _currentLine = " << _currentLine << G4endl;
00273 #endif
00274 }
00275
00276 void BDSMySQLWrapper::RemoveCommentsFromLine(std::string& value){
00277
00278 unsigned pos = value.find('#');
00279 std::string uncommented = value.substr(0,pos);
00280 value=uncommented;
00281 }
00282
00283 void BDSMySQLWrapper::RemoveQuotesFromLine(std::string& value){
00284
00285 value.erase (std::remove(value.begin(), value.end(), '\"'), value.end());
00286 }
00287
00288
00289 void BDSMySQLWrapper::RemoveWhitespace(G4String& val){
00290 std::string s = val;
00291 RemoveWhitespace(s);
00292 val=(G4String)s;
00293 }
00294
00295 void BDSMySQLWrapper::RemoveWhitespace(std::string& val){
00296 val.erase( std::remove_if( val.begin(), val.end(), ::isblank ), val.end() );
00297 }
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311