client.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #pragma once
  2. #include "query.h"
  3. #include "exceptions.h"
  4. #include "columns/array.h"
  5. #include "columns/date.h"
  6. #include "columns/nullable.h"
  7. #include "columns/numeric.h"
  8. #include "columns/string.h"
  9. #include "columns/tuple.h"
  10. #include <library/cpp/openssl/io/stream.h>
  11. #include <util/generic/string.h>
  12. namespace NClickHouse {
  13. /// Метод сжатия
  14. enum class ECompressionMethod {
  15. None = -1,
  16. LZ4 = 1,
  17. };
  18. struct TClientOptions {
  19. #define DECLARE_FIELD(name, type, default) \
  20. type name{default}; \
  21. inline TClientOptions& Set##name(const type& value) { \
  22. name = value; \
  23. return *this; \
  24. }
  25. /// Hostname of the server.
  26. DECLARE_FIELD(Host, TString, TString());
  27. /// Service port.
  28. DECLARE_FIELD(Port, int, 9000);
  29. /// Default database.
  30. DECLARE_FIELD(DefaultDatabase, TString, "default");
  31. /// User name.
  32. DECLARE_FIELD(User, TString, "default");
  33. /// Access password.
  34. DECLARE_FIELD(Password, TString, TString());
  35. /// By default all exceptions received during query execution will be
  36. /// passed to OnException handler. Set rethrow_exceptions to true to
  37. /// enable throwing exceptions with standard c++ exception mechanism.
  38. DECLARE_FIELD(RethrowExceptions, bool, true);
  39. /// Ping server every time before execute any query.
  40. DECLARE_FIELD(PingBeforeQuery, bool, false);
  41. /// Count of retry to send request to server.
  42. DECLARE_FIELD(SendRetries, int, 1);
  43. /// Amount of time to wait before next retry.
  44. DECLARE_FIELD(RetryTimeout, TDuration, TDuration::Seconds(5));
  45. /// Define timeout for establishing a connection to server.
  46. DECLARE_FIELD(ConnectTimeout, TDuration, TDuration::Seconds(5));
  47. /// Define timeout for any operations.
  48. DECLARE_FIELD(RequestTimeout, TDuration, TDuration::Zero());
  49. /// Compression method.
  50. DECLARE_FIELD(CompressionMethod, ECompressionMethod, ECompressionMethod::None);
  51. /// Use SSL encryption
  52. DECLARE_FIELD(UseSsl, bool, false);
  53. /// SSL Options
  54. DECLARE_FIELD(SslOptions, TOpenSslClientIO::TOptions, TOpenSslClientIO::TOptions());
  55. #undef DECLARE_FIELD
  56. };
  57. /**
  58. *
  59. */
  60. class TClient {
  61. public:
  62. TClient(const TClientOptions& opts);
  63. ~TClient();
  64. /// Intends for execute arbitrary queries.
  65. void Execute(const TQuery& query);
  66. /// Intends for execute select queries. Data will be returned with
  67. /// one or more call of \p cb.
  68. void Select(const TString& query, TSelectCallback cb, const TString& query_id = "");
  69. /// Alias for Execute.
  70. void Select(const TQuery& query);
  71. /// Intends for insert block of data into a table \p table_name.
  72. void Insert(const TString& table_name, const TBlock& block, const TString& query_id = "", const TString& deduplication_token = "");
  73. /// Ping server for aliveness.
  74. void Ping();
  75. /// Reset connection with initial params.
  76. void ResetConnection();
  77. private:
  78. TClientOptions Options_;
  79. class TImpl;
  80. THolder<TImpl> Impl_;
  81. };
  82. }