Skip to content

Commit

Permalink
Use composite key in statuses table
Browse files Browse the repository at this point in the history
  • Loading branch information
jocmp committed Dec 30, 2023
1 parent e2ccb05 commit 970bdc9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import kotlin.Boolean;

CREATE TABLE article_statuses (
id INTEGER NOT NULL PRIMARY KEY,
article_id INTEGER NOT NULL REFERENCES articles(id),
feed_id INTEGER NOT NULL REFERENCES feeds(id),
external_id INTEGER NOT NULL REFERENCES articles(external_id),
read INTEGER AS Boolean DEFAULT 0,
starred INTEGER AS Boolean DEFAULT 0,
arrived_at INTEGER NOT NULL
arrived_at INTEGER NOT NULL,
PRIMARY KEY (feed_id, external_id)
);
42 changes: 25 additions & 17 deletions basil/src/main/sqldelight/com/jocmp/basil/db/articles.sq
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ CREATE UNIQUE INDEX articles_external_id_index ON articles(external_id);
countByStatus:
SELECT COUNT(*)
FROM articles
JOIN article_statuses ON articles.id = article_statuses.article_id
JOIN article_statuses ON articles.external_id = article_statuses.external_id
WHERE article_statuses.read IN :read
AND article_statuses.starred IN :starred;

Expand All @@ -26,7 +26,7 @@ SELECT
article_statuses.starred,
article_statuses.read
FROM articles
JOIN article_statuses ON articles.id = article_statuses.article_id
JOIN article_statuses ON articles.external_id = article_statuses.external_id AND article_statuses.feed_id = articles.feed_id
AND article_statuses.read IN :read
AND article_statuses.starred IN :starred
ORDER BY article_statuses.arrived_at DESC
Expand All @@ -35,8 +35,8 @@ LIMIT :limit OFFSET :offset;
countByFeeds:
SELECT COUNT(*)
FROM articles
JOIN article_statuses ON articles.id = article_statuses.article_id
WHERE feed_id IN :feedIDs
JOIN article_statuses ON articles.external_id = article_statuses.external_id AND article_statuses.feed_id = articles.feed_id
WHERE articles.feed_id IN :feedIDs
AND article_statuses.read IN :read
AND article_statuses.starred IN :starred;

Expand All @@ -47,8 +47,8 @@ SELECT
article_statuses.starred,
article_statuses.read
FROM articles
JOIN article_statuses ON articles.id = article_statuses.article_id
WHERE feed_id IN :feedIDs
JOIN article_statuses ON articles.external_id = article_statuses.external_id AND article_statuses.feed_id = articles.feed_id
WHERE articles.feed_id IN :feedIDs
AND article_statuses.read IN :read
AND article_statuses.starred IN :starred
ORDER BY article_statuses.arrived_at DESC
Expand All @@ -61,7 +61,7 @@ SELECT
article_statuses.starred,
article_statuses.read
FROM articles
JOIN article_statuses ON articles.id = article_statuses.article_id
JOIN article_statuses ON articles.external_id = article_statuses.external_id AND article_statuses.feed_id = articles.feed_id
WHERE articles.id = :articleID
LIMIT 1;

Expand All @@ -77,16 +77,24 @@ create {
published_at
)
VALUES (
?,
?,
?,
?,
?,
?,
?,
?
:feed_id,
:external_id,
:title,
:content_html,
:url,
:summary,
:image_url,
:published_at
);

INSERT OR IGNORE INTO article_statuses(article_id, arrived_at)
VALUES (last_insert_rowid(), ?);
INSERT OR IGNORE INTO article_statuses(
feed_id,
external_id,
arrived_at
)
VALUES (
:feed_id,
:external_id,
:arrived_at
);
}

0 comments on commit 970bdc9

Please sign in to comment.