com.ursful.framework.orm.query.MultiQueryImpl Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2017 @ursful.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ursful.framework.orm.query;
import com.ursful.framework.orm.IMultiQuery;
import com.ursful.framework.orm.support.*;
import com.ursful.framework.orm.utils.ORMUtils;
import com.ursful.framework.orm.IQuery;
import com.ursful.framework.orm.annotation.RdTable;
import org.springframework.core.annotation.AnnotationUtils;
import java.util.*;
public class MultiQueryImpl extends QueryImpl implements IMultiQuery{
private List subQueries = new ArrayList();
private List usedAlias = new ArrayList();
public MultiQueryImpl(){}
private IMultiQuery parentQuery;
public MultiQueryImpl(IMultiQuery query){
this.parentQuery = query;
}
private List aliasList = new ArrayList();
private Map aliasTable = new HashMap();
private List groups = new ArrayList();
private List groupCountsSelectColumns = new ArrayList();
private List joins = new ArrayList();
private Class> returnClass;
private List returnColumns = new ArrayList();
private List fixReturnColumns = new ArrayList();
public IMultiQuery createQuery(Class> clazz, Column... columns) {
this.returnClass = clazz;
this.returnColumns.clear();
this.fixReturnColumns.clear();
if(columns != null) {
for (Column column : columns) {
returnColumns.add(column);
}
}
return this;
}
public IMultiQuery createQuery(Column... columns) {
this.returnColumns.clear();
this.fixReturnColumns.clear();
if(columns != null) {
for (Column column : columns) {
returnColumns.add(column);
}
}
return this;
}
public IMultiQuery createQuery(Class> clazz, Columns... columns){
this.returnClass = clazz;
this.returnColumns.clear();
this.fixReturnColumns.clear();
if(columns != null) {
for (Columns column : columns) {
if(!column.getColumnList().isEmpty()) {
returnColumns.addAll(column.getColumnList());
}
}
}
return this;
}
public IMultiQuery addReturnColumn(Column column){
if(column != null) {
this.returnColumns.add(column);
}
return this;
}
public IMultiQuery clearReturnColumns(){
this.returnColumns.clear();
return this;
}
public IMultiQuery addFixedReturnColumn(Column column){
if(column != null) {
this.fixReturnColumns.add(column);
}
return this;
}
public IMultiQuery clearFixedReturnColumns(){
this.fixReturnColumns.clear();
return this;
}
/////////////////////////////////
private String generateUniqueAlias(Object object){
int i = 0;
String prefixAlias = null;
if(object instanceof IQuery){
prefixAlias = "q";
}else if(object instanceof Class){
prefixAlias = ((Class>)object).getSimpleName().substring(0, 1).toLowerCase(Locale.ROOT);
}else{
prefixAlias = object.toString().substring(0, 1).toLowerCase(Locale.ROOT);
}
String alias = prefixAlias + i;
while(containsAlias(alias)){
i++;
alias = prefixAlias + i;
}
return alias;
}
public AliasTable join(IQuery query){
String alias = generateUniqueAlias(query);
return join(query, alias);
}
public AliasTable join(IQuery query, String alias){
AliasTable table = new AliasTable(query);
table.setAlias(alias);
aliasTable.put(alias, query);
addUsedAlias(alias);
return table;
}
public AliasTable table(IQuery query, String alias){
AliasTable table = new AliasTable(query);
table.setAlias(alias);
aliasList.add(alias);
addUsedAlias(alias);
aliasTable.put(alias, query);
return table;
}
public AliasTable table(IQuery query){
String alias = generateUniqueAlias(query);
return table(query, alias);
}
public AliasTable join(Class> clazz, String alias){
ORMUtils.whenEmpty(clazz, "AliasTable join class should not be null.");
RdTable rdTable = AnnotationUtils.findAnnotation(clazz, RdTable.class);
ORMUtils.whenEmpty(rdTable, "Should annotate RdTable");
AliasTable table = new AliasTable(clazz);
table.setAlias(alias);
aliasTable.put(alias, clazz);
addUsedAlias(alias);
return table;
}
public AliasTable join(Class> clazz){
String alias = generateUniqueAlias(clazz);
return join(clazz, alias);
}
@Override
public IMultiQuery whereEqual(Column left, Object value) {
return where(left, value, ExpressionType.CDT_EQUAL);
}
@Override
public IMultiQuery whereNotEqual(Column left, Object value) {
return where(left, value, ExpressionType.CDT_NOT_EQUAL);
}
@Override
public IMultiQuery whereLike(Column left, String value) {
return where(left, value, ExpressionType.CDT_LIKE);
}
@Override
public IMultiQuery whereNotLike(Column left, String value) {
return where(left, value, ExpressionType.CDT_NOT_LIKE);
}
@Override
public IMultiQuery whereStartWith(Column left, String value) {
return where(left, value, ExpressionType.CDT_START_WITH);
}
@Override
public IMultiQuery whereEndWith(Column left, String value) {
return where(left, value, ExpressionType.CDT_END_WITH);
}
@Override
public IMultiQuery whereNotStartWith(Column left, String value) {
return where(left, value, ExpressionType.CDT_NOT_START_WITH);
}
@Override
public IMultiQuery whereNotEndWith(Column left, String value) {
return where(left, value, ExpressionType.CDT_NOT_END_WITH);
}
@Override
public IMultiQuery whereLess(Column left, Object value) {
return where(left, value, ExpressionType.CDT_LESS);
}
@Override
public IMultiQuery whereLessEqual(Column left, Object value) {
return where(left, value, ExpressionType.CDT_LESS_EQUAL);
}
@Override
public IMultiQuery whereMore(Column left, Object value) {
return where(left, value, ExpressionType.CDT_MORE);
}
@Override
public IMultiQuery whereMoreEqual(Column left, Object value) {
return where(left, value, ExpressionType.CDT_MORE_EQUAL);
}
@Override
public IMultiQuery whereIn(Column left, Collection value) {
return where(left, value, ExpressionType.CDT_IN);
}
@Override
public IMultiQuery whereInValues(Column left, Object ... values){
List