From 974aff7a098052831b1479ab2ab2f2bd0d307ca6 Mon Sep 17 00:00:00 2001 From: Will Brown Date: Sun, 24 Nov 2024 21:26:19 -0500 Subject: [PATCH] Add zblock_feed_info_count_channel() --- feed_info.c | 24 ++++++++++++++++++++++++ feed_info.h | 3 +++ 2 files changed, 27 insertions(+) diff --git a/feed_info.c b/feed_info.c index 8a2e3e2..d088ce4 100644 --- a/feed_info.c +++ b/feed_info.c @@ -157,3 +157,27 @@ zblock_feed_info_err zblock_feed_info_update(PGconn *conn, zblock_feed_info_mini PQclear(update_res); return result; } + +// returns the number of feeds in a channel in count +zblock_feed_info_err zblock_feed_info_count_channel(PGconn *conn, u64snowflake channel_id, int64_t *count) { + if (!conn || !count) return ZBLOCK_FEED_INFO_INVALID_ARGS; + + uint64_t channel_id_be = htobe64(channel_id); + const char *const params[] = {(char *) channel_id_be}; + const int param_lengths[] = {sizeof(channel_id_be)}; + const int param_formats[] = {1}; + PGresult *res = PQexecParams(conn, + "SELECT COUNT(*) FROM feeds WHERE channel_id = $1::bigint", + 1, NULL, params, param_lengths, param_formats, 1 + ); + + if (PQresultStatus(res) != PGRES_TUPLES_OK) { + log_error(PQresultErrorMessage(res)); + PQclear(res); + return ZBLOCK_FEED_INFO_DBERROR; + } + + *count = be64toh(*(uint64_t *) PQgetvalue(res, 0, 0)); + PQclear(res); + return ZBLOCK_FEED_INFO_OK; +} diff --git a/feed_info.h b/feed_info.h index be57fa7..bf9888c 100644 --- a/feed_info.h +++ b/feed_info.h @@ -48,4 +48,7 @@ zblock_feed_info_err zblock_feed_info_delete(PGconn *conn, const char *url, u64s // updates the last_pubDate field of a given feed in the database zblock_feed_info_err zblock_feed_info_update(PGconn *conn, zblock_feed_info_minimal *feed); +// returns the number of feeds in a channel in count +zblock_feed_info_err zblock_feed_info_count_channel(PGconn *conn, u64snowflake channel_id, int64_t *count) + #endif