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:
Eike Kettner 2020-11-25 21:06:38 +01:00
parent 52c6659f9f
commit 0919eec3c2
6 changed files with 19 additions and 8 deletions

View File

@ -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]] =

View File

@ -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 {

View File

@ -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") "

View File

@ -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
}

View File

@ -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
}
}

View File

@ -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("***"))
}
}