在groovy中使用像dsl這樣的簡單SQL,並且我有一些構建器類來創建select語句。在跨越多行時忽略Groovy中的括號?
例如
class SelectClause implements Clause {
final Object[] attrs;
final ModTupleDSL parent;
FromClause fromClause;
public SelectClause(ModTupleDSL parent, Object... attrs) {
this.parent = parent;
this.attrs = attrs;
}
public FromClause from (Object relation) {
fromClause = new FromClause(this, relation);
return fromClause;
}
}
和
class FromClause implements Clause {
final Object relation;
final SelectClause parent;
WhereClause whereClause;
public FromClause(SelectClause parent, Object relation) {
this.parent = parent;
this.relation = relation;
}
public WhereClause where(Closure predicate) {
whereClause = new WhereClause(this, predicate);
return whereClause;
}
}
等等
在我的劇本我現在可以這樣說:
select all from foo where {tuple, args -> tuple.get(id) == args[0]};
但是,如果我把換行符到它,這是一個錯誤。
select all
from foo
where {tuple, args -> tuple.get(id) == args[0]};
groovy.lang.MissingMethodException:
No signature of method: foo.Foo.from() is applicable for argument types:
(clojure.lang.Keyword) values: [:foo]|Possible solutions: grep(), find(),
find(groovy.lang.Closure), grep(java.lang.Object), wait(), any()
這是解析Groovy時可選括號的副作用嗎?
在預感上,我把反斜槓放在每一行的末尾,它似乎工作。例如
select all \
from foo \
where {tuple, args -> tuple.get(id) == args[0]};
有沒有更好的方法?