diff --git a/jOOQ/src/main/java/org/jooq/Query.java b/jOOQ/src/main/java/org/jooq/Query.java index 5c8874e004..b93c111db2 100644 --- a/jOOQ/src/main/java/org/jooq/Query.java +++ b/jOOQ/src/main/java/org/jooq/Query.java @@ -36,9 +36,13 @@ package org.jooq; +import java.sql.PreparedStatement; +import java.sql.Statement; import java.util.List; import java.util.Map; +import org.jooq.conf.Settings; +import org.jooq.conf.StatementType; import org.jooq.exception.DataAccessException; import org.jooq.exception.DataTypeException; import org.jooq.impl.Factory; @@ -82,9 +86,44 @@ public interface Query extends QueryPart { *
+ * Note, this is the same as calling {@link #getSQL(boolean)}. The boolean + * parameter will depend on your {@link Factory}'s {@link Settings}: + *
StatementType |
+ * boolean parameter | + *effect | + *
|---|---|---|
| {@link StatementType#PREPARED_STATEMENT} | + *false (default) |
+ * This will render bind variables to be used with a JDBC
+ * {@link PreparedStatement}. You can extract bind values from this
+ * Query using {@link #getBindValues()} |
+ *
| {@link StatementType#STATEMENT} | + *true |
+ * This will inline all bind variables in a statement to be used with a + * JDBC {@link Statement} | + *
+ * See {@link #getSQL()} for more details
+ *
+ * @param inline Whether to inline bind variables
+ * @return The generated SQL
+ */
+ String getSQL(boolean inline);
+
/**
* Retrieve the bind values that will be bound by this Query. This
* List cannot be modified. To modify bind values, use
diff --git a/jOOQ/src/main/java/org/jooq/QueryPartInternal.java b/jOOQ/src/main/java/org/jooq/QueryPartInternal.java
index 16aa42ffae..7ec55d7bb4 100644
--- a/jOOQ/src/main/java/org/jooq/QueryPartInternal.java
+++ b/jOOQ/src/main/java/org/jooq/QueryPartInternal.java
@@ -67,6 +67,13 @@ public interface QueryPartInternal extends QueryPart {
*/
String getSQL();
+ /**
+ * Retrieve the SQL that will be rendered by this {@link QueryPart}
+ *
+ * This method is exposed publicly in {@link Query#getSQL(boolean)} + */ + String getSQL(boolean inline); + /** * Retrieve the bind values that will be bound by this {@link QueryPart} *
@@ -104,7 +111,7 @@ public interface QueryPartInternal extends QueryPart { * Reproduce the SQL dialect this {@link QueryPart} was created with *
* This method is for JOOQ INTERNAL USE only. Do not reference directly - * + * * @return The SQL dialect * @deprecated - 2.0.2 - The attached SQL dialect of a {@link QueryPart} * should no longer be referenced, as query parts become more diff --git a/jOOQ/src/main/java/org/jooq/impl/AbstractQueryPart.java b/jOOQ/src/main/java/org/jooq/impl/AbstractQueryPart.java index 1a31dc2450..8841bed7f1 100644 --- a/jOOQ/src/main/java/org/jooq/impl/AbstractQueryPart.java +++ b/jOOQ/src/main/java/org/jooq/impl/AbstractQueryPart.java @@ -53,6 +53,7 @@ import org.jooq.QueryPart; import org.jooq.QueryPartInternal; import org.jooq.SQLDialect; import org.jooq.Store; +import org.jooq.conf.StatementType; import org.jooq.exception.DataAccessException; import org.jooq.exception.SQLDialectNotSupportedException; @@ -113,7 +114,27 @@ abstract class AbstractQueryPart implements QueryPartInternal, AttachableInterna */ @Override public final String getSQL() { - return create().render(this); + if (Util.getStatementType(create().getSettings()) == StatementType.STATEMENT) { + return getSQL(true); + } + else { + return getSQL(false); + } + } + + /** + * This method is also declared as {@link Query#getSQL(boolean)} + *
+ * {@inheritDoc} + */ + @Override + public final String getSQL(boolean inline) { + if (inline) { + return create().renderInlined(this); + } + else { + return create().render(this); + } } /** diff --git a/jOOQ/src/main/java/org/jooq/impl/Util.java b/jOOQ/src/main/java/org/jooq/impl/Util.java index d7dd126c24..d70c46623e 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Util.java +++ b/jOOQ/src/main/java/org/jooq/impl/Util.java @@ -68,6 +68,9 @@ import org.jooq.RenderContext; import org.jooq.Schema; import org.jooq.Table; import org.jooq.Type; +import org.jooq.conf.Execution; +import org.jooq.conf.Settings; +import org.jooq.conf.StatementType; import org.jooq.exception.DataAccessException; import org.jooq.tools.Convert; import org.jooq.tools.StringUtils; @@ -756,7 +759,7 @@ final class Util { } /** - * Map a {@link Schema} according to the configured {@link SchemaMapping} + * Map a {@link Schema} according to the configured {@link org.jooq.SchemaMapping} */ @SuppressWarnings("deprecation") static Schema getMappedSchema(Configuration configuration, Schema schema) { @@ -769,7 +772,7 @@ final class Util { } /** - * Map a {@link Table} according to the configured {@link SchemaMapping} + * Map a {@link Table} according to the configured {@link org.jooq.SchemaMapping} */ @SuppressWarnings("deprecation") static Table> getMappedTable(Configuration configuration, Table> table) { @@ -814,4 +817,27 @@ final class Util { static int hash(Object object) { return 0x7FFFFFF & object.hashCode(); } + + // ------------------------------------------------------------------------ + // Settings convenience access + // ------------------------------------------------------------------------ + + /** + * Get the statement type from the settings + */ + static StatementType getStatementType(Settings settings) { + if (settings != null) { + Execution execution = settings.getExecution(); + + if (execution != null) { + StatementType result = execution.getStatementType(); + + if (result != null) { + return result; + } + } + } + + return StatementType.PREPARED_STATEMENT; + } } \ No newline at end of file