Added ability to set alias for root table of sql query.

This commit is contained in:
Ryszard-Trojnacki
2015-03-18 10:28:54 +01:00
parent 81aeca5475
commit f78f4ea64a
5 changed files with 63 additions and 1 deletions
+5
View File
@@ -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) {