Optimize imports, disallow wildcard imports because of klint, run linter
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
package exh.search
|
||||
|
||||
class Namespace(var namespace: String,
|
||||
var tag: Text? = null) : QueryComponent()
|
||||
class Namespace(
|
||||
var namespace: String,
|
||||
var tag: Text? = null
|
||||
) : QueryComponent()
|
||||
|
||||
@@ -3,4 +3,4 @@ package exh.search
|
||||
open class QueryComponent {
|
||||
var excluded = false
|
||||
var exact = false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,8 +7,10 @@ import exh.metadata.sql.tables.SearchTitleTable
|
||||
class SearchEngine {
|
||||
private val queryCache = mutableMapOf<String, List<QueryComponent>>()
|
||||
|
||||
fun textToSubQueries(namespace: String?,
|
||||
component: Text?): Pair<String, List<String>>? {
|
||||
fun textToSubQueries(
|
||||
namespace: String?,
|
||||
component: Text?
|
||||
): Pair<String, List<String>>? {
|
||||
val maybeLenientComponent = component?.let {
|
||||
if (!it.exact)
|
||||
it.asLenientTagQueries()
|
||||
@@ -22,20 +24,20 @@ class SearchEngine {
|
||||
"${SearchTagTable.TABLE}.${SearchTagTable.COL_NAME} LIKE ?"
|
||||
}.joinToString(separator = " OR ", prefix = "(", postfix = ")") to params
|
||||
}
|
||||
return if(namespace != null) {
|
||||
return if (namespace != null) {
|
||||
var query = """
|
||||
(SELECT ${SearchTagTable.COL_MANGA_ID} AS $COL_MANGA_ID FROM ${SearchTagTable.TABLE}
|
||||
WHERE ${SearchTagTable.COL_NAMESPACE} IS NOT NULL
|
||||
AND ${SearchTagTable.COL_NAMESPACE} LIKE ?
|
||||
""".trimIndent()
|
||||
val params = mutableListOf(escapeLike(namespace))
|
||||
if(componentTagQuery != null) {
|
||||
if (componentTagQuery != null) {
|
||||
query += "\n AND ${componentTagQuery.first}"
|
||||
params += componentTagQuery.second
|
||||
}
|
||||
|
||||
"$query)" to params
|
||||
} else if(component != null) {
|
||||
} else if (component != null) {
|
||||
// Match title + tags
|
||||
val tagQuery = """
|
||||
SELECT ${SearchTagTable.COL_MANGA_ID} AS $COL_MANGA_ID FROM ${SearchTagTable.TABLE}
|
||||
@@ -59,27 +61,27 @@ class SearchEngine {
|
||||
val include = mutableListOf<Pair<String, List<String>>>()
|
||||
val exclude = mutableListOf<Pair<String, List<String>>>()
|
||||
|
||||
for(component in q) {
|
||||
val query = if(component is Text) {
|
||||
for (component in q) {
|
||||
val query = if (component is Text) {
|
||||
textToSubQueries(null, component)
|
||||
} else if(component is Namespace) {
|
||||
if(component.namespace == "uploader") {
|
||||
} else if (component is Namespace) {
|
||||
if (component.namespace == "uploader") {
|
||||
wheres += "meta.${SearchMetadataTable.COL_UPLOADER} LIKE ?"
|
||||
whereParams += component.tag!!.rawTextEscapedForLike()
|
||||
null
|
||||
} else {
|
||||
if(component.tag!!.components.size > 0) {
|
||||
//Match namespace + tags
|
||||
if (component.tag!!.components.size > 0) {
|
||||
// Match namespace + tags
|
||||
textToSubQueries(component.namespace, component.tag)
|
||||
} else {
|
||||
//Perform namespace search
|
||||
// Perform namespace search
|
||||
textToSubQueries(component.namespace, null)
|
||||
}
|
||||
}
|
||||
} else error("Unknown query component!")
|
||||
|
||||
if(query != null) {
|
||||
(if(component.excluded) exclude else include) += query
|
||||
if (query != null) {
|
||||
(if (component.excluded) exclude else include) += query
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,14 +99,13 @@ class SearchEngine {
|
||||
completeParams += pair.second
|
||||
}
|
||||
|
||||
|
||||
exclude.forEach {
|
||||
wheres += """
|
||||
(meta.${SearchMetadataTable.COL_MANGA_ID} NOT IN ${it.first})
|
||||
""".trimIndent()
|
||||
whereParams += it.second
|
||||
}
|
||||
if(wheres.isNotEmpty()) {
|
||||
if (wheres.isNotEmpty()) {
|
||||
completeParams += whereParams
|
||||
baseQuery += "\nWHERE\n"
|
||||
baseQuery += wheres.joinToString("\nAND\n")
|
||||
@@ -126,7 +127,7 @@ class SearchEngine {
|
||||
var nextIsExact = false
|
||||
|
||||
fun flushText() {
|
||||
if(queuedRawText.isNotEmpty()) {
|
||||
if (queuedRawText.isNotEmpty()) {
|
||||
queuedText += StringTextComponent(queuedRawText.toString())
|
||||
queuedRawText.setLength(0)
|
||||
}
|
||||
@@ -150,24 +151,24 @@ class SearchEngine {
|
||||
}
|
||||
}
|
||||
|
||||
for(char in query.toLowerCase()) {
|
||||
if(char == '"') {
|
||||
for (char in query.toLowerCase()) {
|
||||
if (char == '"') {
|
||||
inQuotes = !inQuotes
|
||||
} else if(enableWildcard && (char == '?' || char == '_')) {
|
||||
} else if (enableWildcard && (char == '?' || char == '_')) {
|
||||
flushText()
|
||||
queuedText.add(SingleWildcard(char.toString()))
|
||||
} else if(enableWildcard && (char == '*' || char == '%')) {
|
||||
} else if (enableWildcard && (char == '*' || char == '%')) {
|
||||
flushText()
|
||||
queuedText.add(MultiWildcard(char.toString()))
|
||||
} else if(char == '-') {
|
||||
} else if (char == '-') {
|
||||
nextIsExcluded = true
|
||||
} else if(char == '$') {
|
||||
} else if (char == '$') {
|
||||
nextIsExact = true
|
||||
} else if(char == ':') {
|
||||
} else if (char == ':') {
|
||||
flushText()
|
||||
var flushed = flushToText().rawTextOnly()
|
||||
//Map tag aliases
|
||||
flushed = when(flushed) {
|
||||
// Map tag aliases
|
||||
flushed = when (flushed) {
|
||||
"a" -> "artist"
|
||||
"c", "char" -> "character"
|
||||
"f" -> "female"
|
||||
@@ -179,7 +180,7 @@ class SearchEngine {
|
||||
else -> flushed
|
||||
}
|
||||
namespace = Namespace(flushed, null)
|
||||
} else if(char == ' ' && !inQuotes) {
|
||||
} else if (char == ' ' && !inQuotes) {
|
||||
flushAll()
|
||||
} else {
|
||||
queuedRawText.append(char)
|
||||
@@ -197,7 +198,6 @@ class SearchEngine {
|
||||
return string.replace("\\", "\\\\")
|
||||
.replace("_", "\\_")
|
||||
.replace("%", "\\%")
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package exh.search
|
||||
import exh.plusAssign
|
||||
import exh.search.SearchEngine.Companion.escapeLike
|
||||
|
||||
class Text: QueryComponent() {
|
||||
class Text : QueryComponent() {
|
||||
val components = mutableListOf<TextComponent>()
|
||||
|
||||
private var query: String? = null
|
||||
@@ -12,26 +12,26 @@ class Text: QueryComponent() {
|
||||
private var rawText: String? = null
|
||||
|
||||
fun asQuery(): String {
|
||||
if(query == null) {
|
||||
if (query == null) {
|
||||
query = rBaseBuilder().toString()
|
||||
}
|
||||
return query!!
|
||||
}
|
||||
|
||||
fun asLenientTitleQuery(): String {
|
||||
if(lenientTitleQuery == null) {
|
||||
if (lenientTitleQuery == null) {
|
||||
lenientTitleQuery = StringBuilder("%").append(rBaseBuilder()).append("%").toString()
|
||||
}
|
||||
return lenientTitleQuery!!
|
||||
}
|
||||
|
||||
fun asLenientTagQueries(): List<String> {
|
||||
if(lenientTagQueries == null) {
|
||||
if (lenientTagQueries == null) {
|
||||
lenientTagQueries = listOf(
|
||||
//Match beginning of tag
|
||||
// Match beginning of tag
|
||||
rBaseBuilder().append("%").toString(),
|
||||
//Tag word matcher (that matches multiple words)
|
||||
//Can't make it match a single word in Realm :(
|
||||
// Tag word matcher (that matches multiple words)
|
||||
// Can't make it match a single word in Realm :(
|
||||
StringBuilder(" ").append(rBaseBuilder()).append(" ").toString(),
|
||||
StringBuilder(" ").append(rBaseBuilder()).toString(),
|
||||
rBaseBuilder().append(" ").toString()
|
||||
@@ -42,8 +42,8 @@ class Text: QueryComponent() {
|
||||
|
||||
fun rBaseBuilder(): StringBuilder {
|
||||
val builder = StringBuilder()
|
||||
for(component in components) {
|
||||
when(component) {
|
||||
for (component in components) {
|
||||
when (component) {
|
||||
is StringTextComponent -> builder += escapeLike(component.value)
|
||||
is SingleWildcard -> builder += "_"
|
||||
is MultiWildcard -> builder += "%"
|
||||
@@ -52,7 +52,7 @@ class Text: QueryComponent() {
|
||||
return builder
|
||||
}
|
||||
|
||||
fun rawTextOnly() = if(rawText != null)
|
||||
fun rawTextOnly() = if (rawText != null)
|
||||
rawText!!
|
||||
else {
|
||||
rawText = components
|
||||
|
||||
Reference in New Issue
Block a user