org.modeshape.sequencer.ddl
Class StandardDdlParser

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

@NotThreadSafe
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.modeshape.sequencer.ddl.DdlConstants
DdlConstants.AstNodeNames, DdlConstants.DataTypes, DdlConstants.DropBehavior, DdlConstants.MatchType, DdlConstants.Problems, DdlConstants.ReferencialAction, DdlConstants.StatementStartPhrases
 
Field Summary
 
Fields inherited from interface org.modeshape.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, GIGA, GRANT, INDEX, INSERT, KEY, KILO, L_PAREN, L_SQUOTE, MEGA, 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.modeshape.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)
           
protected  boolean areNextTokensCreateTableOptions(DdlTokenStream tokens)
           
 void attachNewProblem(DdlParserProblem problem, AstNode parentNode)
           
protected  void computeScore(DdlTokenStream tokens, DdlParserScorer scorer)
           
protected  boolean consumeComment(DdlTokenStream tokens)
          Consumes an an end-of-line comment or in-line comment
protected  String consumeIdentifier(DdlTokenStream tokens)
          Consumes an token identifier which can be of the form of a simple string or a double-quoted string.
protected  String consumeParenBoundedTokens(DdlTokenStream tokens, boolean includeParens)
          Simple method which parses, consumes and returns a string representing text found between parenthesis (i.e. '()') If parents don't exist, method returns NULL;
 boolean doUseTerminator()
          Returns if parser is using statement terminator or not.
 boolean equals(Object obj)
          
 Position getCurrentMarkedPosition()
           
protected  List<String> getCustomDataTypeStartWords()
          Returns a list of custom data type start words which can be used to help identify a column definition sub-statement.
 DataTypeParser getDatatypeParser()
          Returns the data type parser instance.
protected  List<String> getDataTypeStartWords()
          Returns a list of data type start words which can be used to help identify a column definition sub-statement.
 String getId()
          Get the identifier for this parser.
 String[] getIdentifyingKeywords()
           
 List<DdlParserProblem> getProblems()
           
 AstNode getRootNode()
           
 String getStatementTypeName(String[] stmtPhrase)
           
protected  String getTableElementsString(DdlTokenStream tokens, boolean useTerminator)
          Method which extracts the table element string from a CREATE TABLE statement.
protected  String getTerminator()
          Returns current terminator
protected  Name[] getValidSchemaChildTypes()
           
 AstNode handleUnknownToken(DdlTokenStream tokens, String tokenValue)
          Utility method subclasses can override to check unknown tokens and perform additional node manipulation.
 int hashCode()
          
protected  void initializeTokenStream(DdlTokenStream tokens)
          Method called by score(String, String, DdlParserScorer) and parse(String, AstNode, Object) to initialize the token stream, giving subclasses a chance to register key words and statement start phrases.
protected  int integer(String value)
          Returns the integer value of the input string.
protected  boolean isColumnDefinitionStart(DdlTokenStream tokens)
          This utility method provides this parser the ability to distinguish between a CreateTable Constrain and a ColumnDefinition Definition which are the only two statement segment types allowed within the CREATE TABLE parenthesis ( xxxxxx );
protected  boolean isComment(DdlTokenStream tokens)
          Checks if next token is of type comment.
 boolean isMissingTerminatorNode(AstNode node)
           
protected  boolean isTableConstraint(DdlTokenStream tokens)
          This utility method provides this parser the ability to distinguish between a CreateTable Constraint and a ColumnDefinition Definition which are the only two statement segment types allowed within the CREATE TABLE parenthesis ( xxxxxx );
protected  boolean isTerminator(DdlTokenStream tokens)
          Utility method to determine if next token is a terminator.
 boolean isTestMode()
           
 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.
 void parse(String ddl, AstNode rootNode, Object scoreReturnObject)
          Parses a DDL string, adding child AstNodes and properties to the supplied root.
protected  AstNode parseAlterStatement(DdlTokenStream tokens, AstNode parentNode)
          Parses DDL ALTER statement based on SQL 92 specifications.
protected  AstNode parseAlterTableStatement(DdlTokenStream tokens, AstNode parentNode)
          Parses DDL ALTER TABLE AstNode based on SQL 92 specifications.
protected  boolean parseCollateClause(DdlTokenStream tokens, AstNode columnNode)
          Parses the default clause for a column and sets appropriate properties on the column node.
