Continue to merge SearchCriteria2 and GenericSearchBuilder

This commit is contained in:
Alex Huang 2013-09-27 15:19:27 -07:00
parent af8832f6bd
commit a964cf549e
36 changed files with 467 additions and 316 deletions

View File

@ -202,7 +202,7 @@ public class EngineClusterDaoImpl extends GenericDaoBase<EngineClusterVO, Long>
@Override
public List<Long> listDisabledClusters(long zoneId, Long podId) {
GenericSearchBuilder<EngineClusterVO, Long> clusterIdSearch = createSearchBuilder(Long.class);
clusterIdSearch.selectField(clusterIdSearch.entity().getId());
clusterIdSearch.selectFields(clusterIdSearch.entity().getId());
clusterIdSearch.and("dataCenterId", clusterIdSearch.entity().getDataCenterId(), Op.EQ);
if(podId != null){
clusterIdSearch.and("podId", clusterIdSearch.entity().getPodId(), Op.EQ);
@ -224,12 +224,12 @@ public class EngineClusterDaoImpl extends GenericDaoBase<EngineClusterVO, Long>
public List<Long> listClustersWithDisabledPods(long zoneId) {
GenericSearchBuilder<EngineHostPodVO, Long> disabledPodIdSearch = _hostPodDao.createSearchBuilder(Long.class);
disabledPodIdSearch.selectField(disabledPodIdSearch.entity().getId());
disabledPodIdSearch.selectFields(disabledPodIdSearch.entity().getId());
disabledPodIdSearch.and("dataCenterId", disabledPodIdSearch.entity().getDataCenterId(), Op.EQ);
disabledPodIdSearch.and("allocationState", disabledPodIdSearch.entity().getAllocationState(), Op.EQ);
GenericSearchBuilder<EngineClusterVO, Long> clusterIdSearch = createSearchBuilder(Long.class);
clusterIdSearch.selectField(clusterIdSearch.entity().getId());
clusterIdSearch.selectFields(clusterIdSearch.entity().getId());
clusterIdSearch.join("disabledPodIdSearch", disabledPodIdSearch, clusterIdSearch.entity().getPodId(), disabledPodIdSearch.entity().getId(), JoinBuilder.JoinType.INNER);
clusterIdSearch.done();

View File

@ -269,7 +269,7 @@ public class EngineHostDaoImpl extends GenericDaoBase<EngineHostVO, Long> implem
AvailHypevisorInZone.done();
HostsInStatusSearch = createSearchBuilder(Long.class);
HostsInStatusSearch.selectField(HostsInStatusSearch.entity().getId());
HostsInStatusSearch.selectFields(HostsInStatusSearch.entity().getId());
HostsInStatusSearch.and("dc", HostsInStatusSearch.entity().getDataCenterId(), Op.EQ);
HostsInStatusSearch.and("pod", HostsInStatusSearch.entity().getPodId(), Op.EQ);
HostsInStatusSearch.and("cluster", HostsInStatusSearch.entity().getClusterId(), Op.EQ);

View File

@ -138,7 +138,7 @@ public class EngineHostPodDaoImpl extends GenericDaoBase<EngineHostPodVO, Long>
@Override
public List<Long> listDisabledPods(long zoneId) {
GenericSearchBuilder<EngineHostPodVO, Long> podIdSearch = createSearchBuilder(Long.class);
podIdSearch.selectField(podIdSearch.entity().getId());
podIdSearch.selectFields(podIdSearch.entity().getId());
podIdSearch.and("dataCenterId", podIdSearch.entity().getDataCenterId(), Op.EQ);
podIdSearch.and("allocationState", podIdSearch.entity().getAllocationState(), Op.EQ);
podIdSearch.done();

View File

@ -94,7 +94,7 @@ public class ClusterDaoImpl extends GenericDaoBase<ClusterVO, Long> implements C
ZoneClusterSearch.done();
ClusterIdSearch = createSearchBuilder(Long.class);
ClusterIdSearch.selectField(ClusterIdSearch.entity().getId());
ClusterIdSearch.selectFields(ClusterIdSearch.entity().getId());
ClusterIdSearch.and("dataCenterId", ClusterIdSearch.entity().getDataCenterId(), Op.EQ);
ClusterIdSearch.done();
}
@ -196,7 +196,7 @@ public class ClusterDaoImpl extends GenericDaoBase<ClusterVO, Long> implements C
@Override
public List<Long> listDisabledClusters(long zoneId, Long podId) {
GenericSearchBuilder<ClusterVO, Long> clusterIdSearch = createSearchBuilder(Long.class);
clusterIdSearch.selectField(clusterIdSearch.entity().getId());
clusterIdSearch.selectFields(clusterIdSearch.entity().getId());
clusterIdSearch.and("dataCenterId", clusterIdSearch.entity().getDataCenterId(), Op.EQ);
if (podId != null) {
clusterIdSearch.and("podId", clusterIdSearch.entity().getPodId(), Op.EQ);
@ -217,12 +217,12 @@ public class ClusterDaoImpl extends GenericDaoBase<ClusterVO, Long> implements C
public List<Long> listClustersWithDisabledPods(long zoneId) {
GenericSearchBuilder<HostPodVO, Long> disabledPodIdSearch = _hostPodDao.createSearchBuilder(Long.class);
disabledPodIdSearch.selectField(disabledPodIdSearch.entity().getId());
disabledPodIdSearch.selectFields(disabledPodIdSearch.entity().getId());
disabledPodIdSearch.and("dataCenterId", disabledPodIdSearch.entity().getDataCenterId(), Op.EQ);
disabledPodIdSearch.and("allocationState", disabledPodIdSearch.entity().getAllocationState(), Op.EQ);
GenericSearchBuilder<ClusterVO, Long> clusterIdSearch = createSearchBuilder(Long.class);
clusterIdSearch.selectField(clusterIdSearch.entity().getId());
clusterIdSearch.selectFields(clusterIdSearch.entity().getId());
clusterIdSearch.join("disabledPodIdSearch", disabledPodIdSearch, clusterIdSearch.entity().getPodId(), disabledPodIdSearch.entity().getId(), JoinBuilder.JoinType.INNER);
clusterIdSearch.done();

View File

@ -57,7 +57,7 @@ public class HostPodDaoImpl extends GenericDaoBase<HostPodVO, Long> implements H
DataCenterIdSearch.done();
PodIdSearch = createSearchBuilder(Long.class);
PodIdSearch.selectField(PodIdSearch.entity().getId());
PodIdSearch.selectFields(PodIdSearch.entity().getId());
PodIdSearch.and("dataCenterId", PodIdSearch.entity().getDataCenterId(), Op.EQ);
PodIdSearch.and("allocationState", PodIdSearch.entity().getAllocationState(), Op.EQ);
PodIdSearch.done();

View File

@ -56,7 +56,7 @@ public class StorageNetworkIpAddressDaoImpl extends GenericDaoBase<StorageNetwor
countInUserIp.done();
listInUseIp = createSearchBuilder(String.class);
listInUseIp.selectField(listInUseIp.entity().getIpAddress());
listInUseIp.selectFields(listInUseIp.entity().getIpAddress());
listInUseIp.and("rangeId", listInUseIp.entity().getRangeId(), Op.EQ);
listInUseIp.and("taken", listInUseIp.entity().getTakenAt(), Op.NNULL);
listInUseIp.done();

View File

@ -74,7 +74,7 @@ public class DomainDaoImpl extends GenericDaoBase<DomainVO, Long> implements Dom
FindAllChildrenSearch.done();
FindIdsOfAllChildrenSearch = createSearchBuilder(Long.class);
FindIdsOfAllChildrenSearch.selectField(FindIdsOfAllChildrenSearch.entity().getId());
FindIdsOfAllChildrenSearch.selectFields(FindIdsOfAllChildrenSearch.entity().getId());
FindIdsOfAllChildrenSearch.and("path", FindIdsOfAllChildrenSearch.entity().getPath(), SearchCriteria.Op.LIKE);
FindIdsOfAllChildrenSearch.done();

View File

@ -306,7 +306,7 @@ public class HostDaoImpl extends GenericDaoBase<HostVO, Long> implements HostDao
AvailHypevisorInZone.done();
HostsInStatusSearch = createSearchBuilder(Long.class);
HostsInStatusSearch.selectField(HostsInStatusSearch.entity().getId());
HostsInStatusSearch.selectFields(HostsInStatusSearch.entity().getId());
HostsInStatusSearch.and("dc", HostsInStatusSearch.entity().getDataCenterId(), Op.EQ);
HostsInStatusSearch.and("pod", HostsInStatusSearch.entity().getPodId(), Op.EQ);
HostsInStatusSearch.and("cluster", HostsInStatusSearch.entity().getClusterId(), Op.EQ);
@ -370,7 +370,7 @@ public class HostDaoImpl extends GenericDaoBase<HostVO, Long> implements HostDao
HostsInClusterSearch.done();
HostIdSearch = createSearchBuilder(Long.class);
HostIdSearch.selectField(HostIdSearch.entity().getId());
HostIdSearch.selectFields(HostIdSearch.entity().getId());
HostIdSearch.and("dataCenterId", HostIdSearch.entity().getDataCenterId(), Op.EQ);
HostIdSearch.done();

View File

@ -226,7 +226,7 @@ public class NetworkDaoImpl extends GenericDaoBase<NetworkVO, Long> implements N
OfferingAccountNetworkSearch.done();
GarbageCollectedSearch = createSearchBuilder(Long.class);
GarbageCollectedSearch.selectField(GarbageCollectedSearch.entity().getId());
GarbageCollectedSearch.selectFields(GarbageCollectedSearch.entity().getId());
SearchBuilder<NetworkOpVO> join7 = _ntwkOpDao.createSearchBuilder();
join7.and("activenics", join7.entity().getActiveNicsCount(), Op.EQ);
join7.and("gc", join7.entity().isGarbageCollected(), Op.EQ);

View File

@ -39,7 +39,7 @@ public class NetworkOpDaoImpl extends GenericDaoBase<NetworkOpVO, Long> implemen
super();
ActiveNicsSearch = createSearchBuilder(Integer.class);
ActiveNicsSearch.selectField(ActiveNicsSearch.entity().getActiveNicsCount());
ActiveNicsSearch.selectFields(ActiveNicsSearch.entity().getActiveNicsCount());
ActiveNicsSearch.and("network", ActiveNicsSearch.entity().getId(), Op.EQ);
ActiveNicsSearch.done();

View File

@ -57,7 +57,7 @@ public class NetworkServiceMapDaoImpl extends GenericDaoBase<NetworkServiceMapVO
DistinctProvidersSearch.and("networkId", DistinctProvidersSearch.entity().getNetworkId(), SearchCriteria.Op.EQ);
DistinctProvidersSearch.and("provider", DistinctProvidersSearch.entity().getProvider(), SearchCriteria.Op.EQ);
DistinctProvidersSearch.and("service", DistinctProvidersSearch.entity().getService(), SearchCriteria.Op.EQ);
DistinctProvidersSearch.selectField(DistinctProvidersSearch.entity().getProvider());
DistinctProvidersSearch.selectFields(DistinctProvidersSearch.entity().getProvider());
DistinctProvidersSearch.done();
}

View File

@ -35,7 +35,7 @@ public class PhysicalNetworkIsolationMethodDaoImpl extends GenericDaoBase<Physic
protected PhysicalNetworkIsolationMethodDaoImpl() {
super();
IsolationMethodSearch = createSearchBuilder(String.class);
IsolationMethodSearch.selectField(IsolationMethodSearch.entity().getIsolationMethod());
IsolationMethodSearch.selectFields(IsolationMethodSearch.entity().getIsolationMethod());
IsolationMethodSearch.and("physicalNetworkId", IsolationMethodSearch.entity().getPhysicalNetworkId(), Op.EQ);
IsolationMethodSearch.done();

View File

@ -35,7 +35,7 @@ public class PhysicalNetworkTagDaoImpl extends GenericDaoBase<PhysicalNetworkTag
protected PhysicalNetworkTagDaoImpl() {
super();
TagSearch = createSearchBuilder(String.class);
TagSearch.selectField(TagSearch.entity().getTag());
TagSearch.selectFields(TagSearch.entity().getTag());
TagSearch.and("physicalNetworkId", TagSearch.entity().getPhysicalNetworkId(), Op.EQ);
TagSearch.done();

View File

@ -52,31 +52,31 @@ public class PhysicalNetworkTrafficTypeDaoImpl extends GenericDaoBase<PhysicalNe
kvmAllFieldsSearch = createSearchBuilder(String.class);
kvmAllFieldsSearch.and("physicalNetworkId", kvmAllFieldsSearch.entity().getPhysicalNetworkId(), Op.EQ);
kvmAllFieldsSearch.and("trafficType", kvmAllFieldsSearch.entity().getTrafficType(), Op.EQ);
kvmAllFieldsSearch.selectField(kvmAllFieldsSearch.entity().getKvmNetworkLabel());
kvmAllFieldsSearch.selectFields(kvmAllFieldsSearch.entity().getKvmNetworkLabel());
kvmAllFieldsSearch.done();
xenAllFieldsSearch = createSearchBuilder(String.class);
xenAllFieldsSearch.and("physicalNetworkId", xenAllFieldsSearch.entity().getPhysicalNetworkId(), Op.EQ);
xenAllFieldsSearch.and("trafficType", xenAllFieldsSearch.entity().getTrafficType(), Op.EQ);
xenAllFieldsSearch.selectField(xenAllFieldsSearch.entity().getXenNetworkLabel());
xenAllFieldsSearch.selectFields(xenAllFieldsSearch.entity().getXenNetworkLabel());
xenAllFieldsSearch.done();
vmWareAllFieldsSearch = createSearchBuilder(String.class);
vmWareAllFieldsSearch.and("physicalNetworkId", vmWareAllFieldsSearch.entity().getPhysicalNetworkId(), Op.EQ);
vmWareAllFieldsSearch.and("trafficType", vmWareAllFieldsSearch.entity().getTrafficType(), Op.EQ);
vmWareAllFieldsSearch.selectField(vmWareAllFieldsSearch.entity().getVmwareNetworkLabel());
vmWareAllFieldsSearch.selectFields(vmWareAllFieldsSearch.entity().getVmwareNetworkLabel());
vmWareAllFieldsSearch.done();
simulatorAllFieldsSearch = createSearchBuilder(String.class);
simulatorAllFieldsSearch.and("physicalNetworkId", simulatorAllFieldsSearch.entity().getPhysicalNetworkId(), Op.EQ);
simulatorAllFieldsSearch.and("trafficType", simulatorAllFieldsSearch.entity().getTrafficType(), Op.EQ);
simulatorAllFieldsSearch.selectField(simulatorAllFieldsSearch.entity().getSimulatorNetworkLabel());
simulatorAllFieldsSearch.selectFields(simulatorAllFieldsSearch.entity().getSimulatorNetworkLabel());
simulatorAllFieldsSearch.done();
ovmAllFieldsSearch = createSearchBuilder(String.class);
ovmAllFieldsSearch.and("physicalNetworkId", ovmAllFieldsSearch.entity().getPhysicalNetworkId(), Op.EQ);
ovmAllFieldsSearch.and("trafficType", ovmAllFieldsSearch.entity().getTrafficType(), Op.EQ);
ovmAllFieldsSearch.selectField(ovmAllFieldsSearch.entity().getSimulatorNetworkLabel());
ovmAllFieldsSearch.selectFields(ovmAllFieldsSearch.entity().getSimulatorNetworkLabel());
ovmAllFieldsSearch.done();
}

View File

@ -36,7 +36,7 @@ public class RouterNetworkDaoImpl extends GenericDaoBase<RouterNetworkVO, Long>
super();
RouterNetworksSearch = createSearchBuilder(Long.class);
RouterNetworksSearch.selectField(RouterNetworksSearch.entity().getNetworkId());
RouterNetworksSearch.selectFields(RouterNetworksSearch.entity().getNetworkId());
RouterNetworksSearch.and("routerId", RouterNetworksSearch.entity().getRouterId(), Op.EQ);
RouterNetworksSearch.done();

View File

@ -59,7 +59,7 @@ public class SecurityGroupVMMapDaoImpl extends GenericDaoBase<SecurityGroupVMMap
ListVmIdBySecurityGroup = createSearchBuilder(Long.class);
ListVmIdBySecurityGroup.and("securityGroupId", ListVmIdBySecurityGroup.entity().getSecurityGroupId(), SearchCriteria.Op.EQ);
ListVmIdBySecurityGroup.selectField(ListVmIdBySecurityGroup.entity().getInstanceId());
ListVmIdBySecurityGroup.selectFields(ListVmIdBySecurityGroup.entity().getInstanceId());
ListVmIdBySecurityGroup.done();
ListBySecurityGroup = createSearchBuilder();

View File

@ -56,7 +56,7 @@ public class VpcServiceMapDaoImpl extends GenericDaoBase<VpcServiceMapVO, Long>
DistinctProvidersSearch = createSearchBuilder(String.class);
DistinctProvidersSearch.and("vpcId", DistinctProvidersSearch.entity().getVpcId(), SearchCriteria.Op.EQ);
DistinctProvidersSearch.and("provider", DistinctProvidersSearch.entity().getProvider(), SearchCriteria.Op.EQ);
DistinctProvidersSearch.selectField(DistinctProvidersSearch.entity().getProvider());
DistinctProvidersSearch.selectFields(DistinctProvidersSearch.entity().getProvider());
DistinctProvidersSearch.done();
}

View File

@ -76,7 +76,7 @@ public class NetworkOfferingDaoImpl extends GenericDaoBase<NetworkOfferingVO, Lo
AllFieldsSearch.done();
UpgradeSearch = createSearchBuilder(Long.class);
UpgradeSearch.selectField(UpgradeSearch.entity().getId());
UpgradeSearch.selectFields(UpgradeSearch.entity().getId());
UpgradeSearch.and("physicalNetworkId", UpgradeSearch.entity().getId(), Op.NEQ);
UpgradeSearch.and("physicalNetworkId", UpgradeSearch.entity().isSystemOnly(), Op.EQ);
UpgradeSearch.and("trafficType", UpgradeSearch.entity().getTrafficType(), Op.EQ);

View File

@ -72,7 +72,7 @@ public class NetworkOfferingServiceMapDaoImpl extends GenericDaoBase<NetworkOffe
DistinctProvidersSearch = createSearchBuilder(String.class);
DistinctProvidersSearch.and("offId", DistinctProvidersSearch.entity().getNetworkOfferingId(), SearchCriteria.Op.EQ);
DistinctProvidersSearch.and("provider", DistinctProvidersSearch.entity().getProvider(), SearchCriteria.Op.EQ);
DistinctProvidersSearch.selectField(DistinctProvidersSearch.entity().getProvider());
DistinctProvidersSearch.selectFields(DistinctProvidersSearch.entity().getProvider());
DistinctProvidersSearch.done();
}

View File

@ -50,13 +50,13 @@ public class ProjectAccountDaoImpl extends GenericDaoBase<ProjectAccountVO, Long
AllFieldsSearch.done();
AdminSearch = createSearchBuilder(Long.class);
AdminSearch.selectField(AdminSearch.entity().getProjectId());
AdminSearch.selectFields(AdminSearch.entity().getProjectId());
AdminSearch.and("role", AdminSearch.entity().getAccountRole(), Op.EQ);
AdminSearch.and("accountId", AdminSearch.entity().getAccountId(), Op.EQ);
AdminSearch.done();
ProjectAccountSearch = createSearchBuilder(Long.class);
ProjectAccountSearch.selectField(ProjectAccountSearch.entity().getProjectAccountId());
ProjectAccountSearch.selectFields(ProjectAccountSearch.entity().getProjectAccountId());
ProjectAccountSearch.and("accountId", ProjectAccountSearch.entity().getAccountId(), Op.EQ);
ProjectAccountSearch.done();

View File

@ -51,7 +51,7 @@ public class VersionDaoImpl extends GenericDaoBase<VersionVO, Long> implements V
super();
CurrentVersionSearch = createSearchBuilder(String.class);
CurrentVersionSearch.selectField(CurrentVersionSearch.entity().getVersion());
CurrentVersionSearch.selectFields(CurrentVersionSearch.entity().getVersion());
CurrentVersionSearch.and("step", CurrentVersionSearch.entity().getStep(), Op.EQ);
CurrentVersionSearch.done();

View File

@ -96,7 +96,7 @@ public class AccountDaoImpl extends GenericDaoBase<AccountVO, Long> implements A
NonProjectAccountSearch.done();
AccountIdsSearch = createSearchBuilder(Long.class);
AccountIdsSearch.selectField(AccountIdsSearch.entity().getId());
AccountIdsSearch.selectFields(AccountIdsSearch.entity().getId());
AccountIdsSearch.and("ids", AccountIdsSearch.entity().getDomainId(), Op.IN);
AccountIdsSearch.done();
}

View File

@ -198,7 +198,7 @@ public class VMInstanceDaoImpl extends GenericDaoBase<VMInstanceVO, Long> implem
HostNameSearch.done();
FindIdsOfVirtualRoutersByAccount = createSearchBuilder(Long.class);
FindIdsOfVirtualRoutersByAccount.selectField(FindIdsOfVirtualRoutersByAccount.entity().getId());
FindIdsOfVirtualRoutersByAccount.selectFields(FindIdsOfVirtualRoutersByAccount.entity().getId());
FindIdsOfVirtualRoutersByAccount.and("account", FindIdsOfVirtualRoutersByAccount.entity().getAccountId(), SearchCriteria.Op.EQ);
FindIdsOfVirtualRoutersByAccount.and("type", FindIdsOfVirtualRoutersByAccount.entity().getType(), SearchCriteria.Op.EQ);
FindIdsOfVirtualRoutersByAccount.and("state", FindIdsOfVirtualRoutersByAccount.entity().getState(), SearchCriteria.Op.NIN);
@ -233,7 +233,7 @@ public class VMInstanceDaoImpl extends GenericDaoBase<VMInstanceVO, Long> implem
nicSearch.and("networkId", nicSearch.entity().getNetworkId(), SearchCriteria.Op.EQ);
DistinctHostNameSearch = createSearchBuilder(String.class);
DistinctHostNameSearch.selectField(DistinctHostNameSearch.entity().getHostName());
DistinctHostNameSearch.selectFields(DistinctHostNameSearch.entity().getHostName());
DistinctHostNameSearch.and("types", DistinctHostNameSearch.entity().getType(), SearchCriteria.Op.IN);
DistinctHostNameSearch.and("removed", DistinctHostNameSearch.entity().getRemoved(), SearchCriteria.Op.NULL);

View File

@ -56,7 +56,7 @@ public class AffinityGroupVMMapDaoImpl extends GenericDaoBase<AffinityGroupVMMap
ListVmIdByAffinityGroup = createSearchBuilder(Long.class);
ListVmIdByAffinityGroup.and("affinityGroupId", ListVmIdByAffinityGroup.entity().getAffinityGroupId(),
SearchCriteria.Op.EQ);
ListVmIdByAffinityGroup.selectField(ListVmIdByAffinityGroup.entity().getInstanceId());
ListVmIdByAffinityGroup.selectFields(ListVmIdByAffinityGroup.entity().getInstanceId());
ListVmIdByAffinityGroup.done();
ListByAffinityGroup = createSearchBuilder();
@ -89,7 +89,7 @@ public class AffinityGroupVMMapDaoImpl extends GenericDaoBase<AffinityGroupVMMap
ListAffinityGroupIdByVm = createSearchBuilder(Long.class);
ListAffinityGroupIdByVm.and("instanceId", ListAffinityGroupIdByVm.entity().getInstanceId(),
SearchCriteria.Op.EQ);
ListAffinityGroupIdByVm.selectField(ListAffinityGroupIdByVm.entity().getAffinityGroupId());
ListAffinityGroupIdByVm.selectFields(ListAffinityGroupIdByVm.entity().getAffinityGroupId());
ListAffinityGroupIdByVm.done();
}

View File

@ -27,15 +27,16 @@ import java.util.List;
import javax.inject.Inject;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.apache.cloudstack.engine.subsystem.api.storage.DataObject;
import org.apache.cloudstack.engine.subsystem.api.storage.DataStore;
import org.apache.cloudstack.engine.subsystem.api.storage.EndPoint;
import org.apache.cloudstack.engine.subsystem.api.storage.EndPointSelector;
import org.apache.cloudstack.engine.subsystem.api.storage.Scope;
import org.apache.cloudstack.storage.RemoteHostEndPoint;
import org.apache.cloudstack.storage.LocalHostEndpoint;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.apache.cloudstack.storage.RemoteHostEndPoint;
import com.cloud.host.Host;
import com.cloud.host.HostVO;
@ -44,10 +45,9 @@ import com.cloud.host.dao.HostDao;
import com.cloud.storage.DataStoreRole;
import com.cloud.storage.ScopeType;
import com.cloud.utils.db.DB;
import com.cloud.utils.db.SearchCriteria2;
import com.cloud.utils.db.SearchCriteria.Op;
import com.cloud.utils.db.SearchCriteria2;
import com.cloud.utils.db.Transaction;
import com.cloud.utils.db.SearchCriteria.Op;
import com.cloud.utils.exception.CloudRuntimeException;
@Component
@ -55,7 +55,7 @@ public class DefaultEndPointSelector implements EndPointSelector {
private static final Logger s_logger = Logger.getLogger(DefaultEndPointSelector.class);
@Inject
HostDao hostDao;
private String findOneHostOnPrimaryStorage = "select h.id from host h, storage_pool_host_ref s where h.status = 'Up' and h.type = 'Routing' and h.resource_state = 'Enabled' and" +
private final String findOneHostOnPrimaryStorage = "select h.id from host h, storage_pool_host_ref s where h.status = 'Up' and h.type = 'Routing' and h.resource_state = 'Enabled' and" +
" h.id = s.host_id and s.pool_id = ? ";
protected boolean moveBetweenPrimaryImage(DataStore srcStore, DataStore destStore) {
@ -261,7 +261,7 @@ public class DefaultEndPointSelector implements EndPointSelector {
SearchCriteria2<HostVO, HostVO> sc = SearchCriteria2.create(HostVO.class);
sc.addAnd(sc.getEntity().getClusterId(), Op.EQ, store.getScope().getScopeId());
sc.addAnd(sc.getEntity().getStatus(), Op.EQ, Status.Up);
List<HostVO> hosts = sc.find();
List<HostVO> hosts = sc.list();
for (HostVO host : hosts) {
endPoints.add(RemoteHostEndPoint.getHypervisorHostEndPoint(host.getId(), host.getPrivateIpAddress(),
host.getPublicIpAddress()));

View File

@ -928,7 +928,6 @@ public abstract class GenericDaoBase<T, ID extends Serializable> extends Compone
}
@Override @DB(txn=false)
@SuppressWarnings("unchecked")
public T findByUuid(final String uuid) {
SearchCriteria<T> sc = createSearchCriteria();
sc.addAnd("uuid", SearchCriteria.Op.EQ, uuid);
@ -936,7 +935,6 @@ public abstract class GenericDaoBase<T, ID extends Serializable> extends Compone
}
@Override @DB(txn=false)
@SuppressWarnings("unchecked")
public T findByUuidIncludingRemoved(final String uuid) {
SearchCriteria<T> sc = createSearchCriteria();
sc.addAnd("uuid", SearchCriteria.Op.EQ, uuid);
@ -1044,7 +1042,7 @@ public abstract class GenericDaoBase<T, ID extends Serializable> extends Compone
@DB(txn=false)
protected List<Object> addGroupBy(final StringBuilder sql, SearchCriteria<?> sc) {
Pair<GroupBy<?, ?>, List<Object>> groupBys = sc.getGroupBy();
Pair<GroupBy<?, ?, ?>, List<Object>> groupBys = sc.getGroupBy();
if (groupBys != null) {
groupBys.first().toSql(sql);
return groupBys.second();
@ -1368,7 +1366,7 @@ public abstract class GenericDaoBase<T, ID extends Serializable> extends Compone
Object obj = entry.getValue();
EcInfo ec = (EcInfo)attr.attache;
Enumeration en = null;
Enumeration<?> en = null;
if (ec.rawClass == null) {
en = Collections.enumeration(Arrays.asList((Object[])obj));
} else {

View File

@ -19,14 +19,9 @@ package com.cloud.utils.db;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import net.sf.cglib.proxy.Factory;
import com.cloud.utils.db.SearchCriteria.Func;
import com.cloud.utils.db.SearchCriteria.Op;
import com.cloud.utils.db.SearchCriteria.SelectType;
/**
* GenericSearchBuilder is used to build a search based on a VO object
@ -37,34 +32,22 @@ import com.cloud.utils.db.SearchCriteria.SelectType;
* @param <K> Result object that should contain the results.
*/
public class GenericSearchBuilder<T, K> extends SearchBase<T, K> {
@SuppressWarnings("unchecked")
protected GenericSearchBuilder(Class<T> entityType, Class<K> resultType) {
super(entityType, resultType);
}
public T entity() {
return _entity;
}
protected Attribute getSpecifiedAttribute() {
if (_entity == null || _specifiedAttrs == null || _specifiedAttrs.size() != 1) {
throw new RuntimeException("Now now, better specify an attribute or else we can't help you");
}
return _specifiedAttrs.get(0);
}
public GenericSearchBuilder<T, K> selectField(Object... useless) {
public GenericSearchBuilder<T, K> selectFields(Object... useless) {
if (_entity == null) {
throw new RuntimeException("SearchBuilder cannot be modified once it has been setup");
}
if (_specifiedAttrs.size() <= 0) {
throw new RuntimeException("You didn't specify any attributes");
}
if (_selects == null) {
_selects = new ArrayList<Select>();
}
for (Attribute attr : _specifiedAttrs) {
Field field = null;
try {
@ -75,12 +58,12 @@ public class GenericSearchBuilder<T, K> extends SearchBase<T, K> {
}
_selects.add(new Select(Func.NATIVE, attr, field, null));
}
_specifiedAttrs.clear();
return this;
}
/**
* Specifies the field to select.
*
@ -130,13 +113,6 @@ public class GenericSearchBuilder<T, K> extends SearchBase<T, K> {
return this;
}
@Override
protected void set(String name) {
Attribute attr = _attrs.get(name);
assert (attr != null) : "Searching for a field that's not there: " + name;
_specifiedAttrs.add(attr);
}
/**
* Adds an AND condition to the SearchBuilder.
*
@ -155,11 +131,6 @@ public class GenericSearchBuilder<T, K> extends SearchBase<T, K> {
return this;
}
public Preset and(Object useless, Op op) {
Condition condition = constructCondition(UUID.randomUUID().toString(), " AND ", _specifiedAttrs.get(0), op);
return new Preset(this, condition);
}
public GenericSearchBuilder<T, K> and() {
constructCondition(null, " AND ", null, null);
return this;
@ -182,10 +153,6 @@ public class GenericSearchBuilder<T, K> extends SearchBase<T, K> {
return and(name, useless, op);
}
public Preset where(Object useless, Op op) {
return and(useless, op);
}
public GenericSearchBuilder<T, K> left(String name, Object useless, Op op) {
constructCondition(name, " ( ", _specifiedAttrs.get(0), op);
return this;
@ -196,19 +163,10 @@ public class GenericSearchBuilder<T, K> extends SearchBase<T, K> {
return this;
}
public Preset left(Object useless, Op op) {
Condition condition = constructCondition(UUID.randomUUID().toString(), " ( ", _specifiedAttrs.get(0), op);
return new Preset(this, condition);
}
public GenericSearchBuilder<T, K> op(Object useless, Op op, String name) {
return left(useless, op, name);
}
public Preset op(Object useless, Op op) {
return left(useless, op);
}
public GenericSearchBuilder<T, K> op(String name, Object useless, Op op) {
return left(name, useless, op);
}
@ -221,19 +179,12 @@ public class GenericSearchBuilder<T, K> extends SearchBase<T, K> {
return left(name, useless, op);
}
public Preset openParen(Object useless, Op op) {
return left(useless, op);
}
public GroupBy<T, K> groupBy(Object... useless) {
public GroupBy<GenericSearchBuilder<T, K>, T, K> groupBy(Object... useless) {
assert _groupBy == null : "Can't do more than one group bys";
_groupBy = new GroupBy<T, K>(this);
GroupBy<GenericSearchBuilder<T, K>, T, K> groupBy = new GroupBy<GenericSearchBuilder<T, K>, T, K>(this);
_groupBy = groupBy;
return _groupBy;
}
protected List<Attribute> getSpecifiedAttributes() {
return _specifiedAttrs;
return groupBy;
}
/**
@ -254,11 +205,6 @@ public class GenericSearchBuilder<T, K> extends SearchBase<T, K> {
return this;
}
public Preset or(Object useless, Op op) {
Condition condition = constructCondition(UUID.randomUUID().toString(), " OR ", _specifiedAttrs.get(0), op);
return new Preset(this, condition);
}
public GenericSearchBuilder<T, K> join(String name, GenericSearchBuilder<?, ?> builder, Object useless, Object useless2, JoinBuilder.JoinType joinType) {
assert _entity != null : "SearchBuilder cannot be modified once it has been setup";
assert _specifiedAttrs.size() == 1 : "You didn't select the attribute.";
@ -266,9 +212,9 @@ public class GenericSearchBuilder<T, K> extends SearchBase<T, K> {
assert builder._specifiedAttrs.size() == 1 : "You didn't select the attribute.";
assert builder != this : "You can't add yourself, can you? Really think about it!";
JoinBuilder<GenericSearchBuilder<?, ?>> t = new JoinBuilder<GenericSearchBuilder<?, ?>>(builder, _specifiedAttrs.get(0), builder._specifiedAttrs.get(0), joinType);
JoinBuilder<SearchBase<?, ?>> t = new JoinBuilder<SearchBase<?, ?>>(builder, _specifiedAttrs.get(0), builder._specifiedAttrs.get(0), joinType);
if (_joins == null) {
_joins = new HashMap<String, JoinBuilder<GenericSearchBuilder<?, ?>>>();
_joins = new HashMap<String, JoinBuilder<SearchBase<?, ?>>>();
}
_joins.put(name, t);
@ -277,29 +223,7 @@ public class GenericSearchBuilder<T, K> extends SearchBase<T, K> {
return this;
}
protected Condition constructCondition(String conditionName, String cond, Attribute attr, Op op) {
assert _entity != null : "SearchBuilder cannot be modified once it has been setup";
assert op == null || _specifiedAttrs.size() == 1 : "You didn't select the attribute.";
assert op != Op.SC : "Call join";
Condition condition = new Condition(conditionName, cond, attr, op);
_conditions.add(condition);
_specifiedAttrs.clear();
return condition;
}
/**
* creates the SearchCriteria so the actual values can be filled in.
*
* @return SearchCriteria
*/
public SearchCriteria<K> create() {
if (_entity != null) {
done();
}
return new SearchCriteria<K>(this);
}
public SearchCriteria<K> create(String name, Object... values) {
SearchCriteria<K> sc = create();
sc.setParameters(name, values);
@ -320,62 +244,11 @@ public class GenericSearchBuilder<T, K> extends SearchBase<T, K> {
return right();
}
public SelectType getSelectType() {
return _selectType;
}
/**
* Marks the SearchBuilder as completed in building the search conditions.
*/
public synchronized void done() {
if (_entity != null) {
Factory factory = (Factory)_entity;
factory.setCallback(0, null);
_entity = null;
}
if (_joins != null) {
for (JoinBuilder<GenericSearchBuilder<?, ?>> join : _joins.values()) {
join.getT().done();
}
}
if (_selects == null || _selects.size() == 0) {
_selectType = SelectType.Entity;
assert _entityBeanType.equals(_resultType) : "Expecting " + _entityBeanType + " because you didn't specify any selects but instead got " + _resultType;
return;
}
for (Select select : _selects) {
if (select.field == null) {
assert (_selects.size() == 1) : "You didn't specify any fields to put the result in but you're specifying more than one select so where should I put the selects?";
_selectType = SelectType.Single;
return;
}
if (select.func != null) {
_selectType = SelectType.Result;
return;
}
}
_selectType = SelectType.Fields;
super.finalize();
}
public class Preset {
GenericSearchBuilder<T, K> builder;
Condition condition;
protected Preset(GenericSearchBuilder<T, K> builder, Condition condition) {
this.builder = builder;
this.condition = condition;
}
public GenericSearchBuilder<T, K> values(Object... params) {
if (condition.op.getParams() > 0 && condition.op.params != params.length) {
throw new RuntimeException("The # of parameters set " + params.length + " does not match # of parameters required by " + condition.op);
}
condition.setPresets(params);
return builder;
}
}
}

View File

@ -23,12 +23,12 @@ import com.cloud.utils.Pair;
import com.cloud.utils.db.SearchCriteria.Func;
import com.cloud.utils.db.SearchCriteria.Op;
public class GroupBy<T, R> {
GenericSearchBuilder<T, R> _builder;
public class GroupBy<J extends SearchBase<T, R>, T, R> {
J _builder;
List<Pair<Func, Attribute>> _groupBys;
Having _having;
public GroupBy(GenericSearchBuilder<T, R> builder) {
public GroupBy(J builder) {
_builder = builder;
_groupBys = new ArrayList<Pair<Func, Attribute>>();
_having = null;
@ -38,19 +38,19 @@ public class GroupBy<T, R> {
_builder.getSpecifiedAttributes().clear();
}
public GroupBy<T, R> group(Object useless) {
public GroupBy<J, T, R> group(Object useless) {
_groupBys.add(new Pair<Func, Attribute>(null, _builder.getSpecifiedAttributes().get(0)));
_builder.getSpecifiedAttributes().clear();
return this;
return this;
}
public GroupBy<T, R> group(Func func, Object useless) {
public GroupBy<J, T, R> group(Func func, Object useless) {
_groupBys.add(new Pair<Func, Attribute>(func, _builder.getSpecifiedAttributes().get(0)));
_builder.getSpecifiedAttributes().clear();
return this;
}
public GenericSearchBuilder<T, R> having(Func func, Object obj, Op op, Object value) {
public J having(Func func, Object obj, Op op, Object value) {
assert(_having == null) : "You can only specify one having in a group by";
List<Attribute> attrs = _builder.getSpecifiedAttributes();
assert attrs.size() == 1 : "You didn't specified an attribute";

View File

@ -16,64 +16,65 @@
// under the License.
package com.cloud.utils.db;
public class JoinBuilder<T> {
public enum JoinType {
INNER ("INNER JOIN"),
LEFT ("LEFT JOIN"),
RIGHT ("RIGHT JOIN"),
RIGHTOUTER ("RIGHT OUTER JOIN"),
LEFTOUTER ("LEFT OUTER JOIN");
private final String _name;
JoinType(String name) {
_name = name;
}
public String getName() { return _name; }
}
private T t;
private JoinType type;
private Attribute firstAttribute;
private Attribute secondAttribute;
public JoinBuilder(T t, Attribute firstAttribute,
Attribute secondAttribute, JoinType type) {
this.t = t;
this.firstAttribute = firstAttribute;
this.secondAttribute = secondAttribute;
this.type = type;
}
public T getT() {
return t;
}
public void setT(T t) {
this.t = t;
}
public JoinType getType() {
return type;
}
public void setType(JoinType type) {
this.type = type;
}
public Attribute getFirstAttribute() {
return firstAttribute;
}
public void setFirstAttribute(Attribute firstAttribute) {
this.firstAttribute = firstAttribute;
}
public Attribute getSecondAttribute() {
return secondAttribute;
}
public void setSecondAttribute(Attribute secondAttribute) {
this.secondAttribute = secondAttribute;
}
public enum JoinType {
INNER("INNER JOIN"),
LEFT("LEFT JOIN"),
RIGHT("RIGHT JOIN"),
RIGHTOUTER("RIGHT OUTER JOIN"),
LEFTOUTER("LEFT OUTER JOIN");
private final String _name;
JoinType(String name) {
_name = name;
}
public String getName() {
return _name;
}
}
private final T t;
private JoinType type;
private Attribute firstAttribute;
private Attribute secondAttribute;
public JoinBuilder(T t, Attribute firstAttribute,
Attribute secondAttribute, JoinType type) {
this.t = t;
this.firstAttribute = firstAttribute;
this.secondAttribute = secondAttribute;
this.type = type;
}
public T getT() {
return t;
}
public JoinType getType() {
return type;
}
public void setType(JoinType type) {
this.type = type;
}
public Attribute getFirstAttribute() {
return firstAttribute;
}
public void setFirstAttribute(Attribute firstAttribute) {
this.firstAttribute = firstAttribute;
}
public Attribute getSecondAttribute() {
return secondAttribute;
}
public void setSecondAttribute(Attribute secondAttribute) {
this.secondAttribute = secondAttribute;
}
}

View File

@ -0,0 +1,279 @@
package com.cloud.utils.db;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.Column;
import javax.persistence.Transient;
import net.sf.cglib.proxy.Factory;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import com.cloud.utils.db.SearchCriteria.Func;
import com.cloud.utils.db.SearchCriteria.Op;
import com.cloud.utils.db.SearchCriteria.SelectType;
import com.cloud.utils.exception.CloudRuntimeException;
public abstract class SearchBase<T, K> {
final Map<String, Attribute> _attrs;
final Class<T> _entityBeanType;
final Class<K> _resultType;
final GenericDaoBase<? extends T, ? extends Serializable> _dao;
final ArrayList<Condition> _conditions;
final ArrayList<Attribute> _specifiedAttrs;
protected HashMap<String, JoinBuilder<SearchBase<?, ?>>> _joins;
protected ArrayList<Select> _selects;
protected GroupBy<? extends SearchBase<T, K>, T, K> _groupBy = null;
protected SelectType _selectType;
T _entity;
SearchBase(Class<T> entityType, Class<K> resultType) {
_dao = (GenericDaoBase<? extends T, ? extends Serializable>)GenericDaoBase.getDao(entityType);
if (_dao == null) {
throw new CloudRuntimeException("Unable to find DAO for " + entityType);
}
_entityBeanType = entityType;
_resultType = resultType;
_attrs = _dao.getAllAttributes();
_entity = _dao.createSearchEntity(new Interceptor());
_conditions = new ArrayList<Condition>();
_joins = null;
_specifiedAttrs = new ArrayList<Attribute>();
}
public SelectType getSelectType() {
return _selectType;
}
protected void set(String name) {
Attribute attr = _attrs.get(name);
assert (attr != null) : "Searching for a field that's not there: " + name;
_specifiedAttrs.add(attr);
}
public T entity() {
return _entity;
}
protected Attribute getSpecifiedAttribute() {
if (_entity == null || _specifiedAttrs == null || _specifiedAttrs.size() != 1) {
throw new RuntimeException("Now now, better specify an attribute or else we can't help you");
}
return _specifiedAttrs.get(0);
}
protected List<Attribute> getSpecifiedAttributes() {
return _specifiedAttrs;
}
protected Condition constructCondition(String conditionName, String cond, Attribute attr, Op op) {
assert _entity != null : "SearchBuilder cannot be modified once it has been setup";
assert op == null || _specifiedAttrs.size() == 1 : "You didn't select the attribute.";
assert op != Op.SC : "Call join";
Condition condition = new Condition(conditionName, cond, attr, op);
_conditions.add(condition);
_specifiedAttrs.clear();
return condition;
}
/**
* creates the SearchCriteria so the actual values can be filled in.
*
* @return SearchCriteria
*/
public SearchCriteria<K> create() {
if (_entity != null) {
finalize();
}
return new SearchCriteria<K>(this);
}
/**
* Marks the SearchBuilder as completed in building the search conditions.
*/
@Override
public synchronized void finalize() {
if (_entity != null) {
Factory factory = (Factory)_entity;
factory.setCallback(0, null);
_entity = null;
}
if (_joins != null) {
for (JoinBuilder<SearchBase<?, ?>> join : _joins.values()) {
join.getT().finalize();
}
}
if (_selects == null || _selects.size() == 0) {
_selectType = SelectType.Entity;
assert _entityBeanType.equals(_resultType) : "Expecting " + _entityBeanType + " because you didn't specify any selects but instead got " + _resultType;
return;
}
for (Select select : _selects) {
if (select.field == null) {
assert (_selects.size() == 1) : "You didn't specify any fields to put the result in but you're specifying more than one select so where should I put the selects?";
_selectType = SelectType.Single;
return;
}
if (select.func != null) {
_selectType = SelectType.Result;
return;
}
}
_selectType = SelectType.Fields;
}
protected static class Condition {
protected final String name;
protected final String cond;
protected final Op op;
protected final Attribute attr;
protected Object[] presets;
protected Condition(String name) {
this(name, null, null, null);
}
public Condition(String name, String cond, Attribute attr, Op op) {
this.name = name;
this.attr = attr;
this.cond = cond;
this.op = op;
this.presets = null;
}
public boolean isPreset() {
return presets != null;
}
public void setPresets(Object... presets) {
this.presets = presets;
}
public Object[] getPresets() {
return presets;
}
public void toSql(StringBuilder sql, Object[] params, int count) {
if (count > 0) {
sql.append(cond);
}
if (op == null) {
return;
}
if (op == Op.SC) {
sql.append(" (").append(((SearchCriteria<?>)params[0]).getWhereClause()).append(") ");
return;
}
if (attr == null) {
return;
}
sql.append(attr.table).append(".").append(attr.columnName).append(op.toString());
if (op == Op.IN && params.length == 1) {
sql.delete(sql.length() - op.toString().length(), sql.length());
sql.append("=?");
} else if (op == Op.NIN && params.length == 1) {
sql.delete(sql.length() - op.toString().length(), sql.length());
sql.append("!=?");
} else if (op.getParams() == -1) {
for (int i = 0; i < params.length; i++) {
sql.insert(sql.length() - 2, "?,");
}
sql.delete(sql.length() - 3, sql.length() - 2); // remove the last ,
} else if (op == Op.EQ && (params == null || params.length == 0 || params[0] == null)) {
sql.delete(sql.length() - 4, sql.length());
sql.append(" IS NULL ");
} else if (op == Op.NEQ && (params == null || params.length == 0 || params[0] == null)) {
sql.delete(sql.length() - 5, sql.length());
sql.append(" IS NOT NULL ");
} else {
if ((op.getParams() != 0 || params != null) && (params.length != op.getParams())) {
throw new RuntimeException("Problem with condition: " + name);
}
}
}
@Override
public int hashCode() {
return name.hashCode();
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Condition)) {
return false;
}
Condition condition = (Condition)obj;
return name.equals(condition.name);
}
}
protected static class Select {
public Func func;
public Attribute attr;
public Object[] params;
public Field field;
protected Select() {
}
public Select(Func func, Attribute attr, Field field, Object[] params) {
this.func = func;
this.attr = attr;
this.params = params;
this.field = field;
}
}
protected class Interceptor implements MethodInterceptor {
@Override
public Object intercept(Object object, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
String name = method.getName();
if (method.getAnnotation(Transient.class) == null) {
if (name.startsWith("get")) {
String fieldName = Character.toLowerCase(name.charAt(3)) + name.substring(4);
set(fieldName);
return null;
} else if (name.startsWith("is")) {
String fieldName = Character.toLowerCase(name.charAt(2)) + name.substring(3);
set(fieldName);
return null;
} else {
Column ann = method.getAnnotation(Column.class);
if (ann != null) {
String colName = ann.name();
for (Map.Entry<String, Attribute> attr : _attrs.entrySet()) {
if (colName.equals(attr.getValue().columnName)) {
set(attr.getKey());
return null;
}
}
}
throw new RuntimeException("Perhaps you need to make the method start with get or is: " + method);
}
}
return methodProxy.invokeSuper(object, args);
}
}
}

View File

@ -56,7 +56,6 @@ package com.cloud.utils.db;
*/
public class SearchBuilder<T> extends GenericSearchBuilder<T, T> {
@SuppressWarnings("unchecked")
public SearchBuilder(Class<T> entityType) {
super(entityType, entityType);
}

View File

@ -113,12 +113,13 @@ public class SearchCriteria<K> {
private int _counter;
private HashMap<String, JoinBuilder<SearchCriteria<?>>> _joins;
private final ArrayList<Select> _selects;
private final GroupBy<?, K> _groupBy;
private final GroupBy<? extends SearchBase<?, K>, ?, K> _groupBy;
private final List<Object> _groupByValues;
private final Class<K> _resultType;
private final SelectType _selectType;
protected SearchCriteria(final Map<String, Attribute> attrs, ArrayList<GenericSearchBuilder.Condition> conditions, ArrayList<Select> selects, SelectType selectType, Class<K> resultType, HashMap<String, Object[]> params) {
protected SearchCriteria(Map<String, Attribute> attrs, ArrayList<Condition> conditions, ArrayList<Select> selects, SelectType selectType, Class<K> resultType,
HashMap<String, Object[]> params) {
this._attrs = attrs;
this._conditions = conditions;
this._selects = selects;
@ -132,7 +133,7 @@ public class SearchCriteria<K> {
this._groupByValues = null;
}
protected SearchCriteria(GenericSearchBuilder<?, K> sb) {
protected SearchCriteria(SearchBase<?, K> sb) {
this._attrs = sb._attrs;
this._conditions = sb._conditions;
this._additionals = new ArrayList<Condition>();
@ -140,8 +141,8 @@ public class SearchCriteria<K> {
this._joins = null;
if (sb._joins != null) {
_joins = new HashMap<String, JoinBuilder<SearchCriteria<?>>>(sb._joins.size());
for (Map.Entry<String, JoinBuilder<GenericSearchBuilder<?, ?>>> entry : sb._joins.entrySet()) {
JoinBuilder<GenericSearchBuilder<?, ?>> value = entry.getValue();
for (Map.Entry<String, JoinBuilder<SearchBase<?, ?>>> entry : sb._joins.entrySet()) {
JoinBuilder<SearchBase<?, ?>> value = entry.getValue();
_joins.put(entry.getKey(), new JoinBuilder<SearchCriteria<?>>(value.getT().create(),value.getFirstAttribute(), value.getSecondAttribute(), value.getType()));
}
}
@ -242,8 +243,8 @@ public class SearchCriteria<K> {
return _joins.get(joinName).getT();
}
public Pair<GroupBy<?, ?>, List<Object>> getGroupBy() {
return _groupBy == null ? null : new Pair<GroupBy<?, ?>, List<Object>>(_groupBy, _groupByValues);
public Pair<GroupBy<?, ?, ?>, List<Object>> getGroupBy() {
return _groupBy == null ? null : new Pair<GroupBy<?, ?, ?>, List<Object>>(_groupBy, _groupByValues);
}
public void setGroupByValues(Object... values) {

View File

@ -23,11 +23,8 @@ import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import net.sf.cglib.proxy.Factory;
import com.cloud.utils.db.SearchCriteria.Func;
import com.cloud.utils.db.SearchCriteria.Op;
import com.cloud.utils.db.SearchCriteria.SelectType;
public class SearchCriteria2<T, K> extends SearchBase<T, K> {
final HashMap<String, Object[]> _params = new HashMap<String, Object[]>();
@ -69,16 +66,6 @@ public class SearchCriteria2<T, K> extends SearchBase<T, K> {
_specifiedAttrs.clear();
}
private void constructCondition(String conditionName, String cond, Attribute attr, Op op) {
assert _entity != null : "SearchBuilder cannot be modified once it has been setup";
assert op == null || _specifiedAttrs.size() == 1 : "You didn't select the attribute.";
assert op != Op.SC : "Call join";
GenericSearchBuilder.Condition condition = new GenericSearchBuilder.Condition(conditionName, cond, attr, op);
_conditions.add(condition);
_specifiedAttrs.clear();
}
private void setParameters(String conditionName, Object... params) {
assert _conditions.contains(new Condition(conditionName)) : "Couldn't find " + conditionName;
_params.put(conditionName, params);
@ -91,8 +78,8 @@ public class SearchCriteria2<T, K> extends SearchBase<T, K> {
}
public List<K> list() {
done();
SearchCriteria sc1 = createSearchCriteria();
finalize();
SearchCriteria sc1 = create();
if (isSelectAll()) {
return (List<K>)_dao.search(sc1, null);
} else {
@ -108,43 +95,56 @@ public class SearchCriteria2<T, K> extends SearchBase<T, K> {
return _entity;
}
private SearchCriteria<K> createSearchCriteria() {
return new SearchCriteria<K>(_attrs, _conditions, _selects, _selectType, _resultType, _params);
}
private void done() {
if (_entity != null) {
Factory factory = (Factory)_entity;
factory.setCallback(0, null);
_entity = null;
}
if (_selects == null || _selects.size() == 0) {
_selectType = SelectType.Entity;
assert _entityBeanType.equals(_resultType) : "Expecting " + _entityBeanType + " because you didn't specify any selects but instead got " + _resultType;
return;
}
for (Select select : _selects) {
if (select.field == null) {
assert (_selects.size() == 1) : "You didn't specify any fields to put the result in but you're specifying more than one select so where should I put the selects?";
_selectType = SelectType.Single;
return;
}
if (select.func != null) {
_selectType = SelectType.Result;
return;
}
}
_selectType = SelectType.Fields;
}
public <K> K find() {
public K find() {
assert isSelectAll() : "find doesn't support select search";
done();
SearchCriteria sc1 = createSearchCriteria();
finalize();
SearchCriteria sc1 = create();
return (K)_dao.findOneBy(sc1);
}
public Preset and(Object useless, Op op) {
Condition condition = constructCondition(UUID.randomUUID().toString(), " AND ", _specifiedAttrs.get(0), op);
return new Preset(this, condition);
}
public Preset where(Object useless, Op op) {
return and(useless, op);
}
public Preset left(Object useless, Op op) {
Condition condition = constructCondition(UUID.randomUUID().toString(), " ( ", _specifiedAttrs.get(0), op);
return new Preset(this, condition);
}
public Preset op(Object useless, Op op) {
return left(useless, op);
}
public Preset openParen(Object useless, Op op) {
return left(useless, op);
}
public Preset or(Object useless, Op op) {
Condition condition = constructCondition(UUID.randomUUID().toString(), " OR ", _specifiedAttrs.get(0), op);
return new Preset(this, condition);
}
public class Preset {
SearchCriteria2<T, K> builder;
Condition condition;
protected Preset(SearchCriteria2<T, K> builder, Condition condition) {
this.builder = builder;
this.condition = condition;
}
public SearchCriteria2<T, K> values(Object... params) {
if (condition.op.getParams() > 0 && condition.op.params != params.length) {
throw new RuntimeException("The # of parameters set " + params.length + " does not match # of parameters required by " + condition.op);
}
condition.setPresets(params);
return builder;
}
}
}

View File

@ -49,7 +49,7 @@ public class SyncQueueItemDaoImpl extends GenericDaoBase<SyncQueueItemVO, Long>
queueIdSearch = createSearchBuilder(Long.class);
queueIdSearch.and("contentId", queueIdSearch.entity().getContentId(), Op.EQ);
queueIdSearch.and("contentType", queueIdSearch.entity().getContentType(), Op.EQ);
queueIdSearch.selectField(queueIdSearch.entity().getId());
queueIdSearch.selectFields(queueIdSearch.entity().getId());
queueIdSearch.done();
}

View File

@ -898,13 +898,13 @@ public class AsyncJobManagerImpl extends ManagerBase implements AsyncJobManager,
JoinJobSearch = _joinMapDao.createSearchBuilder(Long.class);
JoinJobSearch.and(JoinJobSearch.entity().getJoinJobId(), Op.EQ, "joinJobId");
JoinJobSearch.selectField(JoinJobSearch.entity().getJobId());
JoinJobSearch.selectFields(JoinJobSearch.entity().getJobId());
JoinJobSearch.done();
JoinJobTimeSearch = _joinMapDao.createSearchBuilder(Long.class);
JoinJobTimeSearch.and(JoinJobTimeSearch.entity().getNextWakeupTime(), Op.LT, "beginTime");
JoinJobTimeSearch.and(JoinJobTimeSearch.entity().getExpiration(), Op.GT, "endTime");
JoinJobTimeSearch.selectField(JoinJobTimeSearch.entity().getJobId()).done();
JoinJobTimeSearch.selectFields(JoinJobTimeSearch.entity().getJobId()).done();
JobIdsSearch = _jobDao.createSearchBuilder();
JobIdsSearch.and(JobIdsSearch.entity().getId(), Op.IN, "ids").done();
@ -913,13 +913,13 @@ public class AsyncJobManagerImpl extends ManagerBase implements AsyncJobManager,
QueueJobIdsSearch.and(QueueJobIdsSearch.entity().getContentId(), Op.IN, "contentIds").done();
JoinJobIdsSearch = _joinMapDao.createSearchBuilder(Long.class);
JoinJobIdsSearch.selectField(JoinJobIdsSearch.entity().getJobId());
JoinJobIdsSearch.selectFields(JoinJobIdsSearch.entity().getJobId());
JoinJobIdsSearch.and(JoinJobIdsSearch.entity().getJoinJobId(), Op.EQ, "joinJobId");
JoinJobIdsSearch.and(JoinJobIdsSearch.entity().getJobId(), Op.NIN, "jobIds");
JoinJobIdsSearch.done();
ContentIdsSearch = _queueItemDao.createSearchBuilder(Long.class);
ContentIdsSearch.selectField(ContentIdsSearch.entity().getContentId()).done();
ContentIdsSearch.selectFields(ContentIdsSearch.entity().getContentId()).done();
AsyncJobExecutionContext.init(this, _joinMapDao);
OutcomeImpl.init(this);

View File

@ -459,7 +459,7 @@ public class StorageManagerImpl extends ManagerBase implements StorageManager, C
_serverId = _msServer.getId();
UpHostsInPoolSearch = _storagePoolHostDao.createSearchBuilder(Long.class);
UpHostsInPoolSearch.selectField(UpHostsInPoolSearch.entity().getHostId());
UpHostsInPoolSearch.selectFields(UpHostsInPoolSearch.entity().getHostId());
SearchBuilder<HostVO> hostSearch = _hostDao.createSearchBuilder();
hostSearch.and("status", hostSearch.entity().getStatus(), Op.EQ);
hostSearch.and("resourceState", hostSearch.entity().getResourceState(), Op.EQ);