[#1146] Add Query.getSQL(boolean) to indicate that bind values should be inlined (as a convenience for Factory.renderInlined(QueryPart))
This commit is contained in:
parent
c0764d20c3
commit
f095d9aaf1
@ -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 {
|
||||
* <li>JPA native queries</li>
|
||||
* <li>etc...</li>
|
||||
* </ul>
|
||||
* <p>
|
||||
* Note, this is the same as calling {@link #getSQL(boolean)}. The boolean
|
||||
* parameter will depend on your {@link Factory}'s {@link Settings}:
|
||||
* <table border="1">
|
||||
* <tr>
|
||||
* <th><code>StatementType</code></th>
|
||||
* <th>boolean parameter</th>
|
||||
* <th>effect</th>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td> {@link StatementType#PREPARED_STATEMENT}</td>
|
||||
* <td><code>false</code> (default)</td>
|
||||
* <td>This will render bind variables to be used with a JDBC
|
||||
* {@link PreparedStatement}. You can extract bind values from this
|
||||
* <code>Query</code> using {@link #getBindValues()}</td>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td> {@link StatementType#STATEMENT}</td>
|
||||
* <td><code>true</code></td>
|
||||
* <td>This will inline all bind variables in a statement to be used with a
|
||||
* JDBC {@link Statement}</td>
|
||||
* </tr>
|
||||
* </table>
|
||||
*
|
||||
* @see #getSQL(boolean)
|
||||
*/
|
||||
String getSQL();
|
||||
|
||||
/**
|
||||
* Retrieve the SQL code rendered by this Query
|
||||
* <p>
|
||||
* 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
|
||||
* <code>List</code> cannot be modified. To modify bind values, use
|
||||
|
||||
@ -67,6 +67,13 @@ public interface QueryPartInternal extends QueryPart {
|
||||
*/
|
||||
String getSQL();
|
||||
|
||||
/**
|
||||
* Retrieve the SQL that will be rendered by this {@link QueryPart}
|
||||
* <p>
|
||||
* 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}
|
||||
* <p>
|
||||
@ -104,7 +111,7 @@ public interface QueryPartInternal extends QueryPart {
|
||||
* Reproduce the SQL dialect this {@link QueryPart} was created with
|
||||
* <p>
|
||||
* 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
|
||||
|
||||
@ -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)}
|
||||
* <p>
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public final String getSQL(boolean inline) {
|
||||
if (inline) {
|
||||
return create().renderInlined(this);
|
||||
}
|
||||
else {
|
||||
return create().render(this);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user