2020-04-28 00:56:44 +00:00
|
|
|
#include "ODBCBlockOutputStream.h"
|
|
|
|
|
2021-01-27 00:54:57 +00:00
|
|
|
#include <Common/hex.h>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/logger_useful.h>
|
2020-04-28 00:56:44 +00:00
|
|
|
#include <Core/Field.h>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/LocalDate.h>
|
|
|
|
#include <base/LocalDateTime.h>
|
2020-05-14 21:51:07 +00:00
|
|
|
#include "getIdentifierQuote.h"
|
2021-03-22 11:40:29 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
#include <IO/Operators.h>
|
|
|
|
#include <Formats/FormatFactory.h>
|
2021-08-25 21:51:43 +00:00
|
|
|
#include <Parsers/getInsertQuery.h>
|
2020-05-14 21:51:07 +00:00
|
|
|
|
2020-04-28 00:56:44 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
|
2021-06-07 18:09:16 +00:00
|
|
|
ODBCBlockOutputStream::ODBCBlockOutputStream(nanodbc::ConnectionHolderPtr connection_holder_,
|
2020-04-28 00:56:44 +00:00
|
|
|
const std::string & remote_database_name_,
|
|
|
|
const std::string & remote_table_name_,
|
2020-05-14 21:51:07 +00:00
|
|
|
const Block & sample_block_,
|
2021-04-11 06:13:11 +00:00
|
|
|
ContextPtr local_context_,
|
2020-05-14 21:51:07 +00:00
|
|
|
IdentifierQuotingStyle quoting_)
|
2021-03-22 11:40:29 +00:00
|
|
|
: log(&Poco::Logger::get("ODBCBlockOutputStream"))
|
2021-06-07 18:09:16 +00:00
|
|
|
, connection_holder(std::move(connection_holder_))
|
2020-04-28 00:56:44 +00:00
|
|
|
, db_name(remote_database_name_)
|
|
|
|
, table_name(remote_table_name_)
|
|
|
|
, sample_block(sample_block_)
|
2021-04-11 06:13:11 +00:00
|
|
|
, local_context(local_context_)
|
2020-05-14 21:51:07 +00:00
|
|
|
, quoting(quoting_)
|
2020-04-28 00:56:44 +00:00
|
|
|
{
|
|
|
|
description.init(sample_block);
|
|
|
|
}
|
|
|
|
|
|
|
|
Block ODBCBlockOutputStream::getHeader() const
|
|
|
|
{
|
|
|
|
return sample_block;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ODBCBlockOutputStream::write(const Block & block)
|
|
|
|
{
|
2021-03-22 11:40:29 +00:00
|
|
|
WriteBufferFromOwnString values_buf;
|
2021-04-11 06:13:11 +00:00
|
|
|
auto writer = FormatFactory::instance().getOutputStream("Values", values_buf, sample_block, local_context);
|
2021-03-22 11:40:29 +00:00
|
|
|
writer->write(block);
|
2020-04-28 00:56:44 +00:00
|
|
|
|
2021-03-22 11:40:29 +00:00
|
|
|
std::string query = getInsertQuery(db_name, table_name, block.getColumnsWithTypeAndName(), quoting) + values_buf.str();
|
2021-06-07 18:09:16 +00:00
|
|
|
execute<void>(connection_holder,
|
|
|
|
[&](nanodbc::connection & connection) { execute(connection, query); });
|
2020-04-28 00:56:44 +00:00
|
|
|
}
|
|
|
|
|
2020-05-13 18:30:26 +00:00
|
|
|
}
|