protected  boolean parseColumnConstraint(DdlTokenStream tokens, AstNode columnNode, boolean isAlterTable)
          Parses an in-line column constraint including NULLABLE value, UNIQUE, PRIMARY KEY and REFERENCES to a Foreign Key.
protected  void parseColumnDefinition(DdlTokenStream tokens, AstNode tableNode, boolean isAlterTable)
          Utility method to parse the actual column definition.
protected  List<String> parseColumnNameList(DdlTokenStream tokens)
          Parses a comma separated list of column names.
protected  boolean parseColumnNameList(DdlTokenStream tokens, AstNode parentNode, Name referenceType)
          Adds column reference nodes to a parent node.
protected  void parseColumnsAndConstraints(DdlTokenStream tokens, AstNode tableNode)
          Utility method to parse columns and table constraints within either a CREATE TABLE statement.
protected  void parseConstraintAttributes(DdlTokenStream tokens, AstNode constraintNode)
          Parses the attributes associated with any in-line column constraint definition or a table constrain definition.
protected  AstNode parseCreateAssertionStatement(DdlTokenStream tokens, AstNode parentNode)
          Parses DDL CREATE ASSERTION AstNode based on SQL 92 specifications.
protected  AstNode parseCreateCharacterSetStatement(DdlTokenStream tokens, AstNode parentNode)
          Parses DDL CREATE CHARACTER SET AstNode based on SQL 92 specifications.
protected  AstNode parseCreateCollationStatement(DdlTokenStream tokens, AstNode parentNode)
          Parses DDL CREATE COLLATION AstNode based on SQL 92 specifications.
protected  AstNode parseCreateDomainStatement(DdlTokenStream tokens, AstNode parentNode)
          Parses DDL CREATE DOMAIN AstNode based on SQL 92 specifications.
protected  AstNode parseCreateSchemaStatement(DdlTokenStream tokens, AstNode parentNode)
          Parses DDL CREATE SCHEMA AstNode based on SQL 92 specifications.
protected  AstNode parseCreateStatement(DdlTokenStream tokens, AstNode parentNode)
          Parses DDL CREATE statement based on SQL 92 specifications.
protected  void parseCreateTableOptions(DdlTokenStream tokens, AstNode tableNode)
           
protected  AstNode parseCreateTableStatement(DdlTokenStream tokens, AstNode parentNode)
          Parses DDL CREATE TABLE AstNode based on SQL 92 specifications.
protected  AstNode parseCreateTranslationStatement(DdlTokenStream tokens, AstNode parentNode)
          Parses DDL CREATE TRANSLATION AstNode based on SQL 92 specifications.
protected  AstNode parseCreateViewStatement(DdlTokenStream tokens, AstNode parentNode)
          Parses DDL CREATE VIEW AstNode basedregisterStatementStartPhrase on SQL 92 specifications.
protected  AstNode parseCustomStatement(DdlTokenStream tokens, AstNode parentNode)
          Catch-all method to parse unknown (not registered or handled by sub-classes) statements.
protected  boolean parseDefaultClause(DdlTokenStream tokens, AstNode columnNode)
          Parses the default clause for a column and sets appropriate properties on the column node.
protected  AstNode parseDropStatement(DdlTokenStream tokens, AstNode parentNode)
          Parses DDL DROP AstNode based on SQL 92 specifications.
protected  void parseGrantPrivileges(DdlTokenStream tokens, List<AstNode> privileges)
           
protected  AstNode parseGrantStatement(DdlTokenStream tokens, AstNode parentNode)
          Parses DDL GRANT statement AstNode based on SQL 92 specifications.
protected  AstNode parseIgnorableStatement(DdlTokenStream tokens, String name, AstNode parentNode)
          Utility method to parse a statement that can be ignored.
protected  AstNode parseIgnorableStatement(DdlTokenStream tokens, String name, AstNode parentNode, Name mixinType)
          Utility method to parse a statement that can be ignored.
protected  AstNode parseInsertStatement(DdlTokenStream tokens, AstNode parentNode)
          Parses DDL INSERT AstNode based on SQL 92 specifications.
protected  String parseName(DdlTokenStream tokens)
          Method to parse fully qualified schema, table and column names that are defined with '.' separator and optionally bracketed with square brackets Example: partsSchema.supplier Example: [partsSchema].
