From 38fe6a827946c50d216a6ba856e403c44c962677 Mon Sep 17 00:00:00 2001 From: Alex Kwiatkowski Date: Thu, 8 Feb 2024 13:52:53 -0800 Subject: [PATCH 1/7] use const in zig build --- build.zig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build.zig b/build.zig index b8e748f..987fa92 100644 --- a/build.zig +++ b/build.zig @@ -24,7 +24,7 @@ pub fn build(b: *Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); - var lib = b.addStaticLibrary(.{ + const lib = b.addStaticLibrary(.{ .name = "odbc", .target = target, .optimize = optimize, @@ -60,7 +60,7 @@ pub fn setupOdbcDependencies(step: *std.build.Step.Compile) void { 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; 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, From 04fb00c85f3e08d32a00773adf141471824ffc13 Mon Sep 17 00:00:00 2001 From: Alex Kwiatkowski Date: Thu, 8 Feb 2024 14:12:07 -0800 Subject: [PATCH 2/7] update build.zig for 0.12.0 --- build.zig | 38 ++++++++++++++++---------------------- src/types.zig | 2 +- 2 files changed, 17 insertions(+), 23 deletions(-) diff --git a/build.zig b/build.zig index 987fa92..b5c6cc3 100644 --- a/build.zig +++ b/build.zig @@ -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{ @@ -34,18 +34,30 @@ pub fn build(b: *Build) void { b.installArtifact(lib); _ = b.addModule("zig-odbc", .{ - .source_file = .{ .path = "src/lib.zig" }, + .root_source_file = .{ .path = "src/lib.zig" }, }); const test_cmd = b.step("test", "Run library tests"); - const tests = testStep(b, optimize, target); + var tests: [test_files.len]*std.Build.Step.Compile = undefined; + inline for (test_files, 0..) |item, index| { + const current_tests = b.addTest(.{ + .name = item.name, + .root_source_file = item.source_file, + .optimize = optimize, + .target = target, + }); + + setupOdbcDependencies(current_tests); + + tests[index] = current_tests; + } for (tests) |t| { test_cmd.dependOn(&t.step); } } -pub fn setupOdbcDependencies(step: *std.build.Step.Compile) void { +pub fn setupOdbcDependencies(step: *std.Build.Step.Compile) void { step.linkLibC(); const odbc_library_name = if (builtin.os.tag == .windows) "odbc32" else "odbc"; @@ -56,21 +68,3 @@ pub fn setupOdbcDependencies(step: *std.build.Step.Compile) void { step.linkSystemLibrary(odbc_library_name); } - -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; - inline for (test_files, 0..) |item, index| { - const current_tests = b.addTest(.{ - .name = item.name, - .root_source_file = item.source_file, - .optimize = optimize, - .target = target, - }); - - setupOdbcDependencies(current_tests); - - tests[index] = current_tests; - } - - return tests; -} diff --git a/src/types.zig b/src/types.zig index f1e61da..dd3346b 100644 --- a/src/types.zig +++ b/src/types.zig @@ -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; From dc64496e82199a5a7b8ea88b1b7c6f58bf7b0abe Mon Sep 17 00:00:00 2001 From: Alex Kwiatkowski Date: Thu, 8 Feb 2024 23:45:41 -0800 Subject: [PATCH 3/7] fix enum error test assertions --- build.zig | 18 +++++++++++------- src/statement.zig | 4 ++-- src/util.zig | 5 +---- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/build.zig b/build.zig index b5c6cc3..9b05074 100644 --- a/build.zig +++ b/build.zig @@ -24,22 +24,25 @@ pub fn build(b: *Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); - const lib = b.addStaticLibrary(.{ - .name = "odbc", + 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", .{ .root_source_file = .{ .path = "src/lib.zig" }, }); - const test_cmd = b.step("test", "Run library tests"); + const test_step = b.step("test", "Run library tests"); - 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| { const current_tests = b.addTest(.{ .name = item.name, @@ -50,10 +53,12 @@ pub fn build(b: *Build) void { 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_cmd.dependOn(&t.step); + test_step.dependOn(&t.step); } } @@ -65,6 +70,5 @@ pub fn setupOdbcDependencies(step: *std.Build.Step.Compile) void { step.addIncludeDir("/usr/local/include"); step.addIncludeDir("/usr/local/lib"); } - step.linkSystemLibrary(odbc_library_name); } diff --git a/src/statement.zig b/src/statement.zig index bd17820..47693f6 100644 --- a/src/statement.zig +++ b/src/statement.zig @@ -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); @@ -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) { diff --git a/src/util.zig b/src/util.zig index 8dc4f8c..9f74d66 100644 --- a/src/util.zig +++ b/src/util.zig @@ -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. From 5e2d2aa531678b045ac518f8481607818f2bca34 Mon Sep 17 00:00:00 2001 From: Alex Kwiatkowski Date: Mon, 12 Feb 2024 14:04:48 -1000 Subject: [PATCH 4/7] replace zig build addIncludeDir with addIncludePath --- build.zig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build.zig b/build.zig index 9b05074..622c602 100644 --- a/build.zig +++ b/build.zig @@ -67,8 +67,8 @@ pub fn setupOdbcDependencies(step: *std.Build.Step.Compile) void { 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"); + step.addIncludePath(.{ .path = "/usr/local/include" }); + step.addIncludePath(.{ .path = "/usr/local/lib" }); } step.linkSystemLibrary(odbc_library_name); } From 3f9dff9ad2dc59cb4c8b34c170a7386e2f27e5b8 Mon Sep 17 00:00:00 2001 From: Alex Kwiatkowski Date: Wed, 14 Feb 2024 20:42:17 -1000 Subject: [PATCH 5/7] add build.zig.zon --- build.zig.zon | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 build.zig.zon diff --git a/build.zig.zon b/build.zig.zon new file mode 100644 index 0000000..e0054eb --- /dev/null +++ b/build.zig.zon @@ -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", + }, +} From 65a57b4932918ab9c06d6714bad420a68fb4854e Mon Sep 17 00:00:00 2001 From: Alex Kwiatkowski Date: Wed, 14 Feb 2024 20:49:14 -1000 Subject: [PATCH 6/7] update @import in README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 919b95f..04b4109 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,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(.{}){}; From 8ad15587298bf97691aed4f2e8aaec8e2c8344ef Mon Sep 17 00:00:00 2001 From: Alex Kwiatkowski Date: Wed, 14 Feb 2024 21:24:24 -1000 Subject: [PATCH 7/7] add zig-odbc module before shared lib --- README.md | 10 +++++++++- build.zig | 8 ++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 04b4109..cb25e92 100644 --- a/README.md +++ b/README.md @@ -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/.tar.gz", .hash = "", } - } + }, + .paths = .{ + "build.zig", + "build.zig.zon", + "src", + "LICENSE", + "README.md", + }, } // build.zig diff --git a/build.zig b/build.zig index 622c602..33d6161 100644 --- a/build.zig +++ b/build.zig @@ -24,6 +24,10 @@ pub fn build(b: *Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); + _ = b.addModule("zig-odbc", .{ + .root_source_file = .{ .path = "src/lib.zig" }, + }); + const lib = b.addSharedLibrary(.{ .name = "zigodbc", .root_source_file = .{ .path = "src/lib.zig" }, @@ -36,10 +40,6 @@ pub fn build(b: *Build) void { b.installArtifact(lib); - _ = b.addModule("zig-odbc", .{ - .root_source_file = .{ .path = "src/lib.zig" }, - }); - const test_step = b.step("test", "Run library tests"); var tests: [test_files.len]*std.Build.Step.Run = undefined;