IODA Bundle
SQLTool.cc
Go to the documentation of this file.
1 /*
2  * (C) Copyright 1996-2012 ECMWF.
3  *
4  * This software is licensed under the terms of the Apache Licence Version 2.0
5  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6  * In applying this licence, ECMWF does not waive the privileges and immunities
7  * granted to it by virtue of its status as an intergovernmental organisation nor
8  * does it submit to any jurisdiction.
9  */
10 
11 #include <fstream>
12 #include <ostream>
13 #include <memory>
14 
15 #include "eckit/io/FileHandle.h"
16 #include "eckit/io/Length.h"
17 #include "eckit/io/PartFileHandle.h"
18 #include "eckit/io/FileDescHandle.h"
19 #include "eckit/utils/StringTools.h"
20 
21 #include "eckit/sql/SQLParser.h"
22 #include "eckit/sql/SQLSelectFactory.h"
23 #include "eckit/sql/SQLSession.h"
24 #include "eckit/sql/SQLStatement.h"
25 #include "eckit/types/Types.h"
26 
28 #include "odc/sql/TODATable.h"
29 #include "odc/tools/SQLTool.h"
30 
31 using namespace std;
32 using namespace eckit;
33 
34 namespace odc {
35 namespace tool {
36 
37 //----------------------------------------------------------------------------------------------------------------------
38 
39 SQLTool::SQLTool(int argc,char **argv) :
40  Tool(argc, argv) {
41 
45  registerOptionWithArgument("-delimiter");
46  registerOptionWithArgument("-f"); // output format
47  registerOptionWithArgument("-offset");
48  registerOptionWithArgument("-length");
49 
50  if ((inputFile_ = optionArgument("-i", std::string(""))) == "-")
51  inputFile_ = "/dev/stdin";
52 
53  offset_ = optionArgument("-offset", (long) 0); // FIXME@ optionArgument should accept unsigned long etc
54  length_ = optionArgument("-length", (long) 0);
55 
56  // Configure the output
57 
58  bool noColumnNames = optionIsSet("-T");
59  bool noNULL = optionIsSet("-N");
60  std::string fieldDelimiter = optionArgument("-delimiter", std::string("\t"));
61  std::string outputFormat = optionArgument("-f", std::string(eckit::sql::SQLOutputConfig::defaultOutputFormat));
62  bool bitfieldsBinary = optionIsSet("--bin") || optionIsSet("--binary");
63 // bool bitfieldsHex = optionIsSet("--hex") || optionIsSet("--hexadecimal");
64  bool noColumnAlignment = optionIsSet("--no_alignment");
65  bool fullPrecision = optionIsSet("--full_precision") || optionIsSet("--full-precision");
66 
67 
68  sqlOutputConfig_.reset(new odc::sql::SQLOutputConfig(noColumnNames, noNULL, fieldDelimiter, outputFormat,
69  bitfieldsBinary, noColumnAlignment, fullPrecision));
70 
71  // Configure the output file
72 
73  std::string outputFile = optionArgument("-o", std::string(""));
74  if (outputFile == "-")
75  outputFile = "/dev/stdout";
76 
77  if (!outputFile.empty()) {
78  sqlOutputConfig_->setOutputFile(outputFile);
79  }
80 }
81 
83 
85 {
86  if (parameters().size() < 2) {
87  Log::error() << "Usage: ";
89  Log::error() << std::endl;
90  return;// 1;
91  }
92 
93  std::vector<std::string> params(parameters());
94  params.erase(params.begin());
95 
96  std::string sql(StringTool::isSelectStatement(params[0])
97  ? StringTools::join(" ", params) + ";"
98  // FIXME:
99  : StringTool::readFile(params[0] == "-" ? "/dev/tty" : params[0]) + ";");
100 
101 
102  std::unique_ptr<std::ofstream> outStream;
103  if (optionIsSet("-o") && sqlOutputConfig_->outputFormat() != "odb") {
104  outStream.reset(new std::ofstream(optionArgument("-o", std::string("")).c_str()));
105  sqlOutputConfig_->setOutputStream(*outStream);
106  }
107 
108  // Configure the session to include any specified ODB file
109 
110  eckit::sql::SQLSession session(std::move(sqlOutputConfig_)); // n.b. invalidates sqlOutputConfig_
111  std::unique_ptr<eckit::DataHandle> implicitTableDH;
112 
113  if (!inputFile_.empty()) {
114  if (inputFile_ == "/dev/stdin" || inputFile_ == "stdin") {
115  Log::info() << "Reading table from standard input" << std::endl;
116  implicitTableDH.reset(new FileDescHandle(0));
117  NOTIMP; // Is this working?
118  /// parser.parseString(session, sql, &fh, config);
119  } else if (offset_ == eckit::Offset(0)) {
120  implicitTableDH.reset(new FileHandle(inputFile_));
121  } else {
122  implicitTableDH.reset(new PartFileHandle(inputFile_, offset_, length_));
123  }
124 
125  implicitTableDH->openForRead();
126 
127  eckit::sql::SQLDatabase& db(session.currentDatabase());
128  db.addImplicitTable(new odc::sql::ODATable(db, *implicitTableDH));
129  }
130 
131  // And actually do the SQL!
132 
133  eckit::sql::SQLParser parser;
134  parser.parseString(session, sql);
135  session.statement().execute();
136 }
137 
138 //----------------------------------------------------------------------------------------------------------------------
139 
140 } // namespace tool
141 } // namespace odc
142 
static std::string readFile(const eckit::PathName fileName, bool logging=false)
Definition: StringTool.cc:40
static bool isSelectStatement(const std::string &)
Definition: StringTool.cc:196
bool optionIsSet(const std::string &)
T optionArgument(const std::string &, T defaultValue)
void registerOptionWithArgument(const std::string &)
const std::vector< std::string > parameters()
std::unique_ptr< odc::sql::SQLOutputConfig > sqlOutputConfig_
Definition: SQLTool.h:71
static void usage(const std::string &name, std::ostream &o)
Definition: SQLTool.h:50
eckit::Length length_
Definition: SQLTool.h:75
std::string inputFile_
Definition: SQLTool.h:73
virtual ~SQLTool()
Definition: SQLTool.cc:82
eckit::Offset offset_
Definition: SQLTool.h:74
virtual void run()
Definition: SQLTool.cc:84
Definition: ColumnInfo.h:23
Definition: encode.cc:30