2018-08-19 17:09:54 +00:00
|
|
|
#include "ColumnInfoHandler.h"
|
2018-09-14 19:48:51 +00:00
|
|
|
|
2020-05-08 14:11:19 +00:00
|
|
|
#if USE_ODBC
|
|
|
|
|
|
|
|
# include <DataTypes/DataTypeFactory.h>
|
|
|
|
# include <DataTypes/DataTypeNullable.h>
|
|
|
|
# include <IO/WriteBufferFromHTTPServerResponse.h>
|
|
|
|
# include <IO/WriteHelpers.h>
|
|
|
|
# include <Parsers/ParserQueryWithOutput.h>
|
|
|
|
# include <Parsers/parseQuery.h>
|
|
|
|
# include <Poco/Data/ODBC/ODBCException.h>
|
|
|
|
# include <Poco/Data/ODBC/SessionImpl.h>
|
|
|
|
# include <Poco/Data/ODBC/Utility.h>
|
|
|
|
# include <Poco/Net/HTMLForm.h>
|
|
|
|
# include <Poco/Net/HTTPServerRequest.h>
|
|
|
|
# include <Poco/Net/HTTPServerResponse.h>
|
|
|
|
# include <Poco/NumberParser.h>
|
|
|
|
# include <common/logger_useful.h>
|
2020-05-14 21:51:07 +00:00
|
|
|
# include <Common/quoteString.h>
|
2020-05-08 14:11:19 +00:00
|
|
|
# include <ext/scope_guard.h>
|
|
|
|
# include "getIdentifierQuote.h"
|
|
|
|
# include "validateODBCConnectionString.h"
|
|
|
|
|
|
|
|
# define POCO_SQL_ODBC_CLASS Poco::Data::ODBC
|
2018-08-19 17:09:54 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
DataTypePtr getDataType(SQLSMALLINT type)
|
|
|
|
{
|
|
|
|
const auto & factory = DataTypeFactory::instance();
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
2019-10-25 17:49:49 +00:00
|
|
|
case SQL_TINYINT:
|
|
|
|
return factory.get("Int8");
|
2018-08-19 17:09:54 +00:00
|
|
|
case SQL_INTEGER:
|
|
|
|
return factory.get("Int32");
|
|
|
|
case SQL_SMALLINT:
|
|
|
|
return factory.get("Int16");
|
2019-10-25 17:49:49 +00:00
|
|
|
case SQL_BIGINT:
|
|
|
|
return factory.get("Int64");
|
2018-08-19 17:09:54 +00:00
|
|
|
case SQL_FLOAT:
|
2019-10-25 17:49:49 +00:00
|
|
|
return factory.get("Float64");
|
2018-08-19 17:09:54 +00:00
|
|
|
case SQL_REAL:
|
|
|
|
return factory.get("Float32");
|
|
|
|
case SQL_DOUBLE:
|
|
|
|
return factory.get("Float64");
|
|
|
|
case SQL_DATETIME:
|
|
|
|
return factory.get("DateTime");
|
|
|
|
case SQL_TYPE_TIMESTAMP:
|
|
|
|
return factory.get("DateTime");
|
|
|
|
case SQL_TYPE_DATE:
|
|
|
|
return factory.get("Date");
|
|
|
|
default:
|
|
|
|
return factory.get("String");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ODBCColumnsInfoHandler::handleRequest(Poco::Net::HTTPServerRequest & request, Poco::Net::HTTPServerResponse & response)
|
|
|
|
{
|
|
|
|
Poco::Net::HTMLForm params(request, request.stream());
|
2020-05-23 22:24:01 +00:00
|
|
|
LOG_TRACE(log, "Request URI: {}", request.getURI());
|
2018-08-19 17:09:54 +00:00
|
|
|
|
2018-08-24 00:07:25 +00:00
|
|
|
auto process_error = [&response, this](const std::string & message)
|
|
|
|
{
|
2018-08-19 17:09:54 +00:00
|
|
|
response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_INTERNAL_SERVER_ERROR);
|
|
|
|
if (!response.sent())
|
|
|
|
response.send() << message << std::endl;
|
2020-05-23 22:24:01 +00:00
|
|
|
LOG_WARNING(log, message);
|
2018-08-19 17:09:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (!params.has("table"))
|
|
|
|
{
|
|
|
|
process_error("No 'table' param in request URL");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!params.has("connection_string"))
|
|
|
|
{
|
|
|
|
process_error("No 'connection_string' in request URL");
|
|
|
|
return;
|
|
|
|
}
|
2020-03-09 03:38:43 +00:00
|
|
|
std::string schema_name;
|
2018-08-19 17:09:54 +00:00
|
|
|
std::string table_name = params.get("table");
|
|
|
|
std::string connection_string = params.get("connection_string");
|
2019-10-28 11:01:09 +00:00
|
|
|
|
2018-08-17 09:07:39 +00:00
|
|
|
if (params.has("schema"))
|
|
|
|
{
|
|
|
|
schema_name = params.get("schema");
|
2020-05-23 22:24:01 +00:00
|
|
|
LOG_TRACE(log, "Will fetch info for table '{}'", schema_name + "." + table_name);
|
2018-10-12 03:11:19 +00:00
|
|
|
}
|
|
|
|
else
|
2020-05-23 22:24:01 +00:00
|
|
|
LOG_TRACE(log, "Will fetch info for table '{}'", table_name);
|
|
|
|
LOG_TRACE(log, "Got connection str '{}'", connection_string);
|
2018-08-19 17:09:54 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2019-11-07 06:08:59 +00:00
|
|
|
const bool external_table_functions_use_nulls = Poco::NumberParser::parseBool(params.get("external_table_functions_use_nulls", "false"));
|
|
|
|
|
2018-09-14 19:48:51 +00:00
|
|
|
POCO_SQL_ODBC_CLASS::SessionImpl session(validateODBCConnectionString(connection_string), DBMS_DEFAULT_CONNECT_TIMEOUT_SEC);
|
2018-08-19 17:09:54 +00:00
|
|
|
SQLHDBC hdbc = session.dbc().handle();
|
|
|
|
|
|
|
|
SQLHSTMT hstmt = nullptr;
|
|
|
|
|
2018-09-14 19:48:51 +00:00
|
|
|
if (POCO_SQL_ODBC_CLASS::Utility::isError(SQLAllocStmt(hdbc, &hstmt)))
|
|
|
|
throw POCO_SQL_ODBC_CLASS::ODBCException("Could not allocate connection handle.");
|
2018-08-19 17:09:54 +00:00
|
|
|
|
|
|
|
SCOPE_EXIT(SQLFreeStmt(hstmt, SQL_DROP));
|
|
|
|
|
2020-04-17 16:19:14 +00:00
|
|
|
const auto & context_settings = context.getSettingsRef();
|
2020-04-15 20:28:05 +00:00
|
|
|
|
2018-08-19 17:09:54 +00:00
|
|
|
/// TODO Why not do SQLColumns instead?
|
2020-05-14 21:51:07 +00:00
|
|
|
std::string name = schema_name.empty() ? backQuoteIfNeed(table_name) : backQuoteIfNeed(schema_name) + "." + backQuoteIfNeed(table_name);
|
2020-11-09 16:05:40 +00:00
|
|
|
WriteBufferFromOwnString buf;
|
2018-08-17 09:07:39 +00:00
|
|
|
std::string input = "SELECT * FROM " + name + " WHERE 1 = 0";
|
2020-12-16 17:11:32 +00:00
|
|
|
ParserQueryWithOutput parser(input.data() + input.size());
|
2020-04-15 20:28:05 +00:00
|
|
|
ASTPtr select = parseQuery(parser, input.data(), input.data() + input.size(), "", context_settings.max_query_size, context_settings.max_parser_depth);
|
2018-08-17 09:07:39 +00:00
|
|
|
|
2020-11-09 16:05:40 +00:00
|
|
|
IAST::FormatSettings settings(buf, true);
|
2018-08-17 09:07:39 +00:00
|
|
|
settings.always_quote_identifiers = true;
|
2020-05-14 21:51:07 +00:00
|
|
|
settings.identifier_quoting_style = getQuotingStyle(hdbc);
|
2018-08-17 09:07:39 +00:00
|
|
|
select->format(settings);
|
2020-11-09 16:05:40 +00:00
|
|
|
std::string query = buf.str();
|
2018-08-17 09:07:39 +00:00
|
|
|
|
2020-05-23 22:24:01 +00:00
|
|
|
LOG_TRACE(log, "Inferring structure with query '{}'", query);
|
2018-09-28 02:46:33 +00:00
|
|
|
|
2018-09-14 19:48:51 +00:00
|
|
|
if (POCO_SQL_ODBC_CLASS::Utility::isError(POCO_SQL_ODBC_CLASS::SQLPrepare(hstmt, reinterpret_cast<SQLCHAR *>(query.data()), query.size())))
|
|
|
|
throw POCO_SQL_ODBC_CLASS::DescriptorException(session.dbc());
|
2018-08-19 17:09:54 +00:00
|
|
|
|
2018-09-14 19:48:51 +00:00
|
|
|
if (POCO_SQL_ODBC_CLASS::Utility::isError(SQLExecute(hstmt)))
|
|
|
|
throw POCO_SQL_ODBC_CLASS::StatementException(hstmt);
|
2018-08-19 17:09:54 +00:00
|
|
|
|
|
|
|
SQLSMALLINT cols = 0;
|
2018-09-14 19:48:51 +00:00
|
|
|
if (POCO_SQL_ODBC_CLASS::Utility::isError(SQLNumResultCols(hstmt, &cols)))
|
|
|
|
throw POCO_SQL_ODBC_CLASS::StatementException(hstmt);
|
2018-08-19 17:09:54 +00:00
|
|
|
|
|
|
|
/// TODO cols not checked
|
|
|
|
|
|
|
|
NamesAndTypesList columns;
|
|
|
|
for (SQLSMALLINT ncol = 1; ncol <= cols; ++ncol)
|
|
|
|
{
|
|
|
|
SQLSMALLINT type = 0;
|
|
|
|
/// TODO Why 301?
|
|
|
|
SQLCHAR column_name[301];
|
2019-10-21 09:13:33 +00:00
|
|
|
|
2019-10-28 11:01:09 +00:00
|
|
|
SQLSMALLINT is_nullable;
|
|
|
|
const auto result = POCO_SQL_ODBC_CLASS::SQLDescribeCol(hstmt, ncol, column_name, sizeof(column_name), nullptr, &type, nullptr, nullptr, &is_nullable);
|
2019-10-21 09:13:33 +00:00
|
|
|
if (POCO_SQL_ODBC_CLASS::Utility::isError(result))
|
|
|
|
throw POCO_SQL_ODBC_CLASS::StatementException(hstmt);
|
|
|
|
|
|
|
|
auto column_type = getDataType(type);
|
2019-10-28 11:01:09 +00:00
|
|
|
if (external_table_functions_use_nulls && is_nullable == SQL_NULLABLE)
|
2019-10-21 09:13:33 +00:00
|
|
|
{
|
|
|
|
column_type = std::make_shared<DataTypeNullable>(column_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
columns.emplace_back(reinterpret_cast<char *>(column_name), std::move(column_type));
|
2018-08-19 17:09:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WriteBufferFromHTTPServerResponse out(request, response, keep_alive_timeout);
|
|
|
|
writeStringBinary(columns.toString(), out);
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
process_error("Error getting columns from ODBC '" + getCurrentExceptionMessage(false) + "'");
|
|
|
|
tryLogCurrentException(log);
|
|
|
|
}
|
|
|
|
}
|
2020-05-08 14:11:19 +00:00
|
|
|
|
2018-08-19 17:09:54 +00:00
|
|
|
}
|
2020-05-08 14:11:19 +00:00
|
|
|
|
2018-08-19 17:09:54 +00:00
|
|
|
#endif
|