protected  void parseNextCreateTableOption(DdlTokenStream tokens, AstNode tableNode)
           
protected  AstNode parseNextStatement(DdlTokenStream tokens, AstNode node)
          Performs token match checks for initial statement type and delegates to specific parser methods.
protected  void parseReferences(DdlTokenStream tokens, AstNode constraintNode)
           
protected  AstNode parseRevokeStatement(DdlTokenStream tokens, AstNode parentNode)
           
protected  AstNode parseSetStatement(DdlTokenStream tokens, AstNode parentNode)
          Parses DDL SET AstNode based on SQL 92 specifications.
protected  void parseSingleTerminatedColumnDefinition(DdlTokenStream tokens, AstNode tableNode, boolean isAlterTable)
          Utility method to parse the actual column definition.
protected  AstNode parseStatement(DdlTokenStream tokens, String[] stmt_start_phrase, AstNode parentNode, Name mixinType)
          Utility method to parse a generic statement given a start phrase and statement mixin type.
protected  void parseTableConstraint(DdlTokenStream tokens, AstNode tableNode, boolean isAlterTable)
          Parses full table constraint definition including the "CONSTRAINT" token Examples: CONSTRAINT P_KEY_2a UNIQUE (PERMISSIONUID)
protected  String parseUntilCommaOrTerminator(DdlTokenStream tokens)
           
protected  String parseUntilSemiColon(DdlTokenStream tokens)
          Utility method which parses tokens until a semicolon is found or there are no more tokens.
protected  String parseUntilTerminator(DdlTokenStream tokens)
          Utility method which parses tokens until a terminator is found, another statement is identified or there are no more tokens.
protected  String parseUntilTerminatorIgnoreEmbeddedStatements(DdlTokenStream tokens)
          Utility method which parses tokens until a terminator is found or there are no more tokens.
protected  void removeMissingTerminatorNodes(AstNode parentNode)
           
protected  void rewrite(DdlTokenStream tokens, AstNode rootNode)
           
 Object score(String ddl, String fileName, DdlParserScorer scorer)
          Determine this parser's score for the given DDL string.
 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)
           
protected  boolean setTerminator(String terminator)
           
 void setTestMode(boolean testMode)
           
protected  void testPrint(String str)
           
 AstNode unknownTerminatedNode(AstNode parentNode)
          Constructs a terminator AstNode as child of root node
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StandardDdlParser

public StandardDdlParser()
Method Detail

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.

score

public Object score(String ddl,
                    String fileName,
                    DdlParserScorer scorer)
             throws ParsingException
Determine this parser's score for the given DDL string. This method is called to determine how well this parser handles the supplied DDL, and is often called before the DdlParser.parse(String, AstNode, Object) method.

Specified by:
score in interface DdlParser
Parameters:
ddl - the input string to parse; may not be null
fileName - the name of the DDL content, which may be used to improve the score; may be null if not known
scorer - the scorer that should be used to record the score; may not be null
Returns:
an object that will be passed to the DdlParser.parse(String, AstNode,Object) method
Throws:
ParsingException - if there is an error parsing the supplied DDL content
See Also:
DdlParser.score(java.lang.String, java.lang.String, org.modeshape.sequencer.ddl.DdlParserScorer)

computeScore

protected void computeScore(DdlTokenStream tokens,
                            DdlParserScorer scorer)

getIdentifyingKeywords

public String[] getIdentifyingKeywords()

parse

public void parse(String ddl,
                  AstNode rootNode,
                  Object scoreReturnObject)
           throws ParsingException
Parses a DDL string, adding child AstNodes and properties to the supplied root. 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
scoreReturnObject - the object returned from DdlParser.score(String, String, DdlParserScorer) for the same DDL content; may be null if the DdlParser.score(String, String, DdlParserScorer) method was not called
Throws:
ParsingException - if there is an error parsing the supplied DDL content
See Also:
DdlParser.parse(java.lang.String, org.modeshape.sequencer.ddl.node.AstNode, java.lang.Object)

initializeTokenStream

protected void initializeTokenStream(DdlTokenStream tokens)
Method called by score(String, String, DdlParserScorer) and parse(String, AstNode, Object) to initialize the token stream, giving subclasses a chance to register key words and statement start phrases.

Parameters:
tokens - the stream of tokens

