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

io.baltoro.client.PropertyQuery Maven / Gradle / Ivy

There is a newer version: 4.0.12
Show newest version
package io.baltoro.client;

import java.util.List;

import io.baltoro.obj.Base;

public class PropertyQuery
{
	
	private Class c;
	private LocalDB db;
	private StringBuilder q = new StringBuilder();
	private int count;
	private String sortBy;
	
	PropertyQuery(Class c, LocalDB db)
	{
		this.c = c;
		this.db = db;
		q.append("select uuid from base b where uuid in ");
		sortBy = " order by created_on desc ";
	}
	
	
	public PropertyQuery addEquals(String name, String value)
	{
		if(count == 0)
		{
			q.append("\n(select distinct base_uuid from metadata where name='"+name+"' and value='"+value+"' and version_uuid=b.latest_version_uuid ");
		}
		else
		{
			q.append("\n and base_uuid in (select distinct base_uuid from metadata where name='"+name+"' and value='"+value+"' and version_uuid=b.latest_version_uuid ");
		}
		count++;
		return this;
	}
	
	public PropertyQuery addIn(String name, String value)
	{
		if(count == 0)
		{
			q.append("\n(select distinct base_uuid from metadata where name='"+name+"' and value in ("+value+") and version_uuid=b.latest_version_uuid ");
		}
		else
		{
			q.append("\n and base_uuid in (select distinct base_uuid from metadata where name='"+name+"' and value in ("+value+") and version_uuid=b.latest_version_uuid ");
		}
		count++;
		return this;
	}
	
	
	public PropertyQuery sortBy(String sortBy)
	{
		this.sortBy = sortBy;
		return this;
	}
	
	
	public List execute()
	{
	
		for (int i = 0; i < count; i++)
		{
			q.append(")");
		}
		
		//q.append(")\n");
		
		/*
		if(count>1)
		{
			q.append(")\n");
		}
		*/
		
		String objType = db.getType(c);
		
		q.append("\nand type='"+objType+"'");
		
		if(sortBy != null)
		{
			q.append("\n"+sortBy);
		}
		
		String query = this.q.toString();
		
		System.out.println(query);
		
		RecordList recList = db.query(String.class, query).execute();
	
		if(recList.isEmpty())
		{
			return null;
		}
		
		String[] uuids = recList.toArray(new String[recList.size()]);
		
		List list = db.get(c, uuids);
		
		return list;
		
	}
	
	
	Class getClassT()
	{
		return c;
	}
	
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy