Incorporate doc lib v4 ordering

This commit is contained in:
2024-08-22 23:00:25 -04:00
parent d4c0e4e26c
commit cc3e41ddc5
15 changed files with 184 additions and 181 deletions

View File

@@ -16,16 +16,18 @@ type SQLiteCategoryData(conn: SqliteConnection, ser: JsonSerializer, log: ILogge
let parentIdField = nameof Category.Empty.ParentId
/// Count all categories for the given web log
let countAll webLogId =
let countAll webLogId = backgroundTask {
log.LogTrace "Category.countAll"
Document.countByWebLog Table.Category webLogId conn
let! count = conn.countByFields Table.Category Any [ webLogField webLogId ]
return int count
}
/// Count all top-level categories for the given web log
let countTopLevel webLogId =
let countTopLevel webLogId = backgroundTask {
log.LogTrace "Category.countTopLevel"
let fields = [ webLogField webLogId; Field.NEX parentIdField ]
conn.customScalar
(Query.byFields (Query.count Table.Category) All fields) (addFieldParams fields []) (toCount >> int)
let! count = conn.countByFields Table.Category All [ webLogField webLogId; Field.NEX parentIdField ]
return int count
}
/// Find all categories for the given web log
let findByWebLog webLogId =

View File

@@ -251,12 +251,6 @@ module Document =
/// A query to select from a table by its web log ID
let selectByWebLog table =
Query.statementWhere (Query.find table) whereByWebLog
/// Count documents for the given web log ID
let countByWebLog table (webLogId: WebLogId) conn = backgroundTask {
let! count = Count.byFields table Any [ webLogField webLogId ] conn
return int count
}
/// Functions to support revisions

View File

@@ -17,8 +17,10 @@ type SQLitePageData(conn: SqliteConnection, log: ILogger) =
/// The JSON field name for the "is in page list" flag
let pgListName = nameof Page.Empty.IsInPageList
/// The JSON field for the title of the page
let titleField = $"data->>'{nameof Page.Empty.Title}'"
/// Query to return pages sorted by title
let sortedPages fields =
Query.byFields (Query.find Table.Page) All fields
+ Query.orderBy [ Field.Named $"i:{nameof Page.Empty.Title}" ] SQLite
// SUPPORT FUNCTIONS
@@ -52,21 +54,23 @@ type SQLitePageData(conn: SqliteConnection, log: ILogger) =
log.LogTrace "Page.all"
let field = [ webLogField webLogId ]
conn.customList
$"{Query.byFields (Query.find Table.Page) Any field} ORDER BY LOWER({titleField})"
(sortedPages field)
(addFieldParams field [])
(fun rdr -> { fromData<Page> rdr with Text = ""; Metadata = []; PriorPermalinks = [] })
/// Count all pages for the given web log
let countAll webLogId =
let countAll webLogId = backgroundTask {
log.LogTrace "Page.countAll"
Document.countByWebLog Table.Page webLogId conn
let! count = conn.countByFields Table.Page Any [ webLogField webLogId ]
return int count
}
/// Count all pages shown in the page list for the given web log
let countListed webLogId =
let countListed webLogId = backgroundTask {
log.LogTrace "Page.countListed"
let fields = [ webLogField webLogId; Field.EQ pgListName true ]
conn.customScalar
(Query.byFields (Query.count Table.Page) All fields) (addFieldParams fields []) (toCount >> int)
let! count = conn.countByFields Table.Page All [ webLogField webLogId; Field.EQ pgListName true ]
return int count
}
/// Find a page by its ID (without revisions and prior permalinks)
let findById (pageId: PageId) webLogId = backgroundTask {
@@ -131,17 +135,14 @@ type SQLitePageData(conn: SqliteConnection, log: ILogger) =
log.LogTrace "Page.findListed"
let fields = [ webLogField webLogId; Field.EQ pgListName true ]
conn.customList
$"{Query.byFields (Query.find Table.Page) All fields} ORDER BY LOWER({titleField})"
(addFieldParams fields [])
(fun rdr -> { fromData<Page> rdr with Text = "" })
(sortedPages fields) (addFieldParams fields []) (fun rdr -> { fromData<Page> rdr with Text = "" })
/// Get a page of pages for the given web log (without revisions)
let findPageOfPages webLogId pageNbr =
log.LogTrace "Page.findPageOfPages"
let field = [ webLogField webLogId ]
conn.customList
$"{Query.byFields (Query.find Table.Page) Any field} ORDER BY LOWER({titleField})
LIMIT @pageSize OFFSET @toSkip"
$"{sortedPages field} LIMIT @pageSize OFFSET @toSkip"
(addFieldParams field [ sqlParam "@pageSize" 26; sqlParam "@toSkip" ((pageNbr - 1) * 25) ])
(fun rdr -> { pageWithoutLinks rdr with Metadata = [] })

View File

@@ -16,7 +16,7 @@ type SQLitePostData(conn: SqliteConnection, log: ILogger) =
let linkName = nameof Post.Empty.Permalink
/// The JSON field for when the post was published
let publishField = $"data->>'{nameof Post.Empty.PublishedOn}'"
let publishName = nameof Post.Empty.PublishedOn
/// The name of the JSON field for the post's status
let statName = nameof Post.Empty.Status
@@ -60,11 +60,11 @@ type SQLitePostData(conn: SqliteConnection, log: ILogger) =
}
/// Count posts in a status for the given web log
let countByStatus (status: PostStatus) webLogId =
let countByStatus (status: PostStatus) webLogId = backgroundTask {
log.LogTrace "Post.countByStatus"
let fields = [ webLogField webLogId; Field.EQ statName (string status) ]
conn.customScalar
(Query.byFields (Query.count Table.Post) All fields) (addFieldParams fields []) (toCount >> int)
let! count = conn.countByFields Table.Post All [ webLogField webLogId; Field.EQ statName (string status) ]
return int count
}
/// Find a post by its ID for the given web log (excluding revisions)
let findById (postId: PostId) webLogId = backgroundTask {
@@ -132,19 +132,20 @@ type SQLitePostData(conn: SqliteConnection, log: ILogger) =
log.LogTrace "Post.findPageOfCategorizedPosts"
let catSql, catParams = inJsonArray Table.Post (nameof Post.Empty.CategoryIds) "catId" categoryIds
conn.customList
$"{publishedPostByWebLog} AND {catSql}
ORDER BY {publishField} DESC
LIMIT {postsPerPage + 1} OFFSET {(pageNbr - 1) * postsPerPage}"
$"""{publishedPostByWebLog} AND {catSql}
{Query.orderBy [ Field.Named $"{publishName} DESC" ] SQLite}
LIMIT {postsPerPage + 1} OFFSET {(pageNbr - 1) * postsPerPage}"""
(webLogParam webLogId :: catParams)
postWithoutLinks
/// Get a page of posts for the given web log (excludes text and revisions)
let findPageOfPosts webLogId pageNbr postsPerPage =
log.LogTrace "Post.findPageOfPosts"
let order =
Query.orderBy
[ Field.Named $"{publishName} DESC NULLS FIRST"; Field.Named (nameof Post.Empty.UpdatedOn) ] SQLite
conn.customList
$"{postByWebLog}
ORDER BY {publishField} DESC NULLS FIRST, data->>'{nameof Post.Empty.UpdatedOn}'
LIMIT {postsPerPage + 1} OFFSET {(pageNbr - 1) * postsPerPage}"
$"{postByWebLog}{order} LIMIT {postsPerPage + 1} OFFSET {(pageNbr - 1) * postsPerPage}"
[ webLogParam webLogId ]
postWithoutText
@@ -152,9 +153,9 @@ type SQLitePostData(conn: SqliteConnection, log: ILogger) =
let findPageOfPublishedPosts webLogId pageNbr postsPerPage =
log.LogTrace "Post.findPageOfPublishedPosts"
conn.customList
$"{publishedPostByWebLog}
ORDER BY {publishField} DESC
LIMIT {postsPerPage + 1} OFFSET {(pageNbr - 1) * postsPerPage}"
$"""{publishedPostByWebLog}
{Query.orderBy [ Field.Named $"{publishName} DESC" ] SQLite}
LIMIT {postsPerPage + 1} OFFSET {(pageNbr - 1) * postsPerPage}"""
[ webLogParam webLogId ]
postWithoutLinks
@@ -163,26 +164,28 @@ type SQLitePostData(conn: SqliteConnection, log: ILogger) =
log.LogTrace "Post.findPageOfTaggedPosts"
let tagSql, tagParams = inJsonArray Table.Post (nameof Post.Empty.Tags) "tag" [ tag ]
conn.customList
$"{publishedPostByWebLog} AND {tagSql}
ORDER BY {publishField} DESC
LIMIT {postsPerPage + 1} OFFSET {(pageNbr - 1) * postsPerPage}"
$"""{publishedPostByWebLog} AND {tagSql}
{Query.orderBy [ Field.Named $"{publishName} DESC" ] SQLite}
LIMIT {postsPerPage + 1} OFFSET {(pageNbr - 1) * postsPerPage}"""
(webLogParam webLogId :: tagParams)
postWithoutLinks
/// Find the next newest and oldest post from a publish date for the given web log
let findSurroundingPosts webLogId (publishedOn : Instant) = backgroundTask {
log.LogTrace "Post.findSurroundingPosts"
let parameters = [ webLogParam webLogId; sqlParam "@publishedOn" (instantParam publishedOn) ]
let! older =
let adjacent op order =
let fields = [
webLogField webLogId
Field.EQ (nameof Post.Empty.Status) (string Published)
(if op = "<" then Field.LT else Field.GT) publishName (instantParam publishedOn)
]
conn.customSingle
$"{publishedPostByWebLog} AND {publishField} < @publishedOn ORDER BY {publishField} DESC LIMIT 1"
parameters
postWithoutLinks
let! newer =
conn.customSingle
$"{publishedPostByWebLog} AND {publishField} > @publishedOn ORDER BY {publishField} LIMIT 1"
parameters
(Query.byFields (Query.find Table.Post) All fields
+ Query.orderBy [ Field.Named (publishName + order) ] SQLite + " LIMIT 1")
(addFieldParams fields [])
postWithoutLinks
let! older = adjacent "<" " DESC"
let! newer = adjacent ">" ""
return older, newer
}

View File

@@ -10,8 +10,8 @@ open MyWebLog.Data
/// SQLite myWebLog theme data implementation
type SQLiteThemeData(conn : SqliteConnection, log: ILogger) =
/// The JSON field for the theme ID
let idField = $"data->>'{nameof Theme.Empty.Id}'"
/// The name of the theme ID field
let idName = nameof Theme.Empty.Id
/// Convert a document to a theme with no template text
let withoutTemplateText (rdr: SqliteDataReader) =
@@ -25,7 +25,11 @@ type SQLiteThemeData(conn : SqliteConnection, log: ILogger) =
/// Retrieve all themes (except 'admin'; excludes template text)
let all () =
log.LogTrace "Theme.all"
conn.customList $"{Query.find Table.Theme} WHERE {idField} <> 'admin' ORDER BY {idField}" [] withoutTemplateText
let fields = [ Field.NE idName "admin" ]
conn.customList
(Query.byFields (Query.find Table.Theme) Any fields + Query.orderBy [ Field.Named idName ] SQLite)
(addFieldParams fields [])
withoutTemplateText
/// Does a given theme exist?
let exists (themeId: ThemeId) =