lnav/src/sql_commands.cc

274 lines
7.4 KiB
C++
Raw Permalink Normal View History

2021-03-28 22:50:26 +02:00
/**
* Copyright (c) 2021, Timothy Stack
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of Timothy Stack nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2022-03-16 23:38:08 +01:00
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2021-03-28 22:50:26 +02:00
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
2022-04-13 01:07:13 +02:00
#include "base/auto_mem.hh"
2022-03-13 23:49:41 +01:00
#include "base/fs_util.hh"
2021-03-28 22:50:26 +02:00
#include "base/injector.bind.hh"
2022-03-16 23:38:08 +01:00
#include "base/lnav_log.hh"
#include "bound_tags.hh"
#include "command_executor.hh"
#include "config.h"
2022-03-13 23:49:41 +01:00
#include "readline_context.hh"
2022-03-16 23:38:08 +01:00
#include "shlex.hh"
2021-03-28 22:50:26 +02:00
#include "sqlite-extension-func.hh"
2022-03-13 23:49:41 +01:00
#include "sqlitepp.hh"
2022-03-16 23:38:08 +01:00
#include "view_helpers.hh"
2021-03-28 22:50:26 +02:00
2022-04-13 01:07:13 +02:00
static Result<std::string, lnav::console::user_message>
2022-03-16 23:38:08 +01:00
sql_cmd_dump(exec_context& ec,
std::string cmdline,
std::vector<std::string>& args)
2021-03-28 22:50:26 +02:00
{
static auto& lnav_db = injector::get<auto_sqlite3&>();
2022-07-20 08:48:25 +02:00
static auto& lnav_flags = injector::get<unsigned long&, lnav_flags_tag>();
2022-03-13 23:49:41 +01:00
2021-03-28 22:50:26 +02:00
std::string retval;
if (args.empty()) {
args.emplace_back("filename");
args.emplace_back("tables");
return Ok(retval);
}
if (args.size() < 2) {
return ec.make_error("expecting a file name to write to");
}
2022-07-20 08:48:25 +02:00
if (lnav_flags & LNF_SECURE_MODE) {
return ec.make_error("{} -- unavailable in secure mode", args[0]);
}
2021-03-28 22:50:26 +02:00
auto_mem<FILE> file(fclose);
if ((file = fopen(args[1].c_str(), "w+")) == nullptr) {
2022-03-16 23:38:08 +01:00
return ec.make_error(
"unable to open '{}' for writing: {}", args[1], strerror(errno));
2021-03-28 22:50:26 +02:00
}
for (size_t lpc = 2; lpc < args.size(); lpc++) {
2022-03-13 23:49:41 +01:00
sqlite3_db_dump(lnav_db.in(),
2021-03-28 22:50:26 +02:00
"main",
args[lpc].c_str(),
2022-03-16 23:38:08 +01:00
(int (*)(const char*, void*)) fputs,
2021-03-28 22:50:26 +02:00
file.in());
}
retval = "generated";
return Ok(retval);
}
2022-04-13 01:07:13 +02:00
static Result<std::string, lnav::console::user_message>
2022-03-16 23:38:08 +01:00
sql_cmd_read(exec_context& ec,
std::string cmdline,
std::vector<std::string>& args)
2021-03-28 22:50:26 +02:00
{
static auto& lnav_db = injector::get<auto_sqlite3&>();
2022-07-20 08:48:25 +02:00
static auto& lnav_flags = injector::get<unsigned long&, lnav_flags_tag>();
2022-03-13 23:49:41 +01:00
2021-03-28 22:50:26 +02:00
std::string retval;
if (args.empty()) {
args.emplace_back("filename");
return Ok(retval);
}
2022-07-20 08:48:25 +02:00
if (lnav_flags & LNF_SECURE_MODE) {
return ec.make_error("{} -- unavailable in secure mode", args[0]);
}
2021-03-28 22:50:26 +02:00
std::vector<std::string> split_args;
shlex lexer(cmdline);
if (!lexer.split(split_args, ec.create_resolver())) {
return ec.make_error("unable to parse arguments");
}
for (size_t lpc = 1; lpc < split_args.size(); lpc++) {
2022-03-13 23:49:41 +01:00
auto read_res = lnav::filesystem::read_file(split_args[lpc]);
2021-03-28 22:50:26 +02:00
if (read_res.isErr()) {
return ec.make_error("unable to read script file: {} -- {}",
split_args[lpc],
read_res.unwrapErr());
}
auto script = read_res.unwrap();
auto_mem<sqlite3_stmt> stmt(sqlite3_finalize);
2022-03-16 23:38:08 +01:00
const char* start = script.c_str();
2021-03-28 22:50:26 +02:00
do {
2022-03-16 23:38:08 +01:00
const char* tail;
auto rc = sqlite3_prepare_v2(
lnav_db.in(), start, -1, stmt.out(), &tail);
2021-03-28 22:50:26 +02:00
if (rc != SQLITE_OK) {
2022-03-16 23:38:08 +01:00
const char* errmsg = sqlite3_errmsg(lnav_db.in());
2021-03-28 22:50:26 +02:00
return ec.make_error("{}", errmsg);
}
if (stmt.in() != nullptr) {
std::string alt_msg;
2022-03-16 23:38:08 +01:00
auto exec_res = execute_sql(
ec, std::string(start, tail - start), alt_msg);
2021-03-28 22:50:26 +02:00
if (exec_res.isErr()) {
return exec_res;
}
}
start = tail;
} while (start[0]);
}
return Ok(retval);
}
2022-04-13 01:07:13 +02:00
static Result<std::string, lnav::console::user_message>
2022-03-16 23:38:08 +01:00
sql_cmd_schema(exec_context& ec,
std::string cmdline,
std::vector<std::string>& args)
2021-03-28 22:50:26 +02:00
{
std::string retval;
if (args.empty()) {
return Ok(retval);
}
2022-03-13 23:49:41 +01:00
ensure_view(LNV_SCHEMA);
2021-03-28 22:50:26 +02:00
return Ok(retval);
}
2022-04-13 01:07:13 +02:00
static Result<std::string, lnav::console::user_message>
2022-03-16 23:38:08 +01:00
sql_cmd_generic(exec_context& ec,
std::string cmdline,
std::vector<std::string>& args)
2021-03-28 22:50:26 +02:00
{
std::string retval;
if (args.empty()) {
args.emplace_back("*");
return Ok(retval);
}
return Ok(retval);
}
static readline_context::command_t sql_commands[] = {
{
".dump",
sql_cmd_dump,
2022-03-16 23:38:08 +01:00
help_text(".dump", "Dump the contents of the database")
2021-03-28 22:50:26 +02:00
.sql_command()
.with_parameter({"path", "The path to the file to write"})
2022-03-16 23:38:08 +01:00
.with_tags({
"io",
}),
2021-03-28 22:50:26 +02:00
},
{
".msgformats",
sql_cmd_schema,
2022-03-16 23:38:08 +01:00
help_text(".msgformats", "df").sql_command(),
2021-03-28 22:50:26 +02:00
},
{
".read",
sql_cmd_read,
2022-03-16 23:38:08 +01:00
help_text(".read", "Execute the SQLite statements in the given file")
2022-03-13 23:49:41 +01:00
.sql_command()
.with_parameter({"path", "The path to the file to write"})
2022-03-16 23:38:08 +01:00
.with_tags({
"io",
}),
2021-03-28 22:50:26 +02:00
},
{
".schema",
sql_cmd_schema,
help_text(".schema",
"Switch to the SCHEMA view that contains a dump of the "
"current database schema")
.sql_command(),
},
{
"ATTACH",
sql_cmd_generic,
},
{
"CREATE",
sql_cmd_generic,
},
{
"DELETE",
sql_cmd_generic,
},
{
"DETACH",
sql_cmd_generic,
},
{
"DROP",
sql_cmd_generic,
},
{
"INSERT",
sql_cmd_generic,
},
{
"SELECT",
sql_cmd_generic,
},
{
"UPDATE",
sql_cmd_generic,
},
{
"WITH",
sql_cmd_generic,
},
};
static readline_context::command_map_t sql_cmd_map;
2022-03-16 23:38:08 +01:00
static auto bound_sql_cmd_map
= injector::bind<readline_context::command_map_t,
sql_cmd_map_tag>::to_instance(+[]() {
for (auto& cmd : sql_commands) {
sql_cmd_map[cmd.c_name] = &cmd;
}
2021-03-28 22:50:26 +02:00
2022-03-16 23:38:08 +01:00
return &sql_cmd_map;
});
2021-03-28 22:50:26 +02:00
2021-03-31 07:50:10 +02:00
namespace injector {
2021-03-28 22:50:26 +02:00
template<>
2022-03-16 23:38:08 +01:00
void
force_linking(sql_cmd_map_tag anno)
2021-03-28 22:50:26 +02:00
{
}
2022-03-16 23:38:08 +01:00
} // namespace injector