Class AbstractStatementParser
- java.lang.Object
-
- com.google.cloud.spanner.connection.AbstractStatementParser
-
- Direct Known Subclasses:
PostgreSQLStatementParser,SpannerStatementParser
@InternalApi public abstract class AbstractStatementParser extends Object
Internal class for the Spanner Connection API.Parses
ClientSideStatements 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 forClientSideStatements. This means that an invalid DML statement could be accepted by theAbstractStatementParserand sent to Spanner, and Spanner will then reject it with some error message.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classAbstractStatementParser.ParametersInfoParameter information with positional parameters translated to named parameters.static classAbstractStatementParser.ParsedStatementA statement that has been parsedstatic classAbstractStatementParser.StatementTypeThe type of statement that has been recognized by the parser.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description booleancheckReturningClause(String sql)Checks if the given SQL string contains a Returning clause.protected abstract booleancheckReturningClauseInternal(String sql)Checks if the given SQL string contains a Returning clause.AbstractStatementParser.ParametersInfoconvertPositionalParametersToNamedParameters(char paramChar, String sql)Converts all positional parameters (?) in the given sql string into named parameters.static AbstractStatementParsergetInstance(Dialect dialect)Get an instance ofAbstractStatementParserfor the specified dialect.booleanisDdlStatement(String sql)Checks whether the given statement is (probably) a DDL statement.booleanisQuery(String sql)Checks whether the given statement is (probably) a SELECT query.booleanisUpdateStatement(String sql)Checks whether the given statement is (probably) an update statement.AbstractStatementParser.ParsedStatementparse(Statement statement)Parses the given statement and categorizes it as one of the possibleAbstractStatementParser.StatementTypes.StringremoveCommentsAndTrim(String sql)Removes comments from and trims the given sql statement using the dialect of this parser.protected abstract booleansupportsExplain()
-
-
-
Method Detail
-
getInstance
public static AbstractStatementParser getInstance(Dialect dialect)
Get an instance ofAbstractStatementParserfor the specified dialect.- Parameters:
dialect-- Returns:
-
parse
@InternalApi public AbstractStatementParser.ParsedStatement parse(Statement statement)
Parses the given statement and categorizes it as one of the possibleAbstractStatementParser.StatementTypes. 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
@InternalApi public boolean isDdlStatement(String sql)
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:
trueif the statement is a DDL statement (i.e. starts with 'CREATE', 'ALTER' or 'DROP').
-
isQuery
@InternalApi public boolean isQuery(String sql)
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:
trueif the statement is a SELECT statement (i.e. starts with 'SELECT').
-
isUpdateStatement
@InternalApi public boolean isUpdateStatement(String sql)
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:
trueif the statement is a DML update statement (i.e. starts with 'INSERT', 'UPDATE' or 'DELETE').
-
supportsExplain
protected abstract boolean supportsExplain()
-
removeCommentsAndTrim
@InternalApi public String removeCommentsAndTrim(String sql)
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. This method is used when converting a JDBC statement that uses positional parameters to a Cloud SpannerStatementinstance that requires named parameters. The input SQL string may not contain any comments. There is an exception case if the statement starts with a GSQL comment which forces it to be interpreted as a GoogleSql statement.- Parameters:
sql- The sql string without comments that should be converted- Returns:
- A
AbstractStatementParser.ParametersInfoobject 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
@InternalApi protected abstract boolean checkReturningClauseInternal(String sql)
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
@InternalApi public boolean checkReturningClause(String sql)
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.
-
-