Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make execute_response's insert_id : int64 option #149

Open
wants to merge 1 commit into
base: ahrefs
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions impl/ocaml/mariadb/sqlgg_mariadb.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 ->
Expand Down
9 changes: 7 additions & 2 deletions impl/ocaml/mysql/sqlgg_mysql.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ->
Expand Down
2 changes: 1 addition & 1 deletion impl/ocaml/sqlgg_traits.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions impl/ocaml/sqlite3/sqlgg_sqlite3.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 =
Expand Down