Refactir SqliteDictionary

This commit is contained in:
Marvin Elsen 2024-10-21 21:13:27 +02:00
parent 1b5c32c320
commit a1a7567bd0
Signed by: marvinelsen
GPG Key ID: 820672408CC318C2
5 changed files with 64 additions and 64 deletions

View File

@ -3,10 +3,10 @@ package com.marvinelsen.willow.domain
interface Dictionary {
fun search(query: String, searchMode: SearchMode): List<DictionaryEntry>
fun findWordsBeginning(entry: DictionaryEntry): List<DictionaryEntry>
fun findWordsBeginningWith(entry: DictionaryEntry): List<DictionaryEntry>
fun findWordsContaining(entry: DictionaryEntry): List<DictionaryEntry>
fun findCharacters(entry: DictionaryEntry): List<DictionaryEntry>
fun findCharactersOf(entry: DictionaryEntry): List<DictionaryEntry>
fun findSentencesContaining(entry: DictionaryEntry): List<Sentence>
fun findExampleSentencesFor(entry: DictionaryEntry): List<Sentence>
}

View File

@ -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<DictionaryEntry> {
searchSimplifiedPreparedStatement.setString(1, "$query*")
private fun searchSimplified(simplified: String): List<DictionaryEntry> {
searchSimplifiedPreparedStatement.setString(1, "$simplified*")
val resultSet: ResultSet = searchSimplifiedPreparedStatement.executeQuery()
return resultSet.toListOfDictionaryEntries()
return searchSimplifiedPreparedStatement.executeQuery().use {
it.toListOfDictionaryEntries()
}
}
private fun searchTraditional(query: String): List<DictionaryEntry> {
searchTraditionalPreparedStatement.setString(1, "$query*")
private fun searchTraditional(traditional: String): List<DictionaryEntry> {
searchTraditionalPreparedStatement.setString(1, "$traditional*")
val resultSet: ResultSet = searchTraditionalPreparedStatement.executeQuery()
return resultSet.toListOfDictionaryEntries()
return searchTraditionalPreparedStatement.executeQuery().use {
it.toListOfDictionaryEntries()
}
}
private fun searchPinyin(query: String): List<DictionaryEntry> {
val sanitizedQuery = query.lowercase().replace(whitespaceRegex, "")
private fun searchPinyin(pinyin: String): List<DictionaryEntry> {
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<DictionaryEntry> {
private fun searchSegmentsOf(phrase: String): List<DictionaryEntry> {
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<DictionaryEntry> {
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<DictionaryEntry> {
findWordsBeginning.setString(1, "${entry.traditional}?*")
override fun findWordsBeginningWith(entry: DictionaryEntry): List<DictionaryEntry> {
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<DictionaryEntry> {
override fun findCharactersOf(entry: DictionaryEntry): List<DictionaryEntry> {
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<Sentence> {
findSentences.setString(1, "_%${entry.traditional}%")
override fun findExampleSentencesFor(entry: DictionaryEntry): List<Sentence> {
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())
}
}

View File

@ -15,6 +15,6 @@ class FindCharacterService(private val dictionary: Dictionary) : Service<Observa
override fun createTask() = task {
if (!this::entry.isInitialized) error("Entry is not initialized")
FXCollections.observableList(dictionary.findCharacters(entry.toDomain()).map { it.toFx() })
FXCollections.observableList(dictionary.findCharactersOf(entry.toDomain()).map { it.toFx() })
}
}

View File

@ -16,6 +16,6 @@ class FindSentencesService(private val dictionary: Dictionary) : Service<Observa
override fun createTask() = task {
if (!this::entry.isInitialized) error("Entry is not initialized")
FXCollections.observableList(dictionary.findSentencesContaining(entry.toDomain()).map { it.toFx() })
FXCollections.observableList(dictionary.findExampleSentencesFor(entry.toDomain()).map { it.toFx() })
}
}

View File

@ -15,6 +15,6 @@ class FindWordsBeginningService(private val dictionary: Dictionary) : Service<Ob
override fun createTask() = task {
if (!this::entry.isInitialized) error("Entry is not initialized")
FXCollections.observableList(dictionary.findWordsBeginning(entry.toDomain()).map { it.toFx() })
FXCollections.observableList(dictionary.findWordsBeginningWith(entry.toDomain()).map { it.toFx() })
}
}