package eu.dnetlib.dlms.jdbc.ast;

import java.sql.SQLException;
import java.util.List;

import eu.dnetlib.dlms.jdbc.ExecutorContext;
import eu.dnetlib.dlms.jdbc.server.IStatementExecutor;

/**
 * Statement for a Doroty query.
 * <p>
 * A select statement is composed by the select keyword followed optionally by one or more selectable attributes groupd
 * by paranthesis: e.g. <code>select A/B</code>,<code>select (id, url) A[./B/C]</code>. Selectable attributes are
 * system-defined, they are: id, url and contentUrl (only for atoms). The the query path follows, that is the Doroty
 * xPath that identifies the objects to select. Finally, the query may optionally end with the 'limit' keyword followed
 * by the max number of objects to be returned. By default no limit is applied.
 * </p>
 * 
 * @author alessia
 * 
 */
public class Select extends Statement {
	/** Fields to select. */
	private List<String> fields;
	/** XPath that identifies the interesting object to select. */
	private XPath xPath;
	/** Maximum number of results to search . */
	private Integer limit;

	/**
	 * {@inheritDoc}
	 * 
	 * @see eu.dnetlib.dlms.jdbc.ast.Statement#execute(eu.dnetlib.dlms.jdbc.server.IStatementExecutor)
	 */
	@Override
	public Object execute(final IStatementExecutor executor, final ExecutorContext context) throws SQLException {
		return executor.execute(this, context);
	}

	/**
	 * Constructor.
	 * 
	 * @param fields
	 *            List of fields to select
	 * @param xp
	 *            XPath
	 * @param limit
	 *            number of wanted results (at max)
	 */
	public Select(final List<String> fields, final XPath xp, final Integer limit) {
		this.fields = fields;
		this.xPath = xp;
		this.limit = limit;
	}

	public List<String> getFields() {
		return this.fields;
	}

	public void setFields(final List<String> fields) {
		this.fields = fields;
	}

	public Integer getLimit() {
		return this.limit;
	}

	public void setLimit(final Integer limit) {
		this.limit = limit;
	}

	public void setXPath(final XPath queryPath) {
		this.xPath = queryPath;
	}

	public XPath getXPath() {
		return this.xPath;
	}

	@Override
	public String toString() {
		return "Select [fields=" + this.fields + ", xPath=" + this.xPath + ", limit=" + this.limit + "]";
	}

}
