11package entries
22
3+ import android.database.sqlite.SQLiteDatabase
4+ import androidx.core.database.sqlite.transaction
35import api.Api
46import com.squareup.sqldelight.runtime.coroutines.asFlow
57import com.squareup.sqldelight.runtime.coroutines.mapToList
@@ -11,10 +13,13 @@ import db.Entry
1113import db.Feed
1214import db.SelectAllLinksPublishedAndTitle
1315import db.SelectByQuery
16+ import db.ShortEntry
1417import kotlinx.coroutines.Dispatchers
1518import kotlinx.coroutines.flow.Flow
19+ import kotlinx.coroutines.flow.asFlow
1620import kotlinx.coroutines.flow.first
1721import kotlinx.coroutines.flow.flow
22+ import kotlinx.coroutines.flow.flowOf
1823import kotlinx.coroutines.flow.map
1924import kotlinx.coroutines.withContext
2025import org.koin.core.annotation.Single
@@ -23,53 +28,148 @@ import java.time.OffsetDateTime
2328@Single
2429class 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
0 commit comments