Is there a way to get a raw sql string from a Query object?

1,083 views
Skip to first unread message

Moisés Gramary Barbosa

unread,
Mar 2, 2017, 11:16:43 AM3/2/17
to Ebean ORM
I need to do a UNION of two queries of two tables/entities with same structure, but I don't want to do the full raw sql just for that simple query, so I'm wondering about doing a Query object, and get its raw sql, concat it with a " UNION " and repeat the process with the second Query object...

Is there a way to do that?

Thanks!

Regards.

Moisés Gramary Barbosa

unread,
Mar 2, 2017, 11:23:37 AM3/2/17
to Ebean ORM
Another ORMs like DoctrineORM does it with a simple "getSQL()" function...

http://www.doctrine-project.org/api/dbal/2.3/source-class-Doctrine.DBAL.Query.QueryBuilder.html#212-252

Rob Bygrave

unread,
Mar 4, 2017, 6:18:28 PM3/4/17
to ebean@googlegroups
In short "No" is the answer but ...



The Query object has a getGeneratedSql() method that returns the actual SQL String executed.  If you look at InQueryExpression.prepareExpression() we see:

public void prepareExpression(BeanQueryRequest<?> request) {

  CQuery<?> subQuery = compileSubQuery(request);
  this.bindParams = subQuery.getPredicates().getWhereExprBindValues();
  this.sql = subQuery.getGeneratedSql().replace('\n', ' ');
}

private CQuery<?> compileSubQuery(BeanQueryRequest<?> queryRequest) {

  SpiEbeanServer ebeanServer = (SpiEbeanServer) queryRequest.getEbeanServer();
  return ebeanServer.compileQuery(subQuery, queryRequest.getTransaction());
}


... so you will see a compileQuery() method on SpiEbeanServer (but not publicly exposed per say).

Ultimately this sounds like ... compile 2 queries, combine the result bind values and sql ... and then execute as RawSql or findNative().  So that is "possible" if you are keen etc but to me it is getting into a "questionable complexity" area.


The question I'd ask first is ...
A) can you create a DB view?
B) why not use RawSql ? ... noting that with RawSql Ebean can dynamically extend a parsed (or column defined) RawSql query with custom predicates and order by etc.

... I'd look at these options first before the compile/merge approach (which could get interesting with named parameters etc).



Cheers, Rob.





--

---
You received this message because you are subscribed to the Google Groups "Ebean ORM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ebean+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages