Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zig 0.12.0 support #15

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,20 @@ You can use the following configuration as a starting point when adding `zig-odb
.{
.name = "",
.version = "",
.minimum_zig_version = "0.12.0",
.dependencies = .{
.zig_odbc = .{
.url = "https://github.com/mjoerussell/zig-odbc/<sha>.tar.gz",
.hash = "<hash>",
}
}
},
.paths = .{
"build.zig",
"build.zig.zon",
"src",
"LICENSE",
"README.md",
},
}

// build.zig
Expand Down Expand Up @@ -53,7 +61,7 @@ This example connects to a database, sets and gets some attributes, and creates

```zig
const std = @import("std");
const odbc = @import("odbc");
const odbc = @import("zig-odbc");

pub fn main() anyerror!void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
Expand Down
60 changes: 29 additions & 31 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const Build = std.Build;

const TestItem = struct {
name: []const u8,
source_file: std.build.FileSource,
source_file: std.Build.LazyPath,
};

const test_files = [_]TestItem{
Expand All @@ -24,43 +24,27 @@ pub fn build(b: *Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

var lib = b.addStaticLibrary(.{
.name = "odbc",
_ = b.addModule("zig-odbc", .{
.root_source_file = .{ .path = "src/lib.zig" },
});

const lib = b.addSharedLibrary(.{
.name = "zigodbc",
.root_source_file = .{ .path = "src/lib.zig" },
.version = .{ .major = 0, .minor = 0, .patch = 0 },
.target = target,
.optimize = optimize,
});

setupOdbcDependencies(lib);
b.installArtifact(lib);

_ = b.addModule("zig-odbc", .{
.source_file = .{ .path = "src/lib.zig" },
});

const test_cmd = b.step("test", "Run library tests");

const tests = testStep(b, optimize, target);
for (tests) |t| {
test_cmd.dependOn(&t.step);
}
}

pub fn setupOdbcDependencies(step: *std.build.Step.Compile) void {
step.linkLibC();

const odbc_library_name = if (builtin.os.tag == .windows) "odbc32" else "odbc";
if (builtin.os.tag == .macos) {
step.addIncludeDir("/usr/local/include");
step.addIncludeDir("/usr/local/lib");
}
b.installArtifact(lib);

step.linkSystemLibrary(odbc_library_name);
}
const test_step = b.step("test", "Run library tests");

pub fn testStep(b: *Build, optimize: std.builtin.OptimizeMode, target: std.zig.CrossTarget) [test_files.len]*std.build.Step.Compile {
var tests: [test_files.len]*std.build.Step.Compile = undefined;
var tests: [test_files.len]*std.Build.Step.Run = undefined;
inline for (test_files, 0..) |item, index| {
var current_tests = b.addTest(.{
const current_tests = b.addTest(.{
.name = item.name,
.root_source_file = item.source_file,
.optimize = optimize,
Expand All @@ -69,8 +53,22 @@ pub fn testStep(b: *Build, optimize: std.builtin.OptimizeMode, target: std.zig.C

setupOdbcDependencies(current_tests);

tests[index] = current_tests;
const run_current_unit_tests = b.addRunArtifact(current_tests);

tests[index] = run_current_unit_tests;
}
for (tests) |t| {
test_step.dependOn(&t.step);
}
}

pub fn setupOdbcDependencies(step: *std.Build.Step.Compile) void {
step.linkLibC();

return tests;
const odbc_library_name = if (builtin.os.tag == .windows) "odbc32" else "odbc";
if (builtin.os.tag == .macos) {
step.addIncludePath(.{ .path = "/usr/local/include" });
step.addIncludePath(.{ .path = "/usr/local/lib" });
}
step.linkSystemLibrary(odbc_library_name);
}
18 changes: 18 additions & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.{
.name = "zig-odbc",
.version = "0.1.0",

// This is currently advisory only; Zig does not yet do anything
// with this value.
.minimum_zig_version = "0.12.0",

.dependencies = .{},

.paths = .{
"build.zig",
"build.zig.zon",
"src",
"LICENSE",
"README.md",
},
}
4 changes: 2 additions & 2 deletions src/statement.zig
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ pub const Statement = struct {
var name_length: c_short = 0;
_ = c.SQLGetCursorName(self.handle, null, 0, &name_length);

var name_buffer = try allocator.allocSentinel(u8, name_length, 0);
const name_buffer = try allocator.allocSentinel(u8, name_length, 0);
errdefer allocator.free(name_buffer);

const result = c.SQLGetCursorName(self.handle, name_buffer.ptr, @as(c_short, @intCast(name_buffer.len)), &name_length);
Expand Down Expand Up @@ -346,7 +346,7 @@ pub const Statement = struct {
if (result_type == .success_with_info) {
// SuccessWithInfo might indicate that only part of the column was retrieved, and in that case we need to
// continue fetching the rest of it. If we're getting long data, SQLGetData will return NoData
var error_buffer: [@sizeOf(odbc_error.SqlState) * 3]u8 = undefined;
const error_buffer: [@sizeOf(odbc_error.SqlState) * 3]u8 = undefined;
var fba = std.heap.FixedBufferAllocator.init(error_buffer);
const errors = try self.getErrors(&fba.allocator);
for (errors) |err| if (err == .StringRightTrunc) {
Expand Down
2 changes: 1 addition & 1 deletion src/types.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1604,7 +1604,7 @@ pub const StatementAttributeValue = union(StatementAttribute) {
.RowStatusPointer => |v| std.mem.sliceAsBytes(v)[0..],
};

var result_buffer = try allocator.alloc(u8, bytes.len);
const result_buffer = try allocator.alloc(u8, bytes.len);
std.mem.copy(u8, result_buffer, bytes);

return result_buffer;
Expand Down
5 changes: 1 addition & 4 deletions src/util.zig
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,9 @@ test "bitmask" {

test "enum error" {
const Base = enum { A, B, C };

const BaseError = EnumErrorSet(Base);

try std.testing.expectEqualStrings("A", @typeInfo(BaseError).ErrorSet.?[0].name);
try std.testing.expectEqualStrings("B", @typeInfo(BaseError).ErrorSet.?[1].name);
try std.testing.expectEqualStrings("C", @typeInfo(BaseError).ErrorSet.?[2].name);
try std.testing.expectEqual(BaseError, error{ A, B, C });

// Just making sure everything compiles, that BaseError is accepted in the error
// spot of the return type.
Expand Down
Loading