Class AbstractStatementParser
java.lang.Object
com.google.cloud.spanner.connection.AbstractStatementParser
- Direct Known Subclasses:
PostgreSQLStatementParser
,SpannerStatementParser
Internal class for the Spanner Connection API.
Parses ClientSideStatement
s and normal SQL statements. The parser is able to recognize
the type of statement, allowing the connection API to know which method on Spanner should be
called. The parser does not validate the validity of statements, except for ClientSideStatement
s. This means that an invalid DML statement could be accepted by the AbstractStatementParser
and sent to Spanner, and Spanner will then reject it with some error
message.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Parameter information with positional parameters translated to named parameters.static class
A statement that has been parsedstatic enum
The type of statement that has been recognized by the parser. -
Field Summary
Modifier and TypeFieldDescriptionstatic final int
The default maximum size of the statement cache in Mb. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Checks if the given SQL string contains a Returning clause.protected abstract boolean
Checks if the given SQL string contains a Returning clause.convertPositionalParametersToNamedParameters
(char paramChar, String sql) Converts all positional parameters (?) in the given sql string into named parameters.static AbstractStatementParser
getInstance
(Dialect dialect) Get an instance ofAbstractStatementParser
for the specified dialect.boolean
isDdlStatement
(String sql) Checks whether the given statement is (probably) a DDL statement.boolean
Checks whether the given statement is (probably) a SELECT query.boolean
isUpdateStatement
(String sql) Checks whether the given statement is (probably) an update statement.Parses the given statement and categorizes it as one of the possibleAbstractStatementParser.StatementType
s.Removes comments from and trims the given sql statement using the dialect of this parser.protected abstract boolean
-
Field Details
-
DEFAULT_MAX_STATEMENT_CACHE_SIZE_MB
public static final int DEFAULT_MAX_STATEMENT_CACHE_SIZE_MBThe default maximum size of the statement cache in Mb.- See Also:
-
-
Method Details
-
getInstance
Get an instance ofAbstractStatementParser
for the specified dialect. -
parse
Parses the given statement and categorizes it as one of the possibleAbstractStatementParser.StatementType
s. The validity of the statement is not checked, unless it is a client-side statement.- Parameters:
statement
- The statement to parse.- Returns:
- the parsed and categorized statement.
-
isDdlStatement
Checks whether the given statement is (probably) a DDL statement. The method does not check the validity of the statement, only if it is a DDL statement based on the first word in the statement.- Parameters:
sql
- The statement to check (without any comments).- Returns:
true
if the statement is a DDL statement (i.e. starts with 'CREATE', 'ALTER' or 'DROP').
-
isQuery
Checks whether the given statement is (probably) a SELECT query. The method does not check the validity of the statement, only if it is a SELECT statement based on the first word in the statement.- Parameters:
sql
- The statement to check (without any comments).- Returns:
true
if the statement is a SELECT statement (i.e. starts with 'SELECT').
-
isUpdateStatement
Checks whether the given statement is (probably) an update statement. The method does not check the validity of the statement, only if it is an update statement based on the first word in the statement.- Parameters:
sql
- The statement to check (without any comments).- Returns:
true
if the statement is a DML update statement (i.e. starts with 'INSERT', 'UPDATE' or 'DELETE').
-
supportsExplain
protected abstract boolean supportsExplain() -
removeCommentsAndTrim
Removes comments from and trims the given sql statement using the dialect of this parser.- Parameters:
sql
- The sql statement to remove comments from and to trim.- Returns:
- the sql statement without the comments and leading and trailing spaces.
-
convertPositionalParametersToNamedParameters
@InternalApi public AbstractStatementParser.ParametersInfo convertPositionalParametersToNamedParameters(char paramChar, String sql) Converts all positional parameters (?) in the given sql string into named parameters. The parameters are named @p1, @p2, etc. for GoogleSQL, and $1, $2, etc. for PostgreSQL. This method is used when converting a JDBC statement that uses positional parameters to a Cloud SpannerStatement
instance that requires named parameters.- Parameters:
sql
- The sql string that should be converted to use named parameters- Returns:
- A
AbstractStatementParser.ParametersInfo
object containing a string with named parameters instead of positional parameters and the number of parameters. - Throws:
SpannerException
- If the input sql string contains an unclosed string/byte literal.
-
checkReturningClauseInternal
Checks if the given SQL string contains a Returning clause. This method is used only in case of a DML statement.- Parameters:
sql
- The sql string without comments that has to be evaluated.- Returns:
- A boolean indicating whether the sql string has a Returning clause or not.
-
checkReturningClause
Checks if the given SQL string contains a Returning clause. This method is used only in case of a DML statement.- Parameters:
sql
- The sql string without comments that has to be evaluated.- Returns:
- A boolean indicating whether the sql string has a Returning clause or not.
-