Not able to to MTable.getTables.list

967 Views Asked by At

I wrote this very simple code which I saw in many places on the internet

import scala.slick.driver.H2Driver.simple._
import scala.concurrent.ExecutionContext.Implicits.global
import slick.jdbc.meta._
import scala.concurrent._
import ExecutionContext.Implicits.global
import scala.concurrent.duration._

object Helper {
  def printTableNames() : Unit = {
    val db = Database.forConfig("test1")
    db.withSession {implicit session =>
      val x = Await.result(MTable.getTables().list(), Duration.Inf).toList
      x.foreach(println)
    }
  }
}

But this gives me a cryptic error at runtime

[error] /Users/abhi/ScalaProjects/SlickTest/src/test/scala/HelloSpec.scala:13: overloaded method value getTables with alternatives:
[error]   => slick.profile.BasicStreamingAction[Vector[slick.jdbc.meta.MTable],slick.jdbc.meta.MTable,slick.dbio.Effect.Read] <and>
[error]   (namePattern: String)slick.profile.BasicStreamingAction[Vector[slick.jdbc.meta.MTable],slick.jdbc.meta.MTable,slick.dbio.Effect.Read] <and>
[error]   (cat: Option[String],schemaPattern: Option[String],namePattern: Option[String],types: Option[Seq[String]])slick.profile.BasicStreamingAction[Vector[slick.jdbc.meta.MTable],slick.jdbc.meta.MTable,slick.dbio.Effect.Read]
[error]  cannot be applied to ()
[error]       val x = Await.result(MTable.getTables().list(), Duration.Inf).toList
[error]                                   ^
[error] one error found
[error] (test:compileIncremental) Compilation failed
[error] Total time: 1 s, completed Jun 27, 2015 12:34:03 AM
2

There are 2 best solutions below

0
On

Looks like there are three getTables methods all of which do not match getTables(). Probably you're trying the getTables one (without the parenthesis)?

With that, you'd then call just list (not list()). Slick definition for list:

/** Execute the statement and return an immutable and fully materialized list of the results. */
final def list(implicit session: JdbcBackend#Session) = build[List[R]]

shows how there's no non-implicit parameter list. Therefore you want list rather than list().

BUT... I see that the returned object is a stream, implying that getTables returns a stream (db.stream rather than db.run). If you want the list, rather than a stream, cut out this unnecessary step and use db.run instead.

Putting all that together, this should work, assuming you want to use the stream (given what I know so far):

 Await.result(MTable.getTables, Duration.Inf).toList

(PS, it'd help if you mention the version of Slick you were using. Slick 3 very different to older versions.)

0
On

This

  val x = Await.result(MTable.getTables().list(), Duration.Inf).toList

should be

  val x = Await.result(MTable.getTables(), Duration.Inf).toList

Also x will fetch you a List of MTable something like

List(
  MTable(MQName(TEST.PUBLIC.arguments),TABLE,,None,None,None)
  ...
)

from which you can get the names, if you require only the names.