[#8409] Add support for plain SQL CREATE VIEW statements

This commit is contained in:
lukaseder 2019-04-01 12:32:54 +02:00
parent 78c4295e61
commit 92c45da529
2 changed files with 110 additions and 2 deletions

View File

@ -67,4 +67,64 @@ public interface CreateViewAsStep<R extends Record> {
*/
@Support
CreateViewFinalStep as(Select<? extends R> select);
/**
* Add an <code>AS</code> clause to the <code>CREATE VIEW</code> statement.
* <p>
* <b>NOTE</b>: 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 <code>AS</code> clause to the <code>CREATE VIEW</code> statement.
* <p>
* <b>NOTE</b>: 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 <code>AS</code> clause to the <code>CREATE VIEW</code> statement.
* <p>
* <b>NOTE</b>: 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 <code>AS</code> clause to the <code>CREATE VIEW</code> statement.
* <p>
* <b>NOTE</b>: 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);
}

View File

@ -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<R extends Record> extends AbstractQuery implements
private final BiFunction<? super Field<?>, ? 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<R extends Record> extends AbstractQuery implements
return this;
}
@Override
public final CreateViewFinalStep as(SQL sql) {
this.select = DSL.resultQuery(sql);
if (fieldNameFunction != null) {
Select<?> s = parsed();
List<Field<?>> 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<R extends Record> 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;