Skip to content

Commit

Permalink
fix: don't backticks all in N1QL (#2316)
Browse files Browse the repository at this point in the history
* fix: revert code

* fix: don't backticks all

* fix: replace gluu with jans in tests

Co-authored-by: Yuriy Movchan <Yuriy.Movchan@gmail.com>
  • Loading branch information
yuremm and yurem committed Sep 6, 2022
1 parent d6db13d commit 7cc721e
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ public <T> void removeRecursively(String primaryKey, Class<T> entryClass) {
@Override
public boolean contains(Object entry) {
if (entry == null) {
throw new MappingException("Entry to persist is null");
throw new MappingException("Entry for check if exists is null");
}

// Check entry class
Expand All @@ -531,7 +531,7 @@ public boolean contains(Object entry) {

List<AttributeData> attributes = getAttributesListForPersist(entry, propertiesAnnotations);

String[] ldapReturnAttributes = getAttributes(entry, propertiesAnnotations, false);
String[] ldapReturnAttributes = getAttributes(null, propertiesAnnotations, false);

return contains(dnValue.toString(), entryClass, propertiesAnnotations, attributes, objectClasses, ldapReturnAttributes);
}
Expand Down Expand Up @@ -645,7 +645,7 @@ protected <T> Map<String, PropertyAnnotation> getAttributesMap(T entry, List<Pro
AttributesList.class);
if (ldapAttribute != null) {
if (entry == null) {
continue;
return null;
} else {
List<AttributeData> attributesList = getAttributeDataListFromCustomAttributesList(entry,
(AttributesList) ldapAttribute, propertyName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ private <O> PagedResult<JsonObject> searchImpl(BucketMapping bucketMapping, Stri
}
}

StringBuilder baseQuery = new StringBuilder("SELECT ").append(StringHelper.toString(backticksAttributes(select))).append(" FROM `").append(bucketMapping.getBucketName()).append("` AS gluu_doc ").
StringBuilder baseQuery = new StringBuilder("SELECT ").append(StringHelper.toString(backticksAttributes(select))).append(" FROM `").append(bucketMapping.getBucketName()).append("` AS jans_doc ").
append("WHERE ").append(finalExpression);

StringBuilder baseQueryWithOrder = new StringBuilder(baseQuery);
Expand Down Expand Up @@ -597,7 +597,7 @@ private <O> PagedResult<JsonObject> searchImpl(BucketMapping bucketMapping, Stri
result.setStart(start);

if ((SearchReturnDataType.COUNT == returnDataType) || (SearchReturnDataType.SEARCH_COUNT == returnDataType)) {
StringBuilder selectCountQuery = new StringBuilder("SELECT COUNT(*) as TOTAL").append(" FROM `").append(bucketMapping.getBucketName()).append("` AS gluu_doc ").
StringBuilder selectCountQuery = new StringBuilder("SELECT COUNT(*) as TOTAL").append(" FROM `").append(bucketMapping.getBucketName()).append("` AS jans_doc ").
append("WHERE ").append(finalExpression);
try {
LOG.debug("Calculating count. Execution query: '" + selectCountQuery + "'");
Expand All @@ -624,7 +624,11 @@ private String[] backticksAttributes(String[] attributes) {

String[] resultAttributes = new String[attributes.length];
for (int i = 0; i < attributes.length; i++) {
resultAttributes[i] = '`' + attributes[i] + "`";
if (attributes[i].contains("*")) {
resultAttributes[i] = attributes[i];
} else {
resultAttributes[i] = '`' + attributes[i] + "`";
}
}

return resultAttributes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,25 @@ public void checkObjectClassExcludeFilter() throws SearchException {
ConvertedExpression expression1 = simpleConverter.convertToCouchbaseFilter(filter1, null, null);

String query1 = toSelectSQL(expression1);
assertEquals(query1, "SELECT gluu_doc.* FROM `gluu` AS gluu_doc WHERE ( ( ( objectClass = \"jansPerson\" ) OR ( \"jansPerson\" IN objectClass ) ) AND ( ( ( uid = \"test\" ) OR ( \"test\" IN uid ) ) AND LOWER( uid ) = \"test\" AND ( ( objectClass = \"jansPerson\" ) OR ( \"jansPerson\" IN objectClass ) ) AND ( ( ( uid = \"test\" ) OR ( \"test\" IN uid ) ) AND LOWER( uid ) = \"test\" AND ( ( objectClass = \"jansPerson\" ) OR ( \"jansPerson\" IN objectClass ) ) AND ANY added_ IN added SATISFIES added_ = \"2020-12-16T14:58:18.398Z\" END ) AND ANY added_ IN added SATISFIES added_ = \"2020-12-16T14:58:18.398Z\" END ) )");
assertEquals(query1, "SELECT jans_doc.* FROM `gluu` AS jans_doc WHERE ( ( ( objectClass = \"jansPerson\" ) OR ( \"jansPerson\" IN objectClass ) ) AND ( ( ( uid = \"test\" ) OR ( \"test\" IN uid ) ) AND LOWER( uid ) = \"test\" AND ( ( objectClass = \"jansPerson\" ) OR ( \"jansPerson\" IN objectClass ) ) AND ( ( ( uid = \"test\" ) OR ( \"test\" IN uid ) ) AND LOWER( uid ) = \"test\" AND ( ( objectClass = \"jansPerson\" ) OR ( \"jansPerson\" IN objectClass ) ) AND ANY added_ IN added SATISFIES added_ = \"2020-12-16T14:58:18.398Z\" END ) AND ANY added_ IN added SATISFIES added_ = \"2020-12-16T14:58:18.398Z\" END ) )");

Filter filter2 = filterProcessor.excludeFilter(filter1, filterEq3);

ConvertedExpression expression2 = simpleConverter.convertToCouchbaseFilter(filter2, null, null);

String query2 = toSelectSQL(expression2);
assertEquals(query2, "SELECT gluu_doc.* FROM `gluu` AS gluu_doc WHERE ( ( ( ( uid = \"test\" ) OR ( \"test\" IN uid ) ) AND LOWER( uid ) = \"test\" AND ( ( ( uid = \"test\" ) OR ( \"test\" IN uid ) ) AND LOWER( uid ) = \"test\" AND ANY added_ IN added SATISFIES added_ = \"2020-12-16T14:58:18.398Z\" END ) AND ANY added_ IN added SATISFIES added_ = \"2020-12-16T14:58:18.398Z\" END ) )");
assertEquals(query2, "SELECT jans_doc.* FROM `gluu` AS jans_doc WHERE ( ( ( ( uid = \"test\" ) OR ( \"test\" IN uid ) ) AND LOWER( uid ) = \"test\" AND ( ( ( uid = \"test\" ) OR ( \"test\" IN uid ) ) AND LOWER( uid ) = \"test\" AND ANY added_ IN added SATISFIES added_ = \"2020-12-16T14:58:18.398Z\" END ) AND ANY added_ IN added SATISFIES added_ = \"2020-12-16T14:58:18.398Z\" END ) )");

Filter filter3 = filterProcessor.excludeFilter(filter1, Filter.createEqualityFilter("objectClass", null));

ConvertedExpression expression3 = simpleConverter.convertToCouchbaseFilter(filter3, null, null);

String query3 = toSelectSQL(expression3);
assertEquals(query3, "SELECT gluu_doc.* FROM `gluu` AS gluu_doc WHERE ( ( ( ( uid = \"test\" ) OR ( \"test\" IN uid ) ) AND LOWER( uid ) = \"test\" AND ( ( ( uid = \"test\" ) OR ( \"test\" IN uid ) ) AND LOWER( uid ) = \"test\" AND ANY added_ IN added SATISFIES added_ = \"2020-12-16T14:58:18.398Z\" END ) AND ANY added_ IN added SATISFIES added_ = \"2020-12-16T14:58:18.398Z\" END ) )");
assertEquals(query3, "SELECT jans_doc.* FROM `gluu` AS jans_doc WHERE ( ( ( ( uid = \"test\" ) OR ( \"test\" IN uid ) ) AND LOWER( uid ) = \"test\" AND ( ( ( uid = \"test\" ) OR ( \"test\" IN uid ) ) AND LOWER( uid ) = \"test\" AND ANY added_ IN added SATISFIES added_ = \"2020-12-16T14:58:18.398Z\" END ) AND ANY added_ IN added SATISFIES added_ = \"2020-12-16T14:58:18.398Z\" END ) )");
}

private String toSelectSQL(ConvertedExpression convertedExpression) {
String select = String.format("SELECT gluu_doc.* FROM `gluu` AS gluu_doc WHERE %s", convertedExpression.expression());
String select = String.format("SELECT jans_doc.* FROM `gluu` AS jans_doc WHERE %s", convertedExpression.expression());

// Substitute parameters for test
JsonObject params = convertedExpression.getQueryParameters();
Expand Down
Loading

0 comments on commit 7cc721e

Please sign in to comment.