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 <kucharrom@gmail.com>.
  • Field Details

    • 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 by getParsingExceptionsFromWalker().
    • antlrDdlParserListener

      private AntlrDdlParserListener antlrDdlParserListener
      Parser listener for tree walker.
    • databaseTables

      protected Tables databaseTables
    • dataTypeResolver

      protected DataTypeResolver dataTypeResolver
  • Constructor Details

    • AntlrDdlParser

      public AntlrDdlParser(boolean throwErrorsFromTreeWalk)
    • AntlrDdlParser

      public AntlrDdlParser(boolean throwErrorsFromTreeWalk, boolean includeViews, boolean includeComments)
  • Method Details

    • parse

      public void parse(String ddlContent, Tables databaseTables)
    • 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 null
      start - the interval start position
      stop - the interval stop position
      Returns:
      matched part of the getText
    • skipViews

      public boolean skipViews()
    • skipComments

      public boolean skipComments()
      Overrides:
      skipComments in class AbstractDdlParser
    • 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 null
      ctx - 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 null
      previousDatabaseName - the previous name of the database if it was renamed, or null if it was not renamed
      ctx - 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 null
      ctx - 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 null
      ctx - 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 null
      previousId - the previous name of the view if it was renamed, or null if it was not renamed
      ctx - 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 null
      previousId - the previous name of the view if it was renamed, or null if it was not renamed
      ctx - the start of the statement; may not be null
    • signalDropTable

      public void signalDropTable(TableId id, String statement)
      Overrides:
      signalDropTable in class AbstractDdlParser
    • 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 null
      ctx - 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 null
      ctx - 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 null
      ctx - 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 null
      previousId - the previous name of the view if it was renamed, or null if it was not renamed
      ctx - 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 null
      ctx - 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 null
      id - the table identifier; may be null if the index does not apply to a single table
      ctx - 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 null
      id - the table identifier; may not be null
      ctx - 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)