[#3842] Add DSL.field(Name), DSL.table(Name), DSL.schema(Name),

DSL.sequence(Name) methods
This commit is contained in:
lukaseder 2014-12-04 18:46:59 +01:00
parent bd3ca03e6b
commit 494924a1ab
5 changed files with 243 additions and 37 deletions

View File

@ -5060,6 +5060,28 @@ public class DSL {
return new SchemaImpl(name);
}
/**
* Create a qualified schema, given its schema name.
* <p>
* This constructs a schema reference given the schema's qualified name.
* jOOQ will render the schema name according to your
* {@link Settings#getRenderNameStyle()} settings. Choose
* {@link RenderNameStyle#QUOTED} to prevent syntax errors and/or SQL
* injection.
* <p>
* Example: <code><pre>
* // This schema...
* schema(name("MY_SCHEMA"));
*
* // ... will render this SQL on SQL Server with RenderNameStyle.QUOTED set
* [MY_SCHEMA]
* </pre></code>
*/
@Support
public static Schema schema(Name name) {
return new SchemaImpl(name);
}
/**
* Create a qualified sequence, given its sequence name.
* <p>
@ -5149,6 +5171,81 @@ public class DSL {
return new SequenceImpl<T>(name, schema, type);
}
/**
* Create a qualified sequence, given its sequence name.
* <p>
* This constructs a sequence reference given the sequence's qualified name.
* jOOQ will render the sequence name according to your
* {@link Settings#getRenderNameStyle()} settings. Choose
* {@link RenderNameStyle#QUOTED} to prevent syntax errors and/or SQL
* injection.
* <p>
* Example: <code><pre>
* // This sequence...
* sequence(name("MY_SCHEMA", "MY_SEQUENCE"));
*
* // ... will render this SQL on SQL Server with RenderNameStyle.QUOTED set
* [MY_SCHEMA].[MY_SEQUENCE]
* </pre></code>
*/
@Support
public static Sequence<BigInteger> sequence(Name name) {
return sequence(name, BigInteger.class);
}
/**
* Create a qualified sequence, given its sequence name.
* <p>
* This constructs a sequence reference given the sequence's qualified name.
* jOOQ will render the sequence name according to your
* {@link Settings#getRenderNameStyle()} settings. Choose
* {@link RenderNameStyle#QUOTED} to prevent syntax errors and/or SQL
* injection.
* <p>
* Example: <code><pre>
* // This sequence...
* sequence(name("MY_SCHEMA", "MY_SEQUENCE"));
*
* // ... will render this SQL on SQL Server with RenderNameStyle.QUOTED set
* [MY_SCHEMA].[MY_SEQUENCE]
* </pre></code>
*/
@Support
public static <T extends Number> Sequence<T> sequence(Name name, Class<T> type) {
return sequence(name, getDataType(type));
}
/**
* Create a qualified sequence, given its sequence name.
* <p>
* This constructs a sequence reference given the sequence's qualified name.
* jOOQ will render the sequence name according to your
* {@link Settings#getRenderNameStyle()} settings. Choose
* {@link RenderNameStyle#QUOTED} to prevent syntax errors and/or SQL
* injection.
* <p>
* Example: <code><pre>
* // This sequence...
* sequence(name("MY_SCHEMA", "MY_SEQUENCE"));
*
* // ... will render this SQL on SQL Server with RenderNameStyle.QUOTED set
* [MY_SCHEMA].[MY_SEQUENCE]
* </pre></code>
*/
@Support
public static <T extends Number> Sequence<T> sequence(Name name, DataType<T> type) {
if (name == null)
throw new NullPointerException();
if (name.getName().length < 1 || name.getName().length > 2)
throw new IllegalArgumentException("Must provide a qualified name of length 1 or 2 : " + name);
String n = name.getName()[name.getName().length - 1];
Schema s = name.getName().length == 2 ? schema(name(name.getName()[0])) : null;
return new SequenceImpl<T>(n, s, type);
}
/**
* Create a qualified table, given its table name.
* <p>
@ -5172,7 +5269,29 @@ public class DSL {
*/
@Support
public static Table<Record> tableByName(String... qualifiedName) {
return new QualifiedTable(qualifiedName);
return table(name(qualifiedName));
}
/**
* Create a qualified table, given its table name.
* <p>
* This constructs a table reference given the table's qualified name. jOOQ
* will render the table name according to your
* {@link Settings#getRenderNameStyle()} settings. Choose
* {@link RenderNameStyle#QUOTED} to prevent syntax errors and/or SQL
* injection.
* <p>
* Example: <code><pre>
* // This table...
* tableByName("MY_SCHEMA", "MY_TABLE");
*
* // ... will render this SQL on SQL Server with RenderNameStyle.QUOTED set
* [MY_SCHEMA].[MY_TABLE]
* </pre></code>
*/
@Support
public static Table<Record> table(Name name) {
return new QualifiedTable(name);
}
/**
@ -5279,7 +5398,100 @@ public class DSL {
*/
@Support
public static <T> Field<T> fieldByName(DataType<T> type, String... qualifiedName) {
return new QualifiedField<T>(type, qualifiedName);
return field(name(qualifiedName), type);
}
/**
* Create a qualified field, given its (qualified) field name.
* <p>
* This constructs a field reference given the field's qualified name. jOOQ
* will render the field name according to your
* {@link Settings#getRenderNameStyle()} settings. Choose
* {@link RenderNameStyle#QUOTED} to prevent syntax errors and/or SQL
* injection.
* <p>
* Example: <code><pre>
* // This field...
* field(name("MY_SCHEMA", "MY_TABLE", "MY_FIELD"));
*
* // ... will render this SQL on SQL Server with RenderNameStyle.QUOTED set
* [MY_SCHEMA].[MY_TABLE].[MY_FIELD]
* </pre></code>
* <p>
* Another example: <code><pre>
* create.select(field("length({1})", Integer.class, field(name("TITLE"))))
* .from(table(name("T_BOOK")))
* .fetch();
*
* // ... will execute this SQL on SQL Server:
* select length([TITLE]) from [T_BOOK]
* </pre></code>
*/
@Support
public static Field<Object> field(Name name) {
return field(name, Object.class);
}
/**
* Create a qualified field, given its (qualified) field name.
* <p>
* This constructs a field reference given the field's qualified name. jOOQ
* will render the field name according to your
* {@link Settings#getRenderNameStyle()} settings. Choose
* {@link RenderNameStyle#QUOTED} to prevent syntax errors and/or SQL
* injection.
* <p>
* Example: <code><pre>
* // This field...
* field(name("MY_SCHEMA", "MY_TABLE", "MY_FIELD"));
*
* // ... will render this SQL on SQL Server with RenderNameStyle.QUOTED set
* [MY_SCHEMA].[MY_TABLE].[MY_FIELD]
* </pre></code>
* <p>
* Another example: <code><pre>
* create.select(field("length({1})", Integer.class, field(name("TITLE"))))
* .from(table(name("T_BOOK")))
* .fetch();
*
* // ... will execute this SQL on SQL Server:
* select length([TITLE]) from [T_BOOK]
* </pre></code>
*/
@Support
public static <T> Field<T> field(Name name, Class<T> type) {
return field(name, getDataType(type));
}
/**
* Create a qualified field, given its (qualified) field name.
* <p>
* This constructs a field reference given the field's qualified name. jOOQ
* will render the field name according to your
* {@link Settings#getRenderNameStyle()} settings. Choose
* {@link RenderNameStyle#QUOTED} to prevent syntax errors and/or SQL
* injection.
* <p>
* Example: <code><pre>
* // This field...
* field(name("MY_SCHEMA", "MY_TABLE", "MY_FIELD"));
*
* // ... will render this SQL on SQL Server with RenderNameStyle.QUOTED set
* [MY_SCHEMA].[MY_TABLE].[MY_FIELD]
* </pre></code>
* <p>
* Another example: <code><pre>
* create.select(field("length({1})", Integer.class, field(name("TITLE"))))
* .from(table(name("T_BOOK")))
* .fetch();
*
* // ... will execute this SQL on SQL Server:
* select length([TITLE]) from [T_BOOK]
* </pre></code>
*/
@Support
public static <T> Field<T> field(Name name, DataType<T> type) {
return new QualifiedField<T>(type, name);
}
// -------------------------------------------------------------------------

View File

@ -73,14 +73,21 @@ class NameImpl extends AbstractQueryPart implements Name {
@Override
public final void accept(Context<?> ctx) {
String separator = "";
for (String name : qualifiedName) {
if (!StringUtils.isEmpty(name)) {
ctx.sql(separator).literal(name);
separator = ".";
// [#3437] Fully qualify this field only if allowed in the current context
if (ctx.qualify()) {
String separator = "";
for (String name : qualifiedName) {
if (!StringUtils.isEmpty(name)) {
ctx.sql(separator).literal(name);
separator = ".";
}
}
}
else {
ctx.literal(qualifiedName[qualifiedName.length - 1]);
}
}
@Override

View File

@ -43,6 +43,7 @@ package org.jooq.impl;
import org.jooq.Context;
import org.jooq.DataType;
import org.jooq.Field;
import org.jooq.Name;
import org.jooq.RenderContext;
/**
@ -58,12 +59,12 @@ class QualifiedField<T> extends AbstractField<T> {
*/
private static final long serialVersionUID = 6937002867156868761L;
private final String[] sql;
private final Name name;
QualifiedField(DataType<T> type, String... sql) {
super(sql[sql.length - 1], type);
QualifiedField(DataType<T> type, Name name) {
super(name.getName()[name.getName().length - 1], type);
this.sql = sql;
this.name = name;
}
// ------------------------------------------------------------------------
@ -72,20 +73,6 @@ class QualifiedField<T> extends AbstractField<T> {
@Override
public final void accept(Context<?> ctx) {
// [#3437] Fully qualify this field only if allowed in the current context
if (ctx.qualify()) {
String separator = "";
for (String string : sql) {
ctx.sql(separator);
ctx.literal(string);
separator = ".";
}
}
else {
ctx.literal(sql[sql.length - 1]);
}
ctx.visit(name);
}
}

View File

@ -45,6 +45,7 @@ import static org.jooq.Clause.TABLE_REFERENCE;
import org.jooq.Clause;
import org.jooq.Context;
import org.jooq.Name;
import org.jooq.Record;
import org.jooq.RenderContext;
import org.jooq.Table;
@ -63,12 +64,12 @@ class QualifiedTable extends AbstractTable<Record> {
private static final long serialVersionUID = 6937002867156868761L;
private static final Clause[] CLAUSES = { TABLE, TABLE_REFERENCE };
private final String[] sql;
private final Name name;
QualifiedTable(String... sql) {
super(sql[sql.length - 1]);
QualifiedTable(Name name) {
super(name.getName()[name.getName().length - 1]);
this.sql = sql;
this.name = name;
}
// ------------------------------------------------------------------------
@ -77,13 +78,7 @@ class QualifiedTable extends AbstractTable<Record> {
@Override
public final void accept(Context<?> ctx) {
String separator = "";
for (String string : sql) {
ctx.sql(separator);
ctx.literal(string);
separator = ".";
}
ctx.visit(name);
}
@Override

View File

@ -49,6 +49,7 @@ import java.util.List;
import org.jooq.Clause;
import org.jooq.Context;
import org.jooq.Name;
import org.jooq.Schema;
import org.jooq.Sequence;
import org.jooq.Table;
@ -69,6 +70,10 @@ public class SchemaImpl extends AbstractQueryPart implements Schema {
private final String schemaName;
SchemaImpl(Name name) {
this(name.getName()[0]);
}
public SchemaImpl(String name) {
super();