RC4 changes (#7)
- Add `In` and `InArray` comparisons - Replace `Op` with `Comparison` (internal API, but was public) - Spell out comparisons in `Field` constructor functions Reviewed-on: #7
This commit was merged in pull request #7.
This commit is contained in:
@@ -37,18 +37,26 @@ module Query =
|
||||
let name = ParameterName()
|
||||
fields
|
||||
|> Seq.map (fun it ->
|
||||
match it.Op with
|
||||
| EX | NEX -> $"{it.Path SQLite} {it.Op}"
|
||||
| BT ->
|
||||
match it.Comparison with
|
||||
| Exists | NotExists -> $"{it.Path SQLite AsSql} {it.Comparison.OpSql}"
|
||||
| Between _ ->
|
||||
let p = name.Derive it.ParameterName
|
||||
$"{it.Path SQLite} {it.Op} {p}min AND {p}max"
|
||||
| _ -> $"{it.Path SQLite} {it.Op} {name.Derive it.ParameterName}")
|
||||
$"{it.Path SQLite AsSql} {it.Comparison.OpSql} {p}min AND {p}max"
|
||||
| In values ->
|
||||
let p = name.Derive it.ParameterName
|
||||
let paramNames = values |> Seq.mapi (fun idx _ -> $"{p}_{idx}") |> String.concat ", "
|
||||
$"{it.Path SQLite AsSql} {it.Comparison.OpSql} ({paramNames})"
|
||||
| InArray (table, values) ->
|
||||
let p = name.Derive it.ParameterName
|
||||
let paramNames = values |> Seq.mapi (fun idx _ -> $"{p}_{idx}") |> String.concat ", "
|
||||
$"EXISTS (SELECT 1 FROM json_each({table}.data, '$.{it.Name}') WHERE value IN ({paramNames}))"
|
||||
| _ -> $"{it.Path SQLite AsSql} {it.Comparison.OpSql} {name.Derive it.ParameterName}")
|
||||
|> String.concat $" {howMatched} "
|
||||
|
||||
/// Create a WHERE clause fragment to implement an ID-based query
|
||||
[<CompiledName "WhereById">]
|
||||
let whereById paramName =
|
||||
whereByFields Any [ { Field.EQ (Configuration.idField ()) 0 with ParameterName = Some paramName } ]
|
||||
whereByFields Any [ { Field.Equal (Configuration.idField ()) 0 with ParameterName = Some paramName } ]
|
||||
|
||||
/// Create an UPDATE statement to patch documents
|
||||
[<CompiledName "Patch">]
|
||||
@@ -66,7 +74,7 @@ module Query =
|
||||
let byId<'TKey> statement (docId: 'TKey) =
|
||||
Query.statementWhere
|
||||
statement
|
||||
(whereByFields Any [ { Field.EQ (Configuration.idField ()) docId with ParameterName = Some "@id" } ])
|
||||
(whereByFields Any [ { Field.Equal (Configuration.idField ()) docId with ParameterName = Some "@id" } ])
|
||||
|
||||
/// Create a query on JSON fields
|
||||
[<CompiledName "ByFields">]
|
||||
@@ -103,14 +111,16 @@ module Parameters =
|
||||
fields
|
||||
|> Seq.map (fun it ->
|
||||
seq {
|
||||
match it.Op with
|
||||
| EX | NEX -> ()
|
||||
| BT ->
|
||||
let p = name.Derive it.ParameterName
|
||||
let values = it.Value :?> obj list
|
||||
yield SqliteParameter($"{p}min", List.head values)
|
||||
yield SqliteParameter($"{p}max", List.last values)
|
||||
| _ -> yield SqliteParameter(name.Derive it.ParameterName, it.Value) })
|
||||
match it.Comparison with
|
||||
| Exists | NotExists -> ()
|
||||
| Between (min, max) ->
|
||||
let p = name.Derive it.ParameterName
|
||||
yield! [ SqliteParameter($"{p}min", min); SqliteParameter($"{p}max", max) ]
|
||||
| In values | InArray (_, values) ->
|
||||
let p = name.Derive it.ParameterName
|
||||
yield! values |> Seq.mapi (fun idx v -> SqliteParameter($"{p}_{idx}", v))
|
||||
| Equal v | Greater v | GreaterOrEqual v | Less v | LessOrEqual v | NotEqual v ->
|
||||
yield SqliteParameter(name.Derive it.ParameterName, v) })
|
||||
|> Seq.collect id
|
||||
|> Seq.append parameters
|
||||
|> Seq.toList
|
||||
|
||||
@@ -73,13 +73,13 @@ Count customers in Atlanta:
|
||||
```csharp
|
||||
// C#; parameters are table name, field, operator, and value
|
||||
// Count.ByField type signature is Func<string, Field, Task<long>>
|
||||
var customerCount = await Count.ByField("customer", Field.EQ("City", "Atlanta"));
|
||||
var customerCount = await Count.ByField("customer", Field.Equal("City", "Atlanta"));
|
||||
```
|
||||
|
||||
```fsharp
|
||||
// F#
|
||||
// Count.byField type signature is string -> Field -> Task<int64>
|
||||
let! customerCount = Count.byField "customer" (Field.EQ "City" "Atlanta")
|
||||
let! customerCount = Count.byField "customer" (Field.Equal "City" "Atlanta")
|
||||
```
|
||||
|
||||
Delete customers in Chicago: _(no offense, Second City; just an example...)_
|
||||
@@ -87,13 +87,13 @@ Delete customers in Chicago: _(no offense, Second City; just an example...)_
|
||||
```csharp
|
||||
// C#; parameters are same as above, except return is void
|
||||
// Delete.ByField type signature is Func<string, Field, Task>
|
||||
await Delete.ByField("customer", Field.EQ("City", "Chicago"));
|
||||
await Delete.ByField("customer", Field.Equal("City", "Chicago"));
|
||||
```
|
||||
|
||||
```fsharp
|
||||
// F#
|
||||
// Delete.byField type signature is string -> string -> Op -> obj -> Task<unit>
|
||||
do! Delete.byField "customer" (Field.EQ "City" "Chicago")
|
||||
do! Delete.byField "customer" (Field.Equal "City" "Chicago")
|
||||
```
|
||||
|
||||
## More Information
|
||||
|
||||
Reference in New Issue
Block a user