ClickHouse/src/TableFunctions/TableFunctionPostgreSQL.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

98 lines
2.9 KiB
C++
Raw Normal View History

2023-11-06 03:33:23 +00:00
#include "config.h"
2020-11-25 21:06:33 +00:00
#if USE_LIBPQXX
2023-11-06 03:33:23 +00:00
2020-11-25 21:06:33 +00:00
#include <TableFunctions/ITableFunction.h>
2023-11-06 03:33:23 +00:00
#include <Core/PostgreSQL/PoolWithFailover.h>
#include <Storages/StoragePostgreSQL.h>
#include <Parsers/ASTFunction.h>
2020-11-25 21:06:33 +00:00
#include <TableFunctions/TableFunctionFactory.h>
#include <Common/Exception.h>
#include <Common/parseRemoteDescription.h>
2023-11-06 03:33:23 +00:00
#include "registerTableFunctions.h"
2020-11-25 21:06:33 +00:00
2021-01-11 10:23:44 +00:00
2020-11-25 21:06:33 +00:00
namespace DB
{
namespace ErrorCodes
{
extern const int BAD_ARGUMENTS;
}
2023-11-06 03:33:23 +00:00
namespace
{
class TableFunctionPostgreSQL : public ITableFunction
{
public:
static constexpr auto name = "postgresql";
std::string getName() const override { return name; }
private:
StoragePtr executeImpl(
const ASTPtr & ast_function, ContextPtr context,
const std::string & table_name, ColumnsDescription cached_columns, bool is_insert_query) const override;
const char * getStorageTypeName() const override { return "PostgreSQL"; }
ColumnsDescription getActualTableStructure(ContextPtr context, bool is_insert_query) const override;
void parseArguments(const ASTPtr & ast_function, ContextPtr context) override;
postgres::PoolWithFailoverPtr connection_pool;
std::optional<StoragePostgreSQL::Configuration> configuration;
};
2020-11-25 21:06:33 +00:00
StoragePtr TableFunctionPostgreSQL::executeImpl(const ASTPtr & /*ast_function*/,
ContextPtr context, const std::string & table_name, ColumnsDescription cached_columns, bool /*is_insert_query*/) const
2020-11-25 21:06:33 +00:00
{
auto result = std::make_shared<StoragePostgreSQL>(
2021-04-23 12:18:23 +00:00
StorageID(getDatabaseName(), table_name),
2021-05-11 16:22:24 +00:00
connection_pool,
2021-09-01 23:17:15 +00:00
configuration->table,
cached_columns,
2021-04-23 12:18:23 +00:00
ConstraintsDescription{},
String{},
context,
2021-09-01 23:17:15 +00:00
configuration->schema,
configuration->on_conflict);
2020-11-25 21:06:33 +00:00
result->startup();
return result;
}
ColumnsDescription TableFunctionPostgreSQL::getActualTableStructure(ContextPtr context, bool /*is_insert_query*/) const
2020-11-25 21:06:33 +00:00
{
return StoragePostgreSQL::getTableStructureFromData(connection_pool, configuration->table, configuration->schema, context);
2020-11-25 21:06:33 +00:00
}
void TableFunctionPostgreSQL::parseArguments(const ASTPtr & ast_function, ContextPtr context)
2020-11-25 21:06:33 +00:00
{
const auto & func_args = ast_function->as<ASTFunction &>();
if (!func_args.arguments)
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Table function 'PostgreSQL' must have arguments.");
2020-11-25 21:06:33 +00:00
2021-09-01 23:17:15 +00:00
configuration.emplace(StoragePostgreSQL::getConfiguration(func_args.arguments->children, context));
2022-06-23 21:44:59 +00:00
const auto & settings = context->getSettingsRef();
connection_pool = std::make_shared<postgres::PoolWithFailover>(
*configuration,
settings.postgresql_connection_pool_size,
settings.postgresql_connection_pool_wait_timeout,
POSTGRESQL_POOL_WITH_FAILOVER_DEFAULT_MAX_TRIES,
settings.postgresql_connection_pool_auto_close_connection);
2020-11-25 21:06:33 +00:00
}
2023-11-06 03:33:23 +00:00
}
2020-11-25 21:06:33 +00:00
void registerTableFunctionPostgreSQL(TableFunctionFactory & factory)
{
factory.registerFunction<TableFunctionPostgreSQL>();
}
}
#endif