2

当尝试使用 scalaquery 检索 SQLite 数据库中文本列的长度时,它会生成错误的 SQL。我明白了:

SELECT "t1"."title" FROM "GoodPages" "t1" WHERE ({fn length("t1"."title")} > 65)

当查询真的应该是

SELECT "t1"."title" FROM "GoodPages" "t1" WHERE length("t1"."title") > 65

for我用于获取此查询的comp 是

for (f <- Foo if f.title.length > 65) yield f.title

我拥有的表定义是

object Foo extends Table[(Int,String)]("Foo") {
  def id = column[Int]("id")
  def title = column[String]("title")
  def * = id ~ title
}

似乎 scalaquery 只是生成了错误的length()函数,但我找不到代码中发生这种情况的位置,也没有在 Internet 上找到任何关于此的内容。

4

1 回答 1

1

The generated SQL you see uses JDBC escape syntax, more precisely JDBC escape syntax for fn keyword. This syntax can be used by JDBC drivers but unfortunately is not supported by the SQLite driver.

Slick 1.0.0 knows this as the following snippet from SQLiteDriver suggests

case Apply(j: Library.JdbcFunction, ch) if j != Library.Concat =>
  /* The SQLite JDBC driver does not support ODBC {fn ...} escapes, so we try
   * unescaped function calls by default */
  b"${j.name}("
  b.sep(ch, ",")(expr(_, true))
  b")"
case s: SimpleFunction if s.scalar =>
  /* The SQLite JDBC driver does not support ODBC {fn ...} escapes, so we try
   * unescaped function calls by default */
  b"${s.name}("
  b.sep(s.nodeChildren, ",")(expr(_, true))
  b")"

If Slick 1.0.0 doesn't work for you we can probably find another solution. Let me know.

于 2013-02-23T19:25:14.263 回答