[jOOQ/jOOQ#9925] Add support for XMLEXISTS()

This commit is contained in:
Lukas Eder 2020-03-20 11:16:13 +01:00
parent b6b6007563
commit 236deb450b
4 changed files with 245 additions and 0 deletions

View File

@ -0,0 +1,91 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq;
import static org.jooq.SQLDialect.POSTGRES;
/**
* A step in the construction of an <code>XMLEXISTS</code> predicate.
*
* @author Lukas Eder
*/
public interface XMLExistsPassingStep {
/**
* Add the <code>PASSING</code> clause to the <code>XMLEXISTS</code>
* predicate.
*/
@Support({ POSTGRES })
Condition passing(XML xml);
/**
* Add the <code>PASSING</code> clause to the <code>XMLEXISTS</code>
* predicate.
*/
@Support({ POSTGRES })
Condition passing(Field<XML> xml);
/**
* Add the <code>PASSING BY REF</code> clause to the <code>XMLEXISTS</code>
* predicate.
*/
@Support({ POSTGRES })
Condition passingByRef(XML xml);
/**
* Add the <code>PASSING BY REF</code> clause to the <code>XMLEXISTS</code>
* predicate.
*/
@Support({ POSTGRES })
Condition passingByRef(Field<XML> xml);
/**
* Add the <code>PASSING BY VALUE</code> clause to the <code>XMLEXISTS</code>
* predicate.
*/
@Support({ POSTGRES })
Condition passingByValue(XML xml);
/**
* Add the <code>PASSING BY VALUE</code> clause to the <code>XMLEXISTS</code>
* predicate.
*/
@Support({ POSTGRES })
Condition passingByValue(Field<XML> xml);
}

View File

@ -391,6 +391,7 @@ import org.jooq.WithAsStep9;
import org.jooq.WithStep;
import org.jooq.XML;
import org.jooq.XMLAttributes;
import org.jooq.XMLExistsPassingStep;
import org.jooq.conf.Settings;
import org.jooq.exception.SQLDialectNotSupportedException;
import org.jooq.tools.Convert;
@ -18262,6 +18263,22 @@ public class DSL {
return new XMLAgg(field);
}
/**
* The XML exists function.
*/
@Support({ POSTGRES })
public static XMLExistsPassingStep xmlexists(String xpath) {
return xmlexists(Tools.field(xpath));
}
/**
* The XML exists function.
*/
@Support({ POSTGRES })
public static XMLExistsPassingStep xmlexists(Field<String> xpath) {
return new XMLExists(xpath);
}
// -------------------------------------------------------------------------
// XXX JSON functions
// -------------------------------------------------------------------------

View File

@ -293,6 +293,7 @@ final class Keywords {
static final Keyword K_RAISERROR = keyword("raiserror");
static final Keyword K_RECORD = keyword("record");
static final Keyword K_RECURSIVE = keyword("recursive");
static final Keyword K_REF = keyword("ref");
static final Keyword K_REFERENCES = keyword("references");
static final Keyword K_REGEXP = keyword("regexp");
static final Keyword K_RENAME = keyword("rename");
@ -394,6 +395,7 @@ final class Keywords {
static final Keyword K_WITHIN_GROUP = keyword("within group");
static final Keyword K_WITHOUT_ARRAY_WRAPPER = keyword("without_array_wrapper");
static final Keyword K_XML = keyword("xml");
static final Keyword K_XMLEXISTS = keyword("xmlexists");
static final Keyword K_XMLTABLE = keyword("xmltable");
static final Keyword K_YEAR = keyword("year");
static final Keyword K_YEAR_MONTH = keyword("year_month");

View File

@ -0,0 +1,135 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq.impl;
import static org.jooq.impl.Keywords.K_BY;
import static org.jooq.impl.Keywords.K_PASSING;
import static org.jooq.impl.Keywords.K_REF;
import static org.jooq.impl.Keywords.K_VALUE;
import static org.jooq.impl.Keywords.K_XMLEXISTS;
import static org.jooq.impl.XMLExists.PassingMechanism.BY_REF;
import static org.jooq.impl.XMLExists.PassingMechanism.BY_VALUE;
import org.jooq.Condition;
import org.jooq.Context;
import org.jooq.Field;
import org.jooq.XML;
import org.jooq.XMLExistsPassingStep;
/**
* @author Lukas Eder
*/
final class XMLExists extends AbstractCondition implements XMLExistsPassingStep {
/**
* Generated UID
*/
private static final long serialVersionUID = -4881363881968319258L;
private final Field<String> xpath;
private final Field<XML> passing;
private final PassingMechanism passingMechanism;
XMLExists(Field<String> xpath) {
this(xpath, null, null);
}
private XMLExists(Field<String> xpath, Field<XML> passing, PassingMechanism passingMechanism) {
this.xpath = xpath;
this.passing = passing;
this.passingMechanism = passingMechanism;
}
// -------------------------------------------------------------------------
// XXX: DSL API
// -------------------------------------------------------------------------
@Override
public final Condition passing(XML xml) {
return passing(Tools.field(xml));
}
@Override
public final Condition passing(Field<XML> xml) {
return new XMLExists(xpath, xml, null);
}
@Override
public final Condition passingByRef(XML xml) {
return passingByRef(Tools.field(xml));
}
@Override
public final Condition passingByRef(Field<XML> xml) {
return new XMLExists(xpath, xml, BY_REF);
}
@Override
public final Condition passingByValue(XML xml) {
return passingByRef(Tools.field(xml));
}
@Override
public final Condition passingByValue(Field<XML> xml) {
return new XMLExists(xpath, xml, BY_VALUE);
}
enum PassingMechanism { BY_REF, BY_VALUE }
// -------------------------------------------------------------------------
// XXX: QueryPart API
// -------------------------------------------------------------------------
@Override
public final void accept(Context<?> ctx) {
ctx.visit(K_XMLEXISTS).sql('(')
.formatIndentStart()
.formatNewLine()
.visit(xpath)
.formatSeparator()
.visit(K_PASSING);
if (passingMechanism == BY_REF)
ctx.sql(' ').visit(K_BY).sql(' ').visit(K_REF);
else if (passingMechanism == BY_VALUE)
ctx.sql(' ').visit(K_BY).sql(' ').visit(K_VALUE);
ctx.sql(' ').visit(passing)
.formatIndentEnd()
.formatNewLine()
.sql(')');
}
}