2018-08-19 17:09:54 +00:00
|
|
|
#include "ColumnInfoHandler.h"
|
2018-09-28 02:46:33 +00:00
|
|
|
#include "getIdentifierQuote.h"
|
2018-08-19 17:09:54 +00:00
|
|
|
#if USE_POCO_SQLODBC || USE_POCO_DATAODBC
|
2018-09-14 19:48:51 +00:00
|
|
|
|
|
|
|
#if USE_POCO_SQLODBC
|
2019-06-05 11:52:39 +00:00
|
|
|
#include <Poco/SQL/ODBC/ODBCException.h>
|
|
|
|
#include <Poco/SQL/ODBC/SessionImpl.h>
|
|
|
|
#include <Poco/SQL/ODBC/Utility.h>
|
2018-09-14 19:48:51 +00:00
|
|
|
#define POCO_SQL_ODBC_CLASS Poco::SQL::ODBC
|
|
|
|
#endif
|
|
|
|
#if USE_POCO_DATAODBC
|
2018-08-19 17:09:54 +00:00
|
|
|
#include <Poco/Data/ODBC/ODBCException.h>
|
|
|
|
#include <Poco/Data/ODBC/SessionImpl.h>
|
|
|
|
#include <Poco/Data/ODBC/Utility.h>
|
2018-09-14 19:48:51 +00:00
|
|
|
#define POCO_SQL_ODBC_CLASS Poco::Data::ODBC
|
|
|
|
#endif
|
|
|
|
|
2018-08-19 17:09:54 +00:00
|
|
|
#include <Poco/Net/HTTPServerRequest.h>
|
|
|
|
#include <Poco/Net/HTTPServerResponse.h>
|
2018-09-14 19:48:51 +00:00
|
|
|
#include <Poco/Net/HTMLForm.h>
|
2019-10-28 11:01:09 +00:00
|
|
|
#include <Poco/NumberParser.h>
|
2018-08-19 17:09:54 +00:00
|
|
|
#include <DataTypes/DataTypeFactory.h>
|
2019-10-21 09:13:33 +00:00
|
|
|
#include <DataTypes/DataTypeNullable.h>
|
2018-08-19 17:09:54 +00:00
|
|
|
#include <IO/WriteBufferFromHTTPServerResponse.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
2018-08-17 09:07:39 +00:00
|
|
|
#include <Parsers/ParserQueryWithOutput.h>
|
|
|
|
#include <Parsers/parseQuery.h>
|
2018-08-19 17:09:54 +00:00
|
|
|
#include <common/logger_useful.h>
|
|
|
|
#include <ext/scope_guard.h>
|
|
|
|
#include "validateODBCConnectionString.h"
|
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-03 12:10:57 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
2018-08-19 17:09:54 +00:00
|
|
|
void ODBCColumnsInfoHandler::handleRequest(Poco::Net::HTTPServerRequest & request, Poco::Net::HTTPServerResponse & response)
|
|
|
|
{
|
|
|
|
Poco::Net::HTMLForm params(request, request.stream());
|
|
|
|
LOG_TRACE(log, "Request URI: " + request.getURI());
|
|
|
|
|
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;
|
|
|
|
LOG_WARNING(log, message);
|
|
|
|
};
|
|
|
|
|
|
|
|
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");
|
|
|
|
LOG_TRACE(log, "Will fetch info for table '" << schema_name + "." + table_name << "'");
|
2018-10-12 03:11:19 +00:00
|
|
|
}
|
|
|
|
else
|
2018-08-17 09:07:39 +00:00
|
|
|
LOG_TRACE(log, "Will fetch info for table '" << table_name << "'");
|
2018-08-19 17:09:54 +00:00
|
|
|
LOG_TRACE(log, "Got connection str '" << connection_string << "'");
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
/// TODO Why not do SQLColumns instead?
|
2018-08-17 09:07:39 +00:00
|
|
|
std::string name = schema_name.empty() ? table_name : schema_name + "." + table_name;
|
|
|
|
std::stringstream ss;
|
|
|
|
std::string input = "SELECT * FROM " + name + " WHERE 1 = 0";
|
|
|
|
ParserQueryWithOutput parser;
|
|
|
|
ASTPtr select = parseQuery(parser, input.data(), input.data() + input.size(), "", 0);
|
|
|
|
|
|
|
|
IAST::FormatSettings settings(ss, true);
|
|
|
|
settings.always_quote_identifiers = true;
|
2018-09-28 02:46:33 +00:00
|
|
|
|
|
|
|
auto identifier_quote = getIdentifierQuote(hdbc);
|
|
|
|
if (identifier_quote.length() == 0)
|
|
|
|
settings.identifier_quoting_style = IdentifierQuotingStyle::None;
|
2018-11-23 18:52:00 +00:00
|
|
|
else if (identifier_quote[0] == '`')
|
2018-09-28 02:46:33 +00:00
|
|
|
settings.identifier_quoting_style = IdentifierQuotingStyle::Backticks;
|
2018-11-23 18:52:00 +00:00
|
|
|
else if (identifier_quote[0] == '"')
|
2018-09-28 02:46:33 +00:00
|
|
|
settings.identifier_quoting_style = IdentifierQuotingStyle::DoubleQuotes;
|
|
|
|
else
|
2018-10-03 12:10:57 +00:00
|
|
|
throw Exception("Can not map quote identifier '" + identifier_quote + "' to IdentifierQuotingStyle value", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2018-09-28 02:46:33 +00:00
|
|
|
|
2018-08-17 09:07:39 +00:00
|
|
|
select->format(settings);
|
|
|
|
std::string query = ss.str();
|
|
|
|
|
2018-10-03 12:10:57 +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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|