parseNextStatement

protected AstNode parseNextStatement(DdlTokenStream tokens,
                                     AstNode node)
Performs token match checks for initial statement type and delegates to specific parser methods. If no specific statement is found, then a call is made to parse a custom statement type. Subclasses may override this method, but the StandardDdlParser.parseCustomStatement() method is designed to allow for parsing db-specific statement types.

Parameters:
tokens - the tokenized DdlTokenStream of the DDL input content; may not be null
node - the top level AstNode; may not be null
Returns:
node the new statement node

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)

rewrite

protected void rewrite(DdlTokenStream tokens,
                       AstNode rootNode)

removeMissingTerminatorNodes

protected void removeMissingTerminatorNodes(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

parseCreateStatement

protected AstNode parseCreateStatement(DdlTokenStream tokens,
                                       AstNode parentNode)
                                throws ParsingException
Parses DDL CREATE statement based on SQL 92 specifications.

Parameters:
tokens - the DdlTokenStream representing the tokenized DDL content; may not be null
parentNode - the parent AstNode node; may not be null
Returns:
the parsed CREATE AstNode
Throws:
ParsingException

parseAlterStatement

protected AstNode parseAlterStatement(DdlTokenStream tokens,
                                      AstNode parentNode)
                               throws ParsingException
Parses DDL ALTER statement based on SQL 92 specifications.

Parameters:
tokens - the DdlTokenStream representing the tokenized DDL content; may not be null
parentNode - the parent AstNode node; may not be null
Returns:
the parsed ALTER AstNode
Throws:
ParsingException

parseAlterTableStatement

protected AstNode parseAlterTableStatement(DdlTokenStream tokens,
                                           AstNode parentNode)
                                    throws ParsingException
Parses DDL ALTER TABLE AstNode based on SQL 92 specifications.

Parameters:
tokens - the DdlTokenStream representing the tokenized DDL content; may not be null
parentNode - the parent AstNode node; may not be null
Returns:
the parsed ALTER TABLE AstNode
Throws:
ParsingException

parseDropStatement

protected AstNode parseDropStatement(DdlTokenStream tokens,
                                     AstNode parentNode)
                              throws ParsingException
Parses DDL DROP AstNode based on SQL 92 specifications.

Parameters:
tokens - the DdlTokenStream representing the tokenized DDL content; may not be null
parentNode - the parent AstNode node; may not be null
Returns:
the parsed DROP AstNode
Throws:
ParsingException

parseInsertStatement

protected AstNode parseInsertStatement(DdlTokenStream tokens,
                                       AstNode parentNode)
                                throws ParsingException
Parses DDL INSERT AstNode based on SQL 92 specifications.

Parameters:
tokens - the DdlTokenStream representing the tokenized DDL content; may not be null
parentNode - the parent AstNode node; may not be null
Returns:
the AstNode
Throws:
ParsingException

parseSetStatement

protected AstNode parseSetStatement(DdlTokenStream tokens,
                                    AstNode parentNode)
                             throws ParsingException
Parses DDL SET AstNode based on SQL 92 specifications.

Parameters:
tokens - the DdlTokenStream representing the tokenized DDL content; may not be null
parentNode - the parent AstNode node; may not be null
Returns:
the AstNode
Throws:
ParsingException

parseGrantStatement

protected AstNode parseGrantStatement(DdlTokenStream tokens,
                                      AstNode parentNode)
                               throws ParsingException
Parses DDL GRANT statement AstNode based on SQL 92 specifications.

Parameters:
tokens - the DdlTokenStream representing the tokenized DDL content; may not be null
parentNode - the parent AstNode node; may not be null
Returns:
the AstNode
Throws:
ParsingException

parseGrantPrivileges

protected void parseGrantPrivileges(DdlTokenStream tokens,
                                    List<AstNode> privileges)
                             throws ParsingException
Throws:
ParsingException

parseRevokeStatement

protected AstNode parseRevokeStatement(DdlTokenStream tokens,
                                       AstNode parentNode)
                                throws ParsingException
Throws:
ParsingException

parseCreateDomainStatement

protected AstNode parseCreateDomainStatement(DdlTokenStream tokens,
                                             AstNode parentNode)
                                      throws ParsingException
Parses DDL CREATE DOMAIN AstNode based on SQL 92 specifications.

Parameters:
tokens - the DdlTokenStream representing the tokenized DDL content; may not be null
parentNode - the parent AstNode node; may not be null
Returns:
the parsed statement node AstNode
Throws:
ParsingException

parseCreateCollationStatement

protected AstNode parseCreateCollationStatement(DdlTokenStream tokens,
                                                AstNode parentNode)
                                         throws ParsingException
Parses DDL CREATE COLLATION AstNode based on SQL 92 specifications.

Parameters:
tokens - the DdlTokenStream representing the tokenized DDL content; may not be null
parentNode - the parent AstNode node; may not be null
Returns:
the parsed statement node AstNode
Throws:
ParsingException

parseCreateTranslationStatement

protected AstNode parseCreateTranslationStatement(DdlTokenStream tokens,
                                                  AstNode parentNode)
                                           throws ParsingException
Parses DDL CREATE TRANSLATION AstNode based on SQL 92 specifications.

Parameters:
tokens - the DdlTokenStream representing the tokenized DDL content; may not be null
parentNode - the parent AstNode node; may not be null
Returns:
the parsed statement node AstNode
Throws:
ParsingException

parseCreateCharacterSetStatement

protected AstNode parseCreateCharacterSetStatement(DdlTokenStream tokens,
                                                   AstNode parentNode)
                                            throws ParsingException
Parses DDL CREATE CHARACTER SET AstNode based on SQL 92 specifications.

Parameters:
tokens - the DdlTokenStream representing the tokenized DDL content; may not be null
parentNode - the parent AstNode node; may not be null
Returns:
the parsed statement node AstNode
Throws:
ParsingException

parseCustomStatement

protected AstNode parseCustomStatement(DdlTokenStream tokens,
                                       AstNode parentNode)
                                throws ParsingException
Catch-all method to parse unknown (not registered or handled by sub-classes) statements.

Parameters:
tokens - the DdlTokenStream representing the tokenized DDL content; may not be null
parentNode - the parent AstNode node; may not be null
Returns:
the AstNode
Throws:
ParsingException

parseCreateTableStatement

protected AstNode parseCreateTableStatement(DdlTokenStream tokens,
                                            AstNode parentNode)
                                     throws ParsingException
Parses DDL CREATE TABLE AstNode based on SQL 92 specifications.

Parameters:
tokens - the DdlTokenStream representing the tokenized DDL content; may not be null
parentNode - the parent AstNode node; may not be null
Returns:
the parsed CREATE TABLE AstNode
Throws:
ParsingException

parseCreateTableOptions

protected void parseCreateTableOptions(DdlTokenStream tokens,
                                       AstNode tableNode)
                                throws ParsingException
Throws:
ParsingException

parseNextCreateTableOption

protected void parseNextCreateTableOption(DdlTokenStream tokens,
                                          AstNode tableNode)
                                   throws ParsingException
Throws:
ParsingException

areNextTokensCreateTableOptions

protected boolean areNextTokensCreateTableOptions(DdlTokenStream tokens)
                                           throws ParsingException
Throws:
ParsingException

parseColumnsAndConstraints

protected void parseColumnsAndConstraints(DdlTokenStream tokens,
                                          AstNode tableNode)
                                   throws ParsingException
Utility method to parse columns and table constraints within either a CREATE TABLE statement. Method first parses and copies the text enclosed within the bracketed "( xxxx )" statement. Then the individual column definition or table constraint definition sub-statements are parsed assuming they are comma delimited.

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

parseColumnDefinition

protected void parseColumnDefinition(DdlTokenStream tokens,
                                     AstNode tableNode,
                                     boolean isAlterTable)
                              throws ParsingException
Utility method to parse the actual column definition. SQL-92 Structural Specification ::= { | } [ ] [ ... ] [ ]

Parameters:
tokens - the DdlTokenStream representing the tokenized DDL content; may not be null
tableNode -
isAlterTable - true if in-line constraint is part of add column in alter table statement
Throws:
ParsingException

parseSingleTerminatedColumnDefinition

protected void parseSingleTerminatedColumnDefinition(DdlTokenStream tokens,
                                                     AstNode tableNode,
                                                     boolean isAlterTable)
                                              throws ParsingException
Utility method to parse the actual column definition. SQL-92 Structural Specification ::= { | } [ ] [ ... ] [ ]

Parameters:
tokens - the DdlTokenStream representing the tokenized DDL content; may not be null
tableNode - the alter or create table statement node; may not be null
isAlterTable - true if in-line constraint is part of add column in alter table statement
Throws:
ParsingException

getTableElementsString

protected String getTableElementsString(DdlTokenStream tokens,
                                        boolean useTerminator)
                                 throws ParsingException
Method which extracts the table element string from a CREATE TABLE statement.

Parameters:
tokens - the DdlTokenStream representing the tokenized DDL content; may not be null
useTerminator -
Returns:
the parsed table elements String.
Throws:
ParsingException

consumeParenBoundedTokens

protected String consumeParenBoundedTokens(DdlTokenStream tokens,
                                           boolean includeParens)
                                    throws ParsingException
Simple method which parses, consumes and returns a string representing text found between parenthesis (i.e. '()') If parents don't exist, method returns NULL;

Parameters:
tokens - the DdlTokenStream representing the tokenized DDL content; may not be null
includeParens -
Returns:
the parenthesis bounded text or null if no parens.
Throws:
ParsingException

parseColumnConstraint

protected boolean parseColumnConstraint(DdlTokenStream tokens,
                                        AstNode columnNode,
                                        boolean isAlterTable)
                                 throws ParsingException
Parses an in-line column constraint including NULLABLE value, UNIQUE, PRIMARY KEY and REFERENCES to a Foreign Key. The values for the constraint are set as properties on the input columnNode.

Parameters:
tokens - the DdlTokenStream representing the tokenized DDL content; may not be null
columnNode - the column definition being created; may not be null
isAlterTable - true if in-line constraint is part of add column in alter table statement
Returns:
true if parsed a constraint, else false.
Throws:
ParsingException

parseTableConstraint

protected void parseTableConstraint(DdlTokenStream tokens,
                                    AstNode tableNode,
                                    boolean isAlterTable)
                             throws ParsingException
Parses full table constraint definition including the "CONSTRAINT" token Examples: CONSTRAINT P_KEY_2a UNIQUE (PERMISSIONUID)

Parameters:
tokens - the DdlTokenStream representing the tokenized DDL content; may not be null
tableNode -
isAlterTable - true if in-line constraint is part of add column in alter table statement
Throws:
ParsingException

parseConstraintAttributes

protected void parseConstraintAttributes(DdlTokenStream tokens,
                                         AstNode constraintNode)
                                  throws ParsingException
Parses the attributes associated with any in-line column constraint definition or a table constrain definition.

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

parseReferences

protected void parseReferences(DdlTokenStream tokens,
                               AstNode constraintNode)
                        throws ParsingException
Throws:
ParsingException

parseCreateViewStatement

protected AstNode parseCreateViewStatement(DdlTokenStream tokens,
                                           AstNode parentNode)
                                    throws ParsingException
Parses DDL CREATE VIEW AstNode basedregisterStatementStartPhrase on SQL 92 specifications. Initial implementation here does not parse the statement in detail.

Parameters:
tokens - the DdlTokenStream representing the tokenized DDL content; may not be null
parentNode - the parent AstNode node; may not be null
Returns:
the Create View node
Throws:
ParsingException

parseCreateSchemaStatement

protected AstNode parseCreateSchemaStatement(DdlTokenStream tokens,
                                             AstNode parentNode)
                                      throws ParsingException
Parses DDL CREATE SCHEMA AstNode based on SQL 92 specifications. Initial implementation here does not parse the statement in detail.

Parameters:
tokens - the DdlTokenStream representing the tokenized DDL content; may not be null
parentNode - the parent AstNode node; may not be null
Returns:
the parsed schema node
Throws:
ParsingException

parseCreateAssertionStatement

protected AstNode parseCreateAssertionStatement(DdlTokenStream tokens,
                                                AstNode parentNode)
                                         throws ParsingException
Parses DDL CREATE ASSERTION AstNode based on SQL 92 specifications. Initial implementation here does not parse the statement's search condition in detail.

Parameters:
tokens - the DdlTokenStream representing the tokenized DDL content; may not be null
parentNode - the parent AstNode node; may not be null
Returns:
the parsed schema node
Throws:
ParsingException

parseIgnorableStatement

protected AstNode parseIgnorableStatement(DdlTokenStream tokens,
                                          String name,
                                          AstNode parentNode)
Utility method to parse a statement that can be ignored. The value returned in the generic AstNode will contain all text between starting token and either the terminator (if defined) or the next statement start token. NOTE: This method does NOT mark and add consumed fragment to parent node.

Parameters:
tokens - the DdlTokenStream representing the tokenized DDL content; may not be null
name -
parentNode - the parent AstNode node; may not be null
Returns:
the parsed generic AstNode
Throws:
ParsingException

parseIgnorableStatement

protected AstNode parseIgnorableStatement(DdlTokenStream tokens,
                                          String name,
                                          AstNode parentNode,
                                          Name mixinType)
Utility method to parse a statement that can be ignored. The value returned in the generic AstNode will contain all text between starting token and either the terminator (if defined) or the next statement start token. NOTE: This method does NOT mark and add consumed fragment to parent node.

Parameters:
tokens - the DdlTokenStream representing the tokenized DDL content; may not be null
name -
parentNode - the parent AstNode node; may not be null
mixinType -
Returns:
the parsed generic AstNode
Throws:
ParsingException

parseStatement

protected AstNode parseStatement(DdlTokenStream tokens,
                                 String[] stmt_start_phrase,
                                 AstNode parentNode,
                                 Name mixinType)
Utility method to parse a generic statement given a start phrase and statement mixin type.

Parameters:
tokens - the DdlTokenStream representing the tokenized DDL content; may not be null
stmt_start_phrase - the string array statement start phrase
parentNode - the parent of the newly created node.
mixinType - the mixin type of the newly created statement node
Returns:
the new node

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)

