xref: /minix3/external/bsd/kyua-cli/dist/cli/cmd_db_exec.cpp (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc // Copyright 2011 Google Inc.
2*11be35a1SLionel Sambuc // All rights reserved.
3*11be35a1SLionel Sambuc //
4*11be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
5*11be35a1SLionel Sambuc // modification, are permitted provided that the following conditions are
6*11be35a1SLionel Sambuc // met:
7*11be35a1SLionel Sambuc //
8*11be35a1SLionel Sambuc // * Redistributions of source code must retain the above copyright
9*11be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer.
10*11be35a1SLionel Sambuc // * Redistributions in binary form must reproduce the above copyright
11*11be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer in the
12*11be35a1SLionel Sambuc //   documentation and/or other materials provided with the distribution.
13*11be35a1SLionel Sambuc // * Neither the name of Google Inc. nor the names of its contributors
14*11be35a1SLionel Sambuc //   may be used to endorse or promote products derived from this software
15*11be35a1SLionel Sambuc //   without specific prior written permission.
16*11be35a1SLionel Sambuc //
17*11be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*11be35a1SLionel Sambuc // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*11be35a1SLionel Sambuc // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*11be35a1SLionel Sambuc // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*11be35a1SLionel Sambuc // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*11be35a1SLionel Sambuc // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*11be35a1SLionel Sambuc // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*11be35a1SLionel Sambuc // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*11be35a1SLionel Sambuc // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*11be35a1SLionel Sambuc // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*11be35a1SLionel Sambuc // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc 
29*11be35a1SLionel Sambuc #include "cli/cmd_db_exec.hpp"
30*11be35a1SLionel Sambuc 
31*11be35a1SLionel Sambuc #include <algorithm>
32*11be35a1SLionel Sambuc #include <cstdlib>
33*11be35a1SLionel Sambuc #include <iterator>
34*11be35a1SLionel Sambuc #include <sstream>
35*11be35a1SLionel Sambuc #include <string>
36*11be35a1SLionel Sambuc 
37*11be35a1SLionel Sambuc #include "cli/common.ipp"
38*11be35a1SLionel Sambuc #include "store/backend.hpp"
39*11be35a1SLionel Sambuc #include "utils/defs.hpp"
40*11be35a1SLionel Sambuc #include "utils/format/macros.hpp"
41*11be35a1SLionel Sambuc #include "utils/sanity.hpp"
42*11be35a1SLionel Sambuc #include "utils/sqlite/database.hpp"
43*11be35a1SLionel Sambuc #include "utils/sqlite/exceptions.hpp"
44*11be35a1SLionel Sambuc #include "utils/sqlite/statement.hpp"
45*11be35a1SLionel Sambuc 
46*11be35a1SLionel Sambuc namespace cmdline = utils::cmdline;
47*11be35a1SLionel Sambuc namespace config = utils::config;
48*11be35a1SLionel Sambuc namespace sqlite = utils::sqlite;
49*11be35a1SLionel Sambuc 
50*11be35a1SLionel Sambuc using cli::cmd_db_exec;
51*11be35a1SLionel Sambuc 
52*11be35a1SLionel Sambuc 
53*11be35a1SLionel Sambuc namespace {
54*11be35a1SLionel Sambuc 
55*11be35a1SLionel Sambuc 
56*11be35a1SLionel Sambuc /// Concatenates a vector into a string using ' ' as a separator.
57*11be35a1SLionel Sambuc ///
58*11be35a1SLionel Sambuc /// \param args The objects to join.  This cannot be empty.
59*11be35a1SLionel Sambuc ///
60*11be35a1SLionel Sambuc /// \return The concatenation of all the objects in the set.
61*11be35a1SLionel Sambuc static std::string
flatten_args(const cmdline::args_vector & args)62*11be35a1SLionel Sambuc flatten_args(const cmdline::args_vector& args)
63*11be35a1SLionel Sambuc {
64*11be35a1SLionel Sambuc     std::ostringstream output;
65*11be35a1SLionel Sambuc     std::copy(args.begin(), args.end(),
66*11be35a1SLionel Sambuc               std::ostream_iterator< std::string >(output, " "));
67*11be35a1SLionel Sambuc 
68*11be35a1SLionel Sambuc     std::string result = output.str();
69*11be35a1SLionel Sambuc     result.erase(result.end() - 1);
70*11be35a1SLionel Sambuc     return result;
71*11be35a1SLionel Sambuc }
72*11be35a1SLionel Sambuc 
73*11be35a1SLionel Sambuc 
74*11be35a1SLionel Sambuc }  // anonymous namespace
75*11be35a1SLionel Sambuc 
76*11be35a1SLionel Sambuc 
77*11be35a1SLionel Sambuc /// Formats a particular cell of a statement result.
78*11be35a1SLionel Sambuc ///
79*11be35a1SLionel Sambuc /// \param stmt The statement whose cell to format.
80*11be35a1SLionel Sambuc /// \param index The index of the cell to format.
81*11be35a1SLionel Sambuc ///
82*11be35a1SLionel Sambuc /// \return A textual representation of the cell.
83*11be35a1SLionel Sambuc std::string
format_cell(sqlite::statement & stmt,const int index)84*11be35a1SLionel Sambuc cli::format_cell(sqlite::statement& stmt, const int index)
85*11be35a1SLionel Sambuc {
86*11be35a1SLionel Sambuc     switch (stmt.column_type(index)) {
87*11be35a1SLionel Sambuc     case sqlite::type_blob: {
88*11be35a1SLionel Sambuc         const sqlite::blob blob = stmt.column_blob(index);
89*11be35a1SLionel Sambuc         return F("BLOB of %s bytes") % blob.size;
90*11be35a1SLionel Sambuc     }
91*11be35a1SLionel Sambuc 
92*11be35a1SLionel Sambuc     case sqlite::type_float:
93*11be35a1SLionel Sambuc         return F("%s") % stmt.column_double(index);
94*11be35a1SLionel Sambuc 
95*11be35a1SLionel Sambuc     case sqlite::type_integer:
96*11be35a1SLionel Sambuc         return F("%s") % stmt.column_int64(index);
97*11be35a1SLionel Sambuc 
98*11be35a1SLionel Sambuc     case sqlite::type_null:
99*11be35a1SLionel Sambuc         return "NULL";
100*11be35a1SLionel Sambuc 
101*11be35a1SLionel Sambuc     case sqlite::type_text:
102*11be35a1SLionel Sambuc         return stmt.column_text(index);
103*11be35a1SLionel Sambuc     }
104*11be35a1SLionel Sambuc 
105*11be35a1SLionel Sambuc     UNREACHABLE;
106*11be35a1SLionel Sambuc }
107*11be35a1SLionel Sambuc 
108*11be35a1SLionel Sambuc 
109*11be35a1SLionel Sambuc /// Formats the column names of a statement for output as CSV.
110*11be35a1SLionel Sambuc ///
111*11be35a1SLionel Sambuc /// \param stmt The statement whose columns to format.
112*11be35a1SLionel Sambuc ///
113*11be35a1SLionel Sambuc /// \return A comma-separated list of column names.
114*11be35a1SLionel Sambuc std::string
format_headers(sqlite::statement & stmt)115*11be35a1SLionel Sambuc cli::format_headers(sqlite::statement& stmt)
116*11be35a1SLionel Sambuc {
117*11be35a1SLionel Sambuc     std::string output;
118*11be35a1SLionel Sambuc     int i = 0;
119*11be35a1SLionel Sambuc     for (; i < stmt.column_count() - 1; ++i)
120*11be35a1SLionel Sambuc         output += stmt.column_name(i) + ',';
121*11be35a1SLionel Sambuc     output += stmt.column_name(i);
122*11be35a1SLionel Sambuc     return output;
123*11be35a1SLionel Sambuc }
124*11be35a1SLionel Sambuc 
125*11be35a1SLionel Sambuc 
126*11be35a1SLionel Sambuc /// Formats a row of a statement for output as CSV.
127*11be35a1SLionel Sambuc ///
128*11be35a1SLionel Sambuc /// \param stmt The statement whose current row to format.
129*11be35a1SLionel Sambuc ///
130*11be35a1SLionel Sambuc /// \return A comma-separated list of values.
131*11be35a1SLionel Sambuc std::string
format_row(sqlite::statement & stmt)132*11be35a1SLionel Sambuc cli::format_row(sqlite::statement& stmt)
133*11be35a1SLionel Sambuc {
134*11be35a1SLionel Sambuc     std::string output;
135*11be35a1SLionel Sambuc     int i = 0;
136*11be35a1SLionel Sambuc     for (; i < stmt.column_count() - 1; ++i)
137*11be35a1SLionel Sambuc         output += cli::format_cell(stmt, i) + ',';
138*11be35a1SLionel Sambuc     output += cli::format_cell(stmt, i);
139*11be35a1SLionel Sambuc     return output;
140*11be35a1SLionel Sambuc }
141*11be35a1SLionel Sambuc 
142*11be35a1SLionel Sambuc 
143*11be35a1SLionel Sambuc /// Default constructor for cmd_db_exec.
cmd_db_exec(void)144*11be35a1SLionel Sambuc cmd_db_exec::cmd_db_exec(void) : cli_command(
145*11be35a1SLionel Sambuc     "db-exec", "sql_statement", 1, -1,
146*11be35a1SLionel Sambuc     "Executes an arbitrary SQL statement in the store database and prints "
147*11be35a1SLionel Sambuc     "the resulting table")
148*11be35a1SLionel Sambuc {
149*11be35a1SLionel Sambuc     add_option(store_option);
150*11be35a1SLionel Sambuc     add_option(cmdline::bool_option("no-headers", "Do not show headers in the "
151*11be35a1SLionel Sambuc                                     "output table"));
152*11be35a1SLionel Sambuc }
153*11be35a1SLionel Sambuc 
154*11be35a1SLionel Sambuc 
155*11be35a1SLionel Sambuc /// Entry point for the "db-exec" subcommand.
156*11be35a1SLionel Sambuc ///
157*11be35a1SLionel Sambuc /// \param ui Object to interact with the I/O of the program.
158*11be35a1SLionel Sambuc /// \param cmdline Representation of the command line to the subcommand.
159*11be35a1SLionel Sambuc /// \param unused_user_config The runtime configuration of the program.
160*11be35a1SLionel Sambuc ///
161*11be35a1SLionel Sambuc /// \return 0 if everything is OK, 1 if the statement is invalid or if there is
162*11be35a1SLionel Sambuc /// any other problem.
163*11be35a1SLionel Sambuc int
run(cmdline::ui * ui,const cmdline::parsed_cmdline & cmdline,const config::tree & UTILS_UNUSED_PARAM (user_config))164*11be35a1SLionel Sambuc cmd_db_exec::run(cmdline::ui* ui, const cmdline::parsed_cmdline& cmdline,
165*11be35a1SLionel Sambuc                  const config::tree& UTILS_UNUSED_PARAM(user_config))
166*11be35a1SLionel Sambuc {
167*11be35a1SLionel Sambuc     try {
168*11be35a1SLionel Sambuc         store::backend backend = store::backend::open_rw(
169*11be35a1SLionel Sambuc             cli::store_path(cmdline));
170*11be35a1SLionel Sambuc         sqlite::statement stmt = backend.database().create_statement(
171*11be35a1SLionel Sambuc             flatten_args(cmdline.arguments()));
172*11be35a1SLionel Sambuc 
173*11be35a1SLionel Sambuc         if (stmt.step()) {
174*11be35a1SLionel Sambuc             if (!cmdline.has_option("no-headers"))
175*11be35a1SLionel Sambuc                 ui->out(cli::format_headers(stmt));
176*11be35a1SLionel Sambuc             do
177*11be35a1SLionel Sambuc                 ui->out(cli::format_row(stmt));
178*11be35a1SLionel Sambuc             while (stmt.step());
179*11be35a1SLionel Sambuc         }
180*11be35a1SLionel Sambuc 
181*11be35a1SLionel Sambuc         return EXIT_SUCCESS;
182*11be35a1SLionel Sambuc     } catch (const sqlite::error& e) {
183*11be35a1SLionel Sambuc         cmdline::print_error(ui, F("SQLite error: %s") % e.what());
184*11be35a1SLionel Sambuc         return EXIT_FAILURE;
185*11be35a1SLionel Sambuc     }
186*11be35a1SLionel Sambuc }
187