在像在此堆栈溢出答案中生成查询对象或在ouchbase Java n1ql查询文档中创建示例之后,我们可以在将字符串查询发送到服务器之前获取字符串查询?我不想将其发送到服务器。只是想用DSL构建它,然后获取具有适当参数的完整查询的字符串,将流向服务器的内容并以另一种方式处理它。
Statement
只需调用即可将A转换为String toString()
:
import com.couchbase.client.java.document.json.JsonObject;
import com.couchbase.client.java.query.N1qlQuery;
import com.couchbase.client.java.query.Statement;
import static com.couchbase.client.java.query.Select.select;
import static com.couchbase.client.java.query.dsl.Expression.i;
import static com.couchbase.client.java.query.dsl.Expression.path;
import static com.couchbase.client.java.query.dsl.Expression.s;
import static com.couchbase.client.java.query.dsl.Expression.x;
Statement statement = select(path(i("travel-sample"), "*"))
.from(i("travel-sample"))
.where(x("name").eq(x("$airline_param"))
.and(x("type").eq(s("airline"))));
System.out.println(statement);
输出:
SELECT `travel-sample`.* FROM `travel-sample` WHERE name = $airline_param AND type = "airline"
如您所见,这不会进行任何参数替换。参数与语句分开发送到服务器。如果要查看发送到服务器的内容,可以致电N1qlQuery.n1ql()
:
JsonObject args = JsonObject.create().put("$airline_param", "FooFliers");
N1qlQuery q = N1qlQuery.parameterized(statement, args);
JsonObject statementAndArgs = q.n1ql();
System.out.println(statementAndArgs);
输出(预定):
{
"statement": "SELECT `travel-sample`.* FROM `travel-sample` WHERE name = $airline_param AND type = \"airline\"",
"$airline_param": "FooFliers"
}
如果您需要内联参数,那么您需要自己做这部分,要格外小心,以正确地转义字符串参数。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句