com.mysql.cj

## Class QueryInfo

- java.lang.Object
- - com.mysql.cj.QueryInfo

    public class QueryInfo
    extends Object

Represents the "parsed" state of a prepared query, with the statement broken up into its static and dynamic (where parameters are bound) parts.

### Field Summary

Fields
`private static String` `AS_CLAUSE`
`private QueryInfo` `baseQueryInfo`
`private int` `batchCount`
`private static String` `CLOSING_MARKERS`
`private boolean` `containsOnDuplicateKeyUpdate`
`private String` `encoding`
`private static String` `INSERT_STATEMENT`
`private boolean` `isRewritableWithMultiValuesClause`
`private static String` `LAST_INSERT_ID_FUNC`
`private static String` `LIMIT_CLAUSE`
`private static String` `MULTIPLE_QUERIES_TAG`
`private int` `numberOfPlaceholders`
`private int` `numberOfQueries`
`private static String[]` `ODKU_CLAUSE`
`private static String` `OFFSET_CLAUSE`
`private static String` `OPENING_MARKERS`
`private static String` `OVERRIDING_MARKERS`
`private List<PlaceholderPurpose>` `placeholderPurposes`
`private int` `queryLength`
`private QueryReturnType` `queryReturnType`
`private int` `queryStartPos`
`private static String` `REPLACE_STATEMENT`
`private static String` `SELECT_STATEMENT`
`private String` `sql`
`private char` `statementFirstChar`
`private String` `statementKeyword`
`private byte[][]` `staticSqlParts`
`private static String` `TABLE_STATEMENT`
`private static String` `VALUE_CLAUSE`
`private int` `valuesClauseLength`
`private ArrayList<Integer>` `valuesEndpoints`

### Constructor Summary

Constructors
`private` `QueryInfo(QueryInfo baseQueryInfo, int batchCount)` Constructs a `QueryInfo` object with a multi-value clause for the specified batch count, that stems from the specified baseQueryInfo.
`` `QueryInfo(String sql, Session session, String encoding)` Constructs a `QueryInfo` object for the given query or multi-query.

### Method Summary

All Methods Static Methods Instance Methods Concrete Methods
`boolean` `containsOnDuplicateKeyUpdate()` Does this query info represent a query that contains an ON DUPLICATE KEY UPDATE clause? This operation does not take into consideration the multiplicity of queries in the original SQL.
`static boolean` `containsOnDuplicateKeyUpdateClause(String sql, boolean noBackslashEscapes)` Checks whether the specified SQL contains or not an ON DUPLICATE KEY UPDATE clause.
`static char` `firstCharOfStatementUc(String sql, boolean noBackslashEscapes)` Finds and returns the first non-whitespace character from the specified SQL, skipping comments and quoted text.
`private static String` `getContextForWithStatement(String sql, boolean noBackslashEscapes)` Returns the context of the WITH statement.
`char` `getFirstStmtChar()` Returns the first character of the statement from the query used to build this `QueryInfo`.
`int` `getNumberOfPlaceholders()` Returns the number of placeholders found in the query.
`int` `getNumberOfQueries()` Returns the number of queries identified in the original SQL string.
`List<PlaceholderPurpose>` `getPlaceholderPurposes()` Returns a list containing the purpose of each one of the placeholders found according to their positional ordering.
`QueryInfo` `getQueryInfoForBatch(int count)` Returns a `QueryInfo` for a multi-values INSERT/REPLACE assembled for the specified batch count, without re-parsing.
`QueryReturnType` `getQueryReturnType()` Returns the return type of the parsed query.
`static QueryReturnType` `getQueryReturnType(String sql, boolean noBackslashEscapes)` Returns the type of return that can be expected from executing the given query.
`String` `getSqlForBatch()` Returns a preparable query for the batch count of this `QueryInfo`.
`String` `getSqlForBatch(int count)` Returns a preparable query for the specified batch count.
`String` `getStatementKeyword()` Returns the statement keyword from the query used to build this `QueryInfo`.
`static String` `getStatementKeyword(String sql, boolean noBackslashEscapes)` Finds and returns the statement keyword from the specified SQL, skipping comments and quoted text.
`byte[][]` `getStaticSqlParts()` Returns the static sections of the parsed query, as byte arrays, split by the places where the placeholders were located.
`int` `getValuesClauseLength()` If this object represents a query that is re-writable as a multi-values statement and if rewriting batched statements is enabled, then returns the length of the parsed VALUES clause section, including the placeholder characters themselves, otherwise returns -1.
`static int` `indexOfStatementKeyword(String sql, boolean noBackslashEscapes)` Finds and returns the position of the first non-whitespace character from the specified SQL, skipping comments and quoted text.
`static boolean` `isReadOnlySafeQuery(String sql, boolean noBackslashEscapes)` Checks whether the given query is safe to run in a read-only session.
`boolean` `isRewritableWithMultiValuesClause()` Can this query be rewritten as a multi-values clause?

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Field Detail

#### OPENING_MARKERS

    private static final String OPENING_MARKERS

See Also:
  Constant Field Values

