diff --git a/README.md b/README.md index 1f75797c..43eea407 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,19 @@ C++ client for [ClickHouse](https://clickhouse.com/). * Int128 * UUID +## Requirement + +You need gcc cmake libabsl-dev to build this project. + +* gcc +* cmake +* libabsl-dev + +For ubuntu user, you can install them via command: +```bash +apt-get install -y cmake gcc libabsl-dev +``` + ## Building ```sh @@ -30,6 +43,12 @@ $ cd build $ cmake .. [-DBUILD_TESTS=ON] $ make ``` +if you want to install system lib directory, you may run +```bash +$ sudo make install +``` +Then the include and lib will install into system /usr/local/{lib,include} directory + ## Example @@ -75,3 +94,14 @@ client.Select("SELECT id, name FROM test.numbers", [] (const Block& block) /// Delete table. client.Execute("DROP TABLE test.numbers"); ``` + +If you want to build the example to run hello world, try to build with following command +``` +cd tests/helloworld +mkdir build +cd build +cmake .. +make +export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/lib" +./helloworld +``` \ No newline at end of file diff --git a/tests/helloworld/CMakeLists.txt b/tests/helloworld/CMakeLists.txt new file mode 100644 index 00000000..dc275c09 --- /dev/null +++ b/tests/helloworld/CMakeLists.txt @@ -0,0 +1,15 @@ +ADD_EXECUTABLE (helloworld + main.cpp +) + +TARGET_LINK_LIBRARIES (helloworld + clickhouse-cpp-lib +) + +IF (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") + # there is a problem with __builtin_mul_overflow call at link time + # the error looks like: ... undefined reference to `__muloti4' ... + # caused by clang bug https://bugs.llvm.org/show_bug.cgi?id=16404 + # explicit linking to compiler-rt allows to workaround the problem + set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --rtlib=compiler-rt") +ENDIF () diff --git a/tests/helloworld/main.cpp b/tests/helloworld/main.cpp new file mode 100644 index 00000000..eea6dcbe --- /dev/null +++ b/tests/helloworld/main.cpp @@ -0,0 +1,91 @@ +#include +#include +#include + +#include +#include +#include + +#if defined(_MSC_VER) +# pragma warning(disable : 4996) +#endif + +using namespace clickhouse; +using namespace std; + +std::string getEnvOrDefault(const std::string& env, const std::string& default_val) +{ + const char* v = std::getenv(env.c_str()); + return v ? v : default_val; +} + +inline void PrintBlock(const Block& block) { + for (Block::Iterator bi(block); bi.IsValid(); bi.Next()) { + std::cout << bi.Name() << " "; + } + std::cout << std::endl; + + for (size_t i = 0; i < block.GetRowCount(); ++i) { + std::cout << (*block[0]->As())[i] << " " + << (*block[1]->As())[i] << "\n"; + } +} + + +inline void GenericExample(Client& client) { + /// Create a table. + client.Execute("CREATE TEMPORARY TABLE IF NOT EXISTS test_client (id UInt64, name String)"); + + /// Insert some values. + { + Block block; + + auto id = std::make_shared(); + id->Append(1); + id->Append(7); + + auto name = std::make_shared(); + name->Append("one"); + name->Append("seven"); + + block.AppendColumn("id" , id); + block.AppendColumn("name", name); + + client.Insert("test_client", block); + } + + /// Select values inserted in the previous step. + client.Select("SELECT id, name FROM test_client", [](const Block& block) + { + PrintBlock(block); + } + ); + + /// Delete table. + client.Execute("DROP TEMPORARY TABLE test_client"); +} + + +static void RunTests(Client& client) { + GenericExample(client); +} + +int main() { + try { + { + Client client(ClientOptions() + .SetHost( getEnvOrDefault("CLICKHOUSE_HOST", "localhost")) + .SetPort( std::stoi(getEnvOrDefault("CLICKHOUSE_PORT", "9000"))) + .SetUser( getEnvOrDefault("CLICKHOUSE_USER", "default")) + .SetPassword( getEnvOrDefault("CLICKHOUSE_PASSWORD", "")) + .SetDefaultDatabase(getEnvOrDefault("CLICKHOUSE_DB", "default")) + .SetPingBeforeQuery(true)); + RunTests(client); + } + + } catch (const std::exception& e) { + std::cerr << "exception : " << e.what() << std::endl; + } + + return 0; +}