ClickHouse/programs/odbc-bridge/IdentifierQuoteHandler.cpp

100 lines
3.3 KiB
C++
Raw Normal View History

2018-09-27 15:23:42 +00:00
#include "IdentifierQuoteHandler.h"
#if USE_ODBC
2021-03-22 11:40:29 +00:00
#include <DataTypes/DataTypeFactory.h>
#include <Server/HTTP/HTMLForm.h>
#include <Server/HTTP/WriteBufferFromHTTPServerResponse.h>
#include <IO/ReadHelpers.h>
2021-03-22 11:40:29 +00:00
#include <IO/WriteHelpers.h>
#include <Parsers/ParserQueryWithOutput.h>
#include <Parsers/parseQuery.h>
#include <Poco/Net/HTTPServerRequest.h>
#include <Poco/Net/HTTPServerResponse.h>
#include <Common/BridgeProtocolVersion.h>
2022-04-27 15:05:45 +00:00
#include <Common/logger_useful.h>
2021-10-02 07:13:14 +00:00
#include <base/scope_guard.h>
2021-03-22 11:40:29 +00:00
#include "getIdentifierQuote.h"
#include "validateODBCConnectionString.h"
#include "ODBCPooledConnectionFactory.h"
2021-03-31 12:41:12 +00:00
2018-09-27 15:23:42 +00:00
namespace DB
{
void IdentifierQuoteHandler::handleRequest(HTTPServerRequest & request, HTTPServerResponse & response)
2018-09-27 15:23:42 +00:00
{
2021-06-16 14:33:14 +00:00
HTMLForm params(getContext()->getSettingsRef(), request, request.getStream());
2020-05-23 22:24:01 +00:00
LOG_TRACE(log, "Request URI: {}", request.getURI());
2018-09-27 15:23:42 +00:00
2018-10-10 08:38:54 +00:00
auto process_error = [&response, this](const std::string & message)
{
2018-09-27 15:23:42 +00:00
response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_INTERNAL_SERVER_ERROR);
if (!response.sent())
*response.send() << message << std::endl;
LOG_WARNING(log, fmt::runtime(message));
2018-09-27 15:23:42 +00:00
};
size_t version;
if (!params.has("version"))
version = 0; /// assumed version for too old servers which do not send a version
else
{
String version_str = params.get("version");
if (!tryParse(version, version_str))
{
process_error("Unable to parse 'version' string in request URL: '" + version_str + "' Check if the server and library-bridge have the same version.");
return;
}
}
if (version != XDBC_BRIDGE_PROTOCOL_VERSION)
{
/// backwards compatibility is considered unnecessary for now, just let the user know that the server and the bridge must be upgraded together
process_error("Server and library-bridge have different versions: '" + std::to_string(version) + "' vs. '" + std::to_string(LIBRARY_BRIDGE_PROTOCOL_VERSION) + "'");
return;
}
if (!params.has("connection_string"))
2018-09-27 15:23:42 +00:00
{
process_error("No 'connection_string' in request URL");
return;
}
2023-01-19 00:36:25 +00:00
bool use_connection_pooling = params.getParsed<bool>("use_connection_pooling", true);
2018-09-27 15:23:42 +00:00
try
{
std::string connection_string = params.get("connection_string");
2021-04-06 18:59:34 +00:00
2023-01-19 00:36:25 +00:00
nanodbc::ConnectionHolderPtr connection;
if (use_connection_pooling)
connection = ODBCPooledConnectionFactory::instance().get(
validateODBCConnectionString(connection_string), getContext()->getSettingsRef().odbc_bridge_connection_pool_size);
else
connection = std::make_shared<nanodbc::ConnectionHolder>(validateODBCConnectionString(connection_string));
2021-04-06 18:59:34 +00:00
2021-06-07 18:09:16 +00:00
auto identifier = getIdentifierQuote(std::move(connection));
2018-09-27 15:23:42 +00:00
WriteBufferFromHTTPServerResponse out(response, request.getMethod() == Poco::Net::HTTPRequest::HTTP_HEAD, keep_alive_timeout);
try
{
writeStringBinary(identifier, out);
out.finalize();
}
catch (...)
{
out.finalize();
}
2018-09-27 15:23:42 +00:00
}
catch (...)
{
process_error("Error getting identifier quote style from ODBC '" + getCurrentExceptionMessage(false) + "'");
tryLogCurrentException(log);
}
}
2018-09-27 15:23:42 +00:00
}
2018-10-10 08:38:54 +00:00
#endif