如何使用光滑执行以下操作:
select * from users where location = 3 and (permissions & 4 = 4);
这可能吗?还是我必须使用原始sql?
答案 0 :(得分:0)
如果您的数据库支持SQL运算符,您可以为其定义Slick运算符,如下所述:http://slick.typesafe.com/doc/2.1.0/userdefined.html
答案 1 :(得分:0)
通过遵循数字运算符
的模式,我能够使按位运算符工作in ... / util / slick / BitwiseExtension.scala
package ...util.slick
import slick.ast.Library.SqlOperator
import slick.lifted.{BaseExtensionMethods, ExtensionMethods, Rep}
object Library {
val & = new SqlOperator("&")
}
trait BitwiseColumnExtensionMethods[B1, P1] extends Any with ExtensionMethods[B1, P1] {
def & [P2, R](e: Rep[P2])(implicit om: o#arg[B1, P2]#to[B1, R]) =
om.column(Library.&, n, e.toNode)
}
final class BaseBitwiseColumnExtensionMethods[P1](val c: Rep[P1]) extends AnyVal
with BitwiseColumnExtensionMethods[P1, P1]
with BaseExtensionMethods[P1]
in ... / util / slick / package.scala
package ...util
import slick.ast.{ BaseTypedType, NumericTypedType }
import slick.lifted.Rep
package object slick {
implicit def bitwiseColumnExtensionMethods[B1](c: Rep[B1])(implicit tm: BaseTypedType[B1] with NumericTypedType) = new BaseBitwiseColumnExtensionMethods[B1](c)
}
然后在查询中使用它:
import ...util._
Tables.users.filter({ r =>
(r.permissions & mask) === mask
})
您可以通过查看JdbcStatementBuilderComponent
(https://github.com/slick/slick/blob/6c638ea0688345044e3d3a362fab92c76db9a85b/slick/src/main/scala/slick/jdbc/JdbcStatementBuilderComponent.scala#L378)
case Apply(sym: Library.SqlOperator, ch) =>
b"\("
if(ch.length == 1) {
b"${sym.name} ${ch.head}"
} else b.sep(ch, " " + sym.name + " ")(expr(_))
b"\)"