开发者

How to use ScalaQuery to build a query for count(*) sql?

When I programming with the ScalaQuery, how to build a "select count(*) from table" statement?

I used a

Query(TestTable.count)

but the generated select statement is:

select count(*) from (select column1 from TestTable t2) t1

I want the:

select count(*) from TestTable

sorry for my poor english.


import org.scalaquery.ql.extended.MySQLDriver.Implicit._
import org.scalaquery.session._
import org.sc开发者_JAVA技巧alaquery.session.Database.threadLocalSession
import org.scalaquery.ql.Query
import org.scalaquery.ql.basic.{BasicTable => Table}

object Test {
  val db = Database.forURL(...)
  db withSession {
    val q = Query(TestTable.count)
    println(q.selectStatement)
  }
}
object TestTable extends Table[(Long, Int)]("test") {
  def id = column[Long]("id")
  def config = column[Int]("config")
  def * = id ~ config
}


I shouldn't have called it a bug so quickly. The generated code is correct but clearly not ideal. As of ScalaQuery 0.9.0, you can only get the desired statement by manually inserting the CountAll operator into the query AST:

TestTable.map(t => ColumnOps.CountAll(t))

I have just committed a change to improve this situation so that the unnecessary sub-query will be avoided in many cases. In ScalaQuery 0.9.1 your original attempt "Query(TestTable.count)" should work as expected.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