mirror of
https://github.com/TheAnachronism/docspell.git
synced 2025-01-26 00:18:26 +00:00
Improve field query and fix mariadb's pickiness with parens
If no query is given, don't search with `like '%'`. MariaDB doesn't want parens around columns in the GROUP BY clause.
This commit is contained in:
parent
52c6659f9f
commit
0919eec3c2
@ -96,7 +96,10 @@ object OCustomFields {
|
||||
|
||||
def findAll(coll: Ident, nameQuery: Option[String]): F[Vector[CustomFieldData]] =
|
||||
store.transact(
|
||||
QCustomField.findAllLike(coll, nameQuery.map(WildcardString.apply).map(_.both))
|
||||
QCustomField.findAllLike(
|
||||
coll,
|
||||
nameQuery.map(WildcardString.apply).flatMap(_.both)
|
||||
)
|
||||
)
|
||||
|
||||
def findById(coll: Ident, field: Ident): F[Option[CustomFieldData]] =
|
||||
|
@ -2,10 +2,11 @@ package docspell.backend.ops
|
||||
|
||||
final class WildcardString private (str: String) {
|
||||
|
||||
def both: String =
|
||||
if (str.startsWith("\"") && str.endsWith("\"")) str.drop(1).dropRight(1)
|
||||
else if (str.startsWith("*") || str.endsWith("*")) str
|
||||
else s"*$str*"
|
||||
def both: Option[String] =
|
||||
if (str.startsWith("\"") && str.endsWith("\"")) Some(str.drop(1).dropRight(1))
|
||||
else if (str.startsWith("*") || str.endsWith("*")) Some(str)
|
||||
else if (str.trim == "") None
|
||||
else Some(s"*$str*")
|
||||
|
||||
}
|
||||
object WildcardString {
|
||||
|
@ -13,7 +13,7 @@ trait DoobieSyntax {
|
||||
groupBy(NonEmptyList.of(c0, cs: _*))
|
||||
|
||||
def groupBy(cs: NonEmptyList[Column]): Fragment =
|
||||
fr" GROUP BY (" ++ commas(cs.toList.map(_.f)) ++ fr")"
|
||||
fr" GROUP BY " ++ commas(cs.toList.map(_.f))
|
||||
|
||||
def coalesce(f0: Fragment, fs: Fragment*): Fragment =
|
||||
sql" coalesce(" ++ commas(f0 :: fs.toList) ++ sql") "
|
||||
|
@ -42,7 +42,7 @@ object QCustomField {
|
||||
|
||||
val nameCond = nameQuery.map(QueryWildcard.apply) match {
|
||||
case Some(q) =>
|
||||
or(fName.lowerLike(q), fLabel.lowerLike(q))
|
||||
or(fName.lowerLike(q), and(fLabel.isNotNull, fLabel.lowerLike(q)))
|
||||
case None =>
|
||||
Fragment.empty
|
||||
}
|
||||
|
@ -11,7 +11,9 @@ object QueryWildcard {
|
||||
if (n.endsWith("*")) s"${n.dropRight(1)}%"
|
||||
else n
|
||||
|
||||
prefix(suffix(value))
|
||||
val res = prefix(suffix(value))
|
||||
if (res == "%%") "%"
|
||||
else res
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -18,4 +18,9 @@ object QueryWildcardTest extends SimpleTestSuite {
|
||||
assertEquals("%name%", QueryWildcard("*name*"))
|
||||
assertEquals("%some other name%", QueryWildcard("*some other name*"))
|
||||
}
|
||||
|
||||
test("do not use multiple wildcards") {
|
||||
assertEquals("%", QueryWildcard("**"))
|
||||
assertEquals("%*%", QueryWildcard("***"))
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user