1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 22:17:42 +00:00

SQLServer+SQL+LibSQL: Allow sql client to specify the database name

The database the sql client connected to was 'hardcoded' to the login
name of the calling user.
- Extended the IPC API to be more expressive when connecting, by
returning the name of the database the client connected to in the
'connected' callback.
- Gave the sql client a command line argument (-d/--database) allowing
an alternative database name to be specified

A subsequent commit will have a dot command allowing the user to
connect to different databases from the same sql session.
This commit is contained in:
Jan de Visser 2021-08-26 18:45:37 -04:00 committed by Andreas Kling
parent c5c7a9d198
commit e923cb3739
5 changed files with 18 additions and 10 deletions

View file

@ -12,10 +12,10 @@ SQLClient::~SQLClient()
{ {
} }
void SQLClient::connected(int connection_id) void SQLClient::connected(int connection_id, String const& connected_to_database)
{ {
if (on_connected) if (on_connected)
on_connected(connection_id); on_connected(connection_id, connected_to_database);
} }
void SQLClient::disconnected(int connection_id) void SQLClient::disconnected(int connection_id)

View file

@ -18,7 +18,7 @@ class SQLClient
C_OBJECT(SQLClient); C_OBJECT(SQLClient);
virtual ~SQLClient(); virtual ~SQLClient();
Function<void(int)> on_connected; Function<void(int, String const&)> on_connected;
Function<void(int)> on_disconnected; Function<void(int)> on_disconnected;
Function<void(int, int, String const&)> on_connection_error; Function<void(int, int, String const&)> on_connection_error;
Function<void(int, int, String const&)> on_execution_error; Function<void(int, int, String const&)> on_execution_error;
@ -32,7 +32,7 @@ private:
{ {
} }
virtual void connected(int connection_id) override; virtual void connected(int connection_id, String const& connected_to_database) override;
virtual void connection_error(int connection_id, int code, String const& message) override; virtual void connection_error(int connection_id, int code, String const& message) override;
virtual void execution_success(int statement_id, bool has_results, int created, int updated, int deleted) override; virtual void execution_success(int statement_id, bool has_results, int created, int updated, int deleted) override;
virtual void next_result(int statement_id, Vector<String> const&) override; virtual void next_result(int statement_id, Vector<String> const&) override;

View file

@ -43,7 +43,7 @@ DatabaseConnection::DatabaseConnection(String database_name, int client_id)
m_accept_statements = true; m_accept_statements = true;
auto client_connection = ClientConnection::client_connection_for(m_client_id); auto client_connection = ClientConnection::client_connection_for(m_client_id);
if (client_connection) if (client_connection)
client_connection->async_connected(m_connection_id); client_connection->async_connected(m_connection_id, m_database_name);
else else
warnln("Cannot notify client of database connection. Client disconnected"); warnln("Cannot notify client of database connection. Client disconnected");
}); });

View file

@ -1,6 +1,6 @@
endpoint SQLClient endpoint SQLClient
{ {
connected(int connection_id) =| connected(int connection_id, String connected_to_database) =|
connection_error(int connection_id, int code, String message) =| connection_error(int connection_id, int code, String message) =|
execution_success(int statement_id, bool has_results, int created, int updated, int deleted) =| execution_success(int statement_id, bool has_results, int created, int updated, int deleted) =|
next_result(int statement_id, Vector<String> row) =| next_result(int statement_id, Vector<String> row) =|

View file

@ -7,6 +7,7 @@
#include <AK/Format.h> #include <AK/Format.h>
#include <AK/String.h> #include <AK/String.h>
#include <AK/StringBuilder.h> #include <AK/StringBuilder.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/StandardPaths.h> #include <LibCore/StandardPaths.h>
#include <LibLine/Editor.h> #include <LibLine/Editor.h>
#include <LibSQL/AST/Lexer.h> #include <LibSQL/AST/Lexer.h>
@ -97,8 +98,15 @@ void handle_command(StringView command)
} }
int main() int main(int argc, char** argv)
{ {
String database_name(getlogin());
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");
args_parser.parse(argc, argv);
s_editor = Line::Editor::construct(); s_editor = Line::Editor::construct();
s_editor->load_history(s_history_path); s_editor->load_history(s_history_path);
@ -172,8 +180,8 @@ int main()
sql_client->async_disconnect(the_connection_id); sql_client->async_disconnect(the_connection_id);
}; };
sql_client->on_connected = [&](int connection_id) { sql_client->on_connected = [&](int connection_id, String const& connected_to_database) {
outln("** Connected to {} **", getlogin()); outln("** Connected to {} **", connected_to_database);
the_connection_id = connection_id; the_connection_id = connection_id;
read_sql(); read_sql();
}; };
@ -212,7 +220,7 @@ int main()
loop.quit(0); loop.quit(0);
}; };
sql_client->connect(getlogin()); sql_client->connect(database_name);
auto rc = loop.exec(); auto rc = loop.exec();
s_editor->save_history(s_history_path); s_editor->save_history(s_history_path);