diff options
| author | 2020-12-21 01:10:45 +0100 | |
|---|---|---|
| committer | 2020-12-21 01:10:45 +0100 | |
| commit | 3437d8797a43e214423b282a15a348255ec40937 (patch) | |
| tree | 379412b9abc0cb3c241b894c0e3ac1bae93433ef | |
| parent | reorder the arguments of readBytes (diff) | |
| download | zig-sqlite-3437d8797a43e214423b282a15a348255ec40937.tar.gz zig-sqlite-3437d8797a43e214423b282a15a348255ec40937.tar.xz zig-sqlite-3437d8797a43e214423b282a15a348255ec40937.zip | |
make readInt, readFloat, readBool return a specific type and column
Diffstat (limited to '')
| -rw-r--r-- | sqlite.zig | 31 |
1 files changed, 14 insertions, 17 deletions
| @@ -216,15 +216,15 @@ pub fn Iterator(comptime Type: type) type { | |||
| 216 | switch (TypeInfo) { | 216 | switch (TypeInfo) { |
| 217 | .Int => { | 217 | .Int => { |
| 218 | debug.assert(columns == 1); | 218 | debug.assert(columns == 1); |
| 219 | return try self.readInt(options); | 219 | return try self.readInt(Type, 0, options); |
| 220 | }, | 220 | }, |
| 221 | .Float => { | 221 | .Float => { |
| 222 | debug.assert(columns == 1); | 222 | debug.assert(columns == 1); |
| 223 | return try self.readFloat(options); | 223 | return try self.readFloat(Type, 0, options); |
| 224 | }, | 224 | }, |
| 225 | .Bool => { | 225 | .Bool => { |
| 226 | debug.assert(columns == 1); | 226 | debug.assert(columns == 1); |
| 227 | return try self.readBool(options); | 227 | return try self.readBool(0, options); |
| 228 | }, | 228 | }, |
| 229 | .Void => { | 229 | .Void => { |
| 230 | debug.assert(columns == 1); | 230 | debug.assert(columns == 1); |
| @@ -272,18 +272,18 @@ pub fn Iterator(comptime Type: type) type { | |||
| 272 | } | 272 | } |
| 273 | } | 273 | } |
| 274 | 274 | ||
| 275 | fn readInt(self: *Self, options: anytype) !Type { | 275 | fn readInt(self: *Self, comptime IntType: type, i: usize, options: anytype) !IntType { |
| 276 | const n = c.sqlite3_column_int64(self.stmt, 0); | 276 | const n = c.sqlite3_column_int64(self.stmt, @intCast(c_int, i)); |
| 277 | return @intCast(Type, n); | 277 | return @intCast(IntType, n); |
| 278 | } | 278 | } |
| 279 | 279 | ||
| 280 | fn readFloat(self: *Self, options: anytype) !Type { | 280 | fn readFloat(self: *Self, comptime FloatType: type, i: usize, options: anytype) !FloatType { |
| 281 | const d = c.sqlite3_column_double(self.stmt, 0); | 281 | const d = c.sqlite3_column_double(self.stmt, @intCast(c_int, i)); |
| 282 | return @floatCast(Type, d); | 282 | return @floatCast(FloatType, d); |
| 283 | } | 283 | } |
| 284 | 284 | ||
| 285 | fn readBool(self: *Self, options: anytype) !Type { | 285 | fn readBool(self: *Self, i: usize, options: anytype) !bool { |
| 286 | const d = c.sqlite3_column_int64(self.stmt, 0); | 286 | const d = c.sqlite3_column_int64(self.stmt, @intCast(c_int, i)); |
| 287 | return d > 0; | 287 | return d > 0; |
| 288 | } | 288 | } |
| 289 | 289 | ||
| @@ -335,16 +335,13 @@ pub fn Iterator(comptime Type: type) type { | |||
| 335 | }, | 335 | }, |
| 336 | else => switch (field_type_info) { | 336 | else => switch (field_type_info) { |
| 337 | .Int => { | 337 | .Int => { |
| 338 | const n = c.sqlite3_column_int64(self.stmt, i); | 338 | @field(value, field.name) = try self.readInt(field.field_type, i, options); |
| 339 | @field(value, field.name) = @intCast(field.field_type, n); | ||
| 340 | }, | 339 | }, |
| 341 | .Float => { | 340 | .Float => { |
| 342 | const f = c.sqlite3_column_double(self.stmt, i); | 341 | @field(value, field.name) = try self.readFloat(field.field_type, i, options); |
| 343 | @field(value, field.name) = f; | ||
| 344 | }, | 342 | }, |
| 345 | .Bool => { | 343 | .Bool => { |
| 346 | const n = c.sqlite3_column_int64(self.stmt, i); | 344 | @field(value, field.name) = try self.readBool(i, options); |
| 347 | @field(value, field.name) = n > 0; | ||
| 348 | }, | 345 | }, |
| 349 | .Void => { | 346 | .Void => { |
| 350 | @field(value, field.name) = {}; | 347 | @field(value, field.name) = {}; |