-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0xrinegade
committed
Oct 31, 2024
0 parents
commit 4b656e3
Showing
32 changed files
with
2,131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.zig-cache/ | ||
zig-out/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# clickhouse-zig | ||
clickhouse client for zig |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", .{}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", .{}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", .{}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", .{}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", .{}); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.