ClickHouse/dbms/include/DB/Dictionaries/ExternalQueryBuilder.h

80 lines
2.0 KiB
C++
Raw Normal View History

2016-04-10 02:47:29 +00:00
#pragma once
2016-12-08 02:49:04 +00:00
#include <string>
#include <DB/Columns/IColumn.h>
2016-04-10 02:47:29 +00:00
namespace DB
{
2016-12-12 06:02:35 +00:00
struct DictionaryStructure;
2016-12-08 02:49:04 +00:00
class WriteBuffer;
2016-04-10 02:47:29 +00:00
/** Builds a query to load data from external database.
2016-04-10 02:47:29 +00:00
*/
struct ExternalQueryBuilder
{
const DictionaryStructure & dict_struct;
const std::string & db;
const std::string & table;
const std::string & where;
/// Method to quote identifiers.
/// NOTE There could be differences in escaping rules inside quotes. Escaping rules may not match that required by specific external DBMS.
enum QuotingStyle
{
None, /// Write as-is, without quotes.
Backticks, /// `mysql` style
DoubleQuotes /// "postgres" style
};
QuotingStyle quoting_style;
2016-04-10 02:47:29 +00:00
ExternalQueryBuilder(
const DictionaryStructure & dict_struct,
const std::string & db,
const std::string & table,
const std::string & where,
QuotingStyle quoting_style);
2016-04-10 02:47:29 +00:00
/** Generate a query to load all data. */
2016-12-08 02:49:04 +00:00
std::string composeLoadAllQuery() const;
2016-04-10 02:47:29 +00:00
/** Generate a query to load data by set of UInt64 keys. */
2016-12-08 02:49:04 +00:00
std::string composeLoadIdsQuery(const std::vector<UInt64> & ids);
/** Generate a query to load data by set of composite keys.
* There are two methods of specification of composite keys in WHERE:
* 1. (x = c11 AND y = c12) OR (x = c21 AND y = c22) ...
* 2. (x, y) IN ((c11, c12), (c21, c22), ...)
*/
enum LoadKeysMethod
{
AND_OR_CHAIN,
IN_WITH_TUPLES,
};
2016-04-10 02:47:29 +00:00
std::string composeLoadKeysQuery(
const ConstColumnPlainPtrs & key_columns,
2016-12-08 02:49:04 +00:00
const std::vector<size_t> & requested_rows,
LoadKeysMethod method);
2016-04-10 02:47:29 +00:00
2016-04-10 02:47:29 +00:00
private:
/// Expression in form (x = c1 AND y = c2 ...)
2016-12-08 02:49:04 +00:00
void composeKeyCondition(const ConstColumnPlainPtrs & key_columns, const size_t row, WriteBuffer & out) const;
/// Expression in form (x, y, ...)
2016-12-08 02:49:04 +00:00
std::string composeKeyTupleDefinition() const;
/// Expression in form (c1, c2, ...)
2016-12-08 02:49:04 +00:00
void composeKeyTuple(const ConstColumnPlainPtrs & key_columns, const size_t row, WriteBuffer & out) const;
/// Write string with specified quoting style.
void writeQuoted(const std::string & s, WriteBuffer & out) const;
2016-04-10 02:47:29 +00:00
};
}