Skip to content

Commit

Permalink
add conflict target to upsert
Browse files Browse the repository at this point in the history
The conflict target is required for upserts with DO UPDATE in older versions of sqlite (< 3.35.0)
  • Loading branch information
dgllghr committed Feb 19, 2024
1 parent 758d218 commit 61d8718
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
12 changes: 9 additions & 3 deletions src/Table.zig
Original file line number Diff line number Diff line change
Expand Up @@ -402,9 +402,14 @@ pub fn begin(_: *Self, _: *vtab.CallbackContext) !void {
}

pub fn sync(self: *Self, cb_ctx: *vtab.CallbackContext) !void {
log.debug("txn sync", .{});
if (self.next_rowid) |_| {
try self.row_group_creator.createAll(cb_ctx.arena);
try self.unloadNextRowid(cb_ctx.arena);
self.row_group_creator.createAll(cb_ctx.arena) catch |e| {
return cb_ctx.captureErrMsg(e, "failed to create row groups", .{});
};
self.unloadNextRowid(cb_ctx.arena) catch |e| {
return cb_ctx.captureErrMsg(e, "failed to persist next rowid", .{});
};
}
}

Expand All @@ -429,7 +434,8 @@ pub fn release(self: *Self, cb_ctx: *vtab.CallbackContext, savepoint_id: i32) !v
log.debug("txn savepoint {d} release", .{savepoint_id});
if (self.next_rowid) |_| {
// TODO Is this necessary? Releasing the savepoint does not end the transaction so I don't
// think it is necessary to persist and clear the next rowid.
// think it is necessary to persist and clear the next rowid. Also, if sync is called
// immediately after, row groups will not be created
try self.unloadNextRowid(cb_ctx.arena);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/TableData.zig
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,6 @@ fn writeDml(ctx: VtabCtxSchemaless, arena: *ArenaAllocator) ![]const u8 {
return fmt.allocPrintZ(arena.allocator(),
\\INSERT INTO "{s}_tabledata" (key, value)
\\VALUES (?, ?)
\\ON CONFLICT DO UPDATE SET value = excluded.value
\\ON CONFLICT (key) DO UPDATE SET value = excluded.value
, .{ctx.vtabName()});
}

0 comments on commit 61d8718

Please sign in to comment.