Class BaseParser
- java.lang.Object
-
- migratedb.v1.core.internal.parser.BaseParser
-
- All Implemented Interfaces:
Parser
- Direct Known Subclasses:
BigQueryParser,CockroachDBParser,DB2Parser,DerbyParser,FirebirdParser,H2Parser,HSQLDBParser,IgniteThinParser,InformixParser,MySQLParser,OracleParser,PostgreSQLParser,RedshiftParser,SAPHANAParser,SnowflakeParser,SpannerParser,SQLiteParser,SQLServerParser,SybaseASEParser
public abstract class BaseParser extends Object implements Parser
The main parser all database-specific parsers derive from.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classBaseParser.ParserSqlStatementIterator
-
Constructor Summary
Constructors Modifier Constructor Description protectedBaseParser(Configuration configuration, ParsingContext parsingContext, int peekDepth)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidadjustBlockDepth(ParserContext context, List<Token> tokens, Token keyword, PeekingReader reader)protected voidadjustDelimiter(ParserContext context, StatementType statementType)Adjusts the delimiter if necessary for this statement type.static voidcloseAndAddSuppressed(AutoCloseable c, Exception context)protected ParsedSqlStatementcreateStatement(PeekingReader reader, Recorder recorder, int statementPos, int statementLine, int statementCol, int nonCommentPartPos, int nonCommentPartLine, int nonCommentPartCol, StatementType statementType, boolean canExecuteInTransaction, Delimiter delimiter, String sql)protected BooleandetectCanExecuteInTransaction(String simplifiedStatement, List<Token> keywords)protected StatementTypedetectStatementType(String simplifiedStatement, ParserContext context, PeekingReader reader)protected booleandoTokensMatchPattern(List<Token> previousTokens, Token current, Pattern regex)protected chargetAlternativeIdentifierQuote()protected chargetAlternativeStringLiteralQuote()protected chargetClosingIdentifierSymbol()ConfigurationgetConfiguration()protected DelimitergetDefaultDelimiter()protected chargetIdentifierQuote()protected intgetLastKeywordIndex(List<Token> tokens)protected intgetLastKeywordIndex(List<Token> tokens, int endIndex)protected SqlStatementgetNextStatement(Resource resource, PeekingReader reader, Recorder recorder, PositionTracker tracker, ParserContext context)protected chargetOpeningIdentifierSymbol()ParsingContextgetParsingContext()protected static TokengetPreviousToken(List<Token> tokens, int parensDepth)protected intgetTransactionalDetectionCutoff()protected Set<String>getValidKeywords()protected TokenhandleAlternativeStringLiteral(PeekingReader reader, ParserContext context, int pos, int line, int col)protected TokenhandleCommentDirective(PeekingReader reader, ParserContext context, int pos, int line, int col)protected TokenhandleDelimiter(PeekingReader reader, ParserContext context, int pos, int line, int col)protected TokenhandleKeyword(PeekingReader reader, ParserContext context, int pos, int line, int col, String keyword)protected TokenhandleStringLiteral(PeekingReader reader, ParserContext context, int pos, int line, int col)protected booleanisAlternativeStringLiteral(String peek)protected booleanisCommentDirective(String peek)protected booleanisDelimiter(String peek, ParserContext context, int col, int colIgnoringWhitespace)protected booleanisKeyword(String text)protected booleanisLetter(char c, ParserContext context)protected booleanisSingleLineComment(String peek, ParserContext context, int col)protected static booleanlastTokenIs(List<Token> tokens, int parensDepth, String tokenText)protected static booleanlastTokenIsOnLine(List<Token> tokens, int parensDepth, int line)SqlStatementIteratorparse(Resource resource)SqlStatementIteratorparse(Resource resource, SqlScriptMetadata metadata)Parses this resource into a stream of statements.protected StringreadIdentifier(PeekingReader reader)protected StringreadKeyword(PeekingReader reader, Delimiter delimiter, ParserContext context)protected ReaderreplacePlaceholders(Reader reader, SqlScriptMetadata metadata)Configures this reader for placeholder replacement.protected voidresetDelimiter(ParserContext context)Resets the delimiter to its default value before parsing a new statement.protected booleanshouldAdjustBlockDepth(ParserContext context, List<Token> tokens, Token token)protected booleanshouldDiscard(Token token, boolean nonCommentPartSeen)Whether the current set of tokens should be discarded.protected booleansupportsPeekingMultipleLines()protected static booleantokenAtIndexIs(List<Token> tokens, int index, String tokenText)
-
-
-
Field Detail
-
LOG
protected static final Log LOG
-
-
Constructor Detail
-
BaseParser
protected BaseParser(Configuration configuration, ParsingContext parsingContext, int peekDepth)
-
-
Method Detail
-
closeAndAddSuppressed
public static void closeAndAddSuppressed(AutoCloseable c, Exception context)
-
getConfiguration
public Configuration getConfiguration()
- Specified by:
getConfigurationin interfaceParser
-
getParsingContext
public ParsingContext getParsingContext()
- Specified by:
getParsingContextin interfaceParser
-
getDefaultDelimiter
protected Delimiter getDefaultDelimiter()
-
getIdentifierQuote
protected char getIdentifierQuote()
-
getAlternativeIdentifierQuote
protected char getAlternativeIdentifierQuote()
-
getAlternativeStringLiteralQuote
protected char getAlternativeStringLiteralQuote()
-
getOpeningIdentifierSymbol
protected char getOpeningIdentifierSymbol()
-
getClosingIdentifierSymbol
protected char getClosingIdentifierSymbol()
-
supportsPeekingMultipleLines
protected boolean supportsPeekingMultipleLines()
-
parse
public final SqlStatementIterator parse(Resource resource, SqlScriptMetadata metadata)
Description copied from interface:ParserParses this resource into a stream of statements.
-
parse
public final SqlStatementIterator parse(Resource resource)
-
replacePlaceholders
protected Reader replacePlaceholders(Reader reader, SqlScriptMetadata metadata)
Configures this reader for placeholder replacement.- Parameters:
reader- The original reader.metadata- The resource's metadata.- Returns:
- The new reader with placeholder replacement.
-
getNextStatement
protected SqlStatement getNextStatement(Resource resource, PeekingReader reader, Recorder recorder, PositionTracker tracker, ParserContext context)
-
shouldAdjustBlockDepth
protected boolean shouldAdjustBlockDepth(ParserContext context, List<Token> tokens, Token token)
-
shouldDiscard
protected boolean shouldDiscard(Token token, boolean nonCommentPartSeen)
Whether the current set of tokens should be discarded.- Parameters:
token- The latest token.nonCommentPartSeen- Whether a non-comment part has already been seen.- Returns:
trueif it should,falseif not.
-
resetDelimiter
protected void resetDelimiter(ParserContext context)
Resets the delimiter to its default value before parsing a new statement.
-
adjustDelimiter
protected void adjustDelimiter(ParserContext context, StatementType statementType)
Adjusts the delimiter if necessary for this statement type.- Parameters:
statementType- The statement type.
-
getTransactionalDetectionCutoff
protected int getTransactionalDetectionCutoff()
- Returns:
- The cutoff point in terms of number of tokens after which a statement can no longer be non-transactional.
-
adjustBlockDepth
protected void adjustBlockDepth(ParserContext context, List<Token> tokens, Token keyword, PeekingReader reader) throws IOException
- Throws:
IOException
-
lastTokenIs
protected static boolean lastTokenIs(List<Token> tokens, int parensDepth, String tokenText)
-
lastTokenIsOnLine
protected static boolean lastTokenIsOnLine(List<Token> tokens, int parensDepth, int line)
-
tokenAtIndexIs
protected static boolean tokenAtIndexIs(List<Token> tokens, int index, String tokenText)
-
doTokensMatchPattern
protected boolean doTokensMatchPattern(List<Token> previousTokens, Token current, Pattern regex)
-
createStatement
protected ParsedSqlStatement createStatement(PeekingReader reader, Recorder recorder, int statementPos, int statementLine, int statementCol, int nonCommentPartPos, int nonCommentPartLine, int nonCommentPartCol, StatementType statementType, boolean canExecuteInTransaction, Delimiter delimiter, String sql) throws IOException
- Throws:
IOException
-
detectStatementType
protected StatementType detectStatementType(String simplifiedStatement, ParserContext context, PeekingReader reader)
-
detectCanExecuteInTransaction
protected Boolean detectCanExecuteInTransaction(String simplifiedStatement, List<Token> keywords)
-
readKeyword
protected String readKeyword(PeekingReader reader, Delimiter delimiter, ParserContext context) throws IOException
- Throws:
IOException
-
readIdentifier
protected String readIdentifier(PeekingReader reader) throws IOException
- Throws:
IOException
-
handleDelimiter
protected Token handleDelimiter(PeekingReader reader, ParserContext context, int pos, int line, int col) throws IOException
- Throws:
IOException
-
isAlternativeStringLiteral
protected boolean isAlternativeStringLiteral(String peek)
-
isDelimiter
protected boolean isDelimiter(String peek, ParserContext context, int col, int colIgnoringWhitespace)
-
isLetter
protected boolean isLetter(char c, ParserContext context)
-
isSingleLineComment
protected boolean isSingleLineComment(String peek, ParserContext context, int col)
-
isKeyword
protected boolean isKeyword(String text)
-
isCommentDirective
protected boolean isCommentDirective(String peek)
-
handleCommentDirective
protected Token handleCommentDirective(PeekingReader reader, ParserContext context, int pos, int line, int col) throws IOException
- Throws:
IOException
-
handleStringLiteral
protected Token handleStringLiteral(PeekingReader reader, ParserContext context, int pos, int line, int col) throws IOException
- Throws:
IOException
-
handleAlternativeStringLiteral
protected Token handleAlternativeStringLiteral(PeekingReader reader, ParserContext context, int pos, int line, int col) throws IOException
- Throws:
IOException
-
handleKeyword
protected Token handleKeyword(PeekingReader reader, ParserContext context, int pos, int line, int col, String keyword) throws IOException
- Throws:
IOException
-
-