How could I know if a database table is exists in ScalaQuery

前端 未结 6 955
日久生厌
日久生厌 2020-12-29 13:15

I\'m trying ScalaQuery, it is really amazing. I could defined the database table using Scala class, and query it easily.

But I would like to know, in the following c

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-29 13:51

    ScalaQuery version 0.9.4 includes a number of helpful SQL metadata wrapper classes in the org.scalaquery.meta package, such as MTable:

    http://scalaquery.org/doc/api/scalaquery-0.9.4/#org.scalaquery.meta.MTable

    In the test code for ScalaQuery, we can see examples of these classes being used. In particular, see org.scalaquery.test.MetaTest.

    I wrote this little function to give me a map of all the known tables, keyed by table name.

    import org.scalaquery.meta.{MTable}
    def makeTableMap(dbsess: Session) : Map[String, MTable] = {
        val tableList = MTable.getTables.list()(dbsess);
        val tableMap = tableList.map{t => (t.name.name, t)}.toMap;
        tableMap;
    }
    

    So now, before I create an SQL table, I can check "if (!tableMap.contains(tableName))".

提交回复
热议问题