Package io.debezium.antlr
Class AntlrDdlParser<L extends org.antlr.v4.runtime.Lexer,P extends org.antlr.v4.runtime.Parser>
- java.lang.Object
-
- io.debezium.relational.ddl.AbstractDdlParser
-
- io.debezium.antlr.AntlrDdlParser<L,P>
-
- All Implemented Interfaces:
DdlParser
public abstract class AntlrDdlParser<L extends org.antlr.v4.runtime.Lexer,P extends org.antlr.v4.runtime.Parser> extends AbstractDdlParser
Base implementation of ANTLR based parsers.This abstract class provides generic initialization of parser and its main sequence of steps that are needed to properly start parsing. It also provides implementation of helper methods for any type of ANTLR listeners.
- Author:
- Roman Kuchár
.
-
-
Field Summary
Fields Modifier and Type Field Description private AntlrDdlParserListenerantlrDdlParserListenerParser listener for tree walker.protected TablesdatabaseTablesprotected DataTypeResolverdataTypeResolverprivate booleanthrowErrorsFromTreeWalkFlag to indicate if the errors caught during tree walk will be thrown.-
Fields inherited from class io.debezium.relational.ddl.AbstractDdlParser
ddlChanges, logger, skipComments, skipViews, systemVariables
-
-
Constructor Summary
Constructors Constructor Description AntlrDdlParser(boolean throwErrorsFromTreeWalk)AntlrDdlParser(boolean throwErrorsFromTreeWalk, boolean includeViews, boolean includeComments)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract LcreateNewLexerInstance(org.antlr.v4.runtime.CharStream charStreams)Creates a new generic type instance of ANTLR Lexer.protected abstract PcreateNewParserInstance(org.antlr.v4.runtime.CommonTokenStream commonTokenStream)Creates a new generic type instance of ANTLR Parser.protected abstract AntlrDdlParserListenercreateParseTreeWalkerListener()Creates a new instance of parsed tree walker listener.TablesdatabaseTables()Returns actual tables schema.DataTypeResolverdataTypeResolver()Returns a data type resolver component.voiddebugParsed(org.antlr.v4.runtime.ParserRuleContext ctx)voiddebugSkipped(org.antlr.v4.runtime.ParserRuleContext ctx)Collection<ParsingException>getParsingExceptionsFromWalker()Returns errors catched during tree walk.static StringgetText(org.antlr.v4.runtime.ParserRuleContext ctx)Returns matched part of the getText for the context.static StringgetText(org.antlr.v4.runtime.ParserRuleContext ctx, int start, int stop)Returns matched part of the getText for the context.protected abstract DataTypeResolverinitializeDataTypeResolver()Initialize DB to JDBC data types mapping for resolver.protected abstract booleanisGrammarInUpperCase()Check if the parsed grammar is written in upper case.voidparse(String ddlContent, Tables databaseTables)protected abstract org.antlr.v4.runtime.tree.ParseTreeparseTree(P parser)Examine the supplied string containing DDL statements, and apply those statements to the specified database table definitions.voidsignalAlterDatabase(String databaseName, String previousDatabaseName, org.antlr.v4.runtime.ParserRuleContext ctx)Signal an alter database event to ddl changes listener.voidsignalAlterTable(TableId id, TableId previousId, MySqlParser.RenameTableClauseContext ctx)Signal an alter table event to ddl changes listener.voidsignalAlterTable(TableId id, TableId previousId, org.antlr.v4.runtime.ParserRuleContext ctx)Signal an alter table event to ddl changes listener.voidsignalAlterView(TableId id, TableId previousId, org.antlr.v4.runtime.ParserRuleContext ctx)Signal an alter view event to ddl changes listener.voidsignalCreateDatabase(String databaseName, org.antlr.v4.runtime.ParserRuleContext ctx)Signal a create database event to ddl changes listener.voidsignalCreateIndex(String indexName, TableId id, org.antlr.v4.runtime.ParserRuleContext ctx)Signal a create index event to ddl changes listener.voidsignalCreateTable(TableId id, org.antlr.v4.runtime.ParserRuleContext ctx)Signal a create table event to ddl changes listener.voidsignalCreateView(TableId id, org.antlr.v4.runtime.ParserRuleContext ctx)Signal a create view event to ddl changes listener.voidsignalDropDatabase(String databaseName, org.antlr.v4.runtime.ParserRuleContext ctx)Signal a drop database event to ddl changes listener.voidsignalDropIndex(String indexName, TableId id, org.antlr.v4.runtime.ParserRuleContext ctx)Signal a drop index event to ddl changes listener.voidsignalDropTable(TableId id, String statement)voidsignalDropTable(TableId id, org.antlr.v4.runtime.ParserRuleContext ctx)Signal a drop table event to ddl changes listener.voidsignalDropView(TableId id, org.antlr.v4.runtime.ParserRuleContext ctx)Signal a drop view event to ddl changes listener.voidsignalSetVariable(String variableName, String variableValue, int order, org.antlr.v4.runtime.ParserRuleContext ctx)voidsignalTruncateTable(TableId id, org.antlr.v4.runtime.ParserRuleContext ctx)Signal a truncate table event to ddl changes listener.voidsignalUseDatabase(org.antlr.v4.runtime.ParserRuleContext ctx)booleanskipComments()booleanskipViews()private voidthrowParsingException(Collection<ParsingException> errors)StringwithoutQuotes(org.antlr.v4.runtime.ParserRuleContext ctx)-
Methods inherited from class io.debezium.relational.ddl.AbstractDdlParser
accumulateParsingFailure, accumulateParsingFailure, commentParsed, createColumnFromConstant, createNewSystemVariablesInstance, currentSchema, debugParsed, debugSkipped, getDdlChanges, isQuote, isQuoted, removeLineFeeds, resolveTableId, setCurrentDatabase, setCurrentSchema, setTypeInfoForConstant, signalAlterDatabase, signalAlterTable, signalAlterView, signalChangeEvent, signalCreateDatabase, signalCreateIndex, signalCreateTable, signalCreateView, signalDropDatabase, signalDropIndex, signalDropView, signalSetVariable, signalTruncateTable, signalUseDatabase, systemVariables, terminator, withoutQuotes
-
-
-
-
Field Detail
-
throwErrorsFromTreeWalk
private final boolean throwErrorsFromTreeWalk
Flag to indicate if the errors caught during tree walk will be thrown. true = errors will be thrown false = errors will not be thrown. They will be available to get bygetParsingExceptionsFromWalker().
-
antlrDdlParserListener
private AntlrDdlParserListener antlrDdlParserListener
Parser listener for tree walker.
-
databaseTables
protected Tables databaseTables
-
dataTypeResolver
protected DataTypeResolver dataTypeResolver
-
-
Method Detail
-
getParsingExceptionsFromWalker
public Collection<ParsingException> getParsingExceptionsFromWalker()
Returns errors catched during tree walk.- Returns:
- collection of
ParsingExceptions.
-
parseTree
protected abstract org.antlr.v4.runtime.tree.ParseTree parseTree(P parser)
Examine the supplied string containing DDL statements, and apply those statements to the specified database table definitions.- Parameters:
parser- initialized ANTLR parser instance with common token stream from DDL statement; may not be null
-
createParseTreeWalkerListener
protected abstract AntlrDdlParserListener createParseTreeWalkerListener()
Creates a new instance of parsed tree walker listener.- Returns:
- new instance of parser listener.
-
createNewLexerInstance
protected abstract L createNewLexerInstance(org.antlr.v4.runtime.CharStream charStreams)
Creates a new generic type instance of ANTLR Lexer.- Parameters:
charStreams- the char stream from DDL statement, without one line comments and line feeds; may not be null- Returns:
- new instance of generic ANTLR Lexer
-
createNewParserInstance
protected abstract P createNewParserInstance(org.antlr.v4.runtime.CommonTokenStream commonTokenStream)
Creates a new generic type instance of ANTLR Parser.- Parameters:
commonTokenStream- the stream of ANTLR tokens created from Lexer instance; may not be null- Returns:
- new instance of generic ANTLR Parser
-
isGrammarInUpperCase
protected abstract boolean isGrammarInUpperCase()
Check if the parsed grammar is written in upper case.- Returns:
- true if grammar is written in upper case; false if in lower case
-
initializeDataTypeResolver
protected abstract DataTypeResolver initializeDataTypeResolver()
Initialize DB to JDBC data types mapping for resolver.- Parameters:
dataTypeResolver- data type resolver
-
databaseTables
public Tables databaseTables()
Returns actual tables schema.- Returns:
- table schema.
-
dataTypeResolver
public DataTypeResolver dataTypeResolver()
Returns a data type resolver component.- Returns:
- data type resolver.
-
getText
public static String getText(org.antlr.v4.runtime.ParserRuleContext ctx)
Returns matched part of the getText for the context.- Parameters:
ctx- the parser rule context; may not be null- Returns:
- matched part of the getText
-
getText
public static String getText(org.antlr.v4.runtime.ParserRuleContext ctx, int start, int stop)
Returns matched part of the getText for the context.- Parameters:
ctx- the parser rule context; may not be nullstart- the interval start positionstop- the interval stop position- Returns:
- matched part of the getText
-
skipViews
public boolean skipViews()
-
skipComments
public boolean skipComments()
- Overrides:
skipCommentsin classAbstractDdlParser
-
signalSetVariable
public void signalSetVariable(String variableName, String variableValue, int order, org.antlr.v4.runtime.ParserRuleContext ctx)
-
signalUseDatabase
public void signalUseDatabase(org.antlr.v4.runtime.ParserRuleContext ctx)
-
signalCreateDatabase
public void signalCreateDatabase(String databaseName, org.antlr.v4.runtime.ParserRuleContext ctx)
Signal a create database event to ddl changes listener.- Parameters:
databaseName- the database name; may not be nullctx- the start of the statement; may not be null
-
signalAlterDatabase
public void signalAlterDatabase(String databaseName, String previousDatabaseName, org.antlr.v4.runtime.ParserRuleContext ctx)
Signal an alter database event to ddl changes listener.- Parameters:
databaseName- the database name; may not be nullpreviousDatabaseName- the previous name of the database if it was renamed, or null if it was not renamedctx- the start of the statement; may not be null
-
signalDropDatabase
public void signalDropDatabase(String databaseName, org.antlr.v4.runtime.ParserRuleContext ctx)
Signal a drop database event to ddl changes listener.- Parameters:
databaseName- the database name; may not be nullctx- the start of the statement; may not be null
-
signalCreateTable
public void signalCreateTable(TableId id, org.antlr.v4.runtime.ParserRuleContext ctx)
Signal a create table event to ddl changes listener.- Parameters:
id- the table identifier; may not be nullctx- the start of the statement; may not be null
-
signalAlterTable
public void signalAlterTable(TableId id, TableId previousId, MySqlParser.RenameTableClauseContext ctx)
Signal an alter table event to ddl changes listener.- Parameters:
id- the table identifier; may not be nullpreviousId- the previous name of the view if it was renamed, or null if it was not renamedctx- the start of the statement; may not be null
-
signalAlterTable
public void signalAlterTable(TableId id, TableId previousId, org.antlr.v4.runtime.ParserRuleContext ctx)
Signal an alter table event to ddl changes listener.- Parameters:
id- the table identifier; may not be nullpreviousId- the previous name of the view if it was renamed, or null if it was not renamedctx- the start of the statement; may not be null
-
signalDropTable
public void signalDropTable(TableId id, String statement)
- Overrides:
signalDropTablein classAbstractDdlParser
-
signalDropTable
public void signalDropTable(TableId id, org.antlr.v4.runtime.ParserRuleContext ctx)
Signal a drop table event to ddl changes listener.- Parameters:
id- the table identifier; may not be nullctx- the start of the statement; may not be null
-
signalTruncateTable
public void signalTruncateTable(TableId id, org.antlr.v4.runtime.ParserRuleContext ctx)
Signal a truncate table event to ddl changes listener.- Parameters:
id- the table identifier; may not be nullctx- the start of the statement; may not be null
-
signalCreateView
public void signalCreateView(TableId id, org.antlr.v4.runtime.ParserRuleContext ctx)
Signal a create view event to ddl changes listener.- Parameters:
id- the table identifier; may not be nullctx- the start of the statement; may not be null
-
signalAlterView
public void signalAlterView(TableId id, TableId previousId, org.antlr.v4.runtime.ParserRuleContext ctx)
Signal an alter view event to ddl changes listener.- Parameters:
id- the table identifier; may not be nullpreviousId- the previous name of the view if it was renamed, or null if it was not renamedctx- the start of the statement; may not be null
-
signalDropView
public void signalDropView(TableId id, org.antlr.v4.runtime.ParserRuleContext ctx)
Signal a drop view event to ddl changes listener.- Parameters:
id- the table identifier; may not be nullctx- the start of the statement; may not be null
-
signalCreateIndex
public void signalCreateIndex(String indexName, TableId id, org.antlr.v4.runtime.ParserRuleContext ctx)
Signal a create index event to ddl changes listener.- Parameters:
indexName- the name of the index; may not be nullid- the table identifier; may be null if the index does not apply to a single tablectx- the start of the statement; may not be null
-
signalDropIndex
public void signalDropIndex(String indexName, TableId id, org.antlr.v4.runtime.ParserRuleContext ctx)
Signal a drop index event to ddl changes listener.- Parameters:
indexName- the name of the index; may not be nullid- the table identifier; may not be nullctx- the start of the statement; may not be null
-
debugParsed
public void debugParsed(org.antlr.v4.runtime.ParserRuleContext ctx)
-
debugSkipped
public void debugSkipped(org.antlr.v4.runtime.ParserRuleContext ctx)
-
withoutQuotes
public String withoutQuotes(org.antlr.v4.runtime.ParserRuleContext ctx)
-
throwParsingException
private void throwParsingException(Collection<ParsingException> errors)
-
-