From d4c0e4e26c53ce2a3856850f20254caaa63420fe Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Mon, 19 Aug 2024 22:45:14 -0400 Subject: [PATCH] Tweak PostgreSQL calls --- .../Postgres/PostgresCategoryData.fs | 8 +++---- src/MyWebLog.Data/Postgres/PostgresHelpers.fs | 6 +---- .../Postgres/PostgresPageData.fs | 12 +++++----- .../Postgres/PostgresPostData.fs | 24 +++++++++---------- .../Postgres/PostgresTagMapData.fs | 5 +--- .../Postgres/PostgresThemeData.fs | 10 ++++---- .../Postgres/PostgresWebLogData.fs | 22 ++++++++--------- .../Postgres/PostgresWebLogUserData.fs | 4 ++-- 8 files changed, 42 insertions(+), 49 deletions(-) diff --git a/src/MyWebLog.Data/Postgres/PostgresCategoryData.fs b/src/MyWebLog.Data/Postgres/PostgresCategoryData.fs index e93f364..1c1eec5 100644 --- a/src/MyWebLog.Data/Postgres/PostgresCategoryData.fs +++ b/src/MyWebLog.Data/Postgres/PostgresCategoryData.fs @@ -29,7 +29,7 @@ type PostgresCategoryData(log: ILogger) = log.LogTrace "Category.findAllForView" let! cats = Custom.list - $"{selectWithCriteria Table.Category} ORDER BY LOWER(data ->> '{nameof Category.Empty.Name}')" + $"{selectWithCriteria Table.Category} ORDER BY LOWER(data->>'{nameof Category.Empty.Name}')" [ webLogContains webLogId ] fromData let ordered = Utils.orderByHierarchy cats None None [] @@ -46,7 +46,7 @@ type PostgresCategoryData(log: ILogger) = |> arrayContains (nameof Post.Empty.CategoryIds) id let postCount = Custom.scalar - $"""SELECT COUNT(DISTINCT data ->> '{nameof Post.Empty.Id}') AS it + $"""SELECT COUNT(DISTINCT data->>'{nameof Post.Empty.Id}') AS it FROM {Table.Post} WHERE {Query.whereDataContains "@criteria"} AND {catIdSql}""" @@ -74,7 +74,7 @@ type PostgresCategoryData(log: ILogger) = /// Find all categories for the given web log let findByWebLog webLogId = log.LogTrace "Category.findByWebLog" - Document.findByWebLog Table.Category webLogId + Find.byContains Table.Category (webLogDoc webLogId) /// Delete a category let delete catId webLogId = backgroundTask { @@ -103,7 +103,7 @@ type PostgresCategoryData(log: ILogger) = // Delete the category off all posts where it is assigned let! posts = Custom.list - $"SELECT data FROM {Table.Post} WHERE data -> '{nameof Post.Empty.CategoryIds}' @> @id" + $"SELECT data FROM {Table.Post} WHERE data->'{nameof Post.Empty.CategoryIds}' @> @id" [ jsonParam "@id" [| string catId |] ] fromData if not (List.isEmpty posts) then diff --git a/src/MyWebLog.Data/Postgres/PostgresHelpers.fs b/src/MyWebLog.Data/Postgres/PostgresHelpers.fs index a37efe1..1e2b483 100644 --- a/src/MyWebLog.Data/Postgres/PostgresHelpers.fs +++ b/src/MyWebLog.Data/Postgres/PostgresHelpers.fs @@ -165,10 +165,6 @@ module Document = $"""{Query.find table} WHERE {Query.whereById "@id"} AND {Query.whereDataContains "@criteria"}""" [ "@id", Sql.string (string key); webLogContains webLogId ] fromData<'TDoc> - - /// Find documents for the given web log - let findByWebLog<'TDoc> table webLogId : Task<'TDoc list> = - Find.byContains table (webLogDoc webLogId) /// Functions to support revisions @@ -186,7 +182,7 @@ module Revisions = Custom.list $"""SELECT pr.* FROM %s{revTable} pr - INNER JOIN %s{entityTable} p ON p.data ->> '{nameof Post.Empty.Id}' = pr.{entityTable}_id + INNER JOIN %s{entityTable} p ON p.data->>'{nameof Post.Empty.Id}' = pr.{entityTable}_id WHERE p.{Query.whereDataContains "@criteria"} ORDER BY as_of DESC""" [ webLogContains webLogId ] diff --git a/src/MyWebLog.Data/Postgres/PostgresPageData.fs b/src/MyWebLog.Data/Postgres/PostgresPageData.fs index 3c6e023..d14da27 100644 --- a/src/MyWebLog.Data/Postgres/PostgresPageData.fs +++ b/src/MyWebLog.Data/Postgres/PostgresPageData.fs @@ -47,7 +47,7 @@ type PostgresPageData(log: ILogger) = let all webLogId = log.LogTrace "Page.all" Custom.list - $"{selectWithCriteria Table.Page} ORDER BY LOWER(data ->> '{nameof Page.Empty.Title}')" + $"{selectWithCriteria Table.Page} ORDER BY LOWER(data->>'{nameof Page.Empty.Title}')" [ webLogContains webLogId ] (fun row -> { fromData row with Text = ""; Metadata = []; PriorPermalinks = [] }) @@ -86,8 +86,8 @@ type PostgresPageData(log: ILogger) = match! pageExists pageId webLogId with | true -> do! Custom.nonQuery - $"""DELETE FROM {Table.PageRevision} WHERE page_id = @id; - DELETE FROM {Table.Page} WHERE {Query.whereById "@id"}""" + $"""{Query.delete Table.PageRevision} WHERE page_id = @id; + {Query.delete Table.Page} WHERE {Query.whereById "@id"}""" [ idParam pageId ] return true | false -> return false @@ -110,7 +110,7 @@ type PostgresPageData(log: ILogger) = let linkSql, linkParam = arrayContains (nameof Page.Empty.PriorPermalinks) string permalinks return! Custom.single - $"""SELECT data ->> '{nameof Page.Empty.Permalink}' AS permalink + $"""SELECT data->>'{nameof Page.Empty.Permalink}' AS permalink FROM page WHERE {Query.whereDataContains "@criteria"} AND {linkSql}""" @@ -121,7 +121,7 @@ type PostgresPageData(log: ILogger) = /// Get all complete pages for the given web log let findFullByWebLog webLogId = backgroundTask { log.LogTrace "Page.findFullByWebLog" - let! pages = Document.findByWebLog Table.Page webLogId + let! pages = Find.byContains Table.Page (webLogDoc webLogId) let! revisions = Revisions.findByWebLog Table.PageRevision Table.Page PageId webLogId return pages @@ -133,7 +133,7 @@ type PostgresPageData(log: ILogger) = let findListed webLogId = log.LogTrace "Page.findListed" Custom.list - $"{selectWithCriteria Table.Page} ORDER BY LOWER(data ->> '{nameof Page.Empty.Title}')" + $"{selectWithCriteria Table.Page} ORDER BY LOWER(data->>'{nameof Page.Empty.Title}')" [ jsonParam "@criteria" {| webLogDoc webLogId with IsInPageList = true |} ] pageWithoutText diff --git a/src/MyWebLog.Data/Postgres/PostgresPostData.fs b/src/MyWebLog.Data/Postgres/PostgresPostData.fs index 7724ca0..b986280 100644 --- a/src/MyWebLog.Data/Postgres/PostgresPostData.fs +++ b/src/MyWebLog.Data/Postgres/PostgresPostData.fs @@ -84,9 +84,9 @@ type PostgresPostData(log: ILogger) = match! postExists postId webLogId with | true -> do! Custom.nonQuery - $"""DELETE FROM {Table.PostComment} WHERE {Query.whereDataContains "@criteria"}; - DELETE FROM {Table.PostRevision} WHERE post_id = @id; - DELETE FROM {Table.Post} WHERE {Query.whereById "@id"}""" + $"""{Query.delete Table.PostComment} WHERE {Query.whereDataContains "@criteria"}; + {Query.delete Table.PostRevision} WHERE post_id = @id; + {Query.delete Table.Post} WHERE {Query.whereById "@id"}""" [ idParam postId; jsonParam "@criteria" {| PostId = postId |} ] return true | false -> return false @@ -100,7 +100,7 @@ type PostgresPostData(log: ILogger) = let linkSql, linkParam = arrayContains (nameof Post.Empty.PriorPermalinks) string permalinks return! Custom.single - $"""SELECT data ->> '{nameof Post.Empty.Permalink}' AS permalink + $"""SELECT data->>'{nameof Post.Empty.Permalink}' AS permalink FROM {Table.Post} WHERE {Query.whereDataContains "@criteria"} AND {linkSql}""" @@ -111,7 +111,7 @@ type PostgresPostData(log: ILogger) = /// Get all complete posts for the given web log let findFullByWebLog webLogId = backgroundTask { log.LogTrace "Post.findFullByWebLog" - let! posts = Document.findByWebLog Table.Post webLogId + let! posts = Find.byContains Table.Post (webLogDoc webLogId) let! revisions = Revisions.findByWebLog Table.PostRevision Table.Post PostId webLogId return posts @@ -126,7 +126,7 @@ type PostgresPostData(log: ILogger) = Custom.list $"{selectWithCriteria Table.Post} AND {catSql} - ORDER BY data ->> '{nameof Post.Empty.PublishedOn}' DESC + ORDER BY data->>'{nameof Post.Empty.PublishedOn}' DESC LIMIT {postsPerPage + 1} OFFSET {(pageNbr - 1) * postsPerPage}" [ jsonParam "@criteria" {| webLogDoc webLogId with Status = Published |}; catParam ] postWithoutLinks @@ -136,8 +136,8 @@ type PostgresPostData(log: ILogger) = log.LogTrace "Post.findPageOfPosts" Custom.list $"{selectWithCriteria Table.Post} - ORDER BY data ->> '{nameof Post.Empty.PublishedOn}' DESC NULLS FIRST, - data ->> '{nameof Post.Empty.UpdatedOn}' + ORDER BY data->>'{nameof Post.Empty.PublishedOn}' DESC NULLS FIRST, + data->>'{nameof Post.Empty.UpdatedOn}' LIMIT {postsPerPage + 1} OFFSET {(pageNbr - 1) * postsPerPage}" [ webLogContains webLogId ] postWithoutText @@ -147,7 +147,7 @@ type PostgresPostData(log: ILogger) = log.LogTrace "Post.findPageOfPublishedPosts" Custom.list $"{selectWithCriteria Table.Post} - ORDER BY data ->> '{nameof Post.Empty.PublishedOn}' DESC + ORDER BY data->>'{nameof Post.Empty.PublishedOn}' DESC LIMIT {postsPerPage + 1} OFFSET {(pageNbr - 1) * postsPerPage}" [ jsonParam "@criteria" {| webLogDoc webLogId with Status = Published |} ] postWithoutLinks @@ -158,7 +158,7 @@ type PostgresPostData(log: ILogger) = Custom.list $"{selectWithCriteria Table.Post} AND data['{nameof Post.Empty.Tags}'] @> @tag - ORDER BY data ->> '{nameof Post.Empty.PublishedOn}' DESC + ORDER BY data->>'{nameof Post.Empty.PublishedOn}' DESC LIMIT {postsPerPage + 1} OFFSET {(pageNbr - 1) * postsPerPage}" [ jsonParam "@criteria" {| webLogDoc webLogId with Status = Published |}; jsonParam "@tag" [| tag |] ] postWithoutLinks @@ -171,8 +171,8 @@ type PostgresPostData(log: ILogger) = "@publishedOn", Sql.timestamptz (publishedOn.ToDateTimeOffset()) ] let query op direction = $"{selectWithCriteria Table.Post} - AND (data ->> '{nameof Post.Empty.PublishedOn}')::timestamp with time zone %s{op} @publishedOn - ORDER BY data ->> '{nameof Post.Empty.PublishedOn}' %s{direction} + AND (data->>'{nameof Post.Empty.PublishedOn}')::timestamp with time zone %s{op} @publishedOn + ORDER BY data->>'{nameof Post.Empty.PublishedOn}' %s{direction} LIMIT 1" let! older = Custom.list (query "<" "DESC") (queryParams ()) postWithoutLinks let! newer = Custom.list (query ">" "") (queryParams ()) postWithoutLinks diff --git a/src/MyWebLog.Data/Postgres/PostgresTagMapData.fs b/src/MyWebLog.Data/Postgres/PostgresTagMapData.fs index 2369a6f..ca160f3 100644 --- a/src/MyWebLog.Data/Postgres/PostgresTagMapData.fs +++ b/src/MyWebLog.Data/Postgres/PostgresTagMapData.fs @@ -33,10 +33,7 @@ type PostgresTagMapData(log: ILogger) = /// Get all tag mappings for the given web log let findByWebLog webLogId = log.LogTrace "TagMap.findByWebLog" - Custom.list - $"{selectWithCriteria Table.TagMap} ORDER BY data ->> 'tag'" - [ webLogContains webLogId ] - fromData + Find.byContainsOrdered Table.TagMap (webLogDoc webLogId) [ Field.Named (nameof TagMap.Empty.Tag) ] /// Find any tag mappings in a list of tags for the given web log let findMappingForTags tags webLogId = diff --git a/src/MyWebLog.Data/Postgres/PostgresThemeData.fs b/src/MyWebLog.Data/Postgres/PostgresThemeData.fs index cb953bc..6942f3b 100644 --- a/src/MyWebLog.Data/Postgres/PostgresThemeData.fs +++ b/src/MyWebLog.Data/Postgres/PostgresThemeData.fs @@ -19,8 +19,8 @@ type PostgresThemeData(log: ILogger) = log.LogTrace "Theme.all" Custom.list $"{Query.find Table.Theme} - WHERE data ->> '{nameof Theme.Empty.Id}' <> 'admin' - ORDER BY data ->> '{nameof Theme.Empty.Id}'" + WHERE data->>'{nameof Theme.Empty.Id}' <> 'admin' + ORDER BY data->>'{nameof Theme.Empty.Id}'" [] withoutTemplateText @@ -45,8 +45,8 @@ type PostgresThemeData(log: ILogger) = match! exists themeId with | true -> do! Custom.nonQuery - $"""DELETE FROM {Table.ThemeAsset} WHERE theme_id = @id; - DELETE FROM {Table.Theme} WHERE {Query.whereById "@id"}""" + $"""{Query.delete Table.ThemeAsset} WHERE theme_id = @id; + {Query.delete Table.Theme} WHERE {Query.whereById "@id"}""" [ idParam themeId ] return true | false -> return false @@ -77,7 +77,7 @@ type PostgresThemeAssetData(log: ILogger) = /// Delete all assets for the given theme let deleteByTheme (themeId: ThemeId) = log.LogTrace "ThemeAsset.deleteByTheme" - Custom.nonQuery $"DELETE FROM {Table.ThemeAsset} WHERE theme_id = @id" [ idParam themeId ] + Custom.nonQuery $"{Query.delete Table.ThemeAsset} WHERE theme_id = @id" [ idParam themeId ] /// Find a theme asset by its ID let findById assetId = diff --git a/src/MyWebLog.Data/Postgres/PostgresWebLogData.fs b/src/MyWebLog.Data/Postgres/PostgresWebLogData.fs index 0d7835e..8aca94b 100644 --- a/src/MyWebLog.Data/Postgres/PostgresWebLogData.fs +++ b/src/MyWebLog.Data/Postgres/PostgresWebLogData.fs @@ -23,22 +23,22 @@ type PostgresWebLogData(log: ILogger) = let delete webLogId = log.LogTrace "WebLog.delete" Custom.nonQuery - $"""DELETE FROM {Table.PostComment} - WHERE data->>'{nameof Comment.Empty.PostId}' - IN (SELECT data->>'{nameof Post.Empty.Id}' - FROM {Table.Post} - WHERE {Query.whereDataContains "@criteria"}); - DELETE FROM {Table.PostRevision} - WHERE post_id IN (SELECT data->>'Id' FROM {Table.Post} WHERE {Query.whereDataContains "@criteria"}); - DELETE FROM {Table.PageRevision} - WHERE page_id IN (SELECT data->>'Id' FROM {Table.Page} WHERE {Query.whereDataContains "@criteria"}); + $"""{Query.delete Table.PostComment} + WHERE data->>'{nameof Comment.Empty.PostId}' + IN (SELECT data->>'{nameof Post.Empty.Id}' + FROM {Table.Post} + WHERE {Query.whereDataContains "@criteria"}); + {Query.delete Table.PostRevision} + WHERE post_id IN (SELECT data->>'Id' FROM {Table.Post} WHERE {Query.whereDataContains "@criteria"}); + {Query.delete Table.PageRevision} + WHERE page_id IN (SELECT data->>'Id' FROM {Table.Page} WHERE {Query.whereDataContains "@criteria"}); {Query.byContains (Query.delete Table.Post)}; {Query.byContains (Query.delete Table.Page)}; {Query.byContains (Query.delete Table.Category)}; {Query.byContains (Query.delete Table.TagMap)}; {Query.byContains (Query.delete Table.WebLogUser)}; - DELETE FROM {Table.Upload} WHERE web_log_id = @webLogId; - DELETE FROM {Table.WebLog} WHERE data->>'Id' = @webLogId""" + {Query.delete Table.Upload} WHERE web_log_id = @webLogId; + {Query.delete Table.WebLog} WHERE data->>'Id' = @webLogId""" [ webLogIdParam webLogId; webLogContains webLogId ] /// Find a web log by its host (URL base) diff --git a/src/MyWebLog.Data/Postgres/PostgresWebLogUserData.fs b/src/MyWebLog.Data/Postgres/PostgresWebLogUserData.fs index 8ccf5be..caca8ba 100644 --- a/src/MyWebLog.Data/Postgres/PostgresWebLogUserData.fs +++ b/src/MyWebLog.Data/Postgres/PostgresWebLogUserData.fs @@ -50,14 +50,14 @@ type PostgresWebLogUserData(log: ILogger) = let findByWebLog webLogId = log.LogTrace "WebLogUser.findByWebLog" Custom.list - $"{selectWithCriteria Table.WebLogUser} ORDER BY LOWER(data ->> '{nameof WebLogUser.Empty.PreferredName}')" + $"{selectWithCriteria Table.WebLogUser} ORDER BY LOWER(data->>'{nameof WebLogUser.Empty.PreferredName}')" [ webLogContains webLogId ] fromData /// Find the names of users by their IDs for the given web log let findNames webLogId (userIds: WebLogUserId list) = backgroundTask { log.LogTrace "WebLogUser.findNames" - let idSql, idParams = inClause $"AND data ->> '{nameof WebLogUser.Empty.Id}'" "id" userIds + let idSql, idParams = inClause $"AND data->>'{nameof WebLogUser.Empty.Id}'" "id" userIds let! users = Custom.list $"{selectWithCriteria Table.WebLogUser} {idSql}"