views:

149

answers:

1

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.scalaquery.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
}
+1  A: 

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.

szeiger
Thanks for your answer and your project, szeiger. With your answer I also acquire how to use the ColumnOps object, thanks again.
Googol Shan