JcrSql2QueryParser extends BasicSqlQueryParser { @Override protected LiteralValue literal( TypeSystem typeSystem, Object value ) throws ValueFormatException { JcrValueFactory factory = ((JcrTypeSystem)typeSystem).getValueFactory(); Value jcrValue = null; if (value instanceof String) { jcrValue = factory.createValue((String)value); } else if (value instanceof Boolean) { jcrValue = factory.createValue(((Boolean)value).booleanValue()); } else if (value instanceof Binary) { jcrValue = factory.createValue((Binary)value); } else if (value instanceof DateTime) { jcrValue = factory.createValue(((DateTime)value).toCalendar()); } else if (value instanceof Calendar) { jcrValue = factory.createValue((Calendar)value); } else if (value instanceof BigDecimal) { jcrValue = factory.createValue((BigDecimal)value); } else if (value instanceof Double) { jcrValue = factory.createValue((Double)value); } else if (value instanceof Long) { jcrValue = factory.createValue((Long)value); } else if (value instanceof Reference) { jcrValue = factory.createValue((Reference)value); } else if (value instanceof URI) { jcrValue = factory.createValue((URI)value); } else if (value instanceof InputStream) { Binary binary = factory.createBinary((InputStream)value); jcrValue = factory.createValue(binary); } else if (value instanceof Name || value instanceof Path) { String strValue = typeSystem.getStringFactory().create(value); jcrValue = factory.createValue(strValue); } else if (value instanceof Node) { try { jcrValue = factory.createValue((Node)value); } catch (RepositoryException e) { throw new ValueFormatException(value, PropertyType.REFERENCE, GraphI18n.errorConvertingType.text(Node.class.getSimpleName(), Reference.class.getSimpleName(), value), e); } } else { jcrValue = factory.createValue(value.toString()); } return new LiteralValue(jcrValue, value); }  @Override String getLanguage(); static final String LANGUAGE; }
@Test public void shouldParseSelectStarFromSingleSourceWithWhereContainingPathLikeConstraint() { query = parse("SELECT * FROM [mgnl:content] WHERE PATH() LIKE '/modules/%/templates'"); assertThat(query.source(), is(instanceOf(NamedSelector.class))); assertThat(query.columns().isEmpty(), is(true)); NamedSelector selector = (NamedSelector)query.source(); assertThat(selector.name(), is(selectorName("mgnl:content"))); assertThat(selector.aliasOrName(), is(selectorName("mgnl:content"))); assertThat(selector.alias(), is(nullValue())); Comparison comparison = isComparison(query.constraint()); assertThat(comparison.getOperand1(), is((DynamicOperand)nodePath(selectorName("mgnl:content")))); assertThat(comparison.getOperand2(), is((StaticOperand)literal("/modules/%/templates"))); }
@Test public void shouldParseSelectStarFromSingleSourceWithWhereContainingTwoPathLikeConstraints() { query = parse("SELECT * FROM [mgnl:content] WHERE PATH() LIKE '/modules/%/templates' or PATH() like '/modules/%/other'"); assertThat(query.source(), is(instanceOf(NamedSelector.class))); assertThat(query.columns().isEmpty(), is(true)); NamedSelector selector = (NamedSelector)query.source(); assertThat(selector.name(), is(selectorName("mgnl:content"))); assertThat(selector.aliasOrName(), is(selectorName("mgnl:content"))); assertThat(selector.alias(), is(nullValue())); Or and = isOr(query.constraint()); Comparison comparison1 = isComparison(and.left()); assertThat(comparison1.getOperand1(), is((DynamicOperand)nodePath(selectorName("mgnl:content")))); assertThat(comparison1.getOperand2(), is((StaticOperand)literal("/modules/%/templates"))); Comparison comparison2 = isComparison(and.right()); assertThat(comparison2.getOperand1(), is((DynamicOperand)nodePath(selectorName("mgnl:content")))); assertThat(comparison2.getOperand2(), is((StaticOperand)literal("/modules/%/other"))); }
@Test public void shouldParseSelectStarFromEquijoinAndAdditionalCriteria() { query = parse("SELECT * FROM [modetest:queryable] JOIN [mix:referenceable] ON ISSAMENODE([modetest:queryable],[mix:referenceable]) WHERE PATH([modetest:queryable]) LIKE '/testroot/someQueryableNodeD/%'"); assertThat(query.columns().isEmpty(), is(true)); Join join = isJoin(query.source()); assertThat(join.getLeft(), is((Source)namedSelector(selectorName("modetest:queryable")))); assertThat(join.getRight(), is((Source)namedSelector(selectorName("mix:referenceable")))); assertThat(join.type(), is(JoinType.INNER)); SameNodeJoinCondition joinCondition = isSameNodeJoinCondition(join.getJoinCondition()); assertThat(joinCondition.selector1Name(), is(selectorName("modetest:queryable"))); assertThat(joinCondition.selector2Name(), is(selectorName("mix:referenceable"))); assertThat(joinCondition.getSelector2Path(), is(nullValue())); Comparison comparison = isComparison(query.constraint()); assertThat(comparison.getOperand1(), is((DynamicOperand)nodePath(selectorName("modetest:queryable")))); assertThat(comparison.getOperand2(), is((StaticOperand)literal("/testroot/someQueryableNodeD/%"))); }
@Test public void shouldParseSelectWithChildAxisCriteria() { query = parse("SELECT * FROM [nt:base] WHERE PATH() LIKE '/a/b/%' AND NOT PATH() LIKE '/a/b/%/%'"); assertThat(query.columns().isEmpty(), is(true)); NamedSelector selector = (NamedSelector)query.source(); assertThat(selector.name(), is(selectorName("nt:base"))); assertThat(selector.aliasOrName(), is(selectorName("nt:base"))); assertThat(selector.alias(), is(nullValue())); And and = isAnd(query.constraint()); Comparison comparison1 = isComparison(and.left()); assertThat(comparison1.getOperand1(), is((DynamicOperand)nodePath(selectorName("nt:base")))); assertThat(comparison1.getOperand2(), is((StaticOperand)literal("/a/b/%"))); Not not = isNot(and.right()); Comparison comparison2a = isComparison(not.getConstraint()); assertThat(comparison2a.getOperand1(), is((DynamicOperand)nodePath(selectorName("nt:base")))); assertThat(comparison2a.getOperand2(), is((StaticOperand)literal("/a/b/%/%"))); }