All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.avaje.ebeaninternal.server.bean.BFQueryStatisticFinder Maven / Gradle / Ivy

There is a newer version: 2.8.1
Show newest version
package com.avaje.ebeaninternal.server.bean;

import java.util.Iterator;
import java.util.List;

import javax.persistence.PersistenceException;

import com.avaje.ebean.Query;
import com.avaje.ebean.bean.BeanCollection;
import com.avaje.ebean.common.BeanList;
import com.avaje.ebean.event.BeanFinder;
import com.avaje.ebean.event.BeanQueryRequest;
import com.avaje.ebean.meta.MetaQueryStatistic;
import com.avaje.ebeaninternal.api.SpiEbeanServer;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.query.CQueryPlan;

/**
 * BeanFinder for MetaQueryStatistic.
 */
public class BFQueryStatisticFinder implements BeanFinder {

	
	public MetaQueryStatistic find(BeanQueryRequest request) {
		throw new RuntimeException("Not Supported yet");
	}

	/**
	 * Only returns Lists at this stage.
	 */
	public BeanCollection findMany(BeanQueryRequest request) {

		Query.Type queryType = request.getQuery().getType();
		if (!queryType.equals(Query.Type.LIST)){
			throw new PersistenceException("Only findList() supported at this stage.");
		}
		
		BeanList list = new BeanList();
		
		SpiEbeanServer server = (SpiEbeanServer) request.getEbeanServer();
		build(list, server);
		
		String orderBy = request.getQuery().order().toStringFormat();
		if (orderBy == null){
			orderBy = "beanType, origQueryPlanHash, autofetchTuned";
		}
		server.sort(list, orderBy);

		return list;
	}

	private void build(List list, SpiEbeanServer server) {

		for (BeanDescriptor desc : server.getBeanDescriptors()) {
			desc.clearQueryStatistics();			
			build(list, desc);
		}		
	}
	
	private void build(List list, BeanDescriptor desc) {

		Iterator it = desc.queryPlans();
		while (it.hasNext()) {
			CQueryPlan queryPlan = (CQueryPlan) it.next();
			list.add(queryPlan.createMetaQueryStatistic(desc.getFullName()));
		}
	}
	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy