diff --git a/README.md b/README.md index 919b95f..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 @@ -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(.{}){}; diff --git a/build.zig b/build.zig index b8e748f..33d6161 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{ @@ -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, @@ -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); } 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", + }, +} 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/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; 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.