diff --git a/docs/_assets/icon/zig-logo.png b/docs/_assets/icon/zig-logo.png new file mode 100644 index 00000000..1c310a2a Binary files /dev/null and b/docs/_assets/icon/zig-logo.png differ diff --git a/docs/connect/index.md b/docs/connect/index.md index ffe0cae1..7056d8ad 100644 --- a/docs/connect/index.md +++ b/docs/connect/index.md @@ -113,6 +113,20 @@ CrateDB drivers and adapters for supported programming languages, frameworks, an :::: +::::{grid-item-card} Zig +:link: connect-zig +:link-type: ref +:link-alt: Connect to CrateDB using Zig +:padding: 3 +:text-align: center +:class-card: sd-pt-3 +:class-body: sd-fs-1 +:class-title: sd-fs-6 +```{image} /_assets/icon/zig-logo.png +:height: 50px +``` +:::: + ::::: @@ -187,6 +201,7 @@ javascript php python ruby +zig/index natural All drivers ``` diff --git a/docs/connect/zig/index.md b/docs/connect/zig/index.md new file mode 100644 index 00000000..3c8b7028 --- /dev/null +++ b/docs/connect/zig/index.md @@ -0,0 +1,90 @@ +(connect-zig)= + +# Zig + +:::{div} sd-text-muted +Connect to CrateDB from Zig applications. +::: + +:::{rubric} About +::: + +[pg.zig] is a native PostgreSQL driver / client for Zig. + +:::{rubric} Synopsis +::: + +`build.zig` +```zig +const std = @import("std"); + +pub fn build(b: *std.Build) void { + + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + + const exe = b.addExecutable(.{ + .name = "example", + .root_module = b.createModule(.{ + .root_source_file = b.path("example.zig"), + .target = b.graph.host, + }) + }); + + const pg = b.dependency("pg", .{ + .target = target, + .optimize = optimize, + }); + exe.root_module.addImport("pg", pg.module("pg")); + + b.installArtifact(exe); +} +``` +`example.zig` +```zig +const std = @import("std"); +const builtin = @import("builtin"); + +const pg = @import("pg"); + +pub fn main() !void { + + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + const allocator = if (builtin.mode == .Debug) gpa.allocator() else std.heap.c_allocator; + + const uri = try std.Uri.parse("postgresql://crate:crate@localhost/doc?sslmode=disable"); + var pool = try pg.Pool.initUri(allocator, uri, .{.size=5, .timeout=5_000}); + defer pool.deinit(); + + var result = try pool.query("SELECT mountain, height FROM sys.summits ORDER BY height DESC LIMIT 3", .{}); + defer result.deinit(); + + while (try result.next()) |row| { + const mountain = row.get([]u8, 0); + const height = row.get(i32, 1); + std.debug.print("{s}: {d}\n", .{mountain, height}); + } + +} +``` + +:::{include} ../_cratedb.md +::: +```shell +zig fetch --save git+https://github.com/karlseguin/pg.zig#master +zig build +./zig-out/bin/example +``` + +:::{rubric} CrateDB Cloud +::: + +For connecting to CrateDB Cloud, use the `sslmode=require` parameter, +and replace username, password, and hostname with values matching your +environment. +```zig +const uri = try std.Uri.parse("postgresql://admin:password@testcluster.cratedb.net:5432/doc?sslmode=require"); +``` + + +[pg.zig]: https://github.com/karlseguin/pg.zig