From f2b239a37d2030c45c7d13046b5ab79d5bd3a5b5 Mon Sep 17 00:00:00 2001 From: lukaseder Date: Wed, 22 Mar 2017 13:01:59 +0100 Subject: [PATCH] [#5993] Add Name DSL.quotedName() and unquotedName() --- jOOQ/src/main/java/org/jooq/impl/DSL.java | 56 +++++++++++++++++++ .../src/main/java/org/jooq/impl/NameImpl.java | 13 +++++ 2 files changed, 69 insertions(+) diff --git a/jOOQ/src/main/java/org/jooq/impl/DSL.java b/jOOQ/src/main/java/org/jooq/impl/DSL.java index 386b9b24ea..6730656bef 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DSL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DSL.java @@ -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. + *

+ * 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. + *

+ * 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 qualifiedName) { + return new NameImpl(qualifiedName.toArray(Tools.EMPTY_STRING), true); + } + + /** + * Create a new SQL identifier using a qualified, quoted name. + *

+ * 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. + *

+ * 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 qualifiedName) { + return new NameImpl(qualifiedName.toArray(Tools.EMPTY_STRING), false); + } + // ------------------------------------------------------------------------- // XXX QueryPart composition // ------------------------------------------------------------------------- diff --git a/jOOQ/src/main/java/org/jooq/impl/NameImpl.java b/jOOQ/src/main/java/org/jooq/impl/NameImpl.java index ce1a8f9b56..dffe48654c 100644 --- a/jOOQ/src/main/java/org/jooq/impl/NameImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/NameImpl.java @@ -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