diff --git a/src/main/kotlin/com/marvinelsen/willow/domain/Dictionary.kt b/src/main/kotlin/com/marvinelsen/willow/domain/Dictionary.kt index 57d0a57..16342cd 100644 --- a/src/main/kotlin/com/marvinelsen/willow/domain/Dictionary.kt +++ b/src/main/kotlin/com/marvinelsen/willow/domain/Dictionary.kt @@ -3,10 +3,10 @@ package com.marvinelsen.willow.domain interface Dictionary { fun search(query: String, searchMode: SearchMode): List - fun findWordsBeginning(entry: DictionaryEntry): List + fun findWordsBeginningWith(entry: DictionaryEntry): List fun findWordsContaining(entry: DictionaryEntry): List - fun findCharacters(entry: DictionaryEntry): List + fun findCharactersOf(entry: DictionaryEntry): List - fun findSentencesContaining(entry: DictionaryEntry): List + fun findExampleSentencesFor(entry: DictionaryEntry): List } diff --git a/src/main/kotlin/com/marvinelsen/willow/domain/SqliteDictionary.kt b/src/main/kotlin/com/marvinelsen/willow/domain/SqliteDictionary.kt index e6241f3..5c3f97b 100644 --- a/src/main/kotlin/com/marvinelsen/willow/domain/SqliteDictionary.kt +++ b/src/main/kotlin/com/marvinelsen/willow/domain/SqliteDictionary.kt @@ -58,7 +58,7 @@ class SqliteDictionary(private val connection: Connection) : Dictionary { ) } - private val searchSegments = """ + private val searchSegmentsRawSql = """ WITH cte(id, segment) AS (VALUES ?) SELECT entry.traditional, simplified, pinyin_with_tone_marks, pinyin_with_tone_numbers, zhuyin, cedict_definitions, cross_straits_definitions, moe_definitions FROM entry INNER JOIN cte @@ -66,7 +66,7 @@ class SqliteDictionary(private val connection: Connection) : Dictionary { ORDER BY cte.id """.trimIndent() - private val findWordsBeginning: PreparedStatement by lazy { + private val findWordsBeginningPreparedStatement: PreparedStatement by lazy { connection.prepareStatement( """ SELECT traditional, simplified, pinyin_with_tone_marks, pinyin_with_tone_numbers, zhuyin, cedict_definitions, cross_straits_definitions, moe_definitions @@ -77,7 +77,7 @@ class SqliteDictionary(private val connection: Connection) : Dictionary { ) } - private val findWordsContaining: PreparedStatement by lazy { + private val findWordsContainingPreparedStatement: PreparedStatement by lazy { connection.prepareStatement( """ SELECT traditional, simplified, pinyin_with_tone_marks, pinyin_with_tone_numbers, zhuyin, cedict_definitions, cross_straits_definitions, moe_definitions @@ -88,16 +88,16 @@ class SqliteDictionary(private val connection: Connection) : Dictionary { ) } - private val findCharacters = """ - WITH cte(id, character, syllable) AS (VALUES ?) + private val findCharactersRawSql = """ + WITH cte(id, character, pinyin) AS (VALUES ?) SELECT traditional, simplified, pinyin_with_tone_marks, pinyin_with_tone_numbers, zhuyin, cedict_definitions, cross_straits_definitions, moe_definitions FROM entry INNER JOIN cte ON cte.character = entry.traditional - WHERE cte.syllable = entry.pinyin_with_tone_numbers + WHERE cte.pinyin = entry.pinyin_with_tone_numbers ORDER BY cte.id """.trimIndent() - private val findSentences: PreparedStatement by lazy { + private val findExampleSentencesPreparedStatement: PreparedStatement by lazy { connection.prepareStatement( """ SELECT traditional, simplified @@ -112,67 +112,69 @@ class SqliteDictionary(private val connection: Connection) : Dictionary { SearchMode.SIMPLIFIED -> searchSimplified(query) SearchMode.TRADITIONAL -> searchTraditional(query) SearchMode.PINYIN -> searchPinyin(query) - SearchMode.SEGMENTS -> searchSegments(query) + SearchMode.SEGMENTS -> searchSegmentsOf(query) } - private fun searchSimplified(query: String): List { - searchSimplifiedPreparedStatement.setString(1, "$query*") + private fun searchSimplified(simplified: String): List { + searchSimplifiedPreparedStatement.setString(1, "$simplified*") - val resultSet: ResultSet = searchSimplifiedPreparedStatement.executeQuery() - - return resultSet.toListOfDictionaryEntries() + return searchSimplifiedPreparedStatement.executeQuery().use { + it.toListOfDictionaryEntries() + } } - private fun searchTraditional(query: String): List { - searchTraditionalPreparedStatement.setString(1, "$query*") + private fun searchTraditional(traditional: String): List { + searchTraditionalPreparedStatement.setString(1, "$traditional*") - val resultSet: ResultSet = searchTraditionalPreparedStatement.executeQuery() - - return resultSet.toListOfDictionaryEntries() + return searchTraditionalPreparedStatement.executeQuery().use { + it.toListOfDictionaryEntries() + } } - private fun searchPinyin(query: String): List { - val sanitizedQuery = query.lowercase().replace(whitespaceRegex, "") + private fun searchPinyin(pinyin: String): List { + val sanitizedPinyin = pinyin.lowercase().replace(whitespaceRegex, "") - searchPinyinPreparedStatement.setString(1, "$sanitizedQuery*") - searchPinyinPreparedStatement.setString(2, "$sanitizedQuery*") + searchPinyinPreparedStatement.setString(1, "$sanitizedPinyin*") + searchPinyinPreparedStatement.setString(2, "$sanitizedPinyin*") - val resultSet: ResultSet = searchPinyinPreparedStatement.executeQuery() - - return resultSet.toListOfDictionaryEntries() + return searchPinyinPreparedStatement.executeQuery().use { resultSet -> + resultSet.toListOfDictionaryEntries() + } } - private fun searchSegments(phrase: String): List { + private fun searchSegmentsOf(phrase: String): List { val segments = segmentBs.segment(phrase, SegmentResultHandlers.word()) val segmentsListString = segments - .mapIndexed { index, s -> "($index, '$s')" } + .mapIndexed { index, segment -> "($index, '$segment')" } .joinToString(",") - val query = searchSegments.replace("?", segmentsListString) + val query = searchSegmentsRawSql.replace("?", segmentsListString) - val resultSet: ResultSet = connection.createStatement().executeQuery(query) - - return resultSet.toListOfDictionaryEntries() + return connection.createStatement().use { statement -> + statement.executeQuery(query).use { resultSet -> + resultSet.toListOfDictionaryEntries() + } + } } override fun findWordsContaining(entry: DictionaryEntry): List { - findWordsContaining.setString(1, "_%${entry.traditional}%") + findWordsContainingPreparedStatement.setString(1, "_%${entry.traditional}%") - val resultSet: ResultSet = findWordsContaining.executeQuery() - - return resultSet.toListOfDictionaryEntries() + return findWordsContainingPreparedStatement.executeQuery().use { resultSet -> + resultSet.toListOfDictionaryEntries() + } } - override fun findWordsBeginning(entry: DictionaryEntry): List { - findWordsBeginning.setString(1, "${entry.traditional}?*") + override fun findWordsBeginningWith(entry: DictionaryEntry): List { + findWordsBeginningPreparedStatement.setString(1, "${entry.traditional}?*") - val resultSet: ResultSet = findWordsBeginning.executeQuery() - - return resultSet.toListOfDictionaryEntries() + return findWordsBeginningPreparedStatement.executeQuery().use { resultSet -> + resultSet.toListOfDictionaryEntries() + } } - override fun findCharacters(entry: DictionaryEntry): List { + override fun findCharactersOf(entry: DictionaryEntry): List { val pinyinSyllablesWithToneNumbers = entry.pinyinWithToneNumbers .lowercase() .split(" ") @@ -190,19 +192,21 @@ class SqliteDictionary(private val connection: Connection) : Dictionary { .mapIndexed { index, s -> "($index, '${s.first}', '${s.second}')" } .joinToString(",") - val query = findCharacters.replace("?", queryInput) + val query = findCharactersRawSql.replace("?", queryInput) - val resultSet: ResultSet = connection.createStatement().executeQuery(query) - - return resultSet.toListOfDictionaryEntries() + return connection.createStatement().use { statement -> + statement.executeQuery(query).use { resultSet -> + resultSet.toListOfDictionaryEntries() + } + } } - override fun findSentencesContaining(entry: DictionaryEntry): List { - findSentences.setString(1, "_%${entry.traditional}%") + override fun findExampleSentencesFor(entry: DictionaryEntry): List { + findExampleSentencesPreparedStatement.setString(1, "_%${entry.traditional}%") - val resultSet: ResultSet = findSentences.executeQuery() - - return resultSet.toListOfSentences() + return findExampleSentencesPreparedStatement.executeQuery().use { resultSet -> + resultSet.toListOfSentences() + } } } @@ -219,10 +223,8 @@ private fun ResultSet.toDictionaryEntry() = DictionaryEntry( ) private fun ResultSet.toListOfDictionaryEntries() = buildList { - this@toListOfDictionaryEntries.use { - while (it.next()) { - add(it.toDictionaryEntry()) - } + while (this@toListOfDictionaryEntries.next()) { + add(this@toListOfDictionaryEntries.toDictionaryEntry()) } } @@ -232,9 +234,7 @@ private fun ResultSet.toSentence() = Sentence( ) private fun ResultSet.toListOfSentences() = buildList { - this@toListOfSentences.use { - while (it.next()) { - add(it.toSentence()) - } + while (this@toListOfSentences.next()) { + add(this@toListOfSentences.toSentence()) } } diff --git a/src/main/kotlin/com/marvinelsen/willow/ui/services/FindCharacterService.kt b/src/main/kotlin/com/marvinelsen/willow/ui/services/FindCharacterService.kt index 7e4770a..bfde52b 100644 --- a/src/main/kotlin/com/marvinelsen/willow/ui/services/FindCharacterService.kt +++ b/src/main/kotlin/com/marvinelsen/willow/ui/services/FindCharacterService.kt @@ -15,6 +15,6 @@ class FindCharacterService(private val dictionary: Dictionary) : Service