db.withTransaction{ implicit session : Session =>
if( ! Query(b).filter(_.id==="something").exists.run ){
b.insert( "something" )
d.insert( (1,"something") )
}
}
2.0
val b = TableQuery[b]
db.withTransaction{ implicit session =>
if( ! b.filter(_.id==="something").exists.run ){
b += "something"
d += (1,"something")
}
}
val a = (for {
ns <- coffees.filter(_.name.startsWith("ESPRESSO")).map(_.name).result
_ <- DBIO.seq(ns.map(n => coffees.filter(_.name === n).delete): _*)
} yield ()).transactionally
val f: Future[Unit] = db.run(a)
val result: Future[Boolean] = db.run(products.filter(_.name==="foo").exists.result)
result.map { exists =>
if (!exists) {
products += Product(
None,
productName,
productPrice
)
}
}
val a = (for {
exists <- products.filter(_.name==="foo").exists.result
//???
// _ <- DBIO.seq(ns.map(n => coffees.filter(_.name === n).delete): _*)
} yield ()).transactionally
def createRequest(url: String, verb: String, hostAndPort: String, createdAt: Timestamp) =
(urlScanRequests returning urlScanRequests.map(_.id)) += (0L, url, verb, hostAndPort, createdAt)
def lookupRequest(url: Rep[String], verb: Rep[String]) =
urlScanRequests filter (r => r.url === url && r.verb === verb)
val lookupRequestWith = Compiled(lookupRequest _)
def lookupOrCreate(url: String, verb: String, host: String, createdAt: Timestamp = new Timestamp(new Date().getTime))
(implicit ec: ExecutionContext) =
lookupRequestWith(url, verb).result.headOption.flatMap {
case Some((id, url, verb, host, createdAt)) =>
DBIO.successful(ScanRequest(id, url, verb, host, createdAt))
case None =>
createRequest(url, verb, host, createdAt) map (id => ScanRequest(id, url, verb, host, createdAt))
} transactionally
--
---
You received this message because you are subscribed to the Google Groups "Slick / ScalaQuery" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalaquery+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/scalaquery/8fbf215b-6725-43d2-a0c5-dd3cbdb8320a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
val a = (
products.filter(_.name==="foo").exists.result.flatMap { exists =>
if (!exists) {
products += Product(
None,
productName,
productPrice
)
} else {
DBIO.successful(None) //no-op
}
}
).transactionally
val a = (
products.filter(_.name==="foo").result.headOption.flatMap {
case Some(product) =>
DBIO.successful(Option(product))
case None =>
val productId: FixedSqlAction[Int, NoStream, Effect.Write] =
(products returning products.map(_.id)) += Product(
None,
productName,
productPrice
)
val product = productId.map { id => //product is of type DBIOAction, no product
Product(
Option(id),
productName,
productPrice
)
}
product
// DBIO.successful(Option(product))
}
).transactionally
val b: Future[Any] = db.run(a)
val c: Future[Option[Product]] = db.run(a) // doesn't compile
case class Product(id: Option[Int], name: String, price: BigDecimal)
class Products(tag: Tag) extends Table[Product](tag, "product") {
def id = column[Int]("id", O.PrimaryKey, O.AutoInc) // This is the primary key column
def name = column[String]("name")
def price = column[BigDecimal]("price", O.SqlType("decimal(10, 4)"))
def * = (id.?, name, price) <> (Product.tupled, Product.unapply)
}
val products = TableQuery[Products]
class Products2(tag: Tag) extends Table[(Int, String, BigDecimal)](tag, "product") {
def id = column[Int]("id", O.PrimaryKey, O.AutoInc) // This is the primary key column
def name = column[String]("name")
def price = column[BigDecimal]("price", O.SqlType("decimal(10, 4)"))
def * = (id, name, price)
}
val products2 = TableQuery[Products2]
val a = (
products2.filter(_.name==="foo").result.headOption.flatMap {
case Some(product) =>
DBIO.successful(product)
case None =>
val productId =
(products2 returning products2.map(_.id)) += (
0,
productName,
productPrice
)
val product = productId.map { id => (
id,
productName,
productPrice
)
}
product
}
).transactionally
val b: Future[(Int, String, BigDecimal)] = db.run(a)
...