2021-04-18 21:40:00 +00:00
|
|
|
/*
|
2024-04-17 15:17:46 +00:00
|
|
|
* Copyright (c) 2021-2024, Tim Flynn <trflynn89@serenityos.org>
|
2022-01-25 01:29:26 +00:00
|
|
|
* Copyright (c) 2022, Alex Major
|
2021-04-18 21:40:00 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-04-18 21:40:00 +00:00
|
|
|
*/
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
#include <AK/ByteString.h>
|
2021-04-18 21:40:00 +00:00
|
|
|
#include <AK/Format.h>
|
2023-02-02 10:03:46 +00:00
|
|
|
#include <AK/String.h>
|
2021-04-18 21:40:00 +00:00
|
|
|
#include <AK/StringBuilder.h>
|
2021-08-26 22:45:37 +00:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2021-04-18 21:40:00 +00:00
|
|
|
#include <LibCore/StandardPaths.h>
|
2023-03-21 15:35:30 +00:00
|
|
|
#include <LibFileSystem/FileSystem.h>
|
2021-04-18 21:40:00 +00:00
|
|
|
#include <LibLine/Editor.h>
|
2022-01-25 01:29:26 +00:00
|
|
|
#include <LibMain/Main.h>
|
2021-06-21 14:57:44 +00:00
|
|
|
#include <LibSQL/AST/Lexer.h>
|
|
|
|
#include <LibSQL/AST/Token.h>
|
2021-06-29 01:20:15 +00:00
|
|
|
#include <LibSQL/SQLClient.h>
|
|
|
|
#include <unistd.h>
|
2021-04-18 21:40:00 +00:00
|
|
|
|
2024-04-17 15:17:46 +00:00
|
|
|
#if !defined(AK_OS_SERENITY)
|
2024-04-23 22:59:00 +00:00
|
|
|
# include <LibCore/Process.h>
|
2024-04-17 15:17:46 +00:00
|
|
|
#endif
|
|
|
|
|
2021-09-07 12:46:20 +00:00
|
|
|
class SQLRepl {
|
|
|
|
public:
|
2023-12-16 14:19:34 +00:00
|
|
|
explicit SQLRepl(Core::EventLoop& loop, ByteString const& database_name, NonnullRefPtr<SQL::SQLClient> sql_client)
|
2024-05-21 10:49:35 +00:00
|
|
|
: m_history_path(ByteString::formatted("{}/.sql-history", Core::StandardPaths::home_directory()))
|
|
|
|
, m_sql_client(move(sql_client))
|
2022-12-09 22:09:08 +00:00
|
|
|
, m_loop(loop)
|
2021-09-07 12:46:20 +00:00
|
|
|
{
|
|
|
|
m_editor = Line::Editor::construct();
|
|
|
|
m_editor->load_history(m_history_path);
|
|
|
|
|
|
|
|
m_editor->on_display_refresh = [this](Line::Editor& editor) {
|
|
|
|
editor.strip_styles();
|
|
|
|
|
|
|
|
int open_indents = m_repl_line_level;
|
|
|
|
|
|
|
|
auto line = editor.line();
|
|
|
|
SQL::AST::Lexer lexer(line);
|
|
|
|
|
|
|
|
bool indenters_starting_line = true;
|
|
|
|
for (SQL::AST::Token token = lexer.next(); token.type() != SQL::AST::TokenType::Eof; token = lexer.next()) {
|
|
|
|
auto start = token.start_position().column - 1;
|
2021-09-16 20:25:29 +00:00
|
|
|
auto end = token.end_position().column - 1;
|
2021-09-07 12:46:20 +00:00
|
|
|
|
|
|
|
if (indenters_starting_line) {
|
|
|
|
if (token.type() != SQL::AST::TokenType::ParenClose)
|
|
|
|
indenters_starting_line = false;
|
|
|
|
else
|
|
|
|
--open_indents;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (token.category()) {
|
|
|
|
case SQL::AST::TokenCategory::Invalid:
|
|
|
|
editor.stylize({ start, end }, { Line::Style::Foreground(Line::Style::XtermColor::Red), Line::Style::Underline });
|
|
|
|
break;
|
|
|
|
case SQL::AST::TokenCategory::Number:
|
|
|
|
editor.stylize({ start, end }, { Line::Style::Foreground(Line::Style::XtermColor::Magenta) });
|
|
|
|
break;
|
|
|
|
case SQL::AST::TokenCategory::String:
|
|
|
|
editor.stylize({ start, end }, { Line::Style::Foreground(Line::Style::XtermColor::Green), Line::Style::Bold });
|
|
|
|
break;
|
|
|
|
case SQL::AST::TokenCategory::Blob:
|
|
|
|
editor.stylize({ start, end }, { Line::Style::Foreground(Line::Style::XtermColor::Magenta), Line::Style::Bold });
|
|
|
|
break;
|
|
|
|
case SQL::AST::TokenCategory::Keyword:
|
|
|
|
editor.stylize({ start, end }, { Line::Style::Foreground(Line::Style::XtermColor::Blue), Line::Style::Bold });
|
|
|
|
break;
|
|
|
|
case SQL::AST::TokenCategory::Identifier:
|
|
|
|
editor.stylize({ start, end }, { Line::Style::Foreground(Line::Style::XtermColor::White), Line::Style::Bold });
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-04-18 21:40:00 +00:00
|
|
|
|
2021-09-07 12:46:20 +00:00
|
|
|
m_editor->set_prompt(prompt_for_level(open_indents));
|
|
|
|
};
|
2021-04-18 21:40:00 +00:00
|
|
|
|
2023-02-03 15:33:10 +00:00
|
|
|
m_sql_client->on_execution_success = [this](auto result) {
|
|
|
|
if (result.rows_updated != 0 || result.rows_created != 0 || result.rows_deleted != 0)
|
|
|
|
outln("{} row(s) created, {} updated, {} deleted", result.rows_created, result.rows_updated, result.rows_deleted);
|
|
|
|
if (!result.has_results)
|
2021-09-07 12:46:20 +00:00
|
|
|
read_sql();
|
|
|
|
};
|
2021-04-18 21:40:00 +00:00
|
|
|
|
2023-02-03 15:33:10 +00:00
|
|
|
m_sql_client->on_next_result = [](auto result) {
|
2021-09-07 12:46:20 +00:00
|
|
|
StringBuilder builder;
|
2023-02-03 15:33:10 +00:00
|
|
|
builder.join(", "sv, result.values);
|
2023-12-16 14:19:34 +00:00
|
|
|
outln("{}", builder.to_byte_string());
|
2021-09-07 12:46:20 +00:00
|
|
|
};
|
2021-04-18 21:40:00 +00:00
|
|
|
|
2023-02-03 15:33:10 +00:00
|
|
|
m_sql_client->on_results_exhausted = [this](auto result) {
|
|
|
|
outln("{} row(s)", result.total_rows);
|
2021-09-07 12:46:20 +00:00
|
|
|
read_sql();
|
|
|
|
};
|
2021-04-18 21:40:00 +00:00
|
|
|
|
2023-02-03 15:33:10 +00:00
|
|
|
m_sql_client->on_execution_error = [this](auto result) {
|
|
|
|
outln("\033[33;1mExecution error:\033[0m {}", result.error_message);
|
2021-09-07 12:46:20 +00:00
|
|
|
read_sql();
|
|
|
|
};
|
2021-04-18 21:40:00 +00:00
|
|
|
|
2021-09-07 12:46:20 +00:00
|
|
|
if (!database_name.is_empty())
|
|
|
|
connect(database_name);
|
2021-09-02 10:08:11 +00:00
|
|
|
}
|
|
|
|
|
2021-09-07 12:46:20 +00:00
|
|
|
~SQLRepl()
|
|
|
|
{
|
|
|
|
m_editor->save_history(m_history_path);
|
2021-09-02 10:08:11 +00:00
|
|
|
}
|
2021-08-26 22:45:37 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
void connect(ByteString const& database_name)
|
2021-09-07 12:46:20 +00:00
|
|
|
{
|
2022-12-03 12:07:42 +00:00
|
|
|
if (!m_database_name.is_empty()) {
|
|
|
|
m_sql_client->disconnect(m_connection_id);
|
|
|
|
m_database_name = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto connection_id = m_sql_client->connect(database_name); connection_id.has_value()) {
|
|
|
|
outln("Connected to \033[33;1m{}\033[0m", database_name);
|
|
|
|
m_database_name = database_name;
|
|
|
|
m_connection_id = *connection_id;
|
2021-09-07 12:46:20 +00:00
|
|
|
} else {
|
2022-12-03 12:07:42 +00:00
|
|
|
warnln("\033[33;1mCould not connect to:\033[0m {}", database_name);
|
|
|
|
m_loop.quit(1);
|
2021-09-07 12:46:20 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-18 21:40:00 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
void source_file(ByteString file_name)
|
2021-09-16 20:25:29 +00:00
|
|
|
{
|
|
|
|
m_input_file_chain.append(move(file_name));
|
|
|
|
m_quit_when_files_read = false;
|
|
|
|
}
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
void read_file(ByteString file_name)
|
2021-09-16 20:25:29 +00:00
|
|
|
{
|
|
|
|
m_input_file_chain.append(move(file_name));
|
|
|
|
m_quit_when_files_read = true;
|
|
|
|
}
|
|
|
|
|
2021-09-07 12:46:20 +00:00
|
|
|
auto run()
|
|
|
|
{
|
2022-12-03 12:07:42 +00:00
|
|
|
read_sql();
|
2021-09-07 12:46:20 +00:00
|
|
|
return m_loop.exec();
|
|
|
|
}
|
2021-04-18 21:40:00 +00:00
|
|
|
|
2021-09-07 12:46:20 +00:00
|
|
|
private:
|
2024-05-21 10:49:35 +00:00
|
|
|
ByteString m_history_path;
|
2021-09-07 12:46:20 +00:00
|
|
|
RefPtr<Line::Editor> m_editor { nullptr };
|
|
|
|
int m_repl_line_level { 0 };
|
|
|
|
bool m_keep_running { true };
|
2023-12-16 14:19:34 +00:00
|
|
|
ByteString m_database_name {};
|
2022-12-09 22:09:08 +00:00
|
|
|
NonnullRefPtr<SQL::SQLClient> m_sql_client;
|
2022-12-07 18:01:55 +00:00
|
|
|
SQL::ConnectionID m_connection_id { 0 };
|
2022-12-09 22:09:08 +00:00
|
|
|
Core::EventLoop& m_loop;
|
2023-05-03 22:45:18 +00:00
|
|
|
OwnPtr<Core::InputBufferedFile> m_input_file { nullptr };
|
2021-09-16 20:25:29 +00:00
|
|
|
bool m_quit_when_files_read { false };
|
2023-12-16 14:19:34 +00:00
|
|
|
Vector<ByteString> m_input_file_chain {};
|
2022-12-09 22:09:08 +00:00
|
|
|
Array<u8, 4096> m_buffer {};
|
2021-09-16 20:25:29 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
Optional<ByteString> get_line()
|
2021-09-16 20:25:29 +00:00
|
|
|
{
|
|
|
|
if (!m_input_file && !m_input_file_chain.is_empty()) {
|
|
|
|
auto file_name = m_input_file_chain.take_first();
|
2023-02-09 02:02:46 +00:00
|
|
|
auto file_or_error = Core::File::open(file_name, Core::File::OpenMode::Read);
|
2021-09-16 20:25:29 +00:00
|
|
|
if (file_or_error.is_error()) {
|
2021-11-07 01:15:10 +00:00
|
|
|
warnln("Input file {} could not be opened: {}", file_name, file_or_error.error());
|
2021-09-16 20:25:29 +00:00
|
|
|
return {};
|
|
|
|
}
|
2022-09-20 10:43:48 +00:00
|
|
|
|
2023-05-03 22:45:18 +00:00
|
|
|
auto buffered_file_or_error = Core::InputBufferedFile::create(file_or_error.release_value());
|
2022-09-20 10:43:48 +00:00
|
|
|
if (buffered_file_or_error.is_error()) {
|
|
|
|
warnln("Input file {} could not be buffered: {}", file_name, buffered_file_or_error.error());
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
m_input_file = buffered_file_or_error.release_value();
|
2021-09-16 20:25:29 +00:00
|
|
|
}
|
|
|
|
if (m_input_file) {
|
2022-09-20 10:43:48 +00:00
|
|
|
auto line = m_input_file->read_line(m_buffer);
|
|
|
|
if (line.is_error()) {
|
|
|
|
warnln("Failed to read line: {}", line.error());
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
if (m_input_file->is_eof()) {
|
2021-09-16 20:25:29 +00:00
|
|
|
m_input_file->close();
|
|
|
|
m_input_file = nullptr;
|
|
|
|
if (m_quit_when_files_read && m_input_file_chain.is_empty())
|
|
|
|
return {};
|
|
|
|
}
|
2022-09-20 10:43:48 +00:00
|
|
|
return line.release_value();
|
2021-09-16 20:25:29 +00:00
|
|
|
// If the last file is exhausted but m_quit_when_files_read is false
|
2023-05-06 14:53:22 +00:00
|
|
|
// we fall through to the standard reading from the editor behavior
|
2021-09-16 20:25:29 +00:00
|
|
|
}
|
|
|
|
auto line_result = m_editor->get_line(prompt_for_level(m_repl_line_level));
|
|
|
|
if (line_result.is_error())
|
|
|
|
return {};
|
|
|
|
return line_result.value();
|
|
|
|
}
|
2021-09-07 12:46:20 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
ByteString read_next_piece()
|
2021-09-07 12:46:20 +00:00
|
|
|
{
|
|
|
|
StringBuilder piece;
|
2021-04-18 21:40:00 +00:00
|
|
|
|
2021-09-07 12:46:20 +00:00
|
|
|
do {
|
|
|
|
if (!piece.is_empty())
|
|
|
|
piece.append('\n');
|
2021-04-18 21:40:00 +00:00
|
|
|
|
2021-09-16 20:25:29 +00:00
|
|
|
auto line_maybe = get_line();
|
2021-04-18 21:40:00 +00:00
|
|
|
|
2021-09-16 20:25:29 +00:00
|
|
|
if (!line_maybe.has_value()) {
|
2021-09-07 12:46:20 +00:00
|
|
|
m_keep_running = false;
|
|
|
|
return {};
|
2021-04-18 21:40:00 +00:00
|
|
|
}
|
|
|
|
|
2021-09-16 20:25:29 +00:00
|
|
|
auto& line = line_maybe.value();
|
2021-09-07 12:46:20 +00:00
|
|
|
auto lexer = SQL::AST::Lexer(line);
|
|
|
|
|
|
|
|
m_editor->add_to_history(line);
|
|
|
|
piece.append(line);
|
|
|
|
|
|
|
|
bool is_first_token = true;
|
|
|
|
bool is_command = false;
|
|
|
|
bool last_token_ended_statement = false;
|
2022-03-23 03:33:34 +00:00
|
|
|
bool tokens_found = false;
|
2021-09-07 12:46:20 +00:00
|
|
|
|
|
|
|
for (SQL::AST::Token token = lexer.next(); token.type() != SQL::AST::TokenType::Eof; token = lexer.next()) {
|
2022-03-23 03:33:34 +00:00
|
|
|
tokens_found = true;
|
2021-09-07 12:46:20 +00:00
|
|
|
switch (token.type()) {
|
|
|
|
case SQL::AST::TokenType::ParenOpen:
|
|
|
|
++m_repl_line_level;
|
|
|
|
break;
|
|
|
|
case SQL::AST::TokenType::ParenClose:
|
|
|
|
--m_repl_line_level;
|
|
|
|
break;
|
|
|
|
case SQL::AST::TokenType::SemiColon:
|
|
|
|
last_token_ended_statement = true;
|
|
|
|
break;
|
|
|
|
case SQL::AST::TokenType::Period:
|
|
|
|
if (is_first_token)
|
|
|
|
is_command = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
last_token_ended_statement = is_command;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
is_first_token = false;
|
2021-04-18 21:40:00 +00:00
|
|
|
}
|
|
|
|
|
2022-03-23 03:33:34 +00:00
|
|
|
if (tokens_found)
|
|
|
|
m_repl_line_level = last_token_ended_statement ? 0 : (m_repl_line_level > 0 ? m_repl_line_level : 1);
|
2021-09-07 12:46:20 +00:00
|
|
|
} while ((m_repl_line_level > 0) || piece.is_empty());
|
2021-06-29 01:20:15 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
return piece.to_byte_string();
|
2021-09-07 12:46:20 +00:00
|
|
|
}
|
2021-06-29 01:20:15 +00:00
|
|
|
|
2021-09-07 12:46:20 +00:00
|
|
|
void read_sql()
|
|
|
|
{
|
2023-12-16 14:19:34 +00:00
|
|
|
ByteString piece = read_next_piece();
|
2021-09-16 20:25:29 +00:00
|
|
|
|
|
|
|
// m_keep_running can be set to false when the file we are reading
|
|
|
|
// from is exhausted...
|
|
|
|
if (!m_keep_running) {
|
2022-12-03 12:07:42 +00:00
|
|
|
m_sql_client->disconnect(m_connection_id);
|
|
|
|
m_loop.quit(0);
|
2021-09-07 12:46:20 +00:00
|
|
|
return;
|
2021-09-16 20:25:29 +00:00
|
|
|
}
|
2021-06-29 01:20:15 +00:00
|
|
|
|
2021-09-07 12:46:20 +00:00
|
|
|
if (piece.starts_with('.')) {
|
2022-03-24 03:41:52 +00:00
|
|
|
bool ready_for_input = handle_command(piece);
|
|
|
|
if (ready_for_input)
|
|
|
|
m_loop.deferred_invoke([this]() {
|
|
|
|
read_sql();
|
|
|
|
});
|
2022-12-02 21:25:27 +00:00
|
|
|
} else if (auto statement_id = m_sql_client->prepare_statement(m_connection_id, piece); statement_id.has_value()) {
|
|
|
|
m_sql_client->async_execute_statement(*statement_id, {});
|
2022-12-28 15:57:30 +00:00
|
|
|
} else {
|
|
|
|
warnln("\033[33;1mError parsing SQL statement\033[0m: {}", piece);
|
|
|
|
m_loop.deferred_invoke([this]() {
|
|
|
|
read_sql();
|
|
|
|
});
|
2021-06-29 01:20:15 +00:00
|
|
|
}
|
|
|
|
|
2021-09-16 20:25:29 +00:00
|
|
|
// ...But m_keep_running can also be set to false by a command handler.
|
2021-09-07 12:46:20 +00:00
|
|
|
if (!m_keep_running) {
|
2022-12-03 12:07:42 +00:00
|
|
|
m_sql_client->disconnect(m_connection_id);
|
|
|
|
m_loop.quit(0);
|
2021-09-07 12:46:20 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-07-08 02:48:11 +00:00
|
|
|
}
|
2021-06-29 01:20:15 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
static ByteString prompt_for_level(int level)
|
2021-09-07 12:46:20 +00:00
|
|
|
{
|
|
|
|
static StringBuilder prompt_builder;
|
|
|
|
prompt_builder.clear();
|
2022-07-11 17:32:29 +00:00
|
|
|
prompt_builder.append("> "sv);
|
2021-06-29 01:20:15 +00:00
|
|
|
|
2021-09-07 12:46:20 +00:00
|
|
|
for (auto i = 0; i < level; ++i)
|
2022-07-11 17:32:29 +00:00
|
|
|
prompt_builder.append(" "sv);
|
2021-06-29 01:20:15 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
return prompt_builder.to_byte_string();
|
2021-09-07 12:46:20 +00:00
|
|
|
}
|
2021-06-29 01:20:15 +00:00
|
|
|
|
2022-03-24 03:41:52 +00:00
|
|
|
bool handle_command(StringView command)
|
2021-09-07 12:46:20 +00:00
|
|
|
{
|
2022-03-24 03:41:52 +00:00
|
|
|
bool ready_for_input = true;
|
2021-09-07 12:46:20 +00:00
|
|
|
if (command == ".exit" || command == ".quit") {
|
|
|
|
m_keep_running = false;
|
2022-03-24 03:41:52 +00:00
|
|
|
ready_for_input = false;
|
2022-07-11 17:32:29 +00:00
|
|
|
} else if (command.starts_with(".connect "sv)) {
|
2021-09-07 12:46:20 +00:00
|
|
|
auto parts = command.split_view(' ');
|
2022-03-24 03:41:52 +00:00
|
|
|
if (parts.size() == 2) {
|
2021-09-07 12:46:20 +00:00
|
|
|
connect(parts[1]);
|
2022-03-24 03:41:52 +00:00
|
|
|
ready_for_input = false;
|
|
|
|
} else {
|
2021-09-16 20:25:29 +00:00
|
|
|
outln("\033[33;1mUsage: .connect <database name>\033[0m");
|
2022-03-24 03:41:52 +00:00
|
|
|
}
|
2022-07-11 17:32:29 +00:00
|
|
|
} else if (command.starts_with(".read "sv)) {
|
2021-09-16 20:25:29 +00:00
|
|
|
if (!m_input_file) {
|
|
|
|
auto parts = command.split_view(' ');
|
|
|
|
if (parts.size() == 2) {
|
|
|
|
source_file(parts[1]);
|
|
|
|
} else {
|
|
|
|
outln("\033[33;1mUsage: .read <sql file>\033[0m");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
outln("\033[33;1mCannot recursively read sql files\033[0m");
|
|
|
|
}
|
2021-09-02 10:08:11 +00:00
|
|
|
} else {
|
2021-09-07 12:46:20 +00:00
|
|
|
outln("\033[33;1mUnrecognized command:\033[0m {}", command);
|
2021-09-02 10:08:11 +00:00
|
|
|
}
|
2022-03-24 03:41:52 +00:00
|
|
|
return ready_for_input;
|
2021-09-07 12:46:20 +00:00
|
|
|
}
|
|
|
|
};
|
2021-06-29 01:20:15 +00:00
|
|
|
|
2022-01-25 01:29:26 +00:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2021-09-07 12:46:20 +00:00
|
|
|
{
|
2023-12-16 14:19:34 +00:00
|
|
|
ByteString database_name(getlogin());
|
|
|
|
ByteString file_to_source;
|
|
|
|
ByteString file_to_read;
|
2021-09-16 20:25:29 +00:00
|
|
|
bool suppress_sqlrc = false;
|
2023-12-16 14:19:34 +00:00
|
|
|
auto sqlrc_path = ByteString::formatted("{}/.sqlrc", Core::StandardPaths::home_directory());
|
2022-12-09 22:09:08 +00:00
|
|
|
#if !defined(AK_OS_SERENITY)
|
|
|
|
StringView sql_server_path;
|
|
|
|
#endif
|
2021-06-29 01:20:15 +00:00
|
|
|
|
2021-09-07 12:46:20 +00:00
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
args_parser.set_general_help("This is a client for the SerenitySQL database server.");
|
|
|
|
args_parser.add_option(database_name, "Database to connect to", "database", 'd', "database");
|
2021-09-16 20:25:29 +00:00
|
|
|
args_parser.add_option(file_to_read, "File to read", "read", 'r', "file");
|
|
|
|
args_parser.add_option(file_to_source, "File to source", "source", 's', "file");
|
|
|
|
args_parser.add_option(suppress_sqlrc, "Don't read ~/.sqlrc", "no-sqlrc", 'n');
|
2022-12-09 22:09:08 +00:00
|
|
|
#if !defined(AK_OS_SERENITY)
|
2023-08-18 15:26:53 +00:00
|
|
|
args_parser.add_option(sql_server_path, "Path to SQLServer to launch if needed", "sql-server-path", 'p', "path");
|
2022-12-09 22:09:08 +00:00
|
|
|
#endif
|
2022-01-25 01:29:26 +00:00
|
|
|
args_parser.parse(arguments);
|
2021-04-18 21:40:00 +00:00
|
|
|
|
2022-12-09 22:09:08 +00:00
|
|
|
Core::EventLoop loop;
|
|
|
|
|
|
|
|
#if defined(AK_OS_SERENITY)
|
|
|
|
auto sql_client = TRY(SQL::SQLClient::try_create());
|
|
|
|
#else
|
2023-02-02 10:03:46 +00:00
|
|
|
VERIFY(!sql_server_path.is_empty());
|
2024-04-23 22:59:00 +00:00
|
|
|
|
|
|
|
auto [_, sql_client] = TRY(Core::IPCProcess::spawn_singleton<SQL::SQLClient>({
|
|
|
|
.name = "SQLServer"sv,
|
|
|
|
.executable = sql_server_path,
|
|
|
|
}));
|
2022-12-09 22:09:08 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
SQLRepl repl(loop, database_name, move(sql_client));
|
2021-09-16 20:25:29 +00:00
|
|
|
|
2023-03-21 15:35:30 +00:00
|
|
|
if (!suppress_sqlrc && FileSystem::exists(sqlrc_path))
|
2021-09-16 20:25:29 +00:00
|
|
|
repl.source_file(sqlrc_path);
|
|
|
|
if (!file_to_source.is_empty())
|
|
|
|
repl.source_file(file_to_source);
|
|
|
|
if (!file_to_read.is_empty())
|
|
|
|
repl.read_file(file_to_read);
|
2021-09-07 12:46:20 +00:00
|
|
|
return repl.run();
|
2021-04-18 21:40:00 +00:00
|
|
|
}
|