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

gedi.solutions.geode.io.Querier Maven / Gradle / Ivy

Go to download

GemFire Enterprise Data Integration - common development extensions powered by Apache Geode

The newest version!
package gedi.solutions.geode.io;

import java.util.ArrayList;
import java.util.Collection;

import org.apache.geode.cache.CacheFactory;
import org.apache.geode.cache.execute.RegionFunctionContext;
import org.apache.geode.cache.query.FunctionDomainException;
import org.apache.geode.cache.query.NameResolutionException;
import org.apache.geode.cache.query.Query;
import org.apache.geode.cache.query.QueryInvocationTargetException;
import org.apache.geode.cache.query.QueryService;
import org.apache.geode.cache.query.SelectResults;
import org.apache.geode.cache.query.TypeMismatchException;

import gedi.solutions.geode.functions.JvmRegionFunctionContext;

public class Querier
{
	/**
	 * 
	 * @param query the OQL query
	 * @param  the return type
	 * @param params the bind variables
	 * @return the collection of the return types
	 */
	public static  Collection query(String query,Object...params)
	{
		return query(query, (RegionFunctionContext)null,params);
	}// ------------------------------------------------
	/**
	 * 
	 * @param  the collection of return types
	 * @param query the OQL
	 * @param rfc the region function context
	 * @param params the bind variables
	 * @return Collection of the return type
	 */
	public static  Collection query(String query, RegionFunctionContext rfc, Object... params)
	{
		try
		{

			QueryService queryService = CacheFactory.getAnyInstance().getQueryService();

			// Create the Query Object.
			Query queryObj = queryService.newQuery(query);

			return query(queryObj, rfc,params);
		}
		catch (Exception e)
		{
			throw new RuntimeException("Cannot execute query:" + query + " ERROR:" + e.getMessage(), e);
		}

	}// -------------------------------------------------------------------
	/**
	 * Select results for OQL
	 * @param  the collection return type
	 * @param queryObj the query object
	 * @param rfc the region function context
	 * @param params the bind variables
	 * @return collection of results
	 * @throws FunctionDomainException function error occurs
	 * @throws TypeMismatchException type error occurs
	 * @throws NameResolutionException name resolution exception occurs
	 * @throws QueryInvocationTargetException when query invocation target exception occurs
	 */
	@SuppressWarnings("unchecked")
	public static  Collection query(Query queryObj, RegionFunctionContext rfc, Object... params)
	throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException
	{
		SelectResults selectResults;

		// Execute Query locally. Returns results set.

		if (rfc == null || JvmRegionFunctionContext.class.isAssignableFrom(rfc.getClass()))
		{
			if(params == null || params.length == 0)
			{
				selectResults = (SelectResults) queryObj.execute();				
			}
			else
			{
				selectResults = (SelectResults) queryObj.execute(params);
			}
			if (selectResults == null || selectResults.isEmpty())
				return null;

			ArrayList results = new ArrayList(selectResults.size());
			results.addAll(selectResults.asList());

			return results;
		}
		else
		{
			if(params == null || params.length == 0)
			{
				selectResults = (SelectResults) queryObj.execute(rfc);	
			}
			else
			{
				selectResults = (SelectResults) queryObj.execute(rfc,params);
			}
			

			if (selectResults == null || selectResults.isEmpty())
				return null;

			return selectResults;
		}

	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy