Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
0xrinegade committed Oct 31, 2024
0 parents commit 4b656e3
Show file tree
Hide file tree
Showing 32 changed files with 2,131 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.zig-cache/
zig-out/
24 changes: 24 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <https://unlicense.org>
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# clickhouse-zig
clickhouse client for zig
94 changes: 94 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
const std = @import("std");

pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

// Library
const lib = b.addStaticLibrary(.{
.name = "clickhouse-zig",
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});

// Add LZ4 and ZSTD as dependencies
const lz4_lib = b.addStaticLibrary(.{
.name = "lz4",
.target = target,
.optimize = optimize,
});
lz4_lib.addCSourceFile("deps/lz4/lib/lz4.c", &[_][]const u8{"-std=c99"});
lz4_lib.addIncludePath("deps/lz4/lib");
lib.linkLibrary(lz4_lib);

const zstd_lib = b.addStaticLibrary(.{
.name = "zstd",
.target = target,
.optimize = optimize,
});
zstd_lib.addCSourceFiles(&.{
"deps/zstd/lib/common/entropy_common.c",
"deps/zstd/lib/common/error_private.c",
"deps/zstd/lib/common/fse_decompress.c",
"deps/zstd/lib/common/pool.c",
"deps/zstd/lib/common/threading.c",
"deps/zstd/lib/common/xxhash.c",
"deps/zstd/lib/common/zstd_common.c",
"deps/zstd/lib/compress/fse_compress.c",
"deps/zstd/lib/compress/hist.c",
"deps/zstd/lib/compress/huf_compress.c",
"deps/zstd/lib/compress/zstd_compress.c",
"deps/zstd/lib/compress/zstd_double_fast.c",
"deps/zstd/lib/compress/zstd_fast.c",
"deps/zstd/lib/compress/zstd_lazy.c",
"deps/zstd/lib/compress/zstd_ldm.c",
"deps/zstd/lib/compress/zstd_opt.c",
"deps/zstd/lib/decompress/huf_decompress.c",
"deps/zstd/lib/decompress/zstd_decompress.c",
}, &[_][]const u8{"-std=c99"});
zstd_lib.addIncludePath("deps/zstd/lib");
lib.linkLibrary(zstd_lib);

b.installArtifact(lib);

// Tests
const main_tests = b.addTest(.{
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
main_tests.linkLibrary(lz4_lib);
main_tests.linkLibrary(zstd_lib);

const run_main_tests = b.addRunArtifact(main_tests);

const test_step = b.step("test", "Run library tests");
test_step.dependOn(&run_main_tests.step);

// Examples
const examples = .{
"basic_connection",
"show_databases",
"database_size",
"show_connections",
};

inline for (examples) |example_name| {
const example = b.addExecutable(.{
.name = example_name,
.root_source_file = .{ .path = "examples/" ++ example_name ++ ".zig" },
.target = target,
.optimize = optimize,
});
example.addModule("clickhouse", lib.getModule());
example.linkLibrary(lz4_lib);
example.linkLibrary(zstd_lib);

const run_cmd = b.addRunArtifact(example);
const run_step = b.step("run-" ++ example_name, "Run the " ++ example_name ++ " example");
run_step.dependOn(&run_cmd.step);

b.installArtifact(example);
}
}
23 changes: 23 additions & 0 deletions examples/basic_connection.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const std = @import("std");
const ClickHouseClient = @import("../src/main.zig").ClickHouseClient;
const ClickHouseConfig = @import("../src/main.zig").ClickHouseConfig;

pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();

const config = ClickHouseConfig{
.host = "localhost",
.port = 9000,
.username = "default",
.password = "",
.database = "default",
};

var client = ClickHouseClient.init(allocator, config);
defer client.deinit();

try client.connect();
std.debug.print("Successfully connected to ClickHouse server!\n", .{});
}
37 changes: 37 additions & 0 deletions examples/database_size.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const std = @import("std");
const ClickHouseClient = @import("../src/main.zig").ClickHouseClient;
const ClickHouseConfig = @import("../src/main.zig").ClickHouseConfig;

pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();

const config = ClickHouseConfig{
.host = "localhost",
.port = 9000,
.username = "default",
.password = "",
.database = "default",
};

var client = ClickHouseClient.init(allocator, config);
defer client.deinit();

try client.connect();

// Query to get database sizes in GB
const size_query =
\\SELECT
\\ database,
\\ formatReadableSize(sum(bytes)) AS size,
\\ round(sum(bytes) / pow(1024, 3), 2) AS size_gb
\\FROM system.parts
\\GROUP BY database
\\ORDER BY sum(bytes) DESC
;

try client.query(size_query);

std.debug.print("Query executed: Database sizes retrieved\n", .{});
}
36 changes: 36 additions & 0 deletions examples/pool_example.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const std = @import("std");
const Pool = @import("../src/pool.zig").Pool;
const PoolConfig = @import("../src/pool.zig").PoolConfig;
const ClickHouseConfig = @import("../src/main.zig").ClickHouseConfig;

pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();

const config = ClickHouseConfig{
.host = "localhost",
.port = 9000,
.username = "default",
.password = "",
.database = "default",
};

const pool_config = PoolConfig{
.min_connections = 2,
.max_connections = 5,
.connection_timeout_ms = 5000,
};

var pool = try Pool.init(allocator, config, pool_config);
defer pool.deinit();

// Acquire a connection
var client = try pool.acquire();
try client.query("SELECT 1");

// Release the connection back to the pool
pool.release(client);

std.debug.print("Pool example completed successfully\n", .{});
}
49 changes: 49 additions & 0 deletions examples/query_cancel.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const std = @import("std");
const ClickHouseClient = @import("../src/main.zig").ClickHouseClient;
const ClickHouseConfig = @import("../src/main.zig").ClickHouseConfig;
const QueryContext = @import("../src/query.zig").QueryContext;

pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();

const config = ClickHouseConfig{
.host = "localhost",
.port = 9000,
.username = "default",
.password = "",
.database = "default",
};

var client = ClickHouseClient.init(allocator, config);
defer client.deinit();

try client.connect();

// Create query context
var ctx = try QueryContext.init(allocator);
defer ctx.deinit();

// Start a long-running query
const query = "SELECT sleep(10)";

// Spawn a thread to cancel the query after 2 seconds
const thread = try std.Thread.spawn(.{}, struct {
fn run(token: *QueryContext) void {
std.time.sleep(2 * std.time.ns_per_s);
token.cancel_token.cancel();
}
}.run, .{ctx});

// Execute query with cancellation context
client.queryWithContext(query, ctx) catch |err| {
if (err == error.QueryCancelled) {
std.debug.print("Query was cancelled as expected\n", .{});
} else {
return err;
}
};

thread.join();
}
39 changes: 39 additions & 0 deletions examples/show_connections.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const std = @import("std");
const ClickHouseClient = @import("../src/main.zig").ClickHouseClient;
const ClickHouseConfig = @import("../src/main.zig").ClickHouseConfig;

pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();

const config = ClickHouseConfig{
.host = "localhost",
.port = 9000,
.username = "default",
.password = "",
.database = "default",
};

var client = ClickHouseClient.init(allocator, config);
defer client.deinit();

try client.connect();

// Query to show current connections
const connections_query =
\\SELECT
\\ user,
\\ address,
\\ client_name,
\\ client_version,
\\ query_duration_ms,
\\ query
\\FROM system.processes
\\ORDER BY query_duration_ms DESC
;

try client.query(connections_query);

std.debug.print("Query executed: Current connections retrieved\n", .{});
}
25 changes: 25 additions & 0 deletions examples/show_databases.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const std = @import("std");
const ClickHouseClient = @import("../src/main.zig").ClickHouseClient;
const ClickHouseConfig = @import("../src/main.zig").ClickHouseConfig;

pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();

const config = ClickHouseConfig{
.host = "localhost",
.port = 9000,
.username = "default",
.password = "",
.database = "default",
};

var client = ClickHouseClient.init(allocator, config);
defer client.deinit();

try client.connect();
try client.query("SHOW DATABASES");

std.debug.print("Query executed: SHOW DATABASES\n", .{});
}
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4b656e3

Please sign in to comment.