Skip to content

Commit 354dc97

Browse files
committed
Extract queries (WIP)
1 parent 8e86772 commit 354dc97

File tree

8 files changed

+131
-167
lines changed

8 files changed

+131
-167
lines changed

app/src/main/kotlin/enclosures/EnclosuresRepo.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,10 @@ class EnclosuresRepo(
232232

233233
private suspend fun updateLink(link: Link, entry: Entry): Link {
234234
withContext(Dispatchers.IO) {
235+
// updateLinks:
236+
// UPDATE Entry
237+
// SET links = ?
238+
// WHERE id = ?;
235239
db.entryQueries.updateLinks(
236240
id = entry.id,
237241
links = entry.links.map {

app/src/main/kotlin/entries/EntriesRepo.kt

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,26 +174,96 @@ class EntriesRepo(
174174
}
175175

176176
private fun selectMaxUpdated(): Flow<String?> {
177+
// selectMaxUpdated:
178+
// SELECT MAX(updated)
179+
// FROM Entry;
177180
return db.entryQueries.selectMaxUpdated().asFlow().mapToOneOrNull().map { it?.MAX }
178181
}
179182

180183
fun selectByFtsQuery(query: String): Flow<List<SelectByQuery>> {
184+
// -- https://www.sqlite.org/fts5.html
185+
// -- > It is an error to add types
186+
// -- However, SQLDelight insists on typing every field but it will strip types from the real schema
187+
// CREATE VIRTUAL TABLE entry_search USING fts5(
188+
// id TEXT NOT NULL,
189+
// title TEXT,
190+
// summary TEXT,
191+
// content_text TEXT,
192+
// content=Entry,
193+
// tokenize='trigram'
194+
// );
195+
//
196+
// CREATE TRIGGER entry_fts_after_insert AFTER INSERT ON Entry BEGIN
197+
// INSERT
198+
// INTO entry_search(rowid, id, title, summary, content_text)
199+
// VALUES (new.rowid, new.id, new.title, new.summary, new.content_text);
200+
// END;
201+
//
202+
// CREATE TRIGGER entry_fts_after_delete AFTER DELETE ON Entry BEGIN
203+
// INSERT
204+
// INTO entry_search(entry_search, rowid, id, title, summary, content_text)
205+
// VALUES ('delete', old.rowid, old.id, old.title, old.summary, old.content_text);
206+
// END;
207+
//
208+
// CREATE TRIGGER entry_fts_after_update AFTER UPDATE ON Entry BEGIN
209+
// INSERT
210+
// INTO entry_search(entry_search, rowid, id, title, summary, content_text)
211+
// VALUES ('delete', old.rowid, old.id, old.title, old.summary, old.content_text);
212+
//
213+
// INSERT
214+
// INTO entry_search(rowid, id, title, summary, content_text)
215+
// VALUES (new.rowid, new.id, new.title, new.summary, new.content_text);
216+
// END;
217+
//
218+
// selectByQuery:
219+
// SELECT
220+
// e.id,
221+
// f.ext_show_preview_images,
222+
// e.ext_og_image_url,
223+
// e.ext_og_image_width,
224+
// e.ext_og_image_height,
225+
// e.title,
226+
// f.title AS feedTitle,
227+
// e.published,
228+
// e.summary,
229+
// e.ext_read,
230+
// f.ext_open_entries_in_browser,
231+
// e.links
232+
// FROM entry_search es
233+
// JOIN Entry e ON e.id = es.id
234+
// JOIN Feed f ON f.id = e.feed_id
235+
// WHERE es.title LIKE '%' || :query || '%'
236+
// OR es.summary LIKE '%' || :query || '%'
237+
// OR es.content_text LIKE '%' || :query || '%'
238+
// LIMIT 500;
181239
return db.entrySearchQueries.selectByQuery(query).asFlow().mapToList()
182240
}
183241

184242
suspend fun updateReadByFeedId(read: Boolean, feedId: String) {
243+
// updateReadByFeedId:
244+
// UPDATE Entry
245+
// SET ext_read = :read, ext_read_synced = 0
246+
// WHERE ext_read != :read AND feed_id = :feedId;
185247
withContext(Dispatchers.IO) {
186248
db.entryQueries.updateReadByFeedId(read, feedId)
187249
}
188250
}
189251

190252
suspend fun updateReadByBookmarked(read: Boolean, bookmarked: Boolean) {
253+
// updateReadByBookmarked:
254+
// UPDATE Entry
255+
// SET ext_read = :read, ext_read_synced = 0
256+
// WHERE ext_read != :read AND ext_bookmarked = :bookmarked;
191257
withContext(Dispatchers.IO) {
192258
db.entryQueries.updateReadByBookmarked(read = read, bookmarked = bookmarked)
193259
}
194260
}
195261

196262
suspend fun updateReadAndReadSynced(id: String, read: Boolean, readSynced: Boolean) {
263+
// updateReadAndReadSynced:
264+
// UPDATE Entry
265+
// SET ext_read = ?, ext_read_synced = ?
266+
// WHERE id = ?;
197267
withContext(Dispatchers.IO) {
198268
db.entryQueries.updateReadAndReadSynced(
199269
id = id,
@@ -208,6 +278,10 @@ class EntriesRepo(
208278
bookmarked: Boolean,
209279
bookmarkedSynced: Boolean,
210280
) {
281+
// updateBookmarkedAndBookmaredSynced:
282+
// UPDATE Entry
283+
// SET ext_bookmarked = ?, ext_bookmarked_synced = ?
284+
// WHERE id = ?;
211285
withContext(Dispatchers.IO) {
212286
db.entryQueries.updateBookmarkedAndBookmaredSynced(
213287
id = id,
@@ -253,6 +327,10 @@ class EntriesRepo(
253327

254328
db.entryQueries.transaction {
255329
unsyncedReadEntries.forEach {
330+
// updateReadSynced:
331+
// UPDATE Entry
332+
// SET ext_read_synced = ?
333+
// WHERE id = ?;
256334
db.entryQueries.updateReadSynced(true, it.id)
257335
}
258336
}
@@ -301,6 +379,10 @@ class EntriesRepo(
301379

302380
db.entryQueries.transaction {
303381
notSyncedBookmarkedEntries.forEach {
382+
// updateBookmarkedSynced:
383+
// UPDATE Entry
384+
// SET ext_bookmarked_synced = ?
385+
// WHERE id = ?;
304386
db.entryQueries.updateBookmarkedSynced(true, it.id)
305387
}
306388
}

app/src/main/kotlin/feeds/FeedsRepo.kt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,26 @@ import kotlinx.coroutines.withContext
1717
import okhttp3.HttpUrl
1818
import org.koin.core.annotation.Single
1919

20+
//CREATE TABLE Feed (
21+
//id TEXT PRIMARY KEY NOT NULL,
22+
//links TEXT AS List<Link> NOT NULL,
23+
//title TEXT NOT NULL,
24+
//ext_open_entries_in_browser INTEGER AS Boolean,
25+
//ext_blocked_words TEXT NOT NULL,
26+
//ext_show_preview_images INTEGER AS Boolean
27+
//);
28+
2029
@Single
2130
class FeedsRepo(
2231
private val api: Api,
2332
private val db: Db,
2433
) {
2534

2635
suspend fun insertOrReplace(feed: Feed) {
36+
// insertOrReplace:
37+
// INSERT OR REPLACE
38+
// INTO Feed(id, links, title, ext_open_entries_in_browser, ext_blocked_words, ext_show_preview_images)
39+
// VALUES ?;
2740
withContext(Dispatchers.IO) {
2841
db.feedQueries.insertOrReplace(feed)
2942
}
@@ -39,14 +52,28 @@ class FeedsRepo(
3952
}
4053

4154
fun selectAll(): Flow<List<Feed>> {
55+
// selectAll:
56+
// SELECT *
57+
// FROM Feed
58+
// ORDER BY title;
4259
return db.feedQueries.selectAll().asFlow().mapToList()
4360
}
4461

4562
fun selectAllWithUnreadEntryCount(): Flow<List<SelectAllWithUnreadEntryCount>> {
63+
// selectAllWithUnreadEntryCount:
64+
// SELECT f.id, f.links, f.title, count(e.id) AS unread_entries
65+
// FROM Feed f
66+
// LEFT JOIN Entry e ON e.feed_id = f.id AND e.ext_read = 0 AND e.ext_bookmarked = 0
67+
// GROUP BY f.id
68+
// ORDER BY f.title;
4669
return db.feedQueries.selectAllWithUnreadEntryCount().asFlow().mapToList()
4770
}
4871

4972
fun selectById(id: String): Flow<Feed?> {
73+
// selectById:
74+
// SELECT *
75+
// FROM Feed
76+
// WHERE id = ?;
5077
return db.feedQueries.selectById(id).asFlow().mapToOneOrNull()
5178
}
5279

@@ -73,6 +100,10 @@ class FeedsRepo(
73100
withContext(Dispatchers.IO) {
74101
db.transaction {
75102
db.feedQueries.deleteById(id)
103+
// deleteByFeedId:
104+
// DELETE
105+
// FROM Entry
106+
// WHERE feed_id = ?;
76107
db.entryQueries.deleteByFeedId(id)
77108
}
78109
}

app/src/main/kotlin/opengraph/OpenGraphImagesRepo.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ class OpenGraphImagesRepo(
9595
?: entry.links.firstOrNull { it.rel is AtomLinkRel.Alternate }
9696

9797
if (link == null) {
98+
// updateOgImageChecked:
99+
// UPDATE Entry
100+
// SET ext_og_image_checked = ?
101+
// WHERE id = ?;
98102
db.entryQueries.updateOgImageChecked(true, entry.id)
99103
return@withContext
100104
}
@@ -143,6 +147,10 @@ class OpenGraphImagesRepo(
143147
return@withContext
144148
}
145149

150+
// updateOgImage:
151+
// UPDATE Entry
152+
// SET ext_og_image_url = ?, ext_og_image_width = ?, ext_og_image_height = ?, ext_og_image_checked = 1
153+
// WHERE id = ?;
146154
db.entryQueries.updateOgImage(
147155
ext_og_image_url = imageUrl,
148156
ext_og_image_width = bitmap.width.toLong(),

app/src/main/kotlin/settings/SettingsModel.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,13 @@ class SettingsModel(
9595

9696
db.apply {
9797
transaction {
98+
// deleteAll:
99+
// DELETE
100+
// FROM Feed;
98101
feedQueries.deleteAll()
102+
// deleteAll:
103+
// DELETE
104+
// FROM Entry;
99105
entryQueries.deleteAll()
100106
}
101107
}

app/src/main/sqldelight/db/Entry.sq

Lines changed: 0 additions & 66 deletions
This file was deleted.

app/src/main/sqldelight/db/EntrySearch.sq

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)