3

How do I implement https://stackoverflow.com/a/16392399/14731 using QueryDSL SQL?

I understand that

new SQLSubQuery().from(customer).where(customer.email.eq("foo@example.com"))

models

select customer where customer.email = 'foo@example.com'

but I don't understand how to select [literal] such as:

select 1 from customer or select 'foo@example.com', 0

as required by the aforementioned link.

Community
  • 1
  • 1
Gili
  • 86,244
  • 97
  • 390
  • 689

1 Answers1

8

If it is ok to use parameters then using constants should work

new SQLSubQuery().from(customer)
  .where(customer.email.eq("foo@example.com"))
  .list(Expressions.constant("foo@example.com"),
        Expressions.constant(0))

Expressions.constant is documented here http://www.querydsl.com/static/querydsl/3.2.3/apidocs/com/mysema/query/support/Expressions.html#constant%28T%29

Timo Westkämper
  • 21,824
  • 5
  • 78
  • 111
  • What do you mean `if it is ok to use parameters`? What parameters does the above introduce? – Gili Sep 10 '13 at 05:09
  • Parameters in prepared statements, your example has literals encoded into SQL. – Timo Westkämper Sep 10 '13 at 06:06
  • 1. So you're saying that your solution sets parameters in a `PreparedStatement` instead of injecting literals into the SQL directly? 2. Does QueryDSL support injecting literals into the statement, or does it only support the use of `PreparedStatement` parameters? – Gili Sep 10 '13 at 17:47
  • 1. Yes 2. You can inject SQL snippets via TemplateExpression instances, but there is no automatic conversion/escaping of generic constants to SQL inline literals – Timo Westkämper Sep 10 '13 at 18:25