Created
July 17, 2018 09:16
-
-
Save vilinski/4e2402bb0667b15347611b79f251d3d0 to your computer and use it in GitHub Desktop.
doobie batch insert large rows
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cats._ | |
import cats.implicits._ | |
import doobie._ | |
import doobie.implicits._ | |
def setIntOpt(n: Int, v: Option[Int]): PreparedStatementIO[Unit] = | |
v match { | |
case None => FPS.setNull(n, java.sql.Types.INTEGER) | |
case Some(s) => FPS.setInt(n, s) | |
} | |
def setStringOpt(n: Int, v: Option[String]): PreparedStatementIO[Unit] = | |
v match { | |
case None => FPS.setNull(n, java.sql.Types.VARCHAR) | |
case Some(s) => FPS.setString(n, s) | |
} | |
def setDoubleOpt(n: Int, v: Option[Double]): PreparedStatementIO[Unit] = | |
v match { | |
case None => FPS.setNull(n, java.sql.Types.DOUBLE) | |
case Some(d) => FPS.setDouble(n, d) | |
} | |
type FVal = (Option[String], Option[Double]) | |
case class Row( | |
itemId: Option[Int], | |
detailId: Option[Int], | |
itId: Int, | |
objectType: Option[Int], | |
// ... | |
features: List[FVal] | |
) | |
val batch = List( | |
Row(Some(1), Some(1), 1, Some(1), List((Some("1"), Some(1.0)))), | |
Row(Some(2), Some(2), 2, Some(2), List((Some("2"), Some(2.0)))), | |
) | |
def prepareFeature(f: FVal, n: Int): PreparedStatementIO[Unit] = { | |
val i = n + n + 4 | |
setStringOpt(i, f._1) *> setDoubleOpt(i, f._2) | |
} | |
def prepareFeatures(fs: List[FVal]): PreparedStatementIO[List[Unit]] = | |
fs.zipWithIndex.traverse{ case (f,n) => prepareFeature(f,n) } | |
// ^ | |
// PreparedStatementIO[Unit] doesn't conform to expected type G_[B_] | |
def prepareRowHead(r: Row) = | |
setIntOpt(1, r.itemId) *> | |
setIntOpt(2, r.detailId) *> | |
FPS.setInt(3, r.itId) *> | |
setIntOpt(4, r.objectType) | |
def prepareRow(r: Row): PreparedStatementIO[Unit] = { | |
prepareRowHead(r) *> | |
prepareFeatures(r.features) *> | |
FPS.addBatch | |
} | |
def prepare(batch: List[Row]): PreparedStatementIO[List[Unit]] = | |
batch.traverse(prepareRow) | |
def stmt = | |
(1 to 99).mkString("insert into blah values (?,?,?,?", ",", ")") | |
val prog: ConnectionIO[List[Int]] = | |
HC.prepareStatement(stmt)(prepare(batch) *> HPS.executeBatch) | |
prog.transact(xa) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment