Tool implementations
| 74 | |
| 75 | // Tool implementations |
| 76 | class ClickHouseTools { |
| 77 | public: |
| 78 | explicit ClickHouseTools(std::shared_ptr<clickhouse::Client> client) |
| 79 | : client_(std::move(client)) {} |
| 80 | |
| 81 | Tool createListDatabasesTool() { |
| 82 | return create_simple_tool( |
| 83 | "list_databases", |
| 84 | "List all available databases in the ClickHouse instance", {}, |
| 85 | [this](const JsonValue& args, const ToolExecutionContext& context) { |
| 86 | try { |
| 87 | std::vector<std::string> databases; |
| 88 | client_->Select( |
| 89 | "SELECT name FROM system.databases ORDER BY name", |
| 90 | [&databases](const clickhouse::Block& block) { |
| 91 | for (size_t i = 0; i < block.GetRowCount(); ++i) { |
| 92 | databases.push_back(std::string( |
| 93 | block[0]->As<clickhouse::ColumnString>()->At(i))); |
| 94 | } |
| 95 | }); |
| 96 | |
| 97 | std::stringstream result; |
| 98 | result << "Found " << databases.size() << " databases:\n"; |
| 99 | for (const auto& db : databases) { |
| 100 | result << "- " << db << "\n"; |
| 101 | } |
| 102 | |
| 103 | return JsonValue{{"result", result.str()}, |
| 104 | {"databases", databases}}; |
| 105 | } catch (const std::exception& e) { |
| 106 | throw std::runtime_error(std::string("Error: ") + e.what()); |
| 107 | } |
| 108 | }); |
| 109 | } |
| 110 | |
| 111 | Tool createListTablesInDatabaseTool() { |
| 112 | return create_simple_tool( |
| 113 | "list_tables_in_database", "List all tables in a specific database", |
| 114 | {{"database", "string"}}, |
| 115 | [this](const JsonValue& args, const ToolExecutionContext& context) { |
| 116 | try { |
| 117 | std::string database = args["database"].get<std::string>(); |
| 118 | std::vector<std::string> tables; |
| 119 | |
| 120 | std::string query = |
| 121 | "SELECT name FROM system.tables WHERE database = '" + database + |
| 122 | "' ORDER BY name"; |
| 123 | client_->Select(query, [&tables](const clickhouse::Block& block) { |
| 124 | for (size_t i = 0; i < block.GetRowCount(); ++i) { |
| 125 | tables.push_back(std::string( |
| 126 | block[0]->As<clickhouse::ColumnString>()->At(i))); |
| 127 | } |
| 128 | }); |
| 129 | |
| 130 | std::stringstream result; |
| 131 | result << "Found " << tables.size() << " tables in database '" |
| 132 | << database << "':\n"; |
| 133 | for (const auto& table : tables) { |
nothing calls this directly
no outgoing calls
no test coverage detected