11package org.vestifeed.db.table
22
33import androidx.sqlite.SQLiteConnection
4- import androidx.sqlite.SQLiteStatement
54import androidx.sqlite.execSQL
65import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
6+ import org.vestifeed.db.bindBooleanOrNull
7+ import org.vestifeed.db.getBoolOrNull
78import org.vestifeed.parser.AtomLinkRel
89import kotlin.collections.forEach
910
10- object FeedSchema {
11- const val TABLE_NAME = " feed"
12-
13- override fun toString (): String {
14- return """
15- CREATE TABLE $TABLE_NAME (
16- ${Columns .Id } TEXT PRIMARY KEY NOT NULL,
17- ${Columns .Links } TEXT,
18- ${Columns .Title } TEXT NOT NULL,
19- ${Columns .ExtOpenEntriesInBrowser } INTEGER,
20- ${Columns .ExtBlockedWords } TEXT,
21- ${Columns .ExtShowPreviewImages } INTEGER
22- );
23- """ .trimIndent()
24- }
25-
26- enum class Columns (val sqlName : String ) {
27- Id (" id" ),
28- Links (" links" ),
29- Title (" title" ),
30- ExtOpenEntriesInBrowser (" ext_open_entries_in_browser" ),
31- ExtBlockedWords (" ext_blocked_words" ),
32- ExtShowPreviewImages (" ext_show_preview_images" );
33-
34- override fun toString () = sqlName
35- }
36- }
11+ const val FEED_SCHEMA = """
12+ CREATE TABLE feed (
13+ id TEXT PRIMARY KEY NOT NULL,
14+ links TEXT,
15+ title TEXT NOT NULL,
16+ ext_open_entries_in_browser INTEGER,
17+ ext_blocked_words TEXT,
18+ ext_show_preview_images INTEGER
19+ );
20+ """
3721
3822typealias Feed = FeedProjection
3923
@@ -44,25 +28,7 @@ data class FeedProjection(
4428 val extOpenEntriesInBrowser : Boolean? ,
4529 val extBlockedWords : String ,
4630 val extShowPreviewImages : Boolean? ,
47- ) {
48- companion object {
49- val columns: String
50- get() {
51- return FeedSchema .Columns .entries.joinToString(" ," ) { it.sqlName }
52- }
53-
54- fun fromStatement (stmt : SQLiteStatement ): FeedProjection {
55- return FeedProjection (
56- id = stmt.getText(0 ),
57- links = jsonToLinks(stmt.getText(1 )),
58- title = stmt.getText(2 ),
59- extOpenEntriesInBrowser = if (stmt.isNull(3 )) null else stmt.getInt(3 ) == 1 ,
60- extBlockedWords = stmt.getText(4 ),
61- extShowPreviewImages = if (stmt.isNull(5 )) null else stmt.getInt(5 ) == 1
62- )
63- }
64- }
65- }
31+ )
6632
6733class FeedQueries (private val conn : SQLiteConnection ) {
6834 fun insertOrReplace (feeds : List <Feed >) {
@@ -73,25 +39,17 @@ class FeedQueries(private val conn: SQLiteConnection) {
7339 conn.prepare(
7440 """
7541 INSERT OR REPLACE
76- INTO ${ FeedSchema . TABLE_NAME } ( ${ FeedProjection .columns} )
42+ INTO feed (id, links, title, ext_open_entries_in_browser, ext_blocked_words, ext_show_preview_images )
7743 VALUES (?, ?, ?, ?, ?, ?);
7844 """
7945 ).use { stmt ->
8046 feeds.forEach { feed ->
8147 stmt.bindText(1 , feed.id)
8248 stmt.bindText(2 , linksToJson(feed.links))
8349 stmt.bindText(3 , feed.title)
84- if (feed.extOpenEntriesInBrowser != null ) {
85- stmt.bindInt(4 , if (feed.extOpenEntriesInBrowser) 1 else 0 )
86- } else {
87- stmt.bindNull(4 )
88- }
50+ stmt.bindBooleanOrNull(4 , feed.extOpenEntriesInBrowser)
8951 stmt.bindText(5 , feed.extBlockedWords)
90- if (feed.extShowPreviewImages != null ) {
91- stmt.bindInt(6 , if (feed.extShowPreviewImages) 1 else 0 )
92- } else {
93- stmt.bindNull(6 )
94- }
52+ stmt.bindBooleanOrNull(6 , feed.extShowPreviewImages)
9553 stmt.step()
9654 stmt.reset()
9755 }
@@ -101,14 +59,23 @@ class FeedQueries(private val conn: SQLiteConnection) {
10159 fun selectAll (): List <FeedProjection > {
10260 conn.prepare(
10361 """
104- SELECT ${ FeedProjection .columns}
105- FROM ${ FeedSchema . TABLE_NAME }
106- ORDER BY ${ FeedSchema . Columns . Title } ;
62+ SELECT id, links, title, ext_open_entries_in_browser, ext_blocked_words, ext_show_preview_images
63+ FROM feed
64+ ORDER BY title ;
10765 """
10866 ).use { stmt ->
10967 return buildList {
11068 while (stmt.step()) {
111- add(FeedProjection .fromStatement(stmt))
69+ add(
70+ FeedProjection (
71+ id = stmt.getText(0 ),
72+ links = jsonToLinks(stmt.getText(1 )),
73+ title = stmt.getText(2 ),
74+ extOpenEntriesInBrowser = stmt.getBoolOrNull(3 ),
75+ extBlockedWords = stmt.getText(4 ),
76+ extShowPreviewImages = stmt.getBoolOrNull(5 ),
77+ )
78+ )
11279 }
11380 }
11481 }
@@ -128,10 +95,10 @@ class FeedQueries(private val conn: SQLiteConnection) {
12895 conn.prepare(
12996 """
13097 SELECT f.*, COUNT(e.id) as unread_entries
131- FROM ${ FeedSchema . TABLE_NAME } f
132- LEFT JOIN entry e ON e.feed_id = f.${ FeedSchema . Columns . Id } AND e.ext_read = 0 AND e.ext_bookmarked = 0
133- GROUP BY f.${ FeedSchema . Columns . Id }
134- ORDER BY f.${ FeedSchema . Columns . Title } ;
98+ FROM feed f
99+ LEFT JOIN entry e ON e.feed_id = f.id AND e.ext_read = 0 AND e.ext_bookmarked = 0
100+ GROUP BY f.id
101+ ORDER BY f.title ;
135102 """
136103 ).use { stmt ->
137104 return buildList {
@@ -155,8 +122,8 @@ class FeedQueries(private val conn: SQLiteConnection) {
155122 fun selectAllLinks (): List <List <Link >> {
156123 conn.prepare(
157124 """
158- SELECT ${ FeedSchema . Columns . Links }
159- FROM ${ FeedSchema . TABLE_NAME } ;
125+ SELECT links
126+ FROM feed ;
160127 """
161128 ).use { stmt ->
162129 return buildList {
@@ -170,21 +137,28 @@ class FeedQueries(private val conn: SQLiteConnection) {
170137 fun selectById (id : String ): FeedProjection ? {
171138 conn.prepare(
172139 """
173- SELECT ${ FeedProjection .columns}
174- FROM ${ FeedSchema . TABLE_NAME }
140+ SELECT id, links, title, ext_open_entries_in_browser, ext_blocked_words, ext_show_preview_images
141+ FROM feed
175142 WHERE id = ?;
176143 """
177144 ).use { stmt ->
178145 stmt.bindText(1 , id)
179- return if (stmt.step()) FeedProjection .fromStatement(stmt) else null
146+ return if (stmt.step()) FeedProjection (
147+ id = stmt.getText(0 ),
148+ links = jsonToLinks(stmt.getText(1 )),
149+ title = stmt.getText(2 ),
150+ extOpenEntriesInBrowser = stmt.getBoolOrNull(3 ),
151+ extBlockedWords = stmt.getText(4 ),
152+ extShowPreviewImages = stmt.getBoolOrNull(5 ),
153+ ) else null
180154 }
181155 }
182156
183157 fun deleteById (id : String ) {
184158 conn.prepare(
185159 """
186- DELETE FROM ${ FeedSchema . TABLE_NAME }
187- WHERE ${ FeedSchema . Columns . Id } = ?;
160+ DELETE FROM feed
161+ WHERE id = ?;
188162 """
189163 ).use { stmt ->
190164 stmt.bindText(1 , id)
@@ -193,7 +167,7 @@ class FeedQueries(private val conn: SQLiteConnection) {
193167 }
194168
195169 fun deleteAll () {
196- conn.execSQL(" DELETE FROM ${ FeedSchema . TABLE_NAME } ;" )
170+ conn.execSQL(" DELETE FROM feed ;" )
197171 }
198172}
199173
@@ -237,7 +211,8 @@ private fun jsonToLinks(json: String?): List<Link> {
237211 hreflang = obj.get(" hreflang" )?.asString?.ifEmpty { null },
238212 title = obj.get(" title" )?.asString?.ifEmpty { null },
239213 length = obj.get(" length" )?.let { if (it.isJsonNull) null else it.asLong },
240- extEnclosureDownloadProgress = obj.get(" extEnclosureDownloadProgress" )?.let { if (it.isJsonNull) null else it.asDouble },
214+ extEnclosureDownloadProgress = obj.get(" extEnclosureDownloadProgress" )
215+ ?.let { if (it.isJsonNull) null else it.asDouble },
241216 extCacheUri = obj.get(" extCacheUri" )?.asString?.ifEmpty { null }
242217 )
243218 }
0 commit comments