org.jboss.dna.sequencer.ddl
Class StandardDdlParser

java.lang.Object
  extended by org.jboss.dna.sequencer.ddl.StandardDdlParser
All Implemented Interfaces:
DdlConstants, DdlConstants.StatementStartPhrases, DdlParser
Direct Known Subclasses:
Db2DdlParser, DerbyDdlParser, MySqlDdlParser, OracleDdlParser, PostgresDdlParser, SqlServerDdlParser, SybaseDdlParser

public class StandardDdlParser
extends Object
implements DdlParser, DdlConstants, DdlConstants.StatementStartPhrases

Standard SQL 92 DDL file content parser.


Nested Class Summary
 
Nested classes/interfaces inherited from interface org.jboss.dna.sequencer.ddl.DdlConstants
DdlConstants.AstNodeNames, DdlConstants.DataTypes, DdlConstants.DropBehavior, DdlConstants.MatchType, DdlConstants.Problems, DdlConstants.ReferencialAction, DdlConstants.StatementStartPhrases
 
Field Summary
 
Fields inherited from interface org.jboss.dna.sequencer.ddl.DdlConstants
ALTER, CHECK, COLUMN, COMMA, CONSTRAINT, CONSTRAINT_C, CONSTRAINT_FK, CONSTRAINT_PK, CONSTRAINT_UC, CREATE, DECLARE, DEFAULT_ID_CURRENT_USER, DEFAULT_ID_DATETIME, DEFAULT_ID_LITERAL, DEFAULT_ID_NULL, DEFAULT_ID_SESSION_USER, DEFAULT_ID_SYSTEM_USER, DEFAULT_ID_UNDEFINED, DEFAULT_ID_USER, DEFAULT_TERMINATOR, DELETE, DROP, FOREIGN, FOREIGN_KEY, GRANT, INDEX, INSERT, KEY, L_PAREN, L_SQUOTE, MISSING_TERMINATOR_NODE_LITERAL, OFF, ON, PERIOD, PRIMARY, PRIMARY_KEY, R_PAREN, REVOKE, SCHEMA, SELECT, SEMICOLON, SET, SPACE, SQL_92_RESERVED_WORDS, TABLE, UNIQUE, UPDATE, VIEW
 
Fields inherited from interface org.jboss.dna.sequencer.ddl.DdlConstants.StatementStartPhrases
SQL_92_ALL_PHRASES, STMT_ALTER_DOMAIN, STMT_ALTER_TABLE, STMT_CREATE_ASSERTION, STMT_CREATE_CHARACTER_SET, STMT_CREATE_COLLATION, STMT_CREATE_DOMAIN, STMT_CREATE_GLOBAL_TEMPORARY_TABLE, STMT_CREATE_LOCAL_TEMPORARY_TABLE, STMT_CREATE_OR_REPLACE_VIEW, STMT_CREATE_SCHEMA, STMT_CREATE_TABLE, STMT_CREATE_TRANSLATION, STMT_CREATE_VIEW, STMT_DROP_ASSERTION, STMT_DROP_CHARACTER_SET, STMT_DROP_COLLATION, STMT_DROP_DOMAIN, STMT_DROP_SCHEMA, STMT_DROP_TABLE, STMT_DROP_TRANSLATION, STMT_DROP_VIEW, STMT_GRANT, STMT_INSERT_INTO, STMT_REVOKE, STMT_SET_DEFINE, VALID_SCHEMA_CHILD_TYPES
 
Constructor Summary
StandardDdlParser()
           
 
Method Summary
 void addProblem(DdlParserProblem problem)
           
 void addProblem(DdlParserProblem problem, AstNode node)
           
 void attachNewProblem(DdlParserProblem problem, AstNode parentNode)
           
 boolean doUseTerminator()
          Returns if parser is using statement terminator or not.
 Position getCurrentMarkedPosition()
           
 DataTypeParser getDatatypeParser()
          Returns the data type parser instance.
 String getId()
           
 int getNumberOfKeyWords(DdlTokenStream tokens)
          
 List<DdlParserProblem> getProblems()
           
 AstNode getRootNode()
           
 String getStatementTypeName(String[] stmtPhrase)
           
 AstNode handleUnknownToken(DdlTokenStream tokens, String tokenValue)
          Utility method subclasses can override to check unknown tokens and perform additional node manipulation.
 boolean isMissingTerminatorNode(AstNode node)
           
 boolean isTestMode()
           
 boolean isType(String ddl)
          
 boolean isValidSchemaChild(AstNode node)
           
 void markEndOfStatement(DdlTokenStream tokens, AstNode statementNode)
          Marks the end of a statement by consuming the terminator (if exists).
 void markStartOfStatement(DdlTokenStream tokens)
          Marks the token stream with the current position to help track statement scope within the original input string.
 void mergeNodes(DdlTokenStream tokens, AstNode firstNode, AstNode secondNode)
          Merges second node into first node by re-setting expression source and length.
 AstNode missingTerminatorNode(AstNode parentNode)
          Constructs a terminator AstNode as child of root node
 AstNodeFactory nodeFactory()
          Method to access the node utility class.
 boolean parse(DdlTokenStream tokens, AstNode rootNode)
          Parses DDL content from the DdlTokenStream provided.
 boolean parse(String ddl, AstNode rootNode)
          Parses a DDL string and adds discovered child AstNodes and properties.
 void registerKeyWords(List<String> keywords)
          Adds the provided key words list to the list of registered key words.
 void registerKeyWords(String[] keywords)
          Adds the provided key words array to the list of registered key words.
 void registerStatementStartPhrase(String[] phrase)
          Adds the provided statement start phrase to the list of registered phrases.
 void registerStatementStartPhrase(String[][] phrases)
          Adds the provided statement start phrases to the list of registered phrases.
 void registerWords(DdlTokenStream tokens)
          Method provide means for DB-specific Statement implementations can contribute DDL Start Phrases and Keywords.
 boolean setAsSchemaChildNode(AstNode statementNode, boolean stmtIsMissingTerminator)
           
 void setDatatypeParser(DataTypeParser datatypeParser)
           
 void setDoUseTerminator(boolean useTerminator)
          Sets the value of the use terminator flag for the parser.
 void setRootNode(AstNode rootNode)
           
 void setTestMode(boolean testMode)
           
 AstNode unknownTerminatedNode(AstNode parentNode)
          Constructs a terminator AstNode as child of root node
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StandardDdlParser

public StandardDdlParser()
Method Detail

registerWords

public void registerWords(DdlTokenStream tokens)
Method provide means for DB-specific Statement implementations can contribute DDL Start Phrases and Keywords. These words are critical pieces of data the parser needs to segment the DDL file into statements. These statements all begin with unique start phrases like: CREATE TABLE, DROP VIEW, ALTER TABLE. The base method provided here registers the set of SQL 92 based start phrases as well as the set of SQL 92 reserved words (i.e. CREATE, DROP, SCHEMA, CONSTRAINT, etc...).

Specified by:
registerWords in interface DdlParser
Parameters:
tokens - the token stream containing the tokenized DDL content. may not be null

getDatatypeParser

public DataTypeParser getDatatypeParser()
Returns the data type parser instance.

Returns:
the DataTypeParser

setDatatypeParser

public void setDatatypeParser(DataTypeParser datatypeParser)
Parameters:
datatypeParser -

nodeFactory

public AstNodeFactory nodeFactory()
Method to access the node utility class.

Returns:
the instance of the AstNodeFactory node utility class

getRootNode

public AstNode getRootNode()
Returns:
rootNode

setRootNode

public void setRootNode(AstNode rootNode)
Parameters:
rootNode - Sets rootNode to the specified value.

getNumberOfKeyWords

public int getNumberOfKeyWords(DdlTokenStream tokens)

Specified by:
getNumberOfKeyWords in interface DdlParser
See Also:
DdlParser.getNumberOfKeyWords(org.jboss.dna.sequencer.ddl.DdlTokenStream)

parse

public boolean parse(String ddl,
                     AstNode rootNode)
              throws ParsingException
Parses a DDL string and adds discovered child AstNodes and properties. This method instantiates the tokenizer, calls a method to allow subclasses to register keywords and statement start phrases with the tokenizer and finally performs the tokenizing (i.e. tokens.start()) before calling the actual parse method.

Specified by:
parse in interface DdlParser
Parameters:
ddl - the input string to parse; may not be null
rootNode - the top level AstNode; may not be null
Returns:
true if parsing successful (i.e. no problems were discovered during parsing)
Throws:
ParsingException

parse

public boolean parse(DdlTokenStream tokens,
                     AstNode rootNode)
              throws ParsingException
Parses DDL content from the DdlTokenStream provided. Parsed data is converted to an AST via AstNodes. This tree, represents all recognizable statements and properties within a DDL file. Note that db-specific dialects will need to override many methods, as well as add methods to fully parse their specific DDL.

Specified by:
parse in interface DdlParser
Parameters:
tokens - the tokenized DdlTokenStream of the DDL input content; may not be null
rootNode - the top level AstNode; may not be null
Returns:
true if parsing successful (i.e. no problems were discovered during parsing)
Throws:
ParsingException

addProblem

public final void addProblem(DdlParserProblem problem,
                             AstNode node)

addProblem

public final void addProblem(DdlParserProblem problem)

getProblems

public final List<DdlParserProblem> getProblems()

attachNewProblem

public final void attachNewProblem(DdlParserProblem problem,
                                   AstNode parentNode)

mergeNodes

public void mergeNodes(DdlTokenStream tokens,
                       AstNode firstNode,
                       AstNode secondNode)
Merges second node into first node by re-setting expression source and length.

Parameters:
tokens - the DdlTokenStream representing the tokenized DDL content; may not be null
firstNode - the node to merge into; may not be null
secondNode - the node to merge into first node; may not be null

handleUnknownToken

public AstNode handleUnknownToken(DdlTokenStream tokens,
                                  String tokenValue)
                           throws ParsingException
Utility method subclasses can override to check unknown tokens and perform additional node manipulation. Example would be in Oracle dialect for CREATE FUNCTION statements that can end with an '/' character because statement can contain multiple statements.

Parameters:
tokens - the DdlTokenStream representing the tokenized DDL content; may not be null
tokenValue - the string value of the unknown token; never null
Returns:
the new node
Throws:
ParsingException

unknownTerminatedNode

public final AstNode unknownTerminatedNode(AstNode parentNode)
Constructs a terminator AstNode as child of root node

Parameters:
parentNode - the parent AstNode node; may not be null
Returns:
terminator node

missingTerminatorNode

public final AstNode missingTerminatorNode(AstNode parentNode)
Constructs a terminator AstNode as child of root node

Parameters:
parentNode - the parent AstNode node; may not be null
Returns:
terminator node

isMissingTerminatorNode

public final boolean isMissingTerminatorNode(AstNode node)

isValidSchemaChild

public final boolean isValidSchemaChild(AstNode node)

setAsSchemaChildNode

public final boolean setAsSchemaChildNode(AstNode statementNode,
                                          boolean stmtIsMissingTerminator)

registerKeyWords

public void registerKeyWords(String[] keywords)
Adds the provided key words array to the list of registered key words.

Parameters:
keywords -

registerKeyWords

public void registerKeyWords(List<String> keywords)
Adds the provided key words list to the list of registered key words.

Parameters:
keywords -

registerStatementStartPhrase

public void registerStatementStartPhrase(String[] phrase)
Adds the provided statement start phrase to the list of registered phrases.

Parameters:
phrase -

registerStatementStartPhrase

public void registerStatementStartPhrase(String[][] phrases)
Adds the provided statement start phrases to the list of registered phrases.

Parameters:
phrases -

doUseTerminator

public boolean doUseTerminator()
Returns if parser is using statement terminator or not.

Returns:
value of useTerminator flag.

setDoUseTerminator

public void setDoUseTerminator(boolean useTerminator)
Sets the value of the use terminator flag for the parser. If TRUE, then all statements are expected to be terminated by a terminator. The default terminator ";" can be overridden by setting the value using setTerminator() method.

Parameters:
useTerminator -

getStatementTypeName

public String getStatementTypeName(String[] stmtPhrase)

getCurrentMarkedPosition

public final Position getCurrentMarkedPosition()

markStartOfStatement

public final void markStartOfStatement(DdlTokenStream tokens)
Marks the token stream with the current position to help track statement scope within the original input string.

Parameters:
tokens - the DdlTokenStream representing the tokenized DDL content; may not be null

markEndOfStatement

public final void markEndOfStatement(DdlTokenStream tokens,
                                     AstNode statementNode)
Marks the end of a statement by consuming the terminator (if exists). If it does not exist, a missing terminator node may be added. If the resulting statement node is a valid child node type for a schema, the child node may be re-parented to the schema if the schema is still parentable. Each resulting statement node is tagged with the enclosing source expression, starting line number and column number from the file content as well as a starting character index from that same content.

Parameters:
tokens - the DdlTokenStream representing the tokenized DDL content; may not be null
statementNode -

isTestMode

public boolean isTestMode()
Returns:
testMode

setTestMode

public void setTestMode(boolean testMode)
Parameters:
testMode - Sets testMode to the specified value.

getId

public String getId()
Specified by:
getId in interface DdlParser
Returns:
parserID

isType

public boolean isType(String ddl)

Specified by:
isType in interface DdlParser
Returns:
true if tokens contain DDL type matching instance of DdlParser.
See Also:
DdlParser.isType(String)


Copyright © 2008-2010 JBoss, a division of Red Hat. All Rights Reserved.