Skip to content

Commit

Permalink
Fix CREATE INDEX with implicit class.property syntax
Browse files Browse the repository at this point in the history
Resolves: #8268
  • Loading branch information
luigidellaquila committed May 21, 2018
1 parent eb055ac commit e391de6
Showing 1 changed file with 47 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,36 +83,31 @@ Object execute(OCommandContext ctx) {
idx = database.getMetadata().getIndexManager()
.createIndex(name.getValue(), type.getStringValue(), keyDef, null, null, metadataDoc, engine);

} else if (className == null && keyTypes == null || keyTypes.length == 0) {
//legacy: create index without specifying property names
String[] split = name.getValue().split("\\.");
if (split.length != 2) {
throw new ODatabaseException(
"Impossible to create an index without specify class and property name nor key types: " + toString());
}
OClass oClass = database.getClass(split[0]);
if (oClass == null) {
throw new ODatabaseException("Impossible to create an index, class not found: " + split[0]);
}
if (oClass.getProperty(split[1]) == null) {
throw new ODatabaseException("Impossible to create an index, property not found: " + name.getValue());
}
String[] fields = new String[] { split[1] };
idx = getoIndex(oClass, fields, engine, database, collatesList, metadataDoc);

} else {
throw new ODatabaseException(
"Impossible to create an index without specify the key type or the associated property: " + toString());
}
} else {
String[] fields = calculateProperties(ctx);
OClass oClass = getIndexClass(ctx);
if ((keyTypes == null || keyTypes.size() == 0) && collatesList == null) {

idx = oClass.createIndex(name.getValue(), type.getStringValue(), null, metadataDoc, engine, fields);
} else {
final List<OType> fieldTypeList;
if (keyTypes == null || keyTypes.size() == 0 && fields.length > 0) {
for (final String fieldName : fields) {
if (!fieldName.equals("@rid") && !oClass.existsProperty(fieldName))
throw new OIndexException(
"Index with name : '" + name.getValue() + "' cannot be created on class : '" + oClass.getName()
+ "' because field: '" + fieldName + "' is absent in class definition.");
}
fieldTypeList = ((OClassImpl) oClass).extractFieldTypes(fields);
} else
fieldTypeList = keyTypes.stream().map(x -> OType.valueOf(x.getStringValue())).collect(Collectors.toList());

final OIndexDefinition idxDef = OIndexDefinitionFactory
.createIndexDefinition(oClass, Arrays.asList(fields), fieldTypeList, collatesList, type.getStringValue(), null);

idx = database.getMetadata().getIndexManager()
.createIndex(name.getValue(), type.getStringValue(), idxDef, oClass.getPolymorphicClusterIds(), null, metadataDoc,
engine);
}
idx = getoIndex(oClass, fields, engine, database, collatesList, metadataDoc);
}

if (idx != null)
Expand All @@ -121,6 +116,35 @@ Object execute(OCommandContext ctx) {
return null;
}

private OIndex<?> getoIndex(OClass oClass, String[] fields, String engine, ODatabase database, List<OCollate> collatesList,
ODocument metadataDoc) {
OIndex<?> idx;
if ((keyTypes == null || keyTypes.size() == 0) && collatesList == null) {

idx = oClass.createIndex(name.getValue(), type.getStringValue(), null, metadataDoc, engine, fields);
} else {
final List<OType> fieldTypeList;
if (keyTypes == null || keyTypes.size() == 0 && fields.length > 0) {
for (final String fieldName : fields) {
if (!fieldName.equals("@rid") && !oClass.existsProperty(fieldName))
throw new OIndexException(
"Index with name : '" + name.getValue() + "' cannot be created on class : '" + oClass.getName()
+ "' because field: '" + fieldName + "' is absent in class definition.");
}
fieldTypeList = ((OClassImpl) oClass).extractFieldTypes(fields);
} else
fieldTypeList = keyTypes.stream().map(x -> OType.valueOf(x.getStringValue())).collect(Collectors.toList());

final OIndexDefinition idxDef = OIndexDefinitionFactory
.createIndexDefinition(oClass, Arrays.asList(fields), fieldTypeList, collatesList, type.getStringValue(), null);

idx = database.getMetadata().getIndexManager()
.createIndex(name.getValue(), type.getStringValue(), idxDef, oClass.getPolymorphicClusterIds(), null, metadataDoc,
engine);
}
return idx;
}

/***
* returns the list of property names to be indexed
*
Expand Down

0 comments on commit e391de6

Please sign in to comment.