Skip to content

Commit 4fc8574

Browse files
author
Cuihtlauac ALVARADO
committed
Expose insert_many
1 parent cee1168 commit 4fc8574

File tree

5 files changed

+27
-3
lines changed

5 files changed

+27
-3
lines changed

lib/bson/bson.ml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ end
3232
let some = Ctypes.allocate t
3333
let none = Ctypes.(from_voidp t null)
3434

35-
let ptr_of_opt = Option.value ~default:none
36-
3735
let get_version () =
3836
() |> C.Functions.get_version |> Ctypes_std_views.string_of_char_ptr
3937

lib/bson/bson.mli

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ end
1818

1919
val some : t_struct -> t
2020
val none : t
21-
val ptr_of_opt : t opt
2221
val t : t_struct Ctypes_static.typ
2322
val get_version : unit -> string
2423
val get_major_version : unit -> int

lib/function_description.ml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@ module Functions (F : Ctypes.FOREIGN) = struct
120120
@-> ptr (const Bson.t)
121121
@-> ptr Bson.t @-> ptr Bson.Error.t @-> returning bool)
122122

123+
let insert_many =
124+
foreign "mongoc_collection_insert_many"
125+
(ptr Types_generated.Collection.t
126+
@-> ptr (ptr (const Bson.t))
127+
@-> size_t
128+
@-> ptr (const Bson.t)
129+
@-> ptr Bson.t @-> ptr Bson.Error.t @-> returning bool)
130+
123131
let drop =
124132
foreign "mongoc_collection_drop"
125133
(ptr Types_generated.Collection.t

lib/mongoc.ml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ and Collection : sig
9191
(Int64.t, Bson.Error.t) result
9292

9393
val insert_one : ?opts:Bson.t -> t -> Bson.t -> (Bson.t, Bson.Error.t) result
94+
95+
val insert_many :
96+
?opts:Bson.t -> t -> Bson.t list -> (Bson.t, Bson.Error.t) result
97+
9498
val drop : t -> (unit, Bson.Error.t) result
9599
val destroy : t -> unit
96100
val from_database : Database.t -> string -> t
@@ -124,6 +128,17 @@ end = struct
124128
Ok reply
125129
else Error error
126130

131+
let insert_many ?(opts : Bson.t option) (coll : t) (documents : Bson.t list) :
132+
(Bson.t, Bson.Error.t) result =
133+
let error = Ctypes.(make Bson.Error.t |> addr) in
134+
let opts = Option.value ~default:Bson.none opts in
135+
let reply = Ctypes.(make Bson.t |> addr) in
136+
let length = documents |> List.length |> Unsigned.Size_t.of_int in
137+
let documents = Ctypes.CArray.(of_list (ptr Bson.t) documents |> start) in
138+
if C.Functions.Collection.insert_many coll documents length opts reply error
139+
then Ok reply
140+
else Error error
141+
127142
let drop (coll : t) =
128143
let error = Ctypes.(make Bson.Error.t |> addr) in
129144
if C.Functions.Collection.drop coll error then Ok () else Error error

lib/mongoc.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ and Collection : sig
9999
(Int64.t, Bson.Error.t) result
100100

101101
val insert_one : ?opts:Bson.t -> t -> Bson.t -> (Bson.t, Bson.Error.t) result
102+
103+
val insert_many :
104+
?opts:Bson.t -> t -> Bson.t list -> (Bson.t, Bson.Error.t) result
105+
102106
val drop : t -> (unit, Bson.Error.t) result
103107
val destroy : t -> unit
104108
val from_database : Database.t -> string -> t

0 commit comments

Comments
 (0)