Skip to content

Commit 8e86772

Browse files
committed
Extract queries (WIP)
1 parent 03bba36 commit 8e86772

File tree

5 files changed

+154
-143
lines changed

5 files changed

+154
-143
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package db
2+
3+
import java.time.OffsetDateTime
4+
5+
data class ShortEntry(
6+
val links: List<Link>,
7+
val published: OffsetDateTime,
8+
val title: String,
9+
)

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

Lines changed: 136 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package entries
22

3+
import android.database.sqlite.SQLiteDatabase
4+
import androidx.core.database.sqlite.transaction
35
import api.Api
46
import com.squareup.sqldelight.runtime.coroutines.asFlow
57
import com.squareup.sqldelight.runtime.coroutines.mapToList
@@ -11,10 +13,13 @@ import db.Entry
1113
import db.Feed
1214
import db.SelectAllLinksPublishedAndTitle
1315
import db.SelectByQuery
16+
import db.ShortEntry
1417
import kotlinx.coroutines.Dispatchers
1518
import kotlinx.coroutines.flow.Flow
19+
import kotlinx.coroutines.flow.asFlow
1620
import kotlinx.coroutines.flow.first
1721
import kotlinx.coroutines.flow.flow
22+
import kotlinx.coroutines.flow.flowOf
1823
import kotlinx.coroutines.flow.map
1924
import kotlinx.coroutines.withContext
2025
import org.koin.core.annotation.Single
@@ -23,53 +28,148 @@ import java.time.OffsetDateTime
2328
@Single
2429
class EntriesRepo(
2530
private val api: Api,
26-
private val db: Db,
31+
private val db: SQLiteDatabase,
2732
) {
2833

34+
// CREATE TABLE Entry (
35+
// content_type TEXT,
36+
// content_src TEXT,
37+
// content_text TEXT,
38+
// links TEXT AS List<Link> NOT NULL,
39+
// summary TEXT,
40+
// id TEXT PRIMARY KEY NOT NULL,
41+
// feed_id TEXT NOT NULL,
42+
// title TEXT NOT NULL,
43+
// published TEXT AS OffsetDateTime NOT NULL,
44+
// updated TEXT AS OffsetDateTime NOT NULL,
45+
// author_name TEXT NOT NULL,
46+
// ext_read INTEGER AS Boolean NOT NULL,
47+
// ext_read_synced INTEGER AS Boolean NOT NULL,
48+
// ext_bookmarked INTEGER AS Boolean NOT NULL,
49+
// ext_bookmarked_synced INTEGER AS Boolean NOT NULL,
50+
// ext_nc_guid_hash TEXT NOT NULL,
51+
// ext_comments_url TEXT NOT NULL,
52+
// ext_og_image_checked INTEGER AS Boolean NOT NULL,
53+
// ext_og_image_url TEXT NOT NULL,
54+
// ext_og_image_width INTEGER NOT NULL,
55+
// ext_og_image_height INTEGER NOT NULL
56+
// );
57+
58+
fun insertOrReplace(entry: Entry) {
59+
// INSERT OR REPLACE
60+
// INTO Entry(
61+
// content_type,
62+
// content_src,
63+
// content_text,
64+
// links,
65+
// summary,
66+
// id,
67+
// feed_id,
68+
// title,
69+
// published,
70+
// updated,
71+
// author_name,
72+
// ext_read,
73+
// ext_read_synced,
74+
// ext_bookmarked,
75+
// ext_bookmarked_synced,
76+
// ext_nc_guid_hash,
77+
// ext_comments_url,
78+
// ext_og_image_checked,
79+
// ext_og_image_url,
80+
// ext_og_image_width,
81+
// ext_og_image_height
82+
// )
83+
// VALUES ?;
84+
}
85+
86+
fun selectAll(): List<Entry> {
87+
// SELECT *
88+
// FROM Entry
89+
// ORDER BY published DESC;
90+
return emptyList()
91+
}
92+
2993
suspend fun insertOrReplace(entries: List<Entry>) {
3094
withContext(Dispatchers.IO) {
31-
db.entryQueries.transaction {
95+
db.transaction {
3296
entries.forEach { entry ->
3397
val postProcessedEntry = entry.postProcess()
34-
db.entryQueries.insertOrReplace(postProcessedEntry)
98+
insertOrReplace(postProcessedEntry)
3599
}
36100
}
37101
}
38102
}
39103

40-
fun selectAllLinksPublishedAndTitle(): Flow<List<SelectAllLinksPublishedAndTitle>> {
41-
return db.entryQueries.selectAllLinksPublishedAndTitle().asFlow().mapToList()
104+
fun selectAllLinksPublishedAndTitle(): Flow<List<ShortEntry>> {
105+
// selectAllLinksPublishedAndTitle:
106+
// SELECT links, published, title
107+
// FROM Entry
108+
// ORDER BY published DESC;
109+
//return db.entryQueries.selectAllLinksPublishedAndTitle().asFlow().mapToList()
110+
return flowOf(emptyList())
42111
}
43112

113+
// selectByIds:
114+
// SELECT id
115+
// FROM Entry
116+
// WHERE id IN :ids;
117+
44118
fun selectById(entryId: String): Flow<Entry?> {
45-
return db.entryQueries.selectById(entryId).asFlow().mapToOneOrNull()
119+
// selectById:
120+
// SELECT *
121+
// FROM Entry
122+
// WHERE id = ?;
123+
//return db.entryQueries.selectById(entryId).asFlow().mapToOneOrNull()
124+
return flowOf(null)
46125
}
47126

48127
fun selectByFeedIdAndReadAndBookmarked(
49128
feedId: String,
50129
read: Collection<Boolean>,
51130
bookmarked: Boolean,
52131
): Flow<List<EntriesAdapterRow>> {
53-
return db.entryQueries.selectByFeedIdAndReadAndBookmarked(
54-
feed_id = feedId,
55-
ext_read = read,
56-
ext_bookmarked = bookmarked,
57-
).asFlow().mapToList()
132+
// selectByFeedIdAndReadAndBookmarked:
133+
// SELECT *
134+
// FROM EntriesAdapterRow e
135+
// WHERE e.feed_id = ?
136+
// AND e.ext_read IN ?
137+
// AND e.ext_bookmarked = ?
138+
// ORDER BY e.published DESC;
139+
// return db.entryQueries.selectByFeedIdAndReadAndBookmarked(
140+
// feed_id = feedId,
141+
// ext_read = read,
142+
// ext_bookmarked = bookmarked,
143+
// ).asFlow().mapToList()
144+
return flowOf(emptyList())
58145
}
59146

60147
fun selectByReadAndBookmarked(
61148
read: Collection<Boolean>,
62149
bookmarked: Boolean,
63150
): Flow<List<EntriesAdapterRow>> {
64-
return db.entryQueries.selectByReadAndBookmarked(
65-
ext_read = read,
66-
ext_bookmarked = bookmarked,
67-
).asFlow().mapToList()
151+
// selectByReadAndBookmarked:
152+
// SELECT *
153+
// FROM EntriesAdapterRow e
154+
// WHERE e.ext_read IN ?
155+
// AND e.ext_bookmarked = ?
156+
// ORDER BY e.published DESC
157+
// LIMIT 500;
158+
// return db.entryQueries.selectByReadAndBookmarked(
159+
// ext_read = read,
160+
// ext_bookmarked = bookmarked,
161+
// ).asFlow().mapToList()
162+
return flowOf(emptyList())
68163
}
69164

165+
// selectCount:
166+
// SELECT COUNT(*)
167+
// FROM Entry;
70168
fun selectCount() = db.entryQueries.selectCount().asFlow().mapToOne()
71169

72170
private fun selectMaxId(): Flow<String?> {
171+
// selectMaxId:
172+
// SELECT MAX(id + 0) FROM Entry;
73173
return db.entryQueries.selectMaxId().asFlow().mapToOneOrNull().map { it?.MAX }
74174
}
75175

@@ -132,6 +232,11 @@ class EntriesRepo(
132232

133233
suspend fun syncReadEntries() {
134234
withContext(Dispatchers.IO) {
235+
// selectByReadSynced:
236+
// SELECT *
237+
// FROM EntryWithoutContent
238+
// WHERE ext_read_synced = ?
239+
// ORDER BY published DESC;
135240
val unsyncedEntries = db.entryQueries.selectByReadSynced(false).executeAsList()
136241

137242
if (unsyncedEntries.isEmpty()) {
@@ -172,6 +277,17 @@ class EntriesRepo(
172277

173278
suspend fun syncBookmarkedEntries() {
174279
withContext(Dispatchers.IO) {
280+
// selectByBookmarked:
281+
// SELECT *
282+
// FROM EntryWithoutContent
283+
// WHERE ext_bookmarked = ?
284+
// ORDER BY published DESC;
285+
286+
// selectByBookmarkedSynced:
287+
// SELECT *
288+
// FROM EntryWithoutContent
289+
// WHERE ext_bookmarked_synced = ?
290+
// ORDER BY published DESC;
175291
val notSyncedEntries = db.entryQueries.selectByBookmarkedSynced(false).executeAsList()
176292

177293
if (notSyncedEntries.isEmpty()) {
@@ -234,6 +350,11 @@ class EntriesRepo(
234350
val feed = feeds.firstOrNull { it.id == newEntry.feed_id }
235351
val postProcessedEntry = newEntry.postProcess(feed)
236352

353+
// selectLinksById:
354+
// SELECT links
355+
// FROM Entry
356+
// WHERE id = ?;
357+
237358
val oldLinks = db.entryQueries.selectLinksById(newEntry.id).executeAsOneOrNull()
238359
?: emptyList()
239360

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ class FeedsRepo(
5151
}
5252

5353
fun selectLinks(): Flow<List<Link>> {
54+
// selectLinks:
55+
// SELECT links
56+
// FROM Entry;
5457
return db.feedQueries.selectLinks().asFlow().mapToList().map { it.flatten() }
5558
}
5659

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ class OpenGraphImagesRepo(
5757
.distinctUntilChanged()
5858
.collectLatest { showPreviewImages ->
5959
if (showPreviewImages) {
60+
// selectByOgImageChecked:
61+
// SELECT *
62+
// FROM EntryWithoutContent
63+
// WHERE ext_og_image_checked = :ogImageChecked
64+
// ORDER BY published DESC
65+
// LIMIT :limit;
6066
db.entryQueries.selectByOgImageChecked(false, BATCH_SIZE * 2L)
6167
.asFlow()
6268
.mapToList()

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

Lines changed: 0 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -2,134 +2,6 @@ import db.Link;
22
import java.time.OffsetDateTime;
33
import kotlin.collections.List;
44

5-
CREATE TABLE Entry (
6-
content_type TEXT,
7-
content_src TEXT,
8-
content_text TEXT,
9-
links TEXT AS List<Link> NOT NULL,
10-
summary TEXT,
11-
id TEXT PRIMARY KEY NOT NULL,
12-
feed_id TEXT NOT NULL,
13-
title TEXT NOT NULL,
14-
published TEXT AS OffsetDateTime NOT NULL,
15-
updated TEXT AS OffsetDateTime NOT NULL,
16-
author_name TEXT NOT NULL,
17-
ext_read INTEGER AS Boolean NOT NULL,
18-
ext_read_synced INTEGER AS Boolean NOT NULL,
19-
ext_bookmarked INTEGER AS Boolean NOT NULL,
20-
ext_bookmarked_synced INTEGER AS Boolean NOT NULL,
21-
ext_nc_guid_hash TEXT NOT NULL,
22-
ext_comments_url TEXT NOT NULL,
23-
ext_og_image_checked INTEGER AS Boolean NOT NULL,
24-
ext_og_image_url TEXT NOT NULL,
25-
ext_og_image_width INTEGER NOT NULL,
26-
ext_og_image_height INTEGER NOT NULL
27-
);
28-
29-
insertOrReplace:
30-
INSERT OR REPLACE
31-
INTO Entry(
32-
content_type,
33-
content_src,
34-
content_text,
35-
links,
36-
summary,
37-
id,
38-
feed_id,
39-
title,
40-
published,
41-
updated,
42-
author_name,
43-
ext_read,
44-
ext_read_synced,
45-
ext_bookmarked,
46-
ext_bookmarked_synced,
47-
ext_nc_guid_hash,
48-
ext_comments_url,
49-
ext_og_image_checked,
50-
ext_og_image_url,
51-
ext_og_image_width,
52-
ext_og_image_height
53-
)
54-
VALUES ?;
55-
56-
selectAll:
57-
SELECT *
58-
FROM Entry
59-
ORDER BY published DESC;
60-
61-
selectAllLinksPublishedAndTitle:
62-
SELECT links, published, title
63-
FROM Entry
64-
ORDER BY published DESC;
65-
66-
selectByIds:
67-
SELECT id
68-
FROM Entry
69-
WHERE id IN :ids;
70-
71-
selectById:
72-
SELECT *
73-
FROM Entry
74-
WHERE id = ?;
75-
76-
selectLinksById:
77-
SELECT links
78-
FROM Entry
79-
WHERE id = ?;
80-
81-
selectByFeedIdAndReadAndBookmarked:
82-
SELECT *
83-
FROM EntriesAdapterRow e
84-
WHERE e.feed_id = ?
85-
AND e.ext_read IN ?
86-
AND e.ext_bookmarked = ?
87-
ORDER BY e.published DESC;
88-
89-
selectByReadAndBookmarked:
90-
SELECT *
91-
FROM EntriesAdapterRow e
92-
WHERE e.ext_read IN ?
93-
AND e.ext_bookmarked = ?
94-
ORDER BY e.published DESC
95-
LIMIT 500;
96-
97-
selectByReadSynced:
98-
SELECT *
99-
FROM EntryWithoutContent
100-
WHERE ext_read_synced = ?
101-
ORDER BY published DESC;
102-
103-
selectByBookmarked:
104-
SELECT *
105-
FROM EntryWithoutContent
106-
WHERE ext_bookmarked = ?
107-
ORDER BY published DESC;
108-
109-
selectByBookmarkedSynced:
110-
SELECT *
111-
FROM EntryWithoutContent
112-
WHERE ext_bookmarked_synced = ?
113-
ORDER BY published DESC;
114-
115-
selectByOgImageChecked:
116-
SELECT *
117-
FROM EntryWithoutContent
118-
WHERE ext_og_image_checked = :ogImageChecked
119-
ORDER BY published DESC
120-
LIMIT :limit;
121-
122-
selectLinks:
123-
SELECT links
124-
FROM Entry;
125-
126-
selectCount:
127-
SELECT COUNT(*)
128-
FROM Entry;
129-
130-
selectMaxId:
131-
SELECT MAX(id + 0) FROM Entry;
132-
1335
selectMaxUpdated:
1346
SELECT MAX(updated)
1357
FROM Entry;

0 commit comments

Comments
 (0)