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>
|
2021-03-31 14:02:51 +00:00
|
|
|
#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*/,
|
2023-11-09 15:50:10 +00:00
|
|
|
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,
|
2023-11-09 15:50:10 +00:00
|
|
|
cached_columns,
|
2021-04-23 12:18:23 +00:00
|
|
|
ConstraintsDescription{},
|
|
|
|
String{},
|
2023-05-19 00:44:27 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-07-06 08:56:07 +00:00
|
|
|
ColumnsDescription TableFunctionPostgreSQL::getActualTableStructure(ContextPtr context, bool /*is_insert_query*/) const
|
2020-11-25 21:06:33 +00:00
|
|
|
{
|
2023-05-19 00:44:27 +00:00
|
|
|
return StoragePostgreSQL::getTableStructureFromData(connection_pool, configuration->table, configuration->schema, context);
|
2020-11-25 21:06:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-04-10 23:33:54 +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)
|
2023-01-23 21:13:58 +00:00
|
|
|
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
|