Package org.apache.jena.query
Class Query
- java.lang.Object
-
- org.apache.jena.sparql.core.Prologue
-
- org.apache.jena.query.Query
-
- All Implemented Interfaces:
java.lang.Cloneable,org.apache.jena.atlas.io.Printable
public class Query extends Prologue implements java.lang.Cloneable, org.apache.jena.atlas.io.Printable
The data structure for a query as presented externally. There are two ways of creating a query - use the parser to turn a string description of the query into the executable form, and the programmatic way (the parser is calling the programmatic operations driven by the query string). The declarative approach of passing in a string is preferred. Once a query is built, it can be passed to the QueryFactory to produce a query execution engine.- See Also:
QueryExecutionFactory,ResultSet
-
-
Field Summary
Fields Modifier and Type Field Description static longNOLIMITstatic intORDER_ASCENDINGstatic intORDER_DEFAULTstatic intORDER_DESCENDINGstatic intORDER_UNKNOWstatic intQueryTypeAskstatic intQueryTypeConstructstatic intQueryTypeDescribestatic intQueryTypeJsonstatic intQueryTypeSelectstatic intQueryTypeUnknown
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidaddDescribeNode(Node node)voidaddGraphURI(java.lang.String s)Location of the source for the data.voidaddGroupBy(java.lang.String varName)voidaddGroupBy(Node v)voidaddGroupBy(Var v, Expr expr)voidaddGroupBy(Expr expr)voidaddHavingCondition(Expr expr)voidaddJsonMapping(java.lang.String key, Node value)voidaddNamedGraphURI(java.lang.String uri)Location of the source for the data.voidaddOrderBy(java.lang.String varName, int direction)voidaddOrderBy(Node var, int direction)voidaddOrderBy(SortCondition condition)voidaddOrderBy(Expr expr, int direction)voidaddProjectVars(java.util.Collection<?> vars)Add a collection of projection variables to a SELECT queryvoidaddResultVar(java.lang.String varName)Add a projection variable to a SELECT queryvoidaddResultVar(java.lang.String varName, Expr expr)Add a named expression to a SELECT queryvoidaddResultVar(Node v)voidaddResultVar(Node v, Expr expr)voidaddResultVar(Expr expr)Add an to a SELECT query (a name will be created for it)ExprallocAggregate(Aggregator agg)java.lang.Objectclone()QuerycloneQuery()Makes a copy of this query using the syntax transform machinery.booleanequals(java.lang.Object other)Are two queries equals - tests shape and details.java.util.List<ExprAggregator>getAggregators()TemplategetConstructTemplate()Get the template pattern for a construct queryDatasetDescriptiongetDatasetDescription()Return a dataset description (FROM/FROM NAMED clauses) for the query.java.util.List<java.lang.String>getGraphURIs()Return the list of URIs (strings) for the unnamed graphVarExprListgetGroupBy()java.util.List<Expr>getHavingExprs()java.util.Map<java.lang.String,Node>getJsonMapping()longgetLimit()java.util.List<java.lang.String>getNamedGraphURIs()Return the list of URIs (strings) for the named graphs (FROM NAMED clause)longgetOffset()java.util.List<SortCondition>getOrderBy()VarExprListgetProject()java.util.List<Var>getProjectVars()Return a list of the variables requested (SELECT)ProloguegetPrologue()ElementgetQueryPattern()intgetQueryType()Deprecated.UsequeryType()which returns aQueryTypejava.util.List<Node>getResultURIs()Get the result list (things wanted - not the results themselves) of a DESCRIBE query.java.util.List<java.lang.String>getResultVars()Return a list of the variables requested (SELECT)SyntaxgetSyntax()java.util.List<Binding>getValuesData()Data from a VALUES trailing block.java.util.List<Var>getValuesVariables()Variables from a VALUES trailing blockbooleanhasAggregators()booleanhasDatasetDescription()Return true if the query has either some graph URIs or some named graph URIs in its description.booleanhasGroupBy()booleanhasHaving()inthashCode()booleanhasLimit()booleanhasOffset()booleanhasOrderBy()booleanhasValues()Does the query have a VALUES trailing block?booleanisAskType()booleanisConstructQuad()booleanisConstructType()booleanisDescribeType()booleanisDistinct()booleanisJsonType()booleanisOrdered()booleanisQueryResultStar()Answer whether the query had SELECT/DESCRIBE/CONSTRUCT *booleanisReduced()booleanisSelectType()booleanisStrict()booleanisUnknownType()voidoutput(org.apache.jena.atlas.io.IndentedWriter out)QueryTypequeryType()Return theQueryTypevoidresetResultVars()If modifying a query, it may be necessary to reset the calculate of the result variables of the query forSELECT *andDESCRIBE *andCONSTRUCT.java.lang.Stringserialize()Convert the query to a stringvoidserialize(java.io.OutputStream out)Output the queryvoidserialize(java.io.OutputStream out, Syntax syntax)Output the queryvoidserialize(org.apache.jena.atlas.io.IndentedWriter writer)Format the queryvoidserialize(org.apache.jena.atlas.io.IndentedWriter writer, Syntax outSyntax)Format the queryjava.lang.Stringserialize(Syntax syntax)Convert the query to a string in the given syntaxvoidsetConstructTemplate(Template templ)Set triple patterns for a construct queryvoidsetDistinct(boolean b)voidsetLimit(long limit)voidsetOffset(long offset)voidsetQueryAskType()voidsetQueryConstructType()voidsetQueryDescribeType()voidsetQueryJsonType()voidsetQueryPattern(Element elt)voidsetQueryResultStar(boolean isQueryStar)Set whether the query had SELECT/DESCRIBE *voidsetQuerySelectType()voidsetReduced(boolean b)voidsetResultVars()Set the results variables if necessary, when the query has "*" (SELECT *orDESCRIBE *) and for a construct query.voidsetStrict(boolean isStrict)voidsetSyntax(Syntax syntax)voidsetValuesDataBlock(java.util.List<Var> variables, java.util.List<Binding> values)java.lang.StringtoString()java.lang.StringtoString(Syntax syntax)booleanusesGraphURI(java.lang.String uri)Test whether the query mentions a URI in forming the default graph (FROM clause)booleanusesNamedGraphURI(java.lang.String uri)Test whether the query mentions a URI for a named graph.voidvisit(QueryVisitor visitor)-
Methods inherited from class org.apache.jena.sparql.core.Prologue
copy, expandPrefixedName, explicitlySetBaseURI, getBase, getBaseURI, getPrefix, getPrefixMapping, getResolver, hash, samePrologue, setBase, setBaseURI, setPrefix, setPrefixMapping, shortForm
-
-
-
-
Field Detail
-
QueryTypeUnknown
public static final int QueryTypeUnknown
- See Also:
- Constant Field Values
-
QueryTypeSelect
public static final int QueryTypeSelect
- See Also:
- Constant Field Values
-
QueryTypeConstruct
public static final int QueryTypeConstruct
- See Also:
- Constant Field Values
-
QueryTypeDescribe
public static final int QueryTypeDescribe
- See Also:
- Constant Field Values
-
QueryTypeAsk
public static final int QueryTypeAsk
- See Also:
- Constant Field Values
-
QueryTypeJson
public static final int QueryTypeJson
- See Also:
- Constant Field Values
-
NOLIMIT
public static final long NOLIMIT
- See Also:
- Constant Field Values
-
ORDER_ASCENDING
public static final int ORDER_ASCENDING
- See Also:
- Constant Field Values
-
ORDER_DESCENDING
public static final int ORDER_DESCENDING
- See Also:
- Constant Field Values
-
ORDER_DEFAULT
public static final int ORDER_DEFAULT
- See Also:
- Constant Field Values
-
ORDER_UNKNOW
public static final int ORDER_UNKNOW
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
Query
public Query()
Creates a new empty query
-
Query
public Query(Prologue prologue)
Creates a new empty query with the given prologue
-
-
Method Detail
-
setQuerySelectType
public void setQuerySelectType()
-
setQueryConstructType
public void setQueryConstructType()
-
setQueryDescribeType
public void setQueryDescribeType()
-
setQueryAskType
public void setQueryAskType()
-
setQueryJsonType
public void setQueryJsonType()
-
getQueryType
@Deprecated public int getQueryType()
Deprecated.UsequeryType()which returns aQueryType
-
isSelectType
public boolean isSelectType()
-
isConstructType
public boolean isConstructType()
-
isDescribeType
public boolean isDescribeType()
-
isAskType
public boolean isAskType()
-
isJsonType
public boolean isJsonType()
-
isUnknownType
public boolean isUnknownType()
-
isConstructQuad
public boolean isConstructQuad()
-
getPrologue
public Prologue getPrologue()
-
setStrict
public void setStrict(boolean isStrict)
-
isStrict
public boolean isStrict()
-
setDistinct
public void setDistinct(boolean b)
-
isDistinct
public boolean isDistinct()
-
setReduced
public void setReduced(boolean b)
-
isReduced
public boolean isReduced()
-
getSyntax
public Syntax getSyntax()
- Returns:
- Returns the syntax.
-
setSyntax
public void setSyntax(Syntax syntax)
- Parameters:
syntax- The syntax to set.
-
getLimit
public long getLimit()
-
setLimit
public void setLimit(long limit)
-
hasLimit
public boolean hasLimit()
-
getOffset
public long getOffset()
-
setOffset
public void setOffset(long offset)
-
hasOffset
public boolean hasOffset()
-
hasOrderBy
public boolean hasOrderBy()
-
isOrdered
public boolean isOrdered()
-
addOrderBy
public void addOrderBy(SortCondition condition)
-
addOrderBy
public void addOrderBy(Expr expr, int direction)
-
addOrderBy
public void addOrderBy(Node var, int direction)
-
addOrderBy
public void addOrderBy(java.lang.String varName, int direction)
-
getOrderBy
public java.util.List<SortCondition> getOrderBy()
-
isQueryResultStar
public boolean isQueryResultStar()
Answer whether the query had SELECT/DESCRIBE/CONSTRUCT *- Returns:
- boolean as to whether a * result form was seen
-
setQueryResultStar
public void setQueryResultStar(boolean isQueryStar)
Set whether the query had SELECT/DESCRIBE *- Parameters:
isQueryStar-
-
setQueryPattern
public void setQueryPattern(Element elt)
-
getQueryPattern
public Element getQueryPattern()
-
addGraphURI
public void addGraphURI(java.lang.String s)
Location of the source for the data. If the model is not set, then the QueryEngine will attempt to load the data from these URIs into the default (unamed) graph.
-
addNamedGraphURI
public void addNamedGraphURI(java.lang.String uri)
Location of the source for the data. If the model is not set, then the QueryEngine will attempt to load the data from these URIs as named graphs in the dataset.
-
getGraphURIs
public java.util.List<java.lang.String> getGraphURIs()
Return the list of URIs (strings) for the unnamed graph- Returns:
- List of strings
-
usesGraphURI
public boolean usesGraphURI(java.lang.String uri)
Test whether the query mentions a URI in forming the default graph (FROM clause)- Parameters:
uri-- Returns:
- boolean True if the URI used in a FROM clause
-
getNamedGraphURIs
public java.util.List<java.lang.String> getNamedGraphURIs()
Return the list of URIs (strings) for the named graphs (FROM NAMED clause)- Returns:
- List of strings
-
usesNamedGraphURI
public boolean usesNamedGraphURI(java.lang.String uri)
Test whether the query mentions a URI for a named graph.- Parameters:
uri-- Returns:
- True if the URI used in a FROM NAMED clause
-
hasDatasetDescription
public boolean hasDatasetDescription()
Return true if the query has either some graph URIs or some named graph URIs in its description. This does not mean these URIs will be used - just that they are noted as part of the query.
-
getDatasetDescription
public DatasetDescription getDatasetDescription()
Return a dataset description (FROM/FROM NAMED clauses) for the query.
-
getResultVars
public java.util.List<java.lang.String> getResultVars()
Return a list of the variables requested (SELECT)
-
getProjectVars
public java.util.List<Var> getProjectVars()
Return a list of the variables requested (SELECT)
-
getProject
public VarExprList getProject()
-
addProjectVars
public void addProjectVars(java.util.Collection<?> vars)
Add a collection of projection variables to a SELECT query
-
addResultVar
public void addResultVar(java.lang.String varName)
Add a projection variable to a SELECT query
-
addResultVar
public void addResultVar(Node v)
-
addResultVar
public void addResultVar(Expr expr)
Add an to a SELECT query (a name will be created for it)
-
addResultVar
public void addResultVar(java.lang.String varName, Expr expr)Add a named expression to a SELECT query
-
hasGroupBy
public boolean hasGroupBy()
-
hasHaving
public boolean hasHaving()
-
getGroupBy
public VarExprList getGroupBy()
-
getHavingExprs
public java.util.List<Expr> getHavingExprs()
-
addGroupBy
public void addGroupBy(java.lang.String varName)
-
addGroupBy
public void addGroupBy(Node v)
-
addGroupBy
public void addGroupBy(Expr expr)
-
addHavingCondition
public void addHavingCondition(Expr expr)
-
addJsonMapping
public void addJsonMapping(java.lang.String key, Node value)
-
getJsonMapping
public java.util.Map<java.lang.String,Node> getJsonMapping()
-
hasAggregators
public boolean hasAggregators()
-
getAggregators
public java.util.List<ExprAggregator> getAggregators()
-
allocAggregate
public Expr allocAggregate(Aggregator agg)
-
hasValues
public boolean hasValues()
Does the query have a VALUES trailing block?
-
getValuesVariables
public java.util.List<Var> getValuesVariables()
Variables from a VALUES trailing block
-
getValuesData
public java.util.List<Binding> getValuesData()
Data from a VALUES trailing block. null for a Node means undef
-
setValuesDataBlock
public void setValuesDataBlock(java.util.List<Var> variables, java.util.List<Binding> values)
-
getConstructTemplate
public Template getConstructTemplate()
Get the template pattern for a construct query
-
setConstructTemplate
public void setConstructTemplate(Template templ)
Set triple patterns for a construct query
-
addDescribeNode
public void addDescribeNode(Node node)
-
getResultURIs
public java.util.List<Node> getResultURIs()
Get the result list (things wanted - not the results themselves) of a DESCRIBE query.
-
setResultVars
public void setResultVars()
Set the results variables if necessary, when the query has "*" (SELECT *orDESCRIBE *) and for a construct query. This operation is idempotent and can be called to ensure the results variables have been set.
-
resetResultVars
public void resetResultVars()
If modifying a query, it may be necessary to reset the calculate of the result variables of the query forSELECT *andDESCRIBE *andCONSTRUCT.
-
visit
public void visit(QueryVisitor visitor)
-
clone
public java.lang.Object clone()
-
cloneQuery
public Query cloneQuery()
Makes a copy of this query using the syntax transform machinery.- Returns:
- Copy of this query
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
toString
public java.lang.String toString(Syntax syntax)
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
equals
public boolean equals(java.lang.Object other)
Are two queries equals - tests shape and details. Equality means that the queries do the same thing, including same variables, in the same places. Being unequals does not mean the queries do different things. For example, reordering a group or union means that a query is different. Two instances of a query parsed from the same string are equal.- Overrides:
equalsin classjava.lang.Object
-
output
public void output(org.apache.jena.atlas.io.IndentedWriter out)
- Specified by:
outputin interfaceorg.apache.jena.atlas.io.Printable
-
serialize
public java.lang.String serialize()
Convert the query to a string
-
serialize
public java.lang.String serialize(Syntax syntax)
Convert the query to a string in the given syntax- Parameters:
syntax-
-
serialize
public void serialize(java.io.OutputStream out)
Output the query- Parameters:
out- OutputStream
-
serialize
public void serialize(java.io.OutputStream out, Syntax syntax)Output the query- Parameters:
out- OutputStreamsyntax- Syntax URI
-
serialize
public void serialize(org.apache.jena.atlas.io.IndentedWriter writer)
Format the query- Parameters:
writer- IndentedWriter
-
serialize
public void serialize(org.apache.jena.atlas.io.IndentedWriter writer, Syntax outSyntax)Format the query- Parameters:
writer- IndentedWriteroutSyntax- Syntax URI
-
-