将我的项目从MySQL切换到MS SQL Server并且遇到了问题...
我正在使用Slick,当我尝试执行查询时,我收到了错误
[SQLServerException: Incorrect syntax near '='.]
这是超级描述性的,除了我没有看到试图执行的光滑。因此,经过一些调试后,我将其缩小到导致此异常的过滤器。
//my join
var q = for {
license <- License
category <- Category if license.category === category.id
keyContact <- Contact if license.keyContact === keyContact.id
supplierContact <- Contact if license.supplierContact === supplierContact.id
pricing <- Pricing if license.pricing === pricing.id
location <- Location if license.location === location.id
} yield (license,category,keyContact,supplierContact,pricing,location)
}
//THESE are the lines that don't work!
if(!approved.isDefined) q=q.filter(!_._1.approved)
if(!pendings.isDefined) q=q.filter(_._1.approved)
if(!completes.isDefined) q=q.filter(x => (
x._5.price === -1 ||
x._5.serverCost === -1 ||
x._5.depreciation === -1 ||
((x._5.maintCost === -1 || x._5.maintCost.isNull) &&
x._1.maintAgreement === true)
))
if(!incompletes.isDefined) q=q.filter(x => (
x._5.price =!= -1 &&
x._5.serverCost =!= -1 &&
x._5.depreciation =!= -1 &&
(x._1.maintAgreement === false || x._5.maintCost =!= -1)
))
//The join works if I don't have these filters. I have other filters I left out that do work for simplicity and readability
调用了isDefined的值只是我作为过滤器传递给函数的选项。上面的代码适用于MySQL,但MSSQL似乎不喜欢它。有什么想法吗?
q=q.filter(!_._1.approved)
至少需要做什么?我也试过_._1 === false
。
编辑查询的调试信息...
[debug] s.s.s.BaseSession - Preparing statement: select top 100 percent s25."id", s25."category", s25."tool", s25."supplier", s25."manufacturer", s25."licenseType", s25."version_edition", s25."location", s25."keyContact", s25."supplierContact", s25."supportedBy", s25."licenseEnd", s25."maintAgreement", s25."maintEnd",s25."count", s25."pricing", s25."compatibility_notes", s25."other_notes", s25."approved", s26."id", s26."name", s27."id", s27."nameFirst", s27."nameLast", s27."email", s27."phone", s28."id", s28."nameFirst", s28."nameLast", s28."email", s28."phone", s29."id", s29."price", s29."licenseOneTime", s29."depreciation", s29."maintCost", s29."serverCost", s29."purchCostSaved", s29."maintCostSaved", s30."id", s30."name", s30."city", s30."state", s30."country", s30."contact" from "licenses" s25, "categories" s26, "contacts" s27, "contacts" s28, "pricing" s29, "locations" s30 where (((((s25."category" = s26."id") and (s25."keyContact" = s27."id")) and (s25."supplierContact" = s28."id")) and (s25."pricing" = s29."id")) and (s25."location" = s30."id")) and (not s25."approved") order by s26."name", s25."tool", s25."manufacturer", s30."name", s25."supplier"
有用的部分是最终结果。请注意,这仅在未定义批准时进行查询。
解决方案 我所做的是改变我的预测,以便在apply方法中进行类型转换。
def * = id ~ category ~ tool ~ supplier ~ manufacturer ~ licenseType ~ version_edition ~
location ~ keyContact ~ supplierContact ~ supportedBy.? ~ licenseEnd.? ~ maintAgreement ~
maintEnd.? ~ count ~ pricing ~ compatibility_notes.? ~ other_notes.? ~ approved <> (
{l => License(l._1,l._2,l._3,l._4,l._5,l._6,l._7,l._8,l._9,l._10,l._11,l._12,
(if(l._13 == 1) true else false),l._14,l._15,l._16,l._17,l._18,(if(l._19 == 1) true else false))},
{(l:License) => Some(l.id,l.category,l.tool,l.supplier,l.manufacturer,l.licenseType,l.version_edition,
l.location,l.keyContact,l.supplierContact,l.supportedBy,l.licenseEnd,(if(l.maintAgreement) 1 else 0),
l.maintEnd,l.count,l.pricing,l.compatibility_notes,l.other_notes,(if(l.approved) 1 else 0))})
这不是理想的解决方案,但它可以工作,并且比将我的案例类更改为Int =]更好。
答案 0 :(得分:2)
SQLServer不支持ANSI布尔数据类型。相反,它提供了一个BIT类型,实际上是一个缺少布尔语义的数值数据类型(ANSI声明布尔支持三个值true
,false
,unknown
当然null
)。
如果以前的考虑因素,这不能归咎于Slick的限制。
一个简单的解决方法是将布尔数据类型重新映射为数字类型,如果您的目标是开发多DBMS应用程序,则应提供“工厂”层。
编辑(SQL生成)
从您生成的SQL中可以看到
....
(not s25."approved")
....
应该是
....
(s25."approved" = 0)
....
手动尝试查询:它现在可以正常工作。