getTerminator

protected String getTerminator()
Returns current terminator

Returns:
terminator string value

setTerminator

protected boolean setTerminator(String terminator)
Parameters:
terminator - the string value used as the statement terminator for the ddl dialect
Returns:
if terminator was changed or not

getValidSchemaChildTypes

protected Name[] getValidSchemaChildTypes()

isComment

protected boolean isComment(DdlTokenStream tokens)
                     throws ParsingException
Checks if next token is of type comment.

Parameters:
tokens - the DdlTokenStream representing the tokenized DDL content; may not be null
Returns:
true if next token is a comment.
Throws:
ParsingException

consumeComment

protected boolean consumeComment(DdlTokenStream tokens)
                          throws ParsingException
Consumes an an end-of-line comment or in-line comment

Parameters:
tokens - the DdlTokenStream representing the tokenized DDL content; may not be null
Returns:
true if a comment was found and consumed
Throws:
ParsingException

isTableConstraint

protected boolean isTableConstraint(DdlTokenStream tokens)
                             throws ParsingException
This utility method provides this parser the ability to distinguish between a CreateTable Constraint and a ColumnDefinition Definition which are the only two statement segment types allowed within the CREATE TABLE parenthesis ( xxxxxx );

Parameters:
tokens - the DdlTokenStream representing the tokenized DDL content; may not be null
Returns:
is table constraint
Throws:
ParsingException

