package divconq.db.query;

import divconq.struct.RecordStruct;
import divconq.struct.Struct;
import divconq.struct.scalar.StringStruct;

/**
 * A database foreign key field to use with a subquery.
 * 
 * @author Andy
 *
 */
public class SelectSubquery extends SelectFields implements ISelectField {
	protected RecordStruct subquery = new RecordStruct();
	
	/**
	 * @param field foreign key field name
	 * @param fields/subqueries to use as initial values for select
	 */
	public SelectSubquery(String field, ISelectField... items) {
		this.subquery.setField("Field", field);
		
		if (items != null)
			for (ISelectField itm : items)
				this.fields.addItem(itm.getParams());
		
		this.subquery.setField("Select", this.fields);
	}	
	
	/**
	 * @param field foreign key field name
	 * @param name display name
	 * @param fields/subqueries to use as initial values for select
	 */
	public SelectSubquery(String field, String name, ISelectField... items) {
		this.subquery.setField("Field", field);
		this.subquery.setField("Name", name);
		
		if (items != null)
			for (ISelectField itm : items)
				this.fields.addItem(itm.getParams());
		
		this.subquery.setField("Select", this.fields);
	}	
	
	@Override
	public Struct getParams() {
		return this.subquery.isEmpty() ? new StringStruct("*") : this.subquery;
	}
}
