From d09395fab0364b3af640418ce617f320280667ff Mon Sep 17 00:00:00 2001 From: Daniel Harvey Date: Sun, 11 Feb 2018 13:42:42 +0000 Subject: [PATCH 1/4] Add bs-dependencies to docs --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index bc29760..6212356 100644 --- a/README.md +++ b/README.md @@ -135,7 +135,7 @@ Inside of a BuckleScript project: yarn install --save bs-mysql2 ``` -Then add `bs-mysql2` to your `bs-dependencies` in `bsconfig.json`: +Then add `bs-mysql2` and `bs-mysql-common` to your `bs-dependencies` in `bsconfig.json`: ```json { "bs-dependencies": [ From c9094ee648daf77fabff3aeba557db5221e7e0d8 Mon Sep 17 00:00:00 2001 From: Daniel Harvey Date: Sun, 11 Feb 2018 13:43:38 +0000 Subject: [PATCH 2/4] Add bs-mysql-common to json example --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6212356..cab6b72 100644 --- a/README.md +++ b/README.md @@ -139,7 +139,8 @@ Then add `bs-mysql2` and `bs-mysql-common` to your `bs-dependencies` in `bsconfi ```json { "bs-dependencies": [ - "bs-mysql2" + "bs-mysql2", + "bs-mysql-common" ] } ``` From 6ea6d52778fe1b078cf0c84b0910346eed429918 Mon Sep 17 00:00:00 2001 From: Daniel Harvey Date: Sun, 11 Feb 2018 13:56:11 +0000 Subject: [PATCH 3/4] Include both reason and Ocaml syntax in examples --- README.md | 139 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 109 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index cab6b72..75d64d8 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ [![Coverage Status](https://coveralls.io/repos/github/scull7/bs-mysql2/badge.svg)](https://coveralls.io/github/scull7/bs-mysql2) # bs-mysql2 + ReasonML bindings to the [mysql2] library. This is a very rough implementation that will enable very simple use cases. @@ -25,27 +26,49 @@ Not all of the [mysql2] library [features][mysql2-features] are implemented but there is a usable implementation of the [Promise based wrapper](#promise-interface) and [Named Placeholders](#named-placeholders). - - [x] Faster / Better Performance (_kind of get this for free_) - - [x] [Prepared Statements][mysql2-prepared-statements] - [examples](#prepared-statements)* - - [ ] MySQL Binary Log Protocol - - [ ] [MySQL Server][mysql2-server] - - [ ] Extended support for Encoding and Collation - - [x] [Promise Wrapper][mysql2-promise] - [examples](#promise-interface)* - - [ ] Compression - - [ ] SSL and [Authentication Switch][mysql2-auth-switch] - - [ ] [Custom Streams][mysql2-custom-streams] - - [ ] Pooling +* [x] Faster / Better Performance (_kind of get this for free_) +* [x] [Prepared Statements][mysql2-prepared-statements] - [examples](#prepared-statements)\* +* [ ] MySQL Binary Log Protocol +* [ ] [MySQL Server][mysql2-server] +* [ ] Extended support for Encoding and Collation +* [x] [Promise Wrapper][mysql2-promise] - [examples](#promise-interface)\* +* [ ] Compression +* [ ] SSL and [Authentication Switch][mysql2-auth-switch] +* [ ] [Custom Streams][mysql2-custom-streams] +* [ ] Pooling - _* incomplete but usable implementation_ +_\* incomplete but usable implementation_ - ***NOTE:*** If you're trying to run the tests on macOS then you will need to: - ` brew install watchman` +**_NOTE:_** If you're trying to run the tests on macOS then you will need to: +`brew install watchman` ## Usage ### Standard Callback Interface #### Standard Query Method + +##### Reason syntax + +```reason +let conn = MySql.Connection.make(~host="127.0.0.1", ~port=3306, ~user="root", ()); + +MySql.Query.raw( + conn, + "SHOW DATABASES", + (r) => + switch r { + | Response.Error(e) => Js.log2("ERROR: ", e) + | Response.Select(s) => Js.log2("SELECT: ", s) + | Response.Mutation(m) => Js.log2("MUTATION: ", m) + } +); + +MySql.Connection.close(conn); +``` + +##### OCaml syntax + ```ocaml let conn = MySql.Connection.make ~host:"127.0.0.1" ~port:3306 ~user:"root" () @@ -61,7 +84,25 @@ let _ = MySql.Connection.close conn #### Prepared Statements -##### Named Placeholders +##### Named Placeholders (Reason syntax) + +```reason +let conn = + MySql.Connection.make(~host="127.0.0.1", ~port=3306, ~user="root", ()); + +MySql.Query.with_named_params(conn, "SELECT :x + :y as z", {"x": 1, "y": 2}, result => + switch result { + | Error(e) => Js.log2("ERROR: ", e) + | Mutation(m) => Js.log2("MUTATION: ", m) + | Select(s) => Js.log2("SELECT: ", s) + } +); + +MySql.Connection.close(conn); +``` + +##### Named Placeholders (OCaml syntax) + ```ocaml let conn = MySql.Connection.make ~host:"127.0.0.1" ~port:3306 ~user:"root" () @@ -79,22 +120,31 @@ let _ = MySql.Query.with_named_params conn sql2 params2 (fun r -> ) ``` -```reason -let conn = - MySql.Connection.make(~host="127.0.0.1", ~port=3306, ~user="root", ()); +##### Unnamed Placeholders (Reason syntax) -MySql.Query.with_named_params(conn, "SELECT :x + :y as z", {"x": 1, "y": 2}, result => - switch result { - | Error(e) => Js.log2("ERROR: ", e) - | Mutation(m) => Js.log2("MUTATION: ", m) - | Select(s) => Js.log2("SELECT: ", s) - } +```reason +let conn = MySql.Connection.make(~host="127.0.0.1", ~port=3306, ~user="root", ()); + +let logThenClose = (label, x) => { + let _ = Js.log2(label, x); + MySql.Connection.close(conn) +}; + +MySql.Query.with_params( + conn, + "SELECT 1 + ? + ? as result", + [|5, 6|], + (r) => + switch r { + | Response.Error(e) => logThenClose("ERROR: ", e) + | Response.Select(s) => logThenClose("SELECT: ", s) + | Response.Mutation(m) => logThenClose("MUTATION: ", m) + } ); - -MySql.Connection.close(conn); ``` -##### Unnamed Placeholders +##### Unnamed Placeholders (OCaml syntax) + ```ocaml let conn = MySql.Connection.make ~host:"127.0.0.1" ~port:3306 ~user:"root" () @@ -111,6 +161,32 @@ let _ = MySql.Query.with_params conn "SELECT 1 + ? + ? as result" [|5; 6|] (fun ``` ### Promise Interface + +#### Reason syntax + +```reason +let conn = MySql.Connection.make(~host="127.0.0.1", ~port=3306, ~user="root", ()); + +Js.Promise.resolve(conn) +|> MySql.Promise.pipe_with_params("SELECT ? as search", [|"%schema"|]) +|> Js.Promise.then_( + (value) => { + let _ = Js.log(value); + Js.Promise.resolve(1) + } + ) +|> MySql.Connection.Promise.close(conn) +|> Js.Promise.catch( + (err) => { + let _ = Js.log2(("Failure!!!", err)); + let _ = MySql.Connection.close(conn); + Js.Promise.resolve((-1)) + } + ); +``` + +#### Ocaml syntax + ```ocaml let conn = MySql.Connection.make ~host:"127.0.0.1" ~port:3306 ~user:"root" () @@ -131,32 +207,35 @@ let _ = Js.Promise.resolve(conn) ## How do I install it? Inside of a BuckleScript project: + ```shell yarn install --save bs-mysql2 ``` Then add `bs-mysql2` and `bs-mysql-common` to your `bs-dependencies` in `bsconfig.json`: + ```json { - "bs-dependencies": [ - "bs-mysql2", - "bs-mysql-common" - ] + "bs-dependencies": ["bs-mysql2", "bs-mysql-common"] } ``` ## How do I use it? ### Use it in your project + See the [Usage](#usage) section above... ### Run the examples + ```shell yarn run examples:simple ``` + ```shell yarn run examples:promise ``` + ```shell yarn run examples:prepared-statements ``` From a9e1bc669ec2b7a224cc67f5bef8a62c70ca7d57 Mon Sep 17 00:00:00 2001 From: Daniel Harvey Date: Sun, 11 Feb 2018 13:58:49 +0000 Subject: [PATCH 4/4] Tidy up heading sizes --- README.md | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 75d64d8..a0acffa 100644 --- a/README.md +++ b/README.md @@ -82,9 +82,9 @@ let _ = MySql.Query.raw conn "SHOW DATABASES" (fun r -> let _ = MySql.Connection.close conn ``` -#### Prepared Statements +#### Prepared Statements - Named Placeholders -##### Named Placeholders (Reason syntax) +##### Reason syntax ```reason let conn = @@ -101,7 +101,7 @@ MySql.Query.with_named_params(conn, "SELECT :x + :y as z", {"x": 1, "y": 2}, res MySql.Connection.close(conn); ``` -##### Named Placeholders (OCaml syntax) +##### OCaml syntax ```ocaml let conn = MySql.Connection.make ~host:"127.0.0.1" ~port:3306 ~user:"root" () @@ -120,7 +120,9 @@ let _ = MySql.Query.with_named_params conn sql2 params2 (fun r -> ) ``` -##### Unnamed Placeholders (Reason syntax) +#### Prepared Statements - Un-named Placeholders + +##### Reason syntax ```reason let conn = MySql.Connection.make(~host="127.0.0.1", ~port=3306, ~user="root", ()); @@ -143,7 +145,7 @@ MySql.Query.with_params( ); ``` -##### Unnamed Placeholders (OCaml syntax) +##### OCaml syntax ```ocaml let conn = MySql.Connection.make ~host:"127.0.0.1" ~port:3306 ~user:"root" () @@ -162,7 +164,7 @@ let _ = MySql.Query.with_params conn "SELECT 1 + ? + ? as result" [|5; 6|] (fun ### Promise Interface -#### Reason syntax +##### Reason syntax ```reason let conn = MySql.Connection.make(~host="127.0.0.1", ~port=3306, ~user="root", ()); @@ -185,7 +187,7 @@ Js.Promise.resolve(conn) ); ``` -#### Ocaml syntax +##### Ocaml syntax ```ocaml let conn = MySql.Connection.make ~host:"127.0.0.1" ~port:3306 ~user:"root" ()