如何运行使用Play Framework应用程序数据库的命令行scala脚本? [英] How do I run a comand line scala script that uses Play Framework app database?

查看:72
本文介绍了如何运行使用Play Framework应用程序数据库的命令行scala脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 app / scripts 文件夹中有这个(我在 app / 中创建了这个文件夹)。我不确定如何在这里正确设置类路径,因此我甚至没有运行它来知道它是否实际连接到数据库。如何从命令行以干净的方式运行它?

I have this on my app/scripts folder (I created this folder inside app/). I'm not sure how I can properly set the classpath here, thus I didn't even run this to know if it will actually connect to the database. How can I run this in a clean way from the command line?

package scripts

import scala.collection.TraversableOnce
import scala.collection.generic.SeqForwarder
import scala.io.Source
import scala.slick.jdbc.{StaticQuery => Q}
import scala.slick.session.Session
import scala.slick.session.Database
import play.api.db.DB
import tables.Campeonatos
import tables.Jogos
import org.postgresql.Driver
import play.api.test._
import play.api.test.Helpers._

// ...

class InsertJogosCSV extends App {
  val dao = new DAO()
  val application = FakeApplication()

  def insertJogos(csv: CSV)(implicit s: Session) = {
    val times = dao.getTimeIdByNameMap
    var count = 0
    csv foreach { case cols =>
      count += 1
      dao.insertJogo(cols, times)
    }
    count
  }

  val csvFilePath: String = args(0)
  val csv = new CSV(csvFilePath)
  csv.printLines
  running(application) {
    val realDatabase = Database.forDataSource(DB.getDataSource()(application))
    implicit val s = realDatabase.createSession
    insertJogos(csv)
  }
}


推荐答案

您可以通过在应用程序根目录下使用 play test:console 命令来实现此目的。首先,您可以将代码移动到主方法而不是扩展 App

You could achieve this by using the play test:console command at the root of your app. First you could probably move the code into a main method rather than extending App:

class InsertJogosCSV {
    def main(args: Array[String]) {
        val dao = new DAO()
        val application = FakeApplication()

        def insertJogos(csv: CSV)(implicit s: Session) = {....}

        ....
    }
}

然后运行 play test:console 命令并执行以下操作

then run the play test:console command and do the following

scala> import scripts.InsertJogosCSV
import scripts.InsertJogosCSV

scala> val insert = new InsertJogosCSV()
insert: scripts.InsertJogosCSV = scripts.InsertJogosCSV@7d5f9d2b

scala> insert.main
res0: .....

play test:console 默认情况下会将app文件夹中的所有内容添加到类路径以及脚本所需的 FakeApplication 上下文中。希望有所帮助。

The play test:console by default adds everything from the app folder to your class path as well as the FakeApplication context that you need for your script. Hope that helps.

类似的问题: https://stackoverflow.com/ a / 11297578/2556428

这篇关于如何运行使用Play Framework应用程序数据库的命令行scala脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