diff --git a/impl/ocaml/mariadb/sqlgg_mariadb.ml b/impl/ocaml/mariadb/sqlgg_mariadb.ml index 80889ca..fe9991e 100644 --- a/impl/ocaml/mariadb/sqlgg_mariadb.ml +++ b/impl/ocaml/mariadb/sqlgg_mariadb.ml @@ -198,7 +198,7 @@ type 'a connection = M.t type params = statement * M.Field.value array * int ref type row = M.Field.t array type result = M.Res.t -type execute_response = { affected_rows: int64; insert_id: int64 } +type execute_response = { affected_rows: int64; insert_id: int64 option } module Types = Types @@ -289,8 +289,13 @@ let execute db sql set_params = with_stmt db sql @@ fun stmt -> let open IO in set_params stmt >>= - fun res -> return { affected_rows = Int64.of_int (M.Res.affected_rows res); insert_id = Int64.of_int (M.Res.insert_id res) } - + fun res -> + let insert_id = + match M.Res.insert_id res with + | 0 -> None + | x -> Some (Int64.of_int x) + in + return { affected_rows = Int64.of_int (M.Res.affected_rows res); insert_id } let select_one_maybe db sql set_params convert = with_stmt db sql @@ fun stmt -> diff --git a/impl/ocaml/mysql/sqlgg_mysql.ml b/impl/ocaml/mysql/sqlgg_mysql.ml index 081885a..d61a07a 100644 --- a/impl/ocaml/mysql/sqlgg_mysql.ml +++ b/impl/ocaml/mysql/sqlgg_mysql.ml @@ -123,7 +123,7 @@ type 'a connection = Mysql.dbd type params = statement * string array * int ref type row = string option array type result = P.stmt_result -type execute_response = { affected_rows: int64; insert_id: int64 } +type execute_response = { affected_rows: int64; insert_id: int64 option } module Types = T open Types @@ -204,7 +204,12 @@ let execute db sql set_params = with_stmt db sql (fun stmt -> let _ = set_params stmt in if 0 <> P.real_status stmt then oops "execute : %s" sql; - { affected_rows = P.affected stmt; insert_id = P.insert_id stmt}) + let insert_id = + match P.insert_id stmt with + | 0L -> None + | x -> Some x + in + { affected_rows = P.affected stmt; insert_id; }) let select_one_maybe db sql set_params convert = with_stmt db sql (fun stmt -> diff --git a/impl/ocaml/sqlgg_traits.ml b/impl/ocaml/sqlgg_traits.ml index fb32ea7..841518b 100644 --- a/impl/ocaml/sqlgg_traits.ml +++ b/impl/ocaml/sqlgg_traits.ml @@ -27,7 +27,7 @@ module type M = sig type params type row type result - type execute_response = { affected_rows: int64; insert_id: int64 } + type execute_response = { affected_rows: int64; insert_id: int64 option } (** datatypes *) module Types : sig diff --git a/impl/ocaml/sqlite3/sqlgg_sqlite3.ml b/impl/ocaml/sqlite3/sqlgg_sqlite3.ml index 0b7351a..b15ba51 100644 --- a/impl/ocaml/sqlite3/sqlgg_sqlite3.ml +++ b/impl/ocaml/sqlite3/sqlgg_sqlite3.ml @@ -72,7 +72,7 @@ type 'a connection = S.db type params = statement * int * int ref type row = statement type result = unit -type execute_response = { affected_rows: int64; insert_id: int64 } +type execute_response = { affected_rows: int64; insert_id: int64 option } type num = int64 type text = string @@ -159,7 +159,12 @@ let execute db sql set_params = set_params stmt; let rc = S.step (fst stmt) in if rc <> S.Rc.DONE then raise (Oops (sprintf "execute : %s" sql)); - { affected_rows = Int64.of_int (S.changes db); insert_id = S.last_insert_rowid db } + let insert_id = + match S.last_insert_rowid db with + | 0L -> None + | x -> Some x + in + { affected_rows = Int64.of_int (S.changes db); insert_id; } ) let select_one_maybe db sql set_params convert =