summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--query.zig12
-rw-r--r--sqlite.zig16
2 files changed, 11 insertions, 17 deletions
diff --git a/query.zig b/query.zig
index 99f8bcd..6663f52 100644
--- a/query.zig
+++ b/query.zig
@@ -22,12 +22,6 @@ pub const ParsedQuery = struct {
22 query_size: usize, 22 query_size: usize,
23 23
24 pub fn from(comptime query: []const u8) Self { 24 pub fn from(comptime query: []const u8) Self {
25 const State = enum {
26 Start,
27 BindMarker,
28 BindMarkerType,
29 };
30
31 comptime var buf: [query.len]u8 = undefined; 25 comptime var buf: [query.len]u8 = undefined;
32 comptime var pos = 0; 26 comptime var pos = 0;
33 comptime var state = .Start; 27 comptime var state = .Start;
@@ -38,7 +32,7 @@ pub const ParsedQuery = struct {
38 comptime var parsed_query: ParsedQuery = undefined; 32 comptime var parsed_query: ParsedQuery = undefined;
39 parsed_query.nb_bind_markers = 0; 33 parsed_query.nb_bind_markers = 0;
40 34
41 inline for (query) |c, i| { 35 inline for (query) |c| {
42 switch (state) { 36 switch (state) {
43 .Start => switch (c) { 37 .Start => switch (c) {
44 '?' => { 38 '?' => {
@@ -71,7 +65,7 @@ pub const ParsedQuery = struct {
71 '}' => { 65 '}' => {
72 state = .Start; 66 state = .Start;
73 67
74 const typ = parsed_query.parseType(current_bind_marker_type[0..current_bind_marker_type_pos]); 68 const typ = parseType(current_bind_marker_type[0..current_bind_marker_type_pos]);
75 69
76 parsed_query.bind_markers[parsed_query.nb_bind_markers] = BindMarker{ .Typed = typ }; 70 parsed_query.bind_markers[parsed_query.nb_bind_markers] = BindMarker{ .Typed = typ };
77 parsed_query.nb_bind_markers += 1; 71 parsed_query.nb_bind_markers += 1;
@@ -103,7 +97,7 @@ pub const ParsedQuery = struct {
103 return parsed_query; 97 return parsed_query;
104 } 98 }
105 99
106 fn parseType(comptime self: *Self, type_info: []const u8) type { 100 fn parseType(type_info: []const u8) type {
107 if (type_info.len <= 0) @compileError("invalid type info " ++ type_info); 101 if (type_info.len <= 0) @compileError("invalid type info " ++ type_info);
108 102
109 // Integer 103 // Integer
diff --git a/sqlite.zig b/sqlite.zig
index 143c279..3c2471d 100644
--- a/sqlite.zig
+++ b/sqlite.zig
@@ -243,6 +243,9 @@ pub const DetailedError = struct {
243 message: []const u8, 243 message: []const u8,
244 244
245 pub fn format(self: @This(), comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { 245 pub fn format(self: @This(), comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void {
246 _ = fmt;
247 _ = options;
248
246 _ = try writer.print("{{code: {}, message: {s}}}", .{ self.code, self.message }); 249 _ = try writer.print("{{code: {}, message: {s}}}", .{ self.code, self.message });
247 } 250 }
248}; 251};
@@ -443,7 +446,7 @@ pub const Db = struct {
443 } 446 }
444 447
445 /// prepareWithDiags is like `prepare` but takes an additional options argument. 448 /// prepareWithDiags is like `prepare` but takes an additional options argument.
446 pub fn prepareWithDiags(self: *Self, comptime query: []const u8, options: QueryOptions) !comptime blk: { 449 pub fn prepareWithDiags(self: *Self, comptime query: []const u8, options: QueryOptions) !blk: {
447 @setEvalBranchQuota(100000); 450 @setEvalBranchQuota(100000);
448 break :blk Statement(.{}, ParsedQuery.from(query)); 451 break :blk Statement(.{}, ParsedQuery.from(query));
449 } { 452 } {
@@ -466,7 +469,7 @@ pub const Db = struct {
466 /// This is done because we type check the bind parameters when executing the statement later. 469 /// This is done because we type check the bind parameters when executing the statement later.
467 /// 470 ///
468 /// If you want additional error information in case of failures, use `prepareWithDiags`. 471 /// If you want additional error information in case of failures, use `prepareWithDiags`.
469 pub fn prepare(self: *Self, comptime query: []const u8) !comptime blk: { 472 pub fn prepare(self: *Self, comptime query: []const u8) !blk: {
470 @setEvalBranchQuota(100000); 473 @setEvalBranchQuota(100000);
471 break :blk Statement(.{}, ParsedQuery.from(query)); 474 break :blk Statement(.{}, ParsedQuery.from(query));
472 } { 475 } {
@@ -732,12 +735,6 @@ pub fn Iterator(comptime Type: type) type {
732 // The options must contain an `allocator` field which will be used to create a copy of the data. 735 // The options must contain an `allocator` field which will be used to create a copy of the data.
733 fn readBytes(self: *Self, comptime BytesType: type, allocator: *mem.Allocator, _i: usize, comptime mode: ReadBytesMode) !BytesType { 736 fn readBytes(self: *Self, comptime BytesType: type, allocator: *mem.Allocator, _i: usize, comptime mode: ReadBytesMode) !BytesType {
734 const i = @intCast(c_int, _i); 737 const i = @intCast(c_int, _i);
735 const type_info = @typeInfo(BytesType);
736
737 var ret: BytesType = switch (BytesType) {
738 Text, Blob => .{ .data = "" },
739 else => try dupeWithSentinel(BytesType, allocator, ""),
740 };
741 738
742 switch (mode) { 739 switch (mode) {
743 .Blob => { 740 .Blob => {
@@ -918,6 +915,8 @@ pub const StatementOptions = struct {};
918/// Look at each function for more complete documentation. 915/// Look at each function for more complete documentation.
919/// 916///
920pub fn Statement(comptime opts: StatementOptions, comptime query: ParsedQuery) type { 917pub fn Statement(comptime opts: StatementOptions, comptime query: ParsedQuery) type {
918 _ = opts;
919
921 return struct { 920 return struct {
922 const Self = @This(); 921 const Self = @This();
923 922
@@ -1245,6 +1244,7 @@ fn addTestData(db: *Db) !void {
1245 1244
1246test "sqlite: db init" { 1245test "sqlite: db init" {
1247 var db = try getTestDb(); 1246 var db = try getTestDb();
1247 _ = db;
1248} 1248}
1249 1249
1250test "sqlite: db pragma" { 1250test "sqlite: db pragma" {