How to dump all tables using Squeryl

208 views Asked by At

I'm trying to write a quick data browser for a database using Squeryl but I have difficulty iterating over all the tables in a generic way. Based on the Squeryl SchoolDb example i tried the following:

  def browseTable(name: String) = {
     SchoolDb.tables.find(_.name == name) map { t=>
          val fields = t.posoMetaData.fieldsMetaData
          val rows = from (t) (s => select(s))
          // Print the columns
          println(fields.map(_.columnName).mkString("\t"))
          rows map { row =>
            println(fields.map(f => f.get(row)).mkstring("\t"))
          }
     } 

The compiler is not very happy with this attempt (Missing type type for 'row') and I can sort-of understand its dilemma. Explicitly declaring the parametr as Any just changes the comilation error to "No implicit view available from Any => org.squeryl.dsl.ast.TypedExpressionNode[_]" on 'f.get(row)'

How Can I either fix this issue or change the models (maybe adding a trait of some sort) to enable generic access to all data in all tables?

1

There are 1 answers

0
ghik On BEST ANSWER

The compiler complains because f.get method expects an AnyRef parameter. AFAIK, in Scala everything can be safely cast to AnyRef - the compiler will force necessary boxing if needed. So I think this should work: f.get(row.asInstanceOf[AnyRef])

EDIT: I just tested this and it works.