#### CLOSING_MARKERS

    private static final String CLOSING_MARKERS

See Also:
  Constant Field Values

#### OVERRIDING_MARKERS

    private static final String OVERRIDING_MARKERS

See Also:
  Constant Field Values

#### SELECT_STATEMENT

    private static final String SELECT_STATEMENT

See Also:
  Constant Field Values

#### TABLE_STATEMENT

    private static final String TABLE_STATEMENT

See Also:
  Constant Field Values

#### INSERT_STATEMENT

    private static final String INSERT_STATEMENT

See Also:
  Constant Field Values

#### REPLACE_STATEMENT

    private static final String REPLACE_STATEMENT

See Also:
  Constant Field Values

#### MULTIPLE_QUERIES_TAG

    private static final String MULTIPLE_QUERIES_TAG

See Also:
  Constant Field Values

#### LIMIT_CLAUSE

    private static final String LIMIT_CLAUSE

See Also:
  Constant Field Values

#### OFFSET_CLAUSE

    private static final String OFFSET_CLAUSE

See Also:
  Constant Field Values

#### VALUE_CLAUSE

    private static final String VALUE_CLAUSE

See Also:
  Constant Field Values

#### AS_CLAUSE

    private static final String AS_CLAUSE

See Also:
  Constant Field Values

#### ODKU_CLAUSE

    private static final String[] ODKU_CLAUSE

#### LAST_INSERT_ID_FUNC

    private static final String LAST_INSERT_ID_FUNC

See Also:
  Constant Field Values

#### baseQueryInfo

    private QueryInfo baseQueryInfo

#### sql

    private String sql

#### encoding

    private String encoding

#### queryReturnType

    private QueryReturnType queryReturnType

#### queryLength

    private int queryLength

#### queryStartPos

    private int queryStartPos

#### statementFirstChar

    private char statementFirstChar

#### statementKeyword

    private String statementKeyword

#### batchCount

    private int batchCount

#### numberOfPlaceholders

    private int numberOfPlaceholders

#### numberOfQueries

    private int numberOfQueries

#### containsOnDuplicateKeyUpdate

    private boolean containsOnDuplicateKeyUpdate

#### isRewritableWithMultiValuesClause

    private boolean isRewritableWithMultiValuesClause

#### valuesClauseLength

    private int valuesClauseLength

#### valuesEndpoints

    private ArrayList<Integer> valuesEndpoints

#### staticSqlParts

    private byte[][] staticSqlParts

#### placeholderPurposes

    private List<PlaceholderPurpose> placeholderPurposes

### Constructor Detail

#### QueryInfo

    public QueryInfo(String sql,
                     Session session,
                     String encoding)

Constructs a `QueryInfo` object for the given query or multi-query. The parsed result of this query allows to determine the location of the placeholders, the query static parts and whether this query can be rewritten as a multi-values clause query.

Parameters:
  `sql` - the query SQL string to parse and analyze
  `session` - the `Session` under which the query analysis must be done.
  `encoding` - the characters encoding to use when extracting the query static parts as byte arrays.

#### QueryInfo

    private QueryInfo(QueryInfo baseQueryInfo,
                      int batchCount)

Constructs a `QueryInfo` object with a multi-value clause for the specified batch count, that stems from the specified baseQueryInfo.

Parameters:
  `baseQueryInfo` - the `QueryInfo` instance that provides the query static parts used to create the new instance now augmented to accommodate the extra number of parameters
  `batchCount` - the number of batches, i.e., the number of times the VALUES clause needs to be repeated inside the new query

### Method Detail

#### getNumberOfQueries

    public int getNumberOfQueries()

Returns the number of queries identified in the original SQL string. Different queries are identified by the presence of the query delimiter character, i.e., a semicolon.

Returns:
  the number of queries identified in the original SQL string

#### getNumberOfPlaceholders

    public int getNumberOfPlaceholders()

Returns the number of placeholders found in the query.

Returns:
  the number of placeholders in the query

#### getQueryReturnType

    public QueryReturnType getQueryReturnType()

Returns the return type of the parsed query. This operation does not take into consideration the multiplicity of queries in the specified SQL.

Returns:
  the return type of the parsed query

#### getFirstStmtChar

    public char getFirstStmtChar()

Returns the first character of the statement from the query used to build this `QueryInfo`.

Returns:
  the first character of the statement

#### getStatementKeyword

    public String getStatementKeyword()

Returns the statement keyword from the query used to build this `QueryInfo`.

Returns:
  the statement keyword

#### getValuesClauseLength

    public int getValuesClauseLength()

If this object represents a query that is re-writable as a multi-values statement and if rewriting batched statements is enabled, then returns the length of the parsed VALUES clause section, including the placeholder characters themselves, otherwise returns -1.

Returns:
  the length of the parsed VALUES clause section, including the placeholder characters themselves, otherwise returns -1

#### getPlaceholderPurposes

    public List<PlaceholderPurpose> getPlaceholderPurposes()

Returns a list containing the purpose of each one of the placeholders found according to their positional ordering.

