Skip to content
Snippets Groups Projects
Commit cd10ee01 authored by cmaeder's avatar cmaeder
Browse files

added grouping

parent fb545845
No related branches found
No related tags found
No related merge requests found
......@@ -9,13 +9,23 @@ import scala.collection.JavaConverters._
class QueryDB extends RouteBuilder {
def body2str(ask: Boolean, dirPrefix: String, count: Boolean, subString: String, field: String, b: Object): String = {
def body2str(ask: Boolean, dirPrefix: String, count: Boolean,
subString: String, field: String, group: String, b: Object): String = {
try {
(b.asInstanceOf[util.ArrayList[_]].asScala map {
case jm if group.nonEmpty =>
val sm = jm.asInstanceOf[util.HashMap[String, Object]].asScala
val groupVal = sm.get(group)
val countVal = sm.get("count")
(groupVal, countVal) match {
case (Some(str: String), Some(c: Integer)) =>
"occurrence count in \"" + group + "\" : " + c + " \"" + str + "\""
case _ => "unexpected result: " + sm
}
case jm if count =>
(if (ask) "occurrence count in \"" + field + "\" : "
else "") +
jm.asInstanceOf[util.HashMap[String, Integer]].asScala.getOrElse("COUNT(*)", 0) +
jm.asInstanceOf[util.HashMap[String, Integer]].asScala.getOrElse("count", 0) +
" \"" + subString + "\""
case jm if !count =>
val sm = jm.asInstanceOf[util.HashMap[String, String]].asScala
......@@ -27,13 +37,21 @@ class QueryDB extends RouteBuilder {
}
}
def queryValues(ask: Boolean, dirPrefix: String, count: Boolean, subString: String, field: String): Unit = {
def queryValues(ask: Boolean, dirPrefix: String, count: Boolean,
subString: String, field: String, group: String): Unit = {
val w = field + " LIKE '%" + subString.replaceAll("'", "''") + "%'"
val q = "db:SELECT " + (if (count) "COUNT(*)" else "*") + " FROM " + Table.name + " WHERE " + w
val q = "db:SELECT " + (group match {
case "" if count => "COUNT(*) as count"
case "" if !count => "*"
case _ => assert(Table.columns.contains(group))
group + ", COUNT(" + group + ") as count"
}) +
" FROM " + Table.name + " WHERE " + w +
(if (group.isEmpty) "" else " GROUP BY " + group + " ORDER BY count DESC")
val d = "direct:" + q
val r: RouteDefinition = d ==> {
-->(q).transform(ex =>
body2str(ask, dirPrefix, count, subString, field, ex.getIn.getBody))
body2str(ask, dirPrefix, count, subString, field, group, ex.getIn.getBody))
-->("stream:out")
}
getContext.addRouteDefinition(r)
......@@ -76,8 +94,8 @@ object QueryDB {
println("next search in field: " + field)
}
else {
if (ask) dbSearch.queryValues(ask, QueryDB.dirPrefix, count = false, ln, field)
dbSearch.queryValues(ask, QueryDB.dirPrefix, count = true, ln, field)
if (ask) dbSearch.queryValues(ask, QueryDB.dirPrefix, count = false, ln, field, "")
dbSearch.queryValues(ask, QueryDB.dirPrefix, count = true, ln, field, "")
}
}
}
......
......@@ -7,8 +7,8 @@ object TestQueryDB {
context.addRoutes(dbSearch)
context.start()
val dirPrefix = if (dir.endsWith("/")) dir else dir + "/"
dbSearch.queryValues(true, dirPrefix, false, "skipped", "shortMsg")
dbSearch.queryValues(true, dirPrefix, false, "", "errType")
dbSearch.queryValues(true, dirPrefix, true, "LocalError", "errType")
dbSearch.queryValues(true, dirPrefix, false, "skipped", "shortMsg", "")
dbSearch.queryValues(true, dirPrefix, true, "", "errType", "shortMsg")
dbSearch.queryValues(true, dirPrefix, true, "LocalError", "errType", "")
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment