summaryrefslogtreecommitdiff
path: root/sqlite.zig
diff options
context:
space:
mode:
authorGravatar thisLight2021-10-13 10:20:46 +0800
committerGravatar thisLight2021-10-13 10:20:46 +0800
commit725cc3da65097806efb97e0ac0ea3199a9720b2a (patch)
tree6f7471fbdb91ed526605ba52d246853e7d8b3237 /sqlite.zig
parentDynamicStatement.translateError: fix typo in name (diff)
downloadzig-sqlite-725cc3da65097806efb97e0ac0ea3199a9720b2a.tar.gz
zig-sqlite-725cc3da65097806efb97e0ac0ea3199a9720b2a.tar.xz
zig-sqlite-725cc3da65097806efb97e0ac0ea3199a9720b2a.zip
sqlite: format code
Diffstat (limited to '')
-rw-r--r--sqlite.zig33
1 files changed, 14 insertions, 19 deletions
diff --git a/sqlite.zig b/sqlite.zig
index 59ebb28..ecf78c9 100644
--- a/sqlite.zig
+++ b/sqlite.zig
@@ -1122,7 +1122,7 @@ pub const DynamicStatement = struct {
1122 if (@TypeOf(value) != void) { 1122 if (@TypeOf(value) != void) {
1123 if (@typeInfo(@TypeOf(value)) == .ErrorUnion and @typeInfo(@TypeOf(value)).ErrorUnion.payload == void) { 1123 if (@typeInfo(@TypeOf(value)) == .ErrorUnion and @typeInfo(@TypeOf(value)).ErrorUnion.payload == void) {
1124 return value; 1124 return value;
1125 } else if (@TypeOf(value) == c_int and value == c.SQLITE_OK){ 1125 } else if (@TypeOf(value) == c_int and value == c.SQLITE_OK) {
1126 return; 1126 return;
1127 } else { 1127 } else {
1128 return errors.errorFromResultCode(value); 1128 return errors.errorFromResultCode(value);
@@ -1153,13 +1153,13 @@ pub const DynamicStatement = struct {
1153 else => @compileError("cannot bind field " ++ field_name ++ " of type " ++ @typeName(FieldType)), 1153 else => @compileError("cannot bind field " ++ field_name ++ " of type " ++ @typeName(FieldType)),
1154 }, 1154 },
1155 .Array => |arr| switch (arr.child) { 1155 .Array => |arr| switch (arr.child) {
1156 u8 => u8arr: { 1156 u8 => u8arr: {
1157 const data: []const u8 = field[0..field.len]; 1157 const data: []const u8 = field[0..field.len];
1158 1158
1159 break :u8arr c.sqlite3_bind_text(self.stmt, column, data.ptr, @intCast(c_int, data.len), null); 1159 break :u8arr c.sqlite3_bind_text(self.stmt, column, data.ptr, @intCast(c_int, data.len), null);
1160 },
1161 else => @compileError("cannot bind field " ++ field_name ++ " of type array of " ++ @typeName(arr.child)),
1162 }, 1160 },
1161 else => @compileError("cannot bind field " ++ field_name ++ " of type array of " ++ @typeName(arr.child)),
1162 },
1163 .Optional => |opt| if (field) |non_null_field| { 1163 .Optional => |opt| if (field) |non_null_field| {
1164 return try self.bindField(opt.child, options, field_name, i, non_null_field); 1164 return try self.bindField(opt.child, options, field_name, i, non_null_field);
1165 } else optional_null: { 1165 } else optional_null: {
@@ -1196,10 +1196,10 @@ pub const DynamicStatement = struct {
1196 } 1196 }
1197 1197
1198 fn sqlite3BindParameterIndex(stmt: *c.sqlite3_stmt, comptime name: []const u8) c_int { 1198 fn sqlite3BindParameterIndex(stmt: *c.sqlite3_stmt, comptime name: []const u8) c_int {
1199 inline for (.{":", "@", "$"}) |prefix| { 1199 inline for (.{ ":", "@", "$" }) |prefix| {
1200 const id = std.fmt.comptimePrint(prefix++"{s}", .{name}); 1200 const id = std.fmt.comptimePrint(prefix ++ "{s}", .{name});
1201 const i = c.sqlite3_bind_parameter_index(stmt, id); 1201 const i = c.sqlite3_bind_parameter_index(stmt, id);
1202 if (i > 0) return i-1; // .bindField uses 0-based while sqlite3 uses 1-based index. 1202 if (i > 0) return i - 1; // .bindField uses 0-based while sqlite3 uses 1-based index.
1203 } 1203 }
1204 return -1; 1204 return -1;
1205 } 1205 }
@@ -1212,12 +1212,7 @@ pub const DynamicStatement = struct {
1212 inline for (StructTypeInfo.fields) |struct_field| { 1212 inline for (StructTypeInfo.fields) |struct_field| {
1213 const i = sqlite3BindParameterIndex(self.stmt, struct_field.name); 1213 const i = sqlite3BindParameterIndex(self.stmt, struct_field.name);
1214 if (i >= 0) { 1214 if (i >= 0) {
1215 try self.bindField( 1215 try self.bindField(struct_field.field_type, options, struct_field.name, i, @field(values, struct_field.name));
1216 struct_field.field_type,
1217 options,
1218 struct_field.name,
1219 i,
1220 @field(values, struct_field.name));
1221 } else if (i == -1) { 1216 } else if (i == -1) {
1222 return errors.SQLiteError.SQLiteNotFound; 1217 return errors.SQLiteError.SQLiteNotFound;
1223 // bug: do not put into a else block. reproduced in 0.8.1 and 0.9.0+dev.1193 1218 // bug: do not put into a else block. reproduced in 0.8.1 and 0.9.0+dev.1193
@@ -1228,9 +1223,9 @@ pub const DynamicStatement = struct {
1228 } 1223 }
1229 1224
1230 fn smartBind(self: *Self, options: anytype, values: anytype) !void { 1225 fn smartBind(self: *Self, options: anytype, values: anytype) !void {
1231 if (std.meta.fieldNames(@TypeOf(values)).len == 0){ 1226 if (std.meta.fieldNames(@TypeOf(values)).len == 0) {
1232 return; 1227 return;
1233 }else if (std.meta.trait.isTuple(@TypeOf(values))){ 1228 } else if (std.meta.trait.isTuple(@TypeOf(values))) {
1234 try self.bind(options, values); 1229 try self.bind(options, values);
1235 } else { 1230 } else {
1236 try self.bindNamedStruct(options, values); 1231 try self.bindNamedStruct(options, values);
@@ -1417,7 +1412,7 @@ pub fn Statement(comptime opts: StatementOptions, comptime query: ParsedQuery) t
1417 dynamicStmt: DynamicStatement, 1412 dynamicStmt: DynamicStatement,
1418 1413
1419 fn prepare(db: *Db, options: QueryOptions, flags: c_uint) !Self { 1414 fn prepare(db: *Db, options: QueryOptions, flags: c_uint) !Self {
1420 return Self { 1415 return Self{
1421 .dynamicStmt = try DynamicStatement.prepare(db, query.getQuery(), options, flags), 1416 .dynamicStmt = try DynamicStatement.prepare(db, query.getQuery(), options, flags),
1422 }; 1417 };
1423 } 1418 }
@@ -1518,7 +1513,7 @@ pub fn Statement(comptime opts: StatementOptions, comptime query: ParsedQuery) t
1518 /// 1513 ///
1519 pub fn exec(self: *Self, options: QueryOptions, values: anytype) !void { 1514 pub fn exec(self: *Self, options: QueryOptions, values: anytype) !void {
1520 try self.bind(.{}, values); 1515 try self.bind(.{}, values);
1521 1516
1522 var dummy_diags = Diagnostics{}; 1517 var dummy_diags = Diagnostics{};
1523 var diags = options.diags orelse &dummy_diags; 1518 var diags = options.diags orelse &dummy_diags;
1524 1519