Class JsonQueryElement
java.lang.Object
org.gcube.informationsystem.resourceregistry.queries.json.base.JsonQueryElement
- Direct Known Subclasses:
JsonQueryContext,JsonQueryEntitySchema,JsonQueryERElement,JsonQueryPropertySchema,JsonQueryQueryTemplate,JsonQueryRelationSchema
Base class translating a JSON query subtree into an SQL/MATCH fragment.
Covers every collection queryable via the JSON Query engine, whether or not it
participates in graph relation traversal. Relation-traversal-specific concerns
(edge direction, back-reference traversal) live exclusively in
JsonQueryERElement, the subclass used by Resources/Facets/Relations — a flat,
standalone collection (e.g. QueryTemplate, Context) extends this class directly.
- Author:
- Luca Frosini (ISTI - CNR)
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final org.gcube.informationsystem.base.reference.AccessTypeThe access type category of the query element.protected StringContains the alias if needed by the classprotected List<JsonQueryElement>The chain of callers of this instance analyzer.protected booleanWhether this query element represents the root entry point of the query.The set of field names to ignore/remove during query translation.This field is used by entry point only.protected final org.gcube.com.fasterxml.jackson.databind.JsonNodeThe JSON query subtree node representing this element.protected org.slf4j.LoggerThe logger instance for query generation auditing.protected final org.gcube.com.fasterxml.jackson.databind.ObjectMapperIn-use Jackson object mapper.protected booleanWhether the root match may include registered subtypes.protected intThe breadcrumb.size() provide the level of nesting.protected booleanInstruct the JSON query analyzer if it is a projectionprotected intMonotonic parameter index owned by the entry-point element.Caller-supplied scalar values bound to the generated OrientDB query.protected intit indicates the number of properties in this.jsonNode This number is manipulated while analyzing the jsonNode to properly create the query.protected final StringThe schema type name of the query element. -
Constructor Summary
ConstructorsConstructorDescriptionJsonQueryElement(org.gcube.com.fasterxml.jackson.databind.JsonNode jsonQuery, org.gcube.informationsystem.base.reference.AccessType accessType) Creates a newJsonQueryElementinstance for the specified JSON query subtree. -
Method Summary
Modifier and TypeMethodDescriptionprotected StringBufferaddConstraints(org.gcube.com.fasterxml.jackson.databind.JsonNode jsonNode, LogicalOperator queryLogicalOperator, String fieldNamePrefix) Builds and appends constraints from a JSON node using a logical operator.protected voidaddFieldToEmit(String fieldToEmit) Add a field to emit only if this instance is the entry point.createMatchQuery(StringBuffer stringBuffer) Builds and appends the complete SQL MATCH query for this subtree.createQuery(StringBuffer stringBuffer) Translates this query element into a corresponding SQL query buffer (defaults to MATCH query).protected StringBufferevaluateNode(org.gcube.com.fasterxml.jackson.databind.JsonNode jsonNode, String fieldName, String fieldNamePrefix) Evaluates a single JSON property node and appends its filter condition to the query.protected StringBufferReturns the unique alias buffer for this query element.getAlias()Returns the alias associated with this element.getAlias(boolean generateifNull) Returns the alias associated with this element, optionally generating it if not present.Returns the list of parent query elements visited.protected List<JsonQueryElement>Returns the list of query elements visited down to the current node.protected StringFormats and returns a fully-qualified property field key path.intReturns the position level of this element.Returns a snapshot of the parameters generated while translating this query tree.protected StringgetScalarValue(org.gcube.com.fasterxml.jackson.databind.JsonNode jsonNode) Binds a single scalar JSON node value as an OrientDB named parameter.protected abstract StringBuffergetSpecificMatchQuery(List<JsonQueryElement> childrenBreadcrumb) Generates the specific MATCH query snippet for this element's subclass (subclass hook).getType()Returns the schema type name of this element.protected StringgetValue(org.gcube.com.fasterxml.jackson.databind.JsonNode jsonNode) Converts a JSON value to one or more OrientDB named-parameter placeholders.booleanReturns whether this query element represents the root entry point of the query.booleanReturns whether this query element is part of a projection.voidsetBreadcrumb(List<JsonQueryElement> breadcrumb) Sets the list of parent query elements visited.voidsetEntryPoint(boolean entryPoint) Sets whether this query element represents the root entry point of the query.voidsetPolymorphic(boolean polymorphic) Configures whether the entry-point match includes registered subtypes.voidsetPosition(int position) Sets the position level of this element.voidsetProjection(boolean projection) Sets whether this query element is part of a projection.static voidvalidateType(String type, org.gcube.informationsystem.base.reference.AccessType requiredAccessType) Validates whether the specified schema type belongs to the required access type category.protected StringBufferwrapMatchQuery(StringBuffer buffer) Wraps and formats the completed MATCH query snippet with standard SELECT / EXPAND / RETURN blocks.
-
Field Details
-
logger
protected org.slf4j.Logger loggerThe logger instance for query generation auditing. -
objectMapper
protected final org.gcube.com.fasterxml.jackson.databind.ObjectMapper objectMapperIn-use Jackson object mapper. -
type
The schema type name of the query element. -
jsonNode
protected final org.gcube.com.fasterxml.jackson.databind.JsonNode jsonNodeThe JSON query subtree node representing this element. -
accessType
protected final org.gcube.informationsystem.base.reference.AccessType accessTypeThe access type category of the query element. -
fieldNamesToRemove
The set of field names to ignore/remove during query translation. -
entryPoint
protected boolean entryPointWhether this query element represents the root entry point of the query. -
polymorphic
protected boolean polymorphicWhether the root match may include registered subtypes. -
projection
protected boolean projectionInstruct the JSON query analyzer if it is a projection -
breadcrumb
The chain of callers of this instance analyzer. breadcrumb.get(breadcrumb.size-1) == caller breadcrumb is empty if this instance is an entry point -
position
protected int positionThe breadcrumb.size() provide the level of nesting. The position which element number at the same level. E.g. the index of a ConsistsOf when the caller is a Resource. It is used in conjunction with breadcrumb.size() to attach a number to the alias. This allows to generate a predictive not clashing alias instead of using random string/number. This is useful for testing purposes. -
alias
Contains the alias if needed by the class -
fieldsToEmit
This field is used by entry point only. Any class at any level can retrieve the entry point using breadcrumb.get(0); -
queryParameters
Caller-supplied scalar values bound to the generated OrientDB query. -
queryParameterIndex
protected int queryParameterIndexMonotonic parameter index owned by the entry-point element. -
size
protected int sizeit indicates the number of properties in this.jsonNode This number is manipulated while analyzing the jsonNode to properly create the query.
-
-
Constructor Details
-
JsonQueryElement
public JsonQueryElement(org.gcube.com.fasterxml.jackson.databind.JsonNode jsonQuery, org.gcube.informationsystem.base.reference.AccessType accessType) throws org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaException, org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException Creates a newJsonQueryElementinstance for the specified JSON query subtree.- Parameters:
jsonQuery- the JSON query node representing this elementaccessType- the access type category- Throws:
org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaException- if schema validation failsorg.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException- on database or lookup error
-
-
Method Details
-
validateType
public static void validateType(String type, org.gcube.informationsystem.base.reference.AccessType requiredAccessType) throws org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaException, org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException Validates whether the specified schema type belongs to the required access type category.- Parameters:
type- the schema type name to validaterequiredAccessType- the expected access type category- Throws:
org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaException- if the schema type is not registeredorg.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException- on database error
-
getType
Returns the schema type name of this element.- Returns:
- the schema type name
-
isEntryPoint
public boolean isEntryPoint()Returns whether this query element represents the root entry point of the query.- Returns:
trueif this is the entry point,falseotherwise
-
setEntryPoint
public void setEntryPoint(boolean entryPoint) Sets whether this query element represents the root entry point of the query.- Parameters:
entryPoint-trueif this is the entry point,falseotherwise
-
setPolymorphic
public void setPolymorphic(boolean polymorphic) Configures whether the entry-point match includes registered subtypes.- Parameters:
polymorphic-truefor polymorphic matching;falsefor the exact Type
-
isProjection
public boolean isProjection()Returns whether this query element is part of a projection.- Returns:
trueif this is a projection,falseotherwise
-
setProjection
public void setProjection(boolean projection) Sets whether this query element is part of a projection.- Parameters:
projection-trueif this is a projection,falseotherwise
-
getBreadcrumb
Returns the list of parent query elements visited.- Returns:
- the list of parent query elements
-
setBreadcrumb
Sets the list of parent query elements visited.- Parameters:
breadcrumb- the list of parent query elements to set
-
getQueryParameters
Returns a snapshot of the parameters generated while translating this query tree.- Returns:
- generated OrientDB named parameters
-
getPosition
public int getPosition()Returns the position level of this element.- Returns:
- the position level
-
setPosition
public void setPosition(int position) Sets the position level of this element.- Parameters:
position- the position level to set
-
getAlias
Returns the alias associated with this element.- Returns:
- the alias string
-
getAlias
Returns the alias associated with this element, optionally generating it if not present.- Parameters:
generateifNull-trueto generate alias if null- Returns:
- the alias string
-
generateAlias
Returns the unique alias buffer for this query element.- Returns:
- the alias string buffer
-
addFieldToEmit
Add a field to emit only if this instance is the entry point.- Parameters:
fieldToEmit- the field name or alias to emit in projection
-
createQuery
public StringBuffer createQuery(StringBuffer stringBuffer) throws org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaNotFoundException, org.gcube.informationsystem.resourceregistry.api.exceptions.queries.InvalidQueryException, org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaException, org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException Translates this query element into a corresponding SQL query buffer (defaults to MATCH query).- Parameters:
stringBuffer- the buffer to append to- Returns:
- the SQL query string buffer
- Throws:
org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaNotFoundException- if the schema type is not registeredorg.gcube.informationsystem.resourceregistry.api.exceptions.queries.InvalidQueryException- if query syntax or constraints are violatedorg.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaException- if schema traversal failsorg.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException- on database error
-
addConstraints
protected StringBuffer addConstraints(org.gcube.com.fasterxml.jackson.databind.JsonNode jsonNode, LogicalOperator queryLogicalOperator, String fieldNamePrefix) throws org.gcube.informationsystem.resourceregistry.api.exceptions.queries.InvalidQueryException Builds and appends constraints from a JSON node using a logical operator.- Parameters:
jsonNode- the constraints JSON nodequeryLogicalOperator- the logical operator to connect constraintsfieldNamePrefix- the field name prefix- Returns:
- the formatted constraints string buffer
- Throws:
org.gcube.informationsystem.resourceregistry.api.exceptions.queries.InvalidQueryException- if the constraints are invalid or syntactically incorrect
-
evaluateNode
protected StringBuffer evaluateNode(org.gcube.com.fasterxml.jackson.databind.JsonNode jsonNode, String fieldName, String fieldNamePrefix) throws org.gcube.informationsystem.resourceregistry.api.exceptions.queries.InvalidQueryException Evaluates a single JSON property node and appends its filter condition to the query.- Parameters:
jsonNode- the JSON value nodefieldName- the property field namefieldNamePrefix- the property field name prefix- Returns:
- the formatted filter condition string buffer
- Throws:
org.gcube.informationsystem.resourceregistry.api.exceptions.queries.InvalidQueryException- if the property value is invalid or unsupported
-
getKey
Formats and returns a fully-qualified property field key path.- Parameters:
fieldName- the property field namefieldNamePrefix- the property field name prefix- Returns:
- the fully-qualified field key string
-
getValue
Converts a JSON value to one or more OrientDB named-parameter placeholders.Array nodes (the right-hand operand of collection operators like
_in,_containsAll,_containsAny) are formatted as an OrientDB collection expression (e.g.[:queryValue0, :queryValue1]), with every element bound viagetScalarValue(JsonNode). Scalar nodes produce one placeholder.- Parameters:
jsonNode- the JSON value node- Returns:
- the generated named-parameter expression
-
getScalarValue
Binds a single scalar JSON node value as an OrientDB named parameter.- Parameters:
jsonNode- the JSON scalar value node- Returns:
- the named parameter placeholder
-
getChildrenBreadcrumb
Returns the list of query elements visited down to the current node.- Returns:
- the list of visited query elements
-
wrapMatchQuery
protected StringBuffer wrapMatchQuery(StringBuffer buffer) throws org.gcube.informationsystem.resourceregistry.api.exceptions.queries.InvalidQueryException Wraps and formats the completed MATCH query snippet with standard SELECT / EXPAND / RETURN blocks.- Parameters:
buffer- the accumulated MATCH query snippet- Returns:
- the fully formatted SQL MATCH query string buffer
- Throws:
org.gcube.informationsystem.resourceregistry.api.exceptions.queries.InvalidQueryException- if the query parameters or projection is invalid
-
getSpecificMatchQuery
protected abstract StringBuffer getSpecificMatchQuery(List<JsonQueryElement> childrenBreadcrumb) throws org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaException, org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException Generates the specific MATCH query snippet for this element's subclass (subclass hook).- Parameters:
childrenBreadcrumb- the list of child query elements visited- Returns:
- the MATCH query snippet string buffer
- Throws:
org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaException- if schema validation failsorg.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException- on database error
-
createMatchQuery
public StringBuffer createMatchQuery(StringBuffer stringBuffer) throws org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaException, org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException Builds and appends the complete SQL MATCH query for this subtree.- Parameters:
stringBuffer- the buffer to append to- Returns:
- the complete SQL MATCH query string buffer
- Throws:
org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaException- if schema traversal failsorg.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException- on database error
-