mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-10-24 15:55:55 +00:00
Added ability to set alias for root table of sql query.
This commit is contained in:
@@ -1230,4 +1230,9 @@ public interface Query<T> extends Serializable {
|
||||
* Return true if this query has forUpdate set.
|
||||
*/
|
||||
public boolean isForUpdate();
|
||||
|
||||
/**
|
||||
* Set root table alias.
|
||||
*/
|
||||
public Query<T> alias(String alias);
|
||||
}
|
||||
|
||||
@@ -611,4 +611,9 @@ public interface SpiQuery<T> extends Query<T> {
|
||||
* Return true if this query has been cancelled.
|
||||
*/
|
||||
public boolean isCancelled();
|
||||
|
||||
/**
|
||||
* Return root table alias set by {@link #alias(String)} command.
|
||||
*/
|
||||
public String getAlias();
|
||||
}
|
||||
|
||||
@@ -104,7 +104,7 @@ public class SqlTreeBuilder {
|
||||
this.queryDetail = query.getDetail();
|
||||
|
||||
this.predicates = predicates;
|
||||
this.alias = new SqlTreeAlias(request.getBeanDescriptor().getBaseTableAlias());
|
||||
this.alias = new SqlTreeAlias(request.getQuery().getAlias()==null?request.getBeanDescriptor().getBaseTableAlias():request.getQuery().getAlias());
|
||||
this.ctx = new DefaultDbSqlContext(alias, tableAliasPlaceHolder, columnAliasPrefix, !subQuery);
|
||||
}
|
||||
|
||||
|
||||
@@ -190,6 +190,11 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
|
||||
private boolean autoFetchTuned;
|
||||
|
||||
private boolean logSecondaryQuery;
|
||||
|
||||
/**
|
||||
* Root table alias. For {@link Query#alias(String)} command.
|
||||
*/
|
||||
private String rootTableAlias=null;
|
||||
|
||||
/**
|
||||
* The node of the bean or collection that fired lazy loading. Not null if profiling is on and
|
||||
@@ -680,6 +685,7 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
|
||||
builder.add(id != null);
|
||||
builder.add(rawSql == null ? 0 : rawSql.queryHash());
|
||||
builder.add(includeTableJoin != null ? includeTableJoin.queryHash() : 0);
|
||||
builder.add(rootTableAlias);
|
||||
|
||||
if (detail != null) {
|
||||
detail.queryPlanHash(request, builder);
|
||||
@@ -1284,6 +1290,17 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
|
||||
this.cancelableQuery = cancelableQuery;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Query<T> alias(String alias) {
|
||||
this.rootTableAlias=alias;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAlias() {
|
||||
return rootTableAlias;
|
||||
}
|
||||
|
||||
public void cancel() {
|
||||
synchronized (this) {
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.basic.CKeyParent;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestQueryAlias extends BaseTestCase {
|
||||
@Test
|
||||
public void testExists() {
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<CKeyParent> sq = Ebean.createQuery(CKeyParent.class).select("id.oneKey").alias("st0")
|
||||
.setAutofetch(false).where().query();
|
||||
|
||||
Query<CKeyParent> pq = Ebean.find(CKeyParent.class).alias("myt0").where().in("id.oneKey", sq).query();
|
||||
|
||||
pq.findList();
|
||||
|
||||
String sql = pq.getGeneratedSql();
|
||||
|
||||
System.out.println(sql);
|
||||
// Without alias command is should be:
|
||||
// select t0.one_key c0, t0.two_key c1, t0.name c2, t0.version c3, t0.assoc_id c4 from ckey_parent t0 where (t0.one_key) in (select t0.one_key from ckey_parent t0)
|
||||
// but with alias command SQL should look like this:
|
||||
// select myt0.one_key c0, myt0.two_key c1, myt0.name c2, myt0.version c3, myt0.assoc_id c4 from ckey_parent myt0 where (myt0.one_key) in (select st0.one_key from ckey_parent st0)
|
||||
|
||||
Assert.assertTrue(sql.indexOf("ckey_parent myt0")>0);
|
||||
Assert.assertTrue(sql.indexOf("in (select st0.one_key from ckey_parent st0)")>0);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user