[#5993] Add Name DSL.quotedName() and unquotedName()

This commit is contained in:
lukaseder 2017-03-22 13:01:59 +01:00
parent 8e9ecc764f
commit f2b239a37d
2 changed files with 69 additions and 0 deletions

View File

@ -7059,6 +7059,62 @@ public class DSL {
return new NameImpl(qualifiedName.toArray(Tools.EMPTY_STRING));
}
/**
* Create a new SQL identifier using a qualified, quoted name.
* <p>
* This works like {@link #name(String...)}, except that generated
* identifiers will be guaranteed to be quoted in databases that support
* quoted identifiers.
*
* @param qualifiedName The SQL identifier's qualified name parts
* @return A {@link QueryPart} that will render the SQL identifier
*/
public static Name quotedName(String... qualifiedName) {
return new NameImpl(qualifiedName, true);
}
/**
* Create a new SQL identifier using a qualified, quoted name.
* <p>
* This works like {@link #name(Collection)}, except that generated
* identifiers will be guaranteed to be quoted in databases that support
* quoted identifiers.
*
* @param qualifiedName The SQL identifier's qualified name parts
* @return A {@link QueryPart} that will render the SQL identifier
*/
public static Name quotedName(Collection<String> qualifiedName) {
return new NameImpl(qualifiedName.toArray(Tools.EMPTY_STRING), true);
}
/**
* Create a new SQL identifier using a qualified, quoted name.
* <p>
* This works like {@link #name(String...)}, except that generated
* identifiers will be guaranteed to be quoted in databases that support
* quoted identifiers.
*
* @param qualifiedName The SQL identifier's qualified name parts
* @return A {@link QueryPart} that will render the SQL identifier
*/
public static Name unquotedName(String... qualifiedName) {
return new NameImpl(qualifiedName, false);
}
/**
* Create a new SQL identifier using a qualified, quoted name.
* <p>
* This works like {@link #name(Collection)}, except that generated
* identifiers will be guaranteed to be quoted in databases that support
* quoted identifiers.
*
* @param qualifiedName The SQL identifier's qualified name parts
* @return A {@link QueryPart} that will render the SQL identifier
*/
public static Name unquotedName(Collection<String> qualifiedName) {
return new NameImpl(qualifiedName.toArray(Tools.EMPTY_STRING), false);
}
// -------------------------------------------------------------------------
// XXX QueryPart composition
// -------------------------------------------------------------------------

View File

@ -64,9 +64,15 @@ final class NameImpl extends AbstractQueryPart implements Name {
private static final long serialVersionUID = 8562325639223483938L;
private final String[] qualifiedName;
private final Boolean quoted;
NameImpl(String[] qualifiedName) {
this(qualifiedName, null);
}
NameImpl(String[] qualifiedName, Boolean quoted) {
this.qualifiedName = nonEmpty(qualifiedName);
this.quoted = quoted;
}
private static final String[] nonEmpty(String[] qualifiedName) {
@ -95,6 +101,10 @@ final class NameImpl extends AbstractQueryPart implements Name {
@Override
public final void accept(Context<?> ctx) {
boolean previous = ctx.quote();
if (quoted != null)
ctx.quote(quoted);
// [#3437] Fully qualify this field only if allowed in the current context
if (ctx.qualify()) {
@ -108,6 +118,9 @@ final class NameImpl extends AbstractQueryPart implements Name {
else {
ctx.literal(last());
}
if (quoted != null)
ctx.quote(previous);
}
@Override