1 // Copyright 2011 The Kyua Authors.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "cli/cmd_db_exec.hpp"
37 #include "cli/common.ipp"
38 #include "store/exceptions.hpp"
39 #include "store/layout.hpp"
40 #include "store/read_backend.hpp"
41 #include "utils/cmdline/options.hpp"
42 #include "utils/cmdline/parser.ipp"
43 #include "utils/cmdline/ui.hpp"
44 #include "utils/defs.hpp"
45 #include "utils/format/macros.hpp"
46 #include "utils/fs/path.hpp"
47 #include "utils/sanity.hpp"
48 #include "utils/sqlite/database.hpp"
49 #include "utils/sqlite/exceptions.hpp"
50 #include "utils/sqlite/statement.hpp"
52 namespace cmdline = utils::cmdline;
53 namespace config = utils::config;
54 namespace fs = utils::fs;
55 namespace layout = store::layout;
56 namespace sqlite = utils::sqlite;
58 using cli::cmd_db_exec;
64 /// Concatenates a vector into a string using ' ' as a separator.
66 /// \param args The objects to join. This cannot be empty.
68 /// \return The concatenation of all the objects in the set.
70 flatten_args(const cmdline::args_vector& args)
72 std::ostringstream output;
73 std::copy(args.begin(), args.end(),
74 std::ostream_iterator< std::string >(output, " "));
76 std::string result = output.str();
77 result.erase(result.end() - 1);
82 } // anonymous namespace
85 /// Formats a particular cell of a statement result.
87 /// \param stmt The statement whose cell to format.
88 /// \param index The index of the cell to format.
90 /// \return A textual representation of the cell.
92 cli::format_cell(sqlite::statement& stmt, const int index)
94 switch (stmt.column_type(index)) {
95 case sqlite::type_blob: {
96 const sqlite::blob blob = stmt.column_blob(index);
97 return F("BLOB of %s bytes") % blob.size;
100 case sqlite::type_float:
101 return F("%s") % stmt.column_double(index);
103 case sqlite::type_integer:
104 return F("%s") % stmt.column_int64(index);
106 case sqlite::type_null:
109 case sqlite::type_text:
110 return stmt.column_text(index);
117 /// Formats the column names of a statement for output as CSV.
119 /// \param stmt The statement whose columns to format.
121 /// \return A comma-separated list of column names.
123 cli::format_headers(sqlite::statement& stmt)
127 for (; i < stmt.column_count() - 1; ++i)
128 output += stmt.column_name(i) + ',';
129 output += stmt.column_name(i);
134 /// Formats a row of a statement for output as CSV.
136 /// \param stmt The statement whose current row to format.
138 /// \return A comma-separated list of values.
140 cli::format_row(sqlite::statement& stmt)
144 for (; i < stmt.column_count() - 1; ++i)
145 output += cli::format_cell(stmt, i) + ',';
146 output += cli::format_cell(stmt, i);
151 /// Default constructor for cmd_db_exec.
152 cmd_db_exec::cmd_db_exec(void) : cli_command(
153 "db-exec", "sql_statement", 1, -1,
154 "Executes an arbitrary SQL statement in a results file and prints "
155 "the resulting table")
157 add_option(results_file_open_option);
158 add_option(cmdline::bool_option("no-headers", "Do not show headers in the "
163 /// Entry point for the "db-exec" subcommand.
165 /// \param ui Object to interact with the I/O of the program.
166 /// \param cmdline Representation of the command line to the subcommand.
168 /// \return 0 if everything is OK, 1 if the statement is invalid or if there is
169 /// any other problem.
171 cmd_db_exec::run(cmdline::ui* ui, const cmdline::parsed_cmdline& cmdline,
172 const config::tree& /* user_config */)
175 const fs::path results_file = layout::find_results(
176 results_file_open(cmdline));
178 // TODO(jmmv): Shouldn't be using store::detail here...
179 sqlite::database db = store::detail::open_and_setup(
180 results_file, sqlite::open_readwrite);
181 sqlite::statement stmt = db.create_statement(
182 flatten_args(cmdline.arguments()));
185 if (!cmdline.has_option("no-headers"))
186 ui->out(cli::format_headers(stmt));
188 ui->out(cli::format_row(stmt));
193 } catch (const sqlite::error& e) {
194 cmdline::print_error(ui, F("SQLite error: %s.") % e.what());
196 } catch (const store::error& e) {
197 cmdline::print_error(ui, F("%s.") % e.what());