Skip to content

Commit

Permalink
Hide Connection implementation
Browse files Browse the repository at this point in the history
- Provide connect and close methods in the base interface.
- change all tests and readme to reflect this change.
  • Loading branch information
scull7 committed Mar 11, 2018
1 parent e6bca32 commit 0704c45
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 24 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ and [Named Placeholders](#named-placeholders).
#### Standard Query Method
```reason
let conn
= MySql2.Connection.make(~host=127.0.0.1, ~port=3306, ~user="root", ());
= MySql2.connect(~host=127.0.0.1, ~port=3306, ~user="root", ());
MySql2.execute(conn, "SHOW DATABASES", None, res => {
switch res {
| `Error(e) => Js.log2("ERROR: ", e)
| `Select(rows, meta) => Js.log3("SELECT: ", rows, meta)
| `Mutation(count, id) => Js.log3("MUTATION: ", count, id)
}
MySql2.Connection.close(conn);
MySql2.close(conn);
});
```
Expand All @@ -72,7 +72,7 @@ MySql2.execute(conn, "SHOW DATABASES", None, res => {
##### Named Placeholders
```reason
let conn
= MySql2.Connection.make(~host=127.0.0.1, ~port=3306, ~user="root", ());
= MySql2.connect(~host=127.0.0.1, ~port=3306, ~user="root", ());
let named = `Named(
Json.Encode.object_([
Expand All @@ -88,14 +88,14 @@ MySql2.execute(conn, "SELECT :x + :y AS result", Some(named), res => {
| `Mutation(count, id) => Js.log3("MUTATION: ", count, id)
}
}
MySql2.Connection.close(conn);
MySql2.close(conn);
});
```

##### Unnamed Placeholders
```reason
let conn
= MySql2.Connection.make(~host=127.0.0.1, ~port=3306, ~user="root", ());
= MySql2.connect(~host=127.0.0.1, ~port=3306, ~user="root", ());
let positional = `Positional(
Belt_Array.map([|5, 6|], Json.Encode.int) |> Json.Encode.jsonArray
Expand All @@ -108,7 +108,7 @@ MySql2.execute(conn, "SELECT 1 + ? + ? AS result", Some(positional), res => {
| `Mutation(count, id) => Js.log3("MUTATION: ", count, id)
}
}
MySql2.Connection.close(conn);
MySql2.close(conn);
});
```

Expand Down
4 changes: 2 additions & 2 deletions __tests__/error.re
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
open Jest;

let connect = () =>
MySql2.Connection.make(~host="127.0.0.1", ~port=3306, ~user="root", ());
MySql2.connect(~host="127.0.0.1", ~port=3306, ~user="root", ());

describe("MySql2 Error Handling", () => {
let conn = connect();
Expand All @@ -10,7 +10,7 @@ describe("MySql2 Error Handling", () => {

let accessDeniedTest = "Should respond with an access denied error";
testAsync(accessDeniedTest, finish => {
let c = MySql2.Connection.make(~password="s0m3 g@rb@g3 pw", ());
let c = MySql2.connect(~password="s0m3 g@rb@g3 pw", ());
let sql = "SELECT 1+1 AS result";
MySql2.execute(c, sql, None, res => {
switch res {
Expand Down
2 changes: 1 addition & 1 deletion __tests__/query.re
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
open Jest;

let connect = () =>
MySql2.Connection.make(~host="127.0.0.1", ~port=3306, ~user="root", ());
MySql2.connect(~host="127.0.0.1", ~port=3306, ~user="root", ());

type insert = {
affected_rows: int,
Expand Down
4 changes: 2 additions & 2 deletions __tests__/with_params.re
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
open Jest;

let connect = () =>
MySql2.Connection.make(~host="127.0.0.1", ~port=3306, ~user="root", ~password="", ~database="test", ());
MySql2.connect(~host="127.0.0.1", ~port=3306, ~user="root", ~password="", ~database="test", ());

type result = {result: int};

Expand All @@ -10,7 +10,7 @@ describe("Test parameter interpolation", () => {
let decoder = json => Json.Decode.({
result: json |> field("result", int)
});
afterAll(() => MySql2.Connection.close(conn));
afterAll(() => MySql2.close(conn));
describe("Standard (positional) parameters", () => {
testAsync("Expect parameters to be substituted properly", finish => {
let sql = "SELECT 1 + ? + ? AS result";
Expand Down
2 changes: 1 addition & 1 deletion examples/prepared_statements.re
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

let conn =
MySql2.Connection.make(~host="127.0.0.1", ~port=3306, ~user="root", ());
MySql2.connect(~host="127.0.0.1", ~port=3306, ~user="root", ());

let positional = Some(`Positional(
Belt_Array.map([|5,6|], Json.Encode.int) |> Json.Encode.jsonArray
Expand Down
4 changes: 2 additions & 2 deletions examples/simple.ml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
let conn = MySql2.Connection.make ~host:"127.0.0.1" ~port:3306 ~user:"root" ()
let conn = MySql2.connect ~host:"127.0.0.1" ~port:3306 ~user:"root" ()

let test_handler res =
match res with
Expand Down Expand Up @@ -34,4 +34,4 @@ let _ = MySql2.execute

let _ = MySql2.execute conn "SELECT * FROM test.simple" None test_handler

let _ = MySql2.Connection.close conn
let _ = MySql2.close conn
1 change: 1 addition & 0 deletions src/MySql2.ml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ let result_select rows meta = `Select (
)

let close = Connection.close
let connect = Connection.make

external execute :
Connection.t ->
Expand Down
18 changes: 8 additions & 10 deletions src/MySql2.mli
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,6 @@ type meta = meta_record array

module Connection : sig
type t

val make :
?host:string ->
?port:int ->
?user:string ->
?password:string ->
?database:string ->
unit -> t

val close : t -> unit
end

module Error : sig
Expand All @@ -46,6 +36,14 @@ type callback =

val close : Connection.t -> unit

val connect :
?host:string ->
?port:int ->
?user:string ->
?password:string ->
?database:string ->
unit -> connection

val execute : Connection.t -> string -> params -> callback -> unit

val parse_response :
Expand Down

0 comments on commit 0704c45

Please sign in to comment.