isColumnDefinitionStart

protected boolean isColumnDefinitionStart(DdlTokenStream tokens)
                                   throws ParsingException
This utility method provides this parser the ability to distinguish between a CreateTable Constrain and a ColumnDefinition Definition which are the only two statement segment types allowed within the CREATE TABLE parenthesis ( xxxxxx );

Parameters:
tokens - the DdlTokenStream representing the tokenized DDL content; may not be null
Returns:
is column definition start phrase
Throws:
ParsingException

getDataTypeStartWords

protected List<String> getDataTypeStartWords()
Returns a list of data type start words which can be used to help identify a column definition sub-statement.

Returns:
list of data type start words

getCustomDataTypeStartWords

protected List<String> getCustomDataTypeStartWords()
Returns a list of custom data type start words which can be used to help identify a column definition sub-statement. Sub-classes should override this method to contribute DB-specific data types.

Returns:
list of data type start words

parseName

protected String parseName(DdlTokenStream tokens)
Method to parse fully qualified schema, table and column names that are defined with '.' separator and optionally bracketed with square brackets Example: partsSchema.supplier Example: [partsSchema].[supplier]

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

consumeIdentifier

protected String consumeIdentifier(DdlTokenStream tokens)
                            throws ParsingException
Consumes an token identifier which can be of the form of a simple string or a double-quoted string.

