A library for SQL composition in BucklesScript
SqlComposer.Select.(
make()
|. field("*")
|. from("test")
|. toSql
|. Js.log
);
let _ =
let open SqlComposer.Select in
((((make ()) |. (field "*")) |. (from "test")) |. toSql) |. Js.log
SELECT
*
FROM test
WHERE 1=1
There is an implicit WHERE 1=1
added to every query. This is done to
eliminate confusion with compound queries regarding the concatenation of
where clauses.
SqlComposer.Select.(
make()
|. field("*")
|. from("test")
|. where("AND test.foo = ?")
|. toSql
|. Js.log
);
let _ =
let open SqlComposer.Select in
(((((make ()) |. (field "*")) |. (from "test")) |.
(where "AND test.foo = ?"))
|. toSql)
|. Js.log
SELECT
*
FROM test
WHERE 1=1
AND test.foo = ?
SqlComposer.Select.(
make()
|. field("*")
|. from("test")
|. join("JOIN foo ON test.foo_id = foo.id")
|. toSql
|. Js.log
)
SELECT
*
FROM test
JOIN foo ON test.foo_id = foo.id
WHERE 1=1
let base_query = SqlComposer.Select.(
make()
|. from("test")
|. field("foo")
|. field("bar")
);
SqlComposer.Select.(
base_query
|. where("AND test.foo = ?")
|. toSql
|. Js.log
);
let base_query =
let open SqlComposer.Select in
(((make ()) |. (from "test")) |. (field "foo")) |. (field "bar")
let _ =
let open SqlComposer.Select in
((base_query |. (where "AND test.foo = ?")) |. toSql) |. Js.log
SELECT
foo
, bar
FROM test
WHERE 1=1
AND test.foo = ?
SqlComposer.Select.(
make()
|. from("test")
|. field("foo AS bar")
|. toSql
|. Js.log
);
let _ =
let open SqlComposer.Select in
((((make ()) |. (from "test")) |. (field "foo AS bar")) |. toSql) |.
Js.log
SELECT
foo AS bar
FROM test
WHERE 1=1
SqlComposer.Select.(
make()
|. from("test")
|. field("*")
|. orderBy(`Asc("foo"))
|. orderBy(`Desc("bar"))
|. toSql
|. Js.log
);
SELECT
*
FROM test
WHERE 1=1
ORDER BY
foo ASC
, bar DESC
SqlComposer.Select.(
make()
|. from("test")
|. field("foo AS bar")
|. groupBy("foo")
|. groupBy("thing")
);
let _ =
let open SqlComposer.Select in
((((make ()) |. (from "test")) |. (field "foo AS bar")) |.
(groupBy "foo"))
|. (groupBy "thing")
SELECT
foo AS bar
FROM test
WHERE 1=1
GROUP BY
foo
, thing
SqlComposer.Select.(
make()
|. field("foo")
|. from("test")
|. modifier(`Distinct)
);
let _ =
let open SqlComposer.Select in
(((make ()) |. (field "foo")) |. (from "test")) |. (modifier `Distinct)
SELECT DISTINCT
foo
FROM test
WHERE 1=1
Inside of a BuckleScript project:
yarn install --save bs-sql-composer
Then add bs-sql-composer
to your bs-dependencies
in bsconfig.json
:
{
"bs-dependencies": [ "bs-sql-composer" ]
}