Skip to content

Commit

Permalink
Select response rows and concat methods
Browse files Browse the repository at this point in the history
  • Loading branch information
scull7 committed Jul 11, 2018
1 parent dec6c29 commit 8dc70cd
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
31 changes: 31 additions & 0 deletions __tests__/Test_mysql2_response.re
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,23 @@ describe("MySql2_response", () =>
])
);

describe("concat", () =>
test("should combine the rows of two result sets", () => {
let rows1 = [|Js.Json.string("foo"), Js.Json.string("bar")|];
let rows2 = [|Js.Json.string("boo"), Js.Json.string("baz")|];

let t1 = MySql2_response.Select.make(rows1, [|test_meta|]);
let t2 = MySql2_response.Select.make(rows2, [|test_meta|]);

MySql2_response.Select.concat(t1, t2)
|. MySql2_response.Select.mapDecoder(x =>
x |. Js.Json.decodeString |. Belt.Option.getExn
)
|. Expect.expect
|> Expect.toEqual([|"foo", "bar", "boo", "baz"|]);
})
);

describe("count", () =>
test("count should return 0 when rows is empty", () =>
MySql2_response.Select.make([||], [|test_meta|])
Expand All @@ -27,5 +44,19 @@ describe("MySql2_response", () =>
|> Expect.toBe(0)
)
);

describe("rows", () =>
test("rows should return the current set of rows", () => {
let rows = [|Js.Json.string("foo"), Js.Json.string("bar")|];

MySql2_response.Select.make(rows, [|test_meta|])
|. MySql2_response.Select.rows
|. Belt.Array.map(x =>
x |. Js.Json.decodeString |. Belt.Option.getExn
)
|. Expect.expect
|> Expect.toEqual([|"foo", "bar"|]);
})
);
})
);
4 changes: 4 additions & 0 deletions src/MySql2.rei
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,15 @@ module Select: {

let meta: t => array(Meta.t);

let concat: (t, t) => t;

let count: t => int;

let flatMap: (t, (Js.Json.t, array(Meta.t)) => 'a) => array('a);

let mapDecoder: (t, Js.Json.t => 'a) => array('a);

let rows: t => array(Js.Json.t);
};

type response = [
Expand Down
8 changes: 8 additions & 0 deletions src/MySql2_response.re
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,15 @@ module Select = {

let mapDecoder = (t, decoder) => Belt.Array.map(t |. rowsGet, decoder);

let concat = (t1, t2) =>
t(
~rows=Belt.Array.concat(t1 |. rowsGet, t2 |. rowsGet),
~meta=t1 |. metaGet,
);

let count = t => Belt.Array.length(t |. rowsGet);

let rows = t => t |. rowsGet;
};

/* https://github.com/mysqljs/mysql#getting-the-number-of-changed-rows */
Expand Down

0 comments on commit 8dc70cd

Please sign in to comment.