Returns:
  the list of the placeholders purposes.

#### containsOnDuplicateKeyUpdate

    public boolean containsOnDuplicateKeyUpdate()

Does this query info represent a query that contains an ON DUPLICATE KEY UPDATE clause? This operation does not take into consideration the multiplicity of queries in the original SQL. Checking whether the original query contains an ON DUPLICATE KEY UPDATE clause is conditional to how the connection properties 'dontCheckOnDuplicateKeyUpdateInSQL' and 'rewriteBatchedStatements' are set, with 'rewriteBatchedStatements=true' implicitly disabling 'dontCheckOnDuplicateKeyUpdateInSQL'.

Returns:
  `true` if the query or any of the original queries contain an ON DUPLICATE KEY UPDATE clause.

#### getStaticSqlParts

    public byte[][] getStaticSqlParts()

Returns the static sections of the parsed query, as byte arrays, split by the places where the placeholders were located.

Returns:
  the static sections of the parsed query, as byte arrays, split by the places where the placeholders were located

#### isRewritableWithMultiValuesClause

    public boolean isRewritableWithMultiValuesClause()

Can this query be rewritten as a multi-values clause?

Returns:
  `true` if the query can be rewritten as a multi-values query.

#### getQueryInfoForBatch

    public QueryInfo getQueryInfoForBatch(int count)

Returns a `QueryInfo` for a multi-values INSERT/REPLACE assembled for the specified batch count, without re-parsing.

Parameters:
  `count` - the number of parameter batches
Returns:
  `QueryInfo`

#### getSqlForBatch

    public String getSqlForBatch()

Returns a preparable query for the batch count of this `QueryInfo`.

Returns:
  a preparable query string with the appropriate number of placeholders

#### getSqlForBatch

    public String getSqlForBatch(int count)

Returns a preparable query for the specified batch count.

Parameters:
  `count` - number of parameter batches
Returns:
  a preparable query string with the appropriate number of placeholders

#### indexOfStatementKeyword

    public static int indexOfStatementKeyword(String sql,
                                              boolean noBackslashEscapes)

Finds and returns the position of the first non-whitespace character from the specified SQL, skipping comments and quoted text.

Parameters:
  `sql` - the query to search
  `noBackslashEscapes` - whether backslash escapes are disabled or not
Returns:
  the position of the first character of the query

#### firstCharOfStatementUc

    public static char firstCharOfStatementUc(String sql,
                                              boolean noBackslashEscapes)

Finds and returns the first non-whitespace character from the specified SQL, skipping comments and quoted text.

Parameters:
  `sql` - the query to search
  `noBackslashEscapes` - whether backslash escapes are disabled or not
Returns:
  the first character of the query, in upper case

#### getStatementKeyword

    public static String getStatementKeyword(String sql,
                                             boolean noBackslashEscapes)

Finds and returns the statement keyword from the specified SQL, skipping comments and quoted text.

Parameters:
  `sql` - the query to search
  `noBackslashEscapes` - whether backslash escapes are disabled or not
Returns:
  the statement keyword of the query

#### isReadOnlySafeQuery

    public static boolean isReadOnlySafeQuery(String sql,
                                              boolean noBackslashEscapes)

Checks whether the given query is safe to run in a read-only session. In case of doubt it is assumed to be safe. This operation does not take into consideration the multiplicity of queries in the specified SQL.

Parameters:
  `sql` - the query to check
  `noBackslashEscapes` - whether backslash escapes are disabled or not
Returns:
  `true` if the query is read-only safe, `false` otherwise.

#### getQueryReturnType

    public static QueryReturnType getQueryReturnType(String sql,
                                                     boolean noBackslashEscapes)

Returns the type of return that can be expected from executing the given query. This operation does not take into consideration the multiplicity of queries in the specified SQL.

Parameters:
  `sql` - the query to check
  `noBackslashEscapes` - whether backslash escapes are disabled or not
Returns:
  the return type that can be expected from the given query, one of the elements of `QueryReturnType`.

#### getContextForWithStatement

    private static String getContextForWithStatement(String sql,
                                                     boolean noBackslashEscapes)

Returns the context of the WITH statement. The context can be: SELECT, TABLE, VALUES, UPDATE or DELETE. This operation does not take into consideration the multiplicity of queries in the specified SQL.

Parameters:
  `sql` - the query to search
  `noBackslashEscapes` - whether backslash escapes are disabled or not
Returns:
  the context of the WITH statement or null if failed to find it

#### containsOnDuplicateKeyUpdateClause

    public static boolean containsOnDuplicateKeyUpdateClause(String sql,
                                                             boolean noBackslashEscapes)

Checks whether the specified SQL contains or not an ON DUPLICATE KEY UPDATE clause. This operation does not take into consideration the multiplicity of queries in the specified SQL.

Parameters:
  `sql` - the query to search
  `noBackslashEscapes` - whether backslash escapes are disabled or not.
Returns:
  `true` if the query contains an ON DUPLICATE KEY UPDATE clause, `false` otherwise

Copyright © 2016, 2025, Oracle and/or its affiliates.