Adds a simple query to the count filter

This commit is contained in:
Vitor Pamplona
2026-01-02 16:58:59 -05:00
parent 989c63a717
commit 51f9fdd58c
@@ -117,6 +117,7 @@ class QueryBuilder(
if (newFilter.isSimpleQuery()) { if (newFilter.isSimpleQuery()) {
return makeSimpleQuery( return makeSimpleQuery(
project = true,
ids = newFilter.ids, ids = newFilter.ids,
authors = newFilter.authors, authors = newFilter.authors,
kinds = newFilter.kinds, kinds = newFilter.kinds,
@@ -250,6 +251,23 @@ class QueryBuilder(
filter: Filter, filter: Filter,
db: SQLiteDatabase, db: SQLiteDatabase,
): Int { ): Int {
val newFilter = filter.toFilterWithDTags()
if (newFilter.isSimpleQuery()) {
val sql =
makeSimpleQuery(
project = false,
ids = newFilter.ids,
authors = newFilter.authors,
kinds = newFilter.kinds,
dTags = newFilter.dTags,
since = newFilter.since,
until = newFilter.until,
limit = newFilter.limit,
)
return db.runCount(sql.sql, sql.args)
}
val rowIdSubQuery = prepareRowIDSubQueries(filter, hasher(db)) val rowIdSubQuery = prepareRowIDSubQueries(filter, hasher(db))
return if (rowIdSubQuery == null) { return if (rowIdSubQuery == null) {
@@ -589,6 +607,7 @@ class QueryBuilder(
} }
private fun makeSimpleQuery( private fun makeSimpleQuery(
project: Boolean,
ids: List<HexKey>? = null, ids: List<HexKey>? = null,
authors: List<HexKey>? = null, authors: List<HexKey>? = null,
kinds: List<Kind>? = null, kinds: List<Kind>? = null,
@@ -624,14 +643,20 @@ class QueryBuilder(
val sql = val sql =
buildString { buildString {
append("SELECT id, pubkey, created_at, kind, tags, content, sig FROM event_headers") if (project) {
append("SELECT id, pubkey, created_at, kind, tags, content, sig FROM event_headers")
} else {
append("SELECT count(*) FROM event_headers")
}
if (clause.conditions.isNotEmpty()) { if (clause.conditions.isNotEmpty()) {
append("\nWHERE ") append("\nWHERE ")
append(clause.conditions) append(clause.conditions)
} }
append("\nORDER BY created_at DESC") if (project) {
if (indexStrategy.useAndIndexIdOnOrderBy) { append("\nORDER BY created_at DESC")
append(", event_headers.id ASC") if (indexStrategy.useAndIndexIdOnOrderBy) {
append(", event_headers.id ASC")
}
} }
if (limit != null) { if (limit != null) {
append("\nLIMIT ") append("\nLIMIT ")