Parameters:
tokens - the DdlTokenStream representing the tokenized DDL content; may not be null
Returns:
the identifier
Throws:
ParsingException

isTerminator

protected boolean isTerminator(DdlTokenStream tokens)
                        throws ParsingException
Utility method to determine if next token is a terminator.

Parameters:
tokens - the DdlTokenStream representing the tokenized DDL content; may not be null
Returns:
is terminator token
Throws:
ParsingException

parseColumnNameList

protected boolean parseColumnNameList(DdlTokenStream tokens,
                                      AstNode parentNode,
                                      Name referenceType)
Adds column reference nodes to a parent node. Returns true if column references added, false if not.

Parameters:
tokens - the DdlTokenStream representing the tokenized DDL content; may not be null
parentNode - the parent node
referenceType - the type of the reference node to create
Returns:
true if the column references were found and added to the node, or false if there were no column references found in the stream

parseColumnNameList

protected List<String> parseColumnNameList(DdlTokenStream tokens)
                                    throws ParsingException
Parses a comma separated list of column names.

Parameters:
tokens - the DdlTokenStream representing the tokenized DDL content; may not be null
Returns:
list of column names.
Throws:
ParsingException

parseUntilTerminator

protected String parseUntilTerminator(DdlTokenStream tokens)
                               throws ParsingException
