sql - Is there any way to do multiple inserts/updates in Slick? -
in sql can this:
insert tbl_name (a,b,c) values(1,2,3),(4,5,6),(7,8,9);
is there way multiple/bulk/batch inserts or updates in slick?
can similar, @ least using sql plain queries ?
for inserts, andrew answered, use insertall.
def insertall(items:seq[mycaseclass])(implicit session:session) = { (items.size) match { case s if s > 0 => try { // basequery tablequery object basequery.insertall(tempitems :_*) } catch { case e:exception => e.printstacktrace() } some(tempitems(0)) case _ => none } }
for updates, you're sol. check out scala slick 2.0 updateall equivalent insertall? ended doing. paraphrase, here's code:
private def batchupdatequery = "update table set value = ? id = ?" /** * dropping jdbc b/c slick doesnt support batched update */ def batchupate(batch:list[mycaseclass])(implicit session:session) = { val pstmt = session.conn.preparestatement(batchupdatequery) batch map { mycaseclass => pstmt.setstring(1, mycaseclass.value) pstmt.setstring(2, mycaseclass.id) pstmt.addbatch() } session.withtransaction { pstmt.executebatch() } }
Comments
Post a Comment