@NotThreadSafe public class DdlParser extends Object
| Modifier and Type | Class and Description |
|---|---|
protected static interface |
DdlParser.TokenSet |
| Modifier and Type | Field and Description |
|---|---|
private String |
currentSchema |
protected Tables |
databaseTables |
protected DataTypeParser |
dataTypeParser |
private Set<String> |
keywords |
private List<DdlParserListener> |
listeners |
protected org.slf4j.Logger |
logger |
protected boolean |
skipViews |
private Set<String> |
statementStarts |
private String |
terminator |
protected TokenStream |
tokens |
| Constructor and Description |
|---|
DdlParser(String terminator)
Create a new parser that uses the supplied
DataTypeParser, but that does not include view definitions. |
DdlParser(String terminator,
boolean includeViews)
Create a new parser that uses the supplied
DataTypeParser. |
| Modifier and Type | Method and Description |
|---|---|
void |
addListener(DdlParserListener listener)
Add a listener.
|
protected boolean |
canConsumeJoin(TokenStream.Marker start) |
protected boolean |
canConsumeJoinCondition(TokenStream.Marker start) |
protected String |
consumeDoubleQuotedString()
Consume the next token that is a double-quoted string.
|
protected String |
consumeQuotedString()
Consume the next token that is either a single-quoted string or a double-quoted string.
|
protected void |
consumeRemainingStatement(TokenStream.Marker start)
Consume all tokens from the current position until and including either the
end-of-statement
terminator token or one of
the tokens that is registered as the start of a statement. |
protected String |
consumeSingleQuotedString()
Consume the next token that is a single-quoted string.
|
protected void |
consumeStatement()
Consume all tokens from the current position that is a
starting-statement
token until either the
end-of-statement terminator token or before the next
starting-statement token. |
protected Column |
createColumnFromConstant(String columnName,
String constantValue) |
String |
currentSchema()
Get the name of the current schema.
|
protected void |
debugParsed(TokenStream.Marker statementStart) |
protected void |
debugSkipped(TokenStream.Marker statementStart) |
protected int |
determineTokenType(int type,
String token) |
protected String |
determineTypeNameForConstant(BigDecimal value) |
protected String |
determineTypeNameForConstant(double value) |
protected String |
determineTypeNameForConstant(float value) |
protected String |
determineTypeNameForConstant(long value) |
protected void |
initializeDataTypes(DataTypeParser dataTypeParser) |
protected void |
initializeKeywords(DdlParser.TokenSet keywords) |
protected void |
initializeStatementStarts(DdlParser.TokenSet statementStartTokens) |
void |
parse(String ddlContent,
Tables databaseTables)
Examine the supplied string containing DDL statements, and apply those statements to the specified
database table definitions.
|
void |
parse(TokenStream ddlContent,
Tables databaseTables)
Examine the stream starting at its current position for DDL statements, and apply those statements to the specified
database table definitions.
|
private void |
parseAliasedTableInFrom(TokenStream.Marker start,
Map<String,Table> tablesByAlias)
Parse a potentially qualified table name along with an optional alias.
|
protected void |
parseAlter(TokenStream.Marker marker)
Parse the a DDL "ALTER" statement.
|
protected String |
parseCharacterLiteral(TokenStream.Marker start) |
protected String |
parseCharacterSetName(TokenStream.Marker start) |
protected void |
parseColumnName(TokenStream.Marker start,
Map<String,String> tableAliasByColumnAliases,
Map<String,String> columnNameByAliases)
Parse the potentially qualified and aliased column information, and add the information to the supplied maps.
|
protected Map<String,Column> |
parseColumnsInSelectClause(TokenStream.Marker start)
Parse the column information in the SELECT clause.
|
protected void |
parseComment(TokenStream.Marker marker)
Parse the a DDL line comment.
|
protected void |
parseCreate(TokenStream.Marker marker)
Parse the a DDL "CREATE" statement.
|
protected String |
parseDateLiteral(TokenStream.Marker start) |
protected void |
parseDrop(TokenStream.Marker marker)
Parse the a DDL "DROP" statement.
|
protected Object |
parseLiteral(TokenStream.Marker start) |
protected void |
parseNextStatement(TokenStream.Marker marker)
Parse the next DDL statement.
|
protected Object |
parseNumericLiteral(TokenStream.Marker start,
boolean signed) |
protected TableId |
parseQualifiedTableName(TokenStream.Marker start)
Parse the next tokens for a possibly qualified table name.
|
protected List<TableId> |
parseQualifiedTableNames(TokenStream.Marker start)
Parse the next tokens for one or more comma-separated qualified table names.
|
protected String |
parseSchemaQualifiedName(TokenStream.Marker start)
Parse the next tokens for a possibly qualified name.
|
protected Map<String,Table> |
parseSelectFromClause(TokenStream.Marker start)
Returns the tables keyed by their aliases that appear in a SELECT clause's "FROM" list.
|
protected String |
parseTimeLiteral(TokenStream.Marker start) |
protected String |
parseTimestampLiteral(TokenStream.Marker start) |
protected void |
parseUnknownStatement(TokenStream.Marker marker)
Parse a DDL statement that is not known by the
parseNextStatement(io.debezium.text.TokenStream.Marker). |
protected void |
parsingFailed(Position position,
Collection<ParsingException> errors,
String msg)
Generate a
ParsingException or MultipleParsingExceptions with the supplied error or errors and the
supplied message, which is appended by this method with additional information about the position's line and column. |
protected void |
parsingFailed(Position position,
String msg)
Generate a
ParsingException with the supplied message, which is appended by this method with additional
information about the position's line and column. |
protected void |
parsingFailed(Position position,
String msg,
Throwable t)
Generate a
ParsingException with the supplied message, which is appended by this method with additional
information about the position's line and column. |
private String |
removeLineFeeds(String input) |
boolean |
removeListener(DdlParserListener listener)
Remove an existing listener.
|
void |
removeListeners()
Remove all existing listeners.
|
protected TableId |
resolveTableId(String schemaName,
String tableName)
Create a
TableId from the supplied schema and table names. |
void |
setCurrentSchema(String name)
|
protected void |
setTypeInfoForConstant(String constantValue,
ColumnEditor column) |
protected void |
signalAlterDatabase(String databaseName,
String previousDatabaseName,
TokenStream.Marker statementStart)
Signal an alter database event to all listeners.
|
protected void |
signalAlterTable(TableId id,
TableId previousId,
String statement)
Signal an alter table event to all listeners.
|
protected void |
signalAlterTable(TableId id,
TableId previousId,
TokenStream.Marker statementStart)
Signal an alter table event to all listeners.
|
protected void |
signalAlterView(TableId id,
TableId previousId,
TokenStream.Marker statementStart)
Signal an alter view event to all listeners.
|
protected void |
signalCreateDatabase(String databaseName,
TokenStream.Marker statementStart)
Signal a create database event to all listeners.
|
protected void |
signalCreateIndex(String indexName,
TableId id,
TokenStream.Marker statementStart)
Signal a create index event to all listeners.
|
protected void |
signalCreateTable(TableId id,
TokenStream.Marker statementStart)
Signal a create table event to all listeners.
|
protected void |
signalCreateView(TableId id,
TokenStream.Marker statementStart)
Signal a create view event to all listeners.
|
protected void |
signalDropDatabase(String databaseName,
TokenStream.Marker statementStart)
Signal a drop database event to all listeners.
|
protected void |
signalDropIndex(String indexName,
TableId id,
TokenStream.Marker statementStart)
Signal a drop index event to all listeners.
|
protected void |
signalDropTable(TableId id,
String statement)
Signal a drop table event to all listeners.
|
protected void |
signalDropTable(TableId id,
TokenStream.Marker statementStart)
Signal a drop table event to all listeners.
|
protected void |
signalDropView(TableId id,
String statement)
Signal a drop view event to all listeners.
|
protected void |
signalDropView(TableId id,
TokenStream.Marker statementStart)
Signal a drop view event to all listeners.
|
protected void |
signalEvent(DdlParserListener.Event event)
Signal an event to all listeners.
|
protected boolean |
skipComments()
Determine whether parsing should exclude comments from the token stream.
|
protected String |
statement(TokenStream.Marker statementStart) |
String |
terminator()
The token used to terminate a DDL statement.
|
private final String terminator
private String currentSchema
protected final boolean skipViews
protected final org.slf4j.Logger logger
protected final DataTypeParser dataTypeParser
protected Tables databaseTables
protected TokenStream tokens
private final List<DdlParserListener> listeners
public DdlParser(String terminator)
DataTypeParser, but that does not include view definitions.terminator - the terminator character sequence; may be null if the default terminator (;) should be usedpublic DdlParser(String terminator, boolean includeViews)
DataTypeParser.terminator - the terminator character sequence; may be null if the default terminator (;) should be usedincludeViews - true if view definitions should be included, or false if they should be skippedpublic void addListener(DdlParserListener listener)
listener - the listener; if null nothing is donepublic boolean removeListener(DdlParserListener listener)
listener - the listener; if null nothing is donetrue if the listener was removed, or false otherwisepublic void removeListeners()
protected void initializeDataTypes(DataTypeParser dataTypeParser)
protected void initializeKeywords(DdlParser.TokenSet keywords)
protected void initializeStatementStarts(DdlParser.TokenSet statementStartTokens)
public final String terminator()
protected int determineTokenType(int type,
String token)
public void setCurrentSchema(String name)
name - the name of the current schema; may be nullpublic String currentSchema()
setprotected String parseSchemaQualifiedName(TokenStream.Marker start)
current schema), a '.' delimiter, and the object name. If no schema name is
found, just the object name is returned.start - the start of the statementprotected TableId parseQualifiedTableName(TokenStream.Marker start)
currentSchema(), and then calls resolveTableId(String, String) with
the values.start - the start of the statementTableIdprotected List<TableId> parseQualifiedTableNames(TokenStream.Marker start)
currentSchema(), and then calls resolveTableId(String, String) with
the values.start - the start of the statementTableIdprotected TableId resolveTableId(String schemaName, String tableName)
TableId from the supplied schema and table names. By default, this method uses the supplied schema name
as the TableId's catalog, which often matches the catalog name in JDBC database metadata.schemaName - the name of the schema; may be null if not specifiedtableName - the name of the table; should not be nullprotected boolean skipComments()
true.true if comments should be skipped/excluded, or false if they should not be skippedpublic final void parse(String ddlContent, Tables databaseTables)
ddlContent - the stream of tokens containing the DDL statements; may not be nulldatabaseTables - the database's table definitions, which should be used by this method to create, change, or remove
tables as defined in the DDL content; may not be nullParsingException - if there is a problem parsing the supplied contentpublic final void parse(TokenStream ddlContent, Tables databaseTables) throws ParsingException, IllegalStateException
ddlContent - the stream of tokens containing the DDL statements; may not be nulldatabaseTables - the database's table definitions, which should be used by this method to create, change, or remove
tables as defined in the DDL content; may not be nullParsingException - if there is a problem parsing the supplied contentIllegalStateException - if the supplied token stream is in an invalid stateprotected void parseNextStatement(TokenStream.Marker marker)
marker - the start of the statement; never nullParsingException - if there is an error parsing the statementprotected void parseComment(TokenStream.Marker marker)
parseNextStatement(io.debezium.text.TokenStream.Marker) for line comments that appear
between other DDL statements, and is not typically called for comments that appear within DDL statements.marker - the start of the statement; never nullParsingException - if there is an error parsing the statementprotected void parseCreate(TokenStream.Marker marker)
By default this method simply consumes the complete statement.
marker - the start of the statement; never nullParsingException - if there is an error parsing the statementprotected void parseAlter(TokenStream.Marker marker)
By default this method simply consumes the complete statement.
marker - the start of the statement; never nullParsingException - if there is an error parsing the statementprotected void parseDrop(TokenStream.Marker marker)
By default this method simply consumes the complete statement.
marker - the start of the statement; never nullParsingException - if there is an error parsing the statementprotected void parseUnknownStatement(TokenStream.Marker marker)
parseNextStatement(io.debezium.text.TokenStream.Marker). This method can be overridden by subclasses,
although it will be more common for subclasses to override parseNextStatement(io.debezium.text.TokenStream.Marker).
By default this method simply consumes the complete statement.
marker - the start of the statement; never nullParsingException - if there is an error parsing the statementprotected void signalEvent(DdlParserListener.Event event)
event - the event; may not be nullprotected void signalCreateDatabase(String databaseName, TokenStream.Marker statementStart)
databaseName - the database name; may not be nullstatementStart - the start of the statement; may not be nullprotected void signalAlterDatabase(String databaseName, String previousDatabaseName, TokenStream.Marker statementStart)
databaseName - the database name; may not be nullpreviousDatabaseName - the previous name of the database if it was renamed, or null if it was not renamedstatementStart - the start of the statement; may not be nullprotected void signalDropDatabase(String databaseName, TokenStream.Marker statementStart)
databaseName - the database name; may not be nullstatementStart - the start of the statement; may not be nullprotected void signalCreateTable(TableId id, TokenStream.Marker statementStart)
id - the table identifier; may not be nullstatementStart - the start of the statement; may not be nullprotected void signalAlterTable(TableId id, TableId previousId, TokenStream.Marker statementStart)
id - the table identifier; may not be nullpreviousId - the previous name of the view if it was renamed, or null if it was not renamedstatementStart - the start of the statement; may not be nullprotected void signalAlterTable(TableId id, TableId previousId, String statement)
id - the table identifier; may not be nullpreviousId - the previous name of the view if it was renamed, or null if it was not renamedstatement - the DDL statement; may not be nullprotected void signalDropTable(TableId id, TokenStream.Marker statementStart)
id - the table identifier; may not be nullstatementStart - the start of the statement; may not be nullprotected void signalDropTable(TableId id, String statement)
id - the table identifier; may not be nullstatement - the statement; may not be nullprotected void signalCreateView(TableId id, TokenStream.Marker statementStart)
id - the table identifier; may not be nullstatementStart - the start of the statement; may not be nullprotected void signalAlterView(TableId id, TableId previousId, TokenStream.Marker statementStart)
id - the table identifier; may not be nullpreviousId - the previous name of the view if it was renamed, or null if it was not renamedstatementStart - the start of the statement; may not be nullprotected void signalDropView(TableId id, TokenStream.Marker statementStart)
id - the table identifier; may not be nullstatementStart - the start of the statement; may not be nullprotected void signalDropView(TableId id, String statement)
id - the table identifier; may not be nullstatement - the statement; may not be nullprotected void signalCreateIndex(String indexName, TableId id, TokenStream.Marker statementStart)
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 tablestatementStart - the start of the statement; may not be nullprotected void signalDropIndex(String indexName, TableId id, TokenStream.Marker statementStart)
indexName - the name of the index; may not be nullid - the table identifier; may not be nullstatementStart - the start of the statement; may not be nullprotected void debugParsed(TokenStream.Marker statementStart)
protected void debugSkipped(TokenStream.Marker statementStart)
protected String statement(TokenStream.Marker statementStart)
protected void consumeStatement()
throws ParsingException
starting-statement
token until either the
end-of-statement terminator token or before the next
starting-statement token.ParsingException - if the next token is not a starting-statement tokenprotected void consumeRemainingStatement(TokenStream.Marker start)
end-of-statement
terminator token or one of
the tokens that is registered as the start of a statement.start - the marker at which the statement was begunprotected String consumeSingleQuotedString()
ParsingException - if there is no single-quoted string at the current positionprotected String consumeDoubleQuotedString()
ParsingException - if there is no double-quoted string at the current positionprotected String consumeQuotedString()
ParsingException - if there is no single- or double-quoted string at the current positionprotected void parsingFailed(Position position, String msg)
ParsingException with the supplied message, which is appended by this method with additional
information about the position's line and column.position - the position at which the error occurred; may not be nullmsg - the leading portion of the message; may not be nullprotected void parsingFailed(Position position, String msg, Throwable t)
ParsingException with the supplied message, which is appended by this method with additional
information about the position's line and column.position - the position at which the error occurred; may not be nullmsg - the leading portion of the message; may not be nullt - the exception that occurred; may be nullprotected void parsingFailed(Position position, Collection<ParsingException> errors, String msg)
ParsingException or MultipleParsingExceptions with the supplied error or errors and the
supplied message, which is appended by this method with additional information about the position's line and column.position - the position at which the error occurred; may not be nullerrors - the multiple parsing exception errors; may not be nullmsg - the leading portion of the message; may not be nullprotected Object parseLiteral(TokenStream.Marker start)
protected Object parseNumericLiteral(TokenStream.Marker start, boolean signed)
protected String parseCharacterLiteral(TokenStream.Marker start)
protected String parseCharacterSetName(TokenStream.Marker start)
protected String parseDateLiteral(TokenStream.Marker start)
protected String parseTimeLiteral(TokenStream.Marker start)
protected String parseTimestampLiteral(TokenStream.Marker start)
protected Map<String,Column> parseColumnsInSelectClause(TokenStream.Marker start)
start - the start of the statementprotected Column createColumnFromConstant(String columnName, String constantValue)
protected void setTypeInfoForConstant(String constantValue, ColumnEditor column)
protected String determineTypeNameForConstant(long value)
protected String determineTypeNameForConstant(float value)
protected String determineTypeNameForConstant(double value)
protected String determineTypeNameForConstant(BigDecimal value)
protected void parseColumnName(TokenStream.Marker start, Map<String,String> tableAliasByColumnAliases, Map<String,String> columnNameByAliases)
start - the start of the statementtableAliasByColumnAliases - the map to which is added the column's alias (or name) keyed by the alias of the table
in which the column should appear; may not be nullcolumnNameByAliases - the map to which is added the column's name keyed by the its alias (or itself if there is no
alias); may not be nullprotected Map<String,Table> parseSelectFromClause(TokenStream.Marker start)
various standard joins.start - the start of the statementprotected boolean canConsumeJoin(TokenStream.Marker start)
protected boolean canConsumeJoinCondition(TokenStream.Marker start)
private void parseAliasedTableInFrom(TokenStream.Marker start, Map<String,Table> tablesByAlias)
start - the start of the statementtablesByAlias - the map to which this method should add the table keyed by its alias (or name if there is no alias);
may not be nullCopyright © 2016 JBoss by Red Hat. All rights reserved.