所以我关注:http://slick.typesafe.com/doc/3.0.2/gettingstarted.html现在我想使用案例类而不是将每个模型定义为元组.
所以我有:
case class Character(id: Long, foreName: String, middleNames: String, lastName: String, age: Int)
//class Characters(tag: Tag) extends Table[(Int, String, String, String, Int)](tag, "characters")
class Characters(tag: Tag) extends Table[Characters](tag, "characters")
{
def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
def foreName = column[String]("forename")
def middleNames = column[String]("middlenames")
def lastName = column[String]("lastname")
def age = column[Int]("age")
// def * = (id, foreName, middleNames, lastName, age)
def * = (id, foreName, middleNames, lastName, age) <> (Character.tupled, Character.unapply _)
}
Run Code Online (Sandbox Code Playgroud)
但我得到: …
我正在寻找使用典型的主/从mysql架构配置Slick 3的最佳方法.基本上我想将写入发送到mysql服务器并读取到另一个并信任两个服务器是同步的.
我已经知道了[这] [1]但我认为是Slick 2.我已经开始使用Slick官方文档了,我对此主题没有任何看法.
现在,有一个数据库(没有主从)我在application.conf中有这个.
slick {
dbs {
default {
driver = "slick.driver.MySQLDriver$"
db {
driver = "com.mysql.jdbc.Driver"
url = "jdbc:mysql://localhost/new_chat"
user = "new_chat"
password = "new_chat"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我正在执行这样的查询:
PersistenceUtils.run(conversationMembers += conversationMember)
Run Code Online (Sandbox Code Playgroud)
PersistenceUtils:
def run[R](a: DBIOAction[R, NoStream, Nothing])
(implicit chatContext: ChatContext, ec: ExecutionContext): Future[R] = {
val result = chatContext.db.run(a)
result.onFailure({ case e => logger.error(s"error executing query: ${a.getDumpInfo.mainInfo}", e) })
result
}
Run Code Online (Sandbox Code Playgroud)
有一种方法可以在application.conf中进行配置吗?我不想在dev中使用master/slave,只能在staging和production中使用.我已经阅读了有关ReplicationDriver的内容,但我在Slick doc中看不到任何内容:S
有谁能给我一些线索?:P
谢谢
首先我想声明我是 slick 的新手,正在使用版本 3.1.1 。我一直在阅读手册,但我在查询工作时遇到问题。要么是我的连接字符串有问题,要么是我的 Slick 代码有问题。我从这里获取了我的配置http://slick.typesafe.com/doc/3.1.1/database.html并从页面底部获取了我的更新示例http://slick.typesafe.com/doc/3.1.1/查询.html。好的,这是我的代码
应用程序配置
mydb= {
dataSourceClass = org.postgresql.ds.PGSimpleDataSource
properties = {
databaseName = "Jsmith"
user = "postgres"
password = "unique"
}
numThreads = 10
}
Run Code Online (Sandbox Code Playgroud)
我的控制器 ——数据库表被称为——关系
package controllers
import play.api.mvc._
import slick.driver.PostgresDriver.api._
class Application extends Controller {
class relations(tag: Tag) extends Table[(Int,Int,Int)](tag, "relations") {
def id = column[Int]("id", O.PrimaryKey)
def me = column[Int]("me")
def following = column[Int]("following")
def * = (id,me,following)
}
val profiles = TableQuery[relations]
val db = Database.forConfig("mydb") …
Run Code Online (Sandbox Code Playgroud) 我有一个查询Seq[Int]
作为参数(并执行像一样的过滤WHERE x IN (...)
),由于该查询的操作复杂,因此需要对其进行编译。但是,当我尝试幼稚的方法时:
Compiled((xs: Set[Int]) => someQuery.filter(_.x inSet xs))
Run Code Online (Sandbox Code Playgroud)
它失败并显示以下消息
Computation of type Set[Int] => Query[SomeTable, SomeValue, Seq] cannot be compiled (as type C)
Run Code Online (Sandbox Code Playgroud)
Slick可以编译以一组整数作为参数的查询吗?
更新:我使用PostgreSQL作为数据库,因此可以使用数组代替IN
子句,但是如何?
我试图在 Slick 3.x 中捕获 SQL 错误。下面的代码不打印任何内容,但如果在调试下跟踪,它工作正常(它打印失败)。这段代码有什么问题?
object TestSlick extends App {
val db = Database.forConfig("dbconfig")
val sql = "update table_does_not_exist set zzz=1 where ccc=2"
val q = sqlu"#$sql"
db.run(q.asTry).map {result =>
result match {
case Success(r) => println(r)
case Failure(e) => {
println(s"SQL Error, ${e.getMessage}")
println("command:" + sql)
throw e
}
}
}
}
Run Code Online (Sandbox Code Playgroud) 通常,您将在事务中运行两个或更多语句.但是在我transactionally
在Slick 3中使用的所有示例中,当我通常for
在循环中使用时,可以理解这些语句的分组.
这有效(从事务中的两个表中删除):
val action = db.run((for {
_ <- table1.filter(_.id1 === id).delete
_ <- table2.filter(_.id2=== id).delete
} yield ()).transactionally)
val result = Await.result(action, Duration.Inf)
Run Code Online (Sandbox Code Playgroud)
但for/yield
需要吗?是否有另一种方法可以在事务中运行两个或多个语句?
我在Slick中有以下代码来更新对象用户:
val users = TableQuery[UserDB]
val action = users.filter(_.id === user.id).update(user)
val future = db.run(action)
val result = Await.result(future, Duration.Inf)
Run Code Online (Sandbox Code Playgroud)
但是我不想更新用户对象(密码)中的字段.怎么省略呢?
case class Account(var email:String, var pass:String, var familyId: Int, var accessId: Int, id: Option[Int] = None)
// A Accounts table with 5 columns: id, email, pass, familyId, accessId
class Accounts(tag: Tag) extends Table[Account](tag, "ACCOUNTS") {
def id = column[Int]("ID", O.AutoInc, O.PrimaryKey)
def email = column[String]("EMAIL")
def pass = column[String]("PASS")
def familyId = column[Int]("FAMILY_ID") // TODO: add fk family_id
def accessId = column[Int]("ACCESS_ID") // TODO: add fk access_id
def * = (email, pass, familyId, accessId, id.?) <> (Account.tupled, Account.unapply)
}
Run Code Online (Sandbox Code Playgroud)
创建具有这种结构的表后,表中的所有列都是 …
Slick 3通过使用flatMap方法提供DBIOAction组成。另外,我们可以在两个DBIOAction之间的后端进行一些计算。在大多数情况下,这很好用,但是当计算结果在诸如Future之类的monad中时,我该怎么办?有阻止方式的代码:
val fooQuery = TableQuery[FooTable]
val barQuery = TableQuery[BarTable]
def requestService(content: Iterable[String]): Future[Iterable[Long]] = ???
def modify(ids: Iterable[Long], change: String) = {
val query = fooQuery.filter(_.id inSet ids).result.flatMap{ fooSeq =>
val content = fooSeq.map(_.contentField)
val requestServiceFuture = requestService(content)
val serviceResult = Await.result(requestServiceFuture, 1.minute)
barQuery.filter(_.id inSet serviceResult).delete //or other action
}
db.run(query.transactionally)
}
Run Code Online (Sandbox Code Playgroud)
有没有办法在没有等待的情况下异步执行此代码?
我Json
在 MySql 中有一个类型列,我正在使用带有 Slick 的 Scala。我如何Json
通过 Slick为Column提供支持。
class SampleTable(tag: Tag) extends Table[(String, ??)](tag, "test") {
override def * : ProvenShape[NodeReference] = (name, data)
def name: Rep[String] = column[String]("name", O.PrimaryKey)
def Data: Rep[??] = column[??]("data", O.PrimaryKey)
}
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激。提前致谢