Split SQLite into files; add JSON extraction funcs
This commit is contained in:
parent
1dcc35d0f0
commit
f15feb2238
@ -2,6 +2,7 @@
|
||||
|
||||
open System.IO
|
||||
open System.Text
|
||||
open System.Threading.Tasks
|
||||
|
||||
/// <summary>The type of index to generate for the document</summary>
|
||||
[<Struct>]
|
||||
@ -342,12 +343,13 @@ module Results =
|
||||
/// <param name="sqlProps">The query from which JSON should be extracted</param>
|
||||
[<CompiledName "FSharpWriteJsonArray">]
|
||||
let writeJsonArray (writer: StreamWriter) (mapFunc: RowReader -> string) sqlProps = backgroundTask {
|
||||
let await (it: Task) = it.ConfigureAwait(false).GetAwaiter().GetResult()
|
||||
do! writer.WriteAsync "["
|
||||
let mutable isFirst = true
|
||||
do! sqlProps
|
||||
|> Sql.iterAsync (fun it ->
|
||||
if isFirst then isFirst <- false else writer.Write ","
|
||||
writer.WriteAsync(mapFunc it).ConfigureAwait(false).GetAwaiter().GetResult())
|
||||
if isFirst then isFirst <- false else await (writer.WriteAsync ",")
|
||||
(mapFunc >> writer.WriteAsync >> await) it)
|
||||
do! writer.WriteAsync "]"
|
||||
}
|
||||
|
||||
|
@ -8,6 +8,8 @@
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="Library.fs" />
|
||||
<Compile Include="WithConn.fs" />
|
||||
<Compile Include="Functions.fs" />
|
||||
<Compile Include="Extensions.fs" />
|
||||
<Compile Include="Compat.fs" />
|
||||
<None Include="README.md" Pack="true" PackagePath="\" />
|
||||
|
418
src/Sqlite/Functions.fs
Normal file
418
src/Sqlite/Functions.fs
Normal file
@ -0,0 +1,418 @@
|
||||
namespace BitBadger.Documents.Sqlite
|
||||
|
||||
open Microsoft.Data.Sqlite
|
||||
|
||||
/// <summary>Commands to execute custom SQL queries</summary>
|
||||
[<RequireQualifiedAccess>]
|
||||
module Custom =
|
||||
|
||||
/// <summary>Execute a query that returns a list of results</summary>
|
||||
/// <param name="query">The query to retrieve the results</param>
|
||||
/// <param name="parameters">Parameters to use for the query</param>
|
||||
/// <param name="mapFunc">The mapping function between the document and the domain item</param>
|
||||
/// <returns>A list of results for the given query</returns>
|
||||
[<CompiledName "FSharpList">]
|
||||
let list<'TDoc> query parameters (mapFunc: SqliteDataReader -> 'TDoc) =
|
||||
use conn = Configuration.dbConn ()
|
||||
WithConn.Custom.list<'TDoc> query parameters mapFunc conn
|
||||
|
||||
/// <summary>Execute a query that returns a list of results</summary>
|
||||
/// <param name="query">The query to retrieve the results</param>
|
||||
/// <param name="parameters">Parameters to use for the query</param>
|
||||
/// <param name="mapFunc">The mapping function between the document and the domain item</param>
|
||||
/// <returns>A list of results for the given query</returns>
|
||||
let List<'TDoc>(query, parameters, mapFunc: System.Func<SqliteDataReader, 'TDoc>) =
|
||||
use conn = Configuration.dbConn ()
|
||||
WithConn.Custom.List<'TDoc>(query, parameters, mapFunc, conn)
|
||||
|
||||
/// <summary>Execute a query that returns one or no results</summary>
|
||||
/// <param name="query">The query to retrieve the results</param>
|
||||
/// <param name="parameters">Parameters to use for the query</param>
|
||||
/// <param name="mapFunc">The mapping function between the document and the domain item</param>
|
||||
/// <returns><tt>Some</tt> with the first matching result, or <tt>None</tt> if not found</returns>
|
||||
[<CompiledName "FSharpSingle">]
|
||||
let single<'TDoc> query parameters (mapFunc: SqliteDataReader -> 'TDoc) =
|
||||
use conn = Configuration.dbConn ()
|
||||
WithConn.Custom.single<'TDoc> query parameters mapFunc conn
|
||||
|
||||
/// <summary>Execute a query that returns one or no results</summary>
|
||||
/// <param name="query">The query to retrieve the results</param>
|
||||
/// <param name="parameters">Parameters to use for the query</param>
|
||||
/// <param name="mapFunc">The mapping function between the document and the domain item</param>
|
||||
/// <returns>The first matching result, or <tt>null</tt> if not found</returns>
|
||||
let Single<'TDoc when 'TDoc: null and 'TDoc: not struct>(
|
||||
query, parameters, mapFunc: System.Func<SqliteDataReader, 'TDoc>) =
|
||||
use conn = Configuration.dbConn ()
|
||||
WithConn.Custom.Single<'TDoc>(query, parameters, mapFunc, conn)
|
||||
|
||||
/// <summary>Execute a query that returns no results</summary>
|
||||
/// <param name="query">The query to retrieve the results</param>
|
||||
/// <param name="parameters">Parameters to use for the query</param>
|
||||
[<CompiledName "NonQuery">]
|
||||
let nonQuery query parameters =
|
||||
use conn = Configuration.dbConn ()
|
||||
WithConn.Custom.nonQuery query parameters conn
|
||||
|
||||
/// <summary>Execute a query that returns a scalar value</summary>
|
||||
/// <param name="query">The query to retrieve the value</param>
|
||||
/// <param name="parameters">Parameters to use for the query</param>
|
||||
/// <param name="mapFunc">The mapping function to obtain the value</param>
|
||||
/// <returns>The scalar value for the query</returns>
|
||||
[<CompiledName "FSharpScalar">]
|
||||
let scalar<'T when 'T: struct> query parameters (mapFunc: SqliteDataReader -> 'T) =
|
||||
use conn = Configuration.dbConn ()
|
||||
WithConn.Custom.scalar<'T> query parameters mapFunc conn
|
||||
|
||||
/// <summary>Execute a query that returns a scalar value</summary>
|
||||
/// <param name="query">The query to retrieve the value</param>
|
||||
/// <param name="parameters">Parameters to use for the query</param>
|
||||
/// <param name="mapFunc">The mapping function to obtain the value</param>
|
||||
/// <returns>The scalar value for the query</returns>
|
||||
let Scalar<'T when 'T: struct>(query, parameters, mapFunc: System.Func<SqliteDataReader, 'T>) =
|
||||
use conn = Configuration.dbConn ()
|
||||
WithConn.Custom.Scalar<'T>(query, parameters, mapFunc, conn)
|
||||
|
||||
|
||||
/// <summary>Functions to create tables and indexes</summary>
|
||||
[<RequireQualifiedAccess>]
|
||||
module Definition =
|
||||
|
||||
/// <summary>Create a document table</summary>
|
||||
/// <param name="name">The table whose existence should be ensured (may include schema)</param>
|
||||
[<CompiledName "EnsureTable">]
|
||||
let ensureTable name =
|
||||
use conn = Configuration.dbConn ()
|
||||
WithConn.Definition.ensureTable name conn
|
||||
|
||||
/// <summary>Create an index on field(s) within documents in the specified table</summary>
|
||||
/// <param name="tableName">The table to be indexed (may include schema)</param>
|
||||
/// <param name="indexName">The name of the index to create</param>
|
||||
/// <param name="fields">One or more fields to be indexed</param>
|
||||
[<CompiledName "EnsureFieldIndex">]
|
||||
let ensureFieldIndex tableName indexName fields =
|
||||
use conn = Configuration.dbConn ()
|
||||
WithConn.Definition.ensureFieldIndex tableName indexName fields conn
|
||||
|
||||
|
||||
/// <summary>Document insert/save functions</summary>
|
||||
[<AutoOpen>]
|
||||
module Document =
|
||||
|
||||
/// <summary>Insert a new document</summary>
|
||||
/// <param name="tableName">The table into which the document should be inserted (may include schema)</param>
|
||||
/// <param name="document">The document to be inserted</param>
|
||||
[<CompiledName "Insert">]
|
||||
let insert<'TDoc> tableName (document: 'TDoc) =
|
||||
use conn = Configuration.dbConn ()
|
||||
WithConn.Document.insert tableName document conn
|
||||
|
||||
/// <summary>Save a document, inserting it if it does not exist and updating it if it does (AKA "upsert")</summary>
|
||||
/// <param name="tableName">The table into which the document should be saved (may include schema)</param>
|
||||
/// <param name="document">The document to be saved</param>
|
||||
[<CompiledName "Save">]
|
||||
let save<'TDoc> tableName (document: 'TDoc) =
|
||||
use conn = Configuration.dbConn ()
|
||||
WithConn.Document.save tableName document conn
|
||||
|
||||
|
||||
/// <summary>Commands to count documents</summary>
|
||||
[<RequireQualifiedAccess>]
|
||||
module Count =
|
||||
|
||||
/// <summary>Count all documents in a table</summary>
|
||||
/// <param name="tableName">The table in which documents should be counted (may include schema)</param>
|
||||
/// <returns>The count of the documents in the table</returns>
|
||||
[<CompiledName "All">]
|
||||
let all tableName =
|
||||
use conn = Configuration.dbConn ()
|
||||
WithConn.Count.all tableName conn
|
||||
|
||||
/// <summary>Count matching documents using JSON field comparisons (<tt>->> =</tt>, etc.)</summary>
|
||||
/// <param name="tableName">The table in which documents should be counted (may include schema)</param>
|
||||
/// <param name="howMatched">Whether to match any or all of the field conditions</param>
|
||||
/// <param name="fields">The field conditions to match</param>
|
||||
/// <returns>The count of matching documents in the table</returns>
|
||||
[<CompiledName "ByFields">]
|
||||
let byFields tableName howMatched fields =
|
||||
use conn = Configuration.dbConn ()
|
||||
WithConn.Count.byFields tableName howMatched fields conn
|
||||
|
||||
|
||||
/// <summary>Commands to determine if documents exist</summary>
|
||||
[<RequireQualifiedAccess>]
|
||||
module Exists =
|
||||
|
||||
/// <summary>Determine if a document exists for the given ID</summary>
|
||||
/// <param name="tableName">The table in which existence should be checked (may include schema)</param>
|
||||
/// <param name="docId">The ID of the document whose existence should be checked</param>
|
||||
/// <returns>True if a document exists, false if not</returns>
|
||||
[<CompiledName "ById">]
|
||||
let byId tableName (docId: 'TKey) =
|
||||
use conn = Configuration.dbConn ()
|
||||
WithConn.Exists.byId tableName docId conn
|
||||
|
||||
/// <summary>Determine if a document exists using JSON field comparisons (<tt>->> =</tt>, etc.)</summary>
|
||||
/// <param name="tableName">The table in which existence should be checked (may include schema)</param>
|
||||
/// <param name="howMatched">Whether to match any or all of the field conditions</param>
|
||||
/// <param name="fields">The field conditions to match</param>
|
||||
/// <returns>True if any matching documents exist, false if not</returns>
|
||||
[<CompiledName "ByFields">]
|
||||
let byFields tableName howMatched fields =
|
||||
use conn = Configuration.dbConn ()
|
||||
WithConn.Exists.byFields tableName howMatched fields conn
|
||||
|
||||
|
||||
/// <summary>Commands to retrieve documents</summary>
|
||||
[<RequireQualifiedAccess>]
|
||||
module Find =
|
||||
|
||||
/// <summary>Retrieve all documents in the given table</summary>
|
||||
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
|
||||
/// <returns>All documents from the given table</returns>
|
||||
[<CompiledName "FSharpAll">]
|
||||
let all<'TDoc> tableName =
|
||||
use conn = Configuration.dbConn ()
|
||||
WithConn.Find.all<'TDoc> tableName conn
|
||||
|
||||
/// <summary>Retrieve all documents in the given table</summary>
|
||||
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
|
||||
/// <returns>All documents from the given table</returns>
|
||||
let All<'TDoc> tableName =
|
||||
use conn = Configuration.dbConn ()
|
||||
WithConn.Find.All<'TDoc>(tableName, conn)
|
||||
|
||||
/// <summary>Retrieve all documents in the given table ordered by the given fields in the document</summary>
|
||||
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
|
||||
/// <param name="orderFields">Fields by which the results should be ordered</param>
|
||||
/// <returns>All documents from the given table, ordered by the given fields</returns>
|
||||
[<CompiledName "FSharpAllOrdered">]
|
||||
let allOrdered<'TDoc> tableName orderFields =
|
||||
use conn = Configuration.dbConn ()
|
||||
WithConn.Find.allOrdered<'TDoc> tableName orderFields conn
|
||||
|
||||
/// <summary>Retrieve all documents in the given table ordered by the given fields in the document</summary>
|
||||
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
|
||||
/// <param name="orderFields">Fields by which the results should be ordered</param>
|
||||
/// <returns>All documents from the given table, ordered by the given fields</returns>
|
||||
let AllOrdered<'TDoc> tableName orderFields =
|
||||
use conn = Configuration.dbConn ()
|
||||
WithConn.Find.AllOrdered<'TDoc>(tableName, orderFields, conn)
|
||||
|
||||
/// <summary>Retrieve a document by its ID</summary>
|
||||
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
|
||||
/// <param name="docId">The ID of the document to retrieve</param>
|
||||
/// <returns><tt>Some</tt> with the document if found, <tt>None</tt> otherwise</returns>
|
||||
[<CompiledName "FSharpById">]
|
||||
let byId<'TKey, 'TDoc> tableName docId =
|
||||
use conn = Configuration.dbConn ()
|
||||
WithConn.Find.byId<'TKey, 'TDoc> tableName docId conn
|
||||
|
||||
/// <summary>Retrieve a document by its ID</summary>
|
||||
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
|
||||
/// <param name="docId">The ID of the document to retrieve</param>
|
||||
/// <returns>The document if found, <tt>null</tt> otherwise</returns>
|
||||
let ById<'TKey, 'TDoc when 'TDoc: null and 'TDoc: not struct>(tableName, docId) =
|
||||
use conn = Configuration.dbConn ()
|
||||
WithConn.Find.ById<'TKey, 'TDoc>(tableName, docId, conn)
|
||||
|
||||
/// <summary>Retrieve documents matching JSON field comparisons (<tt>->> =</tt>, etc.)</summary>
|
||||
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
|
||||
/// <param name="howMatched">Whether to match any or all of the field conditions</param>
|
||||
/// <param name="fields">The field conditions to match</param>
|
||||
/// <returns>All documents matching the given fields</returns>
|
||||
[<CompiledName "FSharpByFields">]
|
||||
let byFields<'TDoc> tableName howMatched fields =
|
||||
use conn = Configuration.dbConn ()
|
||||
WithConn.Find.byFields<'TDoc> tableName howMatched fields conn
|
||||
|
||||
/// <summary>Retrieve documents matching JSON field comparisons (<tt>->> =</tt>, etc.)</summary>
|
||||
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
|
||||
/// <param name="howMatched">Whether to match any or all of the field conditions</param>
|
||||
/// <param name="fields">The field conditions to match</param>
|
||||
/// <returns>All documents matching the given fields</returns>
|
||||
let ByFields<'TDoc>(tableName, howMatched, fields) =
|
||||
use conn = Configuration.dbConn ()
|
||||
WithConn.Find.ByFields<'TDoc>(tableName, howMatched, fields, conn)
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve documents matching JSON field comparisons (<tt>->> =</tt>, etc.) ordered by the given fields in
|
||||
/// the document
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
|
||||
/// <param name="howMatched">Whether to match any or all of the field conditions</param>
|
||||
/// <param name="queryFields">The field conditions to match</param>
|
||||
/// <param name="orderFields">Fields by which the results should be ordered</param>
|
||||
/// <returns>All documents matching the given fields, ordered by the other given fields</returns>
|
||||
[<CompiledName "FSharpByFieldsOrdered">]
|
||||
let byFieldsOrdered<'TDoc> tableName howMatched queryFields orderFields =
|
||||
use conn = Configuration.dbConn ()
|
||||
WithConn.Find.byFieldsOrdered<'TDoc> tableName howMatched queryFields orderFields conn
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve documents matching JSON field comparisons (<tt>->> =</tt>, etc.) ordered by the given fields in
|
||||
/// the document
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
|
||||
/// <param name="howMatched">Whether to match any or all of the field conditions</param>
|
||||
/// <param name="queryFields">The field conditions to match</param>
|
||||
/// <param name="orderFields">Fields by which the results should be ordered</param>
|
||||
/// <returns>All documents matching the given fields, ordered by the other given fields</returns>
|
||||
let ByFieldsOrdered<'TDoc>(tableName, howMatched, queryFields, orderFields) =
|
||||
use conn = Configuration.dbConn ()
|
||||
WithConn.Find.ByFieldsOrdered<'TDoc>(tableName, howMatched, queryFields, orderFields, conn)
|
||||
|
||||
/// <summary>Retrieve the first document matching JSON field comparisons (<tt>->> =</tt>, etc.)</summary>
|
||||
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
|
||||
/// <param name="howMatched">Whether to match any or all of the field conditions</param>
|
||||
/// <param name="fields">The field conditions to match</param>
|
||||
/// <returns><tt>Some</tt> with the first document, or <tt>None</tt> if not found</returns>
|
||||
[<CompiledName "FSharpFirstByFields">]
|
||||
let firstByFields<'TDoc> tableName howMatched fields =
|
||||
use conn = Configuration.dbConn ()
|
||||
WithConn.Find.firstByFields<'TDoc> tableName howMatched fields conn
|
||||
|
||||
/// <summary>Retrieve the first document matching JSON field comparisons (<tt>->> =</tt>, etc.)</summary>
|
||||
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
|
||||
/// <param name="howMatched">Whether to match any or all of the field conditions</param>
|
||||
/// <param name="fields">The field conditions to match</param>
|
||||
/// <returns>The first document, or <tt>null</tt> if not found</returns>
|
||||
let FirstByFields<'TDoc when 'TDoc: null and 'TDoc: not struct>(tableName, howMatched, fields) =
|
||||
use conn = Configuration.dbConn ()
|
||||
WithConn.Find.FirstByFields<'TDoc>(tableName, howMatched, fields, conn)
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve the first document matching JSON field comparisons (<tt>->> =</tt>, etc.) ordered by the given
|
||||
/// fields in the document
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
|
||||
/// <param name="howMatched">Whether to match any or all of the field conditions</param>
|
||||
/// <param name="queryFields">The field conditions to match</param>
|
||||
/// <param name="orderFields">Fields by which the results should be ordered</param>
|
||||
/// <returns>
|
||||
/// <tt>Some</tt> with the first document ordered by the given fields, or <tt>None</tt> if not found
|
||||
/// </returns>
|
||||
[<CompiledName "FSharpFirstByFieldsOrdered">]
|
||||
let firstByFieldsOrdered<'TDoc> tableName howMatched queryFields orderFields =
|
||||
use conn = Configuration.dbConn ()
|
||||
WithConn.Find.firstByFieldsOrdered<'TDoc> tableName howMatched queryFields orderFields conn
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve the first document matching JSON field comparisons (<tt>->> =</tt>, etc.) ordered by the given
|
||||
/// fields in the document
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
|
||||
/// <param name="howMatched">Whether to match any or all of the field conditions</param>
|
||||
/// <param name="queryFields">The field conditions to match</param>
|
||||
/// <param name="orderFields">Fields by which the results should be ordered</param>
|
||||
/// <returns>The first document ordered by the given fields, or <tt>null</tt> if not found</returns>
|
||||
let FirstByFieldsOrdered<'TDoc when 'TDoc: null and 'TDoc: not struct>(
|
||||
tableName, howMatched, queryFields, orderFields) =
|
||||
use conn = Configuration.dbConn ()
|
||||
WithConn.Find.FirstByFieldsOrdered<'TDoc>(tableName, howMatched, queryFields, orderFields, conn)
|
||||
|
||||
|
||||
/// <summary>Commands to update documents</summary>
|
||||
[<RequireQualifiedAccess>]
|
||||
module Update =
|
||||
|
||||
/// <summary>Update (replace) an entire document by its ID</summary>
|
||||
/// <param name="tableName">The table in which a document should be updated (may include schema)</param>
|
||||
/// <param name="docId">The ID of the document to be updated (replaced)</param>
|
||||
/// <param name="document">The new document</param>
|
||||
[<CompiledName "ById">]
|
||||
let byId tableName (docId: 'TKey) (document: 'TDoc) =
|
||||
use conn = Configuration.dbConn ()
|
||||
WithConn.Update.byId tableName docId document conn
|
||||
|
||||
/// <summary>
|
||||
/// Update (replace) an entire document by its ID, using the provided function to obtain the ID from the document
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table in which a document should be updated (may include schema)</param>
|
||||
/// <param name="idFunc">The function to obtain the ID of the document</param>
|
||||
/// <param name="document">The new document</param>
|
||||
[<CompiledName "FSharpByFunc">]
|
||||
let byFunc tableName (idFunc: 'TDoc -> 'TKey) (document: 'TDoc) =
|
||||
use conn = Configuration.dbConn ()
|
||||
WithConn.Update.byFunc tableName idFunc document conn
|
||||
|
||||
/// <summary>
|
||||
/// Update (replace) an entire document by its ID, using the provided function to obtain the ID from the document
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table in which a document should be updated (may include schema)</param>
|
||||
/// <param name="idFunc">The function to obtain the ID of the document</param>
|
||||
/// <param name="document">The new document</param>
|
||||
let ByFunc(tableName, idFunc: System.Func<'TDoc, 'TKey>, document: 'TDoc) =
|
||||
use conn = Configuration.dbConn ()
|
||||
WithConn.Update.ByFunc(tableName, idFunc, document, conn)
|
||||
|
||||
|
||||
/// <summary>Commands to patch (partially update) documents</summary>
|
||||
[<RequireQualifiedAccess>]
|
||||
module Patch =
|
||||
|
||||
/// <summary>Patch a document by its ID</summary>
|
||||
/// <param name="tableName">The table in which a document should be patched (may include schema)</param>
|
||||
/// <param name="docId">The ID of the document to patch</param>
|
||||
/// <param name="patch">The partial document to patch the existing document</param>
|
||||
[<CompiledName "ById">]
|
||||
let byId tableName (docId: 'TKey) (patch: 'TPatch) =
|
||||
use conn = Configuration.dbConn ()
|
||||
WithConn.Patch.byId tableName docId patch conn
|
||||
|
||||
/// <summary>
|
||||
/// Patch documents using a JSON field comparison query in the <tt>WHERE</tt> clause (<tt>->> =</tt>, etc.)
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table in which documents should be patched (may include schema)</param>
|
||||
/// <param name="howMatched">Whether to match any or all of the field conditions</param>
|
||||
/// <param name="fields">The field conditions to match</param>
|
||||
/// <param name="patch">The partial document to patch the existing document</param>
|
||||
[<CompiledName "ByFields">]
|
||||
let byFields tableName howMatched fields (patch: 'TPatch) =
|
||||
use conn = Configuration.dbConn ()
|
||||
WithConn.Patch.byFields tableName howMatched fields patch conn
|
||||
|
||||
|
||||
/// <summary>Commands to remove fields from documents</summary>
|
||||
[<RequireQualifiedAccess>]
|
||||
module RemoveFields =
|
||||
|
||||
/// <summary>Remove fields from a document by the document's ID</summary>
|
||||
/// <param name="tableName">The table in which a document should be modified (may include schema)</param>
|
||||
/// <param name="docId">The ID of the document to modify</param>
|
||||
/// <param name="fieldNames">One or more field names to remove from the document</param>
|
||||
[<CompiledName "ById">]
|
||||
let byId tableName (docId: 'TKey) fieldNames =
|
||||
use conn = Configuration.dbConn ()
|
||||
WithConn.RemoveFields.byId tableName docId fieldNames conn
|
||||
|
||||
/// <summary>Remove fields from documents via a comparison on JSON fields in the document</summary>
|
||||
/// <param name="tableName">The table in which documents should be modified (may include schema)</param>
|
||||
/// <param name="howMatched">Whether to match any or all of the field conditions</param>
|
||||
/// <param name="fields">The field conditions to match</param>
|
||||
/// <param name="fieldNames">One or more field names to remove from the matching documents</param>
|
||||
[<CompiledName "ByFields">]
|
||||
let byFields tableName howMatched fields fieldNames =
|
||||
use conn = Configuration.dbConn ()
|
||||
WithConn.RemoveFields.byFields tableName howMatched fields fieldNames conn
|
||||
|
||||
|
||||
/// <summary>Commands to delete documents</summary>
|
||||
[<RequireQualifiedAccess>]
|
||||
module Delete =
|
||||
|
||||
/// <summary>Delete a document by its ID</summary>
|
||||
/// <param name="tableName">The table in which a document should be deleted (may include schema)</param>
|
||||
/// <param name="docId">The ID of the document to delete</param>
|
||||
[<CompiledName "ById">]
|
||||
let byId tableName (docId: 'TKey) =
|
||||
use conn = Configuration.dbConn ()
|
||||
WithConn.Delete.byId tableName docId conn
|
||||
|
||||
/// <summary>Delete documents by matching a JSON field comparison query (<tt>->> =</tt>, etc.)</summary>
|
||||
/// <param name="tableName">The table in which documents should be deleted (may include schema)</param>
|
||||
/// <param name="howMatched">Whether to match any or all of the field conditions</param>
|
||||
/// <param name="fields">The field conditions to match</param>
|
||||
[<CompiledName "ByFields">]
|
||||
let byFields tableName howMatched fields =
|
||||
use conn = Configuration.dbConn ()
|
||||
WithConn.Delete.byFields tableName howMatched fields conn
|
File diff suppressed because it is too large
Load Diff
510
src/Sqlite/WithConn.fs
Normal file
510
src/Sqlite/WithConn.fs
Normal file
@ -0,0 +1,510 @@
|
||||
/// <summary>Versions of queries that accept a <c>SqliteConnection</c> as the last parameter</summary>
|
||||
module BitBadger.Documents.Sqlite.WithConn
|
||||
|
||||
open BitBadger.Documents
|
||||
open Microsoft.Data.Sqlite
|
||||
|
||||
/// <summary>Commands to execute custom SQL queries</summary>
|
||||
[<RequireQualifiedAccess>]
|
||||
module Custom =
|
||||
|
||||
/// <summary>Execute a query that returns a list of results</summary>
|
||||
/// <param name="query">The query to retrieve the results</param>
|
||||
/// <param name="parameters">Parameters to use for the query</param>
|
||||
/// <param name="mapFunc">The mapping function between the document and the domain item</param>
|
||||
/// <param name="conn">The <tt>SqliteConnection</tt> to use to execute the query</param>
|
||||
/// <returns>A list of results for the given query</returns>
|
||||
[<CompiledName "FSharpList">]
|
||||
let list<'TDoc> query (parameters: SqliteParameter seq) (mapFunc: SqliteDataReader -> 'TDoc)
|
||||
(conn: SqliteConnection) =
|
||||
use cmd = conn.CreateCommand()
|
||||
cmd.CommandText <- query
|
||||
cmd.Parameters.AddRange parameters
|
||||
toCustomList<'TDoc> cmd mapFunc
|
||||
|
||||
/// <summary>Execute a query that returns a list of results</summary>
|
||||
/// <param name="query">The query to retrieve the results</param>
|
||||
/// <param name="parameters">Parameters to use for the query</param>
|
||||
/// <param name="mapFunc">The mapping function between the document and the domain item</param>
|
||||
/// <param name="conn">The <tt>SqliteConnection</tt> to use to execute the query</param>
|
||||
/// <returns>A list of results for the given query</returns>
|
||||
let List<'TDoc>(
|
||||
query, parameters: SqliteParameter seq, mapFunc: System.Func<SqliteDataReader, 'TDoc>,
|
||||
conn: SqliteConnection
|
||||
) =
|
||||
use cmd = conn.CreateCommand()
|
||||
cmd.CommandText <- query
|
||||
cmd.Parameters.AddRange parameters
|
||||
ToCustomList<'TDoc>(cmd, mapFunc)
|
||||
|
||||
/// <summary>Execute a query that returns one or no results</summary>
|
||||
/// <param name="query">The query to retrieve the results</param>
|
||||
/// <param name="parameters">Parameters to use for the query</param>
|
||||
/// <param name="mapFunc">The mapping function between the document and the domain item</param>
|
||||
/// <param name="conn">The <tt>SqliteConnection</tt> to use to execute the query</param>
|
||||
/// <returns><tt>Some</tt> with the first matching result, or <tt>None</tt> if not found</returns>
|
||||
[<CompiledName "FSharpSingle">]
|
||||
let single<'TDoc> query parameters (mapFunc: SqliteDataReader -> 'TDoc) conn = backgroundTask {
|
||||
let! results = list query parameters mapFunc conn
|
||||
return FSharp.Collections.List.tryHead results
|
||||
}
|
||||
|
||||
/// <summary>Execute a query that returns one or no results</summary>
|
||||
/// <param name="query">The query to retrieve the results</param>
|
||||
/// <param name="parameters">Parameters to use for the query</param>
|
||||
/// <param name="mapFunc">The mapping function between the document and the domain item</param>
|
||||
/// <param name="conn">The <tt>SqliteConnection</tt> to use to execute the query</param>
|
||||
/// <returns>The first matching result, or <tt>null</tt> if not found</returns>
|
||||
let Single<'TDoc when 'TDoc: null and 'TDoc: not struct>(
|
||||
query, parameters, mapFunc: System.Func<SqliteDataReader, 'TDoc>, conn
|
||||
) = backgroundTask {
|
||||
let! result = single<'TDoc> query parameters mapFunc.Invoke conn
|
||||
return Option.toObj result
|
||||
}
|
||||
|
||||
/// <summary>Execute a query that returns no results</summary>
|
||||
/// <param name="query">The query to retrieve the results</param>
|
||||
/// <param name="parameters">Parameters to use for the query</param>
|
||||
/// <param name="conn">The <tt>SqliteConnection</tt> to use to execute the query</param>
|
||||
[<CompiledName "NonQuery">]
|
||||
let nonQuery query (parameters: SqliteParameter seq) (conn: SqliteConnection) =
|
||||
use cmd = conn.CreateCommand()
|
||||
cmd.CommandText <- query
|
||||
cmd.Parameters.AddRange parameters
|
||||
write cmd
|
||||
|
||||
/// <summary>Execute a query that returns a scalar value</summary>
|
||||
/// <param name="query">The query to retrieve the value</param>
|
||||
/// <param name="parameters">Parameters to use for the query</param>
|
||||
/// <param name="mapFunc">The mapping function to obtain the value</param>
|
||||
/// <param name="conn">The <tt>SqliteConnection</tt> to use to execute the query</param>
|
||||
/// <returns>The scalar value for the query</returns>
|
||||
[<CompiledName "FSharpScalar">]
|
||||
let scalar<'T when 'T : struct> query (parameters: SqliteParameter seq) (mapFunc: SqliteDataReader -> 'T)
|
||||
(conn: SqliteConnection) = backgroundTask {
|
||||
use cmd = conn.CreateCommand()
|
||||
cmd.CommandText <- query
|
||||
cmd.Parameters.AddRange parameters
|
||||
use! rdr = cmd.ExecuteReaderAsync()
|
||||
let! isFound = rdr.ReadAsync()
|
||||
return if isFound then mapFunc rdr else Unchecked.defaultof<'T>
|
||||
}
|
||||
|
||||
/// <summary>Execute a query that returns a scalar value</summary>
|
||||
/// <param name="query">The query to retrieve the value</param>
|
||||
/// <param name="parameters">Parameters to use for the query</param>
|
||||
/// <param name="mapFunc">The mapping function to obtain the value</param>
|
||||
/// <param name="conn">The <tt>SqliteConnection</tt> to use to execute the query</param>
|
||||
/// <returns>The scalar value for the query</returns>
|
||||
let Scalar<'T when 'T: struct>(query, parameters, mapFunc: System.Func<SqliteDataReader, 'T>, conn) =
|
||||
scalar<'T> query parameters mapFunc.Invoke conn
|
||||
|
||||
/// <summary>Functions to create tables and indexes</summary>
|
||||
[<RequireQualifiedAccess>]
|
||||
module Definition =
|
||||
|
||||
/// <summary>Create a document table</summary>
|
||||
/// <param name="name">The table whose existence should be ensured (may include schema)</param>
|
||||
/// <param name="conn">The <tt>SqliteConnection</tt> to use to execute the query</param>
|
||||
[<CompiledName "EnsureTable">]
|
||||
let ensureTable name conn = backgroundTask {
|
||||
do! Custom.nonQuery (Query.Definition.ensureTable name) [] conn
|
||||
do! Custom.nonQuery (Query.Definition.ensureKey name SQLite) [] conn
|
||||
}
|
||||
|
||||
/// <summary>Create an index on field(s) within documents in the specified table</summary>
|
||||
/// <param name="tableName">The table to be indexed (may include schema)</param>
|
||||
/// <param name="indexName">The name of the index to create</param>
|
||||
/// <param name="fields">One or more fields to be indexed</param>
|
||||
/// <param name="conn">The <tt>SqliteConnection</tt> to use to execute the query</param>
|
||||
[<CompiledName "EnsureFieldIndex">]
|
||||
let ensureFieldIndex tableName indexName fields conn =
|
||||
Custom.nonQuery (Query.Definition.ensureIndexOn tableName indexName fields SQLite) [] conn
|
||||
|
||||
/// <summary>Commands to add documents</summary>
|
||||
[<AutoOpen>]
|
||||
module Document =
|
||||
|
||||
/// <summary>Insert a new document</summary>
|
||||
/// <param name="tableName">The table into which the document should be inserted (may include schema)</param>
|
||||
/// <param name="document">The document to be inserted</param>
|
||||
/// <param name="conn">The <tt>SqliteConnection</tt> to use to execute the query</param>
|
||||
[<CompiledName "Insert">]
|
||||
let insert<'TDoc> tableName (document: 'TDoc) conn =
|
||||
let query =
|
||||
match Configuration.autoIdStrategy () with
|
||||
| Disabled -> Query.insert tableName
|
||||
| strategy ->
|
||||
let idField = Configuration.idField ()
|
||||
let dataParam =
|
||||
if AutoId.NeedsAutoId strategy document idField then
|
||||
match strategy with
|
||||
| Number -> $"(SELECT coalesce(max(data->>'{idField}'), 0) + 1 FROM {tableName})"
|
||||
| Guid -> $"'{AutoId.GenerateGuid()}'"
|
||||
| RandomString -> $"'{AutoId.GenerateRandomString(Configuration.idStringLength ())}'"
|
||||
| Disabled -> "@data"
|
||||
|> function it -> $"json_set(@data, '$.{idField}', {it})"
|
||||
else "@data"
|
||||
(Query.insert tableName).Replace("@data", dataParam)
|
||||
Custom.nonQuery query [ jsonParam "@data" document ] conn
|
||||
|
||||
/// <summary>
|
||||
/// Save a document, inserting it if it does not exist and updating it if it does (AKA "upsert")
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table into which the document should be saved (may include schema)</param>
|
||||
/// <param name="document">The document to be saved</param>
|
||||
/// <param name="conn">The <tt>SqliteConnection</tt> to use to execute the query</param>
|
||||
[<CompiledName "Save">]
|
||||
let save<'TDoc> tableName (document: 'TDoc) conn =
|
||||
Custom.nonQuery (Query.save tableName) [ jsonParam "@data" document ] conn
|
||||
|
||||
/// <summary>Commands to count documents</summary>
|
||||
[<RequireQualifiedAccess>]
|
||||
module Count =
|
||||
|
||||
/// <summary>Count all documents in a table</summary>
|
||||
/// <param name="tableName">The table in which documents should be counted (may include schema)</param>
|
||||
/// <param name="conn">The <tt>SqliteConnection</tt> to use to execute the query</param>
|
||||
/// <returns>The count of the documents in the table</returns>
|
||||
[<CompiledName "All">]
|
||||
let all tableName conn =
|
||||
Custom.scalar (Query.count tableName) [] toCount conn
|
||||
|
||||
/// <summary>Count matching documents using JSON field comparisons (<tt>->> =</tt>, etc.)</summary>
|
||||
/// <param name="tableName">The table in which documents should be counted (may include schema)</param>
|
||||
/// <param name="howMatched">Whether to match any or all of the field conditions</param>
|
||||
/// <param name="fields">The field conditions to match</param>
|
||||
/// <param name="conn">The <tt>SqliteConnection</tt> to use to execute the query</param>
|
||||
/// <returns>The count of matching documents in the table</returns>
|
||||
[<CompiledName "ByFields">]
|
||||
let byFields tableName howMatched fields conn =
|
||||
Custom.scalar
|
||||
(Query.byFields (Query.count tableName) howMatched fields) (addFieldParams fields []) toCount conn
|
||||
|
||||
/// <summary>Commands to determine if documents exist</summary>
|
||||
[<RequireQualifiedAccess>]
|
||||
module Exists =
|
||||
|
||||
/// <summary>Determine if a document exists for the given ID</summary>
|
||||
/// <param name="tableName">The table in which existence should be checked (may include schema)</param>
|
||||
/// <param name="docId">The ID of the document whose existence should be checked</param>
|
||||
/// <param name="conn">The <tt>SqliteConnection</tt> to use to execute the query</param>
|
||||
/// <returns>True if a document exists, false if not</returns>
|
||||
[<CompiledName "ById">]
|
||||
let byId tableName (docId: 'TKey) conn =
|
||||
Custom.scalar (Query.exists tableName (Query.whereById docId)) [ idParam docId ] toExists conn
|
||||
|
||||
/// <summary>Determine if a document exists using JSON field comparisons (<tt>->> =</tt>, etc.)</summary>
|
||||
/// <param name="tableName">The table in which existence should be checked (may include schema)</param>
|
||||
/// <param name="howMatched">Whether to match any or all of the field conditions</param>
|
||||
/// <param name="fields">The field conditions to match</param>
|
||||
/// <param name="conn">The <tt>SqliteConnection</tt> to use to execute the query</param>
|
||||
/// <returns>True if any matching documents exist, false if not</returns>
|
||||
[<CompiledName "ByFields">]
|
||||
let byFields tableName howMatched fields conn =
|
||||
Custom.scalar
|
||||
(Query.exists tableName (Query.whereByFields howMatched fields))
|
||||
(addFieldParams fields [])
|
||||
toExists
|
||||
conn
|
||||
|
||||
/// <summary>Commands to retrieve documents</summary>
|
||||
[<RequireQualifiedAccess>]
|
||||
module Find =
|
||||
|
||||
/// <summary>Retrieve all documents in the given table</summary>
|
||||
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
|
||||
/// <param name="conn">The <tt>SqliteConnection</tt> to use to execute the query</param>
|
||||
/// <returns>All documents from the given table</returns>
|
||||
[<CompiledName "FSharpAll">]
|
||||
let all<'TDoc> tableName conn =
|
||||
Custom.list<'TDoc> (Query.find tableName) [] fromData<'TDoc> conn
|
||||
|
||||
/// <summary>Retrieve all documents in the given table</summary>
|
||||
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
|
||||
/// <param name="conn">The <tt>SqliteConnection</tt> to use to execute the query</param>
|
||||
/// <returns>All documents from the given table</returns>
|
||||
let All<'TDoc>(tableName, conn) =
|
||||
Custom.List(Query.find tableName, [], fromData<'TDoc>, conn)
|
||||
|
||||
/// <summary>Retrieve all documents in the given table ordered by the given fields in the document</summary>
|
||||
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
|
||||
/// <param name="orderFields">Fields by which the results should be ordered</param>
|
||||
/// <param name="conn">The <tt>SqliteConnection</tt> to use to execute the query</param>
|
||||
/// <returns>All documents from the given table, ordered by the given fields</returns>
|
||||
[<CompiledName "FSharpAllOrdered">]
|
||||
let allOrdered<'TDoc> tableName orderFields conn =
|
||||
Custom.list<'TDoc> (Query.find tableName + Query.orderBy orderFields SQLite) [] fromData<'TDoc> conn
|
||||
|
||||
/// <summary>Retrieve all documents in the given table ordered by the given fields in the document</summary>
|
||||
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
|
||||
/// <param name="orderFields">Fields by which the results should be ordered</param>
|
||||
/// <param name="conn">The <tt>SqliteConnection</tt> to use to execute the query</param>
|
||||
/// <returns>All documents from the given table, ordered by the given fields</returns>
|
||||
let AllOrdered<'TDoc>(tableName, orderFields, conn) =
|
||||
Custom.List(Query.find tableName + Query.orderBy orderFields SQLite, [], fromData<'TDoc>, conn)
|
||||
|
||||
/// <summary>Retrieve a document by its ID</summary>
|
||||
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
|
||||
/// <param name="docId">The ID of the document to retrieve</param>
|
||||
/// <param name="conn">The <tt>SqliteConnection</tt> to use to execute the query</param>
|
||||
/// <returns><tt>Some</tt> with the document if found, <tt>None</tt> otherwise</returns>
|
||||
[<CompiledName "FSharpById">]
|
||||
let byId<'TKey, 'TDoc> tableName (docId: 'TKey) conn =
|
||||
Custom.single<'TDoc> (Query.byId (Query.find tableName) docId) [ idParam docId ] fromData<'TDoc> conn
|
||||
|
||||
/// <summary>Retrieve a document by its ID</summary>
|
||||
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
|
||||
/// <param name="docId">The ID of the document to retrieve</param>
|
||||
/// <param name="conn">The <tt>SqliteConnection</tt> to use to execute the query</param>
|
||||
/// <returns>The document if found, <tt>null</tt> otherwise</returns>
|
||||
let ById<'TKey, 'TDoc when 'TDoc: null and 'TDoc: not struct>(tableName, docId: 'TKey, conn) =
|
||||
Custom.Single<'TDoc>(Query.byId (Query.find tableName) docId, [ idParam docId ], fromData<'TDoc>, conn)
|
||||
|
||||
/// <summary>Retrieve documents matching JSON field comparisons (<tt>->> =</tt>, etc.)</summary>
|
||||
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
|
||||
/// <param name="howMatched">Whether to match any or all of the field conditions</param>
|
||||
/// <param name="fields">The field conditions to match</param>
|
||||
/// <param name="conn">The <tt>SqliteConnection</tt> to use to execute the query</param>
|
||||
/// <returns>All documents matching the given fields</returns>
|
||||
[<CompiledName "FSharpByFields">]
|
||||
let byFields<'TDoc> tableName howMatched fields conn =
|
||||
Custom.list<'TDoc>
|
||||
(Query.byFields (Query.find tableName) howMatched fields)
|
||||
(addFieldParams fields [])
|
||||
fromData<'TDoc>
|
||||
conn
|
||||
|
||||
/// <summary>Retrieve documents matching JSON field comparisons (<tt>->> =</tt>, etc.)</summary>
|
||||
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
|
||||
/// <param name="howMatched">Whether to match any or all of the field conditions</param>
|
||||
/// <param name="fields">The field conditions to match</param>
|
||||
/// <param name="conn">The <tt>SqliteConnection</tt> to use to execute the query</param>
|
||||
/// <returns>All documents matching the given fields</returns>
|
||||
let ByFields<'TDoc>(tableName, howMatched, fields, conn) =
|
||||
Custom.List<'TDoc>(
|
||||
Query.byFields (Query.find tableName) howMatched fields,
|
||||
addFieldParams fields [],
|
||||
fromData<'TDoc>,
|
||||
conn)
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve documents matching JSON field comparisons (<tt>->> =</tt>, etc.) ordered by the given fields
|
||||
/// in the document
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
|
||||
/// <param name="howMatched">Whether to match any or all of the field conditions</param>
|
||||
/// <param name="queryFields">The field conditions to match</param>
|
||||
/// <param name="orderFields">Fields by which the results should be ordered</param>
|
||||
/// <param name="conn">The <tt>SqliteConnection</tt> to use to execute the query</param>
|
||||
/// <returns>All documents matching the given fields, ordered by the other given fields</returns>
|
||||
[<CompiledName "FSharpByFieldsOrdered">]
|
||||
let byFieldsOrdered<'TDoc> tableName howMatched queryFields orderFields conn =
|
||||
Custom.list<'TDoc>
|
||||
(Query.byFields (Query.find tableName) howMatched queryFields + Query.orderBy orderFields SQLite)
|
||||
(addFieldParams queryFields [])
|
||||
fromData<'TDoc>
|
||||
conn
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve documents matching JSON field comparisons (<tt>->> =</tt>, etc.) ordered by the given fields
|
||||
/// in the document
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table from which documents should be retrieved (may include schema)</param>
|
||||
/// <param name="howMatched">Whether to match any or all of the field conditions</param>
|
||||
/// <param name="queryFields">The field conditions to match</param>
|
||||
/// <param name="orderFields">Fields by which the results should be ordered</param>
|
||||
/// <param name="conn">The <tt>SqliteConnection</tt> to use to execute the query</param>
|
||||
/// <returns>All documents matching the given fields, ordered by the other given fields</returns>
|
||||
let ByFieldsOrdered<'TDoc>(tableName, howMatched, queryFields, orderFields, conn) =
|
||||
Custom.List<'TDoc>(
|
||||
Query.byFields (Query.find tableName) howMatched queryFields + Query.orderBy orderFields SQLite,
|
||||
addFieldParams queryFields [],
|
||||
fromData<'TDoc>,
|
||||
conn)
|
||||
|
||||
/// <summary>Retrieve the first document matching JSON field comparisons (<tt>->> =</tt>, etc.)</summary>
|
||||
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
|
||||
/// <param name="howMatched">Whether to match any or all of the field conditions</param>
|
||||
/// <param name="fields">The field conditions to match</param>
|
||||
/// <param name="conn">The <tt>SqliteConnection</tt> to use to execute the query</param>
|
||||
/// <returns><tt>Some</tt> with the first document, or <tt>None</tt> if not found</returns>
|
||||
[<CompiledName "FSharpFirstByFields">]
|
||||
let firstByFields<'TDoc> tableName howMatched fields conn =
|
||||
Custom.single
|
||||
$"{Query.byFields (Query.find tableName) howMatched fields} LIMIT 1"
|
||||
(addFieldParams fields [])
|
||||
fromData<'TDoc>
|
||||
conn
|
||||
|
||||
/// <summary>Retrieve the first document matching JSON field comparisons (<tt>->> =</tt>, etc.)</summary>
|
||||
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
|
||||
/// <param name="howMatched">Whether to match any or all of the field conditions</param>
|
||||
/// <param name="fields">The field conditions to match</param>
|
||||
/// <param name="conn">The <tt>SqliteConnection</tt> to use to execute the query</param>
|
||||
/// <returns>The first document, or <tt>null</tt> if not found</returns>
|
||||
let FirstByFields<'TDoc when 'TDoc: null and 'TDoc: not struct>(tableName, howMatched, fields, conn) =
|
||||
Custom.Single(
|
||||
$"{Query.byFields (Query.find tableName) howMatched fields} LIMIT 1",
|
||||
addFieldParams fields [],
|
||||
fromData<'TDoc>,
|
||||
conn)
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve the first document matching JSON field comparisons (<tt>->> =</tt>, etc.) ordered by the
|
||||
/// given fields in the document
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
|
||||
/// <param name="howMatched">Whether to match any or all of the field conditions</param>
|
||||
/// <param name="queryFields">The field conditions to match</param>
|
||||
/// <param name="orderFields">Fields by which the results should be ordered</param>
|
||||
/// <param name="conn">The <tt>SqliteConnection</tt> to use to execute the query</param>
|
||||
/// <returns>
|
||||
/// <tt>Some</tt> with the first document ordered by the given fields, or <tt>None</tt> if not found
|
||||
/// </returns>
|
||||
[<CompiledName "FSharpFirstByFieldsOrdered">]
|
||||
let firstByFieldsOrdered<'TDoc> tableName howMatched queryFields orderFields conn =
|
||||
Custom.single
|
||||
$"{Query.byFields (Query.find tableName) howMatched queryFields}{Query.orderBy orderFields SQLite} LIMIT 1"
|
||||
(addFieldParams queryFields [])
|
||||
fromData<'TDoc>
|
||||
conn
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve the first document matching JSON field comparisons (<tt>->> =</tt>, etc.) ordered by the
|
||||
/// given fields in the document
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table from which a document should be retrieved (may include schema)</param>
|
||||
/// <param name="howMatched">Whether to match any or all of the field conditions</param>
|
||||
/// <param name="queryFields">The field conditions to match</param>
|
||||
/// <param name="orderFields">Fields by which the results should be ordered</param>
|
||||
/// <param name="conn">The <tt>SqliteConnection</tt> to use to execute the query</param>
|
||||
/// <returns>The first document ordered by the given fields, or <tt>null</tt> if not found</returns>
|
||||
let FirstByFieldsOrdered<'TDoc when 'TDoc: null and 'TDoc: not struct>(
|
||||
tableName, howMatched, queryFields, orderFields, conn) =
|
||||
Custom.Single(
|
||||
$"{Query.byFields (Query.find tableName) howMatched queryFields}{Query.orderBy orderFields SQLite} LIMIT 1",
|
||||
addFieldParams queryFields [],
|
||||
fromData<'TDoc>,
|
||||
conn)
|
||||
|
||||
/// <summary>Commands to update documents</summary>
|
||||
[<RequireQualifiedAccess>]
|
||||
module Update =
|
||||
|
||||
/// <summary>Update (replace) an entire document by its ID</summary>
|
||||
/// <param name="tableName">The table in which a document should be updated (may include schema)</param>
|
||||
/// <param name="docId">The ID of the document to be updated (replaced)</param>
|
||||
/// <param name="document">The new document</param>
|
||||
/// <param name="conn">The <tt>SqliteConnection</tt> to use to execute the query</param>
|
||||
[<CompiledName "ById">]
|
||||
let byId tableName (docId: 'TKey) (document: 'TDoc) conn =
|
||||
Custom.nonQuery
|
||||
(Query.statementWhere (Query.update tableName) (Query.whereById docId))
|
||||
[ idParam docId; jsonParam "@data" document ]
|
||||
conn
|
||||
|
||||
/// <summary>
|
||||
/// Update (replace) an entire document by its ID, using the provided function to obtain the ID from the
|
||||
/// document
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table in which a document should be updated (may include schema)</param>
|
||||
/// <param name="idFunc">The function to obtain the ID of the document</param>
|
||||
/// <param name="document">The new document</param>
|
||||
/// <param name="conn">The <tt>SqliteConnection</tt> to use to execute the query</param>
|
||||
[<CompiledName "FSharpByFunc">]
|
||||
let byFunc tableName (idFunc: 'TDoc -> 'TKey) (document: 'TDoc) conn =
|
||||
byId tableName (idFunc document) document conn
|
||||
|
||||
/// <summary>
|
||||
/// Update (replace) an entire document by its ID, using the provided function to obtain the ID from the
|
||||
/// document
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table in which a document should be updated (may include schema)</param>
|
||||
/// <param name="idFunc">The function to obtain the ID of the document</param>
|
||||
/// <param name="document">The new document</param>
|
||||
/// <param name="conn">The <tt>SqliteConnection</tt> to use to execute the query</param>
|
||||
let ByFunc(tableName, idFunc: System.Func<'TDoc, 'TKey>, document: 'TDoc, conn) =
|
||||
byFunc tableName idFunc.Invoke document conn
|
||||
|
||||
/// <summary>Commands to patch (partially update) documents</summary>
|
||||
[<RequireQualifiedAccess>]
|
||||
module Patch =
|
||||
|
||||
/// <summary>Patch a document by its ID</summary>
|
||||
/// <param name="tableName">The table in which a document should be patched (may include schema)</param>
|
||||
/// <param name="docId">The ID of the document to patch</param>
|
||||
/// <param name="patch">The partial document to patch the existing document</param>
|
||||
/// <param name="conn">The <tt>SqliteConnection</tt> to use to execute the query</param>
|
||||
[<CompiledName "ById">]
|
||||
let byId tableName (docId: 'TKey) (patch: 'TPatch) conn =
|
||||
Custom.nonQuery
|
||||
(Query.byId (Query.patch tableName) docId) [ idParam docId; jsonParam "@data" patch ] conn
|
||||
|
||||
/// <summary>
|
||||
/// Patch documents using a JSON field comparison query in the <tt>WHERE</tt> clause (<tt>->> =</tt>,
|
||||
/// etc.)
|
||||
/// </summary>
|
||||
/// <param name="tableName">The table in which documents should be patched (may include schema)</param>
|
||||
/// <param name="howMatched">Whether to match any or all of the field conditions</param>
|
||||
/// <param name="fields">The field conditions to match</param>
|
||||
/// <param name="patch">The partial document to patch the existing document</param>
|
||||
/// <param name="conn">The <tt>SqliteConnection</tt> to use to execute the query</param>
|
||||
[<CompiledName "ByFields">]
|
||||
let byFields tableName howMatched fields (patch: 'TPatch) conn =
|
||||
Custom.nonQuery
|
||||
(Query.byFields (Query.patch tableName) howMatched fields)
|
||||
(addFieldParams fields [ jsonParam "@data" patch ])
|
||||
conn
|
||||
|
||||
/// <summary>Commands to remove fields from documents</summary>
|
||||
[<RequireQualifiedAccess>]
|
||||
module RemoveFields =
|
||||
|
||||
/// <summary>Remove fields from a document by the document's ID</summary>
|
||||
/// <param name="tableName">The table in which a document should be modified (may include schema)</param>
|
||||
/// <param name="docId">The ID of the document to modify</param>
|
||||
/// <param name="fieldNames">One or more field names to remove from the document</param>
|
||||
/// <param name="conn">The <tt>SqliteConnection</tt> to use to execute the query</param>
|
||||
[<CompiledName "ById">]
|
||||
let byId tableName (docId: 'TKey) fieldNames conn =
|
||||
let nameParams = fieldNameParams "@name" fieldNames
|
||||
Custom.nonQuery
|
||||
(Query.byId (Query.removeFields tableName nameParams) docId)
|
||||
(idParam docId |> Seq.singleton |> Seq.append nameParams)
|
||||
conn
|
||||
|
||||
/// <summary>Remove fields from documents via a comparison on JSON fields in the document</summary>
|
||||
/// <param name="tableName">The table in which documents should be modified (may include schema)</param>
|
||||
/// <param name="howMatched">Whether to match any or all of the field conditions</param>
|
||||
/// <param name="fields">The field conditions to match</param>
|
||||
/// <param name="fieldNames">One or more field names to remove from the matching documents</param>
|
||||
/// <param name="conn">The <tt>SqliteConnection</tt> to use to execute the query</param>
|
||||
[<CompiledName "ByFields">]
|
||||
let byFields tableName howMatched fields fieldNames conn =
|
||||
let nameParams = fieldNameParams "@name" fieldNames
|
||||
Custom.nonQuery
|
||||
(Query.byFields (Query.removeFields tableName nameParams) howMatched fields)
|
||||
(addFieldParams fields nameParams)
|
||||
conn
|
||||
|
||||
/// Commands to delete documents
|
||||
[<RequireQualifiedAccess>]
|
||||
module Delete =
|
||||
|
||||
/// <summary>Delete a document by its ID</summary>
|
||||
/// <param name="tableName">The table in which a document should be deleted (may include schema)</param>
|
||||
/// <param name="docId">The ID of the document to delete</param>
|
||||
/// <param name="conn">The <tt>SqliteConnection</tt> to use to execute the query</param>
|
||||
[<CompiledName "ById">]
|
||||
let byId tableName (docId: 'TKey) conn =
|
||||
Custom.nonQuery (Query.byId (Query.delete tableName) docId) [ idParam docId ] conn
|
||||
|
||||
/// <summary>Delete documents by matching a JSON field comparison query (<tt>->> =</tt>, etc.)</summary>
|
||||
/// <param name="tableName">The table in which documents should be deleted (may include schema)</param>
|
||||
/// <param name="howMatched">Whether to match any or all of the field conditions</param>
|
||||
/// <param name="fields">The field conditions to match</param>
|
||||
/// <param name="conn">The <tt>SqliteConnection</tt> to use to execute the query</param>
|
||||
[<CompiledName "ByFields">]
|
||||
let byFields tableName howMatched fields conn =
|
||||
Custom.nonQuery (Query.byFields (Query.delete tableName) howMatched fields) (addFieldParams fields []) conn
|
Loading…
x
Reference in New Issue
Block a user