use of org.folio.cql2pgjson.model.DbIndex in project raml-module-builder by folio-org.
the class CQL2PgJSON method indexNode.
private String indexNode(String index, Table targetTable, CQLTermNode node, IndexTextAndJsonValues vals, CqlModifiers modifiers) throws QueryValidationException {
// primary key
if ("id".equals(index)) {
return pgId(node, index);
}
DbIndex dbIndex;
if (targetTable == null || dbTable.equals(targetTable)) {
dbIndex = dbIndexMap.computeIfAbsent(index, i -> DbSchemaUtils.getDbIndex(dbTable, index));
} else {
// foreign table
dbIndex = dbIndexMap.computeIfAbsent(vals.getIndexJson(), i -> DbSchemaUtils.getDbIndex(targetTable, index));
}
if (dbIndex.isForeignKey()) {
return pgId(node, index);
}
String comparator = node.getRelation().getBase().toLowerCase();
switch(comparator) {
case "=":
if (CqlTermFormat.NUMBER == modifiers.getCqlTermFormat()) {
return queryBySql(dbIndex, vals, node, comparator, modifiers);
} else {
return queryByFt(index, dbIndex, vals, node, comparator, modifiers, targetTable);
}
case "adj":
case "all":
case "any":
return queryByFt(index, dbIndex, vals, node, comparator, modifiers, targetTable);
case "==":
case "<>":
if (CqlTermFormat.STRING == modifiers.getCqlTermFormat()) {
return queryByLike(index, dbIndex, vals, node, comparator, modifiers, targetTable);
} else {
return queryBySql(dbIndex, vals, node, comparator, modifiers);
}
case "<":
case ">":
case "<=":
case ">=":
return queryBySql(dbIndex, vals, node, comparator, modifiers);
default:
throw new CQLFeatureUnsupportedException("Relation " + comparator + " not implemented yet: " + node.toString());
}
}
use of org.folio.cql2pgjson.model.DbIndex in project raml-module-builder by folio-org.
the class CQL2PgJSON method queryByFt.
/**
* Create an SQL expression using Full Text query syntax.
*
* @param hasIndex
* @param vals
* @param node
* @param comparator
* @param modifiers
* @return
* @throws QueryValidationException
*/
private String queryByFt(String index, DbIndex dbIndex, IndexTextAndJsonValues vals, CQLTermNode node, String comparator, CqlModifiers modifiers, Table targetTable) throws QueryValidationException {
final String indexText = vals.getIndexText();
if (CqlAccents.RESPECT_ACCENTS == modifiers.getCqlAccents()) {
logger.log(Level.WARNING, "Ignoring /respectAccents modifier for FT search {0}", indexText);
}
if (CqlCase.RESPECT_CASE == modifiers.getCqlCase()) {
logger.log(Level.WARNING, "Ignoring /respectCase modifier for FT search {0}", indexText);
}
// Clean the term. Remove stand-alone ' *', not valid word.
String term = node.getTerm().replaceAll(" +\\*", "").trim();
Index schemaIndex = null;
if (targetTable != null) {
schemaIndex = DbSchemaUtils.getIndex(index, targetTable.getFullTextIndex());
}
String sql = queryByFt(indexText, term, comparator, schemaIndex, targetTable);
// array modifier
List<Modifier> relationModifiers = modifiers.getRelationModifiers();
if (!relationModifiers.isEmpty()) {
sql += " AND " + arrayNode(index, node, modifiers, relationModifiers, schemaIndex, vals, targetTable);
}
if (schemaIndex != null && schemaIndex.isCaseSensitive()) {
throw new CQLFeatureUnsupportedException("full text index does not support case sensitive: " + index);
}
if (!dbIndex.hasFullTextIndex() && !"true".equals(sql)) {
String s = String.format("%s, CQL >>> SQL: %s >>> %s", indexText, node.toCQL(), sql);
logger.log(Level.WARNING, "Doing FT search without index for {0}", s);
}
return sql;
}
use of org.folio.cql2pgjson.model.DbIndex in project raml-module-builder by folio-org.
the class DbSchemaUtilsTest method testGetDbIndexForName.
@Test
public void testGetDbIndexForName() {
DbIndex dbIndex = DbSchemaUtils.getDbIndex(table, "name");
assertTrue(dbIndex.hasFullTextIndex());
assertTrue(dbIndex.hasGinIndex());
assertTrue(dbIndex.hasIndex());
assertFalse(dbIndex.hasUniqueIndex());
assertFalse(dbIndex.hasLikeIndex());
}
use of org.folio.cql2pgjson.model.DbIndex in project raml-module-builder by folio-org.
the class DbSchemaUtilsTest method testGetDbIndexForEmail.
@Test
public void testGetDbIndexForEmail() {
DbIndex dbIndex = DbSchemaUtils.getDbIndex(table, "email");
assertFalse(dbIndex.hasFullTextIndex());
assertFalse(dbIndex.hasGinIndex());
assertFalse(dbIndex.hasIndex());
assertTrue(dbIndex.hasUniqueIndex());
assertFalse(dbIndex.hasLikeIndex());
}
Aggregations