From 4b4330574683b407c952f8fa9b81a77835d949fc Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Fri, 24 Feb 2023 21:51:51 -0500 Subject: [PATCH] Use scalar for custom count/exists --- src/MyWebLog.Data/MyWebLog.Data.fsproj | 2 +- src/MyWebLog.Data/Postgres/PostgresCache.fs | 9 +++------ .../Postgres/PostgresCategoryData.fs | 16 ++++++---------- src/MyWebLog.Data/Postgres/PostgresHelpers.fs | 13 +++++-------- src/MyWebLog.Data/Postgres/PostgresPostData.fs | 8 +------- .../Postgres/PostgresWebLogUserData.fs | 13 +++++-------- 6 files changed, 21 insertions(+), 40 deletions(-) diff --git a/src/MyWebLog.Data/MyWebLog.Data.fsproj b/src/MyWebLog.Data/MyWebLog.Data.fsproj index 840751d..031fc79 100644 --- a/src/MyWebLog.Data/MyWebLog.Data.fsproj +++ b/src/MyWebLog.Data/MyWebLog.Data.fsproj @@ -5,7 +5,7 @@ - + diff --git a/src/MyWebLog.Data/Postgres/PostgresCache.fs b/src/MyWebLog.Data/Postgres/PostgresCache.fs index 44b7071..a7b0280 100644 --- a/src/MyWebLog.Data/Postgres/PostgresCache.fs +++ b/src/MyWebLog.Data/Postgres/PostgresCache.fs @@ -48,13 +48,10 @@ type DistributedCache () = do task { let! exists = - Configuration.dataSource () - |> Sql.fromDataSource - |> Sql.query $" - SELECT EXISTS + Custom.scalar + $"SELECT EXISTS (SELECT 1 FROM pg_tables WHERE schemaname = 'public' AND tablename = 'session') - AS {existsName}" - |> Sql.executeRowAsync Map.toExists + AS {existsName}" [] Map.toExists if not exists then do! Custom.nonQuery "CREATE TABLE session ( diff --git a/src/MyWebLog.Data/Postgres/PostgresCategoryData.fs b/src/MyWebLog.Data/Postgres/PostgresCategoryData.fs index ba15a4e..244faed 100644 --- a/src/MyWebLog.Data/Postgres/PostgresCategoryData.fs +++ b/src/MyWebLog.Data/Postgres/PostgresCategoryData.fs @@ -38,19 +38,15 @@ type PostgresCategoryData (log : ILogger) = |> List.ofSeq |> arrayContains (nameof Post.empty.CategoryIds) id let postCount = - Configuration.dataSource () - |> Sql.fromDataSource - |> Sql.query $""" - SELECT COUNT(DISTINCT id) AS {countName} - FROM {Table.Post} - WHERE {Query.whereDataContains "@criteria"} - AND {catIdSql}""" - |> Sql.parameters + Custom.scalar + $"""SELECT COUNT(DISTINCT id) AS {countName} + FROM {Table.Post} + WHERE {Query.whereDataContains "@criteria"} + AND {catIdSql}""" [ "@criteria", Query.jsonbDocParam {| webLogDoc webLogId with Status = PostStatus.toString Published |} catIdParams - ] - |> Sql.executeRowAsync Map.toCount + ] Map.toCount |> Async.AwaitTask |> Async.RunSynchronously it.Id, postCount) diff --git a/src/MyWebLog.Data/Postgres/PostgresHelpers.fs b/src/MyWebLog.Data/Postgres/PostgresHelpers.fs index 9204ab9..765e669 100644 --- a/src/MyWebLog.Data/Postgres/PostgresHelpers.fs +++ b/src/MyWebLog.Data/Postgres/PostgresHelpers.fs @@ -168,14 +168,11 @@ module Document = /// Determine whether a document exists with the given key for the given web log let existsByWebLog<'TKey> table (key : 'TKey) (keyFunc : 'TKey -> string) webLogId = - Configuration.dataSource () - |> Sql.fromDataSource - |> Sql.query $""" - SELECT EXISTS ( - SELECT 1 FROM %s{table} WHERE id = @id AND {Query.whereDataContains "@criteria"} - ) AS {existsName}""" - |> Sql.parameters [ "@id", Sql.string (keyFunc key); webLogContains webLogId ] - |> Sql.executeRowAsync Map.toExists + Custom.scalar + $""" SELECT EXISTS ( + SELECT 1 FROM %s{table} WHERE id = @id AND {Query.whereDataContains "@criteria"} + ) AS {existsName}""" + [ "@id", Sql.string (keyFunc key); webLogContains webLogId ] Map.toExists /// Find a document by its ID for the given web log let findByIdAndWebLog<'TKey, 'TDoc> table (key : 'TKey) (keyFunc : 'TKey -> string) webLogId = diff --git a/src/MyWebLog.Data/Postgres/PostgresPostData.fs b/src/MyWebLog.Data/Postgres/PostgresPostData.fs index 400c1fd..d3791de 100644 --- a/src/MyWebLog.Data/Postgres/PostgresPostData.fs +++ b/src/MyWebLog.Data/Postgres/PostgresPostData.fs @@ -38,13 +38,7 @@ type PostgresPostData (log : ILogger) = /// Count posts in a status for the given web log let countByStatus status webLogId = log.LogTrace "Post.countByStatus" - Configuration.dataSource () - |> Sql.fromDataSource - |> Sql.query - $"""SELECT COUNT(id) AS {countName} FROM {Table.Post} WHERE {Query.whereDataContains "@criteria"}""" - |> Sql.parameters - [ "@criteria", Query.jsonbDocParam {| webLogDoc webLogId with Status = PostStatus.toString status |} ] - |> Sql.executeRowAsync Map.toCount + Count.byContains Table.Post {| webLogDoc webLogId with Status = PostStatus.toString status |} /// Find a post by its ID for the given web log (excluding revisions) let findById postId webLogId = diff --git a/src/MyWebLog.Data/Postgres/PostgresWebLogUserData.fs b/src/MyWebLog.Data/Postgres/PostgresWebLogUserData.fs index fd15654..80eeee3 100644 --- a/src/MyWebLog.Data/Postgres/PostgresWebLogUserData.fs +++ b/src/MyWebLog.Data/Postgres/PostgresWebLogUserData.fs @@ -21,14 +21,11 @@ type PostgresWebLogUserData (log : ILogger) = | Some _ -> let criteria = Query.whereDataContains "@criteria" let! isAuthor = - Configuration.dataSource () - |> Sql.fromDataSource - |> Sql.query $" - SELECT ( EXISTS (SELECT 1 FROM {Table.Page} WHERE {criteria} - OR EXISTS (SELECT 1 FROM {Table.Post} WHERE {criteria})) - AS {existsName}" - |> Sql.parameters [ "@criteria", Query.jsonbDocParam {| AuthorId = userId |} ] - |> Sql.executeRowAsync Map.toExists + Custom.scalar + $" SELECT ( EXISTS (SELECT 1 FROM {Table.Page} WHERE {criteria} + OR EXISTS (SELECT 1 FROM {Table.Post} WHERE {criteria}) + ) AS {existsName}" + [ "@criteria", Query.jsonbDocParam {| AuthorId = userId |} ] Map.toExists if isAuthor then return Error "User has pages or posts; cannot delete" else