diff --git a/src/Postgres/Library.fs b/src/Postgres/Library.fs index b96d322..ce48e74 100644 --- a/src/Postgres/Library.fs +++ b/src/Postgres/Library.fs @@ -56,8 +56,21 @@ module Parameters = /// Create an ID parameter (name "@id", key will be treated as a string) [] let idParam (key: 'TKey) = - // TODO: bind key by numeric types - "@id", Sql.string (string key) + match box key with + | :? int8 as it -> Sql.int8 it + | :? uint8 as it -> Sql.int8 (int8 it) + | :? int16 as it -> Sql.int16 it + | :? uint16 as it -> Sql.int16 (int16 it) + | :? int as it -> Sql.int it + | :? uint32 as it -> Sql.int (int it) + | :? int64 as it -> Sql.int64 it + | :? uint64 as it -> Sql.int64 (int64 it) + | :? decimal as it -> Sql.decimal it + | :? single as it -> Sql.double (double it) + | :? double as it -> Sql.double it + | :? string as it -> Sql.string it + | _ -> Sql.string (string key) + |> function it -> "@id", it /// Create a parameter with a JSON value [] @@ -135,12 +148,6 @@ module Query = | _ -> $"{it.Path PostgreSQL} {it.Op} {name.Derive it.ParameterName}") |> String.concat (match howMatched with Any -> " OR " | All -> " AND ") - /// Create a WHERE clause fragment to implement a comparison on a field in a JSON document - [] - [] - let whereByField field paramName = - whereByFields Any [ { field with ParameterName = Some paramName } ] - /// Create a WHERE clause fragment to implement an ID-based query [] let whereById paramName = diff --git a/src/Sqlite/Library.fs b/src/Sqlite/Library.fs index 6929be9..7acc7de 100644 --- a/src/Sqlite/Library.fs +++ b/src/Sqlite/Library.fs @@ -45,12 +45,6 @@ module Query = | _ -> $"{it.Path SQLite} {it.Op} {name.Derive it.ParameterName}") |> String.concat (match howMatched with Any -> " OR " | All -> " AND ") - /// Create a WHERE clause fragment to implement a comparison on a field in a JSON document - [] - [] - let whereByField field paramName = - whereByFields Any [ { field with ParameterName = Some paramName } ] - /// Create a WHERE clause fragment to implement an ID-based query [] let whereById paramName = diff --git a/src/Tests.CSharp/PostgresCSharpTests.cs b/src/Tests.CSharp/PostgresCSharpTests.cs index 71ff804..6e6d4c3 100644 --- a/src/Tests.CSharp/PostgresCSharpTests.cs +++ b/src/Tests.CSharp/PostgresCSharpTests.cs @@ -217,31 +217,6 @@ public static class PostgresCSharpTests "WHERE clause not correct"); }) ]), -#pragma warning disable CS0618 - TestList("WhereByField", - [ - TestCase("succeeds when a logical operator is passed", () => - { - Expect.equal(Postgres.Query.WhereByField(Field.GT("theField", 0), "@test"), - "data->>'theField' > @test", "WHERE clause not correct"); - }), - TestCase("succeeds when an existence operator is passed", () => - { - Expect.equal(Postgres.Query.WhereByField(Field.NEX("thatField"), ""), "data->>'thatField' IS NULL", - "WHERE clause not correct"); - }), - TestCase("succeeds when a between operator is passed with numeric values", () => - { - Expect.equal(Postgres.Query.WhereByField(Field.BT("aField", 50, 99), "@range"), - "(data->>'aField')::numeric BETWEEN @rangemin AND @rangemax", "WHERE clause not correct"); - }), - TestCase("succeeds when a between operator is passed with non-numeric values", () => - { - Expect.equal(Postgres.Query.WhereByField(Field.BT("field0", "a", "b"), "@alpha"), - "data->>'field0' BETWEEN @alphamin AND @alphamax", "WHERE clause not correct"); - }) - ]), -#pragma warning restore CS0618 TestCase("WhereById succeeds", () => { Expect.equal(Postgres.Query.WhereById("@id"), "data->>'Id' = @id", "WHERE clause not correct"); diff --git a/src/Tests.CSharp/SqliteCSharpTests.cs b/src/Tests.CSharp/SqliteCSharpTests.cs index 3aa1f86..60bfd09 100644 --- a/src/Tests.CSharp/SqliteCSharpTests.cs +++ b/src/Tests.CSharp/SqliteCSharpTests.cs @@ -63,26 +63,6 @@ public static class SqliteCSharpTests "WHERE clause not correct"); }) ]), -#pragma warning disable CS0618 - TestList("WhereByField", - [ - TestCase("succeeds when a logical operator is passed", () => - { - Expect.equal(Sqlite.Query.WhereByField(Field.GT("theField", 0), "@test"), - "data->>'theField' > @test", "WHERE clause not correct"); - }), - TestCase("succeeds when an existence operator is passed", () => - { - Expect.equal(Sqlite.Query.WhereByField(Field.NEX("thatField"), ""), "data->>'thatField' IS NULL", - "WHERE clause not correct"); - }), - TestCase("succeeds when the between operator is passed", () => - { - Expect.equal(Sqlite.Query.WhereByField(Field.BT("aField", 50, 99), "@range"), - "data->>'aField' BETWEEN @rangemin AND @rangemax", "WHERE clause not correct"); - }) - ]), -#pragma warning restore CS0618 TestCase("WhereById succeeds", () => { Expect.equal(Sqlite.Query.WhereById("@id"), "data->>'Id' = @id", "WHERE clause not correct"); diff --git a/src/Tests/PostgresTests.fs b/src/Tests/PostgresTests.fs index e39bbb9..fdd9068 100644 --- a/src/Tests/PostgresTests.fs +++ b/src/Tests/PostgresTests.fs @@ -196,32 +196,6 @@ let unitTests = "WHERE clause not correct" } ] - testList "whereByField" [ - test "succeeds when a logical operator is passed" { - Expect.equal - (Query.whereByField (Field.GT "theField" 0) "@test") - "data->>'theField' > @test" - "WHERE clause not correct" - } - test "succeeds when an existence operator is passed" { - Expect.equal - (Query.whereByField (Field.NEX "thatField") "") - "data->>'thatField' IS NULL" - "WHERE clause not correct" - } - test "succeeds when a between operator is passed with numeric values" { - Expect.equal - (Query.whereByField (Field.BT "aField" 50 99) "@range") - "(data->>'aField')::numeric BETWEEN @rangemin AND @rangemax" - "WHERE clause not correct" - } - test "succeeds when a between operator is passed with non-numeric values" { - Expect.equal - (Query.whereByField (Field.BT "field0" "a" "b") "@alpha") - "data->>'field0' BETWEEN @alphamin AND @alphamax" - "WHERE clause not correct" - } - ] test "whereById succeeds" { Expect.equal (Query.whereById "@id") "data->>'Id' = @id" "WHERE clause not correct" } @@ -466,7 +440,7 @@ let integrationTests = let! theCount = Count.all PostgresDb.TableName Expect.equal theCount 5 "There should have been 5 matching documents" } - ptestList "byFields" [ + testList "byFields" [ testTask "succeeds when items are found" { use db = PostgresDb.BuildDb() do! loadDocs () @@ -531,7 +505,7 @@ let integrationTests = Expect.isFalse exists "There should not have been an existing document" } ] - testList "byFields" [ + ftestList "byFields" [ testTask "succeeds when documents exist" { use db = PostgresDb.BuildDb() do! loadDocs () @@ -647,7 +621,7 @@ let integrationTests = PostgresDb.TableName All [ Field.EQ "Value" "purple"; Field.EX "Sub" ] Expect.equal (List.length docs) 1 "There should have been one document returned" } - ptestTask "succeeds when documents are not found" { + ftestTask "succeeds when documents are not found" { use db = PostgresDb.BuildDb() do! loadDocs () diff --git a/src/Tests/SqliteTests.fs b/src/Tests/SqliteTests.fs index a19d7d3..911d5aa 100644 --- a/src/Tests/SqliteTests.fs +++ b/src/Tests/SqliteTests.fs @@ -52,26 +52,6 @@ let unitTests = "WHERE clause not correct" } ] - testList "whereByField" [ - test "succeeds when a logical operator is passed" { - Expect.equal - (Query.whereByField (Field.GT "theField" 0) "@test") - "data->>'theField' > @test" - "WHERE clause not correct" - } - test "succeeds when an existence operator is passed" { - Expect.equal - (Query.whereByField (Field.NEX "thatField") "") - "data->>'thatField' IS NULL" - "WHERE clause not correct" - } - test "succeeds when the between operator is passed" { - Expect.equal - (Query.whereByField (Field.BT "aField" 50 99) "@range") - "data->>'aField' BETWEEN @rangemin AND @rangemax" - "WHERE clause not correct" - } - ] test "whereById succeeds" { Expect.equal (Query.whereById "@id") "data->>'Id' = @id" "WHERE clause not correct" }