Skip to content

Commit 9398874

Browse files
author
Jan Rochel
committed
fix replace_if_exists
1 parent 8064c11 commit 9398874

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/extensions/ocsipersist-pgsql/ocsipersist.ml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ let create_table db table =
8888
let insert db table key value =
8989
let query = sprintf "INSERT INTO %s VALUES ( $1 , $2 )
9090
ON CONFLICT ( key ) DO UPDATE SET value = $2 " table
91+
(*TODO: compatibility with < 9.5*)
9192
in exec db query [key; marshal value] >> Lwt.return ()
9293

9394

@@ -139,10 +140,12 @@ let find table key = use_pool @@ fun db ->
139140
let add table key value = use_pool @@ fun db ->
140141
insert db table key value
141142

142-
let replace_if_exists table key value =
143-
try_lwt
144-
find table key >> add table key value
145-
with Not_found -> Lwt.return ()
143+
let replace_if_exists table key value = use_pool @@ fun db ->
144+
let query = sprintf "UPDATE %s SET value = $2 WHERE key = $1 RETURNING 0" table in
145+
lwt result = exec db query [key; marshal value] in
146+
match result with
147+
| [] -> raise Not_found
148+
| _ -> Lwt.return ()
146149

147150
let remove table key = use_pool @@ fun db ->
148151
let query = sprintf "DELETE FROM %s WHERE key = $1 " table in

0 commit comments

Comments
 (0)