diff --git a/jOOQ/src/main/java/org/jooq/CreateViewAsStep.java b/jOOQ/src/main/java/org/jooq/CreateViewAsStep.java index 3a95e9d53a..27eface93b 100644 --- a/jOOQ/src/main/java/org/jooq/CreateViewAsStep.java +++ b/jOOQ/src/main/java/org/jooq/CreateViewAsStep.java @@ -67,4 +67,64 @@ public interface CreateViewAsStep { */ @Support CreateViewFinalStep as(Select select); + + /** + * Add an AS clause to the CREATE VIEW statement. + *

+ * NOTE: When inserting plain SQL into jOOQ objects, you must + * guarantee syntax integrity. You may also create the possibility of + * malicious SQL injection. Be sure to properly use bind variables and/or + * escape literals when concatenated into SQL clauses! + * + * @param sql The SQL + * @see SQL + */ + @Support + @PlainSQL + CreateViewFinalStep as(SQL sql); + + /** + * Add an AS clause to the CREATE VIEW statement. + *

+ * NOTE: When inserting plain SQL into jOOQ objects, you must + * guarantee syntax integrity. You may also create the possibility of + * malicious SQL injection. Be sure to properly use bind variables and/or + * escape literals when concatenated into SQL clauses! + * + * @param sql The SQL + * @see SQL + */ + @Support + @PlainSQL + CreateViewFinalStep as(String sql); + + /** + * Add an AS clause to the CREATE VIEW statement. + *

+ * NOTE: When inserting plain SQL into jOOQ objects, you must + * guarantee syntax integrity. You may also create the possibility of + * malicious SQL injection. Be sure to properly use bind variables and/or + * escape literals when concatenated into SQL clauses! + * + * @param sql The SQL + * @see SQL + */ + @Support + @PlainSQL + CreateViewFinalStep as(String sql, Object... bindings); + + /** + * Add an AS clause to the CREATE VIEW statement. + *

+ * NOTE: When inserting plain SQL into jOOQ objects, you must + * guarantee syntax integrity. You may also create the possibility of + * malicious SQL injection. Be sure to properly use bind variables and/or + * escape literals when concatenated into SQL clauses! + * + * @param sql The SQL + * @see SQL + */ + @Support + @PlainSQL + CreateViewFinalStep as(String sql, QueryPart... parts); } diff --git a/jOOQ/src/main/java/org/jooq/impl/CreateViewImpl.java b/jOOQ/src/main/java/org/jooq/impl/CreateViewImpl.java index dada0fe70d..1d254b2d7e 100644 --- a/jOOQ/src/main/java/org/jooq/impl/CreateViewImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/CreateViewImpl.java @@ -76,8 +76,12 @@ import org.jooq.Configuration; import org.jooq.Context; import org.jooq.CreateViewAsStep; import org.jooq.CreateViewFinalStep; +import org.jooq.DSLContext; import org.jooq.Field; +import org.jooq.QueryPart; import org.jooq.Record; +import org.jooq.ResultQuery; +import org.jooq.SQL; import org.jooq.SQLDialect; import org.jooq.Select; import org.jooq.Table; @@ -107,7 +111,8 @@ final class CreateViewImpl extends AbstractQuery implements private final BiFunction, ? super Integer, ? extends Field> fieldNameFunction; private Field[] fields; - private Select select; + private ResultQuery select; + private transient Select parsed; CreateViewImpl(Configuration configuration, Table view, Field[] fields, boolean ifNotExists, boolean orReplace) { super(configuration); @@ -153,6 +158,38 @@ final class CreateViewImpl extends AbstractQuery implements return this; } + @Override + public final CreateViewFinalStep as(SQL sql) { + this.select = DSL.resultQuery(sql); + + + if (fieldNameFunction != null) { + Select s = parsed(); + List> source = s.getSelect(); + fields = new Field[source.size()]; + for (int i = 0; i < fields.length; i++) + fields[i] = fieldNameFunction.apply(source.get(i), i); + } + + + return this; + } + + @Override + public final CreateViewFinalStep as(String sql) { + return as(DSL.sql(sql)); + } + + @Override + public final CreateViewFinalStep as(String sql, Object... bindings) { + return as(DSL.sql(sql, bindings)); + } + + @Override + public final CreateViewFinalStep as(String sql, QueryPart... parts) { + return as(DSL.sql(sql, parts)); + } + // ------------------------------------------------------------------------ // XXX: QueryPart API // ------------------------------------------------------------------------ @@ -239,12 +276,23 @@ final class CreateViewImpl extends AbstractQuery implements .paramType(INLINED) .visit( rename && !renameSupported - ? selectFrom(table(select).as(name("t"), Tools.fieldNames(f))) + ? selectFrom(table(parsed()).as(name("t"), Tools.fieldNames(f))) : select) .paramType(paramType) .end(CREATE_VIEW_AS); } + private final Select parsed() { + if (parsed != null) + return parsed; + + if (select instanceof Select) + return parsed = (Select) select; + + DSLContext dsl = configuration().dsl(); + return dsl.parser().parseSelect(dsl.renderInlined(select)); + } + @Override public final Clause[] clauses(Context ctx) { return CLAUSES;