Smooth operator with Slick 3

Or how to use custom / database specific operators in Slick 3.x.

Pawel Dolega
Nov 12, 2017 · 5 min read

Problem

Laying out the groundwork

Database table definition
Slick table mapping

Workaround solution

Definition of database function performing XOR
Slick mapping of our XOR function
xor function in a projection clause
xor function in a selection clause
Query with Int and Option[Int]
Error:(66, 81) type mismatch;
found : slick.lifted.Rep[Option[Int]]
required: slick.lifted.Rep[Int]
XorTestTable.map(x => xor(x.number1, x.maybeNumber3)).result
Error:(66, 70) method columnToOptionColumn in trait API is deprecated (since 3.0): Use an explicit conversion to an Option column with `.?`
XorTestTable.map(x => xor(x.number1, x.maybeNumber3)).result

Proper solution

Implementation of logical operators in Slick
import slick.jdbc.MySQLProfile.api._
Extended MySQL profile containing new operator ^
import ExtendedMySQLProfile.api._
Custom ^ operator in Slick
Generated SQL with ^ operator
^ with multiple parameters (Int and Option[Int] mixed together)
SQL generated from query with ^ and three parameters

Conclusion

VirtusLab

Virtus Lab company blog

Pawel Dolega

Written by

VP of reactive & cloud engineering @ VirtusLab. Engineer / Manager / Entrepreneur. Too many interests to list them here.

VirtusLab

VirtusLab

Virtus Lab company blog