| 1 | #include "Database.h"
|
|---|
| 2 |
|
|---|
| 3 | #include "Time.h"
|
|---|
| 4 | #include "Configuration.h"
|
|---|
| 5 |
|
|---|
| 6 | #include <TROOT.h>
|
|---|
| 7 | #include <TSystem.h>
|
|---|
| 8 | #include <TFile.h>
|
|---|
| 9 | #include <TTree.h>
|
|---|
| 10 |
|
|---|
| 11 | using namespace std;
|
|---|
| 12 |
|
|---|
| 13 | // ------------------------------------------------------------------------
|
|---|
| 14 |
|
|---|
| 15 | void SetupConfiguration(Configuration &conf)
|
|---|
| 16 | {
|
|---|
| 17 | po::options_description control("Rootify SQL");
|
|---|
| 18 | control.add_options()
|
|---|
| 19 | ("uri,u", var<string>()->required(), "Database link as in\n\tuser:password@server[:port]/database.")
|
|---|
| 20 | ("query,q", var<string>(""), "MySQL query (overwrites --file)")
|
|---|
| 21 | ("file", var<string>("rootify.sql"), "An ASCII file with the MySQL query (overwrites --query)")
|
|---|
| 22 | ("ignore-null,i", po_switch(), "Do not skip rows containing any NULL field")
|
|---|
| 23 | ("out,o", var<string>("rootify.root"), "Output root file name")
|
|---|
| 24 | ("force,f", po_switch(), "Force overwriting an existing root file ('RECREATE')")
|
|---|
| 25 | ("update", po_switch(), "Update an existing root file with the new tree ('UPDATE')")
|
|---|
| 26 | ("compression,c", var<uint16_t>(1), "zlib compression level for the root file")
|
|---|
| 27 | ("tree,t", var<string>("Result"), "Name of the root tree")
|
|---|
| 28 | ("display,d", po_switch(), "Displays contents on the screen (most usefull in combination with mysql statements as SHOW or EXPLAIN)")
|
|---|
| 29 | ("delimiter", var<string>(""), "The delimiter used if contents are displayed with --display (default=\\t)")
|
|---|
| 30 | ("verbose,v", var<uint16_t>(1), "Verbosity (0: quiet, 1: default, 2: more, 3, ...)")
|
|---|
| 31 | ;
|
|---|
| 32 |
|
|---|
| 33 | po::positional_options_description p;
|
|---|
| 34 | p.add("file", 1); // The 1st positional options
|
|---|
| 35 | p.add("out", 2); // The 2nd positional options
|
|---|
| 36 |
|
|---|
| 37 | conf.AddOptions(control);
|
|---|
| 38 | conf.SetArgumentPositions(p);
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | void PrintUsage()
|
|---|
| 42 | {
|
|---|
| 43 | cout <<
|
|---|
| 44 | "rootifysql - Converts the result of a mysql query into a root file\n"
|
|---|
| 45 | "\n"
|
|---|
| 46 | "Writes the result of a mysql query into a root file. For each column, a branch is "
|
|---|
| 47 | "created of type double with the field name as name. This is usually the column name "
|
|---|
| 48 | "if not specified otherwise by the AS mysql directive.\n"
|
|---|
| 49 | "\n"
|
|---|
| 50 | "Columns with CHAR or VARCHAR as field type are ignored. DATETIME, DATE and TIME "
|
|---|
| 51 | "columns are converted to unix time (time_t). Rows containing any file which is "
|
|---|
| 52 | "NULL are skipped if not suppressed by the --ignore-null option. Ideally, the query "
|
|---|
| 53 | "is compiled in a way that no NULL field is returned. With the --display option the "
|
|---|
| 54 | "result of the request is printed on the screen (NULL skipping still in action). "
|
|---|
| 55 | "This can be useful to create an ascii file or to show results as 'SHOW DATABASES' "
|
|---|
| 56 | "or 'EXPLAIN table'. To redirect the contents into an ascii file, the option -v0 "
|
|---|
| 57 | "is useful.\n"
|
|---|
| 58 | "\n"
|
|---|
| 59 | "The default is to read the query from a file called rootify.sql. Except if a different "
|
|---|
| 60 | "filename is specified by the --file option or a query is given with --query.\n"
|
|---|
| 61 | "\n"
|
|---|
| 62 | "Comments in the query-file can be placed according to the SQL standard inline "
|
|---|
| 63 | "/*comment*/ or on individual lines introduces with # or --.\n"
|
|---|
| 64 | "\n"
|
|---|
| 65 | "In case of succes, 0 is returned, a value>0 otherwise.\n"
|
|---|
| 66 | "\n"
|
|---|
| 67 | "Usage: rootifysql [rootify.sql [rootify.root]] [-u URI] [-q query|-f file] [-i] [-o out] [-f] [-cN] [-t tree] [-vN]\n"
|
|---|
| 68 | "\n"
|
|---|
| 69 | ;
|
|---|
| 70 | cout << endl;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | int main(int argc, const char* argv[])
|
|---|
| 74 | {
|
|---|
| 75 | Time start;
|
|---|
| 76 |
|
|---|
| 77 | gROOT->SetBatch();
|
|---|
| 78 |
|
|---|
| 79 | Configuration conf(argv[0]);
|
|---|
| 80 | conf.SetPrintUsage(PrintUsage);
|
|---|
| 81 | SetupConfiguration(conf);
|
|---|
| 82 |
|
|---|
| 83 | if (!conf.DoParse(argc, argv))
|
|---|
| 84 | return 127;
|
|---|
| 85 |
|
|---|
| 86 | // ----------------------------- Evaluate options --------------------------
|
|---|
| 87 | const string uri = conf.Get<string>("uri");
|
|---|
| 88 | const string out = conf.Get<string>("out");
|
|---|
| 89 | const string file = conf.Get<string>("file");
|
|---|
| 90 | const string tree = conf.Get<string>("tree");
|
|---|
| 91 | const bool force = conf.Get<bool>("force");
|
|---|
| 92 | const bool ignore = conf.Get<bool>("ignore-null");
|
|---|
| 93 | const bool update = conf.Get<bool>("update");
|
|---|
| 94 | const bool display = conf.Get<bool>("display");
|
|---|
| 95 | const uint16_t verbose = conf.Get<uint16_t>("verbose");
|
|---|
| 96 | const uint16_t compression = conf.Get<uint16_t>("compression");
|
|---|
| 97 | const string delimiter = conf.Get<string>("delimiter");
|
|---|
| 98 | // -------------------------------------------------------------------------
|
|---|
| 99 |
|
|---|
| 100 | if (verbose>0)
|
|---|
| 101 | cout << "\n--------------------- Rootify SQL ----------------------" << endl;
|
|---|
| 102 |
|
|---|
| 103 | string query = conf.Get<string>("query");
|
|---|
| 104 | if (query.empty())
|
|---|
| 105 | {
|
|---|
| 106 | if (verbose>0)
|
|---|
| 107 | cout << "Reading query from file '" << file << "'." << endl;
|
|---|
| 108 |
|
|---|
| 109 | ifstream fin(file);
|
|---|
| 110 | if (!fin)
|
|---|
| 111 | {
|
|---|
| 112 | cerr << "Could not open '" << file << "': " << strerror(errno) << endl;
|
|---|
| 113 | return 1;
|
|---|
| 114 | }
|
|---|
| 115 | getline(fin, query, (char)fin.eof());
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | if (query.empty())
|
|---|
| 119 | {
|
|---|
| 120 | cerr << "No query specified." << endl;
|
|---|
| 121 | return 2;
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | // -------------------------- Check for file permssion ---------------------
|
|---|
| 125 | // Strictly speaking, checking for write permission and existance is not necessary,
|
|---|
| 126 | // but it is convenient that the user does not find out that it failed after
|
|---|
| 127 | // waiting long for the query result
|
|---|
| 128 | //
|
|---|
| 129 | // I am using root here instead of boost to be
|
|---|
| 130 | // consistent with the access pattern by TFile
|
|---|
| 131 | TString path(out.c_str());
|
|---|
| 132 | gSystem->ExpandPathName(path);
|
|---|
| 133 |
|
|---|
| 134 | FileStat_t stat;
|
|---|
| 135 | const Int_t exist = !gSystem->GetPathInfo(path, stat);
|
|---|
| 136 | const Bool_t write = !gSystem->AccessPathName(path, kWritePermission) && R_ISREG(stat.fMode);
|
|---|
| 137 |
|
|---|
| 138 | if ((update && !exist) || (update && exist && !write) || (force && exist && !write))
|
|---|
| 139 | {
|
|---|
| 140 | cerr << "File '" << path << "' is not writable." << endl;
|
|---|
| 141 | return 3;
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | if (!update && !force && exist)
|
|---|
| 145 | {
|
|---|
| 146 | cerr << "File '" << path << "' already exists." << endl;
|
|---|
| 147 | return 4;
|
|---|
| 148 | }
|
|---|
| 149 | // -------------------------------------------------------------------------
|
|---|
| 150 |
|
|---|
| 151 | if (query.back()!='\n')
|
|---|
| 152 | query += '\n';
|
|---|
| 153 |
|
|---|
| 154 | if (verbose>1)
|
|---|
| 155 | cout << '\n' << query << endl;
|
|---|
| 156 |
|
|---|
| 157 | if (verbose>0)
|
|---|
| 158 | cout << "Requesting data..." << endl;
|
|---|
| 159 |
|
|---|
| 160 | Time start2;
|
|---|
| 161 |
|
|---|
| 162 | // -------------------------- Request data from database -------------------
|
|---|
| 163 | const mysqlpp::StoreQueryResult res =
|
|---|
| 164 | Database(uri).query(query).store();
|
|---|
| 165 | // -------------------------------------------------------------------------
|
|---|
| 166 |
|
|---|
| 167 | if (verbose>0)
|
|---|
| 168 | cout << "Query time: " << Time().UnixTime()-start2.UnixTime() << "s" << endl;
|
|---|
| 169 |
|
|---|
| 170 | if (res.empty())
|
|---|
| 171 | {
|
|---|
| 172 | cerr << "No results returned." << endl;
|
|---|
| 173 | return 5;
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | if (verbose>0)
|
|---|
| 177 | {
|
|---|
| 178 | cout << res.size() << " entries received." << endl;
|
|---|
| 179 | cout << "Opening file '" << path << "' [compression=" << compression << "]..." << endl;
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | // ----------------------------- Open output file --------------------------
|
|---|
| 183 | TFile tfile(path, update?"UPDATE":(force?"RECREATE":"CREATE"), "Rootify SQL", compression);
|
|---|
| 184 | if (tfile.IsZombie())
|
|---|
| 185 | return 6;
|
|---|
| 186 | // -------------------------------------------------------------------------
|
|---|
| 187 |
|
|---|
| 188 | if (verbose>0)
|
|---|
| 189 | cout << "Configuring branches..." << endl;
|
|---|
| 190 |
|
|---|
| 191 | if (verbose>2)
|
|---|
| 192 | cout << endl;
|
|---|
| 193 |
|
|---|
| 194 | const mysqlpp::Row &r = res.front();
|
|---|
| 195 | const mysqlpp::FieldNames &l = *r.field_list().list;
|
|---|
| 196 |
|
|---|
| 197 | vector<double> buf(l.size());
|
|---|
| 198 | vector<uint8_t> typ(l.size(),'-');
|
|---|
| 199 |
|
|---|
| 200 | // -------------------- Configure branches of TTree ------------------------
|
|---|
| 201 | TTree *ttree = new TTree(tree.c_str(), query.c_str());
|
|---|
| 202 | for (size_t i=0; i<l.size(); i++)
|
|---|
| 203 | {
|
|---|
| 204 | ttree->Branch(l[i].c_str(), buf.data()+i);
|
|---|
| 205 |
|
|---|
| 206 | const string t = r[i].type().sql_name();
|
|---|
| 207 |
|
|---|
| 208 | if (t.find("DATETIME")!=string::npos)
|
|---|
| 209 | typ[i] = 'd';
|
|---|
| 210 | else
|
|---|
| 211 | if (t.find("DATE")!=string::npos)
|
|---|
| 212 | typ[i] = 'D';
|
|---|
| 213 | else
|
|---|
| 214 | if (t.find("TIME")!=string::npos)
|
|---|
| 215 | typ[i] = 'T';
|
|---|
| 216 | else
|
|---|
| 217 | if (t.find("VARCHAR")!=string::npos)
|
|---|
| 218 | typ[i] = 'V';
|
|---|
| 219 | else
|
|---|
| 220 | if (t.find("CHAR")!=string::npos)
|
|---|
| 221 | typ[i] = 'C';
|
|---|
| 222 |
|
|---|
| 223 | if (verbose>2)
|
|---|
| 224 | cout << l[i].c_str() << " [" << typ[i] << "] " << t << '\n';
|
|---|
| 225 | }
|
|---|
| 226 | // -------------------------------------------------------------------------
|
|---|
| 227 |
|
|---|
| 228 | if (verbose>2)
|
|---|
| 229 | cout << endl;
|
|---|
| 230 | if (verbose>0)
|
|---|
| 231 | cout << "Filling branches..." << endl;
|
|---|
| 232 |
|
|---|
| 233 | if (display)
|
|---|
| 234 | {
|
|---|
| 235 | cout << endl;
|
|---|
| 236 | cout << "#";
|
|---|
| 237 | for (size_t i=0; i<l.size(); i++)
|
|---|
| 238 | cout << ' ' << l[i].c_str();
|
|---|
| 239 | cout << endl;
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | // ---------------------- Fill TTree with DB data --------------------------
|
|---|
| 243 | for (auto row=res.begin(); row<res.end(); row++)
|
|---|
| 244 | {
|
|---|
| 245 | ostringstream sout;
|
|---|
| 246 |
|
|---|
| 247 | size_t idx=0;
|
|---|
| 248 | for (auto col=row->begin(); col!=row->end(); col++, idx++)
|
|---|
| 249 | {
|
|---|
| 250 | if (display)
|
|---|
| 251 | {
|
|---|
| 252 | if (idx>0)
|
|---|
| 253 | sout << (delimiter.empty()?"\t":delimiter);
|
|---|
| 254 | sout << col->c_str();
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | if (!ignore && col->is_null())
|
|---|
| 258 | break;
|
|---|
| 259 |
|
|---|
| 260 | switch (typ[idx])
|
|---|
| 261 | {
|
|---|
| 262 | case 'd':
|
|---|
| 263 | buf[idx] = time_t((mysqlpp::DateTime)(*col));
|
|---|
| 264 | break;
|
|---|
| 265 |
|
|---|
| 266 | case 'D':
|
|---|
| 267 | buf[idx] = time_t((mysqlpp::Date)(*col));
|
|---|
| 268 | break;
|
|---|
| 269 |
|
|---|
| 270 | case 'T':
|
|---|
| 271 | buf[idx] = time_t((mysqlpp::Time)(*col));
|
|---|
| 272 | break;
|
|---|
| 273 |
|
|---|
| 274 | case 'V':
|
|---|
| 275 | case 'C':
|
|---|
| 276 | break;
|
|---|
| 277 |
|
|---|
| 278 | default:
|
|---|
| 279 | buf[idx] = atof(col->c_str());
|
|---|
| 280 | }
|
|---|
| 281 | }
|
|---|
| 282 |
|
|---|
| 283 | if (idx==row->size())
|
|---|
| 284 | {
|
|---|
| 285 | ttree->Fill();
|
|---|
| 286 | if (display)
|
|---|
| 287 | cout << sout.str() << endl;
|
|---|
| 288 | }
|
|---|
| 289 | }
|
|---|
| 290 | // -------------------------------------------------------------------------
|
|---|
| 291 |
|
|---|
| 292 | if (display)
|
|---|
| 293 | cout << '\n' << endl;
|
|---|
| 294 |
|
|---|
| 295 | if (verbose>0)
|
|---|
| 296 | cout << ttree->GetEntries() << " filled into tree." << endl;
|
|---|
| 297 |
|
|---|
| 298 | ttree->Write();
|
|---|
| 299 | tfile.Close();
|
|---|
| 300 |
|
|---|
| 301 | if (verbose>0)
|
|---|
| 302 | {
|
|---|
| 303 | cout << "File closed.\n";
|
|---|
| 304 | cout << "Execution time: " << Time().UnixTime()-start.UnixTime() << "s\n";
|
|---|
| 305 | cout << "--------------------------------------------------------" << endl;
|
|---|
| 306 | }
|
|---|
| 307 |
|
|---|
| 308 | return 0;
|
|---|
| 309 | }
|
|---|