0

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?

Abie
  • 88
  • 4
  • Not related to your original question, but have you seen the h2-database inner browser? It works with any jdbc adapter and is quite nice and small (as I think). See at the bottom here: http://www.h2database.com/html/quickstart.html#h2_console – VasiliNovikov Aug 24 '13 at 12:26

1 Answers1

1

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.

ghik
  • 10,706
  • 1
  • 37
  • 50