本文整理汇总了Scala中slick.dbio.NoStream类的典型用法代码示例。如果您正苦于以下问题:Scala NoStream类的具体用法?Scala NoStream怎么用?Scala NoStream使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了NoStream类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: EitherT
//设置package包名称以及导入依赖的类
package io.slicker.instances
import slick.dbio.{DBIOAction, Effect, NoStream}
import scala.concurrent.ExecutionContext
class EitherT[A, B, E <: Effect](val value: DBIOAction[Either[A, B], NoStream, E]) {
def map[C](f: B => C)(implicit
ec: ExecutionContext): EitherT[A, C, E] = new EitherT(value.map({
case Right(b) => Right(f(b))
case Left(a) => Left(a)
}))
def flatMap[C, F <: Effect](f: B => EitherT[A, C, F])(implicit
ec: ExecutionContext): EitherT[A, C, E with F] = {
new EitherT(value.flatMap {
case Right(b) => f(b).value
case Left(a) => DBIOAction.successful(Left(a))
})
}
def flatMapF[C, F <: Effect](f: B => DBIOAction[Either[A, C], NoStream, F])(implicit
ec: ExecutionContext): EitherT[A, C, E with F] = {
new EitherT(value.flatMap {
case Right(b) => f(b)
case Left(a) => DBIOAction.successful(Left(a))
})
}
def ensure(onFailure: => A)(predicate: B => Boolean)(implicit
ec: ExecutionContext): EitherT[A, B, E] = {
new EitherT(value.map {
case Right(b) if predicate(b) => Right(b)
case _ => Left(onFailure)
})
}
def assure(onFailure: B => A)(predicate: B => Boolean)(implicit
ec: ExecutionContext): EitherT[A, B, E] = {
new EitherT(value.map {
case Right(b) if predicate(b) => Right(b)
case Right(b) => Left(onFailure(b))
case Left(a) => Left(a)
})
}
def leftMap[D](f: A => D)(implicit
ec: ExecutionContext): EitherT[D, B, E] = new EitherT(value.map {
case Right(b) => Right(b)
case Left(a) => Left(f(a))
})
}
object EitherT {
def apply[A, B, E <: Effect](dBIOAction: DBIOAction[Either[A, B], NoStream, E]) = new EitherT(dBIOAction)
}
示例2: run
//设置package包名称以及导入依赖的类
package tutor.repo
import slick.dbio.{DBIOAction, NoStream}
import slick.jdbc.{H2Profile, JdbcProfile, OracleProfile}
import scala.concurrent.Future
trait DBConfigProvider {
val jdbcProfile: JdbcProfile
def run[T](action: DBIOAction[T, NoStream, Nothing]):Future[T]
}
trait OracleDB extends DBConfigProvider {
val jdbcProfile: JdbcProfile = OracleProfile
}
trait H2DB extends DBConfigProvider {
val jdbcProfile: JdbcProfile = H2Profile
def run[T](action: DBIOAction[T, NoStream, Nothing]):Future[T] = {
import jdbcProfile.api._
val db = Database.forConfig("h2mem1")
try {
db.run(action)
}finally db.close()
}
}
示例3: runAsync
//设置package包名称以及导入依赖的类
package services.db
import javax.inject.{ Inject, Singleton }
import com.google.inject.ImplementedBy
import play.api.db.slick.DatabaseConfigProvider
import slick.dbio.{ DBIOAction, NoStream }
import slick.driver.JdbcProfile
import scala.concurrent.duration.Duration
import scala.concurrent.{ Await, Future }
@ImplementedBy(classOf[DBServiceImpl])
trait DBService {
def runAsync[R](a: DBIOAction[R, NoStream, Nothing]): Future[R]
def run[R](a: DBIOAction[R, NoStream, Nothing]): R
}
@Singleton
class DBServiceImpl @Inject() (val dbConfigProvider: DatabaseConfigProvider) extends DBService {
private val db = dbConfigProvider.get[JdbcProfile].db
def runAsync[R](a: DBIOAction[R, NoStream, Nothing]): Future[R] = {
db.run(a)
}
def run[R](a: DBIOAction[R, NoStream, Nothing]): R = {
Await.result(runAsync(a), Duration.Inf)
}
}
示例4: SilentDbImporter
//设置package包名称以及导入依赖的类
package com.snapswap.db.helpers.runner
import java.sql.BatchUpdateException
import com.snapswap.db.errors.{DataError, EntityNotFound, InternalDataError, OverrideNotAllowed}
import org.postgresql.util.PSQLException
import slick.basic.BasicBackend
import slick.dbio.{DBIOAction, NoStream}
import slick.util.TreePrinter
import scala.annotation.tailrec
import scala.concurrent.{ExecutionContext, Future}
trait DbRunSafe {
private[this] val pSQLExceptionErrCodes = Set("23505", "23503")
implicit class SilentDbImporter[D <: BasicBackend#DatabaseDef](db: D) {
def runSafe[R](a: DBIOAction[R, NoStream, Nothing])(implicit ec: ExecutionContext): Future[R] = {
def additionalInfo = TreePrinter.default.get(a)
db.run(a).recoverWith {
case ex: UnsupportedOperationException if ex.getMessage == "empty.head" => // When query returns no results then UnsupportedOperationException("empty.head") is thrown instead of NoSuchElementException
Future.failed[R](EntityNotFound(dbDetails = Some(additionalInfo)))
case _: NoSuchElementException =>
Future.failed[R](EntityNotFound(dbDetails = Some(additionalInfo)))
case ex: PSQLException =>
// wrap with Option to avoid NullPointerException
Option(ex.getServerErrorMessage).flatMap(msg => Option(msg.getSQLState)) match {
case Some(sqlState) if pSQLExceptionErrCodes.contains(sqlState) =>
Future.failed[R](OverrideNotAllowed(dbDetails = Some(additionalInfo)))
case _ =>
Future.failed[R](InternalDataError(ex.getMessage, dbDetails = Some(additionalInfo)))
}
case ex: DataError =>
Future.failed[R](ex)
case ex: BatchUpdateException =>
Future.failed[R](InternalDataError(ex.getMessage, dbDetails = Some(getAllExceptions(ex))))
case ex =>
Future.failed[R](InternalDataError(ex.getMessage, dbDetails = Some(additionalInfo)))
}
}
}
@tailrec
private[this] def getAllExceptions(exception: BatchUpdateException, msg: List[String] = List.empty): String = exception match {
case ex: BatchUpdateException =>
ex.getNextException match {
case next: BatchUpdateException =>
getAllExceptions(ex, s"${next.getClass.getSimpleName}: ${next.getMessage}" :: msg)
case ex: PSQLException =>
(s"${ex.getClass.getSimpleName}: ${ex.getMessage}" :: msg).reverse.mkString(",")
}
case _ =>
msg.reverse.mkString(",")
}
}
object DbRunHelper extends DbRunSafe
示例5: runAsync
//设置package包名称以及导入依赖的类
package services.db
import javax.inject.{Inject, Singleton}
import com.google.inject.ImplementedBy
import play.api.db.slick.DatabaseConfigProvider
import slick.dbio.{DBIOAction, NoStream}
import slick.driver.JdbcProfile
import scala.concurrent.duration.Duration
import scala.concurrent.{Await, Future}
@ImplementedBy(classOf[DBServiceImpl])
trait DBService {
def runAsync[R](a: DBIOAction[R, NoStream, Nothing]): Future[R]
def run[R](a: DBIOAction[R, NoStream, Nothing]): R
}
@Singleton
class DBServiceImpl @Inject()(val dbConfigProvider: DatabaseConfigProvider) extends DBService {
private val db = dbConfigProvider.get[JdbcProfile].db
def runAsync[R](a: DBIOAction[R, NoStream, Nothing]): Future[R] = {
db.run(a)
}
def run[R](a: DBIOAction[R, NoStream, Nothing]): R = {
Await.result(runAsync(a), Duration.Inf)
}
}
示例6: BasicStreamingActionPartialTest
//设置package包名称以及导入依赖的类
package org.danielnixon.slickwarts
import org.danielnixon.slickwarts.{ BasicStreamingActionPartial => BasicStreamingActionWart }
import org.scalatest.FunSuite
import org.wartremover.test.WartTestTraverser
import slick.basic.BasicStreamingAction
import slick.dbio.Effect.All
import slick.dbio.NoStream
class BasicStreamingActionPartialTest extends FunSuite {
val basicStreamingAction = new BasicStreamingAction[Nothing, Nothing, All] {
override def head: ResultAction[Nothing, NoStream, All] = ???
override def headOption = ???
override def getDumpInfo = ???
}
test("can't call BasicStreamingAction#head") {
val result = WartTestTraverser(BasicStreamingActionWart) {
basicStreamingAction.head
}
assertResult(List("[wartremover:BasicStreamingActionPartial] BasicStreamingAction#head is disabled - use BasicStreamingAction#headOption instead"), "result.errors")(result.errors)
assertResult(List.empty, "result.warnings")(result.warnings)
}
test("BasicStreamingActionPartial wart obeys SuppressWarnings") {
val result = WartTestTraverser(BasicStreamingActionWart) {
@SuppressWarnings(Array("org.danielnixon.slickwarts.BasicStreamingActionPartial"))
val foo = basicStreamingAction.head
}
assertResult(List.empty, "result.errors")(result.errors)
assertResult(List.empty, "result.warnings")(result.warnings)
}
}
示例7: runAsync
//设置package包名称以及导入依赖的类
package services
import javax.inject.{Inject, Singleton}
import com.google.inject.ImplementedBy
import play.api.db.slick.DatabaseConfigProvider
import slick.dbio.{DBIOAction, NoStream}
import slick.driver.JdbcProfile
import scala.concurrent.duration.Duration
import scala.concurrent.{Await, Future}
@ImplementedBy(classOf[DBServiceImpl])
trait DBService {
def runAsync[R](a: DBIOAction[R, NoStream, Nothing]): Future[R]
def run[R](a: DBIOAction[R, NoStream, Nothing]): R
}
@Singleton
class DBServiceImpl @Inject()(val dbConfigProvider: DatabaseConfigProvider) extends DBService {
private val db = dbConfigProvider.get[JdbcProfile].db
def runAsync[R](a: DBIOAction[R, NoStream, Nothing]): Future[R] = {
db.run(a)
}
def run[R](a: DBIOAction[R, NoStream, Nothing]): R = {
Await.result(runAsync(a), Duration.Inf)
}
}
示例8: runAsync
//设置package包名称以及导入依赖的类
package services
import javax.inject.{Inject, Singleton}
import com.google.inject.ImplementedBy
import play.api.db.slick.DatabaseConfigProvider
import slick.backend.DatabasePublisher
import slick.dbio.{DBIOAction, NoStream, Streaming}
import slick.driver.JdbcProfile
import scala.concurrent.duration.Duration
import scala.concurrent.{Await, Future}
@ImplementedBy(classOf[DBServiceImpl])
trait DBService {
def runAsync[R](a: DBIOAction[R, NoStream, Nothing]): Future[R]
def run[R](a: DBIOAction[R, NoStream, Nothing]): R
def stream[R](a: DBIOAction[_, Streaming[R], Nothing]): DatabasePublisher[R]
}
@Singleton
class DBServiceImpl @Inject()(val dbConfigProvider: DatabaseConfigProvider) extends DBService {
private val db = dbConfigProvider.get[JdbcProfile].db
def runAsync[R](a: DBIOAction[R, NoStream, Nothing]): Future[R] = {
db.run(a)
}
def run[R](a: DBIOAction[R, NoStream, Nothing]): R = {
Await.result(runAsync(a), Duration.Inf)
}
def stream[R](a: DBIOAction[_, Streaming[R], Nothing]): DatabasePublisher[R] = {
db.stream(a)
}
}
示例9: migrate
//设置package包名称以及导入依赖的类
package com.github.xirc.slickx.migrator
import com.github.xirc.slickx.differ.ModelDiffer
import com.github.xirc.slickx.generator.SqlGenerator
import org.slf4j.LoggerFactory
import slick.dbio.{DBIOAction, Effect, NoStream}
import slick.driver.JdbcDriver
import slick.model.Model
import scala.concurrent.ExecutionContext
trait OtfMigrator {
type Database = driver.api.Database
lazy val logger = LoggerFactory.getLogger(getClass)
val driver: JdbcDriver
val generator: SqlGenerator
def migrate(newModel: Model)(
implicit executionContext: ExecutionContext
): DBIOAction[Unit, NoStream, Effect.All] =
{
import driver.api._
driver.createModel().map(oldModel => {
val diff = new ModelDiffer().diffModel(oldModel, newModel)
val sqls = generator.unwrap(generator.generateModel(diff))
val actions = sqls.map(sql => sqlu"#${sql}")
logger.debug(
s"""
|On the fly update (mysql)
|SQLs:
|${sqls.mkString("\n")}
""".stripMargin
)
// Don't use DBIO.sequence and DBIO.seq because these composer apply SQLs eagerly :-)
DBIO.fold(actions, 0)(_ + _).withPinnedSession
})
}
}