Skip to content

Instantly share code, notes, and snippets.

@scull7
Last active February 1, 2018 07:28
Show Gist options
  • Save scull7/03fcb966f92002ef12f8264bdd1de9e7 to your computer and use it in GitHub Desktop.
Save scull7/03fcb966f92002ef12f8264bdd1de9e7 to your computer and use it in GitHub Desktop.
Possible interface for the module.
let conn = Connection.make ~host:"127.0.0.1" ~port:3306 ~user:"root" ()
let test_handler = function
| Driver.Error e ->
let _ = Js.log "ERROR: " in Js.log e
| Driver.Select s ->
let _ = Js.log "Select: " in Js.log s
| Driver.Mutation m ->
let _ = Js.log "Mutation: " in Js.log m
let _ = MySql.raw conn "SHOW DATABASES" test_handler
let table_sql = {|
CREATE TABLE IF NOT EXISTS test.simple (
id bigint(20) NOT NULL AUTO_INCREMENT
, code varchar(32) NOT NULL
, PRIMARY KEY(id)
)
|}
let _ = MySql.raw conn table_sql test_handler
let simple_insert_sql = "INSERT INTO test.simple (code) VALUES ('foo')"
let _ = MySql.raw conn simple_insert_sql test_handler
let simple_update_sql = "UPDATE test.simple SET code='foo2' WHERE code='foo'"
let _ = MySql.raw conn simple_update_sql test_handler
let _ = MySql.raw
conn
"SELECT NULL FROM test.simple WHERE false"
test_handler
let _ = MySql.raw conn "SELECT * FROM test.simple" test_handler
let _ = Connection.end_ conn
let conn =
MySql.Connection.make(~host="127.0.0.1", ~port=3306, ~user="root", ());
MySql.with_params(conn, "SELECT 1 + ? + ? as result", [|5, 6|], result =>
switch result {
| Error(e) => Js.log2("ERROR: ", e)
| Mutation(m) => Js.log2("MUTATION: ", m)
| Select(s) => Js.log2("SELECT: ", s)
}
);
MySql.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.end_(conn);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment