diff options
| author | 2021-08-23 16:02:57 -0700 | |
|---|---|---|
| committer | 2021-08-23 16:02:57 -0700 | |
| commit | 6261f9b923fae73f5309611da991291313bb70bc (patch) | |
| tree | 5cd5c5d4c36aff26471f4d40b27bc0bd61aa8f12 /sqlite.zig | |
| parent | add zigmod to gitignore (diff) | |
| download | zig-sqlite-6261f9b923fae73f5309611da991291313bb70bc.tar.gz zig-sqlite-6261f9b923fae73f5309611da991291313bb70bc.tar.xz zig-sqlite-6261f9b923fae73f5309611da991291313bb70bc.zip | |
add support for enum fields and lay groundwork for #39
Diffstat (limited to 'sqlite.zig')
| -rw-r--r-- | sqlite.zig | 22 |
1 files changed, 21 insertions, 1 deletions
| @@ -906,6 +906,17 @@ pub fn Iterator(comptime Type: type) type { | |||
| 906 | .Array => try self.readArray(FieldType, i), | 906 | .Array => try self.readArray(FieldType, i), |
| 907 | .Pointer => try self.readPointer(FieldType, options.allocator, i), | 907 | .Pointer => try self.readPointer(FieldType, options.allocator, i), |
| 908 | .Optional => try self.readOptional(FieldType, options, i), | 908 | .Optional => try self.readOptional(FieldType, options, i), |
| 909 | .Enum => |TI| { | ||
| 910 | const innervalue = try self.readField(FieldType.BaseType, i, options); | ||
| 911 | |||
| 912 | if (comptime std.meta.trait.isZigString(FieldType.BaseType)) { | ||
| 913 | return std.meta.stringToEnum(FieldType, innervalue) orelse @intToEnum(FieldType, 0); | ||
| 914 | } | ||
| 915 | if (@typeInfo(FieldType.BaseType) == .Int) { | ||
| 916 | return @intToEnum(FieldType, @intCast(TI.tag_type, innervalue)); | ||
| 917 | } | ||
| 918 | @compileError("enum column " ++ @typeName(FieldType) ++ " must have a BaseType of either string or int"); | ||
| 919 | }, | ||
| 909 | else => @compileError("cannot populate field of type " ++ @typeName(FieldType)), | 920 | else => @compileError("cannot populate field of type " ++ @typeName(FieldType)), |
| 910 | }, | 921 | }, |
| 911 | }; | 922 | }; |
| @@ -1039,7 +1050,7 @@ pub fn Statement(comptime opts: StatementOptions, comptime query: ParsedQuery) t | |||
| 1039 | inline for (StructTypeInfo.fields) |struct_field, _i| { | 1050 | inline for (StructTypeInfo.fields) |struct_field, _i| { |
| 1040 | const bind_marker = query.bind_markers[_i]; | 1051 | const bind_marker = query.bind_markers[_i]; |
| 1041 | switch (bind_marker) { | 1052 | switch (bind_marker) { |
| 1042 | .Typed => |typ| if (struct_field.field_type != typ) { | 1053 | .Typed => |typ| if (struct_field.field_type != typ and struct_field.field_type.BaseType != typ) { |
| 1043 | @compileError("value type " ++ @typeName(struct_field.field_type) ++ " is not the bind marker type " ++ @typeName(typ)); | 1054 | @compileError("value type " ++ @typeName(struct_field.field_type) ++ " is not the bind marker type " ++ @typeName(typ)); |
| 1044 | }, | 1055 | }, |
| 1045 | .Untyped => {}, | 1056 | .Untyped => {}, |
| @@ -1089,6 +1100,15 @@ pub fn Statement(comptime opts: StatementOptions, comptime query: ParsedQuery) t | |||
| 1089 | _ = c.sqlite3_bind_null(self.stmt, column); | 1100 | _ = c.sqlite3_bind_null(self.stmt, column); |
| 1090 | }, | 1101 | }, |
| 1091 | .Null => _ = c.sqlite3_bind_null(self.stmt, column), | 1102 | .Null => _ = c.sqlite3_bind_null(self.stmt, column), |
| 1103 | .Enum => { | ||
| 1104 | if (comptime std.meta.trait.isZigString(FieldType.BaseType)) { | ||
| 1105 | return self.bindField(FieldType.BaseType, field_name, column, @tagName(field)); | ||
| 1106 | } | ||
| 1107 | if (@typeInfo(FieldType.BaseType) == .Int) { | ||
| 1108 | return self.bindField(FieldType.BaseType, field_name, column, @enumToInt(field)); | ||
| 1109 | } | ||
| 1110 | @compileError("enum column " ++ @typeName(FieldType) ++ " must have a BaseType of either string or int to bind"); | ||
| 1111 | }, | ||
| 1092 | else => @compileError("cannot bind field " ++ field_name ++ " of type " ++ @typeName(FieldType)), | 1112 | else => @compileError("cannot bind field " ++ field_name ++ " of type " ++ @typeName(FieldType)), |
| 1093 | }, | 1113 | }, |
| 1094 | } | 1114 | } |