Utility method which parses tokens until a terminator is found, another statement is identified or there are no more tokens.

Parameters:
tokens - the DdlTokenStream representing the tokenized DDL content; may not be null
Returns:
the parsed string
Throws:
ParsingException

parseUntilTerminatorIgnoreEmbeddedStatements

protected String parseUntilTerminatorIgnoreEmbeddedStatements(DdlTokenStream tokens)
                                                       throws ParsingException
Utility method which parses tokens until a terminator is found or there are no more tokens. This method differs from parseUntilTermintor() in that it ignores embedded statements. This method can be used for parsers that have statements which can embed statements that should not be parsed.

Parameters:
tokens - the DdlTokenStream representing the tokenized DDL content; may not be null
Returns:
the parsed string
Throws:
ParsingException

parseUntilSemiColon

protected String parseUntilSemiColon(DdlTokenStream tokens)
                              throws ParsingException
Utility method which parses tokens until a semicolon is found or there are no more tokens.

Parameters:
tokens - the DdlTokenStream representing the tokenized DDL content; may not be null
Returns:
the parsed string
Throws:
ParsingException

parseUntilCommaOrTerminator

protected String parseUntilCommaOrTerminator(DdlTokenStream tokens)
                                      throws ParsingException
Throws:
ParsingException

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)

parseDefaultClause

protected boolean parseDefaultClause(DdlTokenStream tokens,
                                     AstNode columnNode)
                              throws ParsingException
Parses the default clause for a column and sets appropriate properties on the column node.

Parameters:
tokens - the DdlTokenStream representing the tokenized DDL content; may not be null
columnNode - the column node which may contain a default clause; may not be null
Returns:
true if default clause was found and parsed, otherwise false
Throws:
ParsingException

parseCollateClause

protected boolean parseCollateClause(DdlTokenStream tokens,
                                     AstNode columnNode)
                              throws ParsingException
Parses the default clause for a column and sets appropriate properties on the column node.

Parameters:
tokens - the DdlTokenStream representing the tokenized DDL content; may not be null
columnNode - the column node which may contain a collate clause; may not be null
Returns:
true if collate clause was found and parsed else return false.
Throws:
ParsingException

integer

protected int integer(String value)
Returns the integer value of the input string. Handles both straight integer string or complex KMG (CLOB or BLOB) value. Throws NumberFormatException if a valid integer is not found.

Parameters:
value - the string to be parsed; may not be null and length must be > 0;
Returns:
integer value

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 -

testPrint

protected void testPrint(String str)

isTestMode

public boolean isTestMode()
Returns:
testMode

setTestMode

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

getId

public String getId()
Get the identifier for this parser.

Specified by:
getId in interface DdlParser
Returns:
the parser's identifier; never null
See Also:
DdlParser.getId()

hashCode

public int hashCode()

Overrides:
hashCode in class Object
See Also:
Object.hashCode()

equals

public boolean equals(Object obj)

Overrides:
equals in class Object
See Also:
Object.equals(java.lang.Object)


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