Class QueryUtils
- java.lang.Object
-
- org.apache.ignite.internal.processors.query.QueryUtils
-
public class QueryUtils extends Object
Utility methods for queries.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classQueryUtils.KeyOrValPropertyProperty used for keyFieldName or valueFieldName
-
Field Summary
Fields Modifier and Type Field Description static StringAFFINITY_KEY_INDEXAffinity key index name.static intDEFAULT_COLUMNS_COUNTstatic charDEFAULT_DELIMDefault SQL delimeter.static intDFLT_INDEXING_DISCOVERY_HISTORY_SIZEstatic StringDFLT_SCHEMADefault schema.static booleanINCLUDE_SENSITIVESetting totrueenables writing sensitive information intoString()output.static ThreadLocal<Boolean>INCLUDE_SENSITIVE_TLEnablesIgniteSystemProperties.IGNITE_TO_STRING_INCLUDE_SENSITIVEmode for current thread.static intKEY_COLKey column.static StringKEY_FIELD_NAMEField name for key.static StringPRIMARY_KEY_INDEXName of Primary Key index for every table.static StringSCHEMA_INFORMATIONSchema for system view.static StringSCHEMA_SYSSchema for system view.static charSPACE_DELIMSpace SQL delimeter.static StringTEMPLATE_PARTITIONEDWell-known template name for PARTITIONED cache.static StringTEMPLATE_REPLICATEDWell-known template name for REPLICATED cache.static intVAL_COLValue column.static StringVAL_FIELD_NAMEField name for value.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static QueryBinaryPropertybuildBinaryProperty(GridKernalContext ctx, String pathStr, Class<?> resType, Map<String,String> aliases, boolean isKeyField, boolean notNull, Object dlftVal, int precision, int scale)Builds binary object property.static QueryClassPropertybuildClassProperty(boolean key, Class<?> cls, String pathStr, Class<?> resType, Map<String,String> aliases, boolean notNull, CacheObjectContext coCtx)static QueryClassPropertybuildClassProperty(Class<?> keyCls, Class<?> valCls, String pathStr, Class<?> resType, Map<String,String> aliases, boolean notNull, CacheObjectContext coCtx)static GridQueryPropertybuildProperty(Class<?> keyCls, Class<?> valCls, String keyFieldName, String valueFieldName, String pathStr, Class<?> resType, Map<String,String> aliases, boolean notNull, CacheObjectContext coCtx)static voidcheckNotNullAllowed(CacheConfiguration cfg)Performs checks to forbid cache configurations that are not compatible with NOT NULL query fields.static SchemaOperationExceptioncheckQueryEntityConflicts(CacheConfiguration<?,?> ccfg, Collection<DynamicCacheDescriptor> descs)Check givenCacheConfigurationfor conflicts in table and index names from any query entities found in collection ofDynamicCacheDescriptors and belonging to the same schema.static IgniteSQLExceptionconvert(SchemaOperationException e)static QueryEntitycopy(QueryEntity entity)Copy query entity.static QueryIndexDescriptorImplcreateIndexDescriptor(QueryTypeDescriptorImpl typeDesc, QueryIndex idx)Create index descriptor.static StringcreateTableCacheName(String schemaName, String tblName)Construct cache name for table.static StringcreateTableKeyTypeName(String valTypeName)Construct key type name for table.static StringcreateTableValueTypeName(String schemaName, String tblName)Construct value type name for table.static chardelimeter()Return space character as an SQL delimeter in caseincludeSensitive()isfalseto make output SQL one line.static intdiscoveryHistorySize()Discovery history size.static StringfieldNameByAlias(QueryEntity entity, String alias)Get field name by alias.static StringglobalQueryId(UUID nodeId, long qryId)Get global query ID.static booleanincludeSensitive()static StringindexName(String tblName, QueryIndex idx)Get index name.static StringindexName(QueryEntity entity, QueryIndex idx)Get index name.static booleanisConvertibleTypes(Object val, Class<?> expCls)static booleanisCustomAffinityMapper(AffinityKeyMapper affinityKeyMapper)Checks whether affinity key mapper is custom or default.static voidisDdlOnSchemaSupported(String schemaName)Check if schema supports DDL statement.static booleanisEnabled(CacheConfiguration<?,?> ccfg)static booleanisGeometryClass(Class<?> cls)Checks if the given class is GEOMETRY.static booleanisSqlType(Class<?> cls)Checks if the given class can be mapped to a simple SQL type.static booleanmatches(String str, String sqlPtrn)Checks whether string matches SQL pattern.static @Nullable StringnormalizeObjectName(@Nullable String str, boolean replace)Normalize object name.static Collection<QueryEntity>normalizeQueryEntities(GridKernalContext ctx, Collection<QueryEntity> entities, CacheConfiguration<?,?> cfg)Normalize cache query entities.static QueryEntitynormalizeQueryEntity(GridKernalContext ctx, QueryEntity entity, boolean escape)Normalize query entity.static StringnormalizeSchemaName(String cacheName, @Nullable String schemaName)Stores rule for constructing schemaName according to cache configuration.static voidprocessBinaryMeta(GridKernalContext ctx, QueryEntity qryEntity, QueryTypeDescriptorImpl d)Processes declarative metadata for binary object.static voidprocessClassMeta(QueryEntity qryEntity, QueryTypeDescriptorImpl d, CacheObjectContext coCtx)Processes declarative metadata for binary object.static voidprocessDynamicIndexChange(String idxName, @Nullable QueryIndex idx, QueryTypeDescriptorImpl d)Process dynamic index change.static StringpropertyInitializationExceptionMessage(Class<?> keyCls, Class<?> valCls, String pathStr, Class<?> resType)Exception message to compare in tests.static booleanremoveFieldAndAlias(QueryEntity entity, String alias)Remove field and corresponding alias by the alias name.static StringtableName(QueryEntity entity)Get table name for entity.static @NotNull SQLExceptiontoSqlException(Exception e)Converts exception in to IgniteSqlException.static QueryTypeCandidatetypeForQueryEntity(GridKernalContext ctx, String cacheName, String schemaName, GridCacheContextInfo cacheInfo, QueryEntity qryEntity, List<Class<?>> mustDeserializeClss, boolean escape)Create type candidate for query entity.static StringtypeName(Class<?> cls)Gets type name by class.static StringtypeName(String clsName)Gets type name by class.static SchemaOperationExceptionvalidateDropColumn(QueryEntity entity, String fieldName, String colName)Checks if given column can be removed from table using itsQueryEntity.static SchemaOperationExceptionvalidateDropColumn(GridQueryTypeDescriptor type, String colName)Checks if given column can be removed from the table using itsGridQueryTypeDescriptor.static intvalidateTimeout(int timeout, TimeUnit timeUnit)static booleanwasCancelled(Throwable e)Returns true if the exception is triggered by query cancel.static SqlFieldsQuerywithQueryTimeout(SqlFieldsQuery qry, int timeout, TimeUnit timeUnit)static @Nullable SchemaOperationExceptionwrapIfNeeded(@Nullable Throwable e)Wrap schema exception if needed.
-
-
-
Field Detail
-
DEFAULT_COLUMNS_COUNT
public static final int DEFAULT_COLUMNS_COUNT
- See Also:
- Constant Field Values
-
KEY_COL
public static final int KEY_COL
Key column.- See Also:
- Constant Field Values
-
VAL_COL
public static final int VAL_COL
Value column.- See Also:
- Constant Field Values
-
DFLT_SCHEMA
public static final String DFLT_SCHEMA
Default schema.- See Also:
- Constant Field Values
-
PRIMARY_KEY_INDEX
public static final String PRIMARY_KEY_INDEX
Name of Primary Key index for every table.- See Also:
- Constant Field Values
-
AFFINITY_KEY_INDEX
public static final String AFFINITY_KEY_INDEX
Affinity key index name.- See Also:
- Constant Field Values
-
SCHEMA_SYS
public static final String SCHEMA_SYS
Schema for system view.
-
SCHEMA_INFORMATION
public static final String SCHEMA_INFORMATION
Schema for system view.- See Also:
- Constant Field Values
-
KEY_FIELD_NAME
public static final String KEY_FIELD_NAME
Field name for key.- See Also:
- Constant Field Values
-
VAL_FIELD_NAME
public static final String VAL_FIELD_NAME
Field name for value.- See Also:
- Constant Field Values
-
TEMPLATE_PARTITIONED
public static final String TEMPLATE_PARTITIONED
Well-known template name for PARTITIONED cache.- See Also:
- Constant Field Values
-
TEMPLATE_REPLICATED
public static final String TEMPLATE_REPLICATED
Well-known template name for REPLICATED cache.- See Also:
- Constant Field Values
-
DFLT_INDEXING_DISCOVERY_HISTORY_SIZE
public static final int DFLT_INDEXING_DISCOVERY_HISTORY_SIZE
-
DEFAULT_DELIM
public static final char DEFAULT_DELIM
Default SQL delimeter.- See Also:
- Constant Field Values
-
SPACE_DELIM
public static final char SPACE_DELIM
Space SQL delimeter.- See Also:
- Constant Field Values
-
INCLUDE_SENSITIVE
public static boolean INCLUDE_SENSITIVE
Setting totrueenables writing sensitive information intoString()output.
-
INCLUDE_SENSITIVE_TL
public static final ThreadLocal<Boolean> INCLUDE_SENSITIVE_TL
EnablesIgniteSystemProperties.IGNITE_TO_STRING_INCLUDE_SENSITIVEmode for current thread. Note, settingINCL_SENS_TLtofalsewill lead to generation of invalid SQL query. For example:
source query - "SELECT * FROM TBL WHERE name = 'Name'"
generated query - "SELECT * FROM TBL WHERE name = ?" - there is no parameter value in query.
It's a desired behaviour, because, whenIgniteSystemProperties.IGNITE_TO_STRING_INCLUDE_SENSITIVE= falsewe want to filter out all sensitive data and those data can be sitting in SQL constants. Please, seeGridSqlConst#getSQL(),IgniteH2Indexing#sqlWithoutConst(GridSqlStatement).
-
-
Method Detail
-
tableName
public static String tableName(QueryEntity entity)
Get table name for entity.- Parameters:
entity- Entity.- Returns:
- Table name.
-
indexName
public static String indexName(QueryEntity entity, QueryIndex idx)
Get index name.- Parameters:
entity- Query entity.idx- Index.- Returns:
- Index name.
-
indexName
public static String indexName(String tblName, QueryIndex idx)
Get index name.- Parameters:
tblName- Table name.idx- Index.- Returns:
- Index name.
-
normalizeQueryEntities
public static Collection<QueryEntity> normalizeQueryEntities(GridKernalContext ctx, Collection<QueryEntity> entities, CacheConfiguration<?,?> cfg)
Normalize cache query entities.- Parameters:
entities- Query entities.cfg- Cache config.- Returns:
- Normalized query entities.
-
normalizeQueryEntity
public static QueryEntity normalizeQueryEntity(GridKernalContext ctx, QueryEntity entity, boolean escape)
Normalize query entity. If "escape" flag is set, nothing changes. Otherwise we convert all object names to upper case and replace inner class separator characters ('$' for Java and '.' for .NET) with underscore.- Parameters:
entity- Query entity.escape- Escape flag taken form configuration.- Returns:
- Normalized query entity.
-
normalizeSchemaName
public static String normalizeSchemaName(String cacheName, @Nullable @Nullable String schemaName)
Stores rule for constructing schemaName according to cache configuration.- Parameters:
cacheName- Cache name.schemaName- Schema name.- Returns:
- Proper schema name according to ANSI-99 standard.
-
normalizeObjectName
@Nullable public static @Nullable String normalizeObjectName(@Nullable @Nullable String str, boolean replace)
Normalize object name.- Parameters:
str- String.replace- Whether to perform replace of special characters.- Returns:
- Escaped string.
-
typeForQueryEntity
public static QueryTypeCandidate typeForQueryEntity(GridKernalContext ctx, String cacheName, String schemaName, GridCacheContextInfo cacheInfo, QueryEntity qryEntity, List<Class<?>> mustDeserializeClss, boolean escape) throws IgniteCheckedException
Create type candidate for query entity.- Parameters:
cacheName- Cache name.schemaName- Schema name.cacheInfo- Cache context info.qryEntity- Query entity.mustDeserializeClss- Classes which must be deserialized.escape- Escape flag.- Returns:
- Type candidate.
- Throws:
IgniteCheckedException- If failed.
-
processBinaryMeta
public static void processBinaryMeta(GridKernalContext ctx, QueryEntity qryEntity, QueryTypeDescriptorImpl d) throws IgniteCheckedException
Processes declarative metadata for binary object.- Parameters:
ctx- Kernal context.qryEntity- Declared metadata.d- Type descriptor.- Throws:
IgniteCheckedException- If failed.
-
processClassMeta
public static void processClassMeta(QueryEntity qryEntity, QueryTypeDescriptorImpl d, CacheObjectContext coCtx) throws IgniteCheckedException
Processes declarative metadata for binary object.- Parameters:
qryEntity- Declared metadata.d- Type descriptor.- Throws:
IgniteCheckedException- If failed.
-
processDynamicIndexChange
public static void processDynamicIndexChange(String idxName, @Nullable @Nullable QueryIndex idx, QueryTypeDescriptorImpl d) throws IgniteCheckedException
Process dynamic index change.- Parameters:
idx- Index.d- Type descriptor to populate.- Throws:
IgniteCheckedException- If failed to build index information.
-
createIndexDescriptor
public static QueryIndexDescriptorImpl createIndexDescriptor(QueryTypeDescriptorImpl typeDesc, QueryIndex idx) throws IgniteCheckedException
Create index descriptor.- Parameters:
typeDesc- Type descriptor.idx- Index.- Returns:
- Index descriptor.
- Throws:
IgniteCheckedException- If failed.
-
buildBinaryProperty
public static QueryBinaryProperty buildBinaryProperty(GridKernalContext ctx, String pathStr, Class<?> resType, Map<String,String> aliases, boolean isKeyField, boolean notNull, Object dlftVal, int precision, int scale)
Builds binary object property.- Parameters:
ctx- Kernal context.pathStr- String representing path to the property. May contains dots '.' to identify nested fields.resType- Result type.aliases- Aliases.isKeyField- Key ownership flag,trueif this property is a field of the key object. Note that key not a field of itself.notNull-trueifnullvalue is not allowed.dlftVal- Default value.precision- Precision.scale- Scale.- Returns:
- Binary property.
-
buildClassProperty
public static QueryClassProperty buildClassProperty(Class<?> keyCls, Class<?> valCls, String pathStr, Class<?> resType, Map<String,String> aliases, boolean notNull, CacheObjectContext coCtx) throws IgniteCheckedException
- Parameters:
keyCls- Key class.valCls- Value class.pathStr- Path string.resType- Result type.aliases- Aliases.notNull-trueifnullvalue is not allowed.coCtx- Cache object context.- Returns:
- Class property.
- Throws:
IgniteCheckedException- If failed.
-
buildProperty
public static GridQueryProperty buildProperty(Class<?> keyCls, Class<?> valCls, String keyFieldName, String valueFieldName, String pathStr, Class<?> resType, Map<String,String> aliases, boolean notNull, CacheObjectContext coCtx) throws IgniteCheckedException
- Parameters:
keyCls- Key class.valCls- Value class.keyFieldName- Key Field.valueFieldName- Value Field.pathStr- Path string.resType- Result type.aliases- Aliases.notNull-trueifnullvalue is not allowed.coCtx- Cache object context.- Returns:
- Class property.
- Throws:
IgniteCheckedException- If failed.
-
propertyInitializationExceptionMessage
public static String propertyInitializationExceptionMessage(Class<?> keyCls, Class<?> valCls, String pathStr, Class<?> resType)
Exception message to compare in tests.- Parameters:
keyCls- key classvalCls- value classpathStr- property nameresType- property type- Returns:
- Exception message.
-
buildClassProperty
public static QueryClassProperty buildClassProperty(boolean key, Class<?> cls, String pathStr, Class<?> resType, Map<String,String> aliases, boolean notNull, CacheObjectContext coCtx)
- Parameters:
key- If this is a key property.cls- Source type class.pathStr- String representing path to the property. May contains dots '.' to identify nested fields.resType- Expected result type.aliases- Aliases.notNull-trueifnullvalue is not allowed.coCtx- Cache object context.- Returns:
- Property instance corresponding to the given path.
-
isSqlType
public static boolean isSqlType(Class<?> cls)
Checks if the given class can be mapped to a simple SQL type.- Parameters:
cls- Class.- Returns:
trueIf can.
-
isGeometryClass
public static boolean isGeometryClass(Class<?> cls)
Checks if the given class is GEOMETRY.- Parameters:
cls- Class.- Returns:
trueIf this is geometry.
-
typeName
public static String typeName(String clsName)
Gets type name by class.- Parameters:
clsName- Class name.- Returns:
- Type name.
-
typeName
public static String typeName(Class<?> cls)
Gets type name by class.- Parameters:
cls- Class.- Returns:
- Type name.
-
validateTimeout
public static int validateTimeout(int timeout, TimeUnit timeUnit)- Parameters:
timeout- Timeout.timeUnit- Time unit.- Returns:
- Converted time.
-
isEnabled
public static boolean isEnabled(CacheConfiguration<?,?> ccfg)
- Parameters:
ccfg- Cache configuration.- Returns:
trueIf query index must be enabled for this cache.
-
discoveryHistorySize
public static int discoveryHistorySize()
Discovery history size.- Returns:
- Discovery history size.
-
wrapIfNeeded
@Nullable public static @Nullable SchemaOperationException wrapIfNeeded(@Nullable @Nullable Throwable e)
Wrap schema exception if needed.- Parameters:
e- Original exception.- Returns:
- Schema exception.
-
checkQueryEntityConflicts
public static SchemaOperationException checkQueryEntityConflicts(CacheConfiguration<?,?> ccfg, Collection<DynamicCacheDescriptor> descs)
Check givenCacheConfigurationfor conflicts in table and index names from any query entities found in collection ofDynamicCacheDescriptors and belonging to the same schema.- Parameters:
ccfg- New cache configuration.descs- Cache descriptors.- Returns:
- Exception message describing found conflict or
nullif none found.
-
createTableCacheName
public static String createTableCacheName(String schemaName, String tblName)
Construct cache name for table.- Parameters:
schemaName- Schema name.tblName- Table name.- Returns:
- Cache name.
-
createTableValueTypeName
public static String createTableValueTypeName(String schemaName, String tblName)
Construct value type name for table.- Parameters:
schemaName- Schema name.tblName- Table name.- Returns:
- Value type name.
-
createTableKeyTypeName
public static String createTableKeyTypeName(String valTypeName)
Construct key type name for table.- Parameters:
valTypeName- Value type name.- Returns:
- Key type name.
-
copy
public static QueryEntity copy(QueryEntity entity)
Copy query entity.- Parameters:
entity- Query entity.- Returns:
- Copied entity.
-
checkNotNullAllowed
public static void checkNotNullAllowed(CacheConfiguration cfg)
Performs checks to forbid cache configurations that are not compatible with NOT NULL query fields. SeeQueryEntity.setNotNullFields(Set).- Parameters:
cfg- Cache configuration.
-
isCustomAffinityMapper
public static boolean isCustomAffinityMapper(AffinityKeyMapper affinityKeyMapper)
Checks whether affinity key mapper is custom or default.- Parameters:
affinityKeyMapper- Affinity key mapper.- Returns:
trueif affinity key mapper is custom.
-
validateDropColumn
public static SchemaOperationException validateDropColumn(QueryEntity entity, String fieldName, String colName)
Checks if given column can be removed from table using itsQueryEntity.- Parameters:
entity- Query entity.fieldName- Name of the field of the key or value object.colName- Name of the column.- Returns:
nullif it's OK to remove the column and exception otherwise.
-
validateDropColumn
public static SchemaOperationException validateDropColumn(GridQueryTypeDescriptor type, String colName)
Checks if given column can be removed from the table using itsGridQueryTypeDescriptor.- Parameters:
type- Type descriptor.colName- Name of the column.- Returns:
nullif it's OK to remove the column and exception otherwise.
-
wasCancelled
public static boolean wasCancelled(Throwable e)
Returns true if the exception is triggered by query cancel.- Parameters:
e- Exception.- Returns:
trueif exception is caused by cancel.
-
toSqlException
@NotNull public static @NotNull SQLException toSqlException(Exception e)
Converts exception in to IgniteSqlException.- Parameters:
e- Exception.- Returns:
- IgniteSqlException.
-
globalQueryId
public static String globalQueryId(UUID nodeId, long qryId)
Get global query ID.- Parameters:
nodeId- Node ID.qryId- Query ID.- Returns:
- Global query ID.
-
matches
public static boolean matches(String str, String sqlPtrn)
Checks whether string matches SQL pattern.- Parameters:
str- String.sqlPtrn- Pattern.- Returns:
- Whether string matches pattern.
-
fieldNameByAlias
public static String fieldNameByAlias(QueryEntity entity, String alias)
Get field name by alias.- Parameters:
entity- Query entity.alias- Filed's alias.- Returns:
- Field name.
-
convert
public static IgniteSQLException convert(SchemaOperationException e)
- Returns:
IgniteSQLExceptionwith the message same as ofthis's and
-
isDdlOnSchemaSupported
public static void isDdlOnSchemaSupported(String schemaName)
Check if schema supports DDL statement.- Parameters:
schemaName- Schema name.
-
removeFieldAndAlias
public static boolean removeFieldAndAlias(QueryEntity entity, String alias)
Remove field and corresponding alias by the alias name.- Parameters:
entity- Query entity.alias- Name of the field alias.- Returns:
trueif the field and corresponding alias is removed. Otherwise, returnsfalse.
-
withQueryTimeout
public static SqlFieldsQuery withQueryTimeout(SqlFieldsQuery qry, int timeout, TimeUnit timeUnit)
- Parameters:
qry- Query.timeout- Timeout.timeUnit- Time units.
-
includeSensitive
public static boolean includeSensitive()
- Returns:
Trueif output sensitive data allowed.
-
delimeter
public static char delimeter()
Return space character as an SQL delimeter in caseincludeSensitive()isfalseto make output SQL one line. Default multiline SQL output looks ugly in system view and other view tool. See,GridSqlConstandIgniteH2Indexing#sqlWithoutConst()for details.- Returns:
- Delimeter